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 contains functions needed to handle mime messages.
12 * @package squirrelmail
16 * input: header_string or array
17 * @package squirrelmail
25 $mail_followup_to = array(),
42 $more_headers = array(); /* only needed for constructing headers
44 function parseHeader($hdr) {
46 $hdr = implode('', $hdr);
48 /* First we replace \r\n by \n and unfold the header */
49 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
51 /* Now we can make a new header array with */
52 /* each element representing a headerline */
53 $hdr = explode("\n" , $hdr);
54 foreach ($hdr as $line) {
55 $pos = strpos($line, ':');
57 $field = substr($line, 0, $pos);
58 if (!strstr($field,' ')) { /* valid field */
59 $value = trim(substr($line, $pos+
1));
60 $this->parseField($field, $value);
64 if ($this->content_type
== '') {
65 $this->parseContentType('text/plain; charset=us-ascii');
69 function stripComments($value) {
71 $cnt = strlen($value);
72 for ($i = 0; $i < $cnt; ++
$i) {
76 while ((++
$i < $cnt) && ($value{$i} != '"')) {
77 if ($value{$i} == '\\') {
81 $result .= $value{$i};
83 $result .= $value{$i};
87 while (($depth > 0) && (++
$i < $cnt)) {
104 $result .= $value{$i};
111 function parseField($field, $value) {
112 $field = strtolower($field);
115 $value = $this->stripComments($value);
116 $d = strtr($value, array(' ' => ' '));
117 $d = explode(' ', $d);
118 $this->date
= getTimeStamp($d);
121 $this->subject
= $value;
124 $this->from
= $this->parseAddress($value,true);
127 $this->sender
= $this->parseAddress($value);
130 $this->reply_to
= $this->parseAddress($value, true);
132 case 'mail-followup-to':
133 $this->mail_followup_to
= $this->parseAddress($value, true);
136 $this->to
= $this->parseAddress($value, true);
139 $this->cc
= $this->parseAddress($value, true);
142 $this->bcc
= $this->parseAddress($value, true);
145 $this->in_reply_to
= $value;
148 $value = $this->stripComments($value);
149 $this->message_id
= $value;
152 $value = $this->stripComments($value);
153 $this->references
= $value;
155 case 'x-confirm-reading-to':
156 case 'return-receipt-to':
157 case 'disposition-notification-to':
158 $value = $this->stripComments($value);
159 $this->dnt
= $this->parseAddress($value);
162 $value = $this->stripComments($value);
163 $value = str_replace(' ', '', $value);
164 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
167 $value = $this->stripComments($value);
168 $this->parseContentType($value);
170 case 'content-disposition':
171 $value = $this->stripComments($value);
172 $this->parseDisposition($value);
174 case 'content-transfer-encoding':
175 $this->encoding
= $value;
177 case 'content-description':
178 $this->content_desc
= $value;
181 $value = $this->stripComments($value);
182 $this->content_id
= $value;
186 $this->xmailer
= $value;
191 $this->priority
= $this->parsePriority($value);
194 $value = $this->stripComments($value);
195 $this->mlist('post', $value);
198 $value = $this->stripComments($value);
199 $this->mlist('reply', $value);
201 case 'list-subscribe':
202 $value = $this->stripComments($value);
203 $this->mlist('subscribe', $value);
205 case 'list-unsubscribe':
206 $value = $this->stripComments($value);
207 $this->mlist('unsubscribe', $value);
210 $value = $this->stripComments($value);
211 $this->mlist('archive', $value);
214 $value = $this->stripComments($value);
215 $this->mlist('owner', $value);
218 $value = $this->stripComments($value);
219 $this->mlist('help', $value);
222 $value = $this->stripComments($value);
223 $this->mlist('id', $value);
230 function getAddressTokens($address) {
232 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
233 $aReplace = array(' (',' <',' ,',' ;',' :');
234 $address = str_replace($aSpecials,$aReplace,$address);
235 $iCnt = strlen($address);
238 $cChar = $address{$i};
242 $iEnd = strpos($address,'>',$i+
1);
244 $sToken = substr($address,$i);
247 $sToken = substr($address,$i,$iEnd - $i +
1);
250 $sToken = str_replace($aReplace, $aSpecials,$sToken);
251 if ($sToken) $aTokens[] = $sToken;
254 $iEnd = strpos($address,$cChar,$i+
1);
256 // skip escaped quotes
257 $prev_char = $address{$iEnd-1};
258 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
259 $iEnd = strpos($address,$cChar,$iEnd+
1);
261 $prev_char = $address{$iEnd-1};
268 $sToken = substr($address,$i);
271 // also remove the surrounding quotes
272 $sToken = substr($address,$i+
1,$iEnd - $i -1);
275 $sToken = str_replace($aReplace, $aSpecials,$sToken);
276 if ($sToken) $aTokens[] = $sToken;
279 array_pop($aTokens); //remove inserted space
280 $iEnd = strpos($address,')',$i);
282 $sToken = substr($address,$i);
287 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
288 $cCharComment = $address{$iComment};
289 switch($cCharComment) {
304 $sToken = substr($address,$i,$iComment - $i +
1);
307 $sToken = substr($address,$i,$iEnd - $i +
1);
311 // check the next token in case comments appear in the middle of email addresses
312 $prevToken = end($aTokens);
313 if (!in_array($prevToken,$aSpecials,true)) {
314 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
315 $iEnd = strpos($address,' ',$i+
1);
317 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
320 $sNextToken = trim(substr($address,$i+
1));
325 // create token and add it again
326 $sNewToken = $prevToken . $sNextToken;
327 if($sNewToken) $aTokens[] = $sNewToken;
330 $sToken = str_replace($aReplace, $aSpecials,$sToken);
331 if ($sToken) $aTokens[] = $sToken;
340 $iEnd = strpos($address,' ',$i+
1);
342 $sToken = trim(substr($address,$i,$iEnd - $i));
345 $sToken = trim(substr($address,$i));
348 if ($sToken) $aTokens[] = $sToken;
354 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
355 //$aStack=explode(' ',implode('',$aStack));
357 while (count($aStack) && !$sEmail) {
358 $sEmail = trim(array_pop($aStack));
361 if (count($aStack)) {
362 $sPersonal = trim(implode('',$aStack));
366 if (!$sPersonal && count($aComment)) {
367 $sComment = trim(implode(' ',$aComment));
368 $sPersonal .= $sComment;
370 $oAddr =& new AddressStructure();
371 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
372 $oAddr->personal
= encodeHeader($sPersonal);
374 $oAddr->personal
= $sPersonal;
376 // $oAddr->group = $sGroup;
377 $iPosAt = strpos($sEmail,'@');
379 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
380 $oAddr->host
= substr($sEmail, $iPosAt+
1);
382 $oAddr->mailbox
= $sEmail;
383 $oAddr->host
= false;
386 $aStack = $aComment = array();
391 * parseAddress: recursive function for parsing address strings and store
392 * them in an address stucture object.
393 * input: $address = string
394 * $ar = boolean (return array instead of only the
396 * $addr_ar = array with parsed addresses // obsolete
397 * $group = string // obsolete
398 * $host = string (default domainname in case of
399 * addresses without a domainname)
400 * $lookup = callback function (for lookup address
401 * strings which are probably nicks
403 * output: array with addressstructure objects or only one
404 * address_structure object.
405 * personal name: encoded: =?charset?Q|B?string?=
408 * email : <mailbox@host>
410 * This function is also used for validating addresses returned from compose
411 * That's also the reason that the function became a little bit huge
414 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
415 $aTokens = $this->getAddressTokens($address);
416 $sPersonal = $sEmail = $sGroup = '';
417 $aStack = $aComment = array();
418 foreach ($aTokens as $sToken) {
428 $aComment[] = substr($sToken,1,-1);
432 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
433 $oAddr = end($aAddress);
434 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
435 $sEmail = $sGroup . ':;';
437 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
439 $aStack = $aComment = array();
443 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
446 $sGroup = trim(implode(' ',$aStack));
447 $sGroup = preg_replace('/\s+/',' ',$sGroup);
451 $sEmail = trim(substr($sToken,1,-1));
456 default: $aStack[] = $sToken; break;
459 /* now do the action again for the last address */
460 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
461 /* try to lookup the addresses in case of invalid email addresses */
462 $aProcessedAddress = array();
463 foreach ($aAddress as $oAddr) {
464 $aAddrBookAddress = array();
466 $grouplookup = false;
468 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
469 if (isset($aAddr['email'])) {
470 if (strpos($aAddr['email'],',')) {
472 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
474 $iPosAt = strpos($aAddr['email'], '@');
475 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
476 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
477 if (isset($aAddr['name'])) {
478 $oAddr->personal
= $aAddr['name'];
480 $oAddr->personal
= encodeHeader($sPersonal);
485 if (!$grouplookup && !$oAddr->mailbox
) {
486 $oAddr->mailbox
= trim($sEmail);
487 if ($sHost && $oAddr->mailbox
) {
488 $oAddr->host
= $sHost;
490 } else if (!$grouplookup && !$oAddr->host
) {
491 if ($sHost && $oAddr->mailbox
) {
492 $oAddr->host
= $sHost;
496 if (!$aAddrBookAddress && $oAddr->mailbox
) {
497 $aProcessedAddress[] = $oAddr;
499 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
503 return $aProcessedAddress;
505 return $aProcessedAddress[0];
510 * Normalise the different Priority headers into a uniform value,
511 * namely that of the X-Priority header (1, 3, 5). Supports:
512 * Prioirty, X-Priority, Importance.
513 * X-MS-Mail-Priority is not parsed because it always coincides
514 * with one of the other headers.
516 * NOTE: this is actually a duplicate from the function in
517 * functions/imap_messages. I'm not sure if it's ok here to call
520 function parsePriority($value) {
521 $value = strtolower(array_shift(split('/\w/',trim($value))));
522 if ( is_numeric($value) ) {
525 if ( $value == 'urgent' ||
$value == 'high' ) {
527 } elseif ( $value == 'non-urgent' ||
$value == 'low' ) {
530 // default is normal priority
534 function parseContentType($value) {
535 $pos = strpos($value, ';');
538 $type = trim(substr($value, 0, $pos));
539 $props = trim(substr($value, $pos+
1));
543 $content_type = new ContentType($type);
545 $properties = $this->parseProperties($props);
546 if (!isset($properties['charset'])) {
547 $properties['charset'] = 'us-ascii';
549 $content_type->properties
= $this->parseProperties($props);
551 $this->content_type
= $content_type;
555 function processParameters($aParameters) {
558 // handle multiline parameters
559 foreach($aParameters as $key => $value) {
560 if ($iPos = strpos($key,'*')) {
561 $sKey = substr($key,0,$iPos);
562 if (!isset($aResults[$sKey])) {
563 $aResults[$sKey] = $value;
564 if (substr($key,-1) == '*') { // parameter contains language/charset info
568 $aResults[$sKey] .= $value;
571 $aResults[$key] = $value;
574 foreach ($aCharset as $key) {
575 $value = $aResults[$key];
576 // extract the charset & language
577 $charset = substr($value,0,strpos($value,"'"));
578 $value = substr($value,strlen($charset)+
1);
579 $language = substr($value,0,strpos($value,"'"));
580 $value = substr($value,strlen($charset)+
1);
581 // FIX ME What's the status of charset decode with language information ????
582 $value = charset_decode($charset,$value);
583 $aResults[$key] = $value;
588 function parseProperties($value) {
589 $propArray = explode(';', $value);
590 $propResultArray = array();
591 foreach ($propArray as $prop) {
593 $pos = strpos($prop, '=');
595 $key = trim(substr($prop, 0, $pos));
596 $val = trim(substr($prop, $pos+
1));
597 if (strlen($val) > 0 && $val{0} == '"') {
598 $val = substr($val, 1, -1);
600 $propResultArray[$key] = $val;
603 return $this->processParameters($propResultArray);
606 function parseDisposition($value) {
607 $pos = strpos($value, ';');
610 $name = trim(substr($value, 0, $pos));
611 $props = trim(substr($value, $pos+
1));
615 $props_a = $this->parseProperties($props);
616 $disp = new Disposition($name);
617 $disp->properties
= $props_a;
618 $this->disposition
= $disp;
621 function mlist($field, $value) {
623 $value_a = explode(',', $value);
624 foreach ($value_a as $val) {
626 if ($val{0} == '<') {
627 $val = substr($val, 1, -1);
629 if (substr($val, 0, 7) == 'mailto:') {
630 $res_a['mailto'] = substr($val, 7);
632 $res_a['href'] = $val;
635 $this->mlist
[$field] = $res_a;
639 * function to get the addres strings out of the header.
640 * Arguments: string or array of strings !
641 * example1: header->getAddr_s('to').
642 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
644 function getAddr_s($arr, $separator = ',',$encoded=false) {
647 if (is_array($arr)) {
648 foreach($arr as $arg) {
649 if ($this->getAddr_s($arg, $separator, $encoded)) {
653 $s = ($s ?
substr($s, 2) : $s);
655 $addr = $this->{$arr};
656 if (is_array($addr)) {
657 foreach ($addr as $addr_o) {
658 if (is_object($addr_o)) {
660 $s .= $addr_o->getEncodedAddress() . $separator;
662 $s .= $addr_o->getAddress() . $separator;
666 $s = substr($s, 0, -strlen($separator));
668 if (is_object($addr)) {
670 $s .= $addr->getEncodedAddress();
672 $s .= $addr->getAddress();
680 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
681 if (is_array($arg)) {
682 foreach($arg as $argument) {
683 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
686 $addr = $this->{$arg};
687 if (is_array($addr)) {
688 foreach ($addr as $next_addr) {
689 if (is_object($next_addr)) {
690 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
691 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
693 $email = $next_addr->mailbox
;
695 $email = strtolower($email);
696 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
697 $arr[$email] = $next_addr->personal
;
702 if (is_object($addr)) {
703 $email = $addr->mailbox
;
704 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
705 $email = strtolower($email);
706 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
707 $arr[$email] = $addr->personal
;
715 function findAddress($address, $recurs = false) {
717 if (is_array($address)) {
719 foreach($address as $argument) {
720 $match = $this->findAddress($argument, true);
724 if (count($match[0]) && !$result) {
731 if (!is_array($this->cc
)) $this->cc
= array();
732 $srch_addr = $this->parseAddress($address);
734 foreach ($this->to
as $to) {
735 if ($to->host
== $srch_addr->host
) {
736 if ($to->mailbox
== $srch_addr->mailbox
) {
737 $results[] = $srch_addr;
738 if ($to->personal
== $srch_addr->personal
) {
740 return array($results, true);
748 foreach ($this->cc
as $cc) {
749 if ($cc->host
== $srch_addr->host
) {
750 if ($cc->mailbox
== $srch_addr->mailbox
) {
751 $results[] = $srch_addr;
752 if ($cc->personal
== $srch_addr->personal
) {
754 return array($results, true);
763 return array($results, false);
764 } elseif (count($result)) {
774 function getContentType($type0, $type1) {
775 $type0 = $this->content_type
->type0
;
776 $type1 = $this->content_type
->type1
;
777 return $this->content_type
->properties
;