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