4 * Rfc822Header.class.php
6 * Copyright (c) 2003-2004 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.
12 * @package squirrelmail
16 * input: header_string or array
17 * @package squirrelmail
25 $mail_followup_to = array(),
42 $more_headers = array(); /* only needed for constructing headers
44 function parseHeader($hdr) {
46 $hdr = implode('', $hdr);
48 /* First we replace \r\n by \n and unfold the header */
49 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
51 /* Now we can make a new header array with */
52 /* each element representing a headerline */
53 $hdr = explode("\n" , $hdr);
54 foreach ($hdr as $line) {
55 $pos = strpos($line, ':');
57 $field = substr($line, 0, $pos);
58 if (!strstr($field,' ')) { /* valid field */
59 $value = trim(substr($line, $pos+
1));
60 $this->parseField($field, $value);
64 if ($this->content_type
== '') {
65 $this->parseContentType('text/plain; charset=us-ascii');
69 function stripComments($value) {
71 $cnt = strlen($value);
72 for ($i = 0; $i < $cnt; ++
$i) {
76 while ((++
$i < $cnt) && ($value{$i} != '"')) {
77 if ($value{$i} == '\\') {
81 $result .= $value{$i};
83 $result .= $value{$i};
87 while (($depth > 0) && (++
$i < $cnt)) {
104 $result .= $value{$i};
111 function parseField($field, $value) {
112 $field = strtolower($field);
115 $value = $this->stripComments($value);
116 $d = strtr($value, array(' ' => ' '));
117 $d = explode(' ', $d);
118 $this->date
= getTimeStamp($d);
121 $this->subject
= $value;
124 $this->from
= $this->parseAddress($value,true);
127 $this->sender
= $this->parseAddress($value);
130 $this->reply_to
= $this->parseAddress($value, true);
132 case 'mail-followup-to':
133 $this->mail_followup_to
= $this->parseAddress($value, true);
136 $this->to
= $this->parseAddress($value, true);
139 $this->cc
= $this->parseAddress($value, true);
142 $this->bcc
= $this->parseAddress($value, true);
145 $this->in_reply_to
= $value;
148 $value = $this->stripComments($value);
149 $this->message_id
= $value;
152 $value = $this->stripComments($value);
153 $this->references
= $value;
155 case 'x-confirm-reading-to':
156 case 'return-receipt-to':
157 case 'disposition-notification-to':
158 $value = $this->stripComments($value);
159 $this->dnt
= $this->parseAddress($value);
162 $value = $this->stripComments($value);
163 $value = str_replace(' ', '', $value);
164 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
167 $value = $this->stripComments($value);
168 $this->parseContentType($value);
170 case 'content-disposition':
171 $value = $this->stripComments($value);
172 $this->parseDisposition($value);
174 case 'content-transfer-encoding':
175 $this->encoding
= $value;
177 case 'content-description':
178 $this->content_desc
= $value;
181 $value = $this->stripComments($value);
182 $this->content_id
= $value;
186 $this->xmailer
= $value;
189 $this->priority
= $value;
192 $value = $this->stripComments($value);
193 $this->mlist('post', $value);
196 $value = $this->stripComments($value);
197 $this->mlist('reply', $value);
199 case 'list-subscribe':
200 $value = $this->stripComments($value);
201 $this->mlist('subscribe', $value);
203 case 'list-unsubscribe':
204 $value = $this->stripComments($value);
205 $this->mlist('unsubscribe', $value);
208 $value = $this->stripComments($value);
209 $this->mlist('archive', $value);
212 $value = $this->stripComments($value);
213 $this->mlist('owner', $value);
216 $value = $this->stripComments($value);
217 $this->mlist('help', $value);
220 $value = $this->stripComments($value);
221 $this->mlist('id', $value);
228 function getAddressTokens($address) {
231 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
232 $aReplace = array(' (',' <',' ,',' ;',' :');
233 $address = str_replace($aSpecials,$aReplace,$address);
234 $iCnt = strlen($address);
237 $cChar = $address{$i};
241 $iEnd = strpos($address,'>',$i+
1);
243 $sToken = substr($address,$i);
246 $sToken = substr($address,$i,$iEnd - $i +
1);
249 $sToken = str_replace($aReplace, $aSpecials,$sToken);
250 if ($sToken) $aTokens[] = $sToken;
253 $iEnd = strpos($address,$cChar,$i+
1);
255 // skip escaped quotes
256 $prev_char = $address{$iEnd-1};
257 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
258 $iEnd = strpos($address,$cChar,$iEnd+
1);
260 $prev_char = $address{$iEnd-1};
267 $sToken = substr($address,$i);
270 // also remove the surrounding quotes
271 $sToken = substr($address,$i+
1,$iEnd - $i -1);
274 $sToken = str_replace($aReplace, $aSpecials,$sToken);
275 if ($sToken) $aTokens[] = $sToken;
278 array_pop($aTokens); //remove inserted space
279 $iEnd = strpos($address,')',$i);
281 $sToken = substr($address,$i);
286 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
287 $cCharComment = $address{$iComment};
288 switch($cCharComment) {
303 $sToken = substr($address,$i,$iComment - $i +
1);
306 $sToken = substr($address,$i,$iEnd - $i +
1);
310 // check the next token in case comments appear in the middle of email addresses
311 $prevToken = end($aTokens);
312 if (!in_array($prevToken,$aSpecials,true)) {
313 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
314 $iEnd = strpos($address,' ',$i+
1);
316 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
319 $sNextToken = trim(substr($address,$i+
1));
324 // create token and add it again
325 $sNewToken = $prevToken . $sNextToken;
326 if($sNewToken) $aTokens[] = $sNewToken;
329 $sToken = str_replace($aReplace, $aSpecials,$sToken);
330 if ($sToken) $aTokens[] = $sToken;
339 $iEnd = strpos($address,' ',$i+
1);
341 $sToken = trim(substr($address,$i,$iEnd - $i));
344 $sToken = trim(substr($address,$i));
347 if ($sToken) $aTokens[] = $sToken;
353 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
354 //$aStack=explode(' ',implode('',$aStack));
356 while (count($aStack) && !$sEmail) {
357 $sEmail = trim(array_pop($aStack));
360 if (count($aStack)) {
361 $sPersonal = trim(implode('',$aStack));
365 if (!$sPersonal && count($aComment)) {
366 $sComment = trim(implode(' ',$aComment));
367 $sPersonal .= $sComment;
369 $oAddr =& new AddressStructure();
370 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
371 $oAddr->personal
= encodeHeader($sPersonal);
373 $oAddr->personal
= $sPersonal;
375 // $oAddr->group = $sGroup;
376 $iPosAt = strpos($sEmail,'@');
378 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
379 $oAddr->host
= substr($sEmail, $iPosAt+
1);
381 $oAddr->mailbox
= $sEmail;
382 $oAddr->host
= false;
385 $aStack = $aComment = array();
390 * parseAddress: recursive function for parsing address strings and store
391 * them in an address stucture object.
392 * input: $address = string
393 * $ar = boolean (return array instead of only the
395 * $addr_ar = array with parsed addresses // obsolete
396 * $group = string // obsolete
397 * $host = string (default domainname in case of
398 * addresses without a domainname)
399 * $lookup = callback function (for lookup address
400 * strings which are probably nicks
402 * output: array with addressstructure objects or only one
403 * address_structure object.
404 * personal name: encoded: =?charset?Q|B?string?=
407 * email : <mailbox@host>
409 * This function is also used for validating addresses returned from compose
410 * That's also the reason that the function became a little bit huge
413 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
414 $aTokens = $this->getAddressTokens($address);
415 $sPersonal = $sEmail = $sComment = $sGroup = '';
416 $aStack = $aComment = array();
417 foreach ($aTokens as $sToken) {
427 $aComment[] = substr($sToken,1,-1);
431 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
432 $oAddr = end($aAddress);
433 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
434 $sEmail = $sGroup . ':;';
436 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
438 $aStack = $aComment = array();
442 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
445 $sGroup = trim(implode(' ',$aStack));
446 $sGroup = preg_replace('/\s+/',' ',$sGroup);
450 $sEmail = trim(substr($sToken,1,-1));
455 default: $aStack[] = $sToken; break;
458 /* now do the action again for the last address */
459 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
460 /* try to lookup the addresses in case of invalid email addresses */
461 $aProcessedAddress = array();
462 foreach ($aAddress as $oAddr) {
463 $aAddrBookAddress = array();
465 $grouplookup = false;
467 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
468 if (isset($aAddr['email'])) {
469 if (strpos($aAddr['email'],',')) {
471 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
473 $iPosAt = strpos($aAddr['email'], '@');
474 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
475 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
476 if (isset($aAddr['name'])) {
477 $oAddr->personal
= $aAddr['name'];
479 $oAddr->personal
= encodeHeader($sPersonal);
484 if (!$grouplookup && !$oAddr->mailbox
) {
485 $oAddr->mailbox
= trim($sEmail);
486 if ($sHost && $oAddr->mailbox
) {
487 $oAddr->host
= $sHost;
489 } else if (!$grouplookup && !$oAddr->host
) {
490 if ($sHost && $oAddr->mailbox
) {
491 $oAddr->host
= $sHost;
495 if (!$aAddrBookAddress && $oAddr->mailbox
) {
496 $aProcessedAddress[] = $oAddr;
498 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
502 return $aProcessedAddress;
504 return $aProcessedAddress[0];
508 function parseContentType($value) {
509 $pos = strpos($value, ';');
512 $type = trim(substr($value, 0, $pos));
513 $props = trim(substr($value, $pos+
1));
517 $content_type = new ContentType($type);
519 $properties = $this->parseProperties($props);
520 if (!isset($properties['charset'])) {
521 $properties['charset'] = 'us-ascii';
523 $content_type->properties
= $this->parseProperties($props);
525 $this->content_type
= $content_type;
529 function processParameters($aParameters) {
532 // handle multiline parameters
533 foreach($aParameters as $key => $value) {
534 if ($iPos = strpos($key,'*')) {
535 $sKey = substr($key,0,$iPos);
536 if (!isset($aResults[$sKey])) {
537 $aResults[$sKey] = $value;
538 if (substr($key,-1) == '*') { // parameter contains language/charset info
542 $aResults[$sKey] .= $value;
545 $aResults[$key] = $value;
548 foreach ($aCharset as $key) {
549 $value = $aResults[$key];
550 // extract the charset & language
551 $charset = substr($value,0,strpos($value,"'"));
552 $value = substr($value,strlen($charset)+
1);
553 $language = substr($value,0,strpos($value,"'"));
554 $value = substr($value,strlen($charset)+
1);
555 // FIX ME What's the status of charset decode with language information ????
556 $value = charset_decode($charset,$value);
557 $aResults[$key] = $value;
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 (strlen($val) > 0 && $val{0} == '"') {
572 $val = substr($val, 1, -1);
574 $propResultArray[$key] = $val;
577 return $this->processParameters($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
;