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.
16 * input: header_string or array
38 $more_headers = array(); /* only needed for constructing headers
40 function parseHeader($hdr) {
42 $hdr = implode('', $hdr);
44 /* First we unfold the header */
45 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
47 /* Now we can make a new header array with */
48 /* each element representing a headerline */
49 $hdr = explode("\r\n" , $hdr);
50 foreach ($hdr as $line) {
51 $pos = strpos($line, ':');
53 $field = substr($line, 0, $pos);
54 if (!strstr($field,' ')) { /* valid field */
55 $value = trim(substr($line, $pos+
1));
56 $this->parseField($field, $value);
60 if ($this->content_type
== '') {
61 $this->parseContentType('text/plain; charset=us-ascii');
65 function stripComments($value) {
67 $cnt = strlen($value);
68 for ($i = 0; $i < $cnt; ++
$i) {
72 while ((++
$i < $cnt) && ($value{$i} != '"')) {
73 if ($value{$i} == '\\') {
77 $result .= $value{$i};
79 $result .= $value{$i};
83 while (($depth > 0) && (++
$i < $cnt)) {
100 $result .= $value{$i};
107 function parseField($field, $value) {
108 $field = strtolower($field);
111 $value = $this->stripComments($value);
112 $d = strtr($value, array(' ' => ' '));
113 $d = explode(' ', $d);
114 $this->date
= getTimeStamp($d);
117 $this->subject
= $value;
120 $this->from
= $this->parseAddress($value,true);
123 $this->sender
= $this->parseAddress($value);
126 $this->reply_to
= $this->parseAddress($value, true);
129 $this->to
= $this->parseAddress($value, true);
132 $this->cc
= $this->parseAddress($value, true);
135 $this->bcc
= $this->parseAddress($value, true);
138 $this->in_reply_to
= $value;
141 $value = $this->stripComments($value);
142 $this->message_id
= $value;
145 $value = $this->stripComments($value);
146 $this->references
= $value;
148 case 'x-confirm-reading-to':
149 case 'return-receipt-to':
150 case 'disposition-notification-to':
151 $value = $this->stripComments($value);
152 $this->dnt
= $this->parseAddress($value);
155 $value = $this->stripComments($value);
156 $value = str_replace(' ', '', $value);
157 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
160 $value = $this->stripComments($value);
161 $this->parseContentType($value);
163 case 'content-disposition':
164 $value = $this->stripComments($value);
165 $this->parseDisposition($value);
169 $this->xmailer
= $value;
172 $this->priority
= $value;
175 $value = $this->stripComments($value);
176 $this->mlist('post', $value);
179 $value = $this->stripComments($value);
180 $this->mlist('reply', $value);
182 case 'list-subscribe':
183 $value = $this->stripComments($value);
184 $this->mlist('subscribe', $value);
186 case 'list-unsubscribe':
187 $value = $this->stripComments($value);
188 $this->mlist('unsubscribe', $value);
191 $value = $this->stripComments($value);
192 $this->mlist('archive', $value);
195 $value = $this->stripComments($value);
196 $this->mlist('owner', $value);
199 $value = $this->stripComments($value);
200 $this->mlist('help', $value);
203 $value = $this->stripComments($value);
204 $this->mlist('id', $value);
211 function getAddressTokens($address) {
214 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
215 $aReplace = array(' (',' <',' ,',' ;',' :');
216 $address = str_replace($aSpecials,$aReplace,$address);
217 $iCnt = strlen($address);
220 $cChar = $address{$i};
224 $iEnd = strpos($address,'>',$i+
1);
226 $sToken = substr($address,$i);
229 $sToken = substr($address,$i,$iEnd - $i +
1);
232 $sToken = str_replace($aReplace, $aSpecials,$sToken);
233 $aTokens[] = $sToken;
236 $iEnd = strpos($address,$cChar,$i+
1);
238 // skip escaped quotes
239 $prev_char = $address{$iEnd-1};
240 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
241 $iEnd = strpos($address,$cChar,$iEnd+
1);
243 $prev_char = $address{$iEnd-1};
250 $sToken = substr($address,$i);
253 // also remove the surrounding quotes
254 $sToken = substr($address,$i+
1,$iEnd - $i -1);
257 $sToken = str_replace($aReplace, $aSpecials,$sToken);
258 if ($sToken) $aTokens[] = $sToken;
261 array_pop($aTokens); //remove inserted space
262 $iEnd = strpos($address,')',$i);
264 $sToken = substr($address,$i);
269 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
270 $cCharComment = $address{$iComment};
271 switch($cCharComment) {
286 $sToken = substr($address,$i,$iComment - $i +
1);
289 $sToken = substr($address,$i,$iEnd - $i +
1);
293 // check the next token in case comments appear in the middle of email addresses
294 $prevToken = end($aTokens);
295 if (!in_array($prevToken,$aSpecials,true)) {
296 if ($i+
1<strlen($address) && !in_array($address{$i+
1},$aSpecials,true)) {
297 $iEnd = strpos($address,' ',$i+
1);
299 $sNextToken = trim(substr($address,$i+
1,$iEnd - $i -1));
302 $sToken = trim(substr($address,$i+
1));
307 // create token and add it again
308 $sNewToken = $prevToken . $sNextToken;
309 $aTokens[] = $sNewToken;
312 $sToken = str_replace($aReplace, $aSpecials,$sToken);
313 $aTokens[] = $sToken;
322 $iEnd = strpos($address,' ',$i+
1);
324 $sToken = trim(substr($address,$i,$iEnd - $i));
327 $sToken = trim(substr($address,$i));
330 if ($sToken) $aTokens[] = $sToken;
336 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
337 //$aStack=explode(' ',implode('',$aStack));
339 while (count($aStack) && !$sEmail) {
340 $sEmail = trim(array_pop($aStack));
343 if (count($aStack)) {
344 $sPersonal = trim(implode('',$aStack));
348 if (!$sPersonal && count($aComment)) {
349 $sComment = trim(implode(' ',$aComment));
350 $sPersonal .= $sComment;
352 $oAddr =& new AddressStructure();
353 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
354 $oAddr->personal
= encodeHeader($sPersonal);
356 $oAddr->personal
= $sPersonal;
358 // $oAddr->group = $sGroup;
359 $iPosAt = strpos($sEmail,'@');
361 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
362 $oAddr->host
= substr($sEmail, $iPosAt+
1);
364 $oAddr->mailbox
= $sEmail;
365 $oAddr->host
= false;
368 $aStack = $aComment = array();
373 * parseAddress: recursive function for parsing address strings and store
374 * them in an address stucture object.
375 * input: $address = string
376 * $ar = boolean (return array instead of only the
378 * $addr_ar = array with parsed addresses // obsolete
379 * $group = string // obsolete
380 * $host = string (default domainname in case of
381 * addresses without a domainname)
382 * $lookup = callback function (for lookup address
383 * strings which are probably nicks
385 * output: array with addressstructure objects or only one
386 * address_structure object.
387 * personal name: encoded: =?charset?Q|B?string?=
390 * email : <mailbox@host>
392 * This function is also used for validating addresses returned from compose
393 * That's also the reason that the function became a little bit huge
396 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
397 $aTokens = $this->getAddressTokens($address);
398 $sPersonal = $sEmail = $sComment = $sGroup = '';
399 $aStack = $aComment = array();
400 foreach ($aTokens as $sToken) {
410 $aComment[] = substr($sToken,1,-1);
414 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
415 $oAddr = end($aAddress);
416 if(!$oAddr ||
((isset($oAddr)) && !$oAddr->mailbox
&& !$oAddr->personal
)) {
417 $sEmail = $sGroup . ':;';
419 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
421 $aStack = $aComment = array();
425 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
428 $sGroup = trim(implode(' ',$aStack));
429 $sGroup = preg_replace('/\s+/',' ',$sGroup);
433 $sEmail = trim(substr($sToken,1,-1));
438 default: $aStack[] = $sToken; break;
441 /* now do the action again for the last address */
442 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
443 /* try to lookup the addresses in case of invalid email addresses */
444 $aProcessedAddress = array();
445 foreach ($aAddress as $oAddr) {
446 $aAddrBookAddress = array();
448 $grouplookup = false;
450 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
451 if (isset($aAddr['email'])) {
452 if (strpos($aAddr['email'],',')) {
454 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
456 $iPosAt = strpos($aAddr['email'], '@');
457 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
458 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
459 if (isset($aAddr['name'])) {
460 $oAddr->personal
= $aAddr['name'];
462 $oAddr->personal
= encodeHeader($sPersonal);
467 if (!$grouplookup && !$oAddr->mailbox
) {
468 $oAddr->mailbox
= trim($sEmail);
469 if ($sHost && $oAddr->mailbox
) {
470 $oAddr->host
= $sHost;
474 if (!$aAddrBookAddress && $oAddr->mailbox
) {
475 $aProcessedAddress[] = $oAddr;
477 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
481 return $aProcessedAddress;
483 return $aProcessedAddress[0];
487 function parseContentType($value) {
488 $pos = strpos($value, ';');
491 $type = trim(substr($value, 0, $pos));
492 $props = trim(substr($value, $pos+
1));
496 $content_type = new ContentType($type);
498 $properties = $this->parseProperties($props);
499 if (!isset($properties['charset'])) {
500 $properties['charset'] = 'us-ascii';
502 $content_type->properties
= $this->parseProperties($props);
504 $this->content_type
= $content_type;
508 function processParameters($aParameters) {
511 // handle multiline parameters
512 foreach($aParameters as $key => $value) {
513 if ($iPos = strpos($key,'*')) {
514 $sKey = substr($key,0,$iPos);
515 if (!isset($aResults[$sKey])) {
516 $aResults[$sKey] = $value;
517 if (substr($key,-1) == '*') { // parameter contains language/charset info
521 $aResults[$sKey] .= $value;
525 foreach ($aCharset as $key) {
526 $value = $aResults[$key];
527 // extract the charset & language
528 $charset = substr($value,0,strpos($value,"'"));
529 $value = substr($value,strlen($charset)+
1);
530 $language = substr($value,0,strpos($value,"'"));
531 $value = substr($value,strlen($charset)+
1);
532 // FIX ME What's the status of charset decode with language information ????
533 $value = charset_decode($charset,$value);
534 $aResults[$key] = $value;
539 function parseProperties($value) {
540 $propArray = explode(';', $value);
541 $propResultArray = array();
542 foreach ($propArray as $prop) {
544 $pos = strpos($prop, '=');
546 $key = trim(substr($prop, 0, $pos));
547 $val = trim(substr($prop, $pos+
1));
548 if ($val{0} == '"') {
549 $val = substr($val, 1, -1);
551 $propResultArray[$key] = $val;
554 return $this->processParameters($propResultArray);
557 function parseDisposition($value) {
558 $pos = strpos($value, ';');
561 $name = trim(substr($value, 0, $pos));
562 $props = trim(substr($value, $pos+
1));
566 $props_a = $this->parseProperties($props);
567 $disp = new Disposition($name);
568 $disp->properties
= $props_a;
569 $this->disposition
= $disp;
572 function mlist($field, $value) {
574 $value_a = explode(',', $value);
575 foreach ($value_a as $val) {
577 if ($val{0} == '<') {
578 $val = substr($val, 1, -1);
580 if (substr($val, 0, 7) == 'mailto:') {
581 $res_a['mailto'] = substr($val, 7);
583 $res_a['href'] = $val;
586 $this->mlist
[$field] = $res_a;
590 * function to get the addres strings out of the header.
591 * Arguments: string or array of strings !
592 * example1: header->getAddr_s('to').
593 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
595 function getAddr_s($arr, $separator = ',',$encoded=false) {
598 if (is_array($arr)) {
599 foreach($arr as $arg) {
600 if ($this->getAddr_s($arg, $separator, $encoded)) {
601 $s .= $separator . $result;
604 $s = ($s ?
substr($s, 2) : $s);
606 $addr = $this->{$arr};
607 if (is_array($addr)) {
608 foreach ($addr as $addr_o) {
609 if (is_object($addr_o)) {
611 $s .= $addr_o->getEncodedAddress() . $separator;
613 $s .= $addr_o->getAddress() . $separator;
617 $s = substr($s, 0, -strlen($separator));
619 if (is_object($addr)) {
621 $s .= $addr->getEncodedAddress();
623 $s .= $addr->getAddress();
631 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
632 if (is_array($arg)) {
633 foreach($arg as $argument) {
634 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
637 $addr = $this->{$arg};
638 if (is_array($addr)) {
639 foreach ($addr as $next_addr) {
640 if (is_object($next_addr)) {
641 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
642 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
644 $email = $next_addr->mailbox
;
646 $email = strtolower($email);
647 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
648 $arr[$email] = $next_addr->personal
;
653 if (is_object($addr)) {
654 $email = $addr->mailbox
;
655 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
656 $email = strtolower($email);
657 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
658 $arr[$email] = $addr->personal
;
666 function findAddress($address, $recurs = false) {
668 if (is_array($address)) {
670 foreach($address as $argument) {
671 $match = $this->findAddress($argument, true);
676 if (count($match[0]) && !$result) {
683 if (!is_array($this->cc
)) $this->cc
= array();
684 $srch_addr = $this->parseAddress($address);
686 foreach ($this->to
as $to) {
687 if ($to->host
== $srch_addr->host
) {
688 if ($to->mailbox
== $srch_addr->mailbox
) {
689 $results[] = $srch_addr;
690 if ($to->personal
== $srch_addr->personal
) {
692 return array($results, true);
700 foreach ($this->cc
as $cc) {
701 if ($cc->host
== $srch_addr->host
) {
702 if ($cc->mailbox
== $srch_addr->mailbox
) {
703 $results[] = $srch_addr;
704 if ($cc->personal
== $srch_addr->personal
) {
706 return array($results, true);
715 return array($results, false);
716 } elseif (count($result)) {
726 function getContentType($type0, $type1) {
727 $type0 = $this->content_type
->type0
;
728 $type1 = $this->content_type
->type1
;
729 return $this->content_type
->properties
;