4 * Rfc822Header.class.php
6 * Copyright (c) 2003 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(),
40 $more_headers = array(); /* only needed for constructing headers
42 function parseHeader($hdr) {
44 $hdr = implode('', $hdr);
46 /* First we unfold the header */
47 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array(' ', ' '), $hdr));
49 /* Now we can make a new header array with */
50 /* each element representing a headerline */
51 $hdr = explode("\r\n" , $hdr);
52 foreach ($hdr as $line) {
53 $pos = strpos($line, ':');
55 $field = substr($line, 0, $pos);
56 if (!strstr($field,' ')) { /* valid field */
57 $value = trim(substr($line, $pos+
1));
58 $this->parseField($field, $value);
62 if ($this->content_type
== '') {
63 $this->parseContentType('text/plain; charset=us-ascii');
67 function stripComments($value) {
69 $cnt = strlen($value);
70 for ($i = 0; $i < $cnt; ++
$i) {
74 while ((++
$i < $cnt) && ($value{$i} != '"')) {
75 if ($value{$i} == '\\') {
79 $result .= $value{$i};
81 $result .= $value{$i};
85 while (($depth > 0) && (++
$i < $cnt)) {
102 $result .= $value{$i};
109 function parseField($field, $value) {
110 $field = strtolower($field);
113 $value = $this->stripComments($value);
114 $d = strtr($value, array(' ' => ' '));
115 $d = explode(' ', $d);
116 $this->date
= getTimeStamp($d);
119 $this->subject
= $value;
122 $this->from
= $this->parseAddress($value,true);
125 $this->sender
= $this->parseAddress($value);
128 $this->reply_to
= $this->parseAddress($value, true);
130 case 'mail-followup-to':
131 $this->mail_followup_to
= $this->parseAddress($value, true);
134 $this->to
= $this->parseAddress($value, true);
137 $this->cc
= $this->parseAddress($value, true);
140 $this->bcc
= $this->parseAddress($value, true);
143 $this->in_reply_to
= $value;
146 $value = $this->stripComments($value);
147 $this->message_id
= $value;
150 $value = $this->stripComments($value);
151 $this->references
= $value;
153 case 'x-confirm-reading-to':
154 case 'return-receipt-to':
155 case 'disposition-notification-to':
156 $value = $this->stripComments($value);
157 $this->dnt
= $this->parseAddress($value);
160 $value = $this->stripComments($value);
161 $value = str_replace(' ', '', $value);
162 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
165 $value = $this->stripComments($value);
166 $this->parseContentType($value);
168 case 'content-disposition':
169 $value = $this->stripComments($value);
170 $this->parseDisposition($value);
174 $this->xmailer
= $value;
177 $this->priority
= $value;
180 $value = $this->stripComments($value);
181 $this->mlist('post', $value);
184 $value = $this->stripComments($value);
185 $this->mlist('reply', $value);
187 case 'list-subscribe':
188 $value = $this->stripComments($value);
189 $this->mlist('subscribe', $value);
191 case 'list-unsubscribe':
192 $value = $this->stripComments($value);
193 $this->mlist('unsubscribe', $value);
196 $value = $this->stripComments($value);
197 $this->mlist('archive', $value);
200 $value = $this->stripComments($value);
201 $this->mlist('owner', $value);
204 $value = $this->stripComments($value);
205 $this->mlist('help', $value);
208 $value = $this->stripComments($value);
209 $this->mlist('id', $value);
216 function getAddressTokens($address) {
219 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
220 $aReplace = array(' (',' <',' ,',' ;',' :');
221 $address = str_replace($aSpecials,$aReplace,$address);
222 $iCnt = strlen($address);
225 $cChar = $address{$i};
229 $iEnd = strpos($address,'>',$i+
1);
231 $sToken = substr($address,$i);
234 $sToken = substr($address,$i,$iEnd - $i +
1);
237 $sToken = str_replace($aReplace, $aSpecials,$sToken);
238 if($sToken) $aTokens[] = $sToken;
241 $iEnd = strpos($address,$cChar,$i+
1);
243 // skip escaped quotes
244 $prev_char = $address{$iEnd-1};
245 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
246 $iEnd = strpos($address,$cChar,$iEnd+
1);
248 $prev_char = $address{$iEnd-1};
255 $sToken = substr($address,$i);
258 // also remove the surrounding quotes
259 $sToken = substr($address,$i+
1,$iEnd - $i -1);
262 $sToken = str_replace($aReplace, $aSpecials,$sToken);
263 if ($sToken) $aTokens[] = $sToken;
266 array_pop($aTokens); //remove inserted space
267 $iEnd = strpos($address,')',$i);
269 $sToken = substr($address,$i);
274 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
275 $cCharComment = $address{$iComment};
276 switch($cCharComment) {
291 $sToken = substr($address,$i,$iComment - $i +
1);
294 $sToken = substr($address,$i,$iEnd - $i +
1);
298 // check the next token in case comments appear in the middle of email addresses
299 $prevToken = end($aTokens);
300 if (!in_array($prevToken,$aSpecials,true)) {
301 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
302 $iEnd = strpos($address,' ',$i+
1);
304 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
307 $sNextToken = trim(substr($address,$i+
1));
312 // create token and add it again
313 $sNewToken = $prevToken . $sNextToken;
314 if($sNewToken) $aTokens[] = $sNewToken;
317 $sToken = str_replace($aReplace, $aSpecials,$sToken);
318 if($sToken) $aTokens[] = $sToken;
327 $iEnd = strpos($address,' ',$i+
1);
329 $sToken = trim(substr($address,$i,$iEnd - $i));
332 $sToken = trim(substr($address,$i));
335 if ($sToken) $aTokens[] = $sToken;
341 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
342 //$aStack=explode(' ',implode('',$aStack));
344 while (count($aStack) && !$sEmail) {
345 $sEmail = trim(array_pop($aStack));
348 if (count($aStack)) {
349 $sPersonal = trim(implode('',$aStack));
353 if (!$sPersonal && count($aComment)) {
354 $sComment = trim(implode(' ',$aComment));
355 $sPersonal .= $sComment;
357 $oAddr =& new AddressStructure();
358 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
359 $oAddr->personal
= encodeHeader($sPersonal);
361 $oAddr->personal
= $sPersonal;
363 // $oAddr->group = $sGroup;
364 $iPosAt = strpos($sEmail,'@');
366 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
367 $oAddr->host
= substr($sEmail, $iPosAt+
1);
369 $oAddr->mailbox
= $sEmail;
370 $oAddr->host
= false;
373 $aStack = $aComment = array();
378 * parseAddress: recursive function for parsing address strings and store
379 * them in an address stucture object.
380 * input: $address = string
381 * $ar = boolean (return array instead of only the
383 * $addr_ar = array with parsed addresses // obsolete
384 * $group = string // obsolete
385 * $host = string (default domainname in case of
386 * addresses without a domainname)
387 * $lookup = callback function (for lookup address
388 * strings which are probably nicks
390 * output: array with addressstructure objects or only one
391 * address_structure object.
392 * personal name: encoded: =?charset?Q|B?string?=
395 * email : <mailbox@host>
397 * This function is also used for validating addresses returned from compose
398 * That's also the reason that the function became a little bit huge
401 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
402 $aTokens = $this->getAddressTokens($address);
403 $sPersonal = $sEmail = $sComment = $sGroup = '';
404 $aStack = $aComment = array();
405 foreach ($aTokens as $sToken) {
415 $aComment[] = substr($sToken,1,-1);
419 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
420 $oAddr = end($aAddress);
421 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
422 $sEmail = $sGroup . ':;';
424 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
426 $aStack = $aComment = array();
430 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
433 $sGroup = trim(implode(' ',$aStack));
434 $sGroup = preg_replace('/\s+/',' ',$sGroup);
438 $sEmail = trim(substr($sToken,1,-1));
443 default: $aStack[] = $sToken; break;
446 /* now do the action again for the last address */
447 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
448 /* try to lookup the addresses in case of invalid email addresses */
449 $aProcessedAddress = array();
450 foreach ($aAddress as $oAddr) {
451 $aAddrBookAddress = array();
453 $grouplookup = false;
455 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
456 if (isset($aAddr['email'])) {
457 if (strpos($aAddr['email'],',')) {
459 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
461 $iPosAt = strpos($aAddr['email'], '@');
462 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
463 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
464 if (isset($aAddr['name'])) {
465 $oAddr->personal
= $aAddr['name'];
467 $oAddr->personal
= encodeHeader($sPersonal);
472 if (!$grouplookup && !$oAddr->mailbox
) {
473 $oAddr->mailbox
= trim($sEmail);
474 if ($sHost && $oAddr->mailbox
) {
475 $oAddr->host
= $sHost;
477 } else if (!$grouplookup && !$oAddr->host
) {
478 if ($sHost && $oAddr->mailbox
) {
479 $oAddr->host
= $sHost;
483 if (!$aAddrBookAddress && $oAddr->mailbox
) {
484 $aProcessedAddress[] = $oAddr;
486 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
490 return $aProcessedAddress;
492 return $aProcessedAddress[0];
496 function parseContentType($value) {
497 $pos = strpos($value, ';');
500 $type = trim(substr($value, 0, $pos));
501 $props = trim(substr($value, $pos+
1));
505 $content_type = new ContentType($type);
507 $properties = $this->parseProperties($props);
508 if (!isset($properties['charset'])) {
509 $properties['charset'] = 'us-ascii';
511 $content_type->properties
= $this->parseProperties($props);
513 $this->content_type
= $content_type;
517 function processParameters($aParameters) {
520 // handle multiline parameters
521 foreach($aParameters as $key => $value) {
522 if ($iPos = strpos($key,'*')) {
523 $sKey = substr($key,0,$iPos);
524 if (!isset($aResults[$sKey])) {
525 $aResults[$sKey] = $value;
526 if (substr($key,-1) == '*') { // parameter contains language/charset info
530 $aResults[$sKey] .= $value;
534 foreach ($aCharset as $key) {
535 $value = $aResults[$key];
536 // extract the charset & language
537 $charset = substr($value,0,strpos($value,"'"));
538 $value = substr($value,strlen($charset)+
1);
539 $language = substr($value,0,strpos($value,"'"));
540 $value = substr($value,strlen($charset)+
1);
541 // FIX ME What's the status of charset decode with language information ????
542 $value = charset_decode($charset,$value);
543 $aResults[$key] = $value;
548 function parseProperties($value) {
549 $propArray = explode(';', $value);
550 $propResultArray = array();
551 foreach ($propArray as $prop) {
553 $pos = strpos($prop, '=');
555 $key = trim(substr($prop, 0, $pos));
556 $val = trim(substr($prop, $pos+
1));
557 if ($val{0} == '"') {
558 $val = substr($val, 1, -1);
560 $propResultArray[$key] = $val;
563 return $this->processParameters($propResultArray);
566 function parseDisposition($value) {
567 $pos = strpos($value, ';');
570 $name = trim(substr($value, 0, $pos));
571 $props = trim(substr($value, $pos+
1));
575 $props_a = $this->parseProperties($props);
576 $disp = new Disposition($name);
577 $disp->properties
= $props_a;
578 $this->disposition
= $disp;
581 function mlist($field, $value) {
583 $value_a = explode(',', $value);
584 foreach ($value_a as $val) {
586 if ($val{0} == '<') {
587 $val = substr($val, 1, -1);
589 if (substr($val, 0, 7) == 'mailto:') {
590 $res_a['mailto'] = substr($val, 7);
592 $res_a['href'] = $val;
595 $this->mlist
[$field] = $res_a;
599 * function to get the addres strings out of the header.
600 * Arguments: string or array of strings !
601 * example1: header->getAddr_s('to').
602 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
604 function getAddr_s($arr, $separator = ',',$encoded=false) {
607 if (is_array($arr)) {
608 foreach($arr as $arg) {
609 if ($this->getAddr_s($arg, $separator, $encoded)) {
610 $s .= $separator . $result;
613 $s = ($s ?
substr($s, 2) : $s);
615 $addr = $this->{$arr};
616 if (is_array($addr)) {
617 foreach ($addr as $addr_o) {
618 if (is_object($addr_o)) {
620 $s .= $addr_o->getEncodedAddress() . $separator;
622 $s .= $addr_o->getAddress() . $separator;
626 $s = substr($s, 0, -strlen($separator));
628 if (is_object($addr)) {
630 $s .= $addr->getEncodedAddress();
632 $s .= $addr->getAddress();
640 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
641 if (is_array($arg)) {
642 foreach($arg as $argument) {
643 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
646 $addr = $this->{$arg};
647 if (is_array($addr)) {
648 foreach ($addr as $next_addr) {
649 if (is_object($next_addr)) {
650 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
651 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
653 $email = $next_addr->mailbox
;
655 $email = strtolower($email);
656 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
657 $arr[$email] = $next_addr->personal
;
662 if (is_object($addr)) {
663 $email = $addr->mailbox
;
664 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
665 $email = strtolower($email);
666 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
667 $arr[$email] = $addr->personal
;
675 function findAddress($address, $recurs = false) {
677 if (is_array($address)) {
679 foreach($address as $argument) {
680 $match = $this->findAddress($argument, true);
685 if (count($match[0]) && !$result) {
692 if (!is_array($this->cc
)) $this->cc
= array();
693 $srch_addr = $this->parseAddress($address);
695 foreach ($this->to
as $to) {
696 if ($to->host
== $srch_addr->host
) {
697 if ($to->mailbox
== $srch_addr->mailbox
) {
698 $results[] = $srch_addr;
699 if ($to->personal
== $srch_addr->personal
) {
701 return array($results, true);
709 foreach ($this->cc
as $cc) {
710 if ($cc->host
== $srch_addr->host
) {
711 if ($cc->mailbox
== $srch_addr->mailbox
) {
712 $results[] = $srch_addr;
713 if ($cc->personal
== $srch_addr->personal
) {
715 return array($results, true);
724 return array($results, false);
725 } elseif (count($result)) {
735 function getContentType($type0, $type1) {
736 $type0 = $this->content_type
->type0
;
737 $type1 = $this->content_type
->type1
;
738 return $this->content_type
->properties
;