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
37 $more_headers = array(); /* only needed for constructing headers
39 function parseHeader($hdr) {
41 $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);
207 * parseAddress: recursive function for parsing address strings and store
208 * them in an address stucture object.
209 * input: $address = string
210 * $ar = boolean (return array instead of only the
212 * $addr_ar = array with parsed addresses
214 * $host = string (default domainname in case of
215 * addresses without a domainname)
216 * $lookup = callback function (for lookup address
217 * strings which are probably nicks
219 * output: array with addressstructure objects or only one
220 * address_structure object.
221 * personal name: encoded: =?charset?Q|B?string?=
224 * email : <mailbox@host>
226 * This function is also used for validating addresses returned from compose
227 * That's also the reason that the function became a little bit huge and horrible
228 * Todo: Find a way to clean up this mess a bit (Marc Groot Koerkamp)
230 function parseAddress
231 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
233 $name = $addr = $comment = $is_encoded = '';
235 * in case of 8 bit addresses some how <SPACE> is represented as
237 * This only happens when we validate addresses from the compose form.
239 * Note: when other charsets have dificulties with characters
241 * then we should find out the value for those characters ans replace
242 * them by proper ASCII values before we start parsing.
245 $address = str_replace("\240",' ',$address);
247 $address = trim($address);
248 $j = strlen($address);
251 $char = $address{$pos};
255 /* get the encoded personal name */
256 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',substr($address,$pos),$reg)) {
258 $pos +
= strlen($reg[1]);
265 case '"': /* get the personal name */
266 $start_encoded = $pos;
268 if ($address{$pos} == '"') {
271 $personal_start = $personal_end = $pos;
273 $personal_end = strpos($address,'"',$pos);
274 if (($personal_end-2)>0 && (substr($address,$personal_end-2,2) === '\\"' ||
275 substr($address,$personal_end-2,2) === '\\\\')) {
276 $pos = $personal_end+
1;
278 $name .= substr($address,$personal_start,$personal_end-$personal_start);
283 $pos = $personal_end+
1;
290 case '<': /* get email address */
292 $addr_end = strpos($address,'>',$addr_start);
293 $addr = substr($address,$addr_start+
1,$addr_end-$addr_start-1);
297 $addr = substr($address,$addr_start+
1);
301 case '(': /* rip off comments */
303 $pos = strpos($address,')');
304 if ($pos !== false) {
305 $comment = substr($address, $addr_start+
1,($pos-$addr_start-1));
306 $address_start = substr($address, 0, $addr_start);
307 $address_end = substr($address, $pos +
1);
308 $address = $address_start . $address_end;
310 $j = strlen($address);
311 $pos = $addr_start +
1;
313 case ',': /* we reached a delimiter */
314 if (!$name && !$addr) {
315 $addr = substr($address, 0, $pos);
317 $addr = trim(substr($address, $addr_start, $pos));
318 } else if ($name == '') {
319 $name = trim(substr($address, 0, $addr_start));
321 $at = strpos($addr, '@');
322 $addr_structure = new AddressStructure();
323 if (!$name && $comment) $name = $comment;
325 $addr_structure->personal
= encodeHeader($name);
327 $addr_structure->personal
= $name;
330 $addr_structure->group
= $group;
332 $addr_structure->mailbox
= substr($addr, 0, $at);
333 $addr_structure->host
= substr($addr, $at+
1);
335 /* if lookup function */
337 $aAddr = call_user_func_array($lookup,array($addr));
338 if (isset($aAddr['email'])) {
339 $at = strpos($aAddr['email'], '@');
340 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
341 $addr_structure->host
= substr($aAddr['email'], $at+
1);
342 if (isset($aAddr['name'])) {
343 $addr_structure->personal
= $aAddr['name'];
345 $addr_structure->personal
= encodeHeader($addr);
349 if (!$addr_structure->mailbox
) {
350 $addr_structure->mailbox
= trim($addr);
352 $addr_structure->host
= $host;
356 $address = trim(substr($address, $pos+
1));
357 $j = strlen($address);
361 $addr_ar[] = $addr_structure;
363 case ':': /* process the group addresses */
365 $group = substr($address, 0, $pos);
366 $address = substr($address, $pos+
1);
367 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
368 $addr_ar = $result[0];
370 $address = substr($address, $pos++
);
371 $j = strlen($address);
376 $address = substr($address, 0, $pos - 1);
385 * this happens in the folowing situations :
386 * 1: unquoted personal name
387 * 2: emailaddress without < and >
388 * 3: unquoted personal name from compose that should be encoded.
389 * if it's a personal name then an emailaddress should follow
390 * the personal name may not have ',' inside it
391 * If it's a emailaddress then the personal name is not set.
392 * we should look for the delimiter ',' or a SPACE
394 /* check for emailaddress */
395 $i_space = strpos($address,' ',$pos);
396 $i_del = strpos($address,',',$pos);
397 if ($i_space ||
$i_del) {
399 $address_part = substr($address,$pos,$i_del-$pos);
401 $address_part = substr($address,$pos);
403 if ($i = strpos($address_part,'@')) {
404 /* an email address is following */
405 if (($i+
$pos) < $i_space) {
407 if ($i_space < $i_del && $i_del) {
409 $addr = substr($address,$pos,$i_space-$pos);
412 $addr = substr($address,$pos);
417 $addr = substr($address,$pos,$i_del-$pos);
420 $addr = substr($address,$pos);
426 $name .= substr($address,$pos,$i_space-$pos) . ' ';
427 $addr_start = $i_space+
1;
430 $addr = substr($address,$pos,$i_del-$pos);
440 /* email address without domain name, could be an alias */
442 $addr = $address_part;
443 $pos = strlen($address_part) +
$pos;
446 $addr = substr($address,$pos);
453 if (!$name && !$addr) {
454 $addr = substr($address, 0, $pos);
456 $addr = trim(substr($address, $addr_start, $pos));
457 } else if ($name == '') {
458 $name = trim(substr($address, 0, $addr_start));
460 if (!$name && $comment) {
462 } else if ($name && $comment) {
463 $name = $name .' ('.$comment.')';
465 $at = strpos($addr, '@');
466 $addr_structure = new AddressStructure();
467 $addr_structure->group
= $group;
469 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
470 $addr_structure->host
= trim(substr($addr, $at+
1));
472 /* if lookup function */
474 $aAddr = call_user_func_array($lookup,array($addr));
475 if (isset($aAddr['email'])) {
476 $at = strpos($aAddr['email'], '@');
477 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
478 $addr_structure->host
= substr($aAddr['email'], $at+
1);
479 if (isset($aAddr['name']) && $aAddr['name']) {
480 $name = $aAddr['name'];
486 if (!$addr_structure->mailbox
) {
487 $addr_structure->mailbox
= trim($addr);
489 $addr_structure->host
= $host;
494 if (!$is_encoded && !$group) {
495 $name = encodeHeader($name);
497 if ($group && $addr == '') { /* no addresses found in group */
499 $addr_structure->personal
= $name;
500 $addr_ar[] = $addr_structure;
501 return (array($addr_ar,$pos+
1 ));
503 $addr_structure->personal
= $name;
504 $addr_ar[] = $addr_structure;
505 return (array($addr_ar,$pos+
1 ));
507 $addr_structure->personal
= $name;
508 if ($name ||
$addr) {
509 $addr_ar[] = $addr_structure;
515 return ($addr_ar[0]);
518 function parseContentType($value) {
519 $pos = strpos($value, ';');
522 $type = trim(substr($value, 0, $pos));
523 $props = trim(substr($value, $pos+
1));
527 $content_type = new ContentType($type);
529 $properties = $this->parseProperties($props);
530 if (!isset($properties['charset'])) {
531 $properties['charset'] = 'us-ascii';
533 $content_type->properties
= $this->parseProperties($props);
535 $this->content_type
= $content_type;
538 function parseProperties($value) {
539 $propArray = explode(';', $value);
540 $propResultArray = array();
541 foreach ($propArray as $prop) {
543 $pos = strpos($prop, '=');
545 $key = trim(substr($prop, 0, $pos));
546 $val = trim(substr($prop, $pos+
1));
547 if ($val{0} == '"') {
548 $val = substr($val, 1, -1);
550 $propResultArray[$key] = $val;
553 return $propResultArray;
556 function parseDisposition($value) {
557 $pos = strpos($value, ';');
560 $name = trim(substr($value, 0, $pos));
561 $props = trim(substr($value, $pos+
1));
565 $props_a = $this->parseProperties($props);
566 $disp = new Disposition($name);
567 $disp->properties
= $props_a;
568 $this->disposition
= $disp;
571 function mlist($field, $value) {
573 $value_a = explode(',', $value);
574 foreach ($value_a as $val) {
576 if ($val{0} == '<') {
577 $val = substr($val, 1, -1);
579 if (substr($val, 0, 7) == 'mailto:') {
580 $res_a['mailto'] = substr($val, 7);
582 $res_a['href'] = $val;
585 $this->mlist
[$field] = $res_a;
589 * function to get the addres strings out of the header.
590 * Arguments: string or array of strings !
591 * example1: header->getAddr_s('to').
592 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
594 function getAddr_s($arr, $separator = ',',$encoded=false) {
597 if (is_array($arr)) {
598 foreach($arr as $arg) {
599 if ($this->getAddr_s($arg, $separator, $encoded)) {
600 $s .= $separator . $result;
603 $s = ($s ?
substr($s, 2) : $s);
605 $addr = $this->{$arr};
606 if (is_array($addr)) {
607 foreach ($addr as $addr_o) {
608 if (is_object($addr_o)) {
610 $s .= $addr_o->getEncodedAddress() . $separator;
612 $s .= $addr_o->getAddress() . $separator;
616 $s = substr($s, 0, -strlen($separator));
618 if (is_object($addr)) {
620 $s .= $addr->getEncodedAddress();
622 $s .= $addr->getAddress();
630 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
631 if (is_array($arg)) {
632 foreach($arg as $argument) {
633 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
636 $addr = $this->{$arg};
637 if (is_array($addr)) {
638 foreach ($addr as $next_addr) {
639 if (is_object($next_addr)) {
640 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
641 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
643 $email = $next_addr->mailbox
;
645 $email = strtolower($email);
646 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
647 $arr[$email] = $next_addr->personal
;
652 if (is_object($addr)) {
653 $email = $addr->mailbox
;
654 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
655 $email = strtolower($email);
656 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
657 $arr[$email] = $addr->personal
;
665 function findAddress($address, $recurs = false) {
667 if (is_array($address)) {
669 foreach($address as $argument) {
670 $match = $this->findAddress($argument, true);
675 if (count($match[0]) && !$result) {
682 if (!is_array($this->cc
)) $this->cc
= array();
683 $srch_addr = $this->parseAddress($address);
685 foreach ($this->to
as $to) {
686 if ($to->host
== $srch_addr->host
) {
687 if ($to->mailbox
== $srch_addr->mailbox
) {
688 $results[] = $srch_addr;
689 if ($to->personal
== $srch_addr->personal
) {
691 return array($results, true);
699 foreach ($this->cc
as $cc) {
700 if ($cc->host
== $srch_addr->host
) {
701 if ($cc->mailbox
== $srch_addr->mailbox
) {
702 $results[] = $srch_addr;
703 if ($cc->personal
== $srch_addr->personal
) {
705 return array($results, true);
714 return array($results, false);
715 } elseif (count($result)) {
725 function getContentType($type0, $type1) {
726 $type0 = $this->content_type
->type0
;
727 $type1 = $this->content_type
->type1
;
728 return $this->content_type
->properties
;