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