Changed the way we handle entities in message/rfc822 types. $msg->entity_id++
[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
546 function setEnt($ent) {
547 $this->entity_id= $ent;
548 }
549
550 function addEntity ($msg) {
551 $msg->parent = &$this;
552 $this->entities[] = $msg;
553 }
554
555 function getFilename() {
556 $filename = '';
557 $filename = $this->header->getParameter('filename');
558 if (!$filename) {
559 $filename = $this->header->getParameter('name');
560 }
561
562 if (!$filename) {
563 $filename = 'untitled-'.$this->entity_id;
564 }
565 return $filename;
566 }
567
568
569 function addRFC822Header($read) {
570 $header = new rfc822_header();
571 $this->rfc822_header = $header->parseHeader($read);
572 }
573
574 function getEntity($ent) {
575 $cur_ent = $this->entity_id;
576 $msg = $this;
577 if (($cur_ent == '') || ($cur_ent == '0')) {
578 $cur_ent_a = array();
579 } else {
580 $cur_ent_a = explode('.', $this->entity_id);
581 }
582 $ent_a = explode('.', $ent);
583
584 $cnt = count($ent_a);
585
586 for ($i = 0; $i < $cnt -1; ++$i) {
587 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
588 $msg = $msg->parent;
589 $cur_ent_a = explode('.', $msg->entity_id);
590 --$i;
591 } else if (!isset($cur_ent_a[$i])) {
592 if (isset($msg->entities[($ent_a[$i]-1)])) {
593 $msg = $msg->entities[($ent_a[$i]-1)];
594 } else {
595 $msg = $msg->entities[0];
596 }
597 }
598 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
599 /*this is a header for a message/rfc822 entity */
600 $msg = $msg->entities[0];
601 }
602 }
603
604 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
605 /*this is a header for a message/rfc822 entity */
606 $msg = $msg->entities[0];
607 }
608
609 if (isset($msg->entities[($ent_a[$cnt-1])-1])) {
610 if (is_object($msg->entities[($ent_a[$cnt-1])-1])) {
611 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
612 }
613 }
614
615 return $msg;
616 }
617
618 function setBody($s) {
619 $this->body_part = $s;
620 }
621
622 function clean_up() {
623 $msg = $this;
624 $msg->body_part = '';
625
626 foreach ($msg->entities as $m) {
627 $m->clean_up();
628 }
629 }
630
631 function getMailbox() {
632 $msg = $this;
633 while (is_object($msg->parent)) {
634 $msg = $msg->parent;
635 }
636 return $msg->mailbox;
637 }
638
639 function calcEntity($msg) {
640 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
641 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
642 } else if (isset($this->entity_id) && ($this->entity_id != '')) {
643 $ent_no = count($this->entities)+1;
644 $par_ent = substr($this->entity_id, -2);
645 if ($par_ent{0} == '.') {
646 $par_ent = $par_ent{1};
647 }
648 if ($par_ent == '0') {
649 $ent_no = count($this->entities) + 1;
650 if ($ent_no > 0) {
651 $ent = substr($this->entity_id, 0, strrpos($this->entity_id, '.'));
652 $ent = ($ent ? $ent . ".$ent_no" : $ent_no);
653 $msg->entity_id = $ent;
654 } else {
655 $msg->entity_id = $ent_no;
656 }
657 } else {
658 $ent = $this->entity_id . ".$ent_no";
659 $msg->entity_id = $ent;
660 }
661 } else {
662 $msg->entity_id = '0';
663 }
664
665 return $msg->entity_id;
666 }
667
668
669 /*
670 * Bodystructure parser, a recursive function for generating the
671 * entity-tree with all the mime-parts.
672 *
673 * It follows RFC2060 and stores all the described fields in the
674 * message object.
675 *
676 * Question/Bugs:
677 *
678 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
679 *
680 */
681 function parseStructure($read, $i = 0) {
682 $arg_no = 0;
683 $arg_a = array();
684 $cnt = strlen($read);
685
686 for (; $i < $cnt; ++$i) {
687 $char = strtoupper($read{$i});
688 switch ($char) {
689 case '(':
690 switch($arg_no) {
691 case 0:
692 if (!isset($msg)) {
693 $msg = new message();
694 $hdr = new msg_header();
695 $hdr->type0 = 'text';
696 $hdr->type1 = 'plain';
697 $hdr->encoding = 'us-ascii';
698 $msg->entity_id = $this->calcEntity($msg);
699 } else {
700 $msg->header->type0 = 'multipart';
701 $msg->type0 = 'multipart';
702 while ($read{$i} == '(') {
703 $res = $msg->parseStructure($read, $i);
704 $i = $res[1];
705 $msg->addEntity($res[0]);
706 }
707 }
708 break;
709 case 1:
710 /* multipart properties */
711 ++$i;
712 $res = $this->parseProperties($read, $i);
713 $arg_a[] = $res[0];
714 $i = $res[1];
715 ++$arg_no;
716 break;
717 case 2:
718 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
719 ++$i;
720 $res = $msg->parseDisposition($read, $i);
721 } else { /* properties */
722 $res = $msg->parseProperties($read, $i);
723 }
724 $arg_a[] = $res[0];
725 $i = $res[1];
726 ++$arg_no;
727 break;
728 case 3:
729 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
730 ++$i;
731 $res= $msg->parseLanguage($read, $i);
732 $arg_a[] = $res[0];
733 $i = $res[1];
734 }
735 case 7:
736 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
737 $msg->header->type0 = $arg_a[0];
738 $msg->header->type1 = $arg_a[1];
739 $msg->type0 = $arg_a[0];
740 $msg->type1 = $arg_a[1];
741 $rfc822_hdr = new rfc822_header();
742 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
743 $msg->rfc822_header = $res[0];
744 $i = $res[1] + 1;
745 while (($i < $cnt) && ($read{$i} != '(')) {
746 ++$i;
747 }
748 $res = $msg->parseStructure($read, $i);
749 $i = $res[1];
750 $msg->addEntity($res[0]);
751 }
752 break;
753 case 8:
754 ++$i;
755 $res = $msg->parseDisposition($read, $i);
756 $arg_a[] = $res[0];
757 $i = $res[1];
758 ++$arg_no;
759 break;
760 case 9:
761 ++$i;
762 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
763 $res = $msg->parseDisposition($read, $i);
764 } else {
765 $res = $msg->parseLanguage($read, $i);
766 }
767 $arg_a[] = $res[0];
768 $i = $res[1];
769 ++$arg_no;
770 break;
771 case 10:
772 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
773 ++$i;
774 $res = $msg->parseLanguage($read, $i);
775 $arg_a[] = $res[0];
776 $i = $res[1];
777 } else {
778 $i = $msg->parseParenthesis($read, $i);
779 $arg_a[] = ''; /* not yet described in rfc2060 */
780 }
781 ++$arg_no;
782 break;
783 default:
784 /* unknown argument, skip this part */
785 $i = $msg->parseParenthesis($read, $i);
786 $arg_a[] = '';
787 ++$arg_no;
788 break;
789 } /* switch */
790 break;
791 case '"':
792 /* inside an entity -> start processing */
793 $debug = substr($read, $i, 20);
794 $res = $msg->parseQuote($read, $i);
795 $arg_s = $res[0];
796 $i = $res[1];
797 ++$arg_no;
798 if ($arg_no < 3) {
799 $arg_s = strtolower($arg_s); /* type0 and type1 */
800 }
801 $arg_a[] = $arg_s;
802 break;
803 case 'n':
804 case 'N':
805 /* probably NIL argument */
806 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
807 $arg_a[] = '';
808 ++$arg_no;
809 $i += 2;
810 }
811 break;
812 case '{':
813 /* process the literal value */
814 $res = $msg->parseLiteral($read, $i);
815 $arg_s = $res[0];
816 $i = $res[1];
817 ++$arg_no;
818 break;
819 case is_numeric($read{$i}):
820 /* process integers */
821 if ($read{$i} == ' ') { break; }
822 $arg_s = $read{$i};;
823 for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) {
824 $arg_s .= $read{$i};
825 }
826 ++$arg_no;
827 $arg_a[] = $arg_s;
828 break;
829 case ')':
830 $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
831 if (!$multipart) {
832 $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
833 $hdr->type0 = $arg_a[0];
834 $hdr->type1 = $arg_a[1];
835
836 $msg->type0 = $arg_a[0];
837 $msg->type1 = $arg_a[1];
838 $arr = $arg_a[2];
839 if (is_array($arr)) {
840 $hdr->parameters = $arg_a[2];
841 }
842 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
843 $hdr->description = $arg_a[4];
844 $hdr->encoding = strtolower($arg_a[5]);
845 $hdr->entity_id = $msg->entity_id;
846 $hdr->size = $arg_a[6];
847 if ($shifted_args) {
848 $hdr->lines = $arg_a[7];
849 $s = 1;
850 } else {
851 $s = 0;
852 }
853 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
854 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
855 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
856 $msg->header = $hdr;
857 if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) {
858 $msg->entity_id = $this->entity_id . '.1';
859 }
860 } else {
861 $hdr->type0 = 'multipart';
862 $hdr->type1 = $arg_a[0];
863 $msg->type0 = 'multipart';
864 $msg->type1 = $arg_a[0];
865 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
866 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
867 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
868 $msg->header = $hdr;
869 }
870 ++$i;
871 return (array($msg, $i));
872 default: break;
873 } /* switch */
874
875 } /* for */
876 } /* parsestructure */
877
878 function parseProperties($read, $i) {
879 $properties = array();
880 $prop_name = '';
881
882 for (; $read{$i} != ')'; ++$i) {
883 $arg_s = '';
884 if ($read{$i} == '"') {
885 $res = $this->parseQuote($read, $i);
886 $arg_s = $res[0];
887 $i = $res[1];
888 } else if ($read{$i} == '{') {
889 $res = $this->parseLiteral($read, $i);
890 $arg_s = $res[0];
891 $i = $res[1];
892 }
893
894 if ($arg_s != '') {
895 if ($prop_name == '') {
896 $prop_name = strtolower($arg_s);
897 $properties[$prop_name] = '';
898 } else if ($prop_name != '') {
899 $properties[$prop_name] = $arg_s;
900 $prop_name = '';
901 }
902 }
903 }
904 return array($properties, $i);
905 }
906
907 function parseEnvelope($read, $i, $hdr) {
908 $arg_no = 0;
909 $arg_a = array();
910 $cnt = strlen($read);
911
912 for (; ($i < $cnt) && ($read{$i} != ')'); ++$i) {
913 ++$i;
914 $char = strtoupper($read{$i});
915 switch ($char) {
916 case '"':
917 $res = $this->parseQuote($read, $i);
918 $arg_a[] = $res[0];
919 $i = $res[1];
920 ++$arg_no;
921 break;
922 case '{':
923 $res = $this->parseLiteral($read, $i);
924 $arg_a[] = $res[0];
925 $i = $res[1];
926 ++$arg_no;
927 break;
928 case 'N':
929 /* probably NIL argument */
930 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
931 $arg_a[] = '';
932 ++$arg_no;
933 $i += 2;
934 }
935 break;
936 case '(':
937 /* Address structure (with group support)
938 * Note: Group support is useless on SMTP connections
939 * because the protocol doesn't support it
940 */
941 $addr_a = array();
942 $group = '';
943 $a=0;
944 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
945 if ($read{$i} == '(') {
946 $res = $this->parseAddress($read, $i);
947 $addr = $res[0];
948 $i = $res[1];
949 if (($addr->host == '') && ($addr->mailbox != '')) {
950 /* start of group */
951 $group = $addr->mailbox;
952 $group_addr = $addr;
953 $j = $a;
954 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
955 /* end group */
956 if ($a == ($j+1)) { /* no group members */
957 $group_addr->group = $group;
958 $group_addr->mailbox = '';
959 $group_addr->personal = "$group: Undisclosed recipients;";
960 $addr_a[] = $group_addr;
961 $group ='';
962 }
963 } else {
964 $addr->group = $group;
965 $addr_a[] = $addr;
966 }
967 ++$a;
968 }
969 }
970 $arg_a[] = $addr_a;
971 break;
972 default: break;
973 }
974 }
975
976 if (count($arg_a) > 9) {
977 /* argument 1: date */
978 $d = strtr($arg_a[0], array(' ' => ' '));
979 $d = explode(' ', $d);
980 $hdr->date = getTimeStamp($d);
981
982 /* argument 2: subject */
983 $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]);
984 $hdr->subject = $arg_a[1];
985
986 $hdr->from = $arg_a[2][0]; /* argument 3: from */
987 $hdr->sender = $arg_a[3][0]; /* argument 4: sender */
988 $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */
989 $hdr->to = $arg_a[5]; /* argument 6: to */
990 $hdr->cc = $arg_a[6]; /* argument 7: cc */
991 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
992 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
993 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
994 }
995 return (array($hdr, $i));
996 }
997
998 function parseLiteral($read, $i) {
999 $lit_cnt = '';
1000 for (++$i; $read{$i} != '}'; ++$i) {
1001 $lit_cnt .= $read{$i};
1002 }
1003
1004 $lit_cnt +=2; /* add the { and } characters */
1005 $s = '';
1006 for ($j = 0; $j < $lit_cnt; ++$j) {
1007 $s .= $read{++$i};
1008 }
1009 return (array($s, $i));
1010 }
1011
1012 function parseQuote($read, $i) {
1013 $s = '';
1014 for (++$i; $read{$i} != '"'; ++$i) {
1015 if ($read{$i} == '\\') {
1016 ++$i;
1017 }
1018 $s .= $read{$i};
1019 }
1020 return (array($s, $i));
1021 }
1022
1023 function parseAddress($read, $i) {
1024 $arg_a = array();
1025
1026 for (; $read{$i} != ')'; ++$i) {
1027 $char = strtoupper($read{$i});
1028 switch ($char) {
1029 case '"':
1030 case '{':
1031 $res = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1032 $arg_a[] = $res[0];
1033 $i = $res[1];
1034 break;
1035 case 'n':
1036 case 'N':
1037 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1038 $arg_a[] = '';
1039 $i += 2;
1040 }
1041 break;
1042 default: break;
1043 }
1044 }
1045
1046 if (count($arg_a) == 4) {
1047 $adr = new address_structure();
1048 $adr->personal = $arg_a[0];
1049 $adr->adl = $arg_a[1];
1050 $adr->mailbox = $arg_a[2];
1051 $adr->host = $arg_a[3];
1052 } else {
1053 $adr = '';
1054 }
1055 return (array($adr, $i));
1056 }
1057
1058 function parseDisposition($read, $i) {
1059 $arg_a = array();
1060
1061 for (; $read{$i} != ')'; ++$i) {
1062 switch ($read{$i}) {
1063 case '"':
1064 case '{':
1065 case '(':
1066 switch ($read{$i}) {
1067 case '"': $res = $this->parseQuote($read, $i); break;
1068 case '{': $res = $this->parseLiteral($read, $i); break;
1069 case '(': $res = $this->parseProperties($read, $i); break;
1070 }
1071 $arg_a[] = $res[0];
1072 $i = $res[1];
1073 break;
1074 default: break;
1075 }
1076 }
1077
1078 if (isset($arg_a[0])) {
1079 $disp = new disposition($arg_a[0]);
1080 if (isset($arg_a[1])) {
1081 $disp->properties = $arg_a[1];
1082 }
1083 }
1084
1085 return (is_object($disp) ? array($disp, $i) : array('', $i));
1086 }
1087
1088 function parseLanguage($read, $i) {
1089 /* no idea how to process this one without examples */
1090 $arg_a = array();
1091
1092 for (; $read{$i} != ')'; ++$i) {
1093 switch ($read{$i}) {
1094 case '"':
1095 case '{':
1096 case '(':
1097 switch ($read{$i}) {
1098 case '"': $res = $this->parseQuote($read, $i); break;
1099 case '{': $res = $this->parseLiteral($read, $i); break;
1100 case '(': $res = $this->parseProperties($read, $i); break;
1101 }
1102 $arg_a[] = $res[0];
1103 $i = $res[1];
1104 break;
1105 default: break;
1106 }
1107 }
1108
1109 if (isset($arg_a[0])) {
1110 $lang = new language($arg_a[0]);
1111 if (isset($arg_a[1])) {
1112 $lang->properties = $arg_a[1];
1113 }
1114 }
1115
1116 return (is_object($lang) ? array($lang, $i) : array('', $i));
1117 }
1118
1119 function parseParenthesis($read, $i) {
1120 for (; $read{$i} != ')'; ++$i) {
1121 switch ($read{$i}) {
1122 case '"':
1123 case '{':
1124 case '(':
1125 switch ($read{$i}) {
1126 case '"': $res = $this->parseQuote($read, $i); break;
1127 case '{': $res = $this->parseLiteral($read, $i); break;
1128 case '(': $res = $this->parseProperties($read, $i); break;
1129 }
1130 $i = $res[1];
1131 break;
1132 default: break;
1133 }
1134 }
1135 return $i;
1136 }
1137
1138 /* Function to fill the message structure in case the */
1139 /* bodystructure is not available NOT FINISHED YET */
1140 function parseMessage($read, $type0, $type1) {
1141 switch ($type0) {
1142 case 'message':
1143 $rfc822_header = true;
1144 $mime_header = false;
1145 break;
1146 case 'multipart':
1147 $rfc822_header = false;
1148 $mime_header = true;
1149 break;
1150 default: return $read;
1151 }
1152
1153 for ($i = 1; $i < $count; ++$i) {
1154 $line = trim($body[$i]);
1155 if (($mime_header || $rfc822_header) &&
1156 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
1157 $bnd = $reg[1];
1158 $bndreg = $bnd;
1159 $bndreg = str_replace("\\", "\\\\", $bndreg);
1160 $bndreg = str_replace("?", "\\?", $bndreg);
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 } else if ($rfc822_header && $line == '') {
1168 $rfc822_header = false;
1169 if ($msg->type0 == 'multipart') {
1170 $mime_header = true;
1171 }
1172 }
1173
1174 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
1175 $cnt=count($boundaries)-1;
1176 $bnd = $boundaries[$cnt]['bnd'];
1177 $bndreg = $boundaries[$cnt]['bndreg'];
1178
1179 $regstr = '/^--'."($bndreg)".".*".'/';
1180 if (preg_match($regstr, $line, $reg)) {
1181 $bndlen = strlen($reg[1]);
1182 $bndend = false;
1183 if (strlen($line) > ($bndlen + 3)) {
1184 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
1185 $bndend = true;
1186 }
1187 }
1188 if ($bndend) {
1189 /* calc offset and return $msg */
1190 //$entStr = CalcEntity("$entStr", -1);
1191 array_pop($boundaries);
1192 $mime_header = true;
1193 $bnd_end = true;
1194 } else {
1195 $mime_header = true;
1196 $bnd_end = false;
1197 //$entStr = CalcEntity("$entStr", 0);
1198 ++$content_indx;
1199 }
1200 } else {
1201 if ($header) { }
1202 }
1203 }
1204 }
1205 }
1206
1207 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
1208 $found = false;
1209 if ($this->type0 == 'multipart') {
1210 if($this->type1 == 'alternative') {
1211 $msg = $this->findAlternativeEntity($alt_order);
1212 if (count($msg->entities) == 0) {
1213 $entity[] = $msg->entity_id;
1214 } else {
1215 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1216 }
1217 $found = true;
1218 } else if ($this->type1 == 'related') { /* RFC 2387 */
1219 $msgs = $this->findRelatedEntity();
1220 foreach ($msgs as $msg) {
1221 if (count($msg->entities) == 0) {
1222 $entity[] = $msg->entity_id;
1223 } else {
1224 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1225 }
1226 }
1227 if (count($msgs) > 0) {
1228 $found = true;
1229 }
1230 } else { /* Treat as multipart/mixed */
1231 foreach ($this->entities as $ent) {
1232 if(strtolower($ent->header->disposition->name) != 'attachment' &&
1233 ($ent->type0 != 'message' && $ent->type1 != 'rfc822'))
1234 {
1235 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1236 $found = true;
1237 }
1238 }
1239 }
1240 } else { /* If not multipart, then just compare with each entry from $alt_order */
1241 $type = $this->type0.'/'.$this->type1;
1242 foreach ($alt_order as $alt) {
1243 if( ($alt == $type) && isset($this->entity_id) ) {
1244 if ( (count($this->entities) == 0) &&
1245 (strtolower($this->header->disposition->name) != 'attachment') )
1246 {
1247 $entity[] = $this->entity_id;
1248 $found = true;
1249 }
1250 }
1251 }
1252 }
1253 if(!$found) {
1254 foreach ($this->entities as $ent) {
1255 if((strtolower($ent->header->disposition->name) != 'attachment') &&
1256 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
1257 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1258 $found = true;
1259 }
1260 }
1261 }
1262 if(!$strict && !$found) {
1263 if ($this->type0 == 'text' &&
1264 ($this->type1 == 'plain' ||
1265 $this->type1 == 'html' ||
1266 $this->type1 == 'message') &&
1267 isset($this->entity_id) )
1268 {
1269 if (count($this->entities) == 0) {
1270 if (strtolower($this->header->disposition->name) != 'attachment') {
1271 $entity[] = $this->entity_id;
1272 }
1273 }
1274 }
1275 }
1276
1277 return $entity;
1278 }
1279
1280 function findAlternativeEntity($alt_order) {
1281 /* If we are dealing with alternative parts then we */
1282 /* choose the best viewable message supported by SM. */
1283 $best_view = 0;
1284 $entity = array();
1285 $altcount = count($alt_order);
1286
1287 foreach($this->entities as $ent) {
1288 $type = $ent->header->type0 . '/' . $ent->header->type1;
1289 if ($type == 'multipart/related') {
1290 $type = $ent->header->getParameter('type');
1291 }
1292 for ($j = $best_view; $j < $altcount; ++$j) {
1293 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1294 $best_view = $j;
1295 $entity = $ent;
1296 }
1297 }
1298 }
1299
1300 return $entity;
1301 }
1302
1303 function findRelatedEntity() {
1304 $msgs = array();
1305 $entcount = count($this->entities);
1306
1307 for ($i = 0; $i < $entcount; ++$i) {
1308 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
1309 if ($this->header->getParameter('type') == $type) {
1310 $msgs[] = $this->entities[$i];
1311 }
1312 }
1313
1314 return $msgs;
1315 }
1316
1317 function getAttachments($exclude_id=array(), $result = array()) {
1318 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
1319 $this = $this->entities[0];
1320 }
1321
1322 if (count($this->entities)) {
1323 foreach ($this->entities as $entity) {
1324 $exclude = false;
1325
1326 foreach ($exclude_id as $excl) {
1327 if ($entity->entity_id === $excl) {
1328 $exclude = true;
1329 }
1330 }
1331
1332 if (!$exclude) {
1333 if (($entity->type0 == 'multipart') &&
1334 ($entity->type1 != 'related')) {
1335 $result = $entity->getAttachments($exclude_id, $result);
1336 } else if ($entity->type0 != 'multipart') {
1337 $result[] = $entity;
1338 }
1339 }
1340 }
1341 } else {
1342 $exclude = false;
1343 foreach ($exclude_id as $excl) {
1344 $exclude = $exclude || ($this->entity_id == $excl);
1345 }
1346
1347 if (!$exclude) {
1348 $result[] = $this;
1349 }
1350 }
1351
1352 return $result;
1353 }
1354 }
1355
1356 class smime_message {
1357
1358 }
1359
1360 class disposition {
1361 function disposition($name) {
1362 $this->name = $name;
1363 $this->properties = array();
1364 }
1365
1366 function getProperty($par) {
1367 $value = strtolower($par);
1368 if (isset($this->properties[$par])) {
1369 return $this->properties[$par];
1370 }
1371 return '';
1372 }
1373 }
1374
1375 class language {
1376 function language($name) {
1377 $this->name = $name;
1378 $this->properties = array();
1379 }
1380 }
1381
1382 class content_type {
1383 var $type0 = 'text',
1384 $type1 = 'plain',
1385 $properties = '';
1386
1387 function content_type($type) {
1388 $pos = strpos($type, '/');
1389 if ($pos > 0) {
1390 $this->type0 = substr($type, 0, $pos);
1391 $this->type1 = substr($type, $pos+1);
1392 } else {
1393 $this->type0 = $type;
1394 }
1395 $this->properties = array();
1396 }
1397 }
1398
1399 ?>