4 * Rfc822Header.class.php
6 * This file contains functions needed to handle headers in mime messages.
8 * @copyright © 2003-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
18 * input: header_string or array
19 * You must call parseHeader() function after creating object in order to fill object's
21 * @todo FIXME: there is no constructor function and class should ignore all input args.
22 * @package squirrelmail
50 var $reply_to = array();
52 * Mail-Followup-To header
55 var $mail_followup_to = array();
75 var $in_reply_to = '';
94 var $content_type = '';
98 var $disposition = '';
110 * Disposition notification for requesting message delivery notification (MDN)
115 * Delivery notification (DR)
126 var $content_id = '';
130 var $content_desc = '';
134 var $mlist = array();
137 * only needed for constructing headers in delivery class
140 var $more_headers = array();
143 * @param mixed $hdr string or array with message headers
145 function parseHeader($hdr) {
146 if (is_array($hdr)) {
147 $hdr = implode('', $hdr);
149 /* First we replace \r\n by \n and unfold the header */
150 /* FIXME: unfolding header with multiple spaces "\n( +)" */
151 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
153 /* Now we can make a new header array with */
154 /* each element representing a headerline */
155 $hdr = explode("\n" , $hdr);
156 foreach ($hdr as $line) {
157 $pos = strpos($line, ':');
159 $field = substr($line, 0, $pos);
160 if (!strstr($field,' ')) { /* valid field */
161 $value = trim(substr($line, $pos+
1));
162 $this->parseField($field, $value);
166 if (!is_object($this->content_type
)) {
167 $this->parseContentType('text/plain; charset=us-ascii');
172 * @param string $value
175 function stripComments($value) {
177 $cnt = strlen($value);
178 for ($i = 0; $i < $cnt; ++
$i) {
179 switch ($value{$i}) {
182 while ((++
$i < $cnt) && ($value{$i} != '"')) {
183 if ($value{$i} == '\\') {
187 $result .= $value{$i};
190 $result .= $value{$i};
195 while (($depth > 0) && (++
$i < $cnt)) {
212 $result .= $value{$i};
220 * Parse header field according to field type
221 * @param string $field field name
222 * @param string $value field value
224 function parseField($field, $value) {
225 $field = strtolower($field);
228 $value = $this->stripComments($value);
229 $d = strtr($value, array(' ' => ' '));
230 $d = explode(' ', $d);
231 $this->date
= getTimeStamp($d);
234 $this->subject
= $value;
237 $this->from
= $this->parseAddress($value,true);
240 $this->sender
= $this->parseAddress($value);
243 $this->reply_to
= $this->parseAddress($value, true);
245 case 'mail-followup-to':
246 $this->mail_followup_to
= $this->parseAddress($value, true);
249 $this->to
= $this->parseAddress($value, true);
252 $this->cc
= $this->parseAddress($value, true);
255 $this->bcc
= $this->parseAddress($value, true);
258 $this->in_reply_to
= $value;
261 $value = $this->stripComments($value);
262 $this->message_id
= $value;
265 $value = $this->stripComments($value);
266 $this->references
= $value;
268 case 'x-confirm-reading-to':
269 case 'disposition-notification-to':
270 $value = $this->stripComments($value);
271 $this->dnt
= $this->parseAddress($value);
273 case 'return-receipt-to':
274 $value = $this->stripComments($value);
275 $this->drnt
= $this->parseAddress($value);
278 $value = $this->stripComments($value);
279 $value = str_replace(' ', '', $value);
280 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
283 $value = $this->stripComments($value);
284 $this->parseContentType($value);
286 case 'content-disposition':
287 $value = $this->stripComments($value);
288 $this->parseDisposition($value);
290 case 'content-transfer-encoding':
291 $this->encoding
= $value;
293 case 'content-description':
294 $this->content_desc
= $value;
297 $value = $this->stripComments($value);
298 $this->content_id
= $value;
302 $this->xmailer
= $value;
307 $this->priority
= $this->parsePriority($value);
310 $value = $this->stripComments($value);
311 $this->mlist('post', $value);
314 $value = $this->stripComments($value);
315 $this->mlist('reply', $value);
317 case 'list-subscribe':
318 $value = $this->stripComments($value);
319 $this->mlist('subscribe', $value);
321 case 'list-unsubscribe':
322 $value = $this->stripComments($value);
323 $this->mlist('unsubscribe', $value);
326 $value = $this->stripComments($value);
327 $this->mlist('archive', $value);
330 $value = $this->stripComments($value);
331 $this->mlist('owner', $value);
334 $value = $this->stripComments($value);
335 $this->mlist('help', $value);
338 $value = $this->stripComments($value);
339 $this->mlist('id', $value);
347 * @param string $address
350 function getAddressTokens($address) {
352 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
353 $aReplace = array(' (',' <',' ,',' ;',' :');
354 $address = str_replace($aSpecials,$aReplace,$address);
355 $iCnt = strlen($address);
358 $cChar = $address{$i};
362 $iEnd = strpos($address,'>',$i+
1);
364 $sToken = substr($address,$i);
367 $sToken = substr($address,$i,$iEnd - $i +
1);
370 $sToken = str_replace($aReplace, $aSpecials,$sToken);
371 if ($sToken) $aTokens[] = $sToken;
374 $iEnd = strpos($address,$cChar,$i+
1);
376 // skip escaped quotes
377 $prev_char = $address{$iEnd-1};
378 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
379 $iEnd = strpos($address,$cChar,$iEnd+
1);
381 $prev_char = $address{$iEnd-1};
388 $sToken = substr($address,$i);
391 // also remove the surrounding quotes
392 $sToken = substr($address,$i+
1,$iEnd - $i -1);
395 $sToken = str_replace($aReplace, $aSpecials,$sToken);
396 if ($sToken) $aTokens[] = $sToken;
399 array_pop($aTokens); //remove inserted space
400 $iEnd = strpos($address,')',$i);
402 $sToken = substr($address,$i);
407 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
408 $cCharComment = $address{$iComment};
409 switch($cCharComment) {
424 $sToken = substr($address,$i,$iComment - $i +
1);
427 $sToken = substr($address,$i,$iEnd - $i +
1);
431 // check the next token in case comments appear in the middle of email addresses
432 $prevToken = end($aTokens);
433 if (!in_array($prevToken,$aSpecials,true)) {
434 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
435 $iEnd = strpos($address,' ',$i+
1);
437 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
440 $sNextToken = trim(substr($address,$i+
1));
445 // create token and add it again
446 $sNewToken = $prevToken . $sNextToken;
447 if($sNewToken) $aTokens[] = $sNewToken;
450 $sToken = str_replace($aReplace, $aSpecials,$sToken);
451 if ($sToken) $aTokens[] = $sToken;
460 $iEnd = strpos($address,' ',$i+
1);
462 $sToken = trim(substr($address,$i,$iEnd - $i));
465 $sToken = trim(substr($address,$i));
468 if ($sToken) $aTokens[] = $sToken;
476 * @param array $aStack
477 * @param array $aComment
478 * @param string $sEmail
479 * @param string $sGroup
480 * @return object AddressStructure object
482 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
483 //$aStack=explode(' ',implode('',$aStack));
485 while (count($aStack) && !$sEmail) {
486 $sEmail = trim(array_pop($aStack));
489 if (count($aStack)) {
490 $sPersonal = trim(implode('',$aStack));
494 if (!$sPersonal && count($aComment)) {
495 $sComment = trim(implode(' ',$aComment));
496 $sPersonal .= $sComment;
498 $oAddr =& new AddressStructure();
499 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
500 $oAddr->personal
= encodeHeader($sPersonal);
502 $oAddr->personal
= $sPersonal;
504 // $oAddr->group = $sGroup;
505 $iPosAt = strpos($sEmail,'@');
507 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
508 $oAddr->host
= substr($sEmail, $iPosAt+
1);
510 $oAddr->mailbox
= $sEmail;
511 $oAddr->host
= false;
514 $aStack = $aComment = array();
519 * recursive function for parsing address strings and storing them in an address stucture object.
520 * personal name: encoded: =?charset?Q|B?string?=
523 * email : <mailbox@host>
525 * This function is also used for validating addresses returned from compose
526 * That's also the reason that the function became a little bit huge
527 * @param string $address
528 * @param boolean $ar return array instead of only the first element
529 * @param array $addr_ar (obsolete) array with parsed addresses
530 * @param string $group (obsolete)
531 * @param string $host default domainname in case of addresses without a domainname
532 * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
533 * @return mixed array with AddressStructure objects or only one address_structure object.
535 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
536 $aTokens = $this->getAddressTokens($address);
537 $sPersonal = $sEmail = $sGroup = '';
538 $aStack = $aComment = array();
539 foreach ($aTokens as $sToken) {
549 $aComment[] = substr($sToken,1,-1);
553 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
554 $oAddr = end($aAddress);
555 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
556 $sEmail = $sGroup . ':;';
558 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
560 $aStack = $aComment = array();
564 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
567 $sGroup = trim(implode(' ',$aStack));
568 $sGroup = preg_replace('/\s+/',' ',$sGroup);
572 $sEmail = trim(substr($sToken,1,-1));
577 default: $aStack[] = $sToken; break;
580 /* now do the action again for the last address */
581 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
582 /* try to lookup the addresses in case of invalid email addresses */
583 $aProcessedAddress = array();
584 foreach ($aAddress as $oAddr) {
585 $aAddrBookAddress = array();
587 $grouplookup = false;
589 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
590 if (isset($aAddr['email'])) {
591 if (strpos($aAddr['email'],',')) {
593 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
595 $iPosAt = strpos($aAddr['email'], '@');
596 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
597 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
598 if (isset($aAddr['name'])) {
599 $oAddr->personal
= $aAddr['name'];
601 $oAddr->personal
= encodeHeader($sPersonal);
606 if (!$grouplookup && !$oAddr->mailbox
) {
607 $oAddr->mailbox
= trim($sEmail);
608 if ($sHost && $oAddr->mailbox
) {
609 $oAddr->host
= $sHost;
611 } else if (!$grouplookup && !$oAddr->host
) {
612 if ($sHost && $oAddr->mailbox
) {
613 $oAddr->host
= $sHost;
617 if (!$aAddrBookAddress && $oAddr->mailbox
) {
618 $aProcessedAddress[] = $oAddr;
620 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
624 return $aProcessedAddress;
626 return $aProcessedAddress[0];
631 * Normalise the different Priority headers into a uniform value,
632 * namely that of the X-Priority header (1, 3, 5). Supports:
633 * Priority, X-Priority, Importance.
634 * X-MS-Mail-Priority is not parsed because it always coincides
635 * with one of the other headers.
637 * NOTE: this is actually a duplicate from the function in
638 * functions/imap_messages. I'm not sure if it's ok here to call
640 * @param string $sValue literal priority name
643 function parsePriority($sValue) {
644 // don't use function call inside array_shift.
645 $aValue = split('/\w/',trim($sValue));
646 $value = strtolower(array_shift($aValue));
648 if ( is_numeric($value) ) {
651 if ( $value == 'urgent' ||
$value == 'high' ) {
653 } elseif ( $value == 'non-urgent' ||
$value == 'low' ) {
656 // default is normal priority
661 * @param string $value content type header
663 function parseContentType($value) {
664 $pos = strpos($value, ';');
667 $type = trim(substr($value, 0, $pos));
668 $props = trim(substr($value, $pos+
1));
672 $content_type = new ContentType($type);
674 $properties = $this->parseProperties($props);
675 if (!isset($properties['charset'])) {
676 $properties['charset'] = 'us-ascii';
678 $content_type->properties
= $this->parseProperties($props);
680 $this->content_type
= $content_type;
685 * @param array $aParameters
688 function processParameters($aParameters) {
691 // handle multiline parameters
692 foreach($aParameters as $key => $value) {
693 if ($iPos = strpos($key,'*')) {
694 $sKey = substr($key,0,$iPos);
695 if (!isset($aResults[$sKey])) {
696 $aResults[$sKey] = $value;
697 if (substr($key,-1) == '*') { // parameter contains language/charset info
701 $aResults[$sKey] .= $value;
704 $aResults[$key] = $value;
707 foreach ($aCharset as $key) {
708 $value = $aResults[$key];
709 // extract the charset & language
710 $charset = substr($value,0,strpos($value,"'"));
711 $value = substr($value,strlen($charset)+
1);
712 $language = substr($value,0,strpos($value,"'"));
713 $value = substr($value,strlen($charset)+
1);
714 /* FIXME: What's the status of charset decode with language information ????
715 * Maybe language information contains only ascii text and charset_decode()
716 * only runs htmlspecialchars() on it. If it contains 8bit information, you
717 * get html encoded text in charset used by selected translation.
719 $value = charset_decode($charset,$value);
720 $aResults[$key] = $value;
726 * @param string $value
729 function parseProperties($value) {
730 $propArray = explode(';', $value);
731 $propResultArray = array();
732 foreach ($propArray as $prop) {
734 $pos = strpos($prop, '=');
736 $key = trim(substr($prop, 0, $pos));
737 $val = trim(substr($prop, $pos+
1));
738 if (strlen($val) > 0 && $val{0} == '"') {
739 $val = substr($val, 1, -1);
741 $propResultArray[$key] = $val;
744 return $this->processParameters($propResultArray);
748 * Fills disposition object in rfc822Header object
749 * @param string $value
751 function parseDisposition($value) {
752 $pos = strpos($value, ';');
755 $name = trim(substr($value, 0, $pos));
756 $props = trim(substr($value, $pos+
1));
760 $props_a = $this->parseProperties($props);
761 $disp = new Disposition($name);
762 $disp->properties
= $props_a;
763 $this->disposition
= $disp;
767 * Fills mlist array keys in rfc822Header object
768 * @param string $field
769 * @param string $value
771 function mlist($field, $value) {
773 $value_a = explode(',', $value);
774 foreach ($value_a as $val) {
776 if ($val{0} == '<') {
777 $val = substr($val, 1, -1);
779 if (substr($val, 0, 7) == 'mailto:') {
780 $res_a['mailto'] = substr($val, 7);
782 $res_a['href'] = $val;
785 $this->mlist
[$field] = $res_a;
789 * function to get the address strings out of the header.
790 * example1: header->getAddr_s('to').
791 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
792 * @param mixed $arr string or array of strings
793 * @param string $separator
794 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
797 function getAddr_s($arr, $separator = ',',$encoded=false) {
800 if (is_array($arr)) {
801 foreach($arr as $arg) {
802 if ($this->getAddr_s($arg, $separator, $encoded)) {
806 $s = ($s ?
substr($s, 2) : $s);
808 $addr = $this->{$arr};
809 if (is_array($addr)) {
810 foreach ($addr as $addr_o) {
811 if (is_object($addr_o)) {
813 $s .= $addr_o->getEncodedAddress() . $separator;
815 $s .= $addr_o->getAddress() . $separator;
819 $s = substr($s, 0, -strlen($separator));
821 if (is_object($addr)) {
823 $s .= $addr->getEncodedAddress();
825 $s .= $addr->getAddress();
834 * function to get the array of addresses out of the header.
835 * @param mixed $arg string or array of strings
836 * @param array $excl_arr array of excluded email addresses
837 * @param array $arr array of added email addresses
840 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
841 if (is_array($arg)) {
842 foreach($arg as $argument) {
843 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
846 $addr = $this->{$arg};
847 if (is_array($addr)) {
848 foreach ($addr as $next_addr) {
849 if (is_object($next_addr)) {
850 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
851 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
853 $email = $next_addr->mailbox
;
855 $email = strtolower($email);
856 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
857 $arr[$email] = $next_addr->personal
;
862 if (is_object($addr)) {
863 $email = $addr->mailbox
;
864 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
865 $email = strtolower($email);
866 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
867 $arr[$email] = $addr->personal
;
876 * @param mixed $address array or string
877 * @param boolean $recurs
878 * @return mixed array, boolean
881 function findAddress($address, $recurs = false) {
883 if (is_array($address)) {
885 foreach($address as $argument) {
886 $match = $this->findAddress($argument, true);
890 if (count($match[0]) && !$result) {
897 if (!is_array($this->cc
)) $this->cc
= array();
898 $srch_addr = $this->parseAddress($address);
900 foreach ($this->to
as $to) {
901 if ($to->host
== $srch_addr->host
) {
902 if ($to->mailbox
== $srch_addr->mailbox
) {
903 $results[] = $srch_addr;
904 if ($to->personal
== $srch_addr->personal
) {
906 return array($results, true);
914 foreach ($this->cc
as $cc) {
915 if ($cc->host
== $srch_addr->host
) {
916 if ($cc->mailbox
== $srch_addr->mailbox
) {
917 $results[] = $srch_addr;
918 if ($cc->personal
== $srch_addr->personal
) {
920 return array($results, true);
929 return array($results, false);
930 } elseif (count($result)) {
941 * @param string $type0 media type
942 * @param string $type1 media subtype
943 * @return array media properties
944 * @todo check use of media type arguments
946 function getContentType($type0, $type1) {
947 $type0 = $this->content_type
->type0
;
948 $type1 = $this->content_type
->type1
;
949 return $this->content_type
->properties
;