phpDocumentor updates
[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 }
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
0f459286 210 /**
211 * Parse header field according to field type
212 * @param string $field field name
213 * @param string $value field value
214 */
19d470aa 215 function parseField($field, $value) {
216 $field = strtolower($field);
217 switch($field) {
218 case 'date':
340d67c2 219 $value = $this->stripComments($value);
19d470aa 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;
b268e66b 236 case 'mail-followup-to':
237 $this->mail_followup_to = $this->parseAddress($value, true);
238 break;
19d470aa 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':
340d67c2 252 $value = $this->stripComments($value);
19d470aa 253 $this->message_id = $value;
254 break;
340d67c2 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':
19d470aa 261 case 'disposition-notification-to':
340d67c2 262 $value = $this->stripComments($value);
19d470aa 263 $this->dnt = $this->parseAddress($value);
264 break;
265 case 'mime-version':
340d67c2 266 $value = $this->stripComments($value);
19d470aa 267 $value = str_replace(' ', '', $value);
268 $this->mime = ($value == '1.0' ? true : $this->mime);
269 break;
270 case 'content-type':
340d67c2 271 $value = $this->stripComments($value);
19d470aa 272 $this->parseContentType($value);
273 break;
274 case 'content-disposition':
340d67c2 275 $value = $this->stripComments($value);
19d470aa 276 $this->parseDisposition($value);
277 break;
cfebb724 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;
19d470aa 288 case 'user-agent':
289 case 'x-mailer':
340d67c2 290 $this->xmailer = $value;
19d470aa 291 break;
292 case 'x-priority':
bddb3448 293 case 'importance':
294 case 'priority':
295 $this->priority = $this->parsePriority($value);
19d470aa 296 break;
297 case 'list-post':
340d67c2 298 $value = $this->stripComments($value);
19d470aa 299 $this->mlist('post', $value);
300 break;
301 case 'list-reply':
cfebb724 302 $value = $this->stripComments($value);
19d470aa 303 $this->mlist('reply', $value);
304 break;
305 case 'list-subscribe':
cfebb724 306 $value = $this->stripComments($value);
19d470aa 307 $this->mlist('subscribe', $value);
308 break;
309 case 'list-unsubscribe':
340d67c2 310 $value = $this->stripComments($value);
19d470aa 311 $this->mlist('unsubscribe', $value);
312 break;
313 case 'list-archive':
340d67c2 314 $value = $this->stripComments($value);
19d470aa 315 $this->mlist('archive', $value);
316 break;
317 case 'list-owner':
340d67c2 318 $value = $this->stripComments($value);
19d470aa 319 $this->mlist('owner', $value);
320 break;
321 case 'list-help':
340d67c2 322 $value = $this->stripComments($value);
19d470aa 323 $this->mlist('help', $value);
324 break;
ba4d5a32 325 case 'list-id':
326 $value = $this->stripComments($value);
327 $this->mlist('id', $value);
328 break;
19d470aa 329 default:
330 break;
331 }
332 }
14882b16 333
0f459286 334 /**
335 * @param string $address
336 * @return array
337 */
14882b16 338 function getAddressTokens($address) {
339 $aTokens = array();
14882b16 340 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
341 $aReplace = array(' (',' <',' ,',' ;',' :');
342 $address = str_replace($aSpecials,$aReplace,$address);
55243181 343 $iCnt = strlen($address);
14882b16 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);
cfebb724 359 if ($sToken) $aTokens[] = $sToken;
14882b16 360 break;
361 case '"':
362 $iEnd = strpos($address,$cChar,$i+1);
0b4d4be7 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 }
14882b16 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);
72956ab6 384 if ($sToken) $aTokens[] = $sToken;
14882b16 385 break;
386 case '(':
0b4d4be7 387 array_pop($aTokens); //remove inserted space
14882b16 388 $iEnd = strpos($address,')',$i);
389 if (!$iEnd) {
390 $sToken = substr($address,$i);
391 $i = $iCnt;
392 } else {
55243181 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 }
14882b16 418 }
0b4d4be7 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)) {
43c0e295 422 if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) {
0b4d4be7 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 {
a5ae4bc8 428 $sNextToken = trim(substr($address,$i+1));
0b4d4be7 429 $i = $iCnt;
430 }
431 // remove the token
432 array_pop($aTokens);
433 // create token and add it again
434 $sNewToken = $prevToken . $sNextToken;
1f0269d6 435 if($sNewToken) $aTokens[] = $sNewToken;
0b4d4be7 436 }
437 }
14882b16 438 $sToken = str_replace($aReplace, $aSpecials,$sToken);
cfebb724 439 if ($sToken) $aTokens[] = $sToken;
14882b16 440 break;
441 case ',':
0b4d4be7 442 case ':':
14882b16 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 }
0f459286 462
463 /**
464 * @param array $aStack
465 * @param array $aComment
466 * @param string $sEmail
467 * @param string $sGroup
468 * @return object AddressStructure object
469 */
14882b16 470 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
0b4d4be7 471 //$aStack=explode(' ',implode('',$aStack));
14882b16 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));
cfebb724 479 } else {
14882b16 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 }
0b4d4be7 492 // $oAddr->group = $sGroup;
14882b16 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 }
14882b16 501 $sEmail = '';
502 $aStack = $aComment = array();
503 return $oAddr;
504 }
505
0f459286 506 /**
507 * recursive function for parsing address strings and storing them in an address stucture object.
340d67c2 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
14882b16 514 * That's also the reason that the function became a little bit huge
0f459286 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.
e74ba378 522 */
14882b16 523 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
524 $aTokens = $this->getAddressTokens($address);
8d8da447 525 $sPersonal = $sEmail = $sGroup = '';
14882b16 526 $aStack = $aComment = array();
527 foreach ($aTokens as $sToken) {
528 $cChar = $sToken{0};
529 switch ($cChar)
340d67c2 530 {
531 case '=':
14882b16 532 case '"':
533 case ' ':
cfebb724 534 $aStack[] = $sToken;
340d67c2 535 break;
14882b16 536 case '(':
537 $aComment[] = substr($sToken,1,-1);
340d67c2 538 break;
3fcadedb 539 case ';':
14882b16 540 if ($sGroup) {
29f842a4 541 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 542 $oAddr = end($aAddress);
0b4d4be7 543 if(!$oAddr || ((isset($oAddr)) && !$oAddr->mailbox && !$oAddr->personal)) {
544 $sEmail = $sGroup . ':;';
cfebb724 545 }
0b4d4be7 546 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 547 $sGroup = '';
548 $aStack = $aComment = array();
549 break;
340d67c2 550 }
14882b16 551 case ',':
552 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
553 break;
cfebb724 554 case ':':
29f842a4 555 $sGroup = trim(implode(' ',$aStack));
0b4d4be7 556 $sGroup = preg_replace('/\s+/',' ',$sGroup);
14882b16 557 $aStack = array();
558 break;
559 case '<':
560 $sEmail = trim(substr($sToken,1,-1));
561 break;
562 case '>':
563 /* skip */
cfebb724 564 break;
14882b16 565 default: $aStack[] = $sToken; break;
19d470aa 566 }
567 }
14882b16 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;
340d67c2 576 if ($lookup) {
14882b16 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 }
340d67c2 593 }
14882b16 594 if (!$grouplookup && !$oAddr->mailbox) {
595 $oAddr->mailbox = trim($sEmail);
596 if ($sHost && $oAddr->mailbox) {
597 $oAddr->host = $sHost;
340d67c2 598 }
8b53b4ba 599 } else if (!$grouplookup && !$oAddr->host) {
600 if ($sHost && $oAddr->mailbox) {
601 $oAddr->host = $sHost;
602 }
cfebb724 603 }
14882b16 604 }
605 if (!$aAddrBookAddress && $oAddr->mailbox) {
606 $aProcessedAddress[] = $oAddr;
607 } else {
cfebb724 608 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
14882b16 609 }
340d67c2 610 }
cfebb724 611 if ($ar) {
14882b16 612 return $aProcessedAddress;
19d470aa 613 } else {
14882b16 614 return $aProcessedAddress[0];
19d470aa 615 }
cfebb724 616 }
19d470aa 617
bddb3448 618 /**
619 * Normalise the different Priority headers into a uniform value,
620 * namely that of the X-Priority header (1, 3, 5). Supports:
0f459286 621 * Priority, X-Priority, Importance.
bddb3448 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?
ba17b6c7 628 * @param string $sValue literal priority name
0f459286 629 * @return integer
bddb3448 630 */
ba17b6c7 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
bddb3448 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
0f459286 648 /**
649 * @param string $value content type header
650 */
19d470aa 651 function parseContentType($value) {
652 $pos = strpos($value, ';');
653 $props = '';
654 if ($pos > 0) {
655 $type = trim(substr($value, 0, $pos));
38d6fba7 656 $props = trim(substr($value, $pos+1));
19d470aa 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 }
cfebb724 670
0f459286 671 /**
672 * RFC2184
673 * @param array $aParameters
674 * @return array
675 */
cfebb724 676 function processParameters($aParameters) {
2ddf00ae 677 $aResults = array();
cfebb724 678 $aCharset = array();
679 // handle multiline parameters
2ddf00ae 680 foreach($aParameters as $key => $value) {
cfebb724 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);
0f459286 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 */
cfebb724 707 $value = charset_decode($charset,$value);
708 $aResults[$key] = $value;
2ddf00ae 709 }
cfebb724 710 return $aResults;
2ddf00ae 711 }
19d470aa 712
0f459286 713 /**
714 * @param string $value
715 * @return array
716 */
19d470aa 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));
bbb92b4c 726 if (strlen($val) > 0 && $val{0} == '"') {
19d470aa 727 $val = substr($val, 1, -1);
728 }
729 $propResultArray[$key] = $val;
730 }
731 }
2ddf00ae 732 return $this->processParameters($propResultArray);
19d470aa 733 }
734
0f459286 735 /**
736 * Fills disposition object in rfc822Header object
737 * @param string $value
738 */
19d470aa 739 function parseDisposition($value) {
740 $pos = strpos($value, ';');
741 $props = '';
742 if ($pos > 0) {
743 $name = trim(substr($value, 0, $pos));
fc9269ec 744 $props = trim(substr($value, $pos+1));
19d470aa 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
0f459286 754 /**
755 * Fills mlist array keys in rfc822Header object
756 * @param string $field
757 * @param string $value
758 */
19d470aa 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
0f459286 776 /**
777 * function to get the address strings out of the header.
19d470aa 778 * example1: header->getAddr_s('to').
779 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
0f459286 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
19d470aa 784 */
2c9ecd11 785 function getAddr_s($arr, $separator = ',',$encoded=false) {
19d470aa 786 $s = '';
787
788 if (is_array($arr)) {
789 foreach($arr as $arg) {
2c9ecd11 790 if ($this->getAddr_s($arg, $separator, $encoded)) {
8d8da447 791 $s .= $separator;
19d470aa 792 }
793 }
794 $s = ($s ? substr($s, 2) : $s);
795 } else {
2c9ecd11 796 $addr = $this->{$arr};
19d470aa 797 if (is_array($addr)) {
798 foreach ($addr as $addr_o) {
799 if (is_object($addr_o)) {
2c9ecd11 800 if ($encoded) {
801 $s .= $addr_o->getEncodedAddress() . $separator;
802 } else {
803 $s .= $addr_o->getAddress() . $separator;
804 }
19d470aa 805 }
806 }
807 $s = substr($s, 0, -strlen($separator));
808 } else {
809 if (is_object($addr)) {
2c9ecd11 810 if ($encoded) {
811 $s .= $addr->getEncodedAddress();
812 } else {
813 $s .= $addr->getAddress();
814 }
19d470aa 815 }
816 }
817 }
818 return $s;
819 }
820
0f459286 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 */
19d470aa 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 {
340d67c2 834 $addr = $this->{$arg};
19d470aa 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 }
cfebb724 862
0f459286 863 /**
864 * @param mixed $address array or string
865 * @param boolean $recurs
866 * @return mixed array, boolean
867 * @since 1.3.2
868 */
d0719411 869 function findAddress($address, $recurs = false) {
340d67c2 870 $result = false;
d0719411 871 if (is_array($address)) {
340d67c2 872 $i=0;
d0719411 873 foreach($address as $argument) {
874 $match = $this->findAddress($argument, true);
340d67c2 875 if ($match[1]) {
876 return $i;
877 } else {
878 if (count($match[0]) && !$result) {
879 $result = $i;
880 }
881 }
cfebb724 882 ++$i;
340d67c2 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 }
d0719411 901 }
0f459286 902 foreach ($this->cc as $cc) {
340d67c2 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;
cfebb724 922 }
340d67c2 923 }
1465f80c 924 //exit;
340d67c2 925 return $result;
d0719411 926 }
19d470aa 927
0f459286 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 */
19d470aa 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
e50f5ac2 941?>