91bd6267610a32fc04c1279fc83fbf9539cbffa6
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 if (strpos($address,';',$pos)) {
393 $group = substr($address, 0, $pos);
394 $address = substr($address, $pos+
1);
395 $result = $this->parseAddress($address, $ar, $addr_ar, $group, $lookup);
396 $addr_ar = $result[0];
398 $address = substr($address, $pos++
);
399 $j = strlen($address);
410 * this happens in the folowing situations :
411 * 1: unquoted personal name
412 * 2: emailaddress without < and >
413 * 3: unquoted personal name from compose that should be encoded.
414 * if it's a personal name then an emailaddress should follow
415 * the personal name may not have ',' inside it
416 * If it's a emailaddress then the personal name is not set.
417 * we should look for the delimiter ',' or a SPACE
419 /* check for emailaddress */
421 /* Blah, this code sucks */
423 /* we need an tokenizer !!!!!!!! */
425 $i_space = strpos($address,' ',$pos);
426 $i_del = strpos($address,',',$pos);
427 if ($i_space ||
$i_del) {
428 if ($i_del) { /* extract the stringpart before the delimiter */
429 $address_part = substr($address,$pos,$i_del-$pos);
430 } else { /* extract the stringpart started with pos */
431 $address_part = substr($address,$pos);
433 if ($i = strpos($address_part,'@')) {
434 /* an email address is following */
435 if (($i+
$pos) < $i_space) {
437 /* multiple addresses are following */
438 if ($i_space < $i_del && $i_del) {
439 /* <space> is present */
441 if ($i = strpos($address_part,'<')) {
442 $name .= substr($address_part,0,$i);
445 $addr = substr($address,$pos,$i_space-$pos);
448 } else { /* no <space> $i_space === false */
449 if ($i = strpos($address_part,'<')) {
450 $name .= substr($address_part,0,$i);
453 $addr = substr($address,$pos);
457 } else { /* <space> is available in the next address */
458 /* OR no delimiter and <space> */
460 /* check for < > addresses */
461 if ($i = strpos($address_part,'<')) {
462 $name .= substr($address_part,0,$i);
465 $addr = substr($address,$pos,$i_del-$pos);
469 } else if ($i_space) { /* can never happen ? */
470 if ($i = strpos($address_part,'<')) {
471 $name .= substr($address_part,0,$i);
474 $addr = substr($address,$pos,$i_space-$pos);
477 } else { /* can never happen */
478 $addr = substr($address,$pos);
482 } else { /* <space> is located after the user@domain part */
483 /* or no <space> present */
485 if ($i = strpos($address_part,'<')) {
486 $name .= substr($address_part,0,$i);
489 $name .= substr($address,$pos,$i_space-$pos) . ' ';
490 $addr_start = $i_space+
1;
493 } else { /* no <space> */
494 $addr = substr($address,$pos,$i_del-$pos);
498 } else { /* can never happen. REMOVE */
504 /* email address without domain name, could be an alias */
506 /* FIXME check for comments */
507 $addr = $address_part;
508 $pos = strlen($address_part) +
$pos;
511 /* check for < > addresses */
512 if ($i = strpos($address,'<')) {
513 $name .= substr($address,$pos,$i-$pos);
516 /* FIXME check for comments */
517 $addr = substr($address,$pos);
525 if (!$name && !$addr) {
526 $addr = substr($address, 0, $pos);
528 $addr = trim(substr($address, $addr_start, $pos));
529 } else if ($name == '') {
530 $name = trim(substr($address, 0, $addr_start));
532 if (!$name && $comment) {
534 } else if ($name && $comment) {
535 $name = $name .' ('.$comment.')';
537 $at = strpos($addr, '@');
538 $addr_structure = new AddressStructure();
539 $addr_structure->group
= $group;
541 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
542 $addr_structure->host
= trim(substr($addr, $at+
1));
544 /* if lookup function */
546 $aAddr = call_user_func_array($lookup,array($addr));
547 if (isset($aAddr['email'])) {
548 if (strpos($aAddr['email'],',')) {
549 return $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
551 $at = strpos($aAddr['email'], '@');
552 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
553 $addr_structure->host
= substr($aAddr['email'], $at+
1);
554 if (isset($aAddr['name']) && $aAddr['name']) {
555 $name = $aAddr['name'];
562 if (!$addr_structure->mailbox
) {
563 $addr_structure->mailbox
= trim($addr);
565 $addr_structure->host
= $host;
570 if (!$is_encoded && !$group) {
571 $name = encodeHeader($name);
573 if ($group && $addr == '') { /* no addresses found in group */
575 $addr_structure->personal
= $name;
576 $addr_ar[] = $addr_structure;
577 return (array($addr_ar,$pos+
1 ));
579 $addr_structure->personal
= $name;
580 $addr_ar[] = $addr_structure;
581 return (array($addr_ar,$pos+
1 ));
583 $addr_structure->personal
= $name;
584 if ($name ||
$addr) {
585 $addr_ar[] = $addr_structure;
591 return ($addr_ar[0]);
594 function parseContentType($value) {
595 $pos = strpos($value, ';');
598 $type = trim(substr($value, 0, $pos));
599 $props = trim(substr($value, $pos+
1));
603 $content_type = new ContentType($type);
605 $properties = $this->parseProperties($props);
606 if (!isset($properties['charset'])) {
607 $properties['charset'] = 'us-ascii';
609 $content_type->properties
= $this->parseProperties($props);
611 $this->content_type
= $content_type;
614 function parseProperties($value) {
615 $propArray = explode(';', $value);
616 $propResultArray = array();
617 foreach ($propArray as $prop) {
619 $pos = strpos($prop, '=');
621 $key = trim(substr($prop, 0, $pos));
622 $val = trim(substr($prop, $pos+
1));
623 if ($val{0} == '"') {
624 $val = substr($val, 1, -1);
626 $propResultArray[$key] = $val;
629 return $propResultArray;
632 function parseDisposition($value) {
633 $pos = strpos($value, ';');
636 $name = trim(substr($value, 0, $pos));
637 $props = trim(substr($value, $pos+
1));
641 $props_a = $this->parseProperties($props);
642 $disp = new Disposition($name);
643 $disp->properties
= $props_a;
644 $this->disposition
= $disp;
647 function mlist($field, $value) {
649 $value_a = explode(',', $value);
650 foreach ($value_a as $val) {
652 if ($val{0} == '<') {
653 $val = substr($val, 1, -1);
655 if (substr($val, 0, 7) == 'mailto:') {
656 $res_a['mailto'] = substr($val, 7);
658 $res_a['href'] = $val;
661 $this->mlist
[$field] = $res_a;
665 * function to get the addres strings out of the header.
666 * Arguments: string or array of strings !
667 * example1: header->getAddr_s('to').
668 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
670 function getAddr_s($arr, $separator = ',',$encoded=false) {
673 if (is_array($arr)) {
674 foreach($arr as $arg) {
675 if ($this->getAddr_s($arg, $separator, $encoded)) {
676 $s .= $separator . $result;
679 $s = ($s ?
substr($s, 2) : $s);
681 $addr = $this->{$arr};
682 if (is_array($addr)) {
683 foreach ($addr as $addr_o) {
684 if (is_object($addr_o)) {
686 $s .= $addr_o->getEncodedAddress() . $separator;
688 $s .= $addr_o->getAddress() . $separator;
692 $s = substr($s, 0, -strlen($separator));
694 if (is_object($addr)) {
696 $s .= $addr->getEncodedAddress();
698 $s .= $addr->getAddress();
706 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
707 if (is_array($arg)) {
708 foreach($arg as $argument) {
709 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
712 $addr = $this->{$arg};
713 if (is_array($addr)) {
714 foreach ($addr as $next_addr) {
715 if (is_object($next_addr)) {
716 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
717 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
719 $email = $next_addr->mailbox
;
721 $email = strtolower($email);
722 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
723 $arr[$email] = $next_addr->personal
;
728 if (is_object($addr)) {
729 $email = $addr->mailbox
;
730 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
731 $email = strtolower($email);
732 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
733 $arr[$email] = $addr->personal
;
741 function findAddress($address, $recurs = false) {
743 if (is_array($address)) {
745 foreach($address as $argument) {
746 $match = $this->findAddress($argument, true);
751 if (count($match[0]) && !$result) {
758 if (!is_array($this->cc
)) $this->cc
= array();
759 $srch_addr = $this->parseAddress($address);
761 foreach ($this->to
as $to) {
762 if ($to->host
== $srch_addr->host
) {
763 if ($to->mailbox
== $srch_addr->mailbox
) {
764 $results[] = $srch_addr;
765 if ($to->personal
== $srch_addr->personal
) {
767 return array($results, true);
775 foreach ($this->cc
as $cc) {
776 if ($cc->host
== $srch_addr->host
) {
777 if ($cc->mailbox
== $srch_addr->mailbox
) {
778 $results[] = $srch_addr;
779 if ($cc->personal
== $srch_addr->personal
) {
781 return array($results, true);
790 return array($results, false);
791 } elseif (count($result)) {
801 function getContentType($type0, $type1) {
802 $type0 = $this->content_type
->type0
;
803 $type1 = $this->content_type
->type1
;
804 return $this->content_type
->properties
;