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