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