fixes parseAddress. I think this thing is working brilliant :)
[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 $encoding = '',
37 $mlist = array(),
38 $more_headers = array(); /* only needed for constructing headers
39 in smtp.php */
40 function parseHeader($hdr) {
41 if (is_array($hdr)) {
42 $hdr = implode('', $hdr);
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 //$name .= parseString($address,$pos);
271 $start_encoded = $pos;
272 ++$pos;
273 if ($address{$pos} == '"') {
274 ++$pos;
275 } else {
276 $personal_start = $personal_end = $pos;
277 while ($pos < $j) {
278 $personal_end = strpos($address,'"',$pos);
279 if (($personal_end-2)>0 && (substr($address,$personal_end-2,2) === '\\"' ||
280 substr($address,$personal_end-2,2) === '\\\\')) {
281 $pos = $personal_end+1;
282 } else {
283 $name .= substr($address,$personal_start,$personal_end-$personal_start);
284 break;
285 }
286 }
287 if ($personal_end) {
288 $pos = $personal_end+1;
289 } else {
290 $pos = $j;
291 }
292 }
293 $addr_start = $pos;
294 break;
295 case '<': /* get email address */
296 $addr_start = $pos;
297 $addr_end = strpos($address,'>',$addr_start);
298 /* check for missing '>' */
299 if ($addr_end === false) {
300 $addr_end = $j;
301 }
302 $addr = substr($address,$addr_start+1,$addr_end-$addr_start-1);
303 if ($addr_end) {
304 $pos = $addr_end+1;
305 } else {
306 $addr = substr($address,$addr_start+1);
307 $pos = $j;
308 }
309 break;
310 case '(': /* rip off comments */
311 $comment_start = $pos;
312 $pos = strpos($address,')');
313 if ($pos !== false) {
314 $comment = substr($address, $comment_start+1,($pos-$comment_start-1));
315 $address_start = substr($address, 0, $comment_start);
316 $address_end = substr($address, $pos + 1);
317 $address = $address_start . $address_end;
318 }
319 $j = strlen($address);
320 if ($comment_start) {
321 $pos = $comment_start-1;
322 } else {
323 $pos = 0;
324 }
325 break;
326 case ';':
327 if ($group) {
328 $address = substr($address, 0, $pos - 1);
329 ++$pos;
330 break;
331 }
332 case ',': /* we reached a delimiter */
333 if (!$name && !$addr) {
334 $addr = substr($address, 0, $pos);
335 } else if (!$addr) {
336 $addr = trim(substr($address, $addr_start, $pos));
337 } else if ($name == '') {
338 $name = trim(substr($address, 0, $addr_start));
339 }
340 $at = strpos($addr, '@');
341 $addr_structure = new AddressStructure();
342 if (!$name && $comment) $name = $comment;
343 if (!$is_encoded) {
344 $addr_structure->personal = encodeHeader($name);
345 } else {
346 $addr_structure->personal = $name;
347 }
348 $is_encoded = false;
349 $addr_structure->group = $group;
350 $grouplookup = false;
351 if ($at) {
352 $addr_structure->mailbox = substr($addr, 0, $at);
353 $addr_structure->host = substr($addr, $at+1);
354 } else {
355 /* if lookup function */
356 if ($lookup) {
357 $aAddr = call_user_func_array($lookup,array($addr));
358 if (isset($aAddr['email'])) {
359 if (strpos($aAddr['email'],',')) {
360 $grouplookup = true;
361 $addr_ar = $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
362 } else {
363 $at = strpos($aAddr['email'], '@');
364 $addr_structure->mailbox = substr($aAddr['email'], 0, $at);
365 $addr_structure->host = substr($aAddr['email'], $at+1);
366 if (isset($aAddr['name'])) {
367 $addr_structure->personal = $aAddr['name'];
368 } else {
369 $addr_structure->personal = encodeHeader($addr);
370 }
371 }
372 }
373 }
374 if (!$grouplookup && !$addr_structure->mailbox) {
375 $addr_structure->mailbox = trim($addr);
376 if ($host) {
377 $addr_structure->host = $host;
378 }
379 }
380 }
381 $address = trim(substr($address, $pos+1));
382 $j = strlen($address);
383 $pos = 0;
384 $name = '';
385 $addr = '';
386 if (!$grouplookup) {
387 $addr_ar[] = $addr_structure;
388 }
389 break;
390 case ':': /* process the group addresses */
391 /* group marker */
392 if (strpos($address,';',$pos)) {
393 $group = substr($address, 0, $pos);
394 $address = substr($address, $pos+1);
395 $result = $this->parseAddress($address, $ar, $addr_ar, $group, $lookup);
396 $addr_ar = $result[0];
397 $pos = $result[1];
398 $address = substr($address, $pos++);
399 $j = strlen($address);
400 $group = '';
401 } else {
402 $pos = $j;
403 }
404 break;
405 case ' ':
406 ++$pos;
407 break;
408 default:
409 /*
410 * this happens in the folowing situations :
411 * 1: unquoted personal name
412 * 2: emailaddress without < and >
413 * 3: unquoted personal name from compose that should be encoded.
414 * if it's a personal name then an emailaddress should follow
415 * the personal name may not have ',' inside it
416 * If it's a emailaddress then the personal name is not set.
417 * we should look for the delimiter ',' or a SPACE
418 */
419 /* check for emailaddress */
420
421 /* Blah, this code sucks */
422
423 /* we need an tokenizer !!!!!!!! */
424
425 $i_space = strpos($address,' ',$pos);
426 $i_del = strpos($address,',',$pos);
427 if ($i_space || $i_del) {
428 if ($i_del) { /* extract the stringpart before the delimiter */
429 $address_part = substr($address,$pos,$i_del-$pos);
430 } else { /* extract the stringpart started with pos */
431 $address_part = substr($address,$pos);
432 }
433 if ($i = strpos($address_part,'@')) {
434 /* an email address is following */
435 if (($i+$pos) < $i_space) {
436 $addr_start = $pos;
437 /* multiple addresses are following */
438 if ($i_space < $i_del && $i_del) {
439 /* <space> is present */
440 if ($i_space) {
441 if ($i = strpos($address_part,'<')) {
442 $name .= substr($address_part,0,$i);
443 $pos = $i+$pos;
444 } else {
445 $addr = substr($address,$pos,$i_space-$pos);
446 $pos = $i_space;
447 }
448 } else { /* no <space> $i_space === false */
449 if ($i = strpos($address_part,'<')) {
450 $name .= substr($address_part,0,$i);
451 $pos = $i+$pos;
452 } else {
453 $addr = substr($address,$pos);
454 $pos = $j;
455 }
456 }
457 } else { /* <space> is available in the next address */
458 /* OR no delimiter and <space> */
459 if ($i_del) {
460 /* check for < > addresses */
461 if ($i = strpos($address_part,'<')) {
462 $name .= substr($address_part,0,$i);
463 $pos = $i+$pos;
464 } else {
465 $addr = substr($address,$pos,$i_del-$pos);
466 $pos = $i_del;
467 }
468 /* no delimiter */
469 } else if ($i_space) { /* can never happen ? */
470 if ($i = strpos($address_part,'<')) {
471 $name .= substr($address_part,0,$i);
472 $pos = $i+$pos;
473 } else {
474 $addr = substr($address,$pos,$i_space-$pos);
475 $pos = $i_space+1;
476 }
477 } else { /* can never happen */
478 $addr = substr($address,$pos);
479 $pos = $j;
480 }
481 }
482 } else { /* <space> is located after the user@domain part */
483 /* or no <space> present */
484 if ($i_space) {
485 if ($i = strpos($address_part,'<')) {
486 $name .= substr($address_part,0,$i);
487 $pos = $i+$pos;
488 } else {
489 $name .= substr($address,$pos,$i_space-$pos) . ' ';
490 $addr_start = $i_space+1;
491 $pos = $i_space+1;
492 }
493 } else { /* no <space> */
494 $addr = substr($address,$pos,$i_del-$pos);
495 $addr_start = $pos;
496 if ($i_del) {
497 $pos = $i_del;
498 } else { /* can never happen. REMOVE */
499 $pos = $j;
500 }
501 }
502 }
503 } else {
504 /* email address without domain name, could be an alias */
505 $addr_start = $pos;
506 /* FIXME check for comments */
507 $addr = $address_part;
508 $pos = strlen($address_part) + $pos;
509 }
510 } else {
511 /* check for < > addresses */
512 if ($i = strpos($address,'<')) {
513 $name .= substr($address,$pos,$i-$pos);
514 $pos = $i;
515 } else {
516 /* FIXME check for comments */
517 $addr = substr($address,$pos);
518 $addr_start = $pos;
519 $pos = $j;
520 }
521 }
522 break;
523 }
524 }
525 if (!$name && !$addr) {
526 $addr = substr($address, 0, $pos);
527 } else if (!$addr) {
528 $addr = trim(substr($address, $addr_start, $pos));
529 } else if ($name == '') {
530 $name = trim(substr($address, 0, $addr_start));
531 }
532 if (!$name && $comment) {
533 $name = $comment;
534 } else if ($name && $comment) {
535 $name = $name .' ('.$comment.')';
536 }
537 $at = strpos($addr, '@');
538 $addr_structure = new AddressStructure();
539 $addr_structure->group = $group;
540 if ($at) {
541 $addr_structure->mailbox = trim(substr($addr, 0, $at));
542 $addr_structure->host = trim(substr($addr, $at+1));
543 } else {
544 /* if lookup function */
545 if ($lookup) {
546 $aAddr = call_user_func_array($lookup,array($addr));
547 if (isset($aAddr['email'])) {
548 if (strpos($aAddr['email'],',')) {
549 return $this->parseAddress($aAddr['email'], $ar, $addr_ar, $group, $host,$lookup);
550 } else {
551 $at = strpos($aAddr['email'], '@');
552 $addr_structure->mailbox = substr($aAddr['email'], 0, $at);
553 $addr_structure->host = substr($aAddr['email'], $at+1);
554 if (isset($aAddr['name']) && $aAddr['name']) {
555 $name = $aAddr['name'];
556 } else {
557 $name = $addr;
558 }
559 }
560 }
561 }
562 if (!$addr_structure->mailbox) {
563 $addr_structure->mailbox = trim($addr);
564 if ($host) {
565 $addr_structure->host = $host;
566 }
567 }
568 }
569 $name = trim($name);
570 if (!$is_encoded && !$group) {
571 $name = encodeHeader($name);
572 }
573 if ($group && $addr == '') { /* no addresses found in group */
574 $name = $group;
575 $addr_structure->personal = $name;
576 $addr_ar[] = $addr_structure;
577 return (array($addr_ar,$pos+1 ));
578 } elseif ($group) {
579 $addr_structure->personal = $name;
580 $addr_ar[] = $addr_structure;
581 return (array($addr_ar,$pos+1 ));
582 } else {
583 $addr_structure->personal = $name;
584 if ($name || $addr) {
585 $addr_ar[] = $addr_structure;
586 }
587 }
588 if ($ar) {
589 return ($addr_ar);
590 }
591 return ($addr_ar[0]);
592 }
593
594 function parseContentType($value) {
595 $pos = strpos($value, ';');
596 $props = '';
597 if ($pos > 0) {
598 $type = trim(substr($value, 0, $pos));
599 $props = trim(substr($value, $pos+1));
600 } else {
601 $type = $value;
602 }
603 $content_type = new ContentType($type);
604 if ($props) {
605 $properties = $this->parseProperties($props);
606 if (!isset($properties['charset'])) {
607 $properties['charset'] = 'us-ascii';
608 }
609 $content_type->properties = $this->parseProperties($props);
610 }
611 $this->content_type = $content_type;
612 }
613
614 function parseProperties($value) {
615 $propArray = explode(';', $value);
616 $propResultArray = array();
617 foreach ($propArray as $prop) {
618 $prop = trim($prop);
619 $pos = strpos($prop, '=');
620 if ($pos > 0) {
621 $key = trim(substr($prop, 0, $pos));
622 $val = trim(substr($prop, $pos+1));
623 if ($val{0} == '"') {
624 $val = substr($val, 1, -1);
625 }
626 $propResultArray[$key] = $val;
627 }
628 }
629 return $propResultArray;
630 }
631
632 function parseDisposition($value) {
633 $pos = strpos($value, ';');
634 $props = '';
635 if ($pos > 0) {
636 $name = trim(substr($value, 0, $pos));
637 $props = trim(substr($value, $pos+1));
638 } else {
639 $name = $value;
640 }
641 $props_a = $this->parseProperties($props);
642 $disp = new Disposition($name);
643 $disp->properties = $props_a;
644 $this->disposition = $disp;
645 }
646
647 function mlist($field, $value) {
648 $res_a = array();
649 $value_a = explode(',', $value);
650 foreach ($value_a as $val) {
651 $val = trim($val);
652 if ($val{0} == '<') {
653 $val = substr($val, 1, -1);
654 }
655 if (substr($val, 0, 7) == 'mailto:') {
656 $res_a['mailto'] = substr($val, 7);
657 } else {
658 $res_a['href'] = $val;
659 }
660 }
661 $this->mlist[$field] = $res_a;
662 }
663
664 /*
665 * function to get the addres strings out of the header.
666 * Arguments: string or array of strings !
667 * example1: header->getAddr_s('to').
668 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
669 */
670 function getAddr_s($arr, $separator = ',',$encoded=false) {
671 $s = '';
672
673 if (is_array($arr)) {
674 foreach($arr as $arg) {
675 if ($this->getAddr_s($arg, $separator, $encoded)) {
676 $s .= $separator . $result;
677 }
678 }
679 $s = ($s ? substr($s, 2) : $s);
680 } else {
681 $addr = $this->{$arr};
682 if (is_array($addr)) {
683 foreach ($addr as $addr_o) {
684 if (is_object($addr_o)) {
685 if ($encoded) {
686 $s .= $addr_o->getEncodedAddress() . $separator;
687 } else {
688 $s .= $addr_o->getAddress() . $separator;
689 }
690 }
691 }
692 $s = substr($s, 0, -strlen($separator));
693 } else {
694 if (is_object($addr)) {
695 if ($encoded) {
696 $s .= $addr->getEncodedAddress();
697 } else {
698 $s .= $addr->getAddress();
699 }
700 }
701 }
702 }
703 return $s;
704 }
705
706 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
707 if (is_array($arg)) {
708 foreach($arg as $argument) {
709 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
710 }
711 } else {
712 $addr = $this->{$arg};
713 if (is_array($addr)) {
714 foreach ($addr as $next_addr) {
715 if (is_object($next_addr)) {
716 if (isset($next_addr->host) && ($next_addr->host != '')) {
717 $email = $next_addr->mailbox . '@' . $next_addr->host;
718 } else {
719 $email = $next_addr->mailbox;
720 }
721 $email = strtolower($email);
722 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
723 $arr[$email] = $next_addr->personal;
724 }
725 }
726 }
727 } else {
728 if (is_object($addr)) {
729 $email = $addr->mailbox;
730 $email .= (isset($addr->host) ? '@' . $addr->host : '');
731 $email = strtolower($email);
732 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
733 $arr[$email] = $addr->personal;
734 }
735 }
736 }
737 }
738 return $arr;
739 }
740
741 function findAddress($address, $recurs = false) {
742 $result = false;
743 if (is_array($address)) {
744 $i=0;
745 foreach($address as $argument) {
746 $match = $this->findAddress($argument, true);
747 $last = end($match);
748 if ($match[1]) {
749 return $i;
750 } else {
751 if (count($match[0]) && !$result) {
752 $result = $i;
753 }
754 }
755 ++$i;
756 }
757 } else {
758 if (!is_array($this->cc)) $this->cc = array();
759 $srch_addr = $this->parseAddress($address);
760 $results = array();
761 foreach ($this->to as $to) {
762 if ($to->host == $srch_addr->host) {
763 if ($to->mailbox == $srch_addr->mailbox) {
764 $results[] = $srch_addr;
765 if ($to->personal == $srch_addr->personal) {
766 if ($recurs) {
767 return array($results, true);
768 } else {
769 return true;
770 }
771 }
772 }
773 }
774 }
775 foreach ($this->cc as $cc) {
776 if ($cc->host == $srch_addr->host) {
777 if ($cc->mailbox == $srch_addr->mailbox) {
778 $results[] = $srch_addr;
779 if ($cc->personal == $srch_addr->personal) {
780 if ($recurs) {
781 return array($results, true);
782 } else {
783 return true;
784 }
785 }
786 }
787 }
788 }
789 if ($recurs) {
790 return array($results, false);
791 } elseif (count($result)) {
792 return true;
793 } else {
794 return false;
795 }
796 }
797 //exit;
798 return $result;
799 }
800
801 function getContentType($type0, $type1) {
802 $type0 = $this->content_type->type0;
803 $type1 = $this->content_type->type1;
804 return $this->content_type->properties;
805 }
806 }
807
808 ?>