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);
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 $start_encoded = $pos;
272 if ($address{$pos} == '"') {
275 $personal_start = $personal_end = $pos;
277 $personal_end = strpos($address,'"',$pos);
278 if (($personal_end-2)>0 && (substr($address,$personal_end-2,2) === '\\"' ||
279 substr($address,$personal_end-2,2) === '\\\\')) {
280 $pos = $personal_end+
1;
282 $name .= substr($address,$personal_start,$personal_end-$personal_start);
287 $pos = $personal_end+
1;
294 case '<': /* get email address */
296 $addr_end = strpos($address,'>',$addr_start);
297 $addr = substr($address,$addr_start+
1,$addr_end-$addr_start-1);
301 $addr = substr($address,$addr_start+
1);
305 case '(': /* rip off comments */
307 $pos = strpos($address,')');
308 if ($pos !== false) {
309 $comment = substr($address, $addr_start+
1,($pos-$addr_start-1));
310 $address_start = substr($address, 0, $addr_start);
311 $address_end = substr($address, $pos +
1);
312 $address = $address_start . $address_end;
314 $j = strlen($address);
315 $pos = $addr_start +
1;
317 case ',': /* we reached a delimiter */
318 if (!$name && !$addr) {
319 $addr = substr($address, 0, $pos);
321 $addr = trim(substr($address, $addr_start, $pos));
322 } else if ($name == '') {
323 $name = trim(substr($address, 0, $addr_start));
325 $at = strpos($addr, '@');
326 $addr_structure = new AddressStructure();
327 if (!$name && $comment) $name = $comment;
329 $addr_structure->personal
= encodeHeader($name);
331 $addr_structure->personal
= $name;
334 $addr_structure->group
= $group;
336 $addr_structure->mailbox
= substr($addr, 0, $at);
337 $addr_structure->host
= substr($addr, $at+
1);
339 /* if lookup function */
341 $aAddr = call_user_func_array($lookup,array($addr));
342 if (isset($aAddr['email'])) {
343 $at = strpos($aAddr['email'], '@');
344 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
345 $addr_structure->host
= substr($aAddr['email'], $at+
1);
346 if (isset($aAddr['name'])) {
347 $addr_structure->personal
= $aAddr['name'];
349 $addr_structure->personal
= encodeHeader($addr);
353 if (!$addr_structure->mailbox
) {
354 $addr_structure->mailbox
= trim($addr);
356 $addr_structure->host
= $host;
360 $address = trim(substr($address, $pos+
1));
361 $j = strlen($address);
365 $addr_ar[] = $addr_structure;
367 case ':': /* process the group addresses */
369 $group = substr($address, 0, $pos);
370 $address = substr($address, $pos+
1);
371 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
372 $addr_ar = $result[0];
374 $address = substr($address, $pos++
);
375 $j = strlen($address);
380 $address = substr($address, 0, $pos - 1);
389 * this happens in the folowing situations :
390 * 1: unquoted personal name
391 * 2: emailaddress without < and >
392 * 3: unquoted personal name from compose that should be encoded.
393 * if it's a personal name then an emailaddress should follow
394 * the personal name may not have ',' inside it
395 * If it's a emailaddress then the personal name is not set.
396 * we should look for the delimiter ',' or a SPACE
398 /* check for emailaddress */
399 $i_space = strpos($address,' ',$pos);
400 $i_del = strpos($address,',',$pos);
401 if ($i_space ||
$i_del) {
403 $address_part = substr($address,$pos,$i_del-$pos);
405 $address_part = substr($address,$pos);
407 if ($i = strpos($address_part,'@')) {
408 /* an email address is following */
409 if (($i+
$pos) < $i_space) {
411 if ($i_space < $i_del && $i_del) {
413 $addr = substr($address,$pos,$i_space-$pos);
416 $addr = substr($address,$pos);
421 $addr = substr($address,$pos,$i_del-$pos);
424 $addr = substr($address,$pos);
430 $name .= substr($address,$pos,$i_space-$pos) . ' ';
431 $addr_start = $i_space+
1;
434 $addr = substr($address,$pos,$i_del-$pos);
444 /* email address without domain name, could be an alias */
446 $addr = $address_part;
447 $pos = strlen($address_part) +
$pos;
450 $addr = substr($address,$pos);
457 if (!$name && !$addr) {
458 $addr = substr($address, 0, $pos);
460 $addr = trim(substr($address, $addr_start, $pos));
461 } else if ($name == '') {
462 $name = trim(substr($address, 0, $addr_start));
464 if (!$name && $comment) {
466 } else if ($name && $comment) {
467 $name = $name .' ('.$comment.')';
469 $at = strpos($addr, '@');
470 $addr_structure = new AddressStructure();
471 $addr_structure->group
= $group;
473 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
474 $addr_structure->host
= trim(substr($addr, $at+
1));
476 /* if lookup function */
478 $aAddr = call_user_func_array($lookup,array($addr));
479 if (isset($aAddr['email'])) {
480 $at = strpos($aAddr['email'], '@');
481 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
482 $addr_structure->host
= substr($aAddr['email'], $at+
1);
483 if (isset($aAddr['name']) && $aAddr['name']) {
484 $name = $aAddr['name'];
490 if (!$addr_structure->mailbox
) {
491 $addr_structure->mailbox
= trim($addr);
493 $addr_structure->host
= $host;
498 if (!$is_encoded && !$group) {
499 $name = encodeHeader($name);
501 if ($group && $addr == '') { /* no addresses found in group */
503 $addr_structure->personal
= $name;
504 $addr_ar[] = $addr_structure;
505 return (array($addr_ar,$pos+
1 ));
507 $addr_structure->personal
= $name;
508 $addr_ar[] = $addr_structure;
509 return (array($addr_ar,$pos+
1 ));
511 $addr_structure->personal
= $name;
512 if ($name ||
$addr) {
513 $addr_ar[] = $addr_structure;
519 return ($addr_ar[0]);
522 function parseContentType($value) {
523 $pos = strpos($value, ';');
526 $type = trim(substr($value, 0, $pos));
527 $props = trim(substr($value, $pos+
1));
531 $content_type = new ContentType($type);
533 $properties = $this->parseProperties($props);
534 if (!isset($properties['charset'])) {
535 $properties['charset'] = 'us-ascii';
537 $content_type->properties
= $this->parseProperties($props);
539 $this->content_type
= $content_type;
542 function parseProperties($value) {
543 $propArray = explode(';', $value);
544 $propResultArray = array();
545 foreach ($propArray as $prop) {
547 $pos = strpos($prop, '=');
549 $key = trim(substr($prop, 0, $pos));
550 $val = trim(substr($prop, $pos+
1));
551 if ($val{0} == '"') {
552 $val = substr($val, 1, -1);
554 $propResultArray[$key] = $val;
557 return $propResultArray;
560 function parseDisposition($value) {
561 $pos = strpos($value, ';');
564 $name = trim(substr($value, 0, $pos));
565 $props = trim(substr($value, $pos+
1));
569 $props_a = $this->parseProperties($props);
570 $disp = new Disposition($name);
571 $disp->properties
= $props_a;
572 $this->disposition
= $disp;
575 function mlist($field, $value) {
577 $value_a = explode(',', $value);
578 foreach ($value_a as $val) {
580 if ($val{0} == '<') {
581 $val = substr($val, 1, -1);
583 if (substr($val, 0, 7) == 'mailto:') {
584 $res_a['mailto'] = substr($val, 7);
586 $res_a['href'] = $val;
589 $this->mlist
[$field] = $res_a;
593 * function to get the addres strings out of the header.
594 * Arguments: string or array of strings !
595 * example1: header->getAddr_s('to').
596 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
598 function getAddr_s($arr, $separator = ',',$encoded=false) {
601 if (is_array($arr)) {
602 foreach($arr as $arg) {
603 if ($this->getAddr_s($arg, $separator, $encoded)) {
604 $s .= $separator . $result;
607 $s = ($s ?
substr($s, 2) : $s);
609 $addr = $this->{$arr};
610 if (is_array($addr)) {
611 foreach ($addr as $addr_o) {
612 if (is_object($addr_o)) {
614 $s .= $addr_o->getEncodedAddress() . $separator;
616 $s .= $addr_o->getAddress() . $separator;
620 $s = substr($s, 0, -strlen($separator));
622 if (is_object($addr)) {
624 $s .= $addr->getEncodedAddress();
626 $s .= $addr->getAddress();
634 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
635 if (is_array($arg)) {
636 foreach($arg as $argument) {
637 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
640 $addr = $this->{$arg};
641 if (is_array($addr)) {
642 foreach ($addr as $next_addr) {
643 if (is_object($next_addr)) {
644 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
645 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
647 $email = $next_addr->mailbox
;
649 $email = strtolower($email);
650 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
651 $arr[$email] = $next_addr->personal
;
656 if (is_object($addr)) {
657 $email = $addr->mailbox
;
658 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
659 $email = strtolower($email);
660 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
661 $arr[$email] = $addr->personal
;
669 function findAddress($address, $recurs = false) {
671 if (is_array($address)) {
673 foreach($address as $argument) {
674 $match = $this->findAddress($argument, true);
679 if (count($match[0]) && !$result) {
686 if (!is_array($this->cc
)) $this->cc
= array();
687 $srch_addr = $this->parseAddress($address);
689 foreach ($this->to
as $to) {
690 if ($to->host
== $srch_addr->host
) {
691 if ($to->mailbox
== $srch_addr->mailbox
) {
692 $results[] = $srch_addr;
693 if ($to->personal
== $srch_addr->personal
) {
695 return array($results, true);
703 foreach ($this->cc
as $cc) {
704 if ($cc->host
== $srch_addr->host
) {
705 if ($cc->mailbox
== $srch_addr->mailbox
) {
706 $results[] = $srch_addr;
707 if ($cc->personal
== $srch_addr->personal
) {
709 return array($results, true);
718 return array($results, false);
719 } elseif (count($result)) {
729 function getContentType($type0, $type1) {
730 $type0 = $this->content_type
->type0
;
731 $type1 = $this->content_type
->type1
;
732 return $this->content_type
->properties
;