using split() with wrong perl regexes will not work.
[squirrelmail.git] / class / mime / Rfc822Header.class.php
CommitLineData
19d470aa 1<?php
2
3/**
4 * Rfc822Header.class.php
5 *
0f459286 6 * This file contains functions needed to handle headers in mime messages.
19d470aa 7 *
4b5049de 8 * @copyright &copy; 2003-2007 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
883d9cd3 10 * @version $Id$
2b646597 11 * @package squirrelmail
0f459286 12 * @subpackage mime
13 * @since 1.3.2
19d470aa 14 */
15
2b646597 16/**
0f459286 17 * MIME header class
19d470aa 18 * input: header_string or array
0f459286 19 * You must call parseHeader() function after creating object in order to fill object's
20 * parameters.
21 * @todo FIXME: there is no constructor function and class should ignore all input args.
2b646597 22 * @package squirrelmail
0f459286 23 * @subpackage mime
24 * @since 1.3.0
19d470aa 25 */
26class Rfc822Header {
9ed80157 27 /**
28 * Date header
29 * @var mixed
30 */
31 var $date = -1;
3aaa3214 32 /**
609f416c 33 * Original date header as fallback for unparsable dates
3aaa3214 34 * @var mixed
35 */
36 var $date_unparsed = '';
9ed80157 37 /**
38 * Subject header
39 * @var string
40 */
41 var $subject = '';
42 /**
43 * From header
44 * @var array
45 */
46 var $from = array();
47 /**
48 * @var mixed
49 */
50 var $sender = '';
51 /**
52 * Reply-To header
53 * @var array
54 */
55 var $reply_to = array();
56 /**
57 * Mail-Followup-To header
58 * @var array
59 */
60 var $mail_followup_to = array();
61 /**
62 * To header
63 * @var array
64 */
65 var $to = array();
66 /**
67 * Cc header
68 * @var array
69 */
70 var $cc = array();
71 /**
72 * Bcc header
73 * @var array
74 */
75 var $bcc = array();
76 /**
77 * In-reply-to header
78 * @var string
79 */
80 var $in_reply_to = '';
81 /**
82 * Message-ID header
83 * @var string
84 */
85 var $message_id = '';
86 /**
87 * References header
88 * @var string
89 */
90 var $references = '';
91 /**
92 * @var mixed
93 */
94 var $mime = false;
95 /**
ae1c6dbe 96 * Content Type object
97 * @var object
9ed80157 98 */
99 var $content_type = '';
100 /**
101 * @var mixed
102 */
103 var $disposition = '';
104 /**
105 * X-Mailer header
106 * @var string
107 */
108 var $xmailer = '';
109 /**
110 * Priority header
111 * @var integer
112 */
113 var $priority = 3;
114 /**
644c6055 115 * Disposition notification for requesting message delivery notification (MDN)
9ed80157 116 * @var mixed
117 */
118 var $dnt = '';
644c6055 119 /**
120 * Delivery notification (DR)
121 * @var mixed
122 */
123 var $drnt = '';
9ed80157 124 /**
125 * @var mixed
126 */
127 var $encoding = '';
128 /**
129 * @var mixed
130 */
131 var $content_id = '';
132 /**
133 * @var mixed
134 */
135 var $content_desc = '';
136 /**
137 * @var mixed
138 */
139 var $mlist = array();
1a64a084 140 /**
141 * SpamAssassin 'x-spam-status' header
142 * @var mixed
143 */
144 var $x_spam_status = array();
9ed80157 145 /**
146 * Extra header
147 * only needed for constructing headers in delivery class
148 * @var array
149 */
150 var $more_headers = array();
151
0f459286 152 /**
153 * @param mixed $hdr string or array with message headers
154 */
19d470aa 155 function parseHeader($hdr) {
156 if (is_array($hdr)) {
157 $hdr = implode('', $hdr);
158 }
2a9b0fad 159 /* First we replace \r\n by \n and unfold the header */
0f459286 160 /* FIXME: unfolding header with multiple spaces "\n( +)" */
2a9b0fad 161 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
19d470aa 162
163 /* Now we can make a new header array with */
164 /* each element representing a headerline */
2a9b0fad 165 $hdr = explode("\n" , $hdr);
19d470aa 166 foreach ($hdr as $line) {
167 $pos = strpos($line, ':');
168 if ($pos > 0) {
169 $field = substr($line, 0, $pos);
340d67c2 170 if (!strstr($field,' ')) { /* valid field */
171 $value = trim(substr($line, $pos+1));
172 $this->parseField($field, $value);
173 }
19d470aa 174 }
175 }
ae1c6dbe 176 if (!is_object($this->content_type)) {
19d470aa 177 $this->parseContentType('text/plain; charset=us-ascii');
178 }
179 }
180
0f459286 181 /**
182 * @param string $value
183 * @return string
184 */
19d470aa 185 function stripComments($value) {
186 $result = '';
19d470aa 187 $cnt = strlen($value);
188 for ($i = 0; $i < $cnt; ++$i) {
189 switch ($value{$i}) {
190 case '"':
191 $result .= '"';
192 while ((++$i < $cnt) && ($value{$i} != '"')) {
193 if ($value{$i} == '\\') {
194 $result .= '\\';
195 ++$i;
196 }
197 $result .= $value{$i};
198 }
bf489229 199 if($i < $cnt) {
200 $result .= $value{$i};
201 }
19d470aa 202 break;
203 case '(':
204 $depth = 1;
205 while (($depth > 0) && (++$i < $cnt)) {
206 switch($value{$i}) {
207 case '\\':
208 ++$i;
209 break;
210 case '(':
211 ++$depth;
212 break;
213 case ')':
214 --$depth;
215 break;
216 default:
217 break;
218 }
219 }
220 break;
221 default:
222 $result .= $value{$i};
223 break;
224 }
225 }
226 return $result;
227 }
228
0f459286 229 /**
230 * Parse header field according to field type
231 * @param string $field field name
232 * @param string $value field value
233 */
19d470aa 234 function parseField($field, $value) {
235 $field = strtolower($field);
236 switch($field) {
237 case 'date':
340d67c2 238 $value = $this->stripComments($value);
19d470aa 239 $d = strtr($value, array(' ' => ' '));
240 $d = explode(' ', $d);
241 $this->date = getTimeStamp($d);
3aaa3214 242 $this->date_unparsed = strtr($value,'<>',' ');
19d470aa 243 break;
244 case 'subject':
245 $this->subject = $value;
246 break;
247 case 'from':
248 $this->from = $this->parseAddress($value,true);
249 break;
250 case 'sender':
251 $this->sender = $this->parseAddress($value);
252 break;
253 case 'reply-to':
254 $this->reply_to = $this->parseAddress($value, true);
255 break;
b268e66b 256 case 'mail-followup-to':
257 $this->mail_followup_to = $this->parseAddress($value, true);
258 break;
19d470aa 259 case 'to':
260 $this->to = $this->parseAddress($value, true);
261 break;
262 case 'cc':
263 $this->cc = $this->parseAddress($value, true);
264 break;
265 case 'bcc':
266 $this->bcc = $this->parseAddress($value, true);
267 break;
268 case 'in-reply-to':
269 $this->in_reply_to = $value;
270 break;
271 case 'message-id':
340d67c2 272 $value = $this->stripComments($value);
19d470aa 273 $this->message_id = $value;
274 break;
340d67c2 275 case 'references':
276 $value = $this->stripComments($value);
277 $this->references = $value;
278 break;
279 case 'x-confirm-reading-to':
19d470aa 280 case 'disposition-notification-to':
340d67c2 281 $value = $this->stripComments($value);
19d470aa 282 $this->dnt = $this->parseAddress($value);
283 break;
644c6055 284 case 'return-receipt-to':
285 $value = $this->stripComments($value);
286 $this->drnt = $this->parseAddress($value);
287 break;
19d470aa 288 case 'mime-version':
340d67c2 289 $value = $this->stripComments($value);
19d470aa 290 $value = str_replace(' ', '', $value);
291 $this->mime = ($value == '1.0' ? true : $this->mime);
292 break;
293 case 'content-type':
340d67c2 294 $value = $this->stripComments($value);
19d470aa 295 $this->parseContentType($value);
296 break;
297 case 'content-disposition':
340d67c2 298 $value = $this->stripComments($value);
19d470aa 299 $this->parseDisposition($value);
300 break;
cfebb724 301 case 'content-transfer-encoding':
302 $this->encoding = $value;
303 break;
304 case 'content-description':
305 $this->content_desc = $value;
306 break;
307 case 'content-id':
308 $value = $this->stripComments($value);
309 $this->content_id = $value;
310 break;
19d470aa 311 case 'user-agent':
312 case 'x-mailer':
340d67c2 313 $this->xmailer = $value;
19d470aa 314 break;
315 case 'x-priority':
bddb3448 316 case 'importance':
317 case 'priority':
318 $this->priority = $this->parsePriority($value);
19d470aa 319 break;
320 case 'list-post':
340d67c2 321 $value = $this->stripComments($value);
19d470aa 322 $this->mlist('post', $value);
323 break;
324 case 'list-reply':
cfebb724 325 $value = $this->stripComments($value);
19d470aa 326 $this->mlist('reply', $value);
327 break;
328 case 'list-subscribe':
cfebb724 329 $value = $this->stripComments($value);
19d470aa 330 $this->mlist('subscribe', $value);
331 break;
332 case 'list-unsubscribe':
340d67c2 333 $value = $this->stripComments($value);
19d470aa 334 $this->mlist('unsubscribe', $value);
335 break;
336 case 'list-archive':
340d67c2 337 $value = $this->stripComments($value);
19d470aa 338 $this->mlist('archive', $value);
339 break;
340 case 'list-owner':
340d67c2 341 $value = $this->stripComments($value);
19d470aa 342 $this->mlist('owner', $value);
343 break;
344 case 'list-help':
340d67c2 345 $value = $this->stripComments($value);
19d470aa 346 $this->mlist('help', $value);
347 break;
ba4d5a32 348 case 'list-id':
349 $value = $this->stripComments($value);
350 $this->mlist('id', $value);
351 break;
3aaa3214 352 case 'x-spam-status':
1a64a084 353 $this->x_spam_status = $this->parseSpamStatus($value);
354 break;
19d470aa 355 default:
356 break;
357 }
358 }
14882b16 359
0f459286 360 /**
361 * @param string $address
362 * @return array
363 */
14882b16 364 function getAddressTokens($address) {
365 $aTokens = array();
14882b16 366 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
367 $aReplace = array(' (',' <',' ,',' ;',' :');
368 $address = str_replace($aSpecials,$aReplace,$address);
55243181 369 $iCnt = strlen($address);
14882b16 370 $i = 0;
371 while ($i < $iCnt) {
372 $cChar = $address{$i};
373 switch($cChar)
374 {
375 case '<':
376 $iEnd = strpos($address,'>',$i+1);
377 if (!$iEnd) {
378 $sToken = substr($address,$i);
379 $i = $iCnt;
380 } else {
381 $sToken = substr($address,$i,$iEnd - $i +1);
382 $i = $iEnd;
383 }
384 $sToken = str_replace($aReplace, $aSpecials,$sToken);
cfebb724 385 if ($sToken) $aTokens[] = $sToken;
14882b16 386 break;
387 case '"':
388 $iEnd = strpos($address,$cChar,$i+1);
0b4d4be7 389 if ($iEnd) {
390 // skip escaped quotes
391 $prev_char = $address{$iEnd-1};
392 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
393 $iEnd = strpos($address,$cChar,$iEnd+1);
394 if ($iEnd) {
395 $prev_char = $address{$iEnd-1};
396 } else {
397 $prev_char = false;
398 }
399 }
400 }
14882b16 401 if (!$iEnd) {
402 $sToken = substr($address,$i);
403 $i = $iCnt;
404 } else {
405 // also remove the surrounding quotes
406 $sToken = substr($address,$i+1,$iEnd - $i -1);
407 $i = $iEnd;
408 }
409 $sToken = str_replace($aReplace, $aSpecials,$sToken);
72956ab6 410 if ($sToken) $aTokens[] = $sToken;
14882b16 411 break;
412 case '(':
0b4d4be7 413 array_pop($aTokens); //remove inserted space
14882b16 414 $iEnd = strpos($address,')',$i);
415 if (!$iEnd) {
416 $sToken = substr($address,$i);
417 $i = $iCnt;
418 } else {
55243181 419 $iDepth = 1;
420 $iComment = $i;
421 while (($iDepth > 0) && (++$iComment < $iCnt)) {
422 $cCharComment = $address{$iComment};
423 switch($cCharComment) {
424 case '\\':
425 ++$iComment;
426 break;
427 case '(':
428 ++$iDepth;
429 break;
430 case ')':
431 --$iDepth;
432 break;
433 default:
434 break;
435 }
436 }
437 if ($iDepth == 0) {
438 $sToken = substr($address,$i,$iComment - $i +1);
439 $i = $iComment;
440 } else {
441 $sToken = substr($address,$i,$iEnd - $i + 1);
442 $i = $iEnd;
443 }
14882b16 444 }
0b4d4be7 445 // check the next token in case comments appear in the middle of email addresses
446 $prevToken = end($aTokens);
447 if (!in_array($prevToken,$aSpecials,true)) {
43c0e295 448 if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) {
0b4d4be7 449 $iEnd = strpos($address,' ',$i+1);
450 if ($iEnd) {
451 $sNextToken = trim(substr($address,$i+1,$iEnd - $i -1));
452 $i = $iEnd-1;
453 } else {
a5ae4bc8 454 $sNextToken = trim(substr($address,$i+1));
0b4d4be7 455 $i = $iCnt;
456 }
457 // remove the token
458 array_pop($aTokens);
459 // create token and add it again
460 $sNewToken = $prevToken . $sNextToken;
1f0269d6 461 if($sNewToken) $aTokens[] = $sNewToken;
0b4d4be7 462 }
463 }
14882b16 464 $sToken = str_replace($aReplace, $aSpecials,$sToken);
cfebb724 465 if ($sToken) $aTokens[] = $sToken;
14882b16 466 break;
467 case ',':
0b4d4be7 468 case ':':
14882b16 469 case ';':
470 case ' ':
471 $aTokens[] = $cChar;
472 break;
473 default:
474 $iEnd = strpos($address,' ',$i+1);
475 if ($iEnd) {
476 $sToken = trim(substr($address,$i,$iEnd - $i));
477 $i = $iEnd-1;
478 } else {
479 $sToken = trim(substr($address,$i));
480 $i = $iCnt;
481 }
482 if ($sToken) $aTokens[] = $sToken;
483 }
484 ++$i;
485 }
486 return $aTokens;
487 }
0f459286 488
489 /**
490 * @param array $aStack
491 * @param array $aComment
492 * @param string $sEmail
493 * @param string $sGroup
494 * @return object AddressStructure object
495 */
14882b16 496 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
0b4d4be7 497 //$aStack=explode(' ',implode('',$aStack));
14882b16 498 if (!$sEmail) {
499 while (count($aStack) && !$sEmail) {
500 $sEmail = trim(array_pop($aStack));
501 }
502 }
503 if (count($aStack)) {
504 $sPersonal = trim(implode('',$aStack));
cfebb724 505 } else {
14882b16 506 $sPersonal = '';
507 }
508 if (!$sPersonal && count($aComment)) {
509 $sComment = trim(implode(' ',$aComment));
510 $sPersonal .= $sComment;
511 }
512 $oAddr =& new AddressStructure();
513 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
514 $oAddr->personal = encodeHeader($sPersonal);
515 } else {
516 $oAddr->personal = $sPersonal;
517 }
0b4d4be7 518 // $oAddr->group = $sGroup;
14882b16 519 $iPosAt = strpos($sEmail,'@');
520 if ($iPosAt) {
521 $oAddr->mailbox = substr($sEmail, 0, $iPosAt);
522 $oAddr->host = substr($sEmail, $iPosAt+1);
523 } else {
524 $oAddr->mailbox = $sEmail;
525 $oAddr->host = false;
526 }
14882b16 527 $sEmail = '';
528 $aStack = $aComment = array();
529 return $oAddr;
530 }
531
0f459286 532 /**
533 * recursive function for parsing address strings and storing them in an address stucture object.
340d67c2 534 * personal name: encoded: =?charset?Q|B?string?=
535 * quoted: "string"
536 * normal: string
537 * email : <mailbox@host>
538 * : mailbox@host
539 * This function is also used for validating addresses returned from compose
14882b16 540 * That's also the reason that the function became a little bit huge
0f459286 541 * @param string $address
542 * @param boolean $ar return array instead of only the first element
543 * @param array $addr_ar (obsolete) array with parsed addresses
544 * @param string $group (obsolete)
545 * @param string $host default domainname in case of addresses without a domainname
546 * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
547 * @return mixed array with AddressStructure objects or only one address_structure object.
e74ba378 548 */
14882b16 549 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
550 $aTokens = $this->getAddressTokens($address);
8d8da447 551 $sPersonal = $sEmail = $sGroup = '';
14882b16 552 $aStack = $aComment = array();
553 foreach ($aTokens as $sToken) {
554 $cChar = $sToken{0};
555 switch ($cChar)
340d67c2 556 {
557 case '=':
14882b16 558 case '"':
559 case ' ':
cfebb724 560 $aStack[] = $sToken;
340d67c2 561 break;
14882b16 562 case '(':
563 $aComment[] = substr($sToken,1,-1);
340d67c2 564 break;
3fcadedb 565 case ';':
14882b16 566 if ($sGroup) {
29f842a4 567 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 568 $oAddr = end($aAddress);
0b4d4be7 569 if(!$oAddr || ((isset($oAddr)) && !$oAddr->mailbox && !$oAddr->personal)) {
570 $sEmail = $sGroup . ':;';
cfebb724 571 }
0b4d4be7 572 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 573 $sGroup = '';
574 $aStack = $aComment = array();
575 break;
340d67c2 576 }
14882b16 577 case ',':
578 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
579 break;
cfebb724 580 case ':':
29f842a4 581 $sGroup = trim(implode(' ',$aStack));
0b4d4be7 582 $sGroup = preg_replace('/\s+/',' ',$sGroup);
14882b16 583 $aStack = array();
584 break;
585 case '<':
586 $sEmail = trim(substr($sToken,1,-1));
587 break;
588 case '>':
589 /* skip */
cfebb724 590 break;
14882b16 591 default: $aStack[] = $sToken; break;
19d470aa 592 }
593 }
14882b16 594 /* now do the action again for the last address */
595 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
596 /* try to lookup the addresses in case of invalid email addresses */
597 $aProcessedAddress = array();
598 foreach ($aAddress as $oAddr) {
599 $aAddrBookAddress = array();
600 if (!$oAddr->host) {
601 $grouplookup = false;
340d67c2 602 if ($lookup) {
14882b16 603 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox));
604 if (isset($aAddr['email'])) {
605 if (strpos($aAddr['email'],',')) {
606 $grouplookup = true;
607 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
608 } else {
609 $iPosAt = strpos($aAddr['email'], '@');
610 $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
611 $oAddr->host = substr($aAddr['email'], $iPosAt+1);
612 if (isset($aAddr['name'])) {
613 $oAddr->personal = $aAddr['name'];
614 } else {
615 $oAddr->personal = encodeHeader($sPersonal);
616 }
617 }
618 }
340d67c2 619 }
14882b16 620 if (!$grouplookup && !$oAddr->mailbox) {
621 $oAddr->mailbox = trim($sEmail);
622 if ($sHost && $oAddr->mailbox) {
623 $oAddr->host = $sHost;
340d67c2 624 }
8b53b4ba 625 } else if (!$grouplookup && !$oAddr->host) {
626 if ($sHost && $oAddr->mailbox) {
627 $oAddr->host = $sHost;
628 }
cfebb724 629 }
14882b16 630 }
631 if (!$aAddrBookAddress && $oAddr->mailbox) {
632 $aProcessedAddress[] = $oAddr;
633 } else {
cfebb724 634 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
14882b16 635 }
340d67c2 636 }
cfebb724 637 if ($ar) {
14882b16 638 return $aProcessedAddress;
19d470aa 639 } else {
14882b16 640 return $aProcessedAddress[0];
19d470aa 641 }
cfebb724 642 }
19d470aa 643
bddb3448 644 /**
645 * Normalise the different Priority headers into a uniform value,
646 * namely that of the X-Priority header (1, 3, 5). Supports:
0f459286 647 * Priority, X-Priority, Importance.
bddb3448 648 * X-MS-Mail-Priority is not parsed because it always coincides
649 * with one of the other headers.
650 *
651 * NOTE: this is actually a duplicate from the function in
652 * functions/imap_messages. I'm not sure if it's ok here to call
653 * that function?
ba17b6c7 654 * @param string $sValue literal priority name
0f459286 655 * @return integer
bddb3448 656 */
ba17b6c7 657 function parsePriority($sValue) {
658 // don't use function call inside array_shift.
c10512eb 659 $aValue = preg_split('/\s/',trim($sValue));
ba17b6c7 660 $value = strtolower(array_shift($aValue));
661
bddb3448 662 if ( is_numeric($value) ) {
663 return $value;
664 }
665 if ( $value == 'urgent' || $value == 'high' ) {
666 return 1;
667 } elseif ( $value == 'non-urgent' || $value == 'low' ) {
668 return 5;
669 }
670 // default is normal priority
671 return 3;
672 }
673
0f459286 674 /**
675 * @param string $value content type header
676 */
19d470aa 677 function parseContentType($value) {
678 $pos = strpos($value, ';');
679 $props = '';
680 if ($pos > 0) {
681 $type = trim(substr($value, 0, $pos));
38d6fba7 682 $props = trim(substr($value, $pos+1));
19d470aa 683 } else {
684 $type = $value;
685 }
686 $content_type = new ContentType($type);
687 if ($props) {
688 $properties = $this->parseProperties($props);
689 if (!isset($properties['charset'])) {
690 $properties['charset'] = 'us-ascii';
691 }
692 $content_type->properties = $this->parseProperties($props);
693 }
694 $this->content_type = $content_type;
695 }
cfebb724 696
0f459286 697 /**
698 * RFC2184
699 * @param array $aParameters
700 * @return array
701 */
cfebb724 702 function processParameters($aParameters) {
2ddf00ae 703 $aResults = array();
cfebb724 704 $aCharset = array();
705 // handle multiline parameters
2ddf00ae 706 foreach($aParameters as $key => $value) {
cfebb724 707 if ($iPos = strpos($key,'*')) {
708 $sKey = substr($key,0,$iPos);
709 if (!isset($aResults[$sKey])) {
710 $aResults[$sKey] = $value;
711 if (substr($key,-1) == '*') { // parameter contains language/charset info
712 $aCharset[] = $sKey;
713 }
714 } else {
715 $aResults[$sKey] .= $value;
716 }
717 } else {
718 $aResults[$key] = $value;
719 }
720 }
721 foreach ($aCharset as $key) {
722 $value = $aResults[$key];
723 // extract the charset & language
724 $charset = substr($value,0,strpos($value,"'"));
725 $value = substr($value,strlen($charset)+1);
726 $language = substr($value,0,strpos($value,"'"));
727 $value = substr($value,strlen($charset)+1);
0f459286 728 /* FIXME: What's the status of charset decode with language information ????
729 * Maybe language information contains only ascii text and charset_decode()
730 * only runs htmlspecialchars() on it. If it contains 8bit information, you
731 * get html encoded text in charset used by selected translation.
732 */
cfebb724 733 $value = charset_decode($charset,$value);
734 $aResults[$key] = $value;
2ddf00ae 735 }
cfebb724 736 return $aResults;
2ddf00ae 737 }
19d470aa 738
0f459286 739 /**
740 * @param string $value
741 * @return array
742 */
19d470aa 743 function parseProperties($value) {
744 $propArray = explode(';', $value);
745 $propResultArray = array();
746 foreach ($propArray as $prop) {
747 $prop = trim($prop);
748 $pos = strpos($prop, '=');
749 if ($pos > 0) {
750 $key = trim(substr($prop, 0, $pos));
751 $val = trim(substr($prop, $pos+1));
bbb92b4c 752 if (strlen($val) > 0 && $val{0} == '"') {
19d470aa 753 $val = substr($val, 1, -1);
754 }
755 $propResultArray[$key] = $val;
756 }
757 }
2ddf00ae 758 return $this->processParameters($propResultArray);
19d470aa 759 }
760
0f459286 761 /**
762 * Fills disposition object in rfc822Header object
763 * @param string $value
764 */
19d470aa 765 function parseDisposition($value) {
766 $pos = strpos($value, ';');
767 $props = '';
768 if ($pos > 0) {
769 $name = trim(substr($value, 0, $pos));
fc9269ec 770 $props = trim(substr($value, $pos+1));
19d470aa 771 } else {
772 $name = $value;
773 }
774 $props_a = $this->parseProperties($props);
775 $disp = new Disposition($name);
776 $disp->properties = $props_a;
777 $this->disposition = $disp;
778 }
779
0f459286 780 /**
781 * Fills mlist array keys in rfc822Header object
782 * @param string $field
783 * @param string $value
784 */
19d470aa 785 function mlist($field, $value) {
786 $res_a = array();
787 $value_a = explode(',', $value);
788 foreach ($value_a as $val) {
789 $val = trim($val);
790 if ($val{0} == '<') {
791 $val = substr($val, 1, -1);
792 }
793 if (substr($val, 0, 7) == 'mailto:') {
794 $res_a['mailto'] = substr($val, 7);
795 } else {
796 $res_a['href'] = $val;
797 }
798 }
799 $this->mlist[$field] = $res_a;
800 }
801
1a64a084 802 /**
803 * Parses the X-Spam-Status header
804 * @param string $value
805 */
806 function parseSpamStatus($value) {
807 // Header value looks like this:
808 // No, score=1.5 required=5.0 tests=MSGID_FROM_MTA_ID,NO_REAL_NAME,UPPERCASE_25_50 autolearn=disabled version=3.1.0-gr0
809
810 $spam_status = array();
811
812 if (preg_match ('/^(No|Yes),\s+score=(-?\d+\.\d+)\s+required=(-?\d+\.\d+)\s+tests=(.*?)\s+autolearn=(.*?)\s+version=(.+?)$/', $value, $matches)) {
813 // full header
814 $spam_status['bad_format'] = 0;
815 $spam_status['value'] = $matches[0];
816 // is_spam
817 if (isset($matches[1])
818 && strtolower($matches[1]) == 'yes') {
819 $spam_status['is_spam'] = true;
820 } else {
821 $spam_status['is_spam'] = false;
822 }
823
824 // score
825 $spam_status['score'] = $matches[2];
826
827 // required
828 $spam_status['required'] = $matches[3];
829
830 // tests
831 $tests = array();
832 $tests = explode(',', $matches[4]);
833 foreach ($tests as $test) {
834 $spam_status['tests'][] = trim($test);
835 }
836
837 // autolearn
838 $spam_status['autolearn'] = $matches[5];
839
840 // version
841 $spam_status['version'] = $matches[6];
842 } else {
843 $spam_status['bad_format'] = 1;
844 $spam_status['value'] = $value;
845 }
846 return $spam_status;
847 }
848
0f459286 849 /**
850 * function to get the address strings out of the header.
19d470aa 851 * example1: header->getAddr_s('to').
852 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
0f459286 853 * @param mixed $arr string or array of strings
854 * @param string $separator
855 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
856 * @return string
19d470aa 857 */
2c9ecd11 858 function getAddr_s($arr, $separator = ',',$encoded=false) {
19d470aa 859 $s = '';
860
861 if (is_array($arr)) {
862 foreach($arr as $arg) {
2c9ecd11 863 if ($this->getAddr_s($arg, $separator, $encoded)) {
8d8da447 864 $s .= $separator;
19d470aa 865 }
866 }
867 $s = ($s ? substr($s, 2) : $s);
868 } else {
2c9ecd11 869 $addr = $this->{$arr};
19d470aa 870 if (is_array($addr)) {
871 foreach ($addr as $addr_o) {
872 if (is_object($addr_o)) {
2c9ecd11 873 if ($encoded) {
874 $s .= $addr_o->getEncodedAddress() . $separator;
875 } else {
876 $s .= $addr_o->getAddress() . $separator;
877 }
19d470aa 878 }
879 }
880 $s = substr($s, 0, -strlen($separator));
881 } else {
882 if (is_object($addr)) {
2c9ecd11 883 if ($encoded) {
884 $s .= $addr->getEncodedAddress();
885 } else {
886 $s .= $addr->getAddress();
887 }
19d470aa 888 }
889 }
890 }
891 return $s;
892 }
893
0f459286 894 /**
895 * function to get the array of addresses out of the header.
896 * @param mixed $arg string or array of strings
897 * @param array $excl_arr array of excluded email addresses
898 * @param array $arr array of added email addresses
899 * @return array
900 */
19d470aa 901 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
902 if (is_array($arg)) {
903 foreach($arg as $argument) {
904 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
905 }
906 } else {
340d67c2 907 $addr = $this->{$arg};
19d470aa 908 if (is_array($addr)) {
909 foreach ($addr as $next_addr) {
910 if (is_object($next_addr)) {
911 if (isset($next_addr->host) && ($next_addr->host != '')) {
912 $email = $next_addr->mailbox . '@' . $next_addr->host;
913 } else {
914 $email = $next_addr->mailbox;
915 }
916 $email = strtolower($email);
917 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
918 $arr[$email] = $next_addr->personal;
919 }
920 }
921 }
922 } else {
923 if (is_object($addr)) {
924 $email = $addr->mailbox;
925 $email .= (isset($addr->host) ? '@' . $addr->host : '');
926 $email = strtolower($email);
927 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
928 $arr[$email] = $addr->personal;
929 }
930 }
931 }
932 }
933 return $arr;
934 }
cfebb724 935
0f459286 936 /**
937 * @param mixed $address array or string
938 * @param boolean $recurs
939 * @return mixed array, boolean
940 * @since 1.3.2
941 */
d0719411 942 function findAddress($address, $recurs = false) {
340d67c2 943 $result = false;
d0719411 944 if (is_array($address)) {
340d67c2 945 $i=0;
d0719411 946 foreach($address as $argument) {
947 $match = $this->findAddress($argument, true);
340d67c2 948 if ($match[1]) {
949 return $i;
950 } else {
951 if (count($match[0]) && !$result) {
952 $result = $i;
953 }
954 }
cfebb724 955 ++$i;
340d67c2 956 }
957 } else {
958 if (!is_array($this->cc)) $this->cc = array();
959 $srch_addr = $this->parseAddress($address);
960 $results = array();
961 foreach ($this->to as $to) {
962 if ($to->host == $srch_addr->host) {
963 if ($to->mailbox == $srch_addr->mailbox) {
964 $results[] = $srch_addr;
965 if ($to->personal == $srch_addr->personal) {
966 if ($recurs) {
967 return array($results, true);
968 } else {
969 return true;
970 }
971 }
972 }
973 }
d0719411 974 }
0f459286 975 foreach ($this->cc as $cc) {
340d67c2 976 if ($cc->host == $srch_addr->host) {
977 if ($cc->mailbox == $srch_addr->mailbox) {
978 $results[] = $srch_addr;
979 if ($cc->personal == $srch_addr->personal) {
980 if ($recurs) {
981 return array($results, true);
982 } else {
983 return true;
984 }
985 }
986 }
987 }
988 }
989 if ($recurs) {
990 return array($results, false);
991 } elseif (count($result)) {
992 return true;
993 } else {
994 return false;
cfebb724 995 }
340d67c2 996 }
1465f80c 997 //exit;
340d67c2 998 return $result;
d0719411 999 }
19d470aa 1000
0f459286 1001 /**
1002 * @param string $type0 media type
1003 * @param string $type1 media subtype
1004 * @return array media properties
1005 * @todo check use of media type arguments
1006 */
19d470aa 1007 function getContentType($type0, $type1) {
1008 $type0 = $this->content_type->type0;
1009 $type1 = $this->content_type->type1;
1010 return $this->content_type->properties;
1011 }
1012}