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]);
268 case '"': /* get the personal name */
269 $start_encoded = $pos;
271 if ($address{$pos} == '"') {
274 $personal_start = $personal_end = $pos;
276 $personal_end = strpos($address,'"',$pos);
277 if (($personal_end-2)>0 && (substr($address,$personal_end-2,2) === '\\"' ||
278 substr($address,$personal_end-2,2) === '\\\\')) {
279 $pos = $personal_end+
1;
281 $name .= substr($address,$personal_start,$personal_end-$personal_start);
286 $pos = $personal_end+
1;
293 case '<': /* get email address */
295 $addr_end = strpos($address,'>',$addr_start);
296 $addr = substr($address,$addr_start+
1,$addr_end-$addr_start-1);
300 $addr = substr($address,$addr_start+
1);
304 case '(': /* rip off comments */
306 $pos = strpos($address,')');
307 if ($pos !== false) {
308 $comment = substr($address, $addr_start+
1,($pos-$addr_start-1));
309 $address_start = substr($address, 0, $addr_start);
310 $address_end = substr($address, $pos +
1);
311 $address = $address_start . $address_end;
313 $j = strlen($address);
314 $pos = $addr_start +
1;
316 case ',': /* we reached a delimiter */
317 if (!$name && !$addr) {
318 $addr = substr($address, 0, $pos);
320 $addr = trim(substr($address, $addr_start, $pos));
321 } else if ($name == '') {
322 $name = trim(substr($address, 0, $addr_start));
324 $at = strpos($addr, '@');
325 $addr_structure = new AddressStructure();
326 if (!$name && $comment) $name = $comment;
328 $addr_structure->personal
= encodeHeader($name);
330 $addr_structure->personal
= $name;
333 $addr_structure->group
= $group;
335 $addr_structure->mailbox
= substr($addr, 0, $at);
336 $addr_structure->host
= substr($addr, $at+
1);
338 /* if lookup function */
340 $aAddr = call_user_func_array($lookup,array($addr));
341 if (isset($aAddr['email'])) {
342 $at = strpos($aAddr['email'], '@');
343 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
344 $addr_structure->host
= substr($aAddr['email'], $at+
1);
345 if (isset($aAddr['name'])) {
346 $addr_structure->personal
= $aAddr['name'];
348 $addr_structure->personal
= encodeHeader($addr);
352 if (!$addr_structure->mailbox
) {
353 $addr_structure->mailbox
= trim($addr);
355 $addr_structure->host
= $host;
359 $address = trim(substr($address, $pos+
1));
360 $j = strlen($address);
364 $addr_ar[] = $addr_structure;
366 case ':': /* process the group addresses */
368 $group = substr($address, 0, $pos);
369 $address = substr($address, $pos+
1);
370 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
371 $addr_ar = $result[0];
373 $address = substr($address, $pos++
);
374 $j = strlen($address);
379 $address = substr($address, 0, $pos - 1);
388 * this happens in the folowing situations :
389 * 1: unquoted personal name
390 * 2: emailaddress without < and >
391 * 3: unquoted personal name from compose that should be encoded.
392 * if it's a personal name then an emailaddress should follow
393 * the personal name may not have ',' inside it
394 * If it's a emailaddress then the personal name is not set.
395 * we should look for the delimiter ',' or a SPACE
397 /* check for emailaddress */
398 $i_space = strpos($address,' ',$pos);
399 $i_del = strpos($address,',',$pos);
400 if ($i_space ||
$i_del) {
402 $address_part = substr($address,$pos,$i_del-$pos);
404 $address_part = substr($address,$pos);
406 if ($i = strpos($address_part,'@')) {
407 /* an email address is following */
408 if (($i+
$pos) < $i_space) {
410 if ($i_space < $i_del && $i_del) {
412 $addr = substr($address,$pos,$i_space-$pos);
415 $addr = substr($address,$pos);
420 $addr = substr($address,$pos,$i_del-$pos);
423 $addr = substr($address,$pos);
429 $name .= substr($address,$pos,$i_space-$pos) . ' ';
430 $addr_start = $i_space+
1;
433 $addr = substr($address,$pos,$i_del-$pos);
443 /* email address without domain name, could be an alias */
445 $addr = $address_part;
446 $pos = strlen($address_part) +
$pos;
449 $addr = substr($address,$pos);
456 if (!$name && !$addr) {
457 $addr = substr($address, 0, $pos);
459 $addr = trim(substr($address, $addr_start, $pos));
460 } else if ($name == '') {
461 $name = trim(substr($address, 0, $addr_start));
463 if (!$name && $comment) $name = $comment;
464 $at = strpos($addr, '@');
465 $addr_structure = new AddressStructure();
466 $addr_structure->group
= $group;
468 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
469 $addr_structure->host
= trim(substr($addr, $at+
1));
471 /* if lookup function */
473 $aAddr = call_user_func_array($lookup,array($addr));
474 if (isset($aAddr['email'])) {
475 $at = strpos($aAddr['email'], '@');
476 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
477 $addr_structure->host
= substr($aAddr['email'], $at+
1);
478 if (isset($aAddr['name']) && $aAddr['name']) {
479 $name = $aAddr['name'];
485 if (!$addr_structure->mailbox
) {
486 $addr_structure->mailbox
= trim($addr);
488 $addr_structure->host
= $host;
493 if (!$is_encoded && !$group) {
494 $name = encodeHeader($name);
496 if ($group && $addr == '') { /* no addresses found in group */
498 $addr_structure->personal
= $name;
499 $addr_ar[] = $addr_structure;
500 return (array($addr_ar,$pos+
1 ));
502 $addr_structure->personal
= $name;
503 $addr_ar[] = $addr_structure;
504 return (array($addr_ar,$pos+
1 ));
506 $addr_structure->personal
= $name;
507 if ($name ||
$addr) {
508 $addr_ar[] = $addr_structure;
514 return ($addr_ar[0]);
517 function parseContentType($value) {
518 $pos = strpos($value, ';');
521 $type = trim(substr($value, 0, $pos));
522 $props = trim(substr($type, $pos+
1));
526 $content_type = new ContentType($type);
528 $properties = $this->parseProperties($props);
529 if (!isset($properties['charset'])) {
530 $properties['charset'] = 'us-ascii';
532 $content_type->properties
= $this->parseProperties($props);
534 $this->content_type
= $content_type;
537 function parseProperties($value) {
538 $propArray = explode(';', $value);
539 $propResultArray = array();
540 foreach ($propArray as $prop) {
542 $pos = strpos($prop, '=');
544 $key = trim(substr($prop, 0, $pos));
545 $val = trim(substr($prop, $pos+
1));
546 if ($val{0} == '"') {
547 $val = substr($val, 1, -1);
549 $propResultArray[$key] = $val;
552 return $propResultArray;
555 function parseDisposition($value) {
556 $pos = strpos($value, ';');
559 $name = trim(substr($value, 0, $pos));
560 $props = trim(substr($value, $pos+
1));
564 $props_a = $this->parseProperties($props);
565 $disp = new Disposition($name);
566 $disp->properties
= $props_a;
567 $this->disposition
= $disp;
570 function mlist($field, $value) {
572 $value_a = explode(',', $value);
573 foreach ($value_a as $val) {
575 if ($val{0} == '<') {
576 $val = substr($val, 1, -1);
578 if (substr($val, 0, 7) == 'mailto:') {
579 $res_a['mailto'] = substr($val, 7);
581 $res_a['href'] = $val;
584 $this->mlist
[$field] = $res_a;
588 * function to get the addres strings out of the header.
589 * Arguments: string or array of strings !
590 * example1: header->getAddr_s('to').
591 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
593 function getAddr_s($arr, $separator = ',',$encoded=false) {
596 if (is_array($arr)) {
597 foreach($arr as $arg) {
598 if ($this->getAddr_s($arg, $separator, $encoded)) {
599 $s .= $separator . $result;
602 $s = ($s ?
substr($s, 2) : $s);
604 $addr = $this->{$arr};
605 if (is_array($addr)) {
606 foreach ($addr as $addr_o) {
607 if (is_object($addr_o)) {
609 $s .= $addr_o->getEncodedAddress() . $separator;
611 $s .= $addr_o->getAddress() . $separator;
615 $s = substr($s, 0, -strlen($separator));
617 if (is_object($addr)) {
619 $s .= $addr->getEncodedAddress();
621 $s .= $addr->getAddress();
629 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
630 if (is_array($arg)) {
631 foreach($arg as $argument) {
632 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
635 $addr = $this->{$arg};
636 if (is_array($addr)) {
637 foreach ($addr as $next_addr) {
638 if (is_object($next_addr)) {
639 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
640 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
642 $email = $next_addr->mailbox
;
644 $email = strtolower($email);
645 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
646 $arr[$email] = $next_addr->personal
;
651 if (is_object($addr)) {
652 $email = $addr->mailbox
;
653 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
654 $email = strtolower($email);
655 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
656 $arr[$email] = $addr->personal
;
664 function findAddress($address, $recurs = false) {
666 if (is_array($address)) {
668 foreach($address as $argument) {
669 $match = $this->findAddress($argument, true);
674 if (count($match[0]) && !$result) {
681 if (!is_array($this->cc
)) $this->cc
= array();
682 $srch_addr = $this->parseAddress($address);
684 foreach ($this->to
as $to) {
685 if ($to->host
== $srch_addr->host
) {
686 if ($to->mailbox
== $srch_addr->mailbox
) {
687 $results[] = $srch_addr;
688 if ($to->personal
== $srch_addr->personal
) {
690 return array($results, true);
698 foreach ($this->cc
as $cc) {
699 if ($cc->host
== $srch_addr->host
) {
700 if ($cc->mailbox
== $srch_addr->mailbox
) {
701 $results[] = $srch_addr;
702 if ($cc->personal
== $srch_addr->personal
) {
704 return array($results, true);
713 return array($results, false);
714 } elseif (count($result)) {
724 function getContentType($type0, $type1) {
725 $type0 = $this->content_type
->type0
;
726 $type1 = $this->content_type
->type1
;
727 return $this->content_type
->properties
;