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