one more identity action to process
[squirrelmail.git] / class / mime / Rfc822Header.class.php
CommitLineData
19d470aa 1<?php
2
3/**
4 * Rfc822Header.class.php
5 *
6c84ba1e 6 * Copyright (c) 2003-2005 The SquirrelMail Project Team
19d470aa 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
0f459286 9 * This file contains functions needed to handle headers in mime messages.
19d470aa 10 *
883d9cd3 11 * @version $Id$
2b646597 12 * @package squirrelmail
0f459286 13 * @subpackage mime
14 * @since 1.3.2
19d470aa 15 */
16
2b646597 17/**
0f459286 18 * MIME header class
19d470aa 19 * input: header_string or array
0f459286 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.
2b646597 23 * @package squirrelmail
0f459286 24 * @subpackage mime
25 * @since 1.3.0
19d470aa 26 */
27class Rfc822Header {
9ed80157 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
0f459286 136 /**
137 * @param mixed $hdr string or array with message headers
138 */
19d470aa 139 function parseHeader($hdr) {
140 if (is_array($hdr)) {
141 $hdr = implode('', $hdr);
142 }
2a9b0fad 143 /* First we replace \r\n by \n and unfold the header */
0f459286 144 /* FIXME: unfolding header with multiple spaces "\n( +)" */
2a9b0fad 145 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
19d470aa 146
147 /* Now we can make a new header array with */
148 /* each element representing a headerline */
2a9b0fad 149 $hdr = explode("\n" , $hdr);
19d470aa 150 foreach ($hdr as $line) {
151 $pos = strpos($line, ':');
152 if ($pos > 0) {
153 $field = substr($line, 0, $pos);
340d67c2 154 if (!strstr($field,' ')) { /* valid field */
155 $value = trim(substr($line, $pos+1));
156 $this->parseField($field, $value);
157 }
19d470aa 158 }
159 }
160 if ($this->content_type == '') {
161 $this->parseContentType('text/plain; charset=us-ascii');
162 }
163 }
164
0f459286 165 /**
166 * @param string $value
167 * @return string
168 */
19d470aa 169 function stripComments($value) {
170 $result = '';
19d470aa 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
0f459286 211 /**
212 * Parse header field according to field type
213 * @param string $field field name
214 * @param string $value field value
215 */
19d470aa 216 function parseField($field, $value) {
217 $field = strtolower($field);
218 switch($field) {
219 case 'date':
340d67c2 220 $value = $this->stripComments($value);
19d470aa 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;
b268e66b 237 case 'mail-followup-to':
238 $this->mail_followup_to = $this->parseAddress($value, true);
239 break;
19d470aa 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':
340d67c2 253 $value = $this->stripComments($value);
19d470aa 254 $this->message_id = $value;
255 break;
340d67c2 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':
19d470aa 262 case 'disposition-notification-to':
340d67c2 263 $value = $this->stripComments($value);
19d470aa 264 $this->dnt = $this->parseAddress($value);
265 break;
266 case 'mime-version':
340d67c2 267 $value = $this->stripComments($value);
19d470aa 268 $value = str_replace(' ', '', $value);
269 $this->mime = ($value == '1.0' ? true : $this->mime);
270 break;
271 case 'content-type':
340d67c2 272 $value = $this->stripComments($value);
19d470aa 273 $this->parseContentType($value);
274 break;
275 case 'content-disposition':
340d67c2 276 $value = $this->stripComments($value);
19d470aa 277 $this->parseDisposition($value);
278 break;
cfebb724 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;
19d470aa 289 case 'user-agent':
290 case 'x-mailer':
340d67c2 291 $this->xmailer = $value;
19d470aa 292 break;
293 case 'x-priority':
bddb3448 294 case 'importance':
295 case 'priority':
296 $this->priority = $this->parsePriority($value);
19d470aa 297 break;
298 case 'list-post':
340d67c2 299 $value = $this->stripComments($value);
19d470aa 300 $this->mlist('post', $value);
301 break;
302 case 'list-reply':
cfebb724 303 $value = $this->stripComments($value);
19d470aa 304 $this->mlist('reply', $value);
305 break;
306 case 'list-subscribe':
cfebb724 307 $value = $this->stripComments($value);
19d470aa 308 $this->mlist('subscribe', $value);
309 break;
310 case 'list-unsubscribe':
340d67c2 311 $value = $this->stripComments($value);
19d470aa 312 $this->mlist('unsubscribe', $value);
313 break;
314 case 'list-archive':
340d67c2 315 $value = $this->stripComments($value);
19d470aa 316 $this->mlist('archive', $value);
317 break;
318 case 'list-owner':
340d67c2 319 $value = $this->stripComments($value);
19d470aa 320 $this->mlist('owner', $value);
321 break;
322 case 'list-help':
340d67c2 323 $value = $this->stripComments($value);
19d470aa 324 $this->mlist('help', $value);
325 break;
ba4d5a32 326 case 'list-id':
327 $value = $this->stripComments($value);
328 $this->mlist('id', $value);
329 break;
19d470aa 330 default:
331 break;
332 }
333 }
14882b16 334
0f459286 335 /**
336 * @param string $address
337 * @return array
338 */
14882b16 339 function getAddressTokens($address) {
340 $aTokens = array();
14882b16 341 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
342 $aReplace = array(' (',' <',' ,',' ;',' :');
343 $address = str_replace($aSpecials,$aReplace,$address);
55243181 344 $iCnt = strlen($address);
14882b16 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);
cfebb724 360 if ($sToken) $aTokens[] = $sToken;
14882b16 361 break;
362 case '"':
363 $iEnd = strpos($address,$cChar,$i+1);
0b4d4be7 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 }
14882b16 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);
72956ab6 385 if ($sToken) $aTokens[] = $sToken;
14882b16 386 break;
387 case '(':
0b4d4be7 388 array_pop($aTokens); //remove inserted space
14882b16 389 $iEnd = strpos($address,')',$i);
390 if (!$iEnd) {
391 $sToken = substr($address,$i);
392 $i = $iCnt;
393 } else {
55243181 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 }
14882b16 419 }
0b4d4be7 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)) {
43c0e295 423 if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) {
0b4d4be7 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 {
a5ae4bc8 429 $sNextToken = trim(substr($address,$i+1));
0b4d4be7 430 $i = $iCnt;
431 }
432 // remove the token
433 array_pop($aTokens);
434 // create token and add it again
435 $sNewToken = $prevToken . $sNextToken;
1f0269d6 436 if($sNewToken) $aTokens[] = $sNewToken;
0b4d4be7 437 }
438 }
14882b16 439 $sToken = str_replace($aReplace, $aSpecials,$sToken);
cfebb724 440 if ($sToken) $aTokens[] = $sToken;
14882b16 441 break;
442 case ',':
0b4d4be7 443 case ':':
14882b16 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 }
0f459286 463
464 /**
465 * @param array $aStack
466 * @param array $aComment
467 * @param string $sEmail
468 * @param string $sGroup
469 * @return object AddressStructure object
470 */
14882b16 471 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
0b4d4be7 472 //$aStack=explode(' ',implode('',$aStack));
14882b16 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));
cfebb724 480 } else {
14882b16 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 }
0b4d4be7 493 // $oAddr->group = $sGroup;
14882b16 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 }
14882b16 502 $sEmail = '';
503 $aStack = $aComment = array();
504 return $oAddr;
505 }
506
0f459286 507 /**
508 * recursive function for parsing address strings and storing them in an address stucture object.
340d67c2 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
14882b16 515 * That's also the reason that the function became a little bit huge
0f459286 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.
e74ba378 523 */
14882b16 524 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
525 $aTokens = $this->getAddressTokens($address);
8d8da447 526 $sPersonal = $sEmail = $sGroup = '';
14882b16 527 $aStack = $aComment = array();
528 foreach ($aTokens as $sToken) {
529 $cChar = $sToken{0};
530 switch ($cChar)
340d67c2 531 {
532 case '=':
14882b16 533 case '"':
534 case ' ':
cfebb724 535 $aStack[] = $sToken;
340d67c2 536 break;
14882b16 537 case '(':
538 $aComment[] = substr($sToken,1,-1);
340d67c2 539 break;
3fcadedb 540 case ';':
14882b16 541 if ($sGroup) {
29f842a4 542 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 543 $oAddr = end($aAddress);
0b4d4be7 544 if(!$oAddr || ((isset($oAddr)) && !$oAddr->mailbox && !$oAddr->personal)) {
545 $sEmail = $sGroup . ':;';
cfebb724 546 }
0b4d4be7 547 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 548 $sGroup = '';
549 $aStack = $aComment = array();
550 break;
340d67c2 551 }
14882b16 552 case ',':
553 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
554 break;
cfebb724 555 case ':':
29f842a4 556 $sGroup = trim(implode(' ',$aStack));
0b4d4be7 557 $sGroup = preg_replace('/\s+/',' ',$sGroup);
14882b16 558 $aStack = array();
559 break;
560 case '<':
561 $sEmail = trim(substr($sToken,1,-1));
562 break;
563 case '>':
564 /* skip */
cfebb724 565 break;
14882b16 566 default: $aStack[] = $sToken; break;
19d470aa 567 }
568 }
14882b16 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;
340d67c2 577 if ($lookup) {
14882b16 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 }
340d67c2 594 }
14882b16 595 if (!$grouplookup && !$oAddr->mailbox) {
596 $oAddr->mailbox = trim($sEmail);
597 if ($sHost && $oAddr->mailbox) {
598 $oAddr->host = $sHost;
340d67c2 599 }
8b53b4ba 600 } else if (!$grouplookup && !$oAddr->host) {
601 if ($sHost && $oAddr->mailbox) {
602 $oAddr->host = $sHost;
603 }
cfebb724 604 }
14882b16 605 }
606 if (!$aAddrBookAddress && $oAddr->mailbox) {
607 $aProcessedAddress[] = $oAddr;
608 } else {
cfebb724 609 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
14882b16 610 }
340d67c2 611 }
cfebb724 612 if ($ar) {
14882b16 613 return $aProcessedAddress;
19d470aa 614 } else {
14882b16 615 return $aProcessedAddress[0];
19d470aa 616 }
cfebb724 617 }
19d470aa 618
bddb3448 619 /**
620 * Normalise the different Priority headers into a uniform value,
621 * namely that of the X-Priority header (1, 3, 5). Supports:
0f459286 622 * Priority, X-Priority, Importance.
bddb3448 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?
ba17b6c7 629 * @param string $sValue literal priority name
0f459286 630 * @return integer
bddb3448 631 */
ba17b6c7 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
bddb3448 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
0f459286 649 /**
650 * @param string $value content type header
651 */
19d470aa 652 function parseContentType($value) {
653 $pos = strpos($value, ';');
654 $props = '';
655 if ($pos > 0) {
656 $type = trim(substr($value, 0, $pos));
38d6fba7 657 $props = trim(substr($value, $pos+1));
19d470aa 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 }
cfebb724 671
0f459286 672 /**
673 * RFC2184
674 * @param array $aParameters
675 * @return array
676 */
cfebb724 677 function processParameters($aParameters) {
2ddf00ae 678 $aResults = array();
cfebb724 679 $aCharset = array();
680 // handle multiline parameters
2ddf00ae 681 foreach($aParameters as $key => $value) {
cfebb724 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);
0f459286 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 */
cfebb724 708 $value = charset_decode($charset,$value);
709 $aResults[$key] = $value;
2ddf00ae 710 }
cfebb724 711 return $aResults;
2ddf00ae 712 }
19d470aa 713
0f459286 714 /**
715 * @param string $value
716 * @return array
717 */
19d470aa 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));
bbb92b4c 727 if (strlen($val) > 0 && $val{0} == '"') {
19d470aa 728 $val = substr($val, 1, -1);
729 }
730 $propResultArray[$key] = $val;
731 }
732 }
2ddf00ae 733 return $this->processParameters($propResultArray);
19d470aa 734 }
735
0f459286 736 /**
737 * Fills disposition object in rfc822Header object
738 * @param string $value
739 */
19d470aa 740 function parseDisposition($value) {
741 $pos = strpos($value, ';');
742 $props = '';
743 if ($pos > 0) {
744 $name = trim(substr($value, 0, $pos));
fc9269ec 745 $props = trim(substr($value, $pos+1));
19d470aa 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
0f459286 755 /**
756 * Fills mlist array keys in rfc822Header object
757 * @param string $field
758 * @param string $value
759 */
19d470aa 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
0f459286 777 /**
778 * function to get the address strings out of the header.
19d470aa 779 * example1: header->getAddr_s('to').
780 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
0f459286 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
19d470aa 785 */
2c9ecd11 786 function getAddr_s($arr, $separator = ',',$encoded=false) {
19d470aa 787 $s = '';
788
789 if (is_array($arr)) {
790 foreach($arr as $arg) {
2c9ecd11 791 if ($this->getAddr_s($arg, $separator, $encoded)) {
8d8da447 792 $s .= $separator;
19d470aa 793 }
794 }
795 $s = ($s ? substr($s, 2) : $s);
796 } else {
2c9ecd11 797 $addr = $this->{$arr};
19d470aa 798 if (is_array($addr)) {
799 foreach ($addr as $addr_o) {
800 if (is_object($addr_o)) {
2c9ecd11 801 if ($encoded) {
802 $s .= $addr_o->getEncodedAddress() . $separator;
803 } else {
804 $s .= $addr_o->getAddress() . $separator;
805 }
19d470aa 806 }
807 }
808 $s = substr($s, 0, -strlen($separator));
809 } else {
810 if (is_object($addr)) {
2c9ecd11 811 if ($encoded) {
812 $s .= $addr->getEncodedAddress();
813 } else {
814 $s .= $addr->getAddress();
815 }
19d470aa 816 }
817 }
818 }
819 return $s;
820 }
821
0f459286 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 */
19d470aa 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 {
340d67c2 835 $addr = $this->{$arg};
19d470aa 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 }
cfebb724 863
0f459286 864 /**
865 * @param mixed $address array or string
866 * @param boolean $recurs
867 * @return mixed array, boolean
868 * @since 1.3.2
869 */
d0719411 870 function findAddress($address, $recurs = false) {
340d67c2 871 $result = false;
d0719411 872 if (is_array($address)) {
340d67c2 873 $i=0;
d0719411 874 foreach($address as $argument) {
875 $match = $this->findAddress($argument, true);
340d67c2 876 if ($match[1]) {
877 return $i;
878 } else {
879 if (count($match[0]) && !$result) {
880 $result = $i;
881 }
882 }
cfebb724 883 ++$i;
340d67c2 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 }
d0719411 902 }
0f459286 903 foreach ($this->cc as $cc) {
340d67c2 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;
cfebb724 923 }
340d67c2 924 }
1465f80c 925 //exit;
340d67c2 926 return $result;
d0719411 927 }
19d470aa 928
0f459286 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 */
19d470aa 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
e50f5ac2 942?>