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