fixing multiline headers. space or tab should not be removed.
[squirrelmail.git] / class / mime / Rfc822Header.class.php
1 <?php
2
3 /**
4 * Rfc822Header.class.php
5 *
6 * Copyright (c) 2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions needed to handle mime messages.
10 *
11 * $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * input: header_string or array
17 * @package squirrelmail
18 */
19 class Rfc822Header {
20 var $date = '',
21 $subject = '',
22 $from = array(),
23 $sender = '',
24 $reply_to = array(),
25 $mail_followup_to = array(),
26 $to = array(),
27 $cc = array(),
28 $bcc = array(),
29 $in_reply_to = '',
30 $message_id = '',
31 $references = '',
32 $mime = false,
33 $content_type = '',
34 $disposition = '',
35 $xmailer = '',
36 $priority = 3,
37 $dnt = '',
38 $encoding = '',
39 $mlist = array(),
40 $more_headers = array(); /* only needed for constructing headers
41 in smtp.php */
42 function parseHeader($hdr) {
43 if (is_array($hdr)) {
44 $hdr = implode('', $hdr);
45 }
46 /* First we unfold the header */
47 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array(' ', ' '), $hdr));
48
49 /* Now we can make a new header array with */
50 /* each element representing a headerline */
51 $hdr = explode("\r\n" , $hdr);
52 foreach ($hdr as $line) {
53 $pos = strpos($line, ':');
54 if ($pos > 0) {
55 $field = substr($line, 0, $pos);
56 if (!strstr($field,' ')) { /* valid field */
57 $value = trim(substr($line, $pos+1));
58 $this->parseField($field, $value);
59 }
60 }
61 }
62 if ($this->content_type == '') {
63 $this->parseContentType('text/plain; charset=us-ascii');
64 }
65 }
66
67 function stripComments($value) {
68 $result = '';
69 $cnt = strlen($value);
70 for ($i = 0; $i < $cnt; ++$i) {
71 switch ($value{$i}) {
72 case '"':
73 $result .= '"';
74 while ((++$i < $cnt) && ($value{$i} != '"')) {
75 if ($value{$i} == '\\') {
76 $result .= '\\';
77 ++$i;
78 }
79 $result .= $value{$i};
80 }
81 $result .= $value{$i};
82 break;
83 case '(':
84 $depth = 1;
85 while (($depth > 0) && (++$i < $cnt)) {
86 switch($value{$i}) {
87 case '\\':
88 ++$i;
89 break;
90 case '(':
91 ++$depth;
92 break;
93 case ')':
94 --$depth;
95 break;
96 default:
97 break;
98 }
99 }
100 break;
101 default:
102 $result .= $value{$i};
103 break;
104 }
105 }
106 return $result;
107 }
108
109 function parseField($field, $value) {
110 $field = strtolower($field);
111 switch($field) {
112 case 'date':
113 $value = $this->stripComments($value);
114 $d = strtr($value, array(' ' => ' '));
115 $d = explode(' ', $d);
116 $this->date = getTimeStamp($d);
117 break;
118 case 'subject':
119 $this->subject = $value;
120 break;
121 case 'from':
122 $this->from = $this->parseAddress($value,true);
123 break;
124 case 'sender':
125 $this->sender = $this->parseAddress($value);
126 break;
127 case 'reply-to':
128 $this->reply_to = $this->parseAddress($value, true);
129 break;
130 case 'mail-followup-to':
131 $this->mail_followup_to = $this->parseAddress($value, true);
132 break;
133 case 'to':
134 $this->to = $this->parseAddress($value, true);
135 break;
136 case 'cc':
137 $this->cc = $this->parseAddress($value, true);
138 break;
139 case 'bcc':
140 $this->bcc = $this->parseAddress($value, true);
141 break;
142 case 'in-reply-to':
143 $this->in_reply_to = $value;
144 break;
145 case 'message-id':
146 $value = $this->stripComments($value);
147 $this->message_id = $value;
148 break;
149 case 'references':
150 $value = $this->stripComments($value);
151 $this->references = $value;
152 break;
153 case 'x-confirm-reading-to':
154 case 'return-receipt-to':
155 case 'disposition-notification-to':
156 $value = $this->stripComments($value);
157 $this->dnt = $this->parseAddress($value);
158 break;
159 case 'mime-version':
160 $value = $this->stripComments($value);
161 $value = str_replace(' ', '', $value);
162 $this->mime = ($value == '1.0' ? true : $this->mime);
163 break;
164 case 'content-type':
165 $value = $this->stripComments($value);
166 $this->parseContentType($value);
167 break;
168 case 'content-disposition':
169 $value = $this->stripComments($value);
170 $this->parseDisposition($value);
171 break;
172 case 'user-agent':
173 case 'x-mailer':
174 $this->xmailer = $value;
175 break;
176 case 'x-priority':
177 $this->priority = $value;
178 break;
179 case 'list-post':
180 $value = $this->stripComments($value);
181 $this->mlist('post', $value);
182 break;
183 case 'list-reply':
184 $value = $this->stripComments($value);
185 $this->mlist('reply', $value);
186 break;
187 case 'list-subscribe':
188 $value = $this->stripComments($value);
189 $this->mlist('subscribe', $value);
190 break;
191 case 'list-unsubscribe':
192 $value = $this->stripComments($value);
193 $this->mlist('unsubscribe', $value);
194 break;
195 case 'list-archive':
196 $value = $this->stripComments($value);
197 $this->mlist('archive', $value);
198 break;
199 case 'list-owner':
200 $value = $this->stripComments($value);
201 $this->mlist('owner', $value);
202 break;
203 case 'list-help':
204 $value = $this->stripComments($value);
205 $this->mlist('help', $value);
206 break;
207 case 'list-id':
208 $value = $this->stripComments($value);
209 $this->mlist('id', $value);
210 break;
211 default:
212 break;
213 }
214 }
215
216 function getAddressTokens($address) {
217 $aTokens = array();
218 $aAddress = array();
219 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
220 $aReplace = array(' (',' <',' ,',' ;',' :');
221 $address = str_replace($aSpecials,$aReplace,$address);
222 $iCnt = strlen($address);
223 $i = 0;
224 while ($i < $iCnt) {
225 $cChar = $address{$i};
226 switch($cChar)
227 {
228 case '<':
229 $iEnd = strpos($address,'>',$i+1);
230 if (!$iEnd) {
231 $sToken = substr($address,$i);
232 $i = $iCnt;
233 } else {
234 $sToken = substr($address,$i,$iEnd - $i +1);
235 $i = $iEnd;
236 }
237 $sToken = str_replace($aReplace, $aSpecials,$sToken);
238 if($sToken) $aTokens[] = $sToken;
239 break;
240 case '"':
241 $iEnd = strpos($address,$cChar,$i+1);
242 if ($iEnd) {
243 // skip escaped quotes
244 $prev_char = $address{$iEnd-1};
245 while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
246 $iEnd = strpos($address,$cChar,$iEnd+1);
247 if ($iEnd) {
248 $prev_char = $address{$iEnd-1};
249 } else {
250 $prev_char = false;
251 }
252 }
253 }
254 if (!$iEnd) {
255 $sToken = substr($address,$i);
256 $i = $iCnt;
257 } else {
258 // also remove the surrounding quotes
259 $sToken = substr($address,$i+1,$iEnd - $i -1);
260 $i = $iEnd;
261 }
262 $sToken = str_replace($aReplace, $aSpecials,$sToken);
263 if ($sToken) $aTokens[] = $sToken;
264 break;
265 case '(':
266 array_pop($aTokens); //remove inserted space
267 $iEnd = strpos($address,')',$i);
268 if (!$iEnd) {
269 $sToken = substr($address,$i);
270 $i = $iCnt;
271 } else {
272 $iDepth = 1;
273 $iComment = $i;
274 while (($iDepth > 0) && (++$iComment < $iCnt)) {
275 $cCharComment = $address{$iComment};
276 switch($cCharComment) {
277 case '\\':
278 ++$iComment;
279 break;
280 case '(':
281 ++$iDepth;
282 break;
283 case ')':
284 --$iDepth;
285 break;
286 default:
287 break;
288 }
289 }
290 if ($iDepth == 0) {
291 $sToken = substr($address,$i,$iComment - $i +1);
292 $i = $iComment;
293 } else {
294 $sToken = substr($address,$i,$iEnd - $i + 1);
295 $i = $iEnd;
296 }
297 }
298 // check the next token in case comments appear in the middle of email addresses
299 $prevToken = end($aTokens);
300 if (!in_array($prevToken,$aSpecials,true)) {
301 if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) {
302 $iEnd = strpos($address,' ',$i+1);
303 if ($iEnd) {
304 $sNextToken = trim(substr($address,$i+1,$iEnd - $i -1));
305 $i = $iEnd-1;
306 } else {
307 $sNextToken = trim(substr($address,$i+1));
308 $i = $iCnt;
309 }
310 // remove the token
311 array_pop($aTokens);
312 // create token and add it again
313 $sNewToken = $prevToken . $sNextToken;
314 if($sNewToken) $aTokens[] = $sNewToken;
315 }
316 }
317 $sToken = str_replace($aReplace, $aSpecials,$sToken);
318 if($sToken) $aTokens[] = $sToken;
319 break;
320 case ',':
321 case ':':
322 case ';':
323 case ' ':
324 $aTokens[] = $cChar;
325 break;
326 default:
327 $iEnd = strpos($address,' ',$i+1);
328 if ($iEnd) {
329 $sToken = trim(substr($address,$i,$iEnd - $i));
330 $i = $iEnd-1;
331 } else {
332 $sToken = trim(substr($address,$i));
333 $i = $iCnt;
334 }
335 if ($sToken) $aTokens[] = $sToken;
336 }
337 ++$i;
338 }
339 return $aTokens;
340 }
341 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
342 //$aStack=explode(' ',implode('',$aStack));
343 if (!$sEmail) {
344 while (count($aStack) && !$sEmail) {
345 $sEmail = trim(array_pop($aStack));
346 }
347 }
348 if (count($aStack)) {
349 $sPersonal = trim(implode('',$aStack));
350 } else {
351 $sPersonal = '';
352 }
353 if (!$sPersonal && count($aComment)) {
354 $sComment = trim(implode(' ',$aComment));
355 $sPersonal .= $sComment;
356 }
357 $oAddr =& new AddressStructure();
358 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
359 $oAddr->personal = encodeHeader($sPersonal);
360 } else {
361 $oAddr->personal = $sPersonal;
362 }
363 // $oAddr->group = $sGroup;
364 $iPosAt = strpos($sEmail,'@');
365 if ($iPosAt) {
366 $oAddr->mailbox = substr($sEmail, 0, $iPosAt);
367 $oAddr->host = substr($sEmail, $iPosAt+1);
368 } else {
369 $oAddr->mailbox = $sEmail;
370 $oAddr->host = false;
371 }
372 $sEmail = '';
373 $aStack = $aComment = array();
374 return $oAddr;
375 }
376
377 /*
378 * parseAddress: recursive function for parsing address strings and store
379 * them in an address stucture object.
380 * input: $address = string
381 * $ar = boolean (return array instead of only the
382 * first element)
383 * $addr_ar = array with parsed addresses // obsolete
384 * $group = string // obsolete
385 * $host = string (default domainname in case of
386 * addresses without a domainname)
387 * $lookup = callback function (for lookup address
388 * strings which are probably nicks
389 * (without @ ) )
390 * output: array with addressstructure objects or only one
391 * address_structure object.
392 * personal name: encoded: =?charset?Q|B?string?=
393 * quoted: "string"
394 * normal: string
395 * email : <mailbox@host>
396 * : mailbox@host
397 * This function is also used for validating addresses returned from compose
398 * That's also the reason that the function became a little bit huge
399 */
400
401 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
402 $aTokens = $this->getAddressTokens($address);
403 $sPersonal = $sEmail = $sComment = $sGroup = '';
404 $aStack = $aComment = array();
405 foreach ($aTokens as $sToken) {
406 $cChar = $sToken{0};
407 switch ($cChar)
408 {
409 case '=':
410 case '"':
411 case ' ':
412 $aStack[] = $sToken;
413 break;
414 case '(':
415 $aComment[] = substr($sToken,1,-1);
416 break;
417 case ';':
418 if ($sGroup) {
419 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
420 $oAddr = end($aAddress);
421 if(!$oAddr || ((isset($oAddr)) && !$oAddr->mailbox && !$oAddr->personal)) {
422 $sEmail = $sGroup . ':;';
423 }
424 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
425 $sGroup = '';
426 $aStack = $aComment = array();
427 break;
428 }
429 case ',':
430 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
431 break;
432 case ':':
433 $sGroup = trim(implode(' ',$aStack));
434 $sGroup = preg_replace('/\s+/',' ',$sGroup);
435 $aStack = array();
436 break;
437 case '<':
438 $sEmail = trim(substr($sToken,1,-1));
439 break;
440 case '>':
441 /* skip */
442 break;
443 default: $aStack[] = $sToken; break;
444 }
445 }
446 /* now do the action again for the last address */
447 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
448 /* try to lookup the addresses in case of invalid email addresses */
449 $aProcessedAddress = array();
450 foreach ($aAddress as $oAddr) {
451 $aAddrBookAddress = array();
452 if (!$oAddr->host) {
453 $grouplookup = false;
454 if ($lookup) {
455 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox));
456 if (isset($aAddr['email'])) {
457 if (strpos($aAddr['email'],',')) {
458 $grouplookup = true;
459 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
460 } else {
461 $iPosAt = strpos($aAddr['email'], '@');
462 $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
463 $oAddr->host = substr($aAddr['email'], $iPosAt+1);
464 if (isset($aAddr['name'])) {
465 $oAddr->personal = $aAddr['name'];
466 } else {
467 $oAddr->personal = encodeHeader($sPersonal);
468 }
469 }
470 }
471 }
472 if (!$grouplookup && !$oAddr->mailbox) {
473 $oAddr->mailbox = trim($sEmail);
474 if ($sHost && $oAddr->mailbox) {
475 $oAddr->host = $sHost;
476 }
477 } else if (!$grouplookup && !$oAddr->host) {
478 if ($sHost && $oAddr->mailbox) {
479 $oAddr->host = $sHost;
480 }
481 }
482 }
483 if (!$aAddrBookAddress && $oAddr->mailbox) {
484 $aProcessedAddress[] = $oAddr;
485 } else {
486 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
487 }
488 }
489 if ($ar) {
490 return $aProcessedAddress;
491 } else {
492 return $aProcessedAddress[0];
493 }
494 }
495
496 function parseContentType($value) {
497 $pos = strpos($value, ';');
498 $props = '';
499 if ($pos > 0) {
500 $type = trim(substr($value, 0, $pos));
501 $props = trim(substr($value, $pos+1));
502 } else {
503 $type = $value;
504 }
505 $content_type = new ContentType($type);
506 if ($props) {
507 $properties = $this->parseProperties($props);
508 if (!isset($properties['charset'])) {
509 $properties['charset'] = 'us-ascii';
510 }
511 $content_type->properties = $this->parseProperties($props);
512 }
513 $this->content_type = $content_type;
514 }
515
516 /* RFC2184 */
517 function processParameters($aParameters) {
518 $aResults = array();
519 $aCharset = array();
520 // handle multiline parameters
521 foreach($aParameters as $key => $value) {
522 if ($iPos = strpos($key,'*')) {
523 $sKey = substr($key,0,$iPos);
524 if (!isset($aResults[$sKey])) {
525 $aResults[$sKey] = $value;
526 if (substr($key,-1) == '*') { // parameter contains language/charset info
527 $aCharset[] = $sKey;
528 }
529 } else {
530 $aResults[$sKey] .= $value;
531 }
532 }
533 }
534 foreach ($aCharset as $key) {
535 $value = $aResults[$key];
536 // extract the charset & language
537 $charset = substr($value,0,strpos($value,"'"));
538 $value = substr($value,strlen($charset)+1);
539 $language = substr($value,0,strpos($value,"'"));
540 $value = substr($value,strlen($charset)+1);
541 // FIX ME What's the status of charset decode with language information ????
542 $value = charset_decode($charset,$value);
543 $aResults[$key] = $value;
544 }
545 return $aResults;
546 }
547
548 function parseProperties($value) {
549 $propArray = explode(';', $value);
550 $propResultArray = array();
551 foreach ($propArray as $prop) {
552 $prop = trim($prop);
553 $pos = strpos($prop, '=');
554 if ($pos > 0) {
555 $key = trim(substr($prop, 0, $pos));
556 $val = trim(substr($prop, $pos+1));
557 if ($val{0} == '"') {
558 $val = substr($val, 1, -1);
559 }
560 $propResultArray[$key] = $val;
561 }
562 }
563 return $this->processParameters($propResultArray);
564 }
565
566 function parseDisposition($value) {
567 $pos = strpos($value, ';');
568 $props = '';
569 if ($pos > 0) {
570 $name = trim(substr($value, 0, $pos));
571 $props = trim(substr($value, $pos+1));
572 } else {
573 $name = $value;
574 }
575 $props_a = $this->parseProperties($props);
576 $disp = new Disposition($name);
577 $disp->properties = $props_a;
578 $this->disposition = $disp;
579 }
580
581 function mlist($field, $value) {
582 $res_a = array();
583 $value_a = explode(',', $value);
584 foreach ($value_a as $val) {
585 $val = trim($val);
586 if ($val{0} == '<') {
587 $val = substr($val, 1, -1);
588 }
589 if (substr($val, 0, 7) == 'mailto:') {
590 $res_a['mailto'] = substr($val, 7);
591 } else {
592 $res_a['href'] = $val;
593 }
594 }
595 $this->mlist[$field] = $res_a;
596 }
597
598 /*
599 * function to get the addres strings out of the header.
600 * Arguments: string or array of strings !
601 * example1: header->getAddr_s('to').
602 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
603 */
604 function getAddr_s($arr, $separator = ',',$encoded=false) {
605 $s = '';
606
607 if (is_array($arr)) {
608 foreach($arr as $arg) {
609 if ($this->getAddr_s($arg, $separator, $encoded)) {
610 $s .= $separator . $result;
611 }
612 }
613 $s = ($s ? substr($s, 2) : $s);
614 } else {
615 $addr = $this->{$arr};
616 if (is_array($addr)) {
617 foreach ($addr as $addr_o) {
618 if (is_object($addr_o)) {
619 if ($encoded) {
620 $s .= $addr_o->getEncodedAddress() . $separator;
621 } else {
622 $s .= $addr_o->getAddress() . $separator;
623 }
624 }
625 }
626 $s = substr($s, 0, -strlen($separator));
627 } else {
628 if (is_object($addr)) {
629 if ($encoded) {
630 $s .= $addr->getEncodedAddress();
631 } else {
632 $s .= $addr->getAddress();
633 }
634 }
635 }
636 }
637 return $s;
638 }
639
640 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
641 if (is_array($arg)) {
642 foreach($arg as $argument) {
643 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
644 }
645 } else {
646 $addr = $this->{$arg};
647 if (is_array($addr)) {
648 foreach ($addr as $next_addr) {
649 if (is_object($next_addr)) {
650 if (isset($next_addr->host) && ($next_addr->host != '')) {
651 $email = $next_addr->mailbox . '@' . $next_addr->host;
652 } else {
653 $email = $next_addr->mailbox;
654 }
655 $email = strtolower($email);
656 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
657 $arr[$email] = $next_addr->personal;
658 }
659 }
660 }
661 } else {
662 if (is_object($addr)) {
663 $email = $addr->mailbox;
664 $email .= (isset($addr->host) ? '@' . $addr->host : '');
665 $email = strtolower($email);
666 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
667 $arr[$email] = $addr->personal;
668 }
669 }
670 }
671 }
672 return $arr;
673 }
674
675 function findAddress($address, $recurs = false) {
676 $result = false;
677 if (is_array($address)) {
678 $i=0;
679 foreach($address as $argument) {
680 $match = $this->findAddress($argument, true);
681 $last = end($match);
682 if ($match[1]) {
683 return $i;
684 } else {
685 if (count($match[0]) && !$result) {
686 $result = $i;
687 }
688 }
689 ++$i;
690 }
691 } else {
692 if (!is_array($this->cc)) $this->cc = array();
693 $srch_addr = $this->parseAddress($address);
694 $results = array();
695 foreach ($this->to as $to) {
696 if ($to->host == $srch_addr->host) {
697 if ($to->mailbox == $srch_addr->mailbox) {
698 $results[] = $srch_addr;
699 if ($to->personal == $srch_addr->personal) {
700 if ($recurs) {
701 return array($results, true);
702 } else {
703 return true;
704 }
705 }
706 }
707 }
708 }
709 foreach ($this->cc as $cc) {
710 if ($cc->host == $srch_addr->host) {
711 if ($cc->mailbox == $srch_addr->mailbox) {
712 $results[] = $srch_addr;
713 if ($cc->personal == $srch_addr->personal) {
714 if ($recurs) {
715 return array($results, true);
716 } else {
717 return true;
718 }
719 }
720 }
721 }
722 }
723 if ($recurs) {
724 return array($results, false);
725 } elseif (count($result)) {
726 return true;
727 } else {
728 return false;
729 }
730 }
731 //exit;
732 return $result;
733 }
734
735 function getContentType($type0, $type1) {
736 $type0 = $this->content_type->type0;
737 $type1 = $this->content_type->type1;
738 return $this->content_type->properties;
739 }
740 }
741
742 ?>