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