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 $addr = substr($address,$addr_start+
1,$addr_end-$addr_start-1);
302 $addr = substr($address,$addr_start+
1);
306 case '(': /* rip off comments */
307 $comment_start = $pos;
308 $pos = strpos($address,')');
309 if ($pos !== false) {
310 $comment = substr($address, $comment_start+
1,($pos-$comment_start-1));
311 $address_start = substr($address, 0, $comment_start);
312 $address_end = substr($address, $pos +
1);
313 $address = $address_start . $address_end;
315 $j = strlen($address);
316 if ($comment_start) {
317 $pos = $comment_start-1;
324 $address = substr($address, 0, $pos - 1);
328 case ',': /* we reached a delimiter */
329 if (!$name && !$addr) {
330 $addr = substr($address, 0, $pos);
332 $addr = trim(substr($address, $addr_start, $pos));
333 } else if ($name == '') {
334 $name = trim(substr($address, 0, $addr_start));
336 $at = strpos($addr, '@');
337 $addr_structure = new AddressStructure();
338 if (!$name && $comment) $name = $comment;
340 $addr_structure->personal
= encodeHeader($name);
342 $addr_structure->personal
= $name;
345 $addr_structure->group
= $group;
346 $grouplookup = false;
348 $addr_structure->mailbox
= substr($addr, 0, $at);
349 $addr_structure->host
= substr($addr, $at+
1);
351 /* if lookup function */
353 $aAddr = call_user_func_array($lookup,array($addr));
354 if (isset($aAddr['email'])) {
355 if (strpos($aAddr['email'],',')) {
357 $addr_ar = $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
359 $at = strpos($aAddr['email'], '@');
360 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
361 $addr_structure->host
= substr($aAddr['email'], $at+
1);
362 if (isset($aAddr['name'])) {
363 $addr_structure->personal
= $aAddr['name'];
365 $addr_structure->personal
= encodeHeader($addr);
370 if (!$grouplookup && !$addr_structure->mailbox
) {
371 $addr_structure->mailbox
= trim($addr);
373 $addr_structure->host
= $host;
377 $address = trim(substr($address, $pos+
1));
378 $j = strlen($address);
383 $addr_ar[] = $addr_structure;
386 case ':': /* process the group addresses */
388 $group = substr($address, 0, $pos);
389 $address = substr($address, $pos+
1);
390 $result = $this->parseAddress($address, $ar, $addr_ar, $group, $lookup);
391 $addr_ar = $result[0];
393 $address = substr($address, $pos++
);
394 $j = strlen($address);
402 * this happens in the folowing situations :
403 * 1: unquoted personal name
404 * 2: emailaddress without < and >
405 * 3: unquoted personal name from compose that should be encoded.
406 * if it's a personal name then an emailaddress should follow
407 * the personal name may not have ',' inside it
408 * If it's a emailaddress then the personal name is not set.
409 * we should look for the delimiter ',' or a SPACE
411 /* check for emailaddress */
412 $i_space = strpos($address,' ',$pos);
413 $i_del = strpos($address,',',$pos);
414 if ($i_space ||
$i_del) {
416 $address_part = substr($address,$pos,$i_del-$pos);
418 $address_part = substr($address,$pos);
420 if ($i = strpos($address_part,'@')) {
421 /* an email address is following */
422 if (($i+
$pos) < $i_space) {
424 if ($i_space < $i_del && $i_del) {
426 $addr = substr($address,$pos,$i_space-$pos);
429 $addr = substr($address,$pos);
434 $addr = substr($address,$pos,$i_del-$pos);
436 } else if ($i_space) {
437 $addr = substr($address,$pos,$i_space-$pos);
440 $addr = substr($address,$pos);
446 $name .= substr($address,$pos,$i_space-$pos) . ' ';
447 $addr_start = $i_space+
1;
450 $addr = substr($address,$pos,$i_del-$pos);
460 /* email address without domain name, could be an alias */
462 $addr = $address_part;
463 $pos = strlen($address_part) +
$pos;
466 $addr = substr($address,$pos);
473 if (!$name && !$addr) {
474 $addr = substr($address, 0, $pos);
476 $addr = trim(substr($address, $addr_start, $pos));
477 } else if ($name == '') {
478 $name = trim(substr($address, 0, $addr_start));
480 if (!$name && $comment) {
482 } else if ($name && $comment) {
483 $name = $name .' ('.$comment.')';
485 $at = strpos($addr, '@');
486 $addr_structure = new AddressStructure();
487 $addr_structure->group
= $group;
489 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
490 $addr_structure->host
= trim(substr($addr, $at+
1));
492 /* if lookup function */
494 $aAddr = call_user_func_array($lookup,array($addr));
495 if (isset($aAddr['email'])) {
496 if (strpos($aAddr['email'],',')) {
497 return $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
499 $at = strpos($aAddr['email'], '@');
500 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
501 $addr_structure->host
= substr($aAddr['email'], $at+
1);
502 if (isset($aAddr['name']) && $aAddr['name']) {
503 $name = $aAddr['name'];
510 if (!$addr_structure->mailbox
) {
511 $addr_structure->mailbox
= trim($addr);
513 $addr_structure->host
= $host;
518 if (!$is_encoded && !$group) {
519 $name = encodeHeader($name);
521 if ($group && $addr == '') { /* no addresses found in group */
523 $addr_structure->personal
= $name;
524 $addr_ar[] = $addr_structure;
525 return (array($addr_ar,$pos+
1 ));
527 $addr_structure->personal
= $name;
528 $addr_ar[] = $addr_structure;
529 return (array($addr_ar,$pos+
1 ));
531 $addr_structure->personal
= $name;
532 if ($name ||
$addr) {
533 $addr_ar[] = $addr_structure;
539 return ($addr_ar[0]);
542 function parseContentType($value) {
543 $pos = strpos($value, ';');
546 $type = trim(substr($value, 0, $pos));
547 $props = trim(substr($value, $pos+
1));
551 $content_type = new ContentType($type);
553 $properties = $this->parseProperties($props);
554 if (!isset($properties['charset'])) {
555 $properties['charset'] = 'us-ascii';
557 $content_type->properties
= $this->parseProperties($props);
559 $this->content_type
= $content_type;
562 function parseProperties($value) {
563 $propArray = explode(';', $value);
564 $propResultArray = array();
565 foreach ($propArray as $prop) {
567 $pos = strpos($prop, '=');
569 $key = trim(substr($prop, 0, $pos));
570 $val = trim(substr($prop, $pos+
1));
571 if ($val{0} == '"') {
572 $val = substr($val, 1, -1);
574 $propResultArray[$key] = $val;
577 return $propResultArray;
580 function parseDisposition($value) {
581 $pos = strpos($value, ';');
584 $name = trim(substr($value, 0, $pos));
585 $props = trim(substr($value, $pos+
1));
589 $props_a = $this->parseProperties($props);
590 $disp = new Disposition($name);
591 $disp->properties
= $props_a;
592 $this->disposition
= $disp;
595 function mlist($field, $value) {
597 $value_a = explode(',', $value);
598 foreach ($value_a as $val) {
600 if ($val{0} == '<') {
601 $val = substr($val, 1, -1);
603 if (substr($val, 0, 7) == 'mailto:') {
604 $res_a['mailto'] = substr($val, 7);
606 $res_a['href'] = $val;
609 $this->mlist
[$field] = $res_a;
613 * function to get the addres strings out of the header.
614 * Arguments: string or array of strings !
615 * example1: header->getAddr_s('to').
616 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
618 function getAddr_s($arr, $separator = ',',$encoded=false) {
621 if (is_array($arr)) {
622 foreach($arr as $arg) {
623 if ($this->getAddr_s($arg, $separator, $encoded)) {
624 $s .= $separator . $result;
627 $s = ($s ?
substr($s, 2) : $s);
629 $addr = $this->{$arr};
630 if (is_array($addr)) {
631 foreach ($addr as $addr_o) {
632 if (is_object($addr_o)) {
634 $s .= $addr_o->getEncodedAddress() . $separator;
636 $s .= $addr_o->getAddress() . $separator;
640 $s = substr($s, 0, -strlen($separator));
642 if (is_object($addr)) {
644 $s .= $addr->getEncodedAddress();
646 $s .= $addr->getAddress();
654 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
655 if (is_array($arg)) {
656 foreach($arg as $argument) {
657 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
660 $addr = $this->{$arg};
661 if (is_array($addr)) {
662 foreach ($addr as $next_addr) {
663 if (is_object($next_addr)) {
664 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
665 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
667 $email = $next_addr->mailbox
;
669 $email = strtolower($email);
670 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
671 $arr[$email] = $next_addr->personal
;
676 if (is_object($addr)) {
677 $email = $addr->mailbox
;
678 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
679 $email = strtolower($email);
680 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
681 $arr[$email] = $addr->personal
;
689 function findAddress($address, $recurs = false) {
691 if (is_array($address)) {
693 foreach($address as $argument) {
694 $match = $this->findAddress($argument, true);
699 if (count($match[0]) && !$result) {
706 if (!is_array($this->cc
)) $this->cc
= array();
707 $srch_addr = $this->parseAddress($address);
709 foreach ($this->to
as $to) {
710 if ($to->host
== $srch_addr->host
) {
711 if ($to->mailbox
== $srch_addr->mailbox
) {
712 $results[] = $srch_addr;
713 if ($to->personal
== $srch_addr->personal
) {
715 return array($results, true);
723 foreach ($this->cc
as $cc) {
724 if ($cc->host
== $srch_addr->host
) {
725 if ($cc->mailbox
== $srch_addr->mailbox
) {
726 $results[] = $srch_addr;
727 if ($cc->personal
== $srch_addr->personal
) {
729 return array($results, true);
738 return array($results, false);
739 } elseif (count($result)) {
749 function getContentType($type0, $type1) {
750 $type0 = $this->content_type
->type0
;
751 $type1 = $this->content_type
->type1
;
752 return $this->content_type
->properties
;