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