4 * Rfc822Header.class.php
6 * Copyright (c) 2003-2004 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;
189 $this->priority
= $value;
192 $value = $this->stripComments($value);
193 $this->mlist('post', $value);
196 $value = $this->stripComments($value);
197 $this->mlist('reply', $value);
199 case 'list-subscribe':
200 $value = $this->stripComments($value);
201 $this->mlist('subscribe', $value);
203 case 'list-unsubscribe':
204 $value = $this->stripComments($value);
205 $this->mlist('unsubscribe', $value);
208 $value = $this->stripComments($value);
209 $this->mlist('archive', $value);
212 $value = $this->stripComments($value);
213 $this->mlist('owner', $value);
216 $value = $this->stripComments($value);
217 $this->mlist('help', $value);
220 $value = $this->stripComments($value);
221 $this->mlist('id', $value);
228 function getAddressTokens($address) {
230 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
231 $aReplace = array(' (',' <',' ,',' ;',' :');
232 $address = str_replace($aSpecials,$aReplace,$address);
233 $iCnt = strlen($address);
236 $cChar = $address{$i};
240 $iEnd = strpos($address,'>',$i+
1);
242 $sToken = substr($address,$i);
245 $sToken = substr($address,$i,$iEnd - $i +
1);
248 $sToken = str_replace($aReplace, $aSpecials,$sToken);
249 if ($sToken) $aTokens[] = $sToken;
252 $iEnd = strpos($address,$cChar,$i+
1);
254 // skip escaped quotes
255 $prev_char = $address{$iEnd-1};
256 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
257 $iEnd = strpos($address,$cChar,$iEnd+
1);
259 $prev_char = $address{$iEnd-1};
266 $sToken = substr($address,$i);
269 // also remove the surrounding quotes
270 $sToken = substr($address,$i+
1,$iEnd - $i -1);
273 $sToken = str_replace($aReplace, $aSpecials,$sToken);
274 if ($sToken) $aTokens[] = $sToken;
277 array_pop($aTokens); //remove inserted space
278 $iEnd = strpos($address,')',$i);
280 $sToken = substr($address,$i);
285 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
286 $cCharComment = $address{$iComment};
287 switch($cCharComment) {
302 $sToken = substr($address,$i,$iComment - $i +
1);
305 $sToken = substr($address,$i,$iEnd - $i +
1);
309 // check the next token in case comments appear in the middle of email addresses
310 $prevToken = end($aTokens);
311 if (!in_array($prevToken,$aSpecials,true)) {
312 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
313 $iEnd = strpos($address,' ',$i+
1);
315 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
318 $sNextToken = trim(substr($address,$i+
1));
323 // create token and add it again
324 $sNewToken = $prevToken . $sNextToken;
325 if($sNewToken) $aTokens[] = $sNewToken;
328 $sToken = str_replace($aReplace, $aSpecials,$sToken);
329 if ($sToken) $aTokens[] = $sToken;
338 $iEnd = strpos($address,' ',$i+
1);
340 $sToken = trim(substr($address,$i,$iEnd - $i));
343 $sToken = trim(substr($address,$i));
346 if ($sToken) $aTokens[] = $sToken;
352 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
353 //$aStack=explode(' ',implode('',$aStack));
355 while (count($aStack) && !$sEmail) {
356 $sEmail = trim(array_pop($aStack));
359 if (count($aStack)) {
360 $sPersonal = trim(implode('',$aStack));
364 if (!$sPersonal && count($aComment)) {
365 $sComment = trim(implode(' ',$aComment));
366 $sPersonal .= $sComment;
368 $oAddr =& new AddressStructure();
369 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
370 $oAddr->personal
= encodeHeader($sPersonal);
372 $oAddr->personal
= $sPersonal;
374 // $oAddr->group = $sGroup;
375 $iPosAt = strpos($sEmail,'@');
377 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
378 $oAddr->host
= substr($sEmail, $iPosAt+
1);
380 $oAddr->mailbox
= $sEmail;
381 $oAddr->host
= false;
384 $aStack = $aComment = array();
389 * parseAddress: recursive function for parsing address strings and store
390 * them in an address stucture object.
391 * input: $address = string
392 * $ar = boolean (return array instead of only the
394 * $addr_ar = array with parsed addresses // obsolete
395 * $group = string // obsolete
396 * $host = string (default domainname in case of
397 * addresses without a domainname)
398 * $lookup = callback function (for lookup address
399 * strings which are probably nicks
401 * output: array with addressstructure objects or only one
402 * address_structure object.
403 * personal name: encoded: =?charset?Q|B?string?=
406 * email : <mailbox@host>
408 * This function is also used for validating addresses returned from compose
409 * That's also the reason that the function became a little bit huge
412 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
413 $aTokens = $this->getAddressTokens($address);
414 $sPersonal = $sEmail = $sGroup = '';
415 $aStack = $aComment = array();
416 foreach ($aTokens as $sToken) {
426 $aComment[] = substr($sToken,1,-1);
430 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
431 $oAddr = end($aAddress);
432 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
433 $sEmail = $sGroup . ':;';
435 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
437 $aStack = $aComment = array();
441 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
444 $sGroup = trim(implode(' ',$aStack));
445 $sGroup = preg_replace('/\s+/',' ',$sGroup);
449 $sEmail = trim(substr($sToken,1,-1));
454 default: $aStack[] = $sToken; break;
457 /* now do the action again for the last address */
458 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
459 /* try to lookup the addresses in case of invalid email addresses */
460 $aProcessedAddress = array();
461 foreach ($aAddress as $oAddr) {
462 $aAddrBookAddress = array();
464 $grouplookup = false;
466 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
467 if (isset($aAddr['email'])) {
468 if (strpos($aAddr['email'],',')) {
470 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
472 $iPosAt = strpos($aAddr['email'], '@');
473 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
474 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
475 if (isset($aAddr['name'])) {
476 $oAddr->personal
= $aAddr['name'];
478 $oAddr->personal
= encodeHeader($sPersonal);
483 if (!$grouplookup && !$oAddr->mailbox
) {
484 $oAddr->mailbox
= trim($sEmail);
485 if ($sHost && $oAddr->mailbox
) {
486 $oAddr->host
= $sHost;
488 } else if (!$grouplookup && !$oAddr->host
) {
489 if ($sHost && $oAddr->mailbox
) {
490 $oAddr->host
= $sHost;
494 if (!$aAddrBookAddress && $oAddr->mailbox
) {
495 $aProcessedAddress[] = $oAddr;
497 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
501 return $aProcessedAddress;
503 return $aProcessedAddress[0];
507 function parseContentType($value) {
508 $pos = strpos($value, ';');
511 $type = trim(substr($value, 0, $pos));
512 $props = trim(substr($value, $pos+
1));
516 $content_type = new ContentType($type);
518 $properties = $this->parseProperties($props);
519 if (!isset($properties['charset'])) {
520 $properties['charset'] = 'us-ascii';
522 $content_type->properties
= $this->parseProperties($props);
524 $this->content_type
= $content_type;
528 function processParameters($aParameters) {
531 // handle multiline parameters
532 foreach($aParameters as $key => $value) {
533 if ($iPos = strpos($key,'*')) {
534 $sKey = substr($key,0,$iPos);
535 if (!isset($aResults[$sKey])) {
536 $aResults[$sKey] = $value;
537 if (substr($key,-1) == '*') { // parameter contains language/charset info
541 $aResults[$sKey] .= $value;
544 $aResults[$key] = $value;
547 foreach ($aCharset as $key) {
548 $value = $aResults[$key];
549 // extract the charset & language
550 $charset = substr($value,0,strpos($value,"'"));
551 $value = substr($value,strlen($charset)+
1);
552 $language = substr($value,0,strpos($value,"'"));
553 $value = substr($value,strlen($charset)+
1);
554 // FIX ME What's the status of charset decode with language information ????
555 $value = charset_decode($charset,$value);
556 $aResults[$key] = $value;
561 function parseProperties($value) {
562 $propArray = explode(';', $value);
563 $propResultArray = array();
564 foreach ($propArray as $prop) {
566 $pos = strpos($prop, '=');
568 $key = trim(substr($prop, 0, $pos));
569 $val = trim(substr($prop, $pos+
1));
570 if (strlen($val) > 0 && $val{0} == '"') {
571 $val = substr($val, 1, -1);
573 $propResultArray[$key] = $val;
576 return $this->processParameters($propResultArray);
579 function parseDisposition($value) {
580 $pos = strpos($value, ';');
583 $name = trim(substr($value, 0, $pos));
584 $props = trim(substr($value, $pos+
1));
588 $props_a = $this->parseProperties($props);
589 $disp = new Disposition($name);
590 $disp->properties
= $props_a;
591 $this->disposition
= $disp;
594 function mlist($field, $value) {
596 $value_a = explode(',', $value);
597 foreach ($value_a as $val) {
599 if ($val{0} == '<') {
600 $val = substr($val, 1, -1);
602 if (substr($val, 0, 7) == 'mailto:') {
603 $res_a['mailto'] = substr($val, 7);
605 $res_a['href'] = $val;
608 $this->mlist
[$field] = $res_a;
612 * function to get the addres strings out of the header.
613 * Arguments: string or array of strings !
614 * example1: header->getAddr_s('to').
615 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
617 function getAddr_s($arr, $separator = ',',$encoded=false) {
620 if (is_array($arr)) {
621 foreach($arr as $arg) {
622 if ($this->getAddr_s($arg, $separator, $encoded)) {
626 $s = ($s ?
substr($s, 2) : $s);
628 $addr = $this->{$arr};
629 if (is_array($addr)) {
630 foreach ($addr as $addr_o) {
631 if (is_object($addr_o)) {
633 $s .= $addr_o->getEncodedAddress() . $separator;
635 $s .= $addr_o->getAddress() . $separator;
639 $s = substr($s, 0, -strlen($separator));
641 if (is_object($addr)) {
643 $s .= $addr->getEncodedAddress();
645 $s .= $addr->getAddress();
653 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
654 if (is_array($arg)) {
655 foreach($arg as $argument) {
656 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
659 $addr = $this->{$arg};
660 if (is_array($addr)) {
661 foreach ($addr as $next_addr) {
662 if (is_object($next_addr)) {
663 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
664 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
666 $email = $next_addr->mailbox
;
668 $email = strtolower($email);
669 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
670 $arr[$email] = $next_addr->personal
;
675 if (is_object($addr)) {
676 $email = $addr->mailbox
;
677 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
678 $email = strtolower($email);
679 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
680 $arr[$email] = $addr->personal
;
688 function findAddress($address, $recurs = false) {
690 if (is_array($address)) {
692 foreach($address as $argument) {
693 $match = $this->findAddress($argument, true);
697 if (count($match[0]) && !$result) {
704 if (!is_array($this->cc
)) $this->cc
= array();
705 $srch_addr = $this->parseAddress($address);
707 foreach ($this->to
as $to) {
708 if ($to->host
== $srch_addr->host
) {
709 if ($to->mailbox
== $srch_addr->mailbox
) {
710 $results[] = $srch_addr;
711 if ($to->personal
== $srch_addr->personal
) {
713 return array($results, true);
721 foreach ($this->cc
as $cc) {
722 if ($cc->host
== $srch_addr->host
) {
723 if ($cc->mailbox
== $srch_addr->mailbox
) {
724 $results[] = $srch_addr;
725 if ($cc->personal
== $srch_addr->personal
) {
727 return array($results, true);
736 return array($results, false);
737 } elseif (count($result)) {
747 function getContentType($type0, $type1) {
748 $type0 = $this->content_type
->type0
;
749 $type1 = $this->content_type
->type1
;
750 return $this->content_type
->properties
;