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