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);
45 /* First we unfold the header */
46 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
48 /* Now we can make a new header array with */
49 /* each element representing a headerline */
50 $hdr = explode("\r\n" , $hdr);
51 foreach ($hdr as $line) {
52 $pos = strpos($line, ':');
54 $field = substr($line, 0, $pos);
55 if (!strstr($field,' ')) { /* valid field */
56 $value = trim(substr($line, $pos+
1));
57 $this->parseField($field, $value);
61 if ($this->content_type
== '') {
62 $this->parseContentType('text/plain; charset=us-ascii');
66 function stripComments($value) {
68 $cnt = strlen($value);
69 for ($i = 0; $i < $cnt; ++
$i) {
73 while ((++
$i < $cnt) && ($value{$i} != '"')) {
74 if ($value{$i} == '\\') {
78 $result .= $value{$i};
80 $result .= $value{$i};
84 while (($depth > 0) && (++
$i < $cnt)) {
101 $result .= $value{$i};
108 function parseField($field, $value) {
109 $field = strtolower($field);
112 $value = $this->stripComments($value);
113 $d = strtr($value, array(' ' => ' '));
114 $d = explode(' ', $d);
115 $this->date
= getTimeStamp($d);
118 $this->subject
= $value;
121 $this->from
= $this->parseAddress($value,true);
124 $this->sender
= $this->parseAddress($value);
127 $this->reply_to
= $this->parseAddress($value, true);
130 $this->to
= $this->parseAddress($value, true);
133 $this->cc
= $this->parseAddress($value, true);
136 $this->bcc
= $this->parseAddress($value, true);
139 $this->in_reply_to
= $value;
142 $value = $this->stripComments($value);
143 $this->message_id
= $value;
146 $value = $this->stripComments($value);
147 $this->references
= $value;
149 case 'x-confirm-reading-to':
150 case 'return-receipt-to':
151 case 'disposition-notification-to':
152 $value = $this->stripComments($value);
153 $this->dnt
= $this->parseAddress($value);
156 $value = $this->stripComments($value);
157 $value = str_replace(' ', '', $value);
158 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
161 $value = $this->stripComments($value);
162 $this->parseContentType($value);
164 case 'content-disposition':
165 $value = $this->stripComments($value);
166 $this->parseDisposition($value);
170 $this->xmailer
= $value;
173 $this->priority
= $value;
176 $value = $this->stripComments($value);
177 $this->mlist('post', $value);
180 $value = $this->stripComments($value);
181 $this->mlist('reply', $value);
183 case 'list-subscribe':
184 $value = $this->stripComments($value);
185 $this->mlist('subscribe', $value);
187 case 'list-unsubscribe':
188 $value = $this->stripComments($value);
189 $this->mlist('unsubscribe', $value);
192 $value = $this->stripComments($value);
193 $this->mlist('archive', $value);
196 $value = $this->stripComments($value);
197 $this->mlist('owner', $value);
200 $value = $this->stripComments($value);
201 $this->mlist('help', $value);
204 $value = $this->stripComments($value);
205 $this->mlist('id', $value);
212 * parseAddress: recursive function for parsing address strings and store
213 * them in an address stucture object.
214 * input: $address = string
215 * $ar = boolean (return array instead of only the
217 * $addr_ar = array with parsed addresses
219 * $host = string (default domainname in case of
220 * addresses without a domainname)
221 * $lookup = callback function (for lookup address
222 * strings which are probably nicks
224 * output: array with addressstructure objects or only one
225 * address_structure object.
226 * personal name: encoded: =?charset?Q|B?string?=
229 * email : <mailbox@host>
231 * This function is also used for validating addresses returned from compose
232 * That's also the reason that the function became a little bit huge and horrible
233 * Todo: Find a way to clean up this mess a bit (Marc Groot Koerkamp)
235 function parseAddress
236 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
238 $name = $addr = $comment = $is_encoded = '';
240 * in case of 8 bit addresses some how <SPACE> is represented as
242 * This only happens when we validate addresses from the compose form.
244 * Note: when other charsets have dificulties with characters
246 * then we should find out the value for those characters ans replace
247 * them by proper ASCII values before we start parsing.
250 $address = str_replace("\240",' ',$address);
252 $address = trim($address);
253 $j = strlen($address);
256 $char = $address{$pos};
260 /* get the encoded personal name */
261 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',substr($address,$pos),$reg)) {
263 $pos +
= strlen($reg[1]);
270 case '"': /* get the personal name */
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 */
308 $pos = strpos($address,')');
309 if ($pos !== false) {
310 $comment = substr($address, $addr_start+
1,($pos-$addr_start-1));
311 $address_start = substr($address, 0, $addr_start);
312 $address_end = substr($address, $pos +
1);
313 $address = $address_start . $address_end;
315 $j = strlen($address);
316 $pos = $addr_start +
1;
318 case ',': /* we reached a delimiter */
319 if (!$name && !$addr) {
320 $addr = substr($address, 0, $pos);
322 $addr = trim(substr($address, $addr_start, $pos));
323 } else if ($name == '') {
324 $name = trim(substr($address, 0, $addr_start));
326 $at = strpos($addr, '@');
327 $addr_structure = new AddressStructure();
328 if (!$name && $comment) $name = $comment;
330 $addr_structure->personal
= encodeHeader($name);
332 $addr_structure->personal
= $name;
335 $addr_structure->group
= $group;
336 $grouplookup = false;
338 $addr_structure->mailbox
= substr($addr, 0, $at);
339 $addr_structure->host
= substr($addr, $at+
1);
341 /* if lookup function */
343 $aAddr = call_user_func_array($lookup,array($addr));
344 if (isset($aAddr['email'])) {
345 if (strpos($aAddr['email'],',')) {
347 $addr_ar = $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
349 $at = strpos($aAddr['email'], '@');
350 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
351 $addr_structure->host
= substr($aAddr['email'], $at+
1);
352 if (isset($aAddr['name'])) {
353 $addr_structure->personal
= $aAddr['name'];
355 $addr_structure->personal
= encodeHeader($addr);
360 if (!$grouplookup && !$addr_structure->mailbox
) {
361 $addr_structure->mailbox
= trim($addr);
363 $addr_structure->host
= $host;
367 $address = trim(substr($address, $pos+
1));
368 $j = strlen($address);
373 $addr_ar[] = $addr_structure;
376 case ':': /* process the group addresses */
378 $group = substr($address, 0, $pos);
379 $address = substr($address, $pos+
1);
380 $result = $this->parseAddress($address, $ar, $addr_ar, $group, $lookup);
381 $addr_ar = $result[0];
383 $address = substr($address, $pos++
);
384 $j = strlen($address);
389 $address = substr($address, 0, $pos - 1);
398 * this happens in the folowing situations :
399 * 1: unquoted personal name
400 * 2: emailaddress without < and >
401 * 3: unquoted personal name from compose that should be encoded.
402 * if it's a personal name then an emailaddress should follow
403 * the personal name may not have ',' inside it
404 * If it's a emailaddress then the personal name is not set.
405 * we should look for the delimiter ',' or a SPACE
407 /* check for emailaddress */
408 $i_space = strpos($address,' ',$pos);
409 $i_del = strpos($address,',',$pos);
410 if ($i_space ||
$i_del) {
412 $address_part = substr($address,$pos,$i_del-$pos);
414 $address_part = substr($address,$pos);
416 if ($i = strpos($address_part,'@')) {
417 /* an email address is following */
418 if (($i+
$pos) < $i_space) {
420 if ($i_space < $i_del && $i_del) {
422 $addr = substr($address,$pos,$i_space-$pos);
425 $addr = substr($address,$pos);
430 $addr = substr($address,$pos,$i_del-$pos);
433 $addr = substr($address,$pos);
439 $name .= substr($address,$pos,$i_space-$pos) . ' ';
440 $addr_start = $i_space+
1;
443 $addr = substr($address,$pos,$i_del-$pos);
453 /* email address without domain name, could be an alias */
455 $addr = $address_part;
456 $pos = strlen($address_part) +
$pos;
459 $addr = substr($address,$pos);
466 if (!$name && !$addr) {
467 $addr = substr($address, 0, $pos);
469 $addr = trim(substr($address, $addr_start, $pos));
470 } else if ($name == '') {
471 $name = trim(substr($address, 0, $addr_start));
473 if (!$name && $comment) {
475 } else if ($name && $comment) {
476 $name = $name .' ('.$comment.')';
478 $at = strpos($addr, '@');
479 $addr_structure = new AddressStructure();
480 $addr_structure->group
= $group;
482 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
483 $addr_structure->host
= trim(substr($addr, $at+
1));
485 /* if lookup function */
487 $aAddr = call_user_func_array($lookup,array($addr));
488 if (isset($aAddr['email'])) {
489 if (strpos($aAddr['email'],',')) {
490 return $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
492 $at = strpos($aAddr['email'], '@');
493 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
494 $addr_structure->host
= substr($aAddr['email'], $at+
1);
495 if (isset($aAddr['name']) && $aAddr['name']) {
496 $name = $aAddr['name'];
503 if (!$addr_structure->mailbox
) {
504 $addr_structure->mailbox
= trim($addr);
506 $addr_structure->host
= $host;
511 if (!$is_encoded && !$group) {
512 $name = encodeHeader($name);
514 if ($group && $addr == '') { /* no addresses found in group */
516 $addr_structure->personal
= $name;
517 $addr_ar[] = $addr_structure;
518 return (array($addr_ar,$pos+
1 ));
520 $addr_structure->personal
= $name;
521 $addr_ar[] = $addr_structure;
522 return (array($addr_ar,$pos+
1 ));
524 $addr_structure->personal
= $name;
525 if ($name ||
$addr) {
526 $addr_ar[] = $addr_structure;
532 return ($addr_ar[0]);
535 function parseContentType($value) {
536 $pos = strpos($value, ';');
539 $type = trim(substr($value, 0, $pos));
540 $props = trim(substr($value, $pos+
1));
544 $content_type = new ContentType($type);
546 $properties = $this->parseProperties($props);
547 if (!isset($properties['charset'])) {
548 $properties['charset'] = 'us-ascii';
550 $content_type->properties
= $this->parseProperties($props);
552 $this->content_type
= $content_type;
555 function parseProperties($value) {
556 $propArray = explode(';', $value);
557 $propResultArray = array();
558 foreach ($propArray as $prop) {
560 $pos = strpos($prop, '=');
562 $key = trim(substr($prop, 0, $pos));
563 $val = trim(substr($prop, $pos+
1));
564 if ($val{0} == '"') {
565 $val = substr($val, 1, -1);
567 $propResultArray[$key] = $val;
570 return $propResultArray;
573 function parseDisposition($value) {
574 $pos = strpos($value, ';');
577 $name = trim(substr($value, 0, $pos));
578 $props = trim(substr($value, $pos+
1));
582 $props_a = $this->parseProperties($props);
583 $disp = new Disposition($name);
584 $disp->properties
= $props_a;
585 $this->disposition
= $disp;
588 function mlist($field, $value) {
590 $value_a = explode(',', $value);
591 foreach ($value_a as $val) {
593 if ($val{0} == '<') {
594 $val = substr($val, 1, -1);
596 if (substr($val, 0, 7) == 'mailto:') {
597 $res_a['mailto'] = substr($val, 7);
599 $res_a['href'] = $val;
602 $this->mlist
[$field] = $res_a;
606 * function to get the addres strings out of the header.
607 * Arguments: string or array of strings !
608 * example1: header->getAddr_s('to').
609 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
611 function getAddr_s($arr, $separator = ',',$encoded=false) {
614 if (is_array($arr)) {
615 foreach($arr as $arg) {
616 if ($this->getAddr_s($arg, $separator, $encoded)) {
617 $s .= $separator . $result;
620 $s = ($s ?
substr($s, 2) : $s);
622 $addr = $this->{$arr};
623 if (is_array($addr)) {
624 foreach ($addr as $addr_o) {
625 if (is_object($addr_o)) {
627 $s .= $addr_o->getEncodedAddress() . $separator;
629 $s .= $addr_o->getAddress() . $separator;
633 $s = substr($s, 0, -strlen($separator));
635 if (is_object($addr)) {
637 $s .= $addr->getEncodedAddress();
639 $s .= $addr->getAddress();
647 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
648 if (is_array($arg)) {
649 foreach($arg as $argument) {
650 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
653 $addr = $this->{$arg};
654 if (is_array($addr)) {
655 foreach ($addr as $next_addr) {
656 if (is_object($next_addr)) {
657 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
658 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
660 $email = $next_addr->mailbox
;
662 $email = strtolower($email);
663 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
664 $arr[$email] = $next_addr->personal
;
669 if (is_object($addr)) {
670 $email = $addr->mailbox
;
671 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
672 $email = strtolower($email);
673 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
674 $arr[$email] = $addr->personal
;
682 function findAddress($address, $recurs = false) {
684 if (is_array($address)) {
686 foreach($address as $argument) {
687 $match = $this->findAddress($argument, true);
692 if (count($match[0]) && !$result) {
699 if (!is_array($this->cc
)) $this->cc
= array();
700 $srch_addr = $this->parseAddress($address);
702 foreach ($this->to
as $to) {
703 if ($to->host
== $srch_addr->host
) {
704 if ($to->mailbox
== $srch_addr->mailbox
) {
705 $results[] = $srch_addr;
706 if ($to->personal
== $srch_addr->personal
) {
708 return array($results, true);
716 foreach ($this->cc
as $cc) {
717 if ($cc->host
== $srch_addr->host
) {
718 if ($cc->mailbox
== $srch_addr->mailbox
) {
719 $results[] = $srch_addr;
720 if ($cc->personal
== $srch_addr->personal
) {
722 return array($results, true);
731 return array($results, false);
732 } elseif (count($result)) {
742 function getContentType($type0, $type1) {
743 $type0 = $this->content_type
->type0
;
744 $type1 = $this->content_type
->type1
;
745 return $this->content_type
->properties
;