a331124dbba2e1e3ff9ccd23423c66658b93170c
4 * Rfc822Header.class.php
6 * Copyright (c) 2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions needed to handle mime messages.
16 * input: header_string or array
38 $more_headers = array(); /* only needed for constructing headers
40 function parseHeader($hdr) {
42 $hdr = implode('', $hdr);
44 /* First we unfold the header */
45 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
47 /* Now we can make a new header array with */
48 /* each element representing a headerline */
49 $hdr = explode("\r\n" , $hdr);
50 foreach ($hdr as $line) {
51 $pos = strpos($line, ':');
53 $field = substr($line, 0, $pos);
54 if (!strstr($field,' ')) { /* valid field */
55 $value = trim(substr($line, $pos+
1));
56 $this->parseField($field, $value);
60 if ($this->content_type
== '') {
61 $this->parseContentType('text/plain; charset=us-ascii');
65 function stripComments($value) {
67 $cnt = strlen($value);
68 for ($i = 0; $i < $cnt; ++
$i) {
72 while ((++
$i < $cnt) && ($value{$i} != '"')) {
73 if ($value{$i} == '\\') {
77 $result .= $value{$i};
79 $result .= $value{$i};
83 while (($depth > 0) && (++
$i < $cnt)) {
100 $result .= $value{$i};
107 function parseField($field, $value) {
108 $field = strtolower($field);
111 $value = $this->stripComments($value);
112 $d = strtr($value, array(' ' => ' '));
113 $d = explode(' ', $d);
114 $this->date
= getTimeStamp($d);
117 $this->subject
= $value;
120 $this->from
= $this->parseAddress($value,true);
123 $this->sender
= $this->parseAddress($value);
126 $this->reply_to
= $this->parseAddress($value, true);
129 $this->to
= $this->parseAddress($value, true);
132 $this->cc
= $this->parseAddress($value, true);
135 $this->bcc
= $this->parseAddress($value, true);
138 $this->in_reply_to
= $value;
141 $value = $this->stripComments($value);
142 $this->message_id
= $value;
145 $value = $this->stripComments($value);
146 $this->references
= $value;
148 case 'x-confirm-reading-to':
149 case 'return-receipt-to':
150 case 'disposition-notification-to':
151 $value = $this->stripComments($value);
152 $this->dnt
= $this->parseAddress($value);
155 $value = $this->stripComments($value);
156 $value = str_replace(' ', '', $value);
157 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
160 $value = $this->stripComments($value);
161 $this->parseContentType($value);
163 case 'content-disposition':
164 $value = $this->stripComments($value);
165 $this->parseDisposition($value);
169 $this->xmailer
= $value;
172 $this->priority
= $value;
175 $value = $this->stripComments($value);
176 $this->mlist('post', $value);
179 $value = $this->stripComments($value);
180 $this->mlist('reply', $value);
182 case 'list-subscribe':
183 $value = $this->stripComments($value);
184 $this->mlist('subscribe', $value);
186 case 'list-unsubscribe':
187 $value = $this->stripComments($value);
188 $this->mlist('unsubscribe', $value);
191 $value = $this->stripComments($value);
192 $this->mlist('archive', $value);
195 $value = $this->stripComments($value);
196 $this->mlist('owner', $value);
199 $value = $this->stripComments($value);
200 $this->mlist('help', $value);
203 $value = $this->stripComments($value);
204 $this->mlist('id', $value);
211 * parseAddress: recursive function for parsing address strings and store
212 * them in an address stucture object.
213 * input: $address = string
214 * $ar = boolean (return array instead of only the
216 * $addr_ar = array with parsed addresses
218 * $host = string (default domainname in case of
219 * addresses without a domainname)
220 * $lookup = callback function (for lookup address
221 * strings which are probably nicks
223 * output: array with addressstructure objects or only one
224 * address_structure object.
225 * personal name: encoded: =?charset?Q|B?string?=
228 * email : <mailbox@host>
230 * This function is also used for validating addresses returned from compose
231 * That's also the reason that the function became a little bit huge and horrible
232 * Todo: Find a way to clean up this mess a bit (Marc Groot Koerkamp)
234 function parseAddress
235 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
237 $name = $addr = $comment = $is_encoded = '';
239 * in case of 8 bit addresses some how <SPACE> is represented as
241 * This only happens when we validate addresses from the compose form.
243 * Note: when other charsets have dificulties with characters
245 * then we should find out the value for those characters ans replace
246 * them by proper ASCII values before we start parsing.
249 $address = str_replace("\240",' ',$address);
251 $address = trim($address);
252 $j = strlen($address);
255 $char = $address{$pos};
259 /* get the encoded personal name */
260 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',substr($address,$pos),$reg)) {
262 $pos +
= strlen($reg[1]);
269 case '"': /* get the personal name */
270 //$name .= parseString($address,$pos);
271 $start_encoded = $pos;
273 if ($address{$pos} == '"') {
276 $personal_start = $personal_end = $pos;
278 $personal_end = strpos($address,'"',$pos);
279 if (($personal_end-2)>0 && (substr($address,$personal_end-2,2) === '\\"' ||
280 substr($address,$personal_end-2,2) === '\\\\')) {
281 $pos = $personal_end+
1;
283 $name .= substr($address,$personal_start,$personal_end-$personal_start);
288 $pos = $personal_end+
1;
295 case '<': /* get email address */
297 $addr_end = strpos($address,'>',$addr_start);
298 /* check for missing '>' */
299 if ($addr_end === false) {
302 $addr = substr($address,$addr_start+
1,$addr_end-$addr_start-1);
306 $addr = substr($address,$addr_start+
1);
310 case '(': /* rip off comments */
311 $comment_start = $pos;
312 $pos = strpos($address,')');
313 if ($pos !== false) {
314 $comment = substr($address, $comment_start+
1,($pos-$comment_start-1));
315 $address_start = substr($address, 0, $comment_start);
316 $address_end = substr($address, $pos +
1);
317 $address = $address_start . $address_end;
319 $j = strlen($address);
320 if ($comment_start) {
321 $pos = $comment_start-1;
328 $address = substr($address, 0, $pos - 1);
332 case ',': /* we reached a delimiter */
333 if (!$name && !$addr) {
334 $addr = substr($address, 0, $pos);
336 $addr = trim(substr($address, $addr_start, $pos));
337 } else if ($name == '') {
338 $name = trim(substr($address, 0, $addr_start));
340 $at = strpos($addr, '@');
341 $addr_structure = new AddressStructure();
342 if (!$name && $comment) $name = $comment;
344 $addr_structure->personal
= encodeHeader($name);
346 $addr_structure->personal
= $name;
349 $addr_structure->group
= $group;
350 $grouplookup = false;
352 $addr_structure->mailbox
= substr($addr, 0, $at);
353 $addr_structure->host
= substr($addr, $at+
1);
355 /* if lookup function */
357 $aAddr = call_user_func_array($lookup,array($addr));
358 if (isset($aAddr['email'])) {
359 if (strpos($aAddr['email'],',')) {
361 $addr_ar = $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
363 $at = strpos($aAddr['email'], '@');
364 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
365 $addr_structure->host
= substr($aAddr['email'], $at+
1);
366 if (isset($aAddr['name'])) {
367 $addr_structure->personal
= $aAddr['name'];
369 $addr_structure->personal
= encodeHeader($addr);
374 if (!$grouplookup && !$addr_structure->mailbox
) {
375 $addr_structure->mailbox
= trim($addr);
377 $addr_structure->host
= $host;
381 $address = trim(substr($address, $pos+
1));
382 $j = strlen($address);
387 $addr_ar[] = $addr_structure;
390 case ':': /* process the group addresses */
392 $group = substr($address, 0, $pos);
393 $address = substr($address, $pos+
1);
394 $result = $this->parseAddress($address, $ar, $addr_ar, $group, $lookup);
395 $addr_ar = $result[0];
397 $address = substr($address, $pos++
);
398 $j = strlen($address);
406 * this happens in the folowing situations :
407 * 1: unquoted personal name
408 * 2: emailaddress without < and >
409 * 3: unquoted personal name from compose that should be encoded.
410 * if it's a personal name then an emailaddress should follow
411 * the personal name may not have ',' inside it
412 * If it's a emailaddress then the personal name is not set.
413 * we should look for the delimiter ',' or a SPACE
415 /* check for emailaddress */
417 /* Blah, this code sucks */
419 /* we need an tokenizer !!!!!!!! */
421 $i_space = strpos($address,' ',$pos);
422 $i_del = strpos($address,',',$pos);
423 if ($i_space ||
$i_del) {
424 if ($i_del) { /* extract the stringpart before the delimiter */
425 $address_part = substr($address,$pos,$i_del-$pos);
426 } else { /* extract the stringpart started with pos */
427 $address_part = substr($address,$pos);
429 if ($i = strpos($address_part,'@')) {
430 /* an email address is following */
431 if (($i+
$pos) < $i_space) {
433 /* multiple addresses are following */
434 if ($i_space < $i_del && $i_del) {
435 /* <space> is present */
437 if ($i = strpos($address_part,'<')) {
438 $name .= substr($address_part,0,$i);
441 $addr = substr($address,$pos,$i_space-$pos);
444 } else { /* no <space> $i_space === false */
445 if ($i = strpos($address_part,'<')) {
446 $name .= substr($address_part,0,$i);
449 $addr = substr($address,$pos);
453 } else { /* <space> is available in the next address */
454 /* OR no delimiter and <space> */
456 /* check for < > addresses */
457 if ($i = strpos($address_part,'<')) {
458 $name .= substr($address_part,0,$i);
461 $addr = substr($address,$pos,$i_del-$pos);
465 } else if ($i_space) { /* can never happen ? */
466 if ($i = strpos($address_part,'<')) {
467 $name .= substr($address_part,0,$i);
470 $addr = substr($address,$pos,$i_space-$pos);
473 } else { /* can never happen */
474 $addr = substr($address,$pos);
478 } else { /* <space> is located after the user@domain part */
479 /* or no <space> present */
481 if ($i = strpos($address_part,'<')) {
482 $name .= substr($address_part,0,$i);
485 $name .= substr($address,$pos,$i_space-$pos) . ' ';
486 $addr_start = $i_space+
1;
489 } else { /* no <space> */
490 $addr = substr($address,$pos,$i_del-$pos);
494 } else { /* can never happen. REMOVE */
500 /* email address without domain name, could be an alias */
502 /* FIXME check for comments */
503 $addr = $address_part;
504 $pos = strlen($address_part) +
$pos;
507 /* check for < > addresses */
508 if ($i = strpos($address,'<')) {
509 $name .= substr($address,$pos,$i-$pos);
512 /* FIXME check for comments */
513 $addr = substr($address,$pos);
521 if (!$name && !$addr) {
522 $addr = substr($address, 0, $pos);
524 $addr = trim(substr($address, $addr_start, $pos));
525 } else if ($name == '') {
526 $name = trim(substr($address, 0, $addr_start));
528 if (!$name && $comment) {
530 } else if ($name && $comment) {
531 $name = $name .' ('.$comment.')';
533 $at = strpos($addr, '@');
534 $addr_structure = new AddressStructure();
535 $addr_structure->group
= $group;
537 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
538 $addr_structure->host
= trim(substr($addr, $at+
1));
540 /* if lookup function */
542 $aAddr = call_user_func_array($lookup,array($addr));
543 if (isset($aAddr['email'])) {
544 if (strpos($aAddr['email'],',')) {
545 return $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
547 $at = strpos($aAddr['email'], '@');
548 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
549 $addr_structure->host
= substr($aAddr['email'], $at+
1);
550 if (isset($aAddr['name']) && $aAddr['name']) {
551 $name = $aAddr['name'];
558 if (!$addr_structure->mailbox
) {
559 $addr_structure->mailbox
= trim($addr);
561 $addr_structure->host
= $host;
566 if (!$is_encoded && !$group) {
567 $name = encodeHeader($name);
569 if ($group && $addr == '') { /* no addresses found in group */
571 $addr_structure->personal
= $name;
572 $addr_ar[] = $addr_structure;
573 return (array($addr_ar,$pos+
1 ));
575 $addr_structure->personal
= $name;
576 $addr_ar[] = $addr_structure;
577 return (array($addr_ar,$pos+
1 ));
579 $addr_structure->personal
= $name;
580 if ($name ||
$addr) {
581 $addr_ar[] = $addr_structure;
587 return ($addr_ar[0]);
590 function parseContentType($value) {
591 $pos = strpos($value, ';');
594 $type = trim(substr($value, 0, $pos));
595 $props = trim(substr($value, $pos+
1));
599 $content_type = new ContentType($type);
601 $properties = $this->parseProperties($props);
602 if (!isset($properties['charset'])) {
603 $properties['charset'] = 'us-ascii';
605 $content_type->properties
= $this->parseProperties($props);
607 $this->content_type
= $content_type;
610 function parseProperties($value) {
611 $propArray = explode(';', $value);
612 $propResultArray = array();
613 foreach ($propArray as $prop) {
615 $pos = strpos($prop, '=');
617 $key = trim(substr($prop, 0, $pos));
618 $val = trim(substr($prop, $pos+
1));
619 if ($val{0} == '"') {
620 $val = substr($val, 1, -1);
622 $propResultArray[$key] = $val;
625 return $propResultArray;
628 function parseDisposition($value) {
629 $pos = strpos($value, ';');
632 $name = trim(substr($value, 0, $pos));
633 $props = trim(substr($value, $pos+
1));
637 $props_a = $this->parseProperties($props);
638 $disp = new Disposition($name);
639 $disp->properties
= $props_a;
640 $this->disposition
= $disp;
643 function mlist($field, $value) {
645 $value_a = explode(',', $value);
646 foreach ($value_a as $val) {
648 if ($val{0} == '<') {
649 $val = substr($val, 1, -1);
651 if (substr($val, 0, 7) == 'mailto:') {
652 $res_a['mailto'] = substr($val, 7);
654 $res_a['href'] = $val;
657 $this->mlist
[$field] = $res_a;
661 * function to get the addres strings out of the header.
662 * Arguments: string or array of strings !
663 * example1: header->getAddr_s('to').
664 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
666 function getAddr_s($arr, $separator = ',',$encoded=false) {
669 if (is_array($arr)) {
670 foreach($arr as $arg) {
671 if ($this->getAddr_s($arg, $separator, $encoded)) {
672 $s .= $separator . $result;
675 $s = ($s ?
substr($s, 2) : $s);
677 $addr = $this->{$arr};
678 if (is_array($addr)) {
679 foreach ($addr as $addr_o) {
680 if (is_object($addr_o)) {
682 $s .= $addr_o->getEncodedAddress() . $separator;
684 $s .= $addr_o->getAddress() . $separator;
688 $s = substr($s, 0, -strlen($separator));
690 if (is_object($addr)) {
692 $s .= $addr->getEncodedAddress();
694 $s .= $addr->getAddress();
702 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
703 if (is_array($arg)) {
704 foreach($arg as $argument) {
705 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
708 $addr = $this->{$arg};
709 if (is_array($addr)) {
710 foreach ($addr as $next_addr) {
711 if (is_object($next_addr)) {
712 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
713 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
715 $email = $next_addr->mailbox
;
717 $email = strtolower($email);
718 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
719 $arr[$email] = $next_addr->personal
;
724 if (is_object($addr)) {
725 $email = $addr->mailbox
;
726 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
727 $email = strtolower($email);
728 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
729 $arr[$email] = $addr->personal
;
737 function findAddress($address, $recurs = false) {
739 if (is_array($address)) {
741 foreach($address as $argument) {
742 $match = $this->findAddress($argument, true);
747 if (count($match[0]) && !$result) {
754 if (!is_array($this->cc
)) $this->cc
= array();
755 $srch_addr = $this->parseAddress($address);
757 foreach ($this->to
as $to) {
758 if ($to->host
== $srch_addr->host
) {
759 if ($to->mailbox
== $srch_addr->mailbox
) {
760 $results[] = $srch_addr;
761 if ($to->personal
== $srch_addr->personal
) {
763 return array($results, true);
771 foreach ($this->cc
as $cc) {
772 if ($cc->host
== $srch_addr->host
) {
773 if ($cc->mailbox
== $srch_addr->mailbox
) {
774 $results[] = $srch_addr;
775 if ($cc->personal
== $srch_addr->personal
) {
777 return array($results, true);
786 return array($results, false);
787 } elseif (count($result)) {
797 function getContentType($type0, $type1) {
798 $type0 = $this->content_type
->type0
;
799 $type1 = $this->content_type
->type1
;
800 return $this->content_type
->properties
;