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
51 var $reply_to = array();
53 * Mail-Followup-To header
56 var $mail_followup_to = array();
76 var $in_reply_to = '';
94 var $content_type = '';
98 var $disposition = '';
120 var $content_id = '';
124 var $content_desc = '';
128 var $mlist = array();
131 * only needed for constructing headers in delivery class
134 var $more_headers = array();
137 * @param mixed $hdr string or array with message headers
139 function parseHeader($hdr) {
140 if (is_array($hdr)) {
141 $hdr = implode('', $hdr);
143 /* First we replace \r\n by \n and unfold the header */
144 /* FIXME: unfolding header with multiple spaces "\n( +)" */
145 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
147 /* Now we can make a new header array with */
148 /* each element representing a headerline */
149 $hdr = explode("\n" , $hdr);
150 foreach ($hdr as $line) {
151 $pos = strpos($line, ':');
153 $field = substr($line, 0, $pos);
154 if (!strstr($field,' ')) { /* valid field */
155 $value = trim(substr($line, $pos+
1));
156 $this->parseField($field, $value);
160 if ($this->content_type
== '') {
161 $this->parseContentType('text/plain; charset=us-ascii');
166 * @param string $value
169 function stripComments($value) {
171 $cnt = strlen($value);
172 for ($i = 0; $i < $cnt; ++
$i) {
173 switch ($value{$i}) {
176 while ((++
$i < $cnt) && ($value{$i} != '"')) {
177 if ($value{$i} == '\\') {
181 $result .= $value{$i};
183 $result .= $value{$i};
187 while (($depth > 0) && (++
$i < $cnt)) {
204 $result .= $value{$i};
212 * Parse header field according to field type
213 * @param string $field field name
214 * @param string $value field value
216 function parseField($field, $value) {
217 $field = strtolower($field);
220 $value = $this->stripComments($value);
221 $d = strtr($value, array(' ' => ' '));
222 $d = explode(' ', $d);
223 $this->date
= getTimeStamp($d);
226 $this->subject
= $value;
229 $this->from
= $this->parseAddress($value,true);
232 $this->sender
= $this->parseAddress($value);
235 $this->reply_to
= $this->parseAddress($value, true);
237 case 'mail-followup-to':
238 $this->mail_followup_to
= $this->parseAddress($value, true);
241 $this->to
= $this->parseAddress($value, true);
244 $this->cc
= $this->parseAddress($value, true);
247 $this->bcc
= $this->parseAddress($value, true);
250 $this->in_reply_to
= $value;
253 $value = $this->stripComments($value);
254 $this->message_id
= $value;
257 $value = $this->stripComments($value);
258 $this->references
= $value;
260 case 'x-confirm-reading-to':
261 case 'return-receipt-to':
262 case 'disposition-notification-to':
263 $value = $this->stripComments($value);
264 $this->dnt
= $this->parseAddress($value);
267 $value = $this->stripComments($value);
268 $value = str_replace(' ', '', $value);
269 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
272 $value = $this->stripComments($value);
273 $this->parseContentType($value);
275 case 'content-disposition':
276 $value = $this->stripComments($value);
277 $this->parseDisposition($value);
279 case 'content-transfer-encoding':
280 $this->encoding
= $value;
282 case 'content-description':
283 $this->content_desc
= $value;
286 $value = $this->stripComments($value);
287 $this->content_id
= $value;
291 $this->xmailer
= $value;
296 $this->priority
= $this->parsePriority($value);
299 $value = $this->stripComments($value);
300 $this->mlist('post', $value);
303 $value = $this->stripComments($value);
304 $this->mlist('reply', $value);
306 case 'list-subscribe':
307 $value = $this->stripComments($value);
308 $this->mlist('subscribe', $value);
310 case 'list-unsubscribe':
311 $value = $this->stripComments($value);
312 $this->mlist('unsubscribe', $value);
315 $value = $this->stripComments($value);
316 $this->mlist('archive', $value);
319 $value = $this->stripComments($value);
320 $this->mlist('owner', $value);
323 $value = $this->stripComments($value);
324 $this->mlist('help', $value);
327 $value = $this->stripComments($value);
328 $this->mlist('id', $value);
336 * @param string $address
339 function getAddressTokens($address) {
341 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
342 $aReplace = array(' (',' <',' ,',' ;',' :');
343 $address = str_replace($aSpecials,$aReplace,$address);
344 $iCnt = strlen($address);
347 $cChar = $address{$i};
351 $iEnd = strpos($address,'>',$i+
1);
353 $sToken = substr($address,$i);
356 $sToken = substr($address,$i,$iEnd - $i +
1);
359 $sToken = str_replace($aReplace, $aSpecials,$sToken);
360 if ($sToken) $aTokens[] = $sToken;
363 $iEnd = strpos($address,$cChar,$i+
1);
365 // skip escaped quotes
366 $prev_char = $address{$iEnd-1};
367 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
368 $iEnd = strpos($address,$cChar,$iEnd+
1);
370 $prev_char = $address{$iEnd-1};
377 $sToken = substr($address,$i);
380 // also remove the surrounding quotes
381 $sToken = substr($address,$i+
1,$iEnd - $i -1);
384 $sToken = str_replace($aReplace, $aSpecials,$sToken);
385 if ($sToken) $aTokens[] = $sToken;
388 array_pop($aTokens); //remove inserted space
389 $iEnd = strpos($address,')',$i);
391 $sToken = substr($address,$i);
396 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
397 $cCharComment = $address{$iComment};
398 switch($cCharComment) {
413 $sToken = substr($address,$i,$iComment - $i +
1);
416 $sToken = substr($address,$i,$iEnd - $i +
1);
420 // check the next token in case comments appear in the middle of email addresses
421 $prevToken = end($aTokens);
422 if (!in_array($prevToken,$aSpecials,true)) {
423 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
424 $iEnd = strpos($address,' ',$i+
1);
426 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
429 $sNextToken = trim(substr($address,$i+
1));
434 // create token and add it again
435 $sNewToken = $prevToken . $sNextToken;
436 if($sNewToken) $aTokens[] = $sNewToken;
439 $sToken = str_replace($aReplace, $aSpecials,$sToken);
440 if ($sToken) $aTokens[] = $sToken;
449 $iEnd = strpos($address,' ',$i+
1);
451 $sToken = trim(substr($address,$i,$iEnd - $i));
454 $sToken = trim(substr($address,$i));
457 if ($sToken) $aTokens[] = $sToken;
465 * @param array $aStack
466 * @param array $aComment
467 * @param string $sEmail
468 * @param string $sGroup
469 * @return object AddressStructure object
471 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
472 //$aStack=explode(' ',implode('',$aStack));
474 while (count($aStack) && !$sEmail) {
475 $sEmail = trim(array_pop($aStack));
478 if (count($aStack)) {
479 $sPersonal = trim(implode('',$aStack));
483 if (!$sPersonal && count($aComment)) {
484 $sComment = trim(implode(' ',$aComment));
485 $sPersonal .= $sComment;
487 $oAddr =& new AddressStructure();
488 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
489 $oAddr->personal
= encodeHeader($sPersonal);
491 $oAddr->personal
= $sPersonal;
493 // $oAddr->group = $sGroup;
494 $iPosAt = strpos($sEmail,'@');
496 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
497 $oAddr->host
= substr($sEmail, $iPosAt+
1);
499 $oAddr->mailbox
= $sEmail;
500 $oAddr->host
= false;
503 $aStack = $aComment = array();
508 * recursive function for parsing address strings and storing them in an address stucture object.
509 * personal name: encoded: =?charset?Q|B?string?=
512 * email : <mailbox@host>
514 * This function is also used for validating addresses returned from compose
515 * That's also the reason that the function became a little bit huge
516 * @param string $address
517 * @param boolean $ar return array instead of only the first element
518 * @param array $addr_ar (obsolete) array with parsed addresses
519 * @param string $group (obsolete)
520 * @param string $host default domainname in case of addresses without a domainname
521 * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
522 * @return mixed array with AddressStructure objects or only one address_structure object.
524 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
525 $aTokens = $this->getAddressTokens($address);
526 $sPersonal = $sEmail = $sGroup = '';
527 $aStack = $aComment = array();
528 foreach ($aTokens as $sToken) {
538 $aComment[] = substr($sToken,1,-1);
542 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
543 $oAddr = end($aAddress);
544 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
545 $sEmail = $sGroup . ':;';
547 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
549 $aStack = $aComment = array();
553 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
556 $sGroup = trim(implode(' ',$aStack));
557 $sGroup = preg_replace('/\s+/',' ',$sGroup);
561 $sEmail = trim(substr($sToken,1,-1));
566 default: $aStack[] = $sToken; break;
569 /* now do the action again for the last address */
570 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
571 /* try to lookup the addresses in case of invalid email addresses */
572 $aProcessedAddress = array();
573 foreach ($aAddress as $oAddr) {
574 $aAddrBookAddress = array();
576 $grouplookup = false;
578 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
579 if (isset($aAddr['email'])) {
580 if (strpos($aAddr['email'],',')) {
582 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
584 $iPosAt = strpos($aAddr['email'], '@');
585 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
586 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
587 if (isset($aAddr['name'])) {
588 $oAddr->personal
= $aAddr['name'];
590 $oAddr->personal
= encodeHeader($sPersonal);
595 if (!$grouplookup && !$oAddr->mailbox
) {
596 $oAddr->mailbox
= trim($sEmail);
597 if ($sHost && $oAddr->mailbox
) {
598 $oAddr->host
= $sHost;
600 } else if (!$grouplookup && !$oAddr->host
) {
601 if ($sHost && $oAddr->mailbox
) {
602 $oAddr->host
= $sHost;
606 if (!$aAddrBookAddress && $oAddr->mailbox
) {
607 $aProcessedAddress[] = $oAddr;
609 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
613 return $aProcessedAddress;
615 return $aProcessedAddress[0];
620 * Normalise the different Priority headers into a uniform value,
621 * namely that of the X-Priority header (1, 3, 5). Supports:
622 * Priority, X-Priority, Importance.
623 * X-MS-Mail-Priority is not parsed because it always coincides
624 * with one of the other headers.
626 * NOTE: this is actually a duplicate from the function in
627 * functions/imap_messages. I'm not sure if it's ok here to call
629 * @param string $value literal priority name
632 function parsePriority($value) {
633 $value = strtolower(array_shift(split('/\w/',trim($value))));
634 if ( is_numeric($value) ) {
637 if ( $value == 'urgent' ||
$value == 'high' ) {
639 } elseif ( $value == 'non-urgent' ||
$value == 'low' ) {
642 // default is normal priority
647 * @param string $value content type header
649 function parseContentType($value) {
650 $pos = strpos($value, ';');
653 $type = trim(substr($value, 0, $pos));
654 $props = trim(substr($value, $pos+
1));
658 $content_type = new ContentType($type);
660 $properties = $this->parseProperties($props);
661 if (!isset($properties['charset'])) {
662 $properties['charset'] = 'us-ascii';
664 $content_type->properties
= $this->parseProperties($props);
666 $this->content_type
= $content_type;
671 * @param array $aParameters
674 function processParameters($aParameters) {
677 // handle multiline parameters
678 foreach($aParameters as $key => $value) {
679 if ($iPos = strpos($key,'*')) {
680 $sKey = substr($key,0,$iPos);
681 if (!isset($aResults[$sKey])) {
682 $aResults[$sKey] = $value;
683 if (substr($key,-1) == '*') { // parameter contains language/charset info
687 $aResults[$sKey] .= $value;
690 $aResults[$key] = $value;
693 foreach ($aCharset as $key) {
694 $value = $aResults[$key];
695 // extract the charset & language
696 $charset = substr($value,0,strpos($value,"'"));
697 $value = substr($value,strlen($charset)+
1);
698 $language = substr($value,0,strpos($value,"'"));
699 $value = substr($value,strlen($charset)+
1);
700 /* FIXME: What's the status of charset decode with language information ????
701 * Maybe language information contains only ascii text and charset_decode()
702 * only runs htmlspecialchars() on it. If it contains 8bit information, you
703 * get html encoded text in charset used by selected translation.
705 $value = charset_decode($charset,$value);
706 $aResults[$key] = $value;
712 * @param string $value
715 function parseProperties($value) {
716 $propArray = explode(';', $value);
717 $propResultArray = array();
718 foreach ($propArray as $prop) {
720 $pos = strpos($prop, '=');
722 $key = trim(substr($prop, 0, $pos));
723 $val = trim(substr($prop, $pos+
1));
724 if (strlen($val) > 0 && $val{0} == '"') {
725 $val = substr($val, 1, -1);
727 $propResultArray[$key] = $val;
730 return $this->processParameters($propResultArray);
734 * Fills disposition object in rfc822Header object
735 * @param string $value
737 function parseDisposition($value) {
738 $pos = strpos($value, ';');
741 $name = trim(substr($value, 0, $pos));
742 $props = trim(substr($value, $pos+
1));
746 $props_a = $this->parseProperties($props);
747 $disp = new Disposition($name);
748 $disp->properties
= $props_a;
749 $this->disposition
= $disp;
753 * Fills mlist array keys in rfc822Header object
754 * @param string $field
755 * @param string $value
757 function mlist($field, $value) {
759 $value_a = explode(',', $value);
760 foreach ($value_a as $val) {
762 if ($val{0} == '<') {
763 $val = substr($val, 1, -1);
765 if (substr($val, 0, 7) == 'mailto:') {
766 $res_a['mailto'] = substr($val, 7);
768 $res_a['href'] = $val;
771 $this->mlist
[$field] = $res_a;
775 * function to get the address strings out of the header.
776 * example1: header->getAddr_s('to').
777 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
778 * @param mixed $arr string or array of strings
779 * @param string $separator
780 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
783 function getAddr_s($arr, $separator = ',',$encoded=false) {
786 if (is_array($arr)) {
787 foreach($arr as $arg) {
788 if ($this->getAddr_s($arg, $separator, $encoded)) {
792 $s = ($s ?
substr($s, 2) : $s);
794 $addr = $this->{$arr};
795 if (is_array($addr)) {
796 foreach ($addr as $addr_o) {
797 if (is_object($addr_o)) {
799 $s .= $addr_o->getEncodedAddress() . $separator;
801 $s .= $addr_o->getAddress() . $separator;
805 $s = substr($s, 0, -strlen($separator));
807 if (is_object($addr)) {
809 $s .= $addr->getEncodedAddress();
811 $s .= $addr->getAddress();
820 * function to get the array of addresses out of the header.
821 * @param mixed $arg string or array of strings
822 * @param array $excl_arr array of excluded email addresses
823 * @param array $arr array of added email addresses
826 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
827 if (is_array($arg)) {
828 foreach($arg as $argument) {
829 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
832 $addr = $this->{$arg};
833 if (is_array($addr)) {
834 foreach ($addr as $next_addr) {
835 if (is_object($next_addr)) {
836 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
837 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
839 $email = $next_addr->mailbox
;
841 $email = strtolower($email);
842 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
843 $arr[$email] = $next_addr->personal
;
848 if (is_object($addr)) {
849 $email = $addr->mailbox
;
850 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
851 $email = strtolower($email);
852 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
853 $arr[$email] = $addr->personal
;
862 * @param mixed $address array or string
863 * @param boolean $recurs
864 * @return mixed array, boolean
867 function findAddress($address, $recurs = false) {
869 if (is_array($address)) {
871 foreach($address as $argument) {
872 $match = $this->findAddress($argument, true);
876 if (count($match[0]) && !$result) {
883 if (!is_array($this->cc
)) $this->cc
= array();
884 $srch_addr = $this->parseAddress($address);
886 foreach ($this->to
as $to) {
887 if ($to->host
== $srch_addr->host
) {
888 if ($to->mailbox
== $srch_addr->mailbox
) {
889 $results[] = $srch_addr;
890 if ($to->personal
== $srch_addr->personal
) {
892 return array($results, true);
900 foreach ($this->cc
as $cc) {
901 if ($cc->host
== $srch_addr->host
) {
902 if ($cc->mailbox
== $srch_addr->mailbox
) {
903 $results[] = $srch_addr;
904 if ($cc->personal
== $srch_addr->personal
) {
906 return array($results, true);
915 return array($results, false);
916 } elseif (count($result)) {
927 * @param string $type0 media type
928 * @param string $type1 media subtype
929 * @return array media properties
930 * @todo check use of media type arguments
932 function getContentType($type0, $type1) {
933 $type0 = $this->content_type
->type0
;
934 $type1 = $this->content_type
->type1
;
935 return $this->content_type
->properties
;