0855604dd930da0c29ae289797813287f3e32e86
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 $iCnt = strlen($address);
215 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
216 $aReplace = array(' (',' <',' ,',' ;',' :');
217 $address = str_replace($aSpecials,$aReplace,$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 $aTokens[] = $sToken;
249 $iEnd = strpos($address,')',$i);
251 $sToken = substr($address,$i);
254 $sToken = substr($address,$i,$iEnd - $i +
1);
257 $sToken = str_replace($aReplace, $aSpecials,$sToken);
258 $aTokens[] = $sToken;
267 $iEnd = strpos($address,' ',$i+
1);
269 $sToken = trim(substr($address,$i,$iEnd - $i));
272 $sToken = trim(substr($address,$i));
275 if ($sToken) $aTokens[] = $sToken;
281 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
283 while (count($aStack) && !$sEmail) {
284 $sEmail = trim(array_pop($aStack));
287 if (count($aStack)) {
288 $sPersonal = trim(implode('',$aStack));
292 if (!$sPersonal && count($aComment)) {
293 $sComment = trim(implode(' ',$aComment));
294 $sPersonal .= $sComment;
296 $oAddr =& new AddressStructure();
297 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
298 $oAddr->personal
= encodeHeader($sPersonal);
300 $oAddr->personal
= $sPersonal;
302 $oAddr->group
= $sGroup;
303 $iPosAt = strpos($sEmail,'@');
305 $oAddr->mailbox
= substr($sEmail, 0, $iPosAt);
306 $oAddr->host
= substr($sEmail, $iPosAt+
1);
308 $oAddr->mailbox
= $sEmail;
309 $oAddr->host
= false;
311 $oAddr->group
= $sGroup;
313 $aStack = $aComment = array();
318 * parseAddress: recursive function for parsing address strings and store
319 * them in an address stucture object.
320 * input: $address = string
321 * $ar = boolean (return array instead of only the
323 * $addr_ar = array with parsed addresses // obsolete
324 * $group = string // obsolete
325 * $host = string (default domainname in case of
326 * addresses without a domainname)
327 * $lookup = callback function (for lookup address
328 * strings which are probably nicks
330 * output: array with addressstructure objects or only one
331 * address_structure object.
332 * personal name: encoded: =?charset?Q|B?string?=
335 * email : <mailbox@host>
337 * This function is also used for validating addresses returned from compose
338 * That's also the reason that the function became a little bit huge
341 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
342 $aTokens = $this->getAddressTokens($address);
343 $sPersonal = $sEmail = $sComment = $sGroup = '';
344 $aStack = $aComment = array();
345 foreach ($aTokens as $sToken) {
355 $aComment[] = substr($sToken,1,-1);
359 $oAddr = end($aAddress);
360 if ($oAddr && $oAddr->group
== $sGroup) {
361 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
364 $aAddress[] = $this->createAddressObject(array(),array(),$sGroup,'');
367 $aStack = $aComment = array();
371 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
374 $sGroup = implode(' ',$aStack); break;
378 $sEmail = trim(substr($sToken,1,-1));
383 default: $aStack[] = $sToken; break;
386 /* now do the action again for the last address */
387 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
388 /* try to lookup the addresses in case of invalid email addresses */
389 $aProcessedAddress = array();
390 foreach ($aAddress as $oAddr) {
391 $aAddrBookAddress = array();
393 $grouplookup = false;
395 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox
));
396 if (isset($aAddr['email'])) {
397 if (strpos($aAddr['email'],',')) {
399 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
401 $iPosAt = strpos($aAddr['email'], '@');
402 $oAddr->mailbox
= substr($aAddr['email'], 0, $iPosAt);
403 $oAddr->host
= substr($aAddr['email'], $iPosAt+
1);
404 if (isset($aAddr['name'])) {
405 $oAddr->personal
= $aAddr['name'];
407 $oAddr->personal
= encodeHeader($sPersonal);
412 if (!$grouplookup && !$oAddr->mailbox
) {
413 $oAddr->mailbox
= trim($sEmail);
414 if ($sHost && $oAddr->mailbox
) {
415 $oAddr->host
= $sHost;
419 if (!$aAddrBookAddress && $oAddr->mailbox
) {
420 $aProcessedAddress[] = $oAddr;
422 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
426 return $aProcessedAddress;
428 return $aProcessedAddress[0];
432 function parseContentType($value) {
433 $pos = strpos($value, ';');
436 $type = trim(substr($value, 0, $pos));
437 $props = trim(substr($value, $pos+
1));
441 $content_type = new ContentType($type);
443 $properties = $this->parseProperties($props);
444 if (!isset($properties['charset'])) {
445 $properties['charset'] = 'us-ascii';
447 $content_type->properties
= $this->parseProperties($props);
449 $this->content_type
= $content_type;
452 function parseProperties($value) {
453 $propArray = explode(';', $value);
454 $propResultArray = array();
455 foreach ($propArray as $prop) {
457 $pos = strpos($prop, '=');
459 $key = trim(substr($prop, 0, $pos));
460 $val = trim(substr($prop, $pos+
1));
461 if ($val{0} == '"') {
462 $val = substr($val, 1, -1);
464 $propResultArray[$key] = $val;
467 return $propResultArray;
470 function parseDisposition($value) {
471 $pos = strpos($value, ';');
474 $name = trim(substr($value, 0, $pos));
475 $props = trim(substr($value, $pos+
1));
479 $props_a = $this->parseProperties($props);
480 $disp = new Disposition($name);
481 $disp->properties
= $props_a;
482 $this->disposition
= $disp;
485 function mlist($field, $value) {
487 $value_a = explode(',', $value);
488 foreach ($value_a as $val) {
490 if ($val{0} == '<') {
491 $val = substr($val, 1, -1);
493 if (substr($val, 0, 7) == 'mailto:') {
494 $res_a['mailto'] = substr($val, 7);
496 $res_a['href'] = $val;
499 $this->mlist
[$field] = $res_a;
503 * function to get the addres strings out of the header.
504 * Arguments: string or array of strings !
505 * example1: header->getAddr_s('to').
506 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
508 function getAddr_s($arr, $separator = ',',$encoded=false) {
511 if (is_array($arr)) {
512 foreach($arr as $arg) {
513 if ($this->getAddr_s($arg, $separator, $encoded)) {
514 $s .= $separator . $result;
517 $s = ($s ?
substr($s, 2) : $s);
519 $addr = $this->{$arr};
520 if (is_array($addr)) {
521 foreach ($addr as $addr_o) {
522 if (is_object($addr_o)) {
524 $s .= $addr_o->getEncodedAddress() . $separator;
526 $s .= $addr_o->getAddress() . $separator;
530 $s = substr($s, 0, -strlen($separator));
532 if (is_object($addr)) {
534 $s .= $addr->getEncodedAddress();
536 $s .= $addr->getAddress();
544 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
545 if (is_array($arg)) {
546 foreach($arg as $argument) {
547 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
550 $addr = $this->{$arg};
551 if (is_array($addr)) {
552 foreach ($addr as $next_addr) {
553 if (is_object($next_addr)) {
554 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
555 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
557 $email = $next_addr->mailbox
;
559 $email = strtolower($email);
560 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
561 $arr[$email] = $next_addr->personal
;
566 if (is_object($addr)) {
567 $email = $addr->mailbox
;
568 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
569 $email = strtolower($email);
570 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
571 $arr[$email] = $addr->personal
;
579 function findAddress($address, $recurs = false) {
581 if (is_array($address)) {
583 foreach($address as $argument) {
584 $match = $this->findAddress($argument, true);
589 if (count($match[0]) && !$result) {
596 if (!is_array($this->cc
)) $this->cc
= array();
597 $srch_addr = $this->parseAddress($address);
599 foreach ($this->to
as $to) {
600 if ($to->host
== $srch_addr->host
) {
601 if ($to->mailbox
== $srch_addr->mailbox
) {
602 $results[] = $srch_addr;
603 if ($to->personal
== $srch_addr->personal
) {
605 return array($results, true);
613 foreach ($this->cc
as $cc) {
614 if ($cc->host
== $srch_addr->host
) {
615 if ($cc->mailbox
== $srch_addr->mailbox
) {
616 $results[] = $srch_addr;
617 if ($cc->personal
== $srch_addr->personal
) {
619 return array($results, true);
628 return array($results, false);
629 } elseif (count($result)) {
639 function getContentType($type0, $type1) {
640 $type0 = $this->content_type
->type0
;
641 $type1 = $this->content_type
->type1
;
642 return $this->content_type
->properties
;