Allow caller to get message object before sending
[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 *
353d074a 8 * @copyright 2003-2018 The SquirrelMail Project Team
4b4abf93 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 /**
609f416c 33 * Original date header as fallback for unparsable dates
3aaa3214 34 * @var mixed
35 */
36 var $date_unparsed = '';
37 /**
9ed80157 38 * Subject header
39 * @var string
40 */
41 var $subject = '';
42 /**
43 * From header
44 * @var array
45 */
46 var $from = array();
47 /**
48 * @var mixed
49 */
50 var $sender = '';
51 /**
52 * Reply-To header
53 * @var array
54 */
55 var $reply_to = array();
56 /**
57 * Mail-Followup-To header
58 * @var array
59 */
60 var $mail_followup_to = array();
61 /**
62 * To header
63 * @var array
64 */
65 var $to = array();
66 /**
67 * Cc header
68 * @var array
69 */
70 var $cc = array();
71 /**
72 * Bcc header
73 * @var array
74 */
75 var $bcc = array();
76 /**
77 * In-reply-to header
78 * @var string
79 */
80 var $in_reply_to = '';
81 /**
82 * Message-ID header
83 * @var string
84 */
85 var $message_id = '';
86 /**
87 * References header
88 * @var string
89 */
90 var $references = '';
91 /**
92 * @var mixed
93 */
94 var $mime = false;
95 /**
ae1c6dbe 96 * Content Type object
97 * @var object
9ed80157 98 */
99 var $content_type = '';
100 /**
101 * @var mixed
102 */
103 var $disposition = '';
104 /**
105 * X-Mailer header
106 * @var string
107 */
108 var $xmailer = '';
109 /**
110 * Priority header
111 * @var integer
112 */
113 var $priority = 3;
114 /**
644c6055 115 * Disposition notification for requesting message delivery notification (MDN)
9ed80157 116 * @var mixed
117 */
118 var $dnt = '';
119 /**
644c6055 120 * Delivery notification (DR)
121 * @var mixed
122 */
123 var $drnt = '';
124 /**
9ed80157 125 * @var mixed
126 */
127 var $encoding = '';
128 /**
129 * @var mixed
130 */
131 var $content_id = '';
132 /**
133 * @var mixed
134 */
135 var $content_desc = '';
136 /**
137 * @var mixed
138 */
139 var $mlist = array();
140 /**
1a64a084 141 * SpamAssassin 'x-spam-status' header
142 * @var mixed
143 */
144 var $x_spam_status = array();
145 /**
9ed80157 146 * Extra header
147 * only needed for constructing headers in delivery class
148 * @var array
149 */
150 var $more_headers = array();
151
0f459286 152 /**
153 * @param mixed $hdr string or array with message headers
154 */
19d470aa 155 function parseHeader($hdr) {
156 if (is_array($hdr)) {
157 $hdr = implode('', $hdr);
158 }
2a9b0fad 159 /* First we replace \r\n by \n and unfold the header */
0f459286 160 /* FIXME: unfolding header with multiple spaces "\n( +)" */
2a9b0fad 161 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
19d470aa 162
163 /* Now we can make a new header array with */
164 /* each element representing a headerline */
2a9b0fad 165 $hdr = explode("\n" , $hdr);
19d470aa 166 foreach ($hdr as $line) {
167 $pos = strpos($line, ':');
168 if ($pos > 0) {
169 $field = substr($line, 0, $pos);
340d67c2 170 if (!strstr($field,' ')) { /* valid field */
171 $value = trim(substr($line, $pos+1));
172 $this->parseField($field, $value);
173 }
19d470aa 174 }
175 }
ae1c6dbe 176 if (!is_object($this->content_type)) {
19d470aa 177 $this->parseContentType('text/plain; charset=us-ascii');
178 }
179 }
180
0f459286 181 /**
182 * @param string $value
183 * @return string
184 */
19d470aa 185 function stripComments($value) {
186 $result = '';
19d470aa 187 $cnt = strlen($value);
188 for ($i = 0; $i < $cnt; ++$i) {
189 switch ($value{$i}) {
190 case '"':
191 $result .= '"';
192 while ((++$i < $cnt) && ($value{$i} != '"')) {
193 if ($value{$i} == '\\') {
194 $result .= '\\';
195 ++$i;
196 }
197 $result .= $value{$i};
198 }
bf489229 199 if($i < $cnt) {
200 $result .= $value{$i};
201 }
19d470aa 202 break;
203 case '(':
204 $depth = 1;
205 while (($depth > 0) && (++$i < $cnt)) {
206 switch($value{$i}) {
207 case '\\':
208 ++$i;
209 break;
210 case '(':
211 ++$depth;
212 break;
213 case ')':
214 --$depth;
215 break;
216 default:
217 break;
218 }
219 }
220 break;
221 default:
222 $result .= $value{$i};
223 break;
224 }
225 }
226 return $result;
227 }
228
0f459286 229 /**
230 * Parse header field according to field type
231 * @param string $field field name
232 * @param string $value field value
233 */
19d470aa 234 function parseField($field, $value) {
235 $field = strtolower($field);
236 switch($field) {
237 case 'date':
340d67c2 238 $value = $this->stripComments($value);
19d470aa 239 $d = strtr($value, array(' ' => ' '));
240 $d = explode(' ', $d);
241 $this->date = getTimeStamp($d);
3aaa3214 242 $this->date_unparsed = strtr($value,'<>',' ');
19d470aa 243 break;
244 case 'subject':
245 $this->subject = $value;
246 break;
247 case 'from':
248 $this->from = $this->parseAddress($value,true);
249 break;
250 case 'sender':
251 $this->sender = $this->parseAddress($value);
252 break;
253 case 'reply-to':
254 $this->reply_to = $this->parseAddress($value, true);
255 break;
b268e66b 256 case 'mail-followup-to':
257 $this->mail_followup_to = $this->parseAddress($value, true);
258 break;
19d470aa 259 case 'to':
260 $this->to = $this->parseAddress($value, true);
261 break;
262 case 'cc':
263 $this->cc = $this->parseAddress($value, true);
264 break;
265 case 'bcc':
266 $this->bcc = $this->parseAddress($value, true);
267 break;
268 case 'in-reply-to':
269 $this->in_reply_to = $value;
270 break;
271 case 'message-id':
340d67c2 272 $value = $this->stripComments($value);
19d470aa 273 $this->message_id = $value;
274 break;
340d67c2 275 case 'references':
276 $value = $this->stripComments($value);
277 $this->references = $value;
278 break;
279 case 'x-confirm-reading-to':
19d470aa 280 case 'disposition-notification-to':
340d67c2 281 $value = $this->stripComments($value);
19d470aa 282 $this->dnt = $this->parseAddress($value);
283 break;
644c6055 284 case 'return-receipt-to':
285 $value = $this->stripComments($value);
286 $this->drnt = $this->parseAddress($value);
287 break;
19d470aa 288 case 'mime-version':
340d67c2 289 $value = $this->stripComments($value);
19d470aa 290 $value = str_replace(' ', '', $value);
291 $this->mime = ($value == '1.0' ? true : $this->mime);
292 break;
293 case 'content-type':
340d67c2 294 $value = $this->stripComments($value);
19d470aa 295 $this->parseContentType($value);
296 break;
297 case 'content-disposition':
340d67c2 298 $value = $this->stripComments($value);
19d470aa 299 $this->parseDisposition($value);
300 break;
cfebb724 301 case 'content-transfer-encoding':
302 $this->encoding = $value;
303 break;
304 case 'content-description':
305 $this->content_desc = $value;
306 break;
307 case 'content-id':
308 $value = $this->stripComments($value);
309 $this->content_id = $value;
310 break;
19d470aa 311 case 'user-agent':
312 case 'x-mailer':
340d67c2 313 $this->xmailer = $value;
19d470aa 314 break;
315 case 'x-priority':
bddb3448 316 case 'importance':
317 case 'priority':
318 $this->priority = $this->parsePriority($value);
19d470aa 319 break;
320 case 'list-post':
340d67c2 321 $value = $this->stripComments($value);
19d470aa 322 $this->mlist('post', $value);
323 break;
324 case 'list-reply':
cfebb724 325 $value = $this->stripComments($value);
19d470aa 326 $this->mlist('reply', $value);
327 break;
328 case 'list-subscribe':
cfebb724 329 $value = $this->stripComments($value);
19d470aa 330 $this->mlist('subscribe', $value);
331 break;
332 case 'list-unsubscribe':
340d67c2 333 $value = $this->stripComments($value);
19d470aa 334 $this->mlist('unsubscribe', $value);
335 break;
336 case 'list-archive':
340d67c2 337 $value = $this->stripComments($value);
19d470aa 338 $this->mlist('archive', $value);
339 break;
340 case 'list-owner':
340d67c2 341 $value = $this->stripComments($value);
19d470aa 342 $this->mlist('owner', $value);
343 break;
344 case 'list-help':
340d67c2 345 $value = $this->stripComments($value);
19d470aa 346 $this->mlist('help', $value);
347 break;
ba4d5a32 348 case 'list-id':
349 $value = $this->stripComments($value);
350 $this->mlist('id', $value);
351 break;
3aaa3214 352 case 'x-spam-status':
1a64a084 353 $this->x_spam_status = $this->parseSpamStatus($value);
354 break;
aff28a6d 355 case 'x-sm-flag-reply':
356 $this->x_sm_flag_reply = $value;
357 break;
19d470aa 358 default:
359 break;
360 }
361 }
14882b16 362
0f459286 363 /**
364 * @param string $address
365 * @return array
366 */
14882b16 367 function getAddressTokens($address) {
368 $aTokens = array();
14882b16 369 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
370 $aReplace = array(' (',' <',' ,',' ;',' :');
371 $address = str_replace($aSpecials,$aReplace,$address);
55243181 372 $iCnt = strlen($address);
14882b16 373 $i = 0;
374 while ($i < $iCnt) {
375 $cChar = $address{$i};
376 switch($cChar)
377 {
378 case '<':
379 $iEnd = strpos($address,'>',$i+1);
380 if (!$iEnd) {
381 $sToken = substr($address,$i);
382 $i = $iCnt;
383 } else {
384 $sToken = substr($address,$i,$iEnd - $i +1);
385 $i = $iEnd;
386 }
387 $sToken = str_replace($aReplace, $aSpecials,$sToken);
cfebb724 388 if ($sToken) $aTokens[] = $sToken;
14882b16 389 break;
390 case '"':
391 $iEnd = strpos($address,$cChar,$i+1);
0b4d4be7 392 if ($iEnd) {
393 // skip escaped quotes
394 $prev_char = $address{$iEnd-1};
395 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
396 $iEnd = strpos($address,$cChar,$iEnd+1);
397 if ($iEnd) {
398 $prev_char = $address{$iEnd-1};
399 } else {
400 $prev_char = false;
401 }
402 }
403 }
14882b16 404 if (!$iEnd) {
405 $sToken = substr($address,$i);
406 $i = $iCnt;
407 } else {
408 // also remove the surrounding quotes
409 $sToken = substr($address,$i+1,$iEnd - $i -1);
410 $i = $iEnd;
411 }
412 $sToken = str_replace($aReplace, $aSpecials,$sToken);
72956ab6 413 if ($sToken) $aTokens[] = $sToken;
14882b16 414 break;
415 case '(':
0b4d4be7 416 array_pop($aTokens); //remove inserted space
14882b16 417 $iEnd = strpos($address,')',$i);
418 if (!$iEnd) {
419 $sToken = substr($address,$i);
420 $i = $iCnt;
421 } else {
55243181 422 $iDepth = 1;
423 $iComment = $i;
424 while (($iDepth > 0) && (++$iComment < $iCnt)) {
425 $cCharComment = $address{$iComment};
426 switch($cCharComment) {
427 case '\\':
428 ++$iComment;
429 break;
430 case '(':
431 ++$iDepth;
432 break;
433 case ')':
434 --$iDepth;
435 break;
436 default:
437 break;
438 }
439 }
440 if ($iDepth == 0) {
441 $sToken = substr($address,$i,$iComment - $i +1);
442 $i = $iComment;
443 } else {
444 $sToken = substr($address,$i,$iEnd - $i + 1);
445 $i = $iEnd;
446 }
14882b16 447 }
0b4d4be7 448 // check the next token in case comments appear in the middle of email addresses
449 $prevToken = end($aTokens);
450 if (!in_array($prevToken,$aSpecials,true)) {
43c0e295 451 if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) {
0b4d4be7 452 $iEnd = strpos($address,' ',$i+1);
453 if ($iEnd) {
454 $sNextToken = trim(substr($address,$i+1,$iEnd - $i -1));
455 $i = $iEnd-1;
456 } else {
a5ae4bc8 457 $sNextToken = trim(substr($address,$i+1));
0b4d4be7 458 $i = $iCnt;
459 }
460 // remove the token
461 array_pop($aTokens);
462 // create token and add it again
463 $sNewToken = $prevToken . $sNextToken;
1f0269d6 464 if($sNewToken) $aTokens[] = $sNewToken;
0b4d4be7 465 }
466 }
14882b16 467 $sToken = str_replace($aReplace, $aSpecials,$sToken);
cfebb724 468 if ($sToken) $aTokens[] = $sToken;
14882b16 469 break;
470 case ',':
0b4d4be7 471 case ':':
14882b16 472 case ';':
473 case ' ':
474 $aTokens[] = $cChar;
475 break;
476 default:
477 $iEnd = strpos($address,' ',$i+1);
478 if ($iEnd) {
479 $sToken = trim(substr($address,$i,$iEnd - $i));
480 $i = $iEnd-1;
481 } else {
482 $sToken = trim(substr($address,$i));
483 $i = $iCnt;
484 }
485 if ($sToken) $aTokens[] = $sToken;
486 }
487 ++$i;
488 }
489 return $aTokens;
490 }
0f459286 491
492 /**
493 * @param array $aStack
494 * @param array $aComment
495 * @param string $sEmail
496 * @param string $sGroup
497 * @return object AddressStructure object
498 */
14882b16 499 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
0b4d4be7 500 //$aStack=explode(' ',implode('',$aStack));
14882b16 501 if (!$sEmail) {
502 while (count($aStack) && !$sEmail) {
503 $sEmail = trim(array_pop($aStack));
504 }
505 }
506 if (count($aStack)) {
507 $sPersonal = trim(implode('',$aStack));
cfebb724 508 } else {
14882b16 509 $sPersonal = '';
510 }
511 if (!$sPersonal && count($aComment)) {
512 $sComment = trim(implode(' ',$aComment));
513 $sPersonal .= $sComment;
514 }
c6e83897 515 $oAddr = new AddressStructure();
14882b16 516 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
517 $oAddr->personal = encodeHeader($sPersonal);
518 } else {
519 $oAddr->personal = $sPersonal;
520 }
0b4d4be7 521 // $oAddr->group = $sGroup;
14882b16 522 $iPosAt = strpos($sEmail,'@');
523 if ($iPosAt) {
524 $oAddr->mailbox = substr($sEmail, 0, $iPosAt);
525 $oAddr->host = substr($sEmail, $iPosAt+1);
526 } else {
527 $oAddr->mailbox = $sEmail;
528 $oAddr->host = false;
529 }
14882b16 530 $sEmail = '';
531 $aStack = $aComment = array();
532 return $oAddr;
533 }
534
0f459286 535 /**
536 * recursive function for parsing address strings and storing them in an address stucture object.
340d67c2 537 * personal name: encoded: =?charset?Q|B?string?=
538 * quoted: "string"
539 * normal: string
540 * email : <mailbox@host>
541 * : mailbox@host
542 * This function is also used for validating addresses returned from compose
14882b16 543 * That's also the reason that the function became a little bit huge
0f459286 544 * @param string $address
545 * @param boolean $ar return array instead of only the first element
546 * @param array $addr_ar (obsolete) array with parsed addresses
547 * @param string $group (obsolete)
548 * @param string $host default domainname in case of addresses without a domainname
549 * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
550 * @return mixed array with AddressStructure objects or only one address_structure object.
e74ba378 551 */
14882b16 552 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
553 $aTokens = $this->getAddressTokens($address);
8d8da447 554 $sPersonal = $sEmail = $sGroup = '';
14882b16 555 $aStack = $aComment = array();
556 foreach ($aTokens as $sToken) {
557 $cChar = $sToken{0};
558 switch ($cChar)
340d67c2 559 {
560 case '=':
14882b16 561 case '"':
562 case ' ':
cfebb724 563 $aStack[] = $sToken;
340d67c2 564 break;
14882b16 565 case '(':
566 $aComment[] = substr($sToken,1,-1);
340d67c2 567 break;
3fcadedb 568 case ';':
14882b16 569 if ($sGroup) {
29f842a4 570 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 571 $oAddr = end($aAddress);
7d38dfae 572 if(!$oAddr || ((isset($oAddr)) && !strlen($oAddr->mailbox) && !$oAddr->personal)) {
0b4d4be7 573 $sEmail = $sGroup . ':;';
cfebb724 574 }
0b4d4be7 575 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
14882b16 576 $sGroup = '';
577 $aStack = $aComment = array();
578 break;
340d67c2 579 }
14882b16 580 case ',':
581 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
582 break;
cfebb724 583 case ':':
29f842a4 584 $sGroup = trim(implode(' ',$aStack));
0b4d4be7 585 $sGroup = preg_replace('/\s+/',' ',$sGroup);
14882b16 586 $aStack = array();
587 break;
588 case '<':
589 $sEmail = trim(substr($sToken,1,-1));
590 break;
591 case '>':
592 /* skip */
cfebb724 593 break;
14882b16 594 default: $aStack[] = $sToken; break;
19d470aa 595 }
596 }
14882b16 597 /* now do the action again for the last address */
598 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
599 /* try to lookup the addresses in case of invalid email addresses */
600 $aProcessedAddress = array();
601 foreach ($aAddress as $oAddr) {
602 $aAddrBookAddress = array();
603 if (!$oAddr->host) {
604 $grouplookup = false;
340d67c2 605 if ($lookup) {
14882b16 606 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox));
607 if (isset($aAddr['email'])) {
608 if (strpos($aAddr['email'],',')) {
609 $grouplookup = true;
610 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
611 } else {
612 $iPosAt = strpos($aAddr['email'], '@');
5b3a1a3a 613 if ($iPosAt === FALSE) {
614 $oAddr->mailbox = $aAddr['email'];
da0135f6 615 $oAddr->host = FALSE;
5b3a1a3a 616 } else {
617 $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
618 $oAddr->host = substr($aAddr['email'], $iPosAt+1);
619 }
14882b16 620 if (isset($aAddr['name'])) {
621 $oAddr->personal = $aAddr['name'];
622 } else {
623 $oAddr->personal = encodeHeader($sPersonal);
624 }
625 }
626 }
340d67c2 627 }
7d38dfae 628 if (!$grouplookup && !strlen($oAddr->mailbox)) {
14882b16 629 $oAddr->mailbox = trim($sEmail);
7d38dfae 630 if ($sHost && strlen($oAddr->mailbox)) {
14882b16 631 $oAddr->host = $sHost;
340d67c2 632 }
8b53b4ba 633 } else if (!$grouplookup && !$oAddr->host) {
7d38dfae 634 if ($sHost && strlen($oAddr->mailbox)) {
8b53b4ba 635 $oAddr->host = $sHost;
636 }
cfebb724 637 }
14882b16 638 }
7d38dfae 639 if (!$aAddrBookAddress && strlen($oAddr->mailbox)) {
14882b16 640 $aProcessedAddress[] = $oAddr;
641 } else {
cfebb724 642 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
14882b16 643 }
340d67c2 644 }
cfebb724 645 if ($ar) {
14882b16 646 return $aProcessedAddress;
19d470aa 647 } else {
66010edf 648 if (isset($aProcessedAddress[0]))
649 return $aProcessedAddress[0];
650 else
651 return '';
19d470aa 652 }
cfebb724 653 }
19d470aa 654
bddb3448 655 /**
656 * Normalise the different Priority headers into a uniform value,
657 * namely that of the X-Priority header (1, 3, 5). Supports:
0f459286 658 * Priority, X-Priority, Importance.
bddb3448 659 * X-MS-Mail-Priority is not parsed because it always coincides
660 * with one of the other headers.
661 *
8b08e46d 662 * NOTE: this is actually a duplicate from the code in
663 * functions/imap_messages:parseFetch().
664 * I'm not sure if it's ok here to call
bddb3448 665 * that function?
ba17b6c7 666 * @param string $sValue literal priority name
0f459286 667 * @return integer
bddb3448 668 */
ba17b6c7 669 function parsePriority($sValue) {
670 // don't use function call inside array_shift.
c10512eb 671 $aValue = preg_split('/\s/',trim($sValue));
ba17b6c7 672 $value = strtolower(array_shift($aValue));
673
bddb3448 674 if ( is_numeric($value) ) {
675 return $value;
676 }
677 if ( $value == 'urgent' || $value == 'high' ) {
678 return 1;
679 } elseif ( $value == 'non-urgent' || $value == 'low' ) {
680 return 5;
681 }
682 // default is normal priority
683 return 3;
684 }
685
0f459286 686 /**
687 * @param string $value content type header
688 */
19d470aa 689 function parseContentType($value) {
690 $pos = strpos($value, ';');
691 $props = '';
692 if ($pos > 0) {
693 $type = trim(substr($value, 0, $pos));
38d6fba7 694 $props = trim(substr($value, $pos+1));
19d470aa 695 } else {
696 $type = $value;
697 }
698 $content_type = new ContentType($type);
699 if ($props) {
700 $properties = $this->parseProperties($props);
701 if (!isset($properties['charset'])) {
702 $properties['charset'] = 'us-ascii';
703 }
704 $content_type->properties = $this->parseProperties($props);
705 }
706 $this->content_type = $content_type;
707 }
cfebb724 708
0f459286 709 /**
710 * RFC2184
711 * @param array $aParameters
712 * @return array
713 */
cfebb724 714 function processParameters($aParameters) {
2ddf00ae 715 $aResults = array();
cfebb724 716 $aCharset = array();
717 // handle multiline parameters
2ddf00ae 718 foreach($aParameters as $key => $value) {
cfebb724 719 if ($iPos = strpos($key,'*')) {
720 $sKey = substr($key,0,$iPos);
721 if (!isset($aResults[$sKey])) {
722 $aResults[$sKey] = $value;
723 if (substr($key,-1) == '*') { // parameter contains language/charset info
724 $aCharset[] = $sKey;
725 }
726 } else {
727 $aResults[$sKey] .= $value;
728 }
729 } else {
730 $aResults[$key] = $value;
731 }
732 }
733 foreach ($aCharset as $key) {
734 $value = $aResults[$key];
735 // extract the charset & language
736 $charset = substr($value,0,strpos($value,"'"));
737 $value = substr($value,strlen($charset)+1);
738 $language = substr($value,0,strpos($value,"'"));
739 $value = substr($value,strlen($charset)+1);
0f459286 740 /* FIXME: What's the status of charset decode with language information ????
741 * Maybe language information contains only ascii text and charset_decode()
3047e291 742 * only runs sm_encode_html_special_chars() on it. If it contains 8bit information, you
0f459286 743 * get html encoded text in charset used by selected translation.
744 */
cfebb724 745 $value = charset_decode($charset,$value);
746 $aResults[$key] = $value;
2ddf00ae 747 }
cfebb724 748 return $aResults;
2ddf00ae 749 }
19d470aa 750
0f459286 751 /**
752 * @param string $value
753 * @return array
754 */
19d470aa 755 function parseProperties($value) {
756 $propArray = explode(';', $value);
757 $propResultArray = array();
758 foreach ($propArray as $prop) {
759 $prop = trim($prop);
760 $pos = strpos($prop, '=');
761 if ($pos > 0) {
762 $key = trim(substr($prop, 0, $pos));
763 $val = trim(substr($prop, $pos+1));
bbb92b4c 764 if (strlen($val) > 0 && $val{0} == '"') {
19d470aa 765 $val = substr($val, 1, -1);
766 }
767 $propResultArray[$key] = $val;
768 }
769 }
2ddf00ae 770 return $this->processParameters($propResultArray);
19d470aa 771 }
772
0f459286 773 /**
774 * Fills disposition object in rfc822Header object
775 * @param string $value
776 */
19d470aa 777 function parseDisposition($value) {
778 $pos = strpos($value, ';');
779 $props = '';
780 if ($pos > 0) {
781 $name = trim(substr($value, 0, $pos));
fc9269ec 782 $props = trim(substr($value, $pos+1));
19d470aa 783 } else {
784 $name = $value;
785 }
786 $props_a = $this->parseProperties($props);
787 $disp = new Disposition($name);
788 $disp->properties = $props_a;
789 $this->disposition = $disp;
790 }
791
0f459286 792 /**
793 * Fills mlist array keys in rfc822Header object
794 * @param string $field
795 * @param string $value
796 */
19d470aa 797 function mlist($field, $value) {
798 $res_a = array();
799 $value_a = explode(',', $value);
800 foreach ($value_a as $val) {
801 $val = trim($val);
802 if ($val{0} == '<') {
803 $val = substr($val, 1, -1);
804 }
805 if (substr($val, 0, 7) == 'mailto:') {
806 $res_a['mailto'] = substr($val, 7);
807 } else {
808 $res_a['href'] = $val;
809 }
810 }
811 $this->mlist[$field] = $res_a;
812 }
813
0f459286 814 /**
1a64a084 815 * Parses the X-Spam-Status header
816 * @param string $value
817 */
818 function parseSpamStatus($value) {
819 // Header value looks like this:
820 // No, score=1.5 required=5.0 tests=MSGID_FROM_MTA_ID,NO_REAL_NAME,UPPERCASE_25_50 autolearn=disabled version=3.1.0-gr0
821
822 $spam_status = array();
823
824 if (preg_match ('/^(No|Yes),\s+score=(-?\d+\.\d+)\s+required=(-?\d+\.\d+)\s+tests=(.*?)\s+autolearn=(.*?)\s+version=(.+?)$/', $value, $matches)) {
825 // full header
826 $spam_status['bad_format'] = 0;
827 $spam_status['value'] = $matches[0];
828 // is_spam
829 if (isset($matches[1])
830 && strtolower($matches[1]) == 'yes') {
831 $spam_status['is_spam'] = true;
832 } else {
833 $spam_status['is_spam'] = false;
834 }
835
836 // score
837 $spam_status['score'] = $matches[2];
838
839 // required
840 $spam_status['required'] = $matches[3];
841
842 // tests
843 $tests = array();
844 $tests = explode(',', $matches[4]);
845 foreach ($tests as $test) {
846 $spam_status['tests'][] = trim($test);
847 }
848
849 // autolearn
850 $spam_status['autolearn'] = $matches[5];
851
852 // version
853 $spam_status['version'] = $matches[6];
854 } else {
855 $spam_status['bad_format'] = 1;
856 $spam_status['value'] = $value;
857 }
858 return $spam_status;
859 }
860
861 /**
0f459286 862 * function to get the address strings out of the header.
19d470aa 863 * example1: header->getAddr_s('to').
864 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
0f459286 865 * @param mixed $arr string or array of strings
866 * @param string $separator
867 * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
a9b9e5d3 868 * @param boolean $unconditionally_quote (since 1.4.21/1.5.2) When TRUE, always
869 * quote the personal part,
870 * whether or not it is
871 * encoded, otherwise quoting
872 * is only added if the
873 * personal part is not encoded
0f459286 874 * @return string
19d470aa 875 */
a9b9e5d3 876 function getAddr_s($arr, $separator = ', ', $encoded=false, $unconditionally_quote=FALSE) {
19d470aa 877 $s = '';
878
879 if (is_array($arr)) {
880 foreach($arr as $arg) {
a9b9e5d3 881 if ($this->getAddr_s($arg, $separator, $encoded, $unconditionally_quote)) {
8d8da447 882 $s .= $separator;
19d470aa 883 }
884 }
885 $s = ($s ? substr($s, 2) : $s);
886 } else {
2c9ecd11 887 $addr = $this->{$arr};
19d470aa 888 if (is_array($addr)) {
889 foreach ($addr as $addr_o) {
890 if (is_object($addr_o)) {
2c9ecd11 891 if ($encoded) {
a9b9e5d3 892 $s .= $addr_o->getEncodedAddress($unconditionally_quote) . $separator;
2c9ecd11 893 } else {
a9b9e5d3 894 $s .= $addr_o->getAddress(TRUE, FALSE, $unconditionally_quote) . $separator;
2c9ecd11 895 }
19d470aa 896 }
897 }
898 $s = substr($s, 0, -strlen($separator));
899 } else {
900 if (is_object($addr)) {
2c9ecd11 901 if ($encoded) {
a9b9e5d3 902 $s .= $addr->getEncodedAddress($unconditionally_quote);
2c9ecd11 903 } else {
a9b9e5d3 904 $s .= $addr->getAddress(TRUE, FALSE, $unconditionally_quote);
2c9ecd11 905 }
19d470aa 906 }
907 }
908 }
909 return $s;
910 }
911
0f459286 912 /**
913 * function to get the array of addresses out of the header.
914 * @param mixed $arg string or array of strings
915 * @param array $excl_arr array of excluded email addresses
916 * @param array $arr array of added email addresses
917 * @return array
918 */
19d470aa 919 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
920 if (is_array($arg)) {
921 foreach($arg as $argument) {
922 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
923 }
924 } else {
340d67c2 925 $addr = $this->{$arg};
19d470aa 926 if (is_array($addr)) {
927 foreach ($addr as $next_addr) {
928 if (is_object($next_addr)) {
929 if (isset($next_addr->host) && ($next_addr->host != '')) {
930 $email = $next_addr->mailbox . '@' . $next_addr->host;
931 } else {
932 $email = $next_addr->mailbox;
933 }
934 $email = strtolower($email);
935 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
936 $arr[$email] = $next_addr->personal;
937 }
938 }
939 }
940 } else {
941 if (is_object($addr)) {
942 $email = $addr->mailbox;
943 $email .= (isset($addr->host) ? '@' . $addr->host : '');
944 $email = strtolower($email);
945 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
946 $arr[$email] = $addr->personal;
947 }
948 }
949 }
950 }
951 return $arr;
952 }
cfebb724 953
0f459286 954 /**
8d2d85f5 955//FIXME: This needs some documentation (inside the function too)! Don't code w/out comments!
b546a42a 956 * Looking at the code years after it was written,
957 * this is my (Paul) best guess as to what this
958 * function does (note that docs previously claimed
959 * that this function returns boolean or an array,
960 * but it no longer appears to return an array - an
961 * integer instead):
962 *
963 * Inspects the TO and CC headers of the message
964 * represented by this object, looking for the
965 * address(es) given by $address
966 *
967 * If $address is a string:
968 * Serves as a test (returns boolean) as to
969 * whether or not the given address is found
970 * anywhere in the TO or CC headers
971 *
972 * If $address is an array:
973 * Looks through this list of addresses and
974 * returns the array index (an integer even
975 * if the array is given with keys of a
177f93ea 976 * different type) of the first matching
b546a42a 977 * $address found in this message's
978 * TO or CC headers, unless there is an exact
979 * match (meaning that the "personal
980 * information" in addition to the email
981 * address also matches), in which case that
982 * index (the first one found) is returned
983 *
984 * @param mixed $address Address(es) to search for in this
985 * message's TO and CC headers - please
986 * see above how the format of this
987 * argument affects the return value
988 * of this function
989 * @param boolean $recurs FOR INTERNAL USE ONLY
990 *
991 * @return mixed Boolean when $address is a scalar,
992 * indicating whether or not the address
993 * was found in the TO or CC headers.
994 * An integer when $address is an array,
995 * containing the index of the value in
996 * that array that was found in the TO
997 * or CC headers, or boolean FALSE if
998 * there were no matches at all
999 *
0f459286 1000 * @since 1.3.2
1001 */
d0719411 1002 function findAddress($address, $recurs = false) {
340d67c2 1003 $result = false;
d0719411 1004 if (is_array($address)) {
340d67c2 1005 $i=0;
d0719411 1006 foreach($address as $argument) {
1007 $match = $this->findAddress($argument, true);
177f93ea 1008 if ($match[1]) { // this indicates when the personal information matched
340d67c2 1009 return $i;
1010 } else {
177f93ea 1011 if (count($match[0]) && $result === FALSE) {
340d67c2 1012 $result = $i;
1013 }
1014 }
cfebb724 1015 ++$i;
340d67c2 1016 }
1017 } else {
1018 if (!is_array($this->cc)) $this->cc = array();
7c8a0b77 1019 if (!is_array($this->to)) $this->to = array();
340d67c2 1020 $srch_addr = $this->parseAddress($address);
1021 $results = array();
1022 foreach ($this->to as $to) {
8d2d85f5 1023 if (strtolower($to->host) == strtolower($srch_addr->host)) {
1024 if (strtolower($to->mailbox) == strtolower($srch_addr->mailbox)) {
340d67c2 1025 $results[] = $srch_addr;
8d2d85f5 1026 if (strtolower($to->personal) == strtolower($srch_addr->personal)) {
340d67c2 1027 if ($recurs) {
1028 return array($results, true);
1029 } else {
1030 return true;
1031 }
1032 }
1033 }
1034 }
d0719411 1035 }
0f459286 1036 foreach ($this->cc as $cc) {
8d2d85f5 1037 if (strtolower($cc->host) == strtolower($srch_addr->host)) {
1038 if (strtolower($cc->mailbox) == strtolower($srch_addr->mailbox)) {
340d67c2 1039 $results[] = $srch_addr;
8d2d85f5 1040 if (strtolower($cc->personal) == strtolower($srch_addr->personal)) {
340d67c2 1041 if ($recurs) {
1042 return array($results, true);
1043 } else {
1044 return true;
1045 }
1046 }
1047 }
1048 }
1049 }
1050 if ($recurs) {
1051 return array($results, false);
fd9881f0 1052 } elseif (count($results)) {
340d67c2 1053 return true;
1054 } else {
1055 return false;
cfebb724 1056 }
340d67c2 1057 }
1465f80c 1058 //exit;
340d67c2 1059 return $result;
d0719411 1060 }
19d470aa 1061
0f459286 1062 /**
1063 * @param string $type0 media type
1064 * @param string $type1 media subtype
1065 * @return array media properties
1066 * @todo check use of media type arguments
1067 */
19d470aa 1068 function getContentType($type0, $type1) {
1069 $type0 = $this->content_type->type0;
1070 $type1 = $this->content_type->type1;
1071 return $this->content_type->properties;
1072 }
1073}