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 $sToken = substr($address,$i);
241 // also remove the surrounding quotes
242 $sToken = substr($address,$i+
1,$iEnd - $i -1);
245 $sToken = str_replace($aReplace, $aSpecials,$sToken);
246 if ($sToken) $aTokens[] = $sToken;
249 $iEnd = strpos($address,')',$i);
251 $sToken = substr($address,$i);
256 while (($iDepth > 0) && (++
$iComment < $iCnt)) {
257 $cCharComment = $address{$iComment};
258 switch($cCharComment) {
273 $sToken = substr($address,$i,$iComment - $i +
1);
276 $sToken = substr($address,$i,$iEnd - $i +
1);
280 $sToken = str_replace($aReplace, $aSpecials,$sToken);
281 $aTokens[] = $sToken;
290 $iEnd = strpos($address,' ',$i+
1);
292 $sToken = trim(substr($address,$i,$iEnd - $i));
295 $sToken = trim(substr($address,$i));
298 if ($sToken) $aTokens[] = $sToken;
304 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
306 while (count($aStack) && !$sEmail) {
307 $sEmail = trim(array_pop($aStack));
310 if (count($aStack)) {
311 $sPersonal = trim(implode('',$aStack));
315 if (!$sPersonal && count($aComment)) {
316 $sComment = trim(implode(' ',$aComment));
317 $sPersonal .= $sComment;
319 $oAddr =& new AddressStructure();
320 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
321 $oAddr->personal
= encodeHeader($sPersonal);
323 $oAddr->personal
= $sPersonal;
325 $oAddr->group
= $sGroup;
326 $iPosAt = strpos($sEmail,'@');
328 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
329 $oAddr->host
= substr($sEmail, $iPosAt+
1);
331 $oAddr->mailbox
= $sEmail;
332 $oAddr->host
= false;
334 $oAddr->group
= $sGroup;
336 $aStack = $aComment = array();
341 * parseAddress: recursive function for parsing address strings and store
342 * them in an address stucture object.
343 * input: $address = string
344 * $ar = boolean (return array instead of only the
346 * $addr_ar = array with parsed addresses // obsolete
347 * $group = string // obsolete
348 * $host = string (default domainname in case of
349 * addresses without a domainname)
350 * $lookup = callback function (for lookup address
351 * strings which are probably nicks
353 * output: array with addressstructure objects or only one
354 * address_structure object.
355 * personal name: encoded: =?charset?Q|B?string?=
358 * email : <mailbox@host>
360 * This function is also used for validating addresses returned from compose
361 * That's also the reason that the function became a little bit huge
364 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
365 $aTokens = $this->getAddressTokens($address);
366 $sPersonal = $sEmail = $sComment = $sGroup = '';
367 $aStack = $aComment = array();
368 foreach ($aTokens as $sToken) {
378 $aComment[] = substr($sToken,1,-1);
382 $oAddr = end($aAddress);
383 if ($oAddr && $oAddr->group
== $sGroup) {
384 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
387 $aAddress[] = $this->createAddressObject(array(),array(),$sGroup,'');
390 $aStack = $aComment = array();
394 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
397 $sGroup = implode(' ',$aStack); break;
401 $sEmail = trim(substr($sToken,1,-1));
406 default: $aStack[] = $sToken; break;
409 /* now do the action again for the last address */
410 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
411 /* try to lookup the addresses in case of invalid email addresses */
412 $aProcessedAddress = array();
413 foreach ($aAddress as $oAddr) {
414 $aAddrBookAddress = array();
416 $grouplookup = false;
418 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
419 if (isset($aAddr['email'])) {
420 if (strpos($aAddr['email'],',')) {
422 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
424 $iPosAt = strpos($aAddr['email'], '@');
425 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
426 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
427 if (isset($aAddr['name'])) {
428 $oAddr->personal
= $aAddr['name'];
430 $oAddr->personal
= encodeHeader($sPersonal);
435 if (!$grouplookup && !$oAddr->mailbox
) {
436 $oAddr->mailbox
= trim($sEmail);
437 if ($sHost && $oAddr->mailbox
) {
438 $oAddr->host
= $sHost;
442 if (!$aAddrBookAddress && $oAddr->mailbox
) {
443 $aProcessedAddress[] = $oAddr;
445 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
449 return $aProcessedAddress;
451 return $aProcessedAddress[0];
455 function parseContentType($value) {
456 $pos = strpos($value, ';');
459 $type = trim(substr($value, 0, $pos));
460 $props = trim(substr($value, $pos+
1));
464 $content_type = new ContentType($type);
466 $properties = $this->parseProperties($props);
467 if (!isset($properties['charset'])) {
468 $properties['charset'] = 'us-ascii';
470 $content_type->properties
= $this->parseProperties($props);
472 $this->content_type
= $content_type;
475 function parseProperties($value) {
476 $propArray = explode(';', $value);
477 $propResultArray = array();
478 foreach ($propArray as $prop) {
480 $pos = strpos($prop, '=');
482 $key = trim(substr($prop, 0, $pos));
483 $val = trim(substr($prop, $pos+
1));
484 if ($val{0} == '"') {
485 $val = substr($val, 1, -1);
487 $propResultArray[$key] = $val;
490 return $propResultArray;
493 function parseDisposition($value) {
494 $pos = strpos($value, ';');
497 $name = trim(substr($value, 0, $pos));
498 $props = trim(substr($value, $pos+
1));
502 $props_a = $this->parseProperties($props);
503 $disp = new Disposition($name);
504 $disp->properties
= $props_a;
505 $this->disposition
= $disp;
508 function mlist($field, $value) {
510 $value_a = explode(',', $value);
511 foreach ($value_a as $val) {
513 if ($val{0} == '<') {
514 $val = substr($val, 1, -1);
516 if (substr($val, 0, 7) == 'mailto:') {
517 $res_a['mailto'] = substr($val, 7);
519 $res_a['href'] = $val;
522 $this->mlist
[$field] = $res_a;
526 * function to get the addres strings out of the header.
527 * Arguments: string or array of strings !
528 * example1: header->getAddr_s('to').
529 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
531 function getAddr_s($arr, $separator = ',',$encoded=false) {
534 if (is_array($arr)) {
535 foreach($arr as $arg) {
536 if ($this->getAddr_s($arg, $separator, $encoded)) {
537 $s .= $separator . $result;
540 $s = ($s ?
substr($s, 2) : $s);
542 $addr = $this->{$arr};
543 if (is_array($addr)) {
544 foreach ($addr as $addr_o) {
545 if (is_object($addr_o)) {
547 $s .= $addr_o->getEncodedAddress() . $separator;
549 $s .= $addr_o->getAddress() . $separator;
553 $s = substr($s, 0, -strlen($separator));
555 if (is_object($addr)) {
557 $s .= $addr->getEncodedAddress();
559 $s .= $addr->getAddress();
567 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
568 if (is_array($arg)) {
569 foreach($arg as $argument) {
570 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
573 $addr = $this->{$arg};
574 if (is_array($addr)) {
575 foreach ($addr as $next_addr) {
576 if (is_object($next_addr)) {
577 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
578 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
580 $email = $next_addr->mailbox
;
582 $email = strtolower($email);
583 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
584 $arr[$email] = $next_addr->personal
;
589 if (is_object($addr)) {
590 $email = $addr->mailbox
;
591 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
592 $email = strtolower($email);
593 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
594 $arr[$email] = $addr->personal
;
602 function findAddress($address, $recurs = false) {
604 if (is_array($address)) {
606 foreach($address as $argument) {
607 $match = $this->findAddress($argument, true);
612 if (count($match[0]) && !$result) {
619 if (!is_array($this->cc
)) $this->cc
= array();
620 $srch_addr = $this->parseAddress($address);
622 foreach ($this->to
as $to) {
623 if ($to->host
== $srch_addr->host
) {
624 if ($to->mailbox
== $srch_addr->mailbox
) {
625 $results[] = $srch_addr;
626 if ($to->personal
== $srch_addr->personal
) {
628 return array($results, true);
636 foreach ($this->cc
as $cc) {
637 if ($cc->host
== $srch_addr->host
) {
638 if ($cc->mailbox
== $srch_addr->mailbox
) {
639 $results[] = $srch_addr;
640 if ($cc->personal
== $srch_addr->personal
) {
642 return array($results, true);
651 return array($results, false);
652 } elseif (count($result)) {
662 function getContentType($type0, $type1) {
663 $type0 = $this->content_type
->type0
;
664 $type1 = $this->content_type
->type1
;
665 return $this->content_type
->properties
;