a34ffc22de8e6ace7746f72a89b28ab11776d798
[squirrelmail.git] / class / mime.class.php
1 <?php
2
3 /**
4 * mime.class
5 *
6 * Copyright (c) 2002 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 rfc822_header {
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 $mime = false,
30 $content_type = '',
31 $disposition = '',
32 $xmailer = '',
33 $priority = 3,
34 $dnt = '',
35 $mlist = array(),
36 $more_headers = array(); /* only needed for constructing headers
37 in smtp.php */
38 function parseHeader($hdr) {
39 if (is_array($hdr)) {
40 $hdr = implode('', $hdr);
41 }
42
43 /* First we unfold the header */
44 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
45
46 /* Now we can make a new header array with */
47 /* each element representing a headerline */
48 $hdr = explode("\r\n" , $hdr);
49 foreach ($hdr as $line) {
50 $pos = strpos($line, ':');
51 if ($pos > 0) {
52 $field = substr($line, 0, $pos);
53 $value = trim(substr($line, $pos+1));
54 if(!preg_match('/^X.*/i', $field) &&
55 !preg_match('/^Subject/i', $field)) {
56 $value = $this->stripComments($value);
57 }
58 $this->parseField($field, $value);
59 }
60 }
61 if ($this->content_type == '') {
62 $this->parseContentType('text/plain; charset=us-ascii');
63 }
64 }
65
66 function stripComments($value) {
67 $result = '';
68
69 $cnt = strlen($value);
70 for ($i = 0; $i < $cnt; $i++) {
71 switch ($value{$i}) {
72 case '"':
73 $result .= '"';
74 while ((++$i < $cnt) && ($value{$i} != '"')) {
75 if ($value{$i} == '\\') {
76 $result .= '\\';
77 $i++;
78 }
79 $result .= $value{$i};
80 }
81 $result .= $value{$i};
82 break;
83 case '(':
84 $depth = 1;
85 while (($depth > 0) && (++$i < $cnt)) {
86 switch($value{$i}) {
87 case '\\':
88 $i++;
89 break;
90 case '(':
91 $depth++;
92 break;
93 case ')':
94 $depth--;
95 break;
96 default:
97 break;
98 }
99 }
100 break;
101 default:
102 $result .= $value{$i};
103 break;
104 }
105 }
106 return $result;
107 }
108
109 function parseField($field, $value) {
110 $field = strtolower($field);
111 switch($field) {
112 case 'date':
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':
142 $this->message_id = $value;
143 break;
144 case 'disposition-notification-to':
145 $this->dnt = $this->parseAddress($value);
146 break;
147 case 'mime-version':
148 $value = str_replace(' ', '', $value);
149 $this->mime = ($value == '1.0' ? true : $this->mime);
150 break;
151 case 'content-type':
152 $this->parseContentType($value);
153 break;
154 case 'content-disposition':
155 $this->parseDisposition($value);
156 break;
157 case 'user-agent':
158 case 'x-mailer':
159 $this->xmailer = $value;
160 break;
161 case 'x-priority':
162 $this->priority = $value;
163 break;
164 case 'list-post':
165 $this->mlist('post', $value);
166 break;
167 case 'list-reply':
168 $this->mlist('reply', $value);
169 break;
170 case 'list-subscribe':
171 $this->mlist('subscribe', $value);
172 break;
173 case 'list-unsubscribe':
174 $this->mlist('unsubscribe', $value);
175 break;
176 case 'list-archive':
177 $this->mlist('archive', $value);
178 break;
179 case 'list-owner':
180 $this->mlist('owner', $value);
181 break;
182 case 'list-help':
183 $this->mlist('help', $value);
184 break;
185 case 'list-id':
186 $this->mlist('id', $value);
187 break;
188 default:
189 break;
190 }
191 }
192
193 function parseAddress
194 ($address, $ar=false, $addr_ar = array(), $group = '') {
195 $pos = 0;
196 $j = strlen($address);
197 $name = '';
198 $addr = '';
199 while ($pos < $j) {
200 switch ($address{$pos}) {
201 case '"': /* get the personal name */
202 if ($address{++$pos} == '"') {
203 ++$pos;
204 } else {
205 while ($pos < $j && $address{$pos} != '"') {
206 if ((substr($address, $pos, 2) == '\\"') ||
207 (substr($address, $pos, 2) == '\\\\')) {
208 $name .= $address{$pos++};
209 }
210 $name .= $address{$pos++};
211 }
212 }
213 ++$pos;
214 break;
215 case '<': /* get email address */
216 $addr_start = $pos++;
217 while ($pos < $j && $address{$pos} != '>') {
218 $addr .= $address{$pos++};
219 }
220 ++$pos;
221 break;
222 case '(': /* rip off comments */
223 $addr_start = $pos;
224 for (++$pos; $pos < $j && $address{$pos} != ')'; ++$pos) {
225 $addr .= $address{$pos};
226 }
227 $address_start = substr($address, 0, $addr_start);
228 $address_end = substr($address, $pos + 1);
229 $address = $address_start . $address_end;
230 $j = strlen($address);
231 $pos = $addr_start + 1;
232 break;
233 case ',': /* we reached a delimiter */
234 if ($addr == '') {
235 $addr = substr($address, 0, $pos);
236 } else if ($name == '') {
237 $name = trim(substr($address, 0, $addr_start));
238 }
239
240 $at = strpos($addr, '@');
241 $addr_structure = new address_structure();
242 $addr_structure->personal = $name;
243 $addr_structure->group = $group;
244 if ($at) {
245 $addr_structure->mailbox = substr($addr, 0, $at);
246 $addr_structure->host = substr($addr, $at+1);
247 } else {
248 $addr_structure->mailbox = $addr;
249 }
250 $address = trim(substr($address, $pos+1));
251 $j = strlen($address);
252 $pos = 0;
253 $name = '';
254 $addr = '';
255 $addr_ar[] = $addr_structure;
256 break;
257 case ':': /* process the group addresses */
258 /* group marker */
259 $group = substr($address, 0, $pos);
260 $address = substr($address, $pos+1);
261 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
262 $addr_ar = $result[0];
263 $pos = $result[1];
264 $address = substr($address, $pos++);
265 $j = strlen($address);
266 $group = '';
267 break;
268 case ';':
269 if ($group) {
270 $address = substr($address, 0, $pos - 1);
271 }
272 ++$pos;
273 break;
274 default:
275 ++$pos;
276 break;
277 }
278 }
279 if ($addr == '') {
280 $addr = substr($address, 0, $pos);
281 } else if ($name == '') {
282 $name = trim(substr($address, 0, $addr_start));
283 }
284 $at = strpos($addr, '@');
285 $addr_structure = new address_structure();
286 $addr_structure->group = $group;
287 if ($at) {
288 $addr_structure->mailbox = trim(substr($addr, 0, $at));
289 $addr_structure->host = trim(substr($addr, $at+1));
290 } else {
291 $addr_structure->mailbox = trim($addr);
292 }
293 if ($group && $addr == '') { /* no addresses found in group */
294 $name = "$group: Undisclosed recipients;";
295 $addr_structure->personal = $name;
296 $addr_ar[] = $addr_structure;
297 return (array($addr_ar, $pos+1));
298 } else {
299 $addr_structure->personal = $name;
300 if ($name || $addr) {
301 $addr_ar[] = $addr_structure;
302 }
303 }
304 if ($ar) {
305 return ($addr_ar);
306 }
307 return ($addr_ar[0]);
308 }
309
310 function parseContentType($value) {
311 $pos = strpos($value, ';');
312 $props = '';
313 if ($pos > 0) {
314 $type = trim(substr($value, 0, $pos));
315 $props = trim(substr($type, $pos+1));
316 } else {
317 $type = $value;
318 }
319 $content_type = new content_type($type);
320 if ($props) {
321 $properties = $this->parseProperties($props);
322 if (!isset($properties['charset'])) {
323 $properties['charset'] = 'us-ascii';
324 }
325 $content_type->properties = $this->parseProperties($props);
326 }
327 $this->content_type = $content_type;
328 }
329
330 function parseProperties($value) {
331 $propArray = explode(';', $value);
332 $propResultArray = array();
333 foreach ($propArray as $prop) {
334 $prop = trim($prop);
335 $pos = strpos($prop, '=');
336 if ($pos > 0) {
337 $key = trim(substr($prop, 0, $pos));
338 $val = trim(substr($prop, $pos+1));
339 if ($val{0} == '"') {
340 $val = substr($val, 1, -1);
341 }
342 $propResultArray[$key] = $val;
343 }
344 }
345 return $propResultArray;
346 }
347
348 function parseDisposition($value) {
349 $pos = strpos($value, ';');
350 $props = '';
351 if ($pos > 0) {
352 $name = trim(substr($value, 0, $pos));
353 $props = trim(substr($type, $pos+1));
354 } else {
355 $name = $value;
356 }
357 $props_a = $this->parseProperties($props);
358 $disp = new disposition($name);
359 $disp->properties = $props_a;
360 $this->disposition = $disp;
361 }
362
363 function mlist($field, $value) {
364 $res_a = array();
365 $value_a = explode(',', $value);
366 foreach ($value_a as $val) {
367 $val = trim($val);
368 if ($val{0} == '<') {
369 $val = substr($val, 1, -1);
370 }
371 if (substr($val, 0, 7) == 'mailto:') {
372 $res_a['mailto'] = substr($val, 7);
373 } else {
374 $res_a['href'] = $val;
375 }
376 }
377 $this->mlist[$field] = $res_a;
378 }
379
380 /*
381 * function to get the addres strings out of the header.
382 * Arguments: string or array of strings !
383 * example1: header->getAddr_s('to').
384 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
385 */
386 function getAddr_s($arr, $separator = ',') {
387 $s = '';
388
389 if (is_array($arr)) {
390 foreach($arr as $arg) {
391 if ($this->getAddr_s($arg)) {
392 $s .= $separator . $result;
393 }
394 }
395 $s = ($s ? substr($s, 2) : $s);
396 } else {
397 eval('$addr = $this->' . $arr . ';') ;
398 if (is_array($addr)) {
399 foreach ($addr as $addr_o) {
400 if (is_object($addr_o)) {
401 $s .= $addr_o->getAddress() . $separator;
402 }
403 }
404 $s = substr($s, 0, -strlen($separator));
405 } else {
406 if (is_object($addr)) {
407 $s .= $addr->getAddress();
408 }
409 }
410 }
411 return $s;
412 }
413
414 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
415 if (is_array($arg)) {
416 foreach($arg as $argument) {
417 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
418 }
419 } else {
420 eval('$addr = $this->' . $arg . ';') ;
421 if (is_array($addr)) {
422 foreach ($addr as $next_addr) {
423 if (is_object($next_addr)) {
424 if (isset($next_addr->host) && ($next_addr->host != '')) {
425 $email = $next_addr->mailbox . '@' . $next_addr->host;
426 } else {
427 $email = $next_addr->mailbox;
428 }
429 $email = strtolower($email);
430 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
431 $arr[$email] = $next_addr->personal;
432 }
433 }
434 }
435 } else {
436 if (is_object($addr)) {
437 $email = $addr->mailbox;
438 $email .= (isset($addr->host) ? '@' . $addr->host : '');
439 $email = strtolower($email);
440 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
441 $arr[$email] = $addr->personal;
442 }
443 }
444 }
445 }
446 return $arr;
447 }
448
449 function getContentType($type0, $type1) {
450 $type0 = $this->content_type->type0;
451 $type1 = $this->content_type->type1;
452 return $this->content_type->properties;
453 }
454 }
455
456 class msg_header {
457 /** msg_header contains all variables available in a bodystructure **/
458 /** entity like described in rfc2060 **/
459
460 var $type0 = '',
461 $type1 = '',
462 $parameters = array(),
463 $id = 0,
464 $description = '',
465 $encoding='',
466 $size = 0,
467 $md5='',
468 $disposition = '',
469 $language='';
470
471 /*
472 * returns addres_list of supplied argument
473 * arguments: array('to', 'from', ...) or just a string like 'to'.
474 * result: string: address1, addres2, ....
475 */
476
477 function setVar($var, $value) {
478 $this->{$var} = $value;
479 }
480
481 function getParameter($p) {
482 $value = strtolower($p);
483 return (isset($this->parameters[$p]) ? $this->parameters[$p] : '');
484 }
485
486 function setParameter($parameter, $value) {
487 $this->parameters[strtolower($parameter)] = $value;
488 }
489 }
490
491
492
493 class address_structure {
494 var $personal = '',
495 $adl = '',
496 $mailbox = '',
497 $host = '',
498 $group = '';
499
500 function getAddress($full = true) {
501 $result = '';
502
503 if (is_object($this)) {
504 if (isset($this->host) && ($this->host != '')) {
505 $email = $this->mailbox.'@'.$this->host;
506 } else {
507 $email = $this->mailbox;
508 }
509 if (trim($this->personal) != '') {
510 if ($email) {
511 $addr = '"' . $this->personal . '" <' .$email.'>';
512 } else {
513 $addr = $this->personal;
514 }
515 $best_dpl = $this->personal;
516 } else {
517 $addr = $email;
518 $best_dpl = $email;
519 }
520 $result = ($full ? $addr : $best_dpl);
521 }
522 return $result;
523 }
524 }
525
526 class message {
527 /** message is the object that contains messages. It is a recursive
528 object in that through the $entities variable, it can contain
529 more objects of type message. See documentation in mime.txt for
530 a better description of how this works.
531 **/
532 var $rfc822_header = '',
533 $mime_header = '',
534 $flags = '',
535 $type0='',
536 $type1='',
537 $entities = array(),
538 $parent_ent, $entity,
539 $parent = '', $decoded_body='',
540 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
541 $is_mdnsent = 0,
542 $body_part = '',
543 $offset = 0, /* for fetching body parts out of raw messages */
544 $length = 0, /* for fetching body parts out of raw messages */
545 $att_local_name = ''; /* location where the tempory attachment
546 is stored. For future usage in smtp.php */
547
548 function setEnt($ent) {
549 $this->entity_id= $ent;
550 }
551
552 function addEntity ($msg) {
553 $msg->parent = &$this;
554 $this->entities[] = $msg;
555 }
556
557 function getFilename() {
558 $filename = '';
559 $filename = $this->header->getParameter('filename');
560 if (!$filename) {
561 $filename = $this->header->getParameter('name');
562 }
563
564 if (!$filename) {
565 $filename = 'untitled-'.$this->entity_id;
566 }
567 return $filename;
568 }
569
570
571 function addRFC822Header($read) {
572 $header = new rfc822_header();
573 $this->rfc822_header = $header->parseHeader($read);
574 }
575
576 function getEntity($ent) {
577 $cur_ent = $this->entity_id;
578 $msg = $this;
579 if (($cur_ent == '') || ($cur_ent == '0')) {
580 $cur_ent_a = array();
581 } else {
582 $cur_ent_a = explode('.', $this->entity_id);
583 }
584 $ent_a = explode('.', $ent);
585
586 $cnt = count($ent_a);
587
588 for ($i = 0; $i < $cnt -1; ++$i) {
589 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
590 $msg = $msg->parent;
591 $cur_ent_a = explode('.', $msg->entity_id);
592 --$i;
593 } else if (!isset($cur_ent_a[$i])) {
594 if (isset($msg->entities[($ent_a[$i]-1)])) {
595 $msg = $msg->entities[($ent_a[$i]-1)];
596 } else {
597 $msg = $msg->entities[0];
598 }
599 }
600 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
601 /*this is a header for a message/rfc822 entity */
602 $msg = $msg->entities[0];
603 }
604 }
605
606 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
607 /*this is a header for a message/rfc822 entity */
608 $msg = $msg->entities[0];
609 }
610
611 if (isset($msg->entities[($ent_a[$cnt-1])-1])) {
612 if (is_object($msg->entities[($ent_a[$cnt-1])-1])) {
613 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
614 }
615 }
616
617 return $msg;
618 }
619
620 function setBody($s) {
621 $this->body_part = $s;
622 }
623
624 function clean_up() {
625 $msg = $this;
626 $msg->body_part = '';
627
628 foreach ($msg->entities as $m) {
629 $m->clean_up();
630 }
631 }
632
633 function getMailbox() {
634 $msg = $this;
635 while (is_object($msg->parent)) {
636 $msg = $msg->parent;
637 }
638 return $msg->mailbox;
639 }
640
641 function calcEntity($msg) {
642 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
643 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
644 } else if (isset($this->entity_id) && ($this->entity_id != '')) {
645 $ent_no = count($this->entities)+1;
646 $par_ent = substr($this->entity_id, -2);
647 if ($par_ent{0} == '.') {
648 $par_ent = $par_ent{1};
649 }
650 if ($par_ent == '0') {
651 $ent_no = count($this->entities) + 1;
652 if ($ent_no > 0) {
653 $ent = substr($this->entity_id, 0, strrpos($this->entity_id, '.'));
654 $ent = ($ent ? $ent . ".$ent_no" : $ent_no);
655 $msg->entity_id = $ent;
656 } else {
657 $msg->entity_id = $ent_no;
658 }
659 } else {
660 $ent = $this->entity_id . ".$ent_no";
661 $msg->entity_id = $ent;
662 }
663 } else {
664 $msg->entity_id = '0';
665 }
666
667 return $msg->entity_id;
668 }
669
670
671 /*
672 * Bodystructure parser, a recursive function for generating the
673 * entity-tree with all the mime-parts.
674 *
675 * It follows RFC2060 and stores all the described fields in the
676 * message object.
677 *
678 * Question/Bugs:
679 *
680 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
681 *
682 */
683 function parseStructure($read, $i = 0) {
684 $arg_no = 0;
685 $arg_a = array();
686 $cnt = strlen($read);
687
688 for (; $i < $cnt; ++$i) {
689 $char = strtoupper($read{$i});
690 switch ($char) {
691 case '(':
692 switch($arg_no) {
693 case 0:
694 if (!isset($msg)) {
695 $msg = new message();
696 $hdr = new msg_header();
697 $hdr->type0 = 'text';
698 $hdr->type1 = 'plain';
699 $hdr->encoding = 'us-ascii';
700 $msg->entity_id = $this->calcEntity($msg);
701 } else {
702 $msg->header->type0 = 'multipart';
703 $msg->type0 = 'multipart';
704 while ($read{$i} == '(') {
705 $res = $msg->parseStructure($read, $i);
706 $i = $res[1];
707 $msg->addEntity($res[0]);
708 }
709 }
710 break;
711 case 1:
712 /* multipart properties */
713 ++$i;
714 $res = $this->parseProperties($read, $i);
715 $arg_a[] = $res[0];
716 $i = $res[1];
717 ++$arg_no;
718 break;
719 case 2:
720 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
721 ++$i;
722 $res = $msg->parseDisposition($read, $i);
723 } else { /* properties */
724 $res = $msg->parseProperties($read, $i);
725 }
726 $arg_a[] = $res[0];
727 $i = $res[1];
728 ++$arg_no;
729 break;
730 case 3:
731 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
732 ++$i;
733 $res= $msg->parseLanguage($read, $i);
734 $arg_a[] = $res[0];
735 $i = $res[1];
736 }
737 case 7:
738 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
739 $msg->header->type0 = $arg_a[0];
740 $msg->header->type1 = $arg_a[1];
741 $msg->type0 = $arg_a[0];
742 $msg->type1 = $arg_a[1];
743 $rfc822_hdr = new rfc822_header();
744 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
745 $msg->rfc822_header = $res[0];
746 $i = $res[1] + 1;
747 while (($i < $cnt) && ($read{$i} != '(')) {
748 ++$i;
749 }
750 $res = $msg->parseStructure($read, $i);
751 $i = $res[1];
752 $msg->addEntity($res[0]);
753 }
754 break;
755 case 8:
756 ++$i;
757 $res = $msg->parseDisposition($read, $i);
758 $arg_a[] = $res[0];
759 $i = $res[1];
760 ++$arg_no;
761 break;
762 case 9:
763 ++$i;
764 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
765 $res = $msg->parseDisposition($read, $i);
766 } else {
767 $res = $msg->parseLanguage($read, $i);
768 }
769 $arg_a[] = $res[0];
770 $i = $res[1];
771 ++$arg_no;
772 break;
773 case 10:
774 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
775 ++$i;
776 $res = $msg->parseLanguage($read, $i);
777 $arg_a[] = $res[0];
778 $i = $res[1];
779 } else {
780 $i = $msg->parseParenthesis($read, $i);
781 $arg_a[] = ''; /* not yet described in rfc2060 */
782 }
783 ++$arg_no;
784 break;
785 default:
786 /* unknown argument, skip this part */
787 $i = $msg->parseParenthesis($read, $i);
788 $arg_a[] = '';
789 ++$arg_no;
790 break;
791 } /* switch */
792 break;
793 case '"':
794 /* inside an entity -> start processing */
795 $debug = substr($read, $i, 20);
796 $res = $msg->parseQuote($read, $i);
797 $arg_s = $res[0];
798 $i = $res[1];
799 ++$arg_no;
800 if ($arg_no < 3) {
801 $arg_s = strtolower($arg_s); /* type0 and type1 */
802 }
803 $arg_a[] = $arg_s;
804 break;
805 case 'n':
806 case 'N':
807 /* probably NIL argument */
808 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
809 $arg_a[] = '';
810 ++$arg_no;
811 $i += 2;
812 }
813 break;
814 case '{':
815 /* process the literal value */
816 $res = $msg->parseLiteral($read, $i);
817 $arg_s = $res[0];
818 $i = $res[1];
819 ++$arg_no;
820 break;
821 case is_numeric($read{$i}):
822 /* process integers */
823 if ($read{$i} == ' ') { break; }
824 $arg_s = $read{$i};;
825 for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) {
826 $arg_s .= $read{$i};
827 }
828 ++$arg_no;
829 $arg_a[] = $arg_s;
830 break;
831 case ')':
832 $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
833 if (!$multipart) {
834 $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
835 $hdr->type0 = $arg_a[0];
836 $hdr->type1 = $arg_a[1];
837
838 $msg->type0 = $arg_a[0];
839 $msg->type1 = $arg_a[1];
840 $arr = $arg_a[2];
841 if (is_array($arr)) {
842 $hdr->parameters = $arg_a[2];
843 }
844 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
845 $hdr->description = $arg_a[4];
846 $hdr->encoding = strtolower($arg_a[5]);
847 $hdr->entity_id = $msg->entity_id;
848 $hdr->size = $arg_a[6];
849 if ($shifted_args) {
850 $hdr->lines = $arg_a[7];
851 $s = 1;
852 } else {
853 $s = 0;
854 }
855 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
856 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
857 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
858 $msg->header = $hdr;
859 if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) {
860 $msg->entity_id = $this->entity_id . '.1';
861 }
862 } else {
863 $hdr->type0 = 'multipart';
864 $hdr->type1 = $arg_a[0];
865 $msg->type0 = 'multipart';
866 $msg->type1 = $arg_a[0];
867 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
868 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
869 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
870 $msg->header = $hdr;
871 }
872 ++$i;
873 return (array($msg, $i));
874 default: break;
875 } /* switch */
876
877 } /* for */
878 } /* parsestructure */
879
880 function parseProperties($read, $i) {
881 $properties = array();
882 $prop_name = '';
883
884 for (; $read{$i} != ')'; ++$i) {
885 $arg_s = '';
886 if ($read{$i} == '"') {
887 $res = $this->parseQuote($read, $i);
888 $arg_s = $res[0];
889 $i = $res[1];
890 } else if ($read{$i} == '{') {
891 $res = $this->parseLiteral($read, $i);
892 $arg_s = $res[0];
893 $i = $res[1];
894 }
895
896 if ($arg_s != '') {
897 if ($prop_name == '') {
898 $prop_name = strtolower($arg_s);
899 $properties[$prop_name] = '';
900 } else if ($prop_name != '') {
901 $properties[$prop_name] = $arg_s;
902 $prop_name = '';
903 }
904 }
905 }
906 return array($properties, $i);
907 }
908
909 function parseEnvelope($read, $i, $hdr) {
910 $arg_no = 0;
911 $arg_a = array();
912 $cnt = strlen($read);
913
914 for (; ($i < $cnt) && ($read{$i} != ')'); ++$i) {
915 ++$i;
916 $char = strtoupper($read{$i});
917 switch ($char) {
918 case '"':
919 $res = $this->parseQuote($read, $i);
920 $arg_a[] = $res[0];
921 $i = $res[1];
922 ++$arg_no;
923 break;
924 case '{':
925 $res = $this->parseLiteral($read, $i);
926 $arg_a[] = $res[0];
927 $i = $res[1];
928 ++$arg_no;
929 break;
930 case 'N':
931 /* probably NIL argument */
932 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
933 $arg_a[] = '';
934 ++$arg_no;
935 $i += 2;
936 }
937 break;
938 case '(':
939 /* Address structure (with group support)
940 * Note: Group support is useless on SMTP connections
941 * because the protocol doesn't support it
942 */
943 $addr_a = array();
944 $group = '';
945 $a=0;
946 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
947 if ($read{$i} == '(') {
948 $res = $this->parseAddress($read, $i);
949 $addr = $res[0];
950 $i = $res[1];
951 if (($addr->host == '') && ($addr->mailbox != '')) {
952 /* start of group */
953 $group = $addr->mailbox;
954 $group_addr = $addr;
955 $j = $a;
956 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
957 /* end group */
958 if ($a == ($j+1)) { /* no group members */
959 $group_addr->group = $group;
960 $group_addr->mailbox = '';
961 $group_addr->personal = "$group: Undisclosed recipients;";
962 $addr_a[] = $group_addr;
963 $group ='';
964 }
965 } else {
966 $addr->group = $group;
967 $addr_a[] = $addr;
968 }
969 ++$a;
970 }
971 }
972 $arg_a[] = $addr_a;
973 break;
974 default: break;
975 }
976 }
977
978 if (count($arg_a) > 9) {
979 /* argument 1: date */
980 $d = strtr($arg_a[0], array(' ' => ' '));
981 $d = explode(' ', $d);
982 $hdr->date = getTimeStamp($d);
983
984 /* argument 2: subject */
985 $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]);
986 $hdr->subject = $arg_a[1];
987
988 $hdr->from = $arg_a[2][0]; /* argument 3: from */
989 $hdr->sender = $arg_a[3][0]; /* argument 4: sender */
990 $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */
991 $hdr->to = $arg_a[5]; /* argument 6: to */
992 $hdr->cc = $arg_a[6]; /* argument 7: cc */
993 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
994 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
995 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
996 }
997 return (array($hdr, $i));
998 }
999
1000 function parseLiteral($read, $i) {
1001 $lit_cnt = '';
1002 for (++$i; $read{$i} != '}'; ++$i) {
1003 $lit_cnt .= $read{$i};
1004 }
1005
1006 $lit_cnt +=2; /* add the { and } characters */
1007 $s = '';
1008 for ($j = 0; $j < $lit_cnt; ++$j) {
1009 $s .= $read{++$i};
1010 }
1011 return (array($s, $i));
1012 }
1013
1014 function parseQuote($read, $i) {
1015 $s = '';
1016 for (++$i; $read{$i} != '"'; ++$i) {
1017 if ($read{$i} == '\\') {
1018 ++$i;
1019 }
1020 $s .= $read{$i};
1021 }
1022 return (array($s, $i));
1023 }
1024
1025 function parseAddress($read, $i) {
1026 $arg_a = array();
1027
1028 for (; $read{$i} != ')'; ++$i) {
1029 $char = strtoupper($read{$i});
1030 switch ($char) {
1031 case '"':
1032 case '{':
1033 $res = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1034 $arg_a[] = $res[0];
1035 $i = $res[1];
1036 break;
1037 case 'n':
1038 case 'N':
1039 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1040 $arg_a[] = '';
1041 $i += 2;
1042 }
1043 break;
1044 default: break;
1045 }
1046 }
1047
1048 if (count($arg_a) == 4) {
1049 $adr = new address_structure();
1050 $adr->personal = $arg_a[0];
1051 $adr->adl = $arg_a[1];
1052 $adr->mailbox = $arg_a[2];
1053 $adr->host = $arg_a[3];
1054 } else {
1055 $adr = '';
1056 }
1057 return (array($adr, $i));
1058 }
1059
1060 function parseDisposition($read, $i) {
1061 $arg_a = array();
1062
1063 for (; $read{$i} != ')'; ++$i) {
1064 switch ($read{$i}) {
1065 case '"':
1066 case '{':
1067 case '(':
1068 switch ($read{$i}) {
1069 case '"': $res = $this->parseQuote($read, $i); break;
1070 case '{': $res = $this->parseLiteral($read, $i); break;
1071 case '(': $res = $this->parseProperties($read, $i); break;
1072 }
1073 $arg_a[] = $res[0];
1074 $i = $res[1];
1075 break;
1076 default: break;
1077 }
1078 }
1079
1080 if (isset($arg_a[0])) {
1081 $disp = new disposition($arg_a[0]);
1082 if (isset($arg_a[1])) {
1083 $disp->properties = $arg_a[1];
1084 }
1085 }
1086
1087 return (is_object($disp) ? array($disp, $i) : array('', $i));
1088 }
1089
1090 function parseLanguage($read, $i) {
1091 /* no idea how to process this one without examples */
1092 $arg_a = array();
1093
1094 for (; $read{$i} != ')'; ++$i) {
1095 switch ($read{$i}) {
1096 case '"':
1097 case '{':
1098 case '(':
1099 switch ($read{$i}) {
1100 case '"': $res = $this->parseQuote($read, $i); break;
1101 case '{': $res = $this->parseLiteral($read, $i); break;
1102 case '(': $res = $this->parseProperties($read, $i); break;
1103 }
1104 $arg_a[] = $res[0];
1105 $i = $res[1];
1106 break;
1107 default: break;
1108 }
1109 }
1110
1111 if (isset($arg_a[0])) {
1112 $lang = new language($arg_a[0]);
1113 if (isset($arg_a[1])) {
1114 $lang->properties = $arg_a[1];
1115 }
1116 }
1117
1118 return (is_object($lang) ? array($lang, $i) : array('', $i));
1119 }
1120
1121 function parseParenthesis($read, $i) {
1122 for (; $read{$i} != ')'; ++$i) {
1123 switch ($read{$i}) {
1124 case '"':
1125 case '{':
1126 case '(':
1127 switch ($read{$i}) {
1128 case '"': $res = $this->parseQuote($read, $i); break;
1129 case '{': $res = $this->parseLiteral($read, $i); break;
1130 case '(': $res = $this->parseProperties($read, $i); break;
1131 }
1132 $i = $res[1];
1133 break;
1134 default: break;
1135 }
1136 }
1137 return $i;
1138 }
1139
1140 /* Function to fill the message structure in case the */
1141 /* bodystructure is not available NOT FINISHED YET */
1142 function parseMessage($read, $type0, $type1) {
1143 switch ($type0) {
1144 case 'message':
1145 $rfc822_header = true;
1146 $mime_header = false;
1147 break;
1148 case 'multipart':
1149 $rfc822_header = false;
1150 $mime_header = true;
1151 break;
1152 default: return $read;
1153 }
1154
1155 for ($i = 1; $i < $count; ++$i) {
1156 $line = trim($body[$i]);
1157 if (($mime_header || $rfc822_header) &&
1158 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
1159 $bnd = $reg[1];
1160 $bndreg = $bnd;
1161 $bndreg = str_replace("\\", "\\\\", $bndreg);
1162 $bndreg = str_replace("?", "\\?", $bndreg);
1163 $bndreg = str_replace("+", "\\+", $bndreg);
1164 $bndreg = str_replace(".", "\\.", $bndreg);
1165 $bndreg = str_replace("/", "\\/", $bndreg);
1166 $bndreg = str_replace("-", "\\-", $bndreg);
1167 $bndreg = str_replace("(", "\\(", $bndreg);
1168 $bndreg = str_replace(")", "\\)", $bndreg);
1169 } else if ($rfc822_header && $line == '') {
1170 $rfc822_header = false;
1171 if ($msg->type0 == 'multipart') {
1172 $mime_header = true;
1173 }
1174 }
1175
1176 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
1177 $cnt=count($boundaries)-1;
1178 $bnd = $boundaries[$cnt]['bnd'];
1179 $bndreg = $boundaries[$cnt]['bndreg'];
1180
1181 $regstr = '/^--'."($bndreg)".".*".'/';
1182 if (preg_match($regstr, $line, $reg)) {
1183 $bndlen = strlen($reg[1]);
1184 $bndend = false;
1185 if (strlen($line) > ($bndlen + 3)) {
1186 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
1187 $bndend = true;
1188 }
1189 }
1190 if ($bndend) {
1191 /* calc offset and return $msg */
1192 //$entStr = CalcEntity("$entStr", -1);
1193 array_pop($boundaries);
1194 $mime_header = true;
1195 $bnd_end = true;
1196 } else {
1197 $mime_header = true;
1198 $bnd_end = false;
1199 //$entStr = CalcEntity("$entStr", 0);
1200 ++$content_indx;
1201 }
1202 } else {
1203 if ($header) { }
1204 }
1205 }
1206 }
1207 }
1208
1209 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
1210 $found = false;
1211 if ($this->type0 == 'multipart') {
1212 if($this->type1 == 'alternative') {
1213 $msg = $this->findAlternativeEntity($alt_order);
1214 if (count($msg->entities) == 0) {
1215 $entity[] = $msg->entity_id;
1216 } else {
1217 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1218 }
1219 $found = true;
1220 } else if ($this->type1 == 'related') { /* RFC 2387 */
1221 $msgs = $this->findRelatedEntity();
1222 foreach ($msgs as $msg) {
1223 if (count($msg->entities) == 0) {
1224 $entity[] = $msg->entity_id;
1225 } else {
1226 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1227 }
1228 }
1229 if (count($msgs) > 0) {
1230 $found = true;
1231 }
1232 } else { /* Treat as multipart/mixed */
1233 foreach ($this->entities as $ent) {
1234 if(strtolower($ent->header->disposition->name) != 'attachment' &&
1235 ($ent->type0 != 'message' && $ent->type1 != 'rfc822'))
1236 {
1237 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1238 $found = true;
1239 }
1240 }
1241 }
1242 } else { /* If not multipart, then just compare with each entry from $alt_order */
1243 $type = $this->type0.'/'.$this->type1;
1244 foreach ($alt_order as $alt) {
1245 if( ($alt == $type) && isset($this->entity_id) ) {
1246 if ( (count($this->entities) == 0) &&
1247 (strtolower($this->header->disposition->name) != 'attachment') )
1248 {
1249 $entity[] = $this->entity_id;
1250 $found = true;
1251 }
1252 }
1253 }
1254 }
1255 if(!$found) {
1256 foreach ($this->entities as $ent) {
1257 if((strtolower($ent->header->disposition->name) != 'attachment') &&
1258 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
1259 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1260 $found = true;
1261 }
1262 }
1263 }
1264 if(!$strict && !$found) {
1265 if ($this->type0 == 'text' &&
1266 ($this->type1 == 'plain' ||
1267 $this->type1 == 'html' ||
1268 $this->type1 == 'message') &&
1269 isset($this->entity_id) )
1270 {
1271 if (count($this->entities) == 0) {
1272 if (strtolower($this->header->disposition->name) != 'attachment') {
1273 $entity[] = $this->entity_id;
1274 }
1275 }
1276 }
1277 }
1278
1279 return $entity;
1280 }
1281
1282 function findAlternativeEntity($alt_order) {
1283 /* If we are dealing with alternative parts then we */
1284 /* choose the best viewable message supported by SM. */
1285 $best_view = 0;
1286 $entity = array();
1287 $altcount = count($alt_order);
1288
1289 foreach($this->entities as $ent) {
1290 $type = $ent->header->type0 . '/' . $ent->header->type1;
1291 if ($type == 'multipart/related') {
1292 $type = $ent->header->getParameter('type');
1293 }
1294 for ($j = $best_view; $j < $altcount; ++$j) {
1295 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1296 $best_view = $j;
1297 $entity = $ent;
1298 }
1299 }
1300 }
1301
1302 return $entity;
1303 }
1304
1305 function findRelatedEntity() {
1306 $msgs = array();
1307 $entcount = count($this->entities);
1308
1309 for ($i = 0; $i < $entcount; ++$i) {
1310 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
1311 if ($this->header->getParameter('type') == $type) {
1312 $msgs[] = $this->entities[$i];
1313 }
1314 }
1315
1316 return $msgs;
1317 }
1318
1319 function getAttachments($exclude_id=array(), $result = array()) {
1320 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
1321 $this = $this->entities[0];
1322 }
1323
1324 if (count($this->entities)) {
1325 foreach ($this->entities as $entity) {
1326 $exclude = false;
1327
1328 foreach ($exclude_id as $excl) {
1329 if ($entity->entity_id === $excl) {
1330 $exclude = true;
1331 }
1332 }
1333
1334 if (!$exclude) {
1335 if (($entity->type0 == 'multipart') &&
1336 ($entity->type1 != 'related')) {
1337 $result = $entity->getAttachments($exclude_id, $result);
1338 } else if ($entity->type0 != 'multipart') {
1339 $result[] = $entity;
1340 }
1341 }
1342 }
1343 } else {
1344 $exclude = false;
1345 foreach ($exclude_id as $excl) {
1346 $exclude = $exclude || ($this->entity_id == $excl);
1347 }
1348
1349 if (!$exclude) {
1350 $result[] = $this;
1351 }
1352 }
1353
1354 return $result;
1355 }
1356 }
1357
1358 class smime_message {
1359
1360 }
1361
1362 class disposition {
1363 function disposition($name) {
1364 $this->name = $name;
1365 $this->properties = array();
1366 }
1367
1368 function getProperty($par) {
1369 $value = strtolower($par);
1370 if (isset($this->properties[$par])) {
1371 return $this->properties[$par];
1372 }
1373 return '';
1374 }
1375 }
1376
1377 class language {
1378 function language($name) {
1379 $this->name = $name;
1380 $this->properties = array();
1381 }
1382 }
1383
1384 class content_type {
1385 var $type0 = 'text',
1386 $type1 = 'plain',
1387 $properties = '';
1388
1389 function content_type($type) {
1390 $pos = strpos($type, '/');
1391 if ($pos > 0) {
1392 $this->type0 = substr($type, 0, $pos);
1393 $this->type1 = substr($type, $pos+1);
1394 } else {
1395 $this->type0 = $type;
1396 }
1397 $this->properties = array();
1398 }
1399 }
1400
1401 ?>