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