Happy New Year!
[squirrelmail.git] / class / mime / Rfc822Header.class.php
1 <?php
2
3 /**
4 * Rfc822Header.class.php
5 *
6 * This file contains functions needed to handle headers in mime messages.
7 *
8 * @copyright 2003-2011 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage mime
13 * @since 1.3.2
14 */
15
16 /**
17 * MIME header class
18 * input: header_string or array
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.
22 * @package squirrelmail
23 * @subpackage mime
24 * @since 1.3.0
25 */
26 class Rfc822Header {
27 /**
28 * Date header
29 * @var mixed
30 */
31 var $date = -1;
32 /**
33 * Original date header as fallback for unparsable dates
34 * @var mixed
35 */
36 var $date_unparsed = '';
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 /**
96 * Content Type object
97 * @var object
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 /**
115 * Disposition notification for requesting message delivery notification (MDN)
116 * @var mixed
117 */
118 var $dnt = '';
119 /**
120 * Delivery notification (DR)
121 * @var mixed
122 */
123 var $drnt = '';
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();
140 /**
141 * SpamAssassin 'x-spam-status' header
142 * @var mixed
143 */
144 var $x_spam_status = array();
145 /**
146 * Extra header
147 * only needed for constructing headers in delivery class
148 * @var array
149 */
150 var $more_headers = array();
151
152 /**
153 * @param mixed $hdr string or array with message headers
154 */
155 function parseHeader($hdr) {
156 if (is_array($hdr)) {
157 $hdr = implode('', $hdr);
158 }
159 /* First we replace \r\n by \n and unfold the header */
160 /* FIXME: unfolding header with multiple spaces "\n( +)" */
161 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
162
163 /* Now we can make a new header array with */
164 /* each element representing a headerline */
165 $hdr = explode("\n" , $hdr);
166 foreach ($hdr as $line) {
167 $pos = strpos($line, ':');
168 if ($pos > 0) {
169 $field = substr($line, 0, $pos);
170 if (!strstr($field,' ')) { /* valid field */
171 $value = trim(substr($line, $pos+1));
172 $this->parseField($field, $value);
173 }
174 }
175 }
176 if (!is_object($this->content_type)) {
177 $this->parseContentType('text/plain; charset=us-ascii');
178 }
179 }
180
181 /**
182 * @param string $value
183 * @return string
184 */
185 function stripComments($value) {
186 $result = '';
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 }
199 if($i < $cnt) {
200 $result .= $value{$i};
201 }
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
229 /**
230 * Parse header field according to field type
231 * @param string $field field name
232 * @param string $value field value
233 */
234 function parseField($field, $value) {
235 $field = strtolower($field);
236 switch($field) {
237 case 'date':
238 $value = $this->stripComments($value);
239 $d = strtr($value, array(' ' => ' '));
240 $d = explode(' ', $d);
241 $this->date = getTimeStamp($d);
242 $this->date_unparsed = strtr($value,'<>',' ');
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;
256 case 'mail-followup-to':
257 $this->mail_followup_to = $this->parseAddress($value, true);
258 break;
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':
272 $value = $this->stripComments($value);
273 $this->message_id = $value;
274 break;
275 case 'references':
276 $value = $this->stripComments($value);
277 $this->references = $value;
278 break;
279 case 'x-confirm-reading-to':
280 case 'disposition-notification-to':
281 $value = $this->stripComments($value);
282 $this->dnt = $this->parseAddress($value);
283 break;
284 case 'return-receipt-to':
285 $value = $this->stripComments($value);
286 $this->drnt = $this->parseAddress($value);
287 break;
288 case 'mime-version':
289 $value = $this->stripComments($value);
290 $value = str_replace(' ', '', $value);
291 $this->mime = ($value == '1.0' ? true : $this->mime);
292 break;
293 case 'content-type':
294 $value = $this->stripComments($value);
295 $this->parseContentType($value);
296 break;
297 case 'content-disposition':
298 $value = $this->stripComments($value);
299 $this->parseDisposition($value);
300 break;
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;
311 case 'user-agent':
312 case 'x-mailer':
313 $this->xmailer = $value;
314 break;
315 case 'x-priority':
316 case 'importance':
317 case 'priority':
318 $this->priority = $this->parsePriority($value);
319 break;
320 case 'list-post':
321 $value = $this->stripComments($value);
322 $this->mlist('post', $value);
323 break;
324 case 'list-reply':
325 $value = $this->stripComments($value);
326 $this->mlist('reply', $value);
327 break;
328 case 'list-subscribe':
329 $value = $this->stripComments($value);
330 $this->mlist('subscribe', $value);
331 break;
332 case 'list-unsubscribe':
333 $value = $this->stripComments($value);
334 $this->mlist('unsubscribe', $value);
335 break;
336 case 'list-archive':
337 $value = $this->stripComments($value);
338 $this->mlist('archive', $value);
339 break;
340 case 'list-owner':
341 $value = $this->stripComments($value);
342 $this->mlist('owner', $value);
343 break;
344 case 'list-help':
345 $value = $this->stripComments($value);
346 $this->mlist('help', $value);
347 break;
348 case 'list-id':
349 $value = $this->stripComments($value);
350 $this->mlist('id', $value);
351 break;
352 case 'x-spam-status':
353 $this->x_spam_status = $this->parseSpamStatus($value);
354 break;
355 default:
356 break;
357 }
358 }
359
360 /**
361 * @param string $address
362 * @return array
363 */
364 function getAddressTokens($address) {
365 $aTokens = array();
366 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
367 $aReplace = array(' (',' <',' ,',' ;',' :');
368 $address = str_replace($aSpecials,$aReplace,$address);
369 $iCnt = strlen($address);
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);
385 if ($sToken) $aTokens[] = $sToken;
386 break;
387 case '"':
388 $iEnd = strpos($address,$cChar,$i+1);
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 }
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);
410 if ($sToken) $aTokens[] = $sToken;
411 break;
412 case '(':
413 array_pop($aTokens); //remove inserted space
414 $iEnd = strpos($address,')',$i);
415 if (!$iEnd) {
416 $sToken = substr($address,$i);
417 $i = $iCnt;
418 } else {
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 }
444 }
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)) {
448 if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) {
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 {
454 $sNextToken = trim(substr($address,$i+1));
455 $i = $iCnt;
456 }
457 // remove the token
458 array_pop($aTokens);
459 // create token and add it again
460 $sNewToken = $prevToken . $sNextToken;
461 if($sNewToken) $aTokens[] = $sNewToken;
462 }
463 }
464 $sToken = str_replace($aReplace, $aSpecials,$sToken);
465 if ($sToken) $aTokens[] = $sToken;
466 break;
467 case ',':
468 case ':':
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 }
488
489 /**
490 * @param array $aStack
491 * @param array $aComment
492 * @param string $sEmail
493 * @param string $sGroup
494 * @return object AddressStructure object
495 */
496 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
497 //$aStack=explode(' ',implode('',$aStack));
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));
505 } else {
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 }
518 // $oAddr->group = $sGroup;
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 }
527 $sEmail = '';
528 $aStack = $aComment = array();
529 return $oAddr;
530 }
531
532 /**
533 * recursive function for parsing address strings and storing them in an address stucture object.
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
540 * That's also the reason that the function became a little bit huge
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.
548 */
549 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
550 $aTokens = $this->getAddressTokens($address);
551 $sPersonal = $sEmail = $sGroup = '';
552 $aStack = $aComment = array();
553 foreach ($aTokens as $sToken) {
554 $cChar = $sToken{0};
555 switch ($cChar)
556 {
557 case '=':
558 case '"':
559 case ' ':
560 $aStack[] = $sToken;
561 break;
562 case '(':
563 $aComment[] = substr($sToken,1,-1);
564 break;
565 case ';':
566 if ($sGroup) {
567 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
568 $oAddr = end($aAddress);
569 if(!$oAddr || ((isset($oAddr)) && !strlen($oAddr->mailbox) && !$oAddr->personal)) {
570 $sEmail = $sGroup . ':;';
571 }
572 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
573 $sGroup = '';
574 $aStack = $aComment = array();
575 break;
576 }
577 case ',':
578 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
579 break;
580 case ':':
581 $sGroup = trim(implode(' ',$aStack));
582 $sGroup = preg_replace('/\s+/',' ',$sGroup);
583 $aStack = array();
584 break;
585 case '<':
586 $sEmail = trim(substr($sToken,1,-1));
587 break;
588 case '>':
589 /* skip */
590 break;
591 default: $aStack[] = $sToken; break;
592 }
593 }
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;
602 if ($lookup) {
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 if ($iPosAt === FALSE) {
611 $oAddr->mailbox = $aAddr['email'];
612 $oAddr->host = FALSE;
613 } else {
614 $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
615 $oAddr->host = substr($aAddr['email'], $iPosAt+1);
616 }
617 if (isset($aAddr['name'])) {
618 $oAddr->personal = $aAddr['name'];
619 } else {
620 $oAddr->personal = encodeHeader($sPersonal);
621 }
622 }
623 }
624 }
625 if (!$grouplookup && !strlen($oAddr->mailbox)) {
626 $oAddr->mailbox = trim($sEmail);
627 if ($sHost && strlen($oAddr->mailbox)) {
628 $oAddr->host = $sHost;
629 }
630 } else if (!$grouplookup && !$oAddr->host) {
631 if ($sHost && strlen($oAddr->mailbox)) {
632 $oAddr->host = $sHost;
633 }
634 }
635 }
636 if (!$aAddrBookAddress && strlen($oAddr->mailbox)) {
637 $aProcessedAddress[] = $oAddr;
638 } else {
639 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
640 }
641 }
642 if ($ar) {
643 return $aProcessedAddress;
644 } else {
645 if (isset($aProcessedAddress[0]))
646 return $aProcessedAddress[0];
647 else
648 return '';
649 }
650 }
651
652 /**
653 * Normalise the different Priority headers into a uniform value,
654 * namely that of the X-Priority header (1, 3, 5). Supports:
655 * Priority, X-Priority, Importance.
656 * X-MS-Mail-Priority is not parsed because it always coincides
657 * with one of the other headers.
658 *
659 * NOTE: this is actually a duplicate from the code in
660 * functions/imap_messages:parseFetch().
661 * I'm not sure if it's ok here to call
662 * that function?
663 * @param string $sValue literal priority name
664 * @return integer
665 */
666 function parsePriority($sValue) {
667 // don't use function call inside array_shift.
668 $aValue = preg_split('/\s/',trim($sValue));
669 $value = strtolower(array_shift($aValue));
670
671 if ( is_numeric($value) ) {
672 return $value;
673 }
674 if ( $value == 'urgent' || $value == 'high' ) {
675 return 1;
676 } elseif ( $value == 'non-urgent' || $value == 'low' ) {
677 return 5;
678 }
679 // default is normal priority
680 return 3;
681 }
682
683 /**
684 * @param string $value content type header
685 */
686 function parseContentType($value) {
687 $pos = strpos($value, ';');
688 $props = '';
689 if ($pos > 0) {
690 $type = trim(substr($value, 0, $pos));
691 $props = trim(substr($value, $pos+1));
692 } else {
693 $type = $value;
694 }
695 $content_type = new ContentType($type);
696 if ($props) {
697 $properties = $this->parseProperties($props);
698 if (!isset($properties['charset'])) {
699 $properties['charset'] = 'us-ascii';
700 }
701 $content_type->properties = $this->parseProperties($props);
702 }
703 $this->content_type = $content_type;
704 }
705
706 /**
707 * RFC2184
708 * @param array $aParameters
709 * @return array
710 */
711 function processParameters($aParameters) {
712 $aResults = array();
713 $aCharset = array();
714 // handle multiline parameters
715 foreach($aParameters as $key => $value) {
716 if ($iPos = strpos($key,'*')) {
717 $sKey = substr($key,0,$iPos);
718 if (!isset($aResults[$sKey])) {
719 $aResults[$sKey] = $value;
720 if (substr($key,-1) == '*') { // parameter contains language/charset info
721 $aCharset[] = $sKey;
722 }
723 } else {
724 $aResults[$sKey] .= $value;
725 }
726 } else {
727 $aResults[$key] = $value;
728 }
729 }
730 foreach ($aCharset as $key) {
731 $value = $aResults[$key];
732 // extract the charset & language
733 $charset = substr($value,0,strpos($value,"'"));
734 $value = substr($value,strlen($charset)+1);
735 $language = substr($value,0,strpos($value,"'"));
736 $value = substr($value,strlen($charset)+1);
737 /* FIXME: What's the status of charset decode with language information ????
738 * Maybe language information contains only ascii text and charset_decode()
739 * only runs htmlspecialchars() on it. If it contains 8bit information, you
740 * get html encoded text in charset used by selected translation.
741 */
742 $value = charset_decode($charset,$value);
743 $aResults[$key] = $value;
744 }
745 return $aResults;
746 }
747
748 /**
749 * @param string $value
750 * @return array
751 */
752 function parseProperties($value) {
753 $propArray = explode(';', $value);
754 $propResultArray = array();
755 foreach ($propArray as $prop) {
756 $prop = trim($prop);
757 $pos = strpos($prop, '=');
758 if ($pos > 0) {
759 $key = trim(substr($prop, 0, $pos));
760 $val = trim(substr($prop, $pos+1));
761 if (strlen($val) > 0 && $val{0} == '"') {
762 $val = substr($val, 1, -1);
763 }
764 $propResultArray[$key] = $val;
765 }
766 }
767 return $this->processParameters($propResultArray);
768 }
769
770 /**
771 * Fills disposition object in rfc822Header object
772 * @param string $value
773 */
774 function parseDisposition($value) {
775 $pos = strpos($value, ';');
776 $props = '';
777 if ($pos > 0) {
778 $name = trim(substr($value, 0, $pos));
779 $props = trim(substr($value, $pos+1));
780 } else {
781 $name = $value;
782 }
783 $props_a = $this->parseProperties($props);
784 $disp = new Disposition($name);
785 $disp->properties = $props_a;
786 $this->disposition = $disp;
787 }
788
789 /**
790 * Fills mlist array keys in rfc822Header object
791 * @param string $field
792 * @param string $value
793 */
794 function mlist($field, $value) {
795 $res_a = array();
796 $value_a = explode(',', $value);
797 foreach ($value_a as $val) {
798 $val = trim($val);
799 if ($val{0} == '<') {
800 $val = substr($val, 1, -1);
801 }
802 if (substr($val, 0, 7) == 'mailto:') {
803 $res_a['mailto'] = substr($val, 7);
804 } else {
805 $res_a['href'] = $val;
806 }
807 }
808 $this->mlist[$field] = $res_a;
809 }
810
811 /**
812 * Parses the X-Spam-Status header
813 * @param string $value
814 */
815 function parseSpamStatus($value) {
816 // Header value looks like this:
817 // 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
818
819 $spam_status = array();
820
821 if (preg_match ('/^(No|Yes),\s+score=(-?\d+\.\d+)\s+required=(-?\d+\.\d+)\s+tests=(.*?)\s+autolearn=(.*?)\s+version=(.+?)$/', $value, $matches)) {
822 // full header
823 $spam_status['bad_format'] = 0;
824 $spam_status['value'] = $matches[0];
825 // is_spam
826 if (isset($matches[1])
827 && strtolower($matches[1]) == 'yes') {
828 $spam_status['is_spam'] = true;
829 } else {
830 $spam_status['is_spam'] = false;
831 }
832
833 // score
834 $spam_status['score'] = $matches[2];
835
836 // required
837 $spam_status['required'] = $matches[3];
838
839 // tests
840 $tests = array();
841 $tests = explode(',', $matches[4]);
842 foreach ($tests as $test) {
843 $spam_status['tests'][] = trim($test);
844 }
845
846 // autolearn
847 $spam_status['autolearn'] = $matches[5];
848
849 // version
850 $spam_status['version'] = $matches[6];
851 } else {
852 $spam_status['bad_format'] = 1;
853 $spam_status['value'] = $value;
854 }
855 return $spam_status;
856 }
857
858 /**
859 * function to get the address strings out of the header.
860 * example1: header->getAddr_s('to').
861 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
862 * @param mixed $arr string or array of strings
863 * @param string $separator
864 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
865 * @param boolean $unconditionally_quote (since 1.4.21/1.5.2) When TRUE, always
866 * quote the personal part,
867 * whether or not it is
868 * encoded, otherwise quoting
869 * is only added if the
870 * personal part is not encoded
871 * @return string
872 */
873 function getAddr_s($arr, $separator = ', ', $encoded=false, $unconditionally_quote=FALSE) {
874 $s = '';
875
876 if (is_array($arr)) {
877 foreach($arr as $arg) {
878 if ($this->getAddr_s($arg, $separator, $encoded, $unconditionally_quote)) {
879 $s .= $separator;
880 }
881 }
882 $s = ($s ? substr($s, 2) : $s);
883 } else {
884 $addr = $this->{$arr};
885 if (is_array($addr)) {
886 foreach ($addr as $addr_o) {
887 if (is_object($addr_o)) {
888 if ($encoded) {
889 $s .= $addr_o->getEncodedAddress($unconditionally_quote) . $separator;
890 } else {
891 $s .= $addr_o->getAddress(TRUE, FALSE, $unconditionally_quote) . $separator;
892 }
893 }
894 }
895 $s = substr($s, 0, -strlen($separator));
896 } else {
897 if (is_object($addr)) {
898 if ($encoded) {
899 $s .= $addr->getEncodedAddress($unconditionally_quote);
900 } else {
901 $s .= $addr->getAddress(TRUE, FALSE, $unconditionally_quote);
902 }
903 }
904 }
905 }
906 return $s;
907 }
908
909 /**
910 * function to get the array of addresses out of the header.
911 * @param mixed $arg string or array of strings
912 * @param array $excl_arr array of excluded email addresses
913 * @param array $arr array of added email addresses
914 * @return array
915 */
916 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
917 if (is_array($arg)) {
918 foreach($arg as $argument) {
919 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
920 }
921 } else {
922 $addr = $this->{$arg};
923 if (is_array($addr)) {
924 foreach ($addr as $next_addr) {
925 if (is_object($next_addr)) {
926 if (isset($next_addr->host) && ($next_addr->host != '')) {
927 $email = $next_addr->mailbox . '@' . $next_addr->host;
928 } else {
929 $email = $next_addr->mailbox;
930 }
931 $email = strtolower($email);
932 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
933 $arr[$email] = $next_addr->personal;
934 }
935 }
936 }
937 } else {
938 if (is_object($addr)) {
939 $email = $addr->mailbox;
940 $email .= (isset($addr->host) ? '@' . $addr->host : '');
941 $email = strtolower($email);
942 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
943 $arr[$email] = $addr->personal;
944 }
945 }
946 }
947 }
948 return $arr;
949 }
950
951 /**
952 //FIXME: This needs some documentation (inside the function too)! Don't code w/out comments!
953 * @param mixed $address array or string
954 * @param boolean $recurs
955 * @return mixed array, boolean
956 * @since 1.3.2
957 */
958 function findAddress($address, $recurs = false) {
959 $result = false;
960 if (is_array($address)) {
961 $i=0;
962 foreach($address as $argument) {
963 $match = $this->findAddress($argument, true);
964 if ($match[1]) {
965 return $i;
966 } else {
967 if (count($match[0]) && !$result) {
968 $result = $i;
969 }
970 }
971 ++$i;
972 }
973 } else {
974 if (!is_array($this->cc)) $this->cc = array();
975 if (!is_array($this->to)) $this->to = array();
976 $srch_addr = $this->parseAddress($address);
977 $results = array();
978 foreach ($this->to as $to) {
979 if (strtolower($to->host) == strtolower($srch_addr->host)) {
980 if (strtolower($to->mailbox) == strtolower($srch_addr->mailbox)) {
981 $results[] = $srch_addr;
982 if (strtolower($to->personal) == strtolower($srch_addr->personal)) {
983 if ($recurs) {
984 return array($results, true);
985 } else {
986 return true;
987 }
988 }
989 }
990 }
991 }
992 foreach ($this->cc as $cc) {
993 if (strtolower($cc->host) == strtolower($srch_addr->host)) {
994 if (strtolower($cc->mailbox) == strtolower($srch_addr->mailbox)) {
995 $results[] = $srch_addr;
996 if (strtolower($cc->personal) == strtolower($srch_addr->personal)) {
997 if ($recurs) {
998 return array($results, true);
999 } else {
1000 return true;
1001 }
1002 }
1003 }
1004 }
1005 }
1006 if ($recurs) {
1007 return array($results, false);
1008 } elseif (count($result)) {
1009 return true;
1010 } else {
1011 return false;
1012 }
1013 }
1014 //exit;
1015 return $result;
1016 }
1017
1018 /**
1019 * @param string $type0 media type
1020 * @param string $type1 media subtype
1021 * @return array media properties
1022 * @todo check use of media type arguments
1023 */
1024 function getContentType($type0, $type1) {
1025 $type0 = $this->content_type->type0;
1026 $type1 = $this->content_type->type1;
1027 return $this->content_type->properties;
1028 }
1029 }