one more identity action to process
[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 $sValue literal priority name
630 * @return integer
631 */
632 function parsePriority($sValue) {
633 // don't use function call inside array_shift.
634 $aValue = split('/\w/',trim($sValue));
635 $value = strtolower(array_shift($aValue));
636
637 if ( is_numeric($value) ) {
638 return $value;
639 }
640 if ( $value == 'urgent' || $value == 'high' ) {
641 return 1;
642 } elseif ( $value == 'non-urgent' || $value == 'low' ) {
643 return 5;
644 }
645 // default is normal priority
646 return 3;
647 }
648
649 /**
650 * @param string $value content type header
651 */
652 function parseContentType($value) {
653 $pos = strpos($value, ';');
654 $props = '';
655 if ($pos > 0) {
656 $type = trim(substr($value, 0, $pos));
657 $props = trim(substr($value, $pos+1));
658 } else {
659 $type = $value;
660 }
661 $content_type = new ContentType($type);
662 if ($props) {
663 $properties = $this->parseProperties($props);
664 if (!isset($properties['charset'])) {
665 $properties['charset'] = 'us-ascii';
666 }
667 $content_type->properties = $this->parseProperties($props);
668 }
669 $this->content_type = $content_type;
670 }
671
672 /**
673 * RFC2184
674 * @param array $aParameters
675 * @return array
676 */
677 function processParameters($aParameters) {
678 $aResults = array();
679 $aCharset = array();
680 // handle multiline parameters
681 foreach($aParameters as $key => $value) {
682 if ($iPos = strpos($key,'*')) {
683 $sKey = substr($key,0,$iPos);
684 if (!isset($aResults[$sKey])) {
685 $aResults[$sKey] = $value;
686 if (substr($key,-1) == '*') { // parameter contains language/charset info
687 $aCharset[] = $sKey;
688 }
689 } else {
690 $aResults[$sKey] .= $value;
691 }
692 } else {
693 $aResults[$key] = $value;
694 }
695 }
696 foreach ($aCharset as $key) {
697 $value = $aResults[$key];
698 // extract the charset & language
699 $charset = substr($value,0,strpos($value,"'"));
700 $value = substr($value,strlen($charset)+1);
701 $language = substr($value,0,strpos($value,"'"));
702 $value = substr($value,strlen($charset)+1);
703 /* FIXME: What's the status of charset decode with language information ????
704 * Maybe language information contains only ascii text and charset_decode()
705 * only runs htmlspecialchars() on it. If it contains 8bit information, you
706 * get html encoded text in charset used by selected translation.
707 */
708 $value = charset_decode($charset,$value);
709 $aResults[$key] = $value;
710 }
711 return $aResults;
712 }
713
714 /**
715 * @param string $value
716 * @return array
717 */
718 function parseProperties($value) {
719 $propArray = explode(';', $value);
720 $propResultArray = array();
721 foreach ($propArray as $prop) {
722 $prop = trim($prop);
723 $pos = strpos($prop, '=');
724 if ($pos > 0) {
725 $key = trim(substr($prop, 0, $pos));
726 $val = trim(substr($prop, $pos+1));
727 if (strlen($val) > 0 && $val{0} == '"') {
728 $val = substr($val, 1, -1);
729 }
730 $propResultArray[$key] = $val;
731 }
732 }
733 return $this->processParameters($propResultArray);
734 }
735
736 /**
737 * Fills disposition object in rfc822Header object
738 * @param string $value
739 */
740 function parseDisposition($value) {
741 $pos = strpos($value, ';');
742 $props = '';
743 if ($pos > 0) {
744 $name = trim(substr($value, 0, $pos));
745 $props = trim(substr($value, $pos+1));
746 } else {
747 $name = $value;
748 }
749 $props_a = $this->parseProperties($props);
750 $disp = new Disposition($name);
751 $disp->properties = $props_a;
752 $this->disposition = $disp;
753 }
754
755 /**
756 * Fills mlist array keys in rfc822Header object
757 * @param string $field
758 * @param string $value
759 */
760 function mlist($field, $value) {
761 $res_a = array();
762 $value_a = explode(',', $value);
763 foreach ($value_a as $val) {
764 $val = trim($val);
765 if ($val{0} == '<') {
766 $val = substr($val, 1, -1);
767 }
768 if (substr($val, 0, 7) == 'mailto:') {
769 $res_a['mailto'] = substr($val, 7);
770 } else {
771 $res_a['href'] = $val;
772 }
773 }
774 $this->mlist[$field] = $res_a;
775 }
776
777 /**
778 * function to get the address strings out of the header.
779 * example1: header->getAddr_s('to').
780 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
781 * @param mixed $arr string or array of strings
782 * @param string $separator
783 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
784 * @return string
785 */
786 function getAddr_s($arr, $separator = ',',$encoded=false) {
787 $s = '';
788
789 if (is_array($arr)) {
790 foreach($arr as $arg) {
791 if ($this->getAddr_s($arg, $separator, $encoded)) {
792 $s .= $separator;
793 }
794 }
795 $s = ($s ? substr($s, 2) : $s);
796 } else {
797 $addr = $this->{$arr};
798 if (is_array($addr)) {
799 foreach ($addr as $addr_o) {
800 if (is_object($addr_o)) {
801 if ($encoded) {
802 $s .= $addr_o->getEncodedAddress() . $separator;
803 } else {
804 $s .= $addr_o->getAddress() . $separator;
805 }
806 }
807 }
808 $s = substr($s, 0, -strlen($separator));
809 } else {
810 if (is_object($addr)) {
811 if ($encoded) {
812 $s .= $addr->getEncodedAddress();
813 } else {
814 $s .= $addr->getAddress();
815 }
816 }
817 }
818 }
819 return $s;
820 }
821
822 /**
823 * function to get the array of addresses out of the header.
824 * @param mixed $arg string or array of strings
825 * @param array $excl_arr array of excluded email addresses
826 * @param array $arr array of added email addresses
827 * @return array
828 */
829 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
830 if (is_array($arg)) {
831 foreach($arg as $argument) {
832 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
833 }
834 } else {
835 $addr = $this->{$arg};
836 if (is_array($addr)) {
837 foreach ($addr as $next_addr) {
838 if (is_object($next_addr)) {
839 if (isset($next_addr->host) && ($next_addr->host != '')) {
840 $email = $next_addr->mailbox . '@' . $next_addr->host;
841 } else {
842 $email = $next_addr->mailbox;
843 }
844 $email = strtolower($email);
845 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
846 $arr[$email] = $next_addr->personal;
847 }
848 }
849 }
850 } else {
851 if (is_object($addr)) {
852 $email = $addr->mailbox;
853 $email .= (isset($addr->host) ? '@' . $addr->host : '');
854 $email = strtolower($email);
855 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
856 $arr[$email] = $addr->personal;
857 }
858 }
859 }
860 }
861 return $arr;
862 }
863
864 /**
865 * @param mixed $address array or string
866 * @param boolean $recurs
867 * @return mixed array, boolean
868 * @since 1.3.2
869 */
870 function findAddress($address, $recurs = false) {
871 $result = false;
872 if (is_array($address)) {
873 $i=0;
874 foreach($address as $argument) {
875 $match = $this->findAddress($argument, true);
876 if ($match[1]) {
877 return $i;
878 } else {
879 if (count($match[0]) && !$result) {
880 $result = $i;
881 }
882 }
883 ++$i;
884 }
885 } else {
886 if (!is_array($this->cc)) $this->cc = array();
887 $srch_addr = $this->parseAddress($address);
888 $results = array();
889 foreach ($this->to as $to) {
890 if ($to->host == $srch_addr->host) {
891 if ($to->mailbox == $srch_addr->mailbox) {
892 $results[] = $srch_addr;
893 if ($to->personal == $srch_addr->personal) {
894 if ($recurs) {
895 return array($results, true);
896 } else {
897 return true;
898 }
899 }
900 }
901 }
902 }
903 foreach ($this->cc as $cc) {
904 if ($cc->host == $srch_addr->host) {
905 if ($cc->mailbox == $srch_addr->mailbox) {
906 $results[] = $srch_addr;
907 if ($cc->personal == $srch_addr->personal) {
908 if ($recurs) {
909 return array($results, true);
910 } else {
911 return true;
912 }
913 }
914 }
915 }
916 }
917 if ($recurs) {
918 return array($results, false);
919 } elseif (count($result)) {
920 return true;
921 } else {
922 return false;
923 }
924 }
925 //exit;
926 return $result;
927 }
928
929 /**
930 * @param string $type0 media type
931 * @param string $type1 media subtype
932 * @return array media properties
933 * @todo check use of media type arguments
934 */
935 function getContentType($type0, $type1) {
936 $type0 = $this->content_type->type0;
937 $type1 = $this->content_type->type1;
938 return $this->content_type->properties;
939 }
940 }
941
942 ?>