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};
182 $result .= $value{$i};
186 while (($depth > 0) && (++
$i < $cnt)) {
203 $result .= $value{$i};
211 * Parse header field according to field type
212 * @param string $field field name
213 * @param string $value field value
215 function parseField($field, $value) {
216 $field = strtolower($field);
219 $value = $this->stripComments($value);
220 $d = strtr($value, array(' ' => ' '));
221 $d = explode(' ', $d);
222 $this->date
= getTimeStamp($d);
225 $this->subject
= $value;
228 $this->from
= $this->parseAddress($value,true);
231 $this->sender
= $this->parseAddress($value);
234 $this->reply_to
= $this->parseAddress($value, true);
236 case 'mail-followup-to':
237 $this->mail_followup_to
= $this->parseAddress($value, true);
240 $this->to
= $this->parseAddress($value, true);
243 $this->cc
= $this->parseAddress($value, true);
246 $this->bcc
= $this->parseAddress($value, true);
249 $this->in_reply_to
= $value;
252 $value = $this->stripComments($value);
253 $this->message_id
= $value;
256 $value = $this->stripComments($value);
257 $this->references
= $value;
259 case 'x-confirm-reading-to':
260 case 'return-receipt-to':
261 case 'disposition-notification-to':
262 $value = $this->stripComments($value);
263 $this->dnt
= $this->parseAddress($value);
266 $value = $this->stripComments($value);
267 $value = str_replace(' ', '', $value);
268 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
271 $value = $this->stripComments($value);
272 $this->parseContentType($value);
274 case 'content-disposition':
275 $value = $this->stripComments($value);
276 $this->parseDisposition($value);
278 case 'content-transfer-encoding':
279 $this->encoding
= $value;
281 case 'content-description':
282 $this->content_desc
= $value;
285 $value = $this->stripComments($value);
286 $this->content_id
= $value;
290 $this->xmailer
= $value;
295 $this->priority
= $this->parsePriority($value);
298 $value = $this->stripComments($value);
299 $this->mlist('post', $value);
302 $value = $this->stripComments($value);
303 $this->mlist('reply', $value);
305 case 'list-subscribe':
306 $value = $this->stripComments($value);
307 $this->mlist('subscribe', $value);
309 case 'list-unsubscribe':
310 $value = $this->stripComments($value);
311 $this->mlist('unsubscribe', $value);
314 $value = $this->stripComments($value);
315 $this->mlist('archive', $value);
318 $value = $this->stripComments($value);
319 $this->mlist('owner', $value);
322 $value = $this->stripComments($value);
323 $this->mlist('help', $value);
326 $value = $this->stripComments($value);
327 $this->mlist('id', $value);
335 * @param string $address
338 function getAddressTokens($address) {
340 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
341 $aReplace = array(' (',' <',' ,',' ;',' :');
342 $address = str_replace($aSpecials,$aReplace,$address);
343 $iCnt = strlen($address);
346 $cChar = $address{$i};
350 $iEnd = strpos($address,'>',$i+
1);
352 $sToken = substr($address,$i);
355 $sToken = substr($address,$i,$iEnd - $i +
1);
358 $sToken = str_replace($aReplace, $aSpecials,$sToken);
359 if ($sToken) $aTokens[] = $sToken;
362 $iEnd = strpos($address,$cChar,$i+
1);
364 // skip escaped quotes
365 $prev_char = $address{$iEnd-1};
366 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
367 $iEnd = strpos($address,$cChar,$iEnd+
1);
369 $prev_char = $address{$iEnd-1};
376 $sToken = substr($address,$i);
379 // also remove the surrounding quotes
380 $sToken = substr($address,$i+
1,$iEnd - $i -1);
383 $sToken = str_replace($aReplace, $aSpecials,$sToken);
384 if ($sToken) $aTokens[] = $sToken;
387 array_pop($aTokens); //remove inserted space
388 $iEnd = strpos($address,')',$i);
390 $sToken = substr($address,$i);
395 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
396 $cCharComment = $address{$iComment};
397 switch($cCharComment) {
412 $sToken = substr($address,$i,$iComment - $i +
1);
415 $sToken = substr($address,$i,$iEnd - $i +
1);
419 // check the next token in case comments appear in the middle of email addresses
420 $prevToken = end($aTokens);
421 if (!in_array($prevToken,$aSpecials,true)) {
422 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
423 $iEnd = strpos($address,' ',$i+
1);
425 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
428 $sNextToken = trim(substr($address,$i+
1));
433 // create token and add it again
434 $sNewToken = $prevToken . $sNextToken;
435 if($sNewToken) $aTokens[] = $sNewToken;
438 $sToken = str_replace($aReplace, $aSpecials,$sToken);
439 if ($sToken) $aTokens[] = $sToken;
448 $iEnd = strpos($address,' ',$i+
1);
450 $sToken = trim(substr($address,$i,$iEnd - $i));
453 $sToken = trim(substr($address,$i));
456 if ($sToken) $aTokens[] = $sToken;
464 * @param array $aStack
465 * @param array $aComment
466 * @param string $sEmail
467 * @param string $sGroup
468 * @return object AddressStructure object
470 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
471 //$aStack=explode(' ',implode('',$aStack));
473 while (count($aStack) && !$sEmail) {
474 $sEmail = trim(array_pop($aStack));
477 if (count($aStack)) {
478 $sPersonal = trim(implode('',$aStack));
482 if (!$sPersonal && count($aComment)) {
483 $sComment = trim(implode(' ',$aComment));
484 $sPersonal .= $sComment;
486 $oAddr =& new AddressStructure();
487 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
488 $oAddr->personal
= encodeHeader($sPersonal);
490 $oAddr->personal
= $sPersonal;
492 // $oAddr->group = $sGroup;
493 $iPosAt = strpos($sEmail,'@');
495 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
496 $oAddr->host
= substr($sEmail, $iPosAt+
1);
498 $oAddr->mailbox
= $sEmail;
499 $oAddr->host
= false;
502 $aStack = $aComment = array();
507 * recursive function for parsing address strings and storing them in an address stucture object.
508 * personal name: encoded: =?charset?Q|B?string?=
511 * email : <mailbox@host>
513 * This function is also used for validating addresses returned from compose
514 * That's also the reason that the function became a little bit huge
515 * @param string $address
516 * @param boolean $ar return array instead of only the first element
517 * @param array $addr_ar (obsolete) array with parsed addresses
518 * @param string $group (obsolete)
519 * @param string $host default domainname in case of addresses without a domainname
520 * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
521 * @return mixed array with AddressStructure objects or only one address_structure object.
523 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
524 $aTokens = $this->getAddressTokens($address);
525 $sPersonal = $sEmail = $sGroup = '';
526 $aStack = $aComment = array();
527 foreach ($aTokens as $sToken) {
537 $aComment[] = substr($sToken,1,-1);
541 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
542 $oAddr = end($aAddress);
543 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
544 $sEmail = $sGroup . ':;';
546 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
548 $aStack = $aComment = array();
552 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
555 $sGroup = trim(implode(' ',$aStack));
556 $sGroup = preg_replace('/\s+/',' ',$sGroup);
560 $sEmail = trim(substr($sToken,1,-1));
565 default: $aStack[] = $sToken; break;
568 /* now do the action again for the last address */
569 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
570 /* try to lookup the addresses in case of invalid email addresses */
571 $aProcessedAddress = array();
572 foreach ($aAddress as $oAddr) {
573 $aAddrBookAddress = array();
575 $grouplookup = false;
577 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
578 if (isset($aAddr['email'])) {
579 if (strpos($aAddr['email'],',')) {
581 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
583 $iPosAt = strpos($aAddr['email'], '@');
584 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
585 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
586 if (isset($aAddr['name'])) {
587 $oAddr->personal
= $aAddr['name'];
589 $oAddr->personal
= encodeHeader($sPersonal);
594 if (!$grouplookup && !$oAddr->mailbox
) {
595 $oAddr->mailbox
= trim($sEmail);
596 if ($sHost && $oAddr->mailbox
) {
597 $oAddr->host
= $sHost;
599 } else if (!$grouplookup && !$oAddr->host
) {
600 if ($sHost && $oAddr->mailbox
) {
601 $oAddr->host
= $sHost;
605 if (!$aAddrBookAddress && $oAddr->mailbox
) {
606 $aProcessedAddress[] = $oAddr;
608 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
612 return $aProcessedAddress;
614 return $aProcessedAddress[0];
619 * Normalise the different Priority headers into a uniform value,
620 * namely that of the X-Priority header (1, 3, 5). Supports:
621 * Priority, X-Priority, Importance.
622 * X-MS-Mail-Priority is not parsed because it always coincides
623 * with one of the other headers.
625 * NOTE: this is actually a duplicate from the function in
626 * functions/imap_messages. I'm not sure if it's ok here to call
628 * @param string $sValue literal priority name
631 function parsePriority($sValue) {
632 // don't use function call inside array_shift.
633 $aValue = split('/\w/',trim($sValue));
634 $value = strtolower(array_shift($aValue));
636 if ( is_numeric($value) ) {
639 if ( $value == 'urgent' ||
$value == 'high' ) {
641 } elseif ( $value == 'non-urgent' ||
$value == 'low' ) {
644 // default is normal priority
649 * @param string $value content type header
651 function parseContentType($value) {
652 $pos = strpos($value, ';');
655 $type = trim(substr($value, 0, $pos));
656 $props = trim(substr($value, $pos+
1));
660 $content_type = new ContentType($type);
662 $properties = $this->parseProperties($props);
663 if (!isset($properties['charset'])) {
664 $properties['charset'] = 'us-ascii';
666 $content_type->properties
= $this->parseProperties($props);
668 $this->content_type
= $content_type;
673 * @param array $aParameters
676 function processParameters($aParameters) {
679 // handle multiline parameters
680 foreach($aParameters as $key => $value) {
681 if ($iPos = strpos($key,'*')) {
682 $sKey = substr($key,0,$iPos);
683 if (!isset($aResults[$sKey])) {
684 $aResults[$sKey] = $value;
685 if (substr($key,-1) == '*') { // parameter contains language/charset info
689 $aResults[$sKey] .= $value;
692 $aResults[$key] = $value;
695 foreach ($aCharset as $key) {
696 $value = $aResults[$key];
697 // extract the charset & language
698 $charset = substr($value,0,strpos($value,"'"));
699 $value = substr($value,strlen($charset)+
1);
700 $language = substr($value,0,strpos($value,"'"));
701 $value = substr($value,strlen($charset)+
1);
702 /* FIXME: What's the status of charset decode with language information ????
703 * Maybe language information contains only ascii text and charset_decode()
704 * only runs htmlspecialchars() on it. If it contains 8bit information, you
705 * get html encoded text in charset used by selected translation.
707 $value = charset_decode($charset,$value);
708 $aResults[$key] = $value;
714 * @param string $value
717 function parseProperties($value) {
718 $propArray = explode(';', $value);
719 $propResultArray = array();
720 foreach ($propArray as $prop) {
722 $pos = strpos($prop, '=');
724 $key = trim(substr($prop, 0, $pos));
725 $val = trim(substr($prop, $pos+
1));
726 if (strlen($val) > 0 && $val{0} == '"') {
727 $val = substr($val, 1, -1);
729 $propResultArray[$key] = $val;
732 return $this->processParameters($propResultArray);
736 * Fills disposition object in rfc822Header object
737 * @param string $value
739 function parseDisposition($value) {
740 $pos = strpos($value, ';');
743 $name = trim(substr($value, 0, $pos));
744 $props = trim(substr($value, $pos+
1));
748 $props_a = $this->parseProperties($props);
749 $disp = new Disposition($name);
750 $disp->properties
= $props_a;
751 $this->disposition
= $disp;
755 * Fills mlist array keys in rfc822Header object
756 * @param string $field
757 * @param string $value
759 function mlist($field, $value) {
761 $value_a = explode(',', $value);
762 foreach ($value_a as $val) {
764 if ($val{0} == '<') {
765 $val = substr($val, 1, -1);
767 if (substr($val, 0, 7) == 'mailto:') {
768 $res_a['mailto'] = substr($val, 7);
770 $res_a['href'] = $val;
773 $this->mlist
[$field] = $res_a;
777 * function to get the address strings out of the header.
778 * example1: header->getAddr_s('to').
779 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
780 * @param mixed $arr string or array of strings
781 * @param string $separator
782 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
785 function getAddr_s($arr, $separator = ',',$encoded=false) {
788 if (is_array($arr)) {
789 foreach($arr as $arg) {
790 if ($this->getAddr_s($arg, $separator, $encoded)) {
794 $s = ($s ?
substr($s, 2) : $s);
796 $addr = $this->{$arr};
797 if (is_array($addr)) {
798 foreach ($addr as $addr_o) {
799 if (is_object($addr_o)) {
801 $s .= $addr_o->getEncodedAddress() . $separator;
803 $s .= $addr_o->getAddress() . $separator;
807 $s = substr($s, 0, -strlen($separator));
809 if (is_object($addr)) {
811 $s .= $addr->getEncodedAddress();
813 $s .= $addr->getAddress();
822 * function to get the array of addresses out of the header.
823 * @param mixed $arg string or array of strings
824 * @param array $excl_arr array of excluded email addresses
825 * @param array $arr array of added email addresses
828 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
829 if (is_array($arg)) {
830 foreach($arg as $argument) {
831 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
834 $addr = $this->{$arg};
835 if (is_array($addr)) {
836 foreach ($addr as $next_addr) {
837 if (is_object($next_addr)) {
838 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
839 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
841 $email = $next_addr->mailbox
;
843 $email = strtolower($email);
844 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
845 $arr[$email] = $next_addr->personal
;
850 if (is_object($addr)) {
851 $email = $addr->mailbox
;
852 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
853 $email = strtolower($email);
854 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
855 $arr[$email] = $addr->personal
;
864 * @param mixed $address array or string
865 * @param boolean $recurs
866 * @return mixed array, boolean
869 function findAddress($address, $recurs = false) {
871 if (is_array($address)) {
873 foreach($address as $argument) {
874 $match = $this->findAddress($argument, true);
878 if (count($match[0]) && !$result) {
885 if (!is_array($this->cc
)) $this->cc
= array();
886 $srch_addr = $this->parseAddress($address);
888 foreach ($this->to
as $to) {
889 if ($to->host
== $srch_addr->host
) {
890 if ($to->mailbox
== $srch_addr->mailbox
) {
891 $results[] = $srch_addr;
892 if ($to->personal
== $srch_addr->personal
) {
894 return array($results, true);
902 foreach ($this->cc
as $cc) {
903 if ($cc->host
== $srch_addr->host
) {
904 if ($cc->mailbox
== $srch_addr->mailbox
) {
905 $results[] = $srch_addr;
906 if ($cc->personal
== $srch_addr->personal
) {
908 return array($results, true);
917 return array($results, false);
918 } elseif (count($result)) {
929 * @param string $type0 media type
930 * @param string $type1 media subtype
931 * @return array media properties
932 * @todo check use of media type arguments
934 function getContentType($type0, $type1) {
935 $type0 = $this->content_type
->type0
;
936 $type1 = $this->content_type
->type1
;
937 return $this->content_type
->properties
;