4 * Rfc822Header.class.php
6 * Copyright (c) 2003-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This file contains functions needed to handle headers in mime messages.
12 * @package squirrelmail
19 * input: header_string or array
20 * You must call parseHeader() function after creating object in order to fill object's
22 * @todo FIXME: there is no constructor function and class should ignore all input args.
23 * @package squirrelmail
33 $mail_followup_to = array(),
50 $more_headers = array(); /* only needed for constructing headers
53 * @param mixed $hdr string or array with message headers
55 function parseHeader($hdr) {
57 $hdr = implode('', $hdr);
59 /* First we replace \r\n by \n and unfold the header */
60 /* FIXME: unfolding header with multiple spaces "\n( +)" */
61 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
63 /* Now we can make a new header array with */
64 /* each element representing a headerline */
65 $hdr = explode("\n" , $hdr);
66 foreach ($hdr as $line) {
67 $pos = strpos($line, ':');
69 $field = substr($line, 0, $pos);
70 if (!strstr($field,' ')) { /* valid field */
71 $value = trim(substr($line, $pos+
1));
72 $this->parseField($field, $value);
76 if ($this->content_type
== '') {
77 $this->parseContentType('text/plain; charset=us-ascii');
82 * @param string $value
85 function stripComments($value) {
87 $cnt = strlen($value);
88 for ($i = 0; $i < $cnt; ++
$i) {
92 while ((++
$i < $cnt) && ($value{$i} != '"')) {
93 if ($value{$i} == '\\') {
97 $result .= $value{$i};
99 $result .= $value{$i};
103 while (($depth > 0) && (++
$i < $cnt)) {
120 $result .= $value{$i};
128 * Parse header field according to field type
129 * @param string $field field name
130 * @param string $value field value
132 function parseField($field, $value) {
133 $field = strtolower($field);
136 $value = $this->stripComments($value);
137 $d = strtr($value, array(' ' => ' '));
138 $d = explode(' ', $d);
139 $this->date
= getTimeStamp($d);
142 $this->subject
= $value;
145 $this->from
= $this->parseAddress($value,true);
148 $this->sender
= $this->parseAddress($value);
151 $this->reply_to
= $this->parseAddress($value, true);
153 case 'mail-followup-to':
154 $this->mail_followup_to
= $this->parseAddress($value, true);
157 $this->to
= $this->parseAddress($value, true);
160 $this->cc
= $this->parseAddress($value, true);
163 $this->bcc
= $this->parseAddress($value, true);
166 $this->in_reply_to
= $value;
169 $value = $this->stripComments($value);
170 $this->message_id
= $value;
173 $value = $this->stripComments($value);
174 $this->references
= $value;
176 case 'x-confirm-reading-to':
177 case 'return-receipt-to':
178 case 'disposition-notification-to':
179 $value = $this->stripComments($value);
180 $this->dnt
= $this->parseAddress($value);
183 $value = $this->stripComments($value);
184 $value = str_replace(' ', '', $value);
185 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
188 $value = $this->stripComments($value);
189 $this->parseContentType($value);
191 case 'content-disposition':
192 $value = $this->stripComments($value);
193 $this->parseDisposition($value);
195 case 'content-transfer-encoding':
196 $this->encoding
= $value;
198 case 'content-description':
199 $this->content_desc
= $value;
202 $value = $this->stripComments($value);
203 $this->content_id
= $value;
207 $this->xmailer
= $value;
212 $this->priority
= $this->parsePriority($value);
215 $value = $this->stripComments($value);
216 $this->mlist('post', $value);
219 $value = $this->stripComments($value);
220 $this->mlist('reply', $value);
222 case 'list-subscribe':
223 $value = $this->stripComments($value);
224 $this->mlist('subscribe', $value);
226 case 'list-unsubscribe':
227 $value = $this->stripComments($value);
228 $this->mlist('unsubscribe', $value);
231 $value = $this->stripComments($value);
232 $this->mlist('archive', $value);
235 $value = $this->stripComments($value);
236 $this->mlist('owner', $value);
239 $value = $this->stripComments($value);
240 $this->mlist('help', $value);
243 $value = $this->stripComments($value);
244 $this->mlist('id', $value);
252 * @param string $address
255 function getAddressTokens($address) {
257 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
258 $aReplace = array(' (',' <',' ,',' ;',' :');
259 $address = str_replace($aSpecials,$aReplace,$address);
260 $iCnt = strlen($address);
263 $cChar = $address{$i};
267 $iEnd = strpos($address,'>',$i+
1);
269 $sToken = substr($address,$i);
272 $sToken = substr($address,$i,$iEnd - $i +
1);
275 $sToken = str_replace($aReplace, $aSpecials,$sToken);
276 if ($sToken) $aTokens[] = $sToken;
279 $iEnd = strpos($address,$cChar,$i+
1);
281 // skip escaped quotes
282 $prev_char = $address{$iEnd-1};
283 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
284 $iEnd = strpos($address,$cChar,$iEnd+
1);
286 $prev_char = $address{$iEnd-1};
293 $sToken = substr($address,$i);
296 // also remove the surrounding quotes
297 $sToken = substr($address,$i+
1,$iEnd - $i -1);
300 $sToken = str_replace($aReplace, $aSpecials,$sToken);
301 if ($sToken) $aTokens[] = $sToken;
304 array_pop($aTokens); //remove inserted space
305 $iEnd = strpos($address,')',$i);
307 $sToken = substr($address,$i);
312 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
313 $cCharComment = $address{$iComment};
314 switch($cCharComment) {
329 $sToken = substr($address,$i,$iComment - $i +
1);
332 $sToken = substr($address,$i,$iEnd - $i +
1);
336 // check the next token in case comments appear in the middle of email addresses
337 $prevToken = end($aTokens);
338 if (!in_array($prevToken,$aSpecials,true)) {
339 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
340 $iEnd = strpos($address,' ',$i+
1);
342 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
345 $sNextToken = trim(substr($address,$i+
1));
350 // create token and add it again
351 $sNewToken = $prevToken . $sNextToken;
352 if($sNewToken) $aTokens[] = $sNewToken;
355 $sToken = str_replace($aReplace, $aSpecials,$sToken);
356 if ($sToken) $aTokens[] = $sToken;
365 $iEnd = strpos($address,' ',$i+
1);
367 $sToken = trim(substr($address,$i,$iEnd - $i));
370 $sToken = trim(substr($address,$i));
373 if ($sToken) $aTokens[] = $sToken;
381 * @param array $aStack
382 * @param array $aComment
383 * @param string $sEmail
384 * @param string $sGroup
385 * @return object AddressStructure object
387 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
388 //$aStack=explode(' ',implode('',$aStack));
390 while (count($aStack) && !$sEmail) {
391 $sEmail = trim(array_pop($aStack));
394 if (count($aStack)) {
395 $sPersonal = trim(implode('',$aStack));
399 if (!$sPersonal && count($aComment)) {
400 $sComment = trim(implode(' ',$aComment));
401 $sPersonal .= $sComment;
403 $oAddr =& new AddressStructure();
404 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
405 $oAddr->personal
= encodeHeader($sPersonal);
407 $oAddr->personal
= $sPersonal;
409 // $oAddr->group = $sGroup;
410 $iPosAt = strpos($sEmail,'@');
412 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
413 $oAddr->host
= substr($sEmail, $iPosAt+
1);
415 $oAddr->mailbox
= $sEmail;
416 $oAddr->host
= false;
419 $aStack = $aComment = array();
424 * recursive function for parsing address strings and storing them in an address stucture object.
425 * personal name: encoded: =?charset?Q|B?string?=
428 * email : <mailbox@host>
430 * This function is also used for validating addresses returned from compose
431 * That's also the reason that the function became a little bit huge
432 * @param string $address
433 * @param boolean $ar return array instead of only the first element
434 * @param array $addr_ar (obsolete) array with parsed addresses
435 * @param string $group (obsolete)
436 * @param string $host default domainname in case of addresses without a domainname
437 * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
438 * @return mixed array with AddressStructure objects or only one address_structure object.
440 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
441 $aTokens = $this->getAddressTokens($address);
442 $sPersonal = $sEmail = $sGroup = '';
443 $aStack = $aComment = array();
444 foreach ($aTokens as $sToken) {
454 $aComment[] = substr($sToken,1,-1);
458 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
459 $oAddr = end($aAddress);
460 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
461 $sEmail = $sGroup . ':;';
463 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
465 $aStack = $aComment = array();
469 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
472 $sGroup = trim(implode(' ',$aStack));
473 $sGroup = preg_replace('/\s+/',' ',$sGroup);
477 $sEmail = trim(substr($sToken,1,-1));
482 default: $aStack[] = $sToken; break;
485 /* now do the action again for the last address */
486 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
487 /* try to lookup the addresses in case of invalid email addresses */
488 $aProcessedAddress = array();
489 foreach ($aAddress as $oAddr) {
490 $aAddrBookAddress = array();
492 $grouplookup = false;
494 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
495 if (isset($aAddr['email'])) {
496 if (strpos($aAddr['email'],',')) {
498 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
500 $iPosAt = strpos($aAddr['email'], '@');
501 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
502 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
503 if (isset($aAddr['name'])) {
504 $oAddr->personal
= $aAddr['name'];
506 $oAddr->personal
= encodeHeader($sPersonal);
511 if (!$grouplookup && !$oAddr->mailbox
) {
512 $oAddr->mailbox
= trim($sEmail);
513 if ($sHost && $oAddr->mailbox
) {
514 $oAddr->host
= $sHost;
516 } else if (!$grouplookup && !$oAddr->host
) {
517 if ($sHost && $oAddr->mailbox
) {
518 $oAddr->host
= $sHost;
522 if (!$aAddrBookAddress && $oAddr->mailbox
) {
523 $aProcessedAddress[] = $oAddr;
525 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
529 return $aProcessedAddress;
531 return $aProcessedAddress[0];
536 * Normalise the different Priority headers into a uniform value,
537 * namely that of the X-Priority header (1, 3, 5). Supports:
538 * Priority, X-Priority, Importance.
539 * X-MS-Mail-Priority is not parsed because it always coincides
540 * with one of the other headers.
542 * NOTE: this is actually a duplicate from the function in
543 * functions/imap_messages. I'm not sure if it's ok here to call
545 * @param string $value literal priority name
548 function parsePriority($value) {
549 $value = strtolower(array_shift(split('/\w/',trim($value))));
550 if ( is_numeric($value) ) {
553 if ( $value == 'urgent' ||
$value == 'high' ) {
555 } elseif ( $value == 'non-urgent' ||
$value == 'low' ) {
558 // default is normal priority
563 * @param string $value content type header
565 function parseContentType($value) {
566 $pos = strpos($value, ';');
569 $type = trim(substr($value, 0, $pos));
570 $props = trim(substr($value, $pos+
1));
574 $content_type = new ContentType($type);
576 $properties = $this->parseProperties($props);
577 if (!isset($properties['charset'])) {
578 $properties['charset'] = 'us-ascii';
580 $content_type->properties
= $this->parseProperties($props);
582 $this->content_type
= $content_type;
587 * @param array $aParameters
590 function processParameters($aParameters) {
593 // handle multiline parameters
594 foreach($aParameters as $key => $value) {
595 if ($iPos = strpos($key,'*')) {
596 $sKey = substr($key,0,$iPos);
597 if (!isset($aResults[$sKey])) {
598 $aResults[$sKey] = $value;
599 if (substr($key,-1) == '*') { // parameter contains language/charset info
603 $aResults[$sKey] .= $value;
606 $aResults[$key] = $value;
609 foreach ($aCharset as $key) {
610 $value = $aResults[$key];
611 // extract the charset & language
612 $charset = substr($value,0,strpos($value,"'"));
613 $value = substr($value,strlen($charset)+
1);
614 $language = substr($value,0,strpos($value,"'"));
615 $value = substr($value,strlen($charset)+
1);
616 /* FIXME: What's the status of charset decode with language information ????
617 * Maybe language information contains only ascii text and charset_decode()
618 * only runs htmlspecialchars() on it. If it contains 8bit information, you
619 * get html encoded text in charset used by selected translation.
621 $value = charset_decode($charset,$value);
622 $aResults[$key] = $value;
628 * @param string $value
631 function parseProperties($value) {
632 $propArray = explode(';', $value);
633 $propResultArray = array();
634 foreach ($propArray as $prop) {
636 $pos = strpos($prop, '=');
638 $key = trim(substr($prop, 0, $pos));
639 $val = trim(substr($prop, $pos+
1));
640 if (strlen($val) > 0 && $val{0} == '"') {
641 $val = substr($val, 1, -1);
643 $propResultArray[$key] = $val;
646 return $this->processParameters($propResultArray);
650 * Fills disposition object in rfc822Header object
651 * @param string $value
653 function parseDisposition($value) {
654 $pos = strpos($value, ';');
657 $name = trim(substr($value, 0, $pos));
658 $props = trim(substr($value, $pos+
1));
662 $props_a = $this->parseProperties($props);
663 $disp = new Disposition($name);
664 $disp->properties
= $props_a;
665 $this->disposition
= $disp;
669 * Fills mlist array keys in rfc822Header object
670 * @param string $field
671 * @param string $value
673 function mlist($field, $value) {
675 $value_a = explode(',', $value);
676 foreach ($value_a as $val) {
678 if ($val{0} == '<') {
679 $val = substr($val, 1, -1);
681 if (substr($val, 0, 7) == 'mailto:') {
682 $res_a['mailto'] = substr($val, 7);
684 $res_a['href'] = $val;
687 $this->mlist
[$field] = $res_a;
691 * function to get the address strings out of the header.
692 * example1: header->getAddr_s('to').
693 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
694 * @param mixed $arr string or array of strings
695 * @param string $separator
696 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
699 function getAddr_s($arr, $separator = ',',$encoded=false) {
702 if (is_array($arr)) {
703 foreach($arr as $arg) {
704 if ($this->getAddr_s($arg, $separator, $encoded)) {
708 $s = ($s ?
substr($s, 2) : $s);
710 $addr = $this->{$arr};
711 if (is_array($addr)) {
712 foreach ($addr as $addr_o) {
713 if (is_object($addr_o)) {
715 $s .= $addr_o->getEncodedAddress() . $separator;
717 $s .= $addr_o->getAddress() . $separator;
721 $s = substr($s, 0, -strlen($separator));
723 if (is_object($addr)) {
725 $s .= $addr->getEncodedAddress();
727 $s .= $addr->getAddress();
736 * function to get the array of addresses out of the header.
737 * @param mixed $arg string or array of strings
738 * @param array $excl_arr array of excluded email addresses
739 * @param array $arr array of added email addresses
742 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
743 if (is_array($arg)) {
744 foreach($arg as $argument) {
745 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
748 $addr = $this->{$arg};
749 if (is_array($addr)) {
750 foreach ($addr as $next_addr) {
751 if (is_object($next_addr)) {
752 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
753 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
755 $email = $next_addr->mailbox
;
757 $email = strtolower($email);
758 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
759 $arr[$email] = $next_addr->personal
;
764 if (is_object($addr)) {
765 $email = $addr->mailbox
;
766 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
767 $email = strtolower($email);
768 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
769 $arr[$email] = $addr->personal
;
778 * @param mixed $address array or string
779 * @param boolean $recurs
780 * @return mixed array, boolean
783 function findAddress($address, $recurs = false) {
785 if (is_array($address)) {
787 foreach($address as $argument) {
788 $match = $this->findAddress($argument, true);
792 if (count($match[0]) && !$result) {
799 if (!is_array($this->cc
)) $this->cc
= array();
800 $srch_addr = $this->parseAddress($address);
802 foreach ($this->to
as $to) {
803 if ($to->host
== $srch_addr->host
) {
804 if ($to->mailbox
== $srch_addr->mailbox
) {
805 $results[] = $srch_addr;
806 if ($to->personal
== $srch_addr->personal
) {
808 return array($results, true);
816 foreach ($this->cc
as $cc) {
817 if ($cc->host
== $srch_addr->host
) {
818 if ($cc->mailbox
== $srch_addr->mailbox
) {
819 $results[] = $srch_addr;
820 if ($cc->personal
== $srch_addr->personal
) {
822 return array($results, true);
831 return array($results, false);
832 } elseif (count($result)) {
843 * @param string $type0 media type
844 * @param string $type1 media subtype
845 * @return array media properties
846 * @todo check use of media type arguments
848 function getContentType($type0, $type1) {
849 $type0 = $this->content_type
->type0
;
850 $type1 = $this->content_type
->type1
;
851 return $this->content_type
->properties
;