d2b2ff1a50703d005107e2ae228da5ca6b65314d
4 * Rfc822Header.class.php
6 * This file contains functions needed to handle headers in mime messages.
8 * @copyright © 2003-2005 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 = '';
93 var $content_type = '';
97 var $disposition = '';
119 var $content_id = '';
123 var $content_desc = '';
127 var $mlist = array();
130 * only needed for constructing headers in delivery class
133 var $more_headers = array();
136 * @param mixed $hdr string or array with message headers
138 function parseHeader($hdr) {
139 if (is_array($hdr)) {
140 $hdr = implode('', $hdr);
142 /* First we replace \r\n by \n and unfold the header */
143 /* FIXME: unfolding header with multiple spaces "\n( +)" */
144 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
146 /* Now we can make a new header array with */
147 /* each element representing a headerline */
148 $hdr = explode("\n" , $hdr);
149 foreach ($hdr as $line) {
150 $pos = strpos($line, ':');
152 $field = substr($line, 0, $pos);
153 if (!strstr($field,' ')) { /* valid field */
154 $value = trim(substr($line, $pos+
1));
155 $this->parseField($field, $value);
159 if ($this->content_type
== '') {
160 $this->parseContentType('text/plain; charset=us-ascii');
165 * @param string $value
168 function stripComments($value) {
170 $cnt = strlen($value);
171 for ($i = 0; $i < $cnt; ++
$i) {
172 switch ($value{$i}) {
175 while ((++
$i < $cnt) && ($value{$i} != '"')) {
176 if ($value{$i} == '\\') {
180 $result .= $value{$i};
183 $result .= $value{$i};
188 while (($depth > 0) && (++
$i < $cnt)) {
205 $result .= $value{$i};
213 * Parse header field according to field type
214 * @param string $field field name
215 * @param string $value field value
217 function parseField($field, $value) {
218 $field = strtolower($field);
221 $value = $this->stripComments($value);
222 $d = strtr($value, array(' ' => ' '));
223 $d = explode(' ', $d);
224 $this->date
= getTimeStamp($d);
227 $this->subject
= $value;
230 $this->from
= $this->parseAddress($value,true);
233 $this->sender
= $this->parseAddress($value);
236 $this->reply_to
= $this->parseAddress($value, true);
238 case 'mail-followup-to':
239 $this->mail_followup_to
= $this->parseAddress($value, true);
242 $this->to
= $this->parseAddress($value, true);
245 $this->cc
= $this->parseAddress($value, true);
248 $this->bcc
= $this->parseAddress($value, true);
251 $this->in_reply_to
= $value;
254 $value = $this->stripComments($value);
255 $this->message_id
= $value;
258 $value = $this->stripComments($value);
259 $this->references
= $value;
261 case 'x-confirm-reading-to':
262 case 'return-receipt-to':
263 case 'disposition-notification-to':
264 $value = $this->stripComments($value);
265 $this->dnt
= $this->parseAddress($value);
268 $value = $this->stripComments($value);
269 $value = str_replace(' ', '', $value);
270 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
273 $value = $this->stripComments($value);
274 $this->parseContentType($value);
276 case 'content-disposition':
277 $value = $this->stripComments($value);
278 $this->parseDisposition($value);
280 case 'content-transfer-encoding':
281 $this->encoding
= $value;
283 case 'content-description':
284 $this->content_desc
= $value;
287 $value = $this->stripComments($value);
288 $this->content_id
= $value;
292 $this->xmailer
= $value;
297 $this->priority
= $this->parsePriority($value);
300 $value = $this->stripComments($value);
301 $this->mlist('post', $value);
304 $value = $this->stripComments($value);
305 $this->mlist('reply', $value);
307 case 'list-subscribe':
308 $value = $this->stripComments($value);
309 $this->mlist('subscribe', $value);
311 case 'list-unsubscribe':
312 $value = $this->stripComments($value);
313 $this->mlist('unsubscribe', $value);
316 $value = $this->stripComments($value);
317 $this->mlist('archive', $value);
320 $value = $this->stripComments($value);
321 $this->mlist('owner', $value);
324 $value = $this->stripComments($value);
325 $this->mlist('help', $value);
328 $value = $this->stripComments($value);
329 $this->mlist('id', $value);
337 * @param string $address
340 function getAddressTokens($address) {
342 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
343 $aReplace = array(' (',' <',' ,',' ;',' :');
344 $address = str_replace($aSpecials,$aReplace,$address);
345 $iCnt = strlen($address);
348 $cChar = $address{$i};
352 $iEnd = strpos($address,'>',$i+
1);
354 $sToken = substr($address,$i);
357 $sToken = substr($address,$i,$iEnd - $i +
1);
360 $sToken = str_replace($aReplace, $aSpecials,$sToken);
361 if ($sToken) $aTokens[] = $sToken;
364 $iEnd = strpos($address,$cChar,$i+
1);
366 // skip escaped quotes
367 $prev_char = $address{$iEnd-1};
368 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
369 $iEnd = strpos($address,$cChar,$iEnd+
1);
371 $prev_char = $address{$iEnd-1};
378 $sToken = substr($address,$i);
381 // also remove the surrounding quotes
382 $sToken = substr($address,$i+
1,$iEnd - $i -1);
385 $sToken = str_replace($aReplace, $aSpecials,$sToken);
386 if ($sToken) $aTokens[] = $sToken;
389 array_pop($aTokens); //remove inserted space
390 $iEnd = strpos($address,')',$i);
392 $sToken = substr($address,$i);
397 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
398 $cCharComment = $address{$iComment};
399 switch($cCharComment) {
414 $sToken = substr($address,$i,$iComment - $i +
1);
417 $sToken = substr($address,$i,$iEnd - $i +
1);
421 // check the next token in case comments appear in the middle of email addresses
422 $prevToken = end($aTokens);
423 if (!in_array($prevToken,$aSpecials,true)) {
424 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
425 $iEnd = strpos($address,' ',$i+
1);
427 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
430 $sNextToken = trim(substr($address,$i+
1));
435 // create token and add it again
436 $sNewToken = $prevToken . $sNextToken;
437 if($sNewToken) $aTokens[] = $sNewToken;
440 $sToken = str_replace($aReplace, $aSpecials,$sToken);
441 if ($sToken) $aTokens[] = $sToken;
450 $iEnd = strpos($address,' ',$i+
1);
452 $sToken = trim(substr($address,$i,$iEnd - $i));
455 $sToken = trim(substr($address,$i));
458 if ($sToken) $aTokens[] = $sToken;
466 * @param array $aStack
467 * @param array $aComment
468 * @param string $sEmail
469 * @param string $sGroup
470 * @return object AddressStructure object
472 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
473 //$aStack=explode(' ',implode('',$aStack));
475 while (count($aStack) && !$sEmail) {
476 $sEmail = trim(array_pop($aStack));
479 if (count($aStack)) {
480 $sPersonal = trim(implode('',$aStack));
484 if (!$sPersonal && count($aComment)) {
485 $sComment = trim(implode(' ',$aComment));
486 $sPersonal .= $sComment;
488 $oAddr =& new AddressStructure();
489 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
490 $oAddr->personal
= encodeHeader($sPersonal);
492 $oAddr->personal
= $sPersonal;
494 // $oAddr->group = $sGroup;
495 $iPosAt = strpos($sEmail,'@');
497 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
498 $oAddr->host
= substr($sEmail, $iPosAt+
1);
500 $oAddr->mailbox
= $sEmail;
501 $oAddr->host
= false;
504 $aStack = $aComment = array();
509 * recursive function for parsing address strings and storing them in an address stucture object.
510 * personal name: encoded: =?charset?Q|B?string?=
513 * email : <mailbox@host>
515 * This function is also used for validating addresses returned from compose
516 * That's also the reason that the function became a little bit huge
517 * @param string $address
518 * @param boolean $ar return array instead of only the first element
519 * @param array $addr_ar (obsolete) array with parsed addresses
520 * @param string $group (obsolete)
521 * @param string $host default domainname in case of addresses without a domainname
522 * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
523 * @return mixed array with AddressStructure objects or only one address_structure object.
525 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
526 $aTokens = $this->getAddressTokens($address);
527 $sPersonal = $sEmail = $sGroup = '';
528 $aStack = $aComment = array();
529 foreach ($aTokens as $sToken) {
539 $aComment[] = substr($sToken,1,-1);
543 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
544 $oAddr = end($aAddress);
545 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
546 $sEmail = $sGroup . ':;';
548 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
550 $aStack = $aComment = array();
554 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
557 $sGroup = trim(implode(' ',$aStack));
558 $sGroup = preg_replace('/\s+/',' ',$sGroup);
562 $sEmail = trim(substr($sToken,1,-1));
567 default: $aStack[] = $sToken; break;
570 /* now do the action again for the last address */
571 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
572 /* try to lookup the addresses in case of invalid email addresses */
573 $aProcessedAddress = array();
574 foreach ($aAddress as $oAddr) {
575 $aAddrBookAddress = array();
577 $grouplookup = false;
579 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
580 if (isset($aAddr['email'])) {
581 if (strpos($aAddr['email'],',')) {
583 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
585 $iPosAt = strpos($aAddr['email'], '@');
586 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
587 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
588 if (isset($aAddr['name'])) {
589 $oAddr->personal
= $aAddr['name'];
591 $oAddr->personal
= encodeHeader($sPersonal);
596 if (!$grouplookup && !$oAddr->mailbox
) {
597 $oAddr->mailbox
= trim($sEmail);
598 if ($sHost && $oAddr->mailbox
) {
599 $oAddr->host
= $sHost;
601 } else if (!$grouplookup && !$oAddr->host
) {
602 if ($sHost && $oAddr->mailbox
) {
603 $oAddr->host
= $sHost;
607 if (!$aAddrBookAddress && $oAddr->mailbox
) {
608 $aProcessedAddress[] = $oAddr;
610 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
614 return $aProcessedAddress;
616 return $aProcessedAddress[0];
621 * Normalise the different Priority headers into a uniform value,
622 * namely that of the X-Priority header (1, 3, 5). Supports:
623 * Priority, X-Priority, Importance.
624 * X-MS-Mail-Priority is not parsed because it always coincides
625 * with one of the other headers.
627 * NOTE: this is actually a duplicate from the function in
628 * functions/imap_messages. I'm not sure if it's ok here to call
630 * @param string $sValue literal priority name
633 function parsePriority($sValue) {
634 // don't use function call inside array_shift.
635 $aValue = split('/\w/',trim($sValue));
636 $value = strtolower(array_shift($aValue));
638 if ( is_numeric($value) ) {
641 if ( $value == 'urgent' ||
$value == 'high' ) {
643 } elseif ( $value == 'non-urgent' ||
$value == 'low' ) {
646 // default is normal priority
651 * @param string $value content type header
653 function parseContentType($value) {
654 $pos = strpos($value, ';');
657 $type = trim(substr($value, 0, $pos));
658 $props = trim(substr($value, $pos+
1));
662 $content_type = new ContentType($type);
664 $properties = $this->parseProperties($props);
665 if (!isset($properties['charset'])) {
666 $properties['charset'] = 'us-ascii';
668 $content_type->properties
= $this->parseProperties($props);
670 $this->content_type
= $content_type;
675 * @param array $aParameters
678 function processParameters($aParameters) {
681 // handle multiline parameters
682 foreach($aParameters as $key => $value) {
683 if ($iPos = strpos($key,'*')) {
684 $sKey = substr($key,0,$iPos);
685 if (!isset($aResults[$sKey])) {
686 $aResults[$sKey] = $value;
687 if (substr($key,-1) == '*') { // parameter contains language/charset info
691 $aResults[$sKey] .= $value;
694 $aResults[$key] = $value;
697 foreach ($aCharset as $key) {
698 $value = $aResults[$key];
699 // extract the charset & language
700 $charset = substr($value,0,strpos($value,"'"));
701 $value = substr($value,strlen($charset)+
1);
702 $language = substr($value,0,strpos($value,"'"));
703 $value = substr($value,strlen($charset)+
1);
704 /* FIXME: What's the status of charset decode with language information ????
705 * Maybe language information contains only ascii text and charset_decode()
706 * only runs htmlspecialchars() on it. If it contains 8bit information, you
707 * get html encoded text in charset used by selected translation.
709 $value = charset_decode($charset,$value);
710 $aResults[$key] = $value;
716 * @param string $value
719 function parseProperties($value) {
720 $propArray = explode(';', $value);
721 $propResultArray = array();
722 foreach ($propArray as $prop) {
724 $pos = strpos($prop, '=');
726 $key = trim(substr($prop, 0, $pos));
727 $val = trim(substr($prop, $pos+
1));
728 if (strlen($val) > 0 && $val{0} == '"') {
729 $val = substr($val, 1, -1);
731 $propResultArray[$key] = $val;
734 return $this->processParameters($propResultArray);
738 * Fills disposition object in rfc822Header object
739 * @param string $value
741 function parseDisposition($value) {
742 $pos = strpos($value, ';');
745 $name = trim(substr($value, 0, $pos));
746 $props = trim(substr($value, $pos+
1));
750 $props_a = $this->parseProperties($props);
751 $disp = new Disposition($name);
752 $disp->properties
= $props_a;
753 $this->disposition
= $disp;
757 * Fills mlist array keys in rfc822Header object
758 * @param string $field
759 * @param string $value
761 function mlist($field, $value) {
763 $value_a = explode(',', $value);
764 foreach ($value_a as $val) {
766 if ($val{0} == '<') {
767 $val = substr($val, 1, -1);
769 if (substr($val, 0, 7) == 'mailto:') {
770 $res_a['mailto'] = substr($val, 7);
772 $res_a['href'] = $val;
775 $this->mlist
[$field] = $res_a;
779 * function to get the address strings out of the header.
780 * example1: header->getAddr_s('to').
781 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
782 * @param mixed $arr string or array of strings
783 * @param string $separator
784 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
787 function getAddr_s($arr, $separator = ',',$encoded=false) {
790 if (is_array($arr)) {
791 foreach($arr as $arg) {
792 if ($this->getAddr_s($arg, $separator, $encoded)) {
796 $s = ($s ?
substr($s, 2) : $s);
798 $addr = $this->{$arr};
799 if (is_array($addr)) {
800 foreach ($addr as $addr_o) {
801 if (is_object($addr_o)) {
803 $s .= $addr_o->getEncodedAddress() . $separator;
805 $s .= $addr_o->getAddress() . $separator;
809 $s = substr($s, 0, -strlen($separator));
811 if (is_object($addr)) {
813 $s .= $addr->getEncodedAddress();
815 $s .= $addr->getAddress();
824 * function to get the array of addresses out of the header.
825 * @param mixed $arg string or array of strings
826 * @param array $excl_arr array of excluded email addresses
827 * @param array $arr array of added email addresses
830 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
831 if (is_array($arg)) {
832 foreach($arg as $argument) {
833 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
836 $addr = $this->{$arg};
837 if (is_array($addr)) {
838 foreach ($addr as $next_addr) {
839 if (is_object($next_addr)) {
840 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
841 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
843 $email = $next_addr->mailbox
;
845 $email = strtolower($email);
846 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
847 $arr[$email] = $next_addr->personal
;
852 if (is_object($addr)) {
853 $email = $addr->mailbox
;
854 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
855 $email = strtolower($email);
856 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
857 $arr[$email] = $addr->personal
;
866 * @param mixed $address array or string
867 * @param boolean $recurs
868 * @return mixed array, boolean
871 function findAddress($address, $recurs = false) {
873 if (is_array($address)) {
875 foreach($address as $argument) {
876 $match = $this->findAddress($argument, true);
880 if (count($match[0]) && !$result) {
887 if (!is_array($this->cc
)) $this->cc
= array();
888 $srch_addr = $this->parseAddress($address);
890 foreach ($this->to
as $to) {
891 if ($to->host
== $srch_addr->host
) {
892 if ($to->mailbox
== $srch_addr->mailbox
) {
893 $results[] = $srch_addr;
894 if ($to->personal
== $srch_addr->personal
) {
896 return array($results, true);
904 foreach ($this->cc
as $cc) {
905 if ($cc->host
== $srch_addr->host
) {
906 if ($cc->mailbox
== $srch_addr->mailbox
) {
907 $results[] = $srch_addr;
908 if ($cc->personal
== $srch_addr->personal
) {
910 return array($results, true);
919 return array($results, false);
920 } elseif (count($result)) {
931 * @param string $type0 media type
932 * @param string $type1 media subtype
933 * @return array media properties
934 * @todo check use of media type arguments
936 function getContentType($type0, $type1) {
937 $type0 = $this->content_type
->type0
;
938 $type1 = $this->content_type
->type1
;
939 return $this->content_type
->properties
;