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