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