f18b90266c64c56c7d54a003446de606e882c6c1
[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
558 if (is_object($this->header->disposition)) {
559 $filename = $this->header->disposition->getproperty('filename');
560 if (!$filename) {
561 $filename = $this->header->disposition->getproperty('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
841 $arr = $arg_a[2];
842 if (is_array($arr)) {
843 $hdr->parameters = $arg_a[2];
844 }
845 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
846 $hdr->description = $arg_a[4];
847 $hdr->encoding = strtolower($arg_a[5]);
848 $hdr->entity_id = $msg->entity_id;
849 $hdr->size = $arg_a[6];
850 if ($shifted_args) {
851 $hdr->lines = $arg_a[7];
852 $s = 1;
853 } else {
854 $s = 0;
855 }
856 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
857 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
858 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
859 $msg->header = $hdr;
860 // $arg_no = 0;
861 ++$i;
862 if ((substr($msg->entity_id, -2) == '.0') && ($msg->type0 !='multipart')) {
863 $msg->entity_id++;
864 }
865 } else {
866 $hdr->type0 = 'multipart';
867 $hdr->type1 = $arg_a[0];
868 $msg->type0 = 'multipart';
869 $msg->type1 = $arg_a[0];
870 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
871 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
872 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
873 $msg->header = $hdr;
874 }
875 return (array($msg, $i));
876 default: break;
877 } /* switch */
878
879 } /* for */
880 } /* parsestructure */
881
882 function parseProperties($read, $i) {
883 $properties = array();
884 $prop_name = '';
885
886 for (; $read{$i} != ')'; ++$i) {
887 $arg_s = '';
888 if ($read{$i} == '"') {
889 $res = $this->parseQuote($read, $i);
890 $arg_s = $res[0];
891 $i = $res[1];
892 } else if ($read{$i} == '{') {
893 $res = $this->parseLiteral($read, $i);
894 $arg_s = $res[0];
895 $i = $res[1];
896 }
897
898 if ($arg_s != '') {
899 if ($prop_name == '') {
900 $prop_name = strtolower($arg_s);
901 $properties[$prop_name] = '';
902 } else if ($prop_name != '') {
903 $properties[$prop_name] = $arg_s;
904 $prop_name = '';
905 }
906 }
907 }
908
909 return array($properties, $i);
910 }
911
912 function parseEnvelope($read, $i, $hdr) {
913 $arg_no = 0;
914 $arg_a = array();
915 $cnt = strlen($read);
916
917 for (; ($i < $cnt) && ($read{$i} != ')'); ++$i) {
918 ++$i;
919 $char = strtoupper($read{$i});
920 switch ($char) {
921 case '"':
922 $res = $this->parseQuote($read, $i);
923 $arg_a[] = $res[0];
924 $i = $res[1];
925 ++$arg_no;
926 break;
927 case '{':
928 $res = $this->parseLiteral($read, $i);
929 $arg_a[] = $res[0];
930 $i = $res[1];
931 ++$arg_no;
932 break;
933 case 'N':
934 /* probably NIL argument */
935 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
936 $arg_a[] = '';
937 ++$arg_no;
938 $i += 2;
939 }
940 break;
941 case '(':
942 /* Address structure (with group support)
943 * Note: Group support is useless on SMTP connections
944 * because the protocol doesn't support it
945 */
946 $addr_a = array();
947 $group = '';
948 $a=0;
949 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
950 if ($read{$i} == '(') {
951 $res = $this->parseAddress($read, $i);
952 $addr = $res[0];
953 $i = $res[1];
954 if (($addr->host == '') && ($addr->mailbox != '')) {
955 /* start of group */
956 $group = $addr->mailbox;
957 $group_addr = $addr;
958 $j = $a;
959 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
960 /* end group */
961 if ($a == ($j+1)) { /* no group members */
962 $group_addr->group = $group;
963 $group_addr->mailbox = '';
964 $group_addr->personal = "$group: Undisclosed recipients;";
965 $addr_a[] = $group_addr;
966 $group ='';
967 }
968 } else {
969 $addr->group = $group;
970 $addr_a[] = $addr;
971 }
972 ++$a;
973 }
974 }
975 $arg_a[] = $addr_a;
976 break;
977 default: break;
978 }
979 }
980
981 if (count($arg_a) > 9) {
982 /* argument 1: date */
983 $d = strtr($arg_a[0], array(' ' => ' '));
984 $d = explode(' ', $d);
985 $hdr->date = getTimeStamp($d);
986
987 /* argument 2: subject */
988 $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]);
989 $hdr->subject = $arg_a[1];
990
991 $hdr->from = $arg_a[2][0]; /* argument 3: from */
992 $hdr->sender = $arg_a[3][0]; /* argument 4: sender */
993 $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */
994 $hdr->to = $arg_a[5]; /* argument 6: to */
995 $hdr->cc = $arg_a[6]; /* argument 7: cc */
996 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
997 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
998 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
999 }
1000 return (array($hdr, $i));
1001 }
1002
1003 function parseLiteral($read, $i) {
1004 $lit_cnt = '';
1005 for (++$i; $read{$i} != '}'; ++$i) {
1006 $lit_cnt .= $read{$i};
1007 }
1008
1009 $lit_cnt +=2; /* add the { and } characters */
1010 $s = '';
1011 for ($j = 0; $j < $lit_cnt; ++$j) {
1012 $s .= $read{++$i};
1013 }
1014 return (array($s, $i));
1015 }
1016
1017 function parseQuote($read, $i) {
1018 $s = '';
1019 for (++$i; $read{$i} != '"'; ++$i) {
1020 if ($read{$i} == '\\') {
1021 ++$i;
1022 }
1023 $s .= $read{$i};
1024 }
1025 return (array($s, $i));
1026 }
1027
1028 function parseAddress($read, $i) {
1029 $arg_a = array();
1030
1031 for (; $read{$i} != ')'; ++$i) {
1032 $char = strtoupper($read{$i});
1033 switch ($char) {
1034 case '"':
1035 case '{':
1036 $res = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1037 $arg_a[] = $res[0];
1038 $i = $res[1];
1039 break;
1040 case 'n':
1041 case 'N':
1042 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1043 $arg_a[] = '';
1044 $i += 2;
1045 }
1046 break;
1047 default: break;
1048 }
1049 }
1050
1051 if (count($arg_a) == 4) {
1052 $adr = new address_structure();
1053 $adr->personal = $arg_a[0];
1054 $adr->adl = $arg_a[1];
1055 $adr->mailbox = $arg_a[2];
1056 $adr->host = $arg_a[3];
1057 } else {
1058 $adr = '';
1059 }
1060 return (array($adr, $i));
1061 }
1062
1063 function parseDisposition($read, $i) {
1064 $arg_a = array();
1065
1066 for (; $read{$i} != ')'; ++$i) {
1067 switch ($read{$i}) {
1068 case '"':
1069 case '{':
1070 case '(':
1071 switch ($read{$i}) {
1072 case '"': $res = $this->parseQuote($read, $i); break;
1073 case '{': $res = $this->parseLiteral($read, $i); break;
1074 case '(': $res = $this->parseProperties($read, $i); break;
1075 }
1076 $arg_a[] = $res[0];
1077 $i = $res[1];
1078 break;
1079 default: break;
1080 }
1081 }
1082
1083 if (isset($arg_a[0])) {
1084 $disp = new disposition($arg_a[0]);
1085 if (isset($arg_a[1])) {
1086 $disp->properties = $arg_a[1];
1087 }
1088 }
1089
1090 return (is_object($disp) ? array($disp, $i) : array('', $i));
1091 }
1092
1093 function parseLanguage($read, $i) {
1094 /* no idea how to process this one without examples */
1095 $arg_a = array();
1096
1097 for (; $read{$i} != ')'; ++$i) {
1098 switch ($read{$i}) {
1099 case '"':
1100 case '{':
1101 case '(':
1102 switch ($read{$i}) {
1103 case '"': $res = $this->parseQuote($read, $i); break;
1104 case '{': $res = $this->parseLiteral($read, $i); break;
1105 case '(': $res = $this->parseProperties($read, $i); break;
1106 }
1107 $arg_a[] = $res[0];
1108 $i = $res[1];
1109 break;
1110 default: break;
1111 }
1112 }
1113
1114 if (isset($arg_a[0])) {
1115 $lang = new language($arg_a[0]);
1116 if (isset($arg_a[1])) {
1117 $lang->properties = $arg_a[1];
1118 }
1119 }
1120
1121 return (is_object($lang) ? array($lang, $i) : array('', $i));
1122 }
1123
1124 function parseParenthesis($read, $i) {
1125 for (; $read{$i} != ')'; ++$i) {
1126 switch ($read{$i}) {
1127 case '"':
1128 case '{':
1129 case '(':
1130 switch ($read{$i}) {
1131 case '"': $res = $this->parseQuote($read, $i); break;
1132 case '{': $res = $this->parseLiteral($read, $i); break;
1133 case '(': $res = $this->parseProperties($read, $i); break;
1134 }
1135 $i = $res[1];
1136 break;
1137 default: break;
1138 }
1139 }
1140 return $i;
1141 }
1142
1143 /* Function to fill the message structure in case the */
1144 /* bodystructure is not available NOT FINISHED YET */
1145 function parseMessage($read, $type0, $type1) {
1146 switch ($type0) {
1147 case 'message':
1148 $rfc822_header = true;
1149 $mime_header = false;
1150 break;
1151 case 'multipart':
1152 $rfc822_header = false;
1153 $mime_header = true;
1154 break;
1155 default: return $read;
1156 }
1157
1158 for ($i = 1; $i < $count; ++$i) {
1159 $line = trim($body[$i]);
1160 if (($mime_header || $rfc822_header) &&
1161 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
1162 $bnd = $reg[1];
1163 $bndreg = $bnd;
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 $bndreg = str_replace("-", "\\-", $bndreg);
1170 $bndreg = str_replace("(", "\\(", $bndreg);
1171 $bndreg = str_replace(")", "\\)", $bndreg);
1172 } else if ($rfc822_header && $line == '') {
1173 $rfc822_header = false;
1174 if ($msg->type0 == 'multipart') {
1175 $mime_header = true;
1176 }
1177 }
1178
1179 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
1180 $cnt=count($boundaries)-1;
1181 $bnd = $boundaries[$cnt]['bnd'];
1182 $bndreg = $boundaries[$cnt]['bndreg'];
1183
1184 $regstr = '/^--'."($bndreg)".".*".'/';
1185 if (preg_match($regstr, $line, $reg)) {
1186 $bndlen = strlen($reg[1]);
1187 $bndend = false;
1188 if (strlen($line) > ($bndlen + 3)) {
1189 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
1190 $bndend = true;
1191 }
1192 }
1193 if ($bndend) {
1194 /* calc offset and return $msg */
1195 //$entStr = CalcEntity("$entStr", -1);
1196 array_pop($boundaries);
1197 $mime_header = true;
1198 $bnd_end = true;
1199 } else {
1200 $mime_header = true;
1201 $bnd_end = false;
1202 //$entStr = CalcEntity("$entStr", 0);
1203 ++$content_indx;
1204 }
1205 } else {
1206 if ($header) { }
1207 }
1208 }
1209 }
1210 }
1211
1212 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
1213 $found = false;
1214 if ($this->type0 == 'multipart') {
1215 if($this->type1 == 'alternative') {
1216 $msg = $this->findAlternativeEntity($alt_order);
1217 if (count($msg->entities) == 0) {
1218 $entity[] = $msg->entity_id;
1219 } else {
1220 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1221 }
1222 $found = true;
1223 } else if ($this->type1 == 'related') { /* RFC 2387 */
1224 $msgs = $this->findRelatedEntity();
1225 foreach ($msgs as $msg) {
1226 if (count($msg->entities) == 0) {
1227 $entity[] = $msg->entity_id;
1228 } else {
1229 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1230 }
1231 }
1232 if (count($msgs) > 0) {
1233 $found = true;
1234 }
1235 } else { /* Treat as multipart/mixed */
1236 foreach ($this->entities as $ent) {
1237 if(strtolower($ent->header->disposition->name) != 'attachment' &&
1238 ($ent->type0 != 'message' && $ent->type1 != 'rfc822'))
1239 {
1240 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1241 $found = true;
1242 }
1243 }
1244 }
1245 } else { /* If not multipart, then just compare with each entry from $alt_order */
1246 $type = $this->type0.'/'.$this->type1;
1247 foreach ($alt_order as $alt) {
1248 if( ($alt == $type) && isset($this->entity_id) ) {
1249 if ( (count($this->entities) == 0) &&
1250 (strtolower($this->header->disposition->name) != 'attachment') )
1251 {
1252 $entity[] = $this->entity_id;
1253 $found = true;
1254 }
1255 }
1256 }
1257 }
1258 if(!$found) {
1259 foreach ($this->entities as $ent) {
1260 if((strtolower($ent->header->disposition->name) != 'attachment') &&
1261 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
1262 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1263 $found = true;
1264 }
1265 }
1266 }
1267 if(!$strict && !$found) {
1268 if ($this->type0 == 'text' &&
1269 ($this->type1 == 'plain' ||
1270 $this->type1 == 'html' ||
1271 $this->type1 == 'message') &&
1272 isset($this->entity_id) )
1273 {
1274 if (count($this->entities) == 0) {
1275 if (strtolower($this->header->disposition->name) != 'attachment') {
1276 $entity[] = $this->entity_id;
1277 }
1278 }
1279 }
1280 }
1281
1282 return $entity;
1283 }
1284
1285 function findAlternativeEntity($alt_order) {
1286 /* If we are dealing with alternative parts then we */
1287 /* choose the best viewable message supported by SM. */
1288 $best_view = 0;
1289 $entity = array();
1290 $altcount = count($alt_order);
1291
1292 foreach($this->entities as $ent) {
1293 $type = $ent->header->type0 . '/' . $ent->header->type1;
1294 if ($type == 'multipart/related') {
1295 $type = $ent->header->getParameter('type');
1296 }
1297 for ($j = $best_view; $j < $altcount; ++$j) {
1298 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1299 $best_view = $j;
1300 $entity = $ent;
1301 }
1302 }
1303 }
1304
1305 return $entity;
1306 }
1307
1308 function findRelatedEntity() {
1309 $msgs = array();
1310 $entcount = count($this->entities);
1311
1312 for ($i = 0; $i < $entcount; ++$i) {
1313 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
1314 if ($this->header->getParameter('type') == $type) {
1315 $msgs[] = $this->entities[$i];
1316 }
1317 }
1318
1319 return $msgs;
1320 }
1321
1322 function getAttachments($exclude_id=array(), $result = array()) {
1323 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
1324 $this = $this->entities[0];
1325 }
1326
1327 if (count($this->entities)) {
1328 foreach ($this->entities as $entity) {
1329 $exclude = false;
1330
1331 foreach ($exclude_id as $excl) {
1332 if ($entity->entity_id === $excl) {
1333 $exclude = true;
1334 }
1335 }
1336
1337 if (!$exclude) {
1338 if (($entity->type0 == 'multipart') &&
1339 ($entity->type1 != 'related')) {
1340 $result = $entity->getAttachments($exclude_id, $result);
1341 } else if ($entity->type0 != 'multipart') {
1342 $result[] = $entity;
1343 }
1344 }
1345 }
1346 } else {
1347 $exclude = false;
1348 foreach ($exclude_id as $excl) {
1349 $exclude = $exclude || ($this->entity_id == $excl);
1350 }
1351
1352 if (!$exclude) {
1353 $result[] = $this;
1354 }
1355 }
1356
1357 return $result;
1358 }
1359 }
1360
1361 class smime_message {
1362
1363 }
1364
1365 class disposition {
1366 function disposition($name) {
1367 $this->name = $name;
1368 $this->properties = array();
1369 }
1370
1371 function getProperty($par) {
1372 $value = strtolower($par);
1373 if (isset($this->properties[$par])) {
1374 return $this->properties[$par];
1375 }
1376 return '';
1377 }
1378 }
1379
1380 class language {
1381 function language($name) {
1382 $this->name = $name;
1383 $this->properties = array();
1384 }
1385 }
1386
1387 class content_type {
1388 var $type0 = 'text',
1389 $type1 = 'plain',
1390 $properties = '';
1391
1392 function content_type($type) {
1393 $pos = strpos($type, '/');
1394 if ($pos > 0) {
1395 $this->type0 = substr($type, 0, $pos);
1396 $this->type1 = substr($type, $pos+1);
1397 } else {
1398 $this->type0 = $type;
1399 }
1400 $this->properties = array();
1401 }
1402 }
1403
1404 ?>