8d1def3d81cb4a7afa55a7ceda5150600840f5bc
[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 *
10 * This contains functions needed to handle mime messages.
11 *
12 * $Id$
13 */
14
15
16
17 /*
18 * rdc822_header class
19 * input: header_string or array
20 */
21 class rfc822_header
22 {
23 var $date = '',
24 $subject = '',
25 $from = array(),
26 $sender = '',
27 $reply_to = array(),
28 $to = array(),
29 $cc = array(),
30 $bcc = array(),
31 $in_reply_to = '',
32 $message_id = '',
33 $mime = false,
34 $content_type = '',
35 $disposition = '',
36 $xmailer = '',
37 $priority = 3,
38 $dnt = '',
39 $mlist = array(),
40 $more_headers = array(); /* only needed for constructing headers
41 in smtp.php */
42
43 function parseHeader($hdr)
44 {
45 if (is_array($hdr))
46 {
47 $hdr = implode('',$hdr);
48 }
49 /* first we unfold the header */
50 $hdr = trim(str_replace(array("\r\n\t","\r\n "),array('',''),$hdr));
51 /*
52 * now we can make a new header array with each element representing
53 * a headerline
54 */
55 $hdr = explode("\r\n" , $hdr);
56 foreach ($hdr as $line)
57 {
58 $pos = strpos($line,':');
59 if ($pos > 0)
60 {
61 $field = substr($line,0,$pos);
62 $value = trim(substr($line,$pos+1));
63 if(!preg_match('/^X.*/i',$field)) {
64 $value = $this->stripComments($value);
65 }
66 $this->parseField($field,$value);
67 }
68 }
69 if ($this->content_type == '')
70 {
71 $this->parseContentType('text/plain; charset=us-ascii');
72 }
73 }
74
75 function stripComments($value)
76 {
77 $cnt = strlen($value);
78 $s = '';
79 $i = 0;
80 while ($i < $cnt)
81 {
82 switch ($value{$i})
83 {
84 case ('"'):
85 $s .= '"';
86 $i++;
87 while ($value{$i} != '"')
88 {
89 if ($value{$i} == '\\')
90 {
91 $s .= '\\';
92 $i++;
93 }
94 $s .= $value{$i};
95 $i++;
96 if ($i > $cnt) break;
97 }
98 $s .= $value{$i};
99 break;
100 case ('('):
101 while ($value{$i} != ')')
102 {
103 if ($value{$i} == '\\')
104 {
105 $i++;
106 }
107 $i++;
108 }
109 break;
110 default:
111 $s .= $value{$i};
112 break;
113 }
114 $i++;
115 }
116 return $s;
117 }
118
119 function parseField($field,$value)
120 {
121 $field = strtolower($field);
122 switch($field)
123 {
124 case ('date'):
125 $d = strtr($value, array(' ' => ' '));
126 $d = explode(' ', $d);
127 $this->date = getTimeStamp($d);
128 break;
129 case ('subject'):
130 $this->subject = $value;
131 break;
132 case ('from'):
133 $this->from = $this->parseAddress($value,true);
134 break;
135 case ('sender'):
136 $this->sender = $this->parseAddress($value);
137 break;
138 case ('reply-to'):
139 $this->reply_to = $this->parseAddress($value, true);
140 break;
141 case ('to'):
142 $this->to = $this->parseAddress($value, true);
143 break;
144 case ('cc'):
145 $this->cc = $this->parseAddress($value, true);
146 break;
147 case ('bcc'):
148 $this->bcc = $this->parseAddress($value, true);
149 break;
150 case ('in-reply-to'):
151 $this->in_reply_to = $value;
152 break;
153 case ('message_id'):
154 $this->message_id = $value;
155 break;
156 case ('disposition-notification-to'):
157 $this->dnt = $this->parseAddress($value);
158 break;
159 case ('mime-Version'):
160 $value = str_replace(' ','',$value);
161 if ($value == '1.0')
162 {
163 $this->mime = true;
164 }
165 break;
166 case ('content-type'):
167 $this->parseContentType($value);
168 break;
169 case ('content-disposition'):
170 $this->parseDisposition($value);
171 break;
172 case ('x-mailer'):
173 $this->xmailer = $value;
174 break;
175 case ('x-priority'):
176 $this->priority = $value;
177 break;
178 case ('list-post'):
179 $this->mlist('post',$value);
180 break;
181 case ('list-reply'):
182 $this->mlist('reply',$value);
183 break;
184 case ('list-subscribe'):
185 $this->mlist('subscribe',$value);
186 break;
187 case ('list-unsubscribe'):
188 $this->mlist('unsubscribe',$value);
189 break;
190 case ('list-archive'):
191 $this->mlist('archive',$value);
192 break;
193 case ('list-owner'):
194 $this->mlist('owner',$value);
195 break;
196 case ('list-help'):
197 $this->mlist('help',$value);
198 break;
199 case ('list-id'):
200 $this->mlist('id',$value);
201 break;
202 default:
203 break;
204 }
205 }
206
207 function parseAddress($address, $ar=false, $addr_ar = array(), $group = '')
208 {
209 $pos = 0;
210 $j = strlen( $address );
211 $name = '';
212 $addr = '';
213 while ( $pos < $j ) {
214 switch ($address{$pos})
215 {
216 case ('"'): /* get the personal name */
217 $pos++;
218 if ($address{$pos} == '"')
219 {
220 $pos++;
221 } else
222 {
223 while ( $pos < $j && $address{$pos} != '"')
224 {
225 if (substr($address, $pos, 2) == '\\"')
226 {
227 $name .= $address{$pos};
228 $pos++;
229 } elseif (substr($address, $pos, 2) == '\\\\')
230 {
231 $name .= $address{$pos};
232 $pos++;
233 }
234 $name .= $address{$pos};
235 $pos++;
236 }
237 }
238 $pos++;
239 break;
240 case ('<'): /* get email address */
241 $addr_start=$pos;
242 $pos++;
243 while ( $pos < $j && $address{$pos} != '>' )
244 {
245 $addr .= $address{$pos};
246 $pos++;
247 }
248 $pos++;
249 break;
250 case ('('): /* rip off comments */
251 $addr_start=$pos;
252 $pos++;
253 while ( $pos < $j && $address{$pos} != ')' )
254 {
255 $addr .= $address{$pos};
256 $pos++;
257 }
258 $address_start = substr($address,0,$addr_start);
259 $address_end = substr($address,$pos+1);
260 $address = $address_start . $address_end;
261 $j = strlen( $address );
262 $pos = $addr_start;
263 $pos++;
264 break;
265 case (','): /* we reached a delimiter */
266 if ($addr == '')
267 {
268 $addr = substr($address,0,$pos);
269 } elseif ($name == '') {
270 $name = trim(substr($address,0,$addr_start));
271 }
272
273 $at = strpos($addr, '@');
274 $addr_structure = new address_structure();
275 $addr_structure->personal = $name;
276 $addr_structure->group = $group;
277 if ($at)
278 {
279 $addr_structure->mailbox = substr($addr,0,$at);
280 $addr_structure->host = substr($addr,$at+1);
281 } else
282 {
283 $addr_structure->mailbox = $addr;
284 }
285 $address = trim(substr($address,$pos+1));
286 $j = strlen( $address );
287 $pos = 0;
288 $name = '';
289 $addr = '';
290 $addr_ar[] = $addr_structure;
291 break;
292 case (':'): /* process the group addresses */
293 /* group marker */
294 $group = substr($address,0,$pos);
295 $address = substr($address,$pos+1);
296 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
297 $addr_ar = $result[0];
298 $pos = $result[1];
299 $address = substr($address,$pos);
300 $j = strlen( $address );
301 $group = '';
302 $pos++;
303 break;
304 case (';'):
305 if ($group)
306 {
307 $address = substr($address, 0, $pos-1);
308 }
309 $pos++;
310 break;
311 default:
312 $pos++;
313 break;
314 }
315
316 }
317 if ($addr == '')
318 {
319 $addr = substr($address,0,$pos);
320 } elseif ($name == '')
321 {
322 $name = trim(substr($address,0,$addr_start));
323 }
324 $at = strpos($addr, '@');
325 $addr_structure = new address_structure();
326 $addr_structure->group = $group;
327 if ($at)
328 {
329 $addr_structure->mailbox = trim(substr($addr,0,$at));
330 $addr_structure->host = trim(substr($addr,$at+1));
331 } else
332 {
333 $addr_structure->mailbox = trim($addr);
334 }
335 if ($group && $addr == '') /* no addresses found in group */
336 {
337 $name = "$group: Undisclosed recipients;";
338 $addr_structure->personal = $name;
339 $addr_ar[] = $addr_structure;
340 return (array($addr_ar,$pos+1));
341 } else
342 {
343 $addr_structure->personal = $name;
344 if ($name || $addr)
345 {
346 $addr_ar[] = $addr_structure;
347 }
348 }
349 if ($ar)
350 {
351 return ($addr_ar);
352 } else
353 {
354 return ($addr_ar[0]);
355 }
356 }
357
358 function parseContentType($value)
359 {
360 $pos = strpos($value,';');
361 $props = '';
362 if ($pos > 0)
363 {
364 $type = trim(substr($value,0,$pos));
365 $props = trim(substr($type,$pos+1));
366 } else
367 {
368 $type = $value;
369 }
370 $content_type = new content_type($type);
371 if ($props)
372 {
373 $properties = $this->parseProperties($props);
374 if (!isset($properties['charset']))
375 {
376 $properties['charset'] = 'us-ascii';
377 }
378 $content_type->properties = $this->parseProperties($props);
379 }
380 $this->content_type = $content_type;
381 }
382
383 function parseProperties($value)
384 {
385 $propArray = explode(';',$value);
386 $propResultArray = array();
387 foreach ($propArray as $prop)
388 {
389 $prop = trim($prop);
390 $pos = strpos($prop,'=');
391 if ($pos>0)
392 {
393 $key = trim(substr($prop,0,$pos));
394 $val = trim(substr($prop,$pos+1));
395 if ($val{0} == '"')
396 {
397 $val = substr($val,1,-1);
398 }
399 $propResultArray[$key] = $val;
400 }
401 }
402 return $propResultArray;
403 }
404
405 function parseDisposition($value)
406 {
407 $pos = strpos($value,';');
408 $props = '';
409 if ($pos > 0)
410 {
411 $name = trim(substr($value,0,$pos));
412 $props = trim(substr($type,$pos+1));
413 } else
414 {
415 $name = $value;
416 }
417 $props_a = $this->parseProperties($props);
418 $disp = new disposition($name);
419 $disp->properties = $props_a;
420 $this->disposition = $disp;
421 }
422
423 function mlist($field, $value)
424 {
425 $res_a = array();
426 $value_a = explode(',',$value);
427 foreach ($value_a as $val) {
428 $val = trim($val);
429 if ($val{0} == '<')
430 {
431 $val = substr($val,1,-1);
432 }
433 if (substr($val,0,7) == 'mailto:')
434 {
435 $res_a['mailto'] = substr($val,7);
436 } else
437 {
438 $res_a['href'] = $val;
439 }
440 }
441 $this->mlist[$field] = $res_a;
442 }
443
444 /*
445 * function to get the addres strings out of the header.
446 * Arguments: string or array of strings !
447 * example1: header->getAddr_s('to').
448 * example2: header->getAddr_s(array('to','cc','bcc'))
449 */
450 function getAddr_s($arr, $separator=', ')
451 {
452 if (is_array($arr))
453 {
454 $s = '';
455 foreach($arr as $arg )
456 {
457 $result = $this->getAddr_s($arg);
458 if ($result)
459 {
460 $s .= $separator . $result;
461 }
462 }
463 if ($s) $s = substr($s,2);
464 return $s;
465 } else
466 {
467 $s = '';
468 eval('$addr = $this->'.$arr.';') ;
469 if (is_array($addr))
470 {
471 foreach ($addr as $addr_o)
472 {
473 if (is_object($addr_o))
474 {
475 $s .= $addr_o->getAddress() . $separator;
476 }
477 }
478 $s = substr($s,0,-strlen($separator));
479 } else
480 {
481 if (is_object($addr))
482 {
483 $s .= $addr->getAddress();
484 }
485 }
486 return $s;
487 }
488 }
489
490 function getAddr_a($arg, $excl_arr=array(), $arr = array())
491 {
492 if (is_array($arg))
493 {
494 foreach($arg as $argument )
495 {
496 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
497 }
498 return $arr;
499 } else
500 {
501 eval('$addr = $this->'.$arg.';') ;
502 if (is_array($addr))
503 {
504 foreach ($addr as $addr_o)
505 {
506 if (is_object($addr_o))
507 {
508 if (isset($addr_o->host) && $addr_o->host !='')
509 {
510 $email = $addr_o->mailbox.'@'.$addr_o->host;
511 } else
512 {
513 $email = $addr_o->mailbox;
514 }
515 $email = strtolower($email);
516 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email]))
517 {
518 $arr[$email] = $addr_o->personal;
519 }
520 }
521 }
522 } else
523 {
524 if (is_object($addr))
525 {
526 if (isset($addr->host))
527 {
528 $email = $addr->mailbox.'@'.$addr->host;
529 } else
530 {
531 $email = $addr->mailbox;
532 }
533 $email = strtolower($email);
534 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email]))
535 {
536 $arr[$email] = $addr->personal;
537 }
538 }
539 }
540 return $arr;
541 }
542 }
543
544 function getContentType($type0, $type1)
545 {
546 $type0 = $this->content_type->type0;
547 $type1 = $this->content_type->type1;
548 return $this->content_type->properties;
549 }
550 }
551
552 class msg_header
553 {
554 /** msg_header contains all variables available in a bodystructure **/
555 /** entity like described in rfc2060 **/
556
557 var $type0 = '',
558 $type1 = '',
559 $parameters = array(),
560 $id = 0,
561 $description = '',
562 $encoding='',
563 $size = 0,
564 $md5='',
565 $disposition = '',
566 $language='';
567
568 /*
569 * returns addres_list of supplied argument
570 * arguments: array('to', 'from', ...) or just a string like 'to'.
571 * result: string: address1, addres2, ....
572 */
573
574 function setVar($var, $value)
575 {
576 $this->{$var} = $value;
577 }
578
579 function getParameter($par)
580 {
581 $value = strtolower($par);
582 if (isset($this->parameters[$par]))
583 {
584 return $this->parameters[$par];
585 }
586 return '';
587 }
588
589 function setParameter($parameter, $value)
590 {
591 $this->parameters[strtolower($parameter)] = $value;
592 }
593 }
594
595
596
597 class address_structure
598 {
599 var $personal = '', $adl = '', $mailbox = '', $host = '', $group = '';
600
601 function getAddress($full=true)
602 {
603 if (is_object($this))
604 {
605 if (isset($this->host) && $this->host !='')
606 {
607 $email = $this->mailbox.'@'.$this->host;
608 } else
609 {
610 $email = $this->mailbox;
611 }
612 if (trim($this->personal) !='')
613 {
614 if ($email)
615 {
616 $addr = '"' . $this->personal . '" <' .$email.'>';
617 } else
618 {
619 $addr = $this->personal;
620 }
621 $best_dpl = $this->personal;
622 } else
623 {
624 $addr = $email;
625 $best_dpl = $email;
626 }
627 if ($full)
628 {
629 return $addr;
630 } else
631 {
632 return $best_dpl;
633 }
634 } else return '';
635 }
636 }
637
638 class message
639 {
640 /** message is the object that contains messages. It is a recursive
641 object in that through the $entities variable, it can contain
642 more objects of type message. See documentation in mime.txt for
643 a better description of how this works.
644 **/
645 var $rfc822_header = '',
646 $mime_header = '',
647 $flags = '',
648 $type0='',
649 $type1='',
650 $entities = array(),
651 $parent_ent, $entity,
652 $parent = '', $decoded_body='',
653 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
654 $is_mdnsent = 0,
655 $body_part = '',
656 $offset = 0, /* for fetching body parts out of raw messages */
657 $length = 0; /* for fetching body parts out of raw messages */
658
659 function setEnt($ent)
660 {
661 $this->entity_id= $ent;
662 }
663
664 function addEntity ($msg)
665 {
666 $msg->parent = &$this;
667 $this->entities[] = $msg;
668 }
669
670 function addRFC822Header($read)
671 {
672 $header = new rfc822_header();
673 $this->rfc822_header = $header->parseHeader($read);
674 }
675
676 function getEntity($ent)
677 {
678 $cur_ent = $this->entity_id;
679 $msg = $this;
680 if ($cur_ent == '' || $cur_ent == '0')
681 {
682 $cur_ent_a = array();
683 } else
684 {
685 $cur_ent_a = explode('.',$this->entity_id);
686 }
687 $ent_a = explode('.',$ent);
688
689 $cnt = count($ent_a);
690
691 for ($i=0;$i<$cnt -1;$i++)
692 {
693 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i])
694 {
695 $msg = $msg->parent;
696 $cur_ent_a = explode('.',$msg->entity_id);
697 $i--;
698 } else if (!isset($cur_ent_a[$i]))
699 {
700 if (isset($msg->entities[($ent_a[$i]-1)]))
701 {
702 $msg = $msg->entities[($ent_a[$i]-1)];
703 } else {
704 $msg = $msg->entities[0];
705 }
706 }
707 if ($msg->type0 == 'message' && $msg->type1 == 'rfc822')
708 {
709 /*this is a header for a message/rfc822 entity */
710 $msg = $msg->entities[0];
711 }
712 }
713
714 if ($msg->type0 == 'message' && $msg->type1 == 'rfc822')
715 {
716 /*this is a header for a message/rfc822 entity */
717 $msg = $msg->entities[0];
718 }
719
720 if (isset($msg->entities[($ent_a[$cnt-1])-1]))
721 {
722 if (is_object($msg->entities[($ent_a[$cnt-1])-1]))
723 {
724 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
725 }
726 }
727
728 return $msg;
729 }
730
731 function setBody($s)
732 {
733 $this->body_part = $s;
734 }
735
736 function clean_up()
737 {
738 $msg = $this;
739 $msg->body_part = '';
740 $i=0;
741 while ( isset($msg->entities[$i]))
742 {
743 $msg->entities[$i]->clean_up();
744 $i++;
745 }
746 }
747
748 function getMailbox()
749 {
750 $msg = $this;
751 while (is_object($msg->parent))
752 {
753 $msg = $msg->parent;
754 }
755 return $msg->mailbox;
756 }
757
758 function calcEntity($msg)
759 {
760 if ($this->type0 == 'message' && $this->type1 == 'rfc822')
761 {
762 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
763 } else if (isset($this->entity_id) && $this->entity_id !='')
764 {
765 $ent_no = count($this->entities)+1;
766 $par_ent = substr($this->entity_id,-2);
767 if ($par_ent{0} == '.')
768 {
769 $par_ent = $par_ent{1};
770 }
771 if ($par_ent == '0')
772 {
773 $ent_no = count($this->entities)+1;
774 if ($ent_no > 0)
775 {
776 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
777 if ($ent)
778 {
779 $ent = $ent . ".$ent_no";
780 } else
781 {
782 $ent = $ent_no;
783 }
784 $msg->entity_id = $ent;
785 } else
786 {
787 $msg->entity_id = $ent_no;
788 }
789 } else
790 {
791 $ent = $this->entity_id . ".$ent_no";
792 $msg->entity_id = $ent;
793 }
794 } else
795 {
796 $msg->entity_id = '0';
797 }
798 return $msg->entity_id;
799 }
800
801
802 /*
803 * Bodystructure parser, a recursive function for generating the
804 * entity-tree with all the mime-parts.
805 *
806 * It follows RFC2060 and stores all the described fields in the
807 * message object.
808 *
809 * Question/Bugs:
810 *
811 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
812 *
813 */
814 function parseStructure($read, $i=0)
815 {
816 $arg_no = 0;
817 $arg_a = array();
818 $cnt = strlen($read);
819 while ($i < $cnt)
820 {
821 $char = strtoupper($read{$i});
822 switch ($char)
823 {
824 case '(':
825 if ($arg_no == 0 )
826 {
827 if (!isset($msg))
828 {
829 $msg = new message();
830 $hdr = new msg_header();
831 $hdr->type0 = 'text';
832 $hdr->type1 = 'plain';
833 $hdr->encoding = 'us-ascii';
834 $msg->entity_id = $this->calcEntity($msg);
835 } else
836 {
837 $msg->header->type0 = 'multipart';
838 $msg->type0 = 'multipart';
839 while ($read{$i} == '(')
840 {
841 $res = $msg->parseStructure($read,$i);
842 $i = $res[1];
843 $msg->addEntity($res[0]);
844 }
845 }
846 } else
847 {
848 switch ($arg_no)
849 {
850 case 1:
851 /* multipart properties */
852 $i++;
853 $res = $this->parseProperties($read,$i);
854
855 $arg_a[] = $res[0];
856 $i = $res[1];
857 $arg_no++;
858 break;
859 case 2:
860 if (isset($msg->type0) && $msg->type0 == 'multipart')
861 {
862 $i++;
863 $res = $msg->parseDisposition($read,$i);
864 $arg_a[] = $res[0];
865 $i = $res[1];
866 } else /* properties */
867 {
868 $res = $msg->parseProperties($read,$i);
869 $arg_a[] = $res[0];
870 $i = $res[1];
871 }
872 $arg_no++;
873 break;
874 case 3:
875 if (isset($msg->type0) && $msg->type0 == 'multipart')
876 {
877 $i++;
878 $res= $msg->parseLanguage($read,$i);
879 $arg_a[] = $res[0];
880 $i = $res[1];
881 }
882 case 7:
883 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')
884 {
885 $msg->header->type0 = $arg_a[0];
886 $msg->type0 = $arg_a[0];
887 $msg->header->type1 = $arg_a[1];
888 $msg->type1 = $arg_a[1];
889 $rfc822_hdr = new rfc822_header();
890 $res = $msg->parseEnvelope($read,$i,$rfc822_hdr);
891 $i = $res[1];
892 $msg->rfc822_header = $res[0];
893 $i++;
894 while ($i < $cnt && $read{$i} != '(')
895 {
896 $i++;
897 }
898 $res = $msg->parseStructure($read,$i);
899 $i = $res[1];
900 $msg->addEntity($res[0]);
901 }
902 break;
903 case 8:
904 $i++;
905 $res = $msg->parseDisposition($read,$i);
906 $arg_a[] = $res[0];
907 $i = $res[1];
908 $arg_no++;
909 break;
910 case 9:
911 if ($arg_a[0] == 'text' ||
912 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
913 {
914 $i++;
915 $res = $msg->parseDisposition($read,$i);
916 $arg_a[] = $res[0];
917 $i = $res[1];
918 } else
919 {
920 $i++;
921 $res = $msg->parseLanguage($read,$i);
922 $arg_a[] = $res[0];
923 $i = $res[1];
924 }
925 $arg_no++;
926 break;
927 case 10:
928 if ($arg_a[0] == 'text' ||
929 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
930 {
931 $i++;
932 $res = $msg->parseLanguage($read,$i);
933 $arg_a[] = $res[0];
934 $i = $res[1];
935 } else
936 {
937 $i = $msg->parseParenthesis($read,$i);
938 $arg_a[] = ''; /* not yet desribed in rfc2060 */
939 }
940 $arg_no++;
941 break;
942 default:
943 /* unknown argument, skip this part */
944 $i = $msg->parseParenthesis($read,$i);
945 $arg_a[] = '';
946 $arg_no++;
947 break;
948 } /* switch */
949 }
950 break;
951 case '"':
952 /* inside an entity -> start processing */
953 $debug = substr($read,$i,20);
954 $res = $msg->parseQuote($read,$i);
955 $arg_s = $res[0];
956 $i = $res[1];
957 $arg_no++;
958 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
959 $arg_a[] = $arg_s;
960 break;
961 case 'n':
962 case 'N':
963 /* probably NIL argument */
964 if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
965 strtoupper(substr($read,$i,4)) == 'NIL)')
966 {
967 $arg_a[] = '';
968 $arg_no++;
969 $i = $i+2;
970 }
971 break;
972 case '{':
973 /* process the literal value */
974 $res = $msg->parseLiteral($read,$i);
975 $arg_s = $res[0];
976 $i = $res[1];
977 $arg_no++;
978 break;
979 case (is_numeric($read{$i}) ):
980 /* process integers */
981 if ($read{$i} == ' ') break;
982 $arg_s = $read{$i};;
983 $i++;
984 while (preg_match('/^[0-9]{1}$/',$read{$i}))
985 {
986 $arg_s .= $read{$i};
987 $i++;
988 }
989 $arg_no++;
990 $arg_a[] = $arg_s;
991 break;
992 case ')':
993 if (isset($msg->type0) && $msg->type0 == 'multipart')
994 {
995 $multipart = true;
996 } else
997 {
998 $multipart = false;
999 }
1000 if (!$multipart)
1001 {
1002 if ($arg_a[0] == 'text' ||
1003 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
1004 {
1005 $shifted_args = true;
1006 } else
1007 {
1008 $shifted_args = false;
1009 }
1010 $hdr->type0 = $arg_a[0];
1011 $hdr->type1 = $arg_a[1];
1012
1013 $msg->type0 = $arg_a[0];
1014 $msg->type1 = $arg_a[1];
1015
1016 $arr = $arg_a[2];
1017 if (is_array($arr))
1018 {
1019 $hdr->parameters = $arg_a[2];
1020 }
1021 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
1022 $hdr->description = $arg_a[4];
1023 $hdr->encoding = strtolower($arg_a[5]);
1024 $hdr->entity_id = $msg->entity_id;
1025 $hdr->size = $arg_a[6];
1026 if ($shifted_args)
1027 {
1028 $hdr->lines = $arg_a[7];
1029 if (isset($arg_a[8]))
1030 {
1031 $hdr->md5 = $arg_a[8];
1032 }
1033 if (isset($arg_a[9]))
1034 {
1035 $hdr->disposition = $arg_a[9];
1036 }
1037 if (isset($arg_a[10]))
1038 {
1039 $hdr->language = $arg_a[10];
1040 }
1041 } else
1042 {
1043 if (isset($arg_a[7]))
1044 {
1045 $hdr->md5 = $arg_a[7];
1046 }
1047 if (isset($arg_a[8]))
1048 {
1049 $hdr->disposition = $arg_a[8];
1050 }
1051 if (isset($arg_a[9]))
1052 {
1053 $hdr->language = $arg_a[9];
1054 }
1055 }
1056 $msg->header = $hdr;
1057 $arg_no = 0;
1058 $i++;
1059 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart')
1060 {
1061 $msg->entity_id++;
1062 }
1063 return (array($msg, $i));
1064 } else
1065 {
1066 $hdr->type0 = 'multipart';
1067 $hdr->type1 = $arg_a[0];
1068 $msg->type0 = 'multipart';
1069 $msg->type1 = $arg_a[0];
1070 if (is_array($arg_a[1]))
1071 {
1072 $hdr->parameters = $arg_a[1];
1073 }
1074 if (isset($arg_a[2]))
1075 {
1076 $hdr->disposition = $arg_a[2];
1077 }
1078 if (isset($arg_a[3]))
1079 {
1080 $hdr->language = $arg_a[3];
1081 }
1082 $msg->header = $hdr;
1083 return (array($msg, $i));
1084 }
1085 default:
1086 break;
1087 } /* switch */
1088 $i++;
1089 } /* while */
1090 } /* parsestructure */
1091
1092 function parseProperties($read, $i)
1093 {
1094 $properties = array();
1095 $arg_s = '';
1096 $prop_name = '';
1097 while ($read{$i} != ')')
1098 {
1099 if ($read{$i} == '"')
1100 {
1101 $res = $this->parseQuote($read,$i);
1102 $arg_s = $res[0];
1103 $i = $res[1];
1104 } else if ($read{$i} == '{')
1105 {
1106 $res = $this->parseLiteral($read,$i);
1107 $arg_s = $res[0];
1108 $i = $res[1];
1109 }
1110 if ($prop_name == '' && $arg_s)
1111 {
1112 $prop_name = strtolower($arg_s);
1113 $properties[$prop_name] = '';
1114 $arg_s = '';
1115 } elseif ($prop_name != '' && $arg_s != '')
1116 {
1117 $properties[$prop_name] = $arg_s;
1118 $prop_name = '';
1119 $arg_s = '';
1120 }
1121 $i++;
1122 }
1123 return (array($properties, $i));
1124 }
1125
1126 function parseEnvelope($read, $i, $hdr)
1127 {
1128 $arg_no = 0;
1129 $arg_a = array();
1130 $cnt = strlen($read);
1131 while ($i< $cnt && $read{$i} != ')')
1132 {
1133 $i++;
1134 $char = strtoupper($read{$i});
1135 switch ($char)
1136 {
1137 case '"':
1138 $res = $this->parseQuote($read,$i);
1139 $arg_a[] = $res[0];
1140 $i = $res[1];
1141 $arg_no++;
1142 break;
1143 case '{':
1144 $res = $this->parseLiteral($read,$i);
1145 $arg_a[] = $res[0];
1146 $i = $res[1];
1147 $arg_no++;
1148 break;
1149 case 'N':
1150 /* probably NIL argument */
1151 if (strtoupper(substr($read,$i,3)) == 'NIL') {
1152 $arg_a[] = '';
1153 $arg_no++;
1154 $i = $i+2;
1155 }
1156 break;
1157 case '(':
1158 /* Address structure
1159 * With group support.
1160 * Note: Group support is useless on SMTP connections
1161 * because the protocol doesn't support it
1162 */
1163 $addr_a = array();
1164 $group = '';
1165 $a=0;
1166 while ($i < $cnt && $read{$i} != ')')
1167 {
1168 if ($read{$i} == '(')
1169 {
1170 $res = $this->parseAddress($read,$i);
1171 $addr = $res[0];
1172 $i = $res[1];
1173 if ($addr->host == '' && $addr->mailbox != '')
1174 {
1175 /* start of group */
1176 $group = $addr->mailbox;
1177 $group_addr = $addr;
1178 $j = $a;
1179 } elseif ($group && $addr->host == '' && $addr->mailbox == '')
1180 {
1181 /* end group */
1182 if ($a == $j+1) /* no group members */
1183 {
1184 $group_addr->group = $group;
1185 $group_addr->mailbox = '';
1186 $group_addr->personal = "$group: Undisclosed recipients;";
1187 $addr_a[] = $group_addr;
1188 $group ='';
1189 }
1190 } else
1191 {
1192 $addr->group = $group;
1193 $addr_a[] = $addr;
1194 }
1195 $a++;
1196 }
1197 $i++;
1198 }
1199 $arg_a[] = $addr_a;
1200 break;
1201 default:
1202 break;
1203 }
1204 $i++;
1205 }
1206 if (count($arg_a) > 9)
1207 {
1208 /* argument 1: date */
1209 $d = strtr($arg_a[0], array(' ' => ' '));
1210 $d = explode(' ', $d);
1211 $hdr->date = getTimeStamp($d);
1212 /* argument 2: subject */
1213 if (!trim($arg_a[1]))
1214 {
1215 $arg_a[1]= _("(no subject)");
1216 }
1217 $hdr->subject = $arg_a[1];
1218 /* argument 3: from */
1219 $hdr->from = $arg_a[2][0];
1220 /* argument 4: sender */
1221 $hdr->sender = $arg_a[3][0];
1222 /* argument 5: reply-to */
1223 $hdr->replyto = $arg_a[4][0];
1224 /* argument 6: to */
1225 $hdr->to = $arg_a[5];
1226 /* argument 7: cc */
1227 $hdr->cc = $arg_a[6];
1228 /* argument 8: bcc */
1229 $hdr->bcc = $arg_a[7];
1230 /* argument 9: in-reply-to */
1231 $hdr->inreplyto = $arg_a[8];
1232 /* argument 10: message-id */
1233 $hdr->message_id = $arg_a[9];
1234 }
1235 return (array($hdr,$i));
1236 }
1237
1238 function parseLiteral($read, $i)
1239 {
1240 $lit_cnt = '';
1241 $i++;
1242 while ($read{$i} != '}')
1243 {
1244 $lit_cnt .= $read{$i};
1245 $i++;
1246 }
1247 $lit_cnt +=2; /* add the { and } characters */
1248 $s = '';
1249 for ($j = 0; $j < $lit_cnt; $j++)
1250 {
1251 $i++;
1252 $s .= $read{$i};
1253 }
1254 return (array($s, $i));
1255 }
1256
1257 function parseQuote($read, $i)
1258 {
1259 $i++;
1260 $s = '';
1261 while ($read{$i} != '"')
1262 {
1263 if ($read{$i} == '\\')
1264 {
1265 $i++;
1266 }
1267 $s .= $read{$i};
1268 $i++;
1269 }
1270 return (array($s, $i));
1271 }
1272
1273 function parseAddress($read, $i)
1274 {
1275 $arg_a = array();
1276 while ($read{$i} != ')' )
1277 {
1278 $char = strtoupper($read{$i});
1279 switch ($char)
1280 {
1281 case '"':
1282 $res = $this->parseQuote($read,$i);
1283 $arg_a[] = $res[0];
1284 $i = $res[1];
1285 break;
1286 case '{':
1287 $res = $this->parseLiteral($read,$i);
1288 $arg_a[] = $res[0];
1289 $i = $res[1];
1290 break;
1291 case 'n':
1292 case 'N':
1293 if (strtoupper(substr($read,$i,3)) == 'NIL') {
1294 $arg_a[] = '';
1295 $i = $i+2;
1296 }
1297 break;
1298 default:
1299 break;
1300 }
1301 $i++;
1302 }
1303 if (count($arg_a) == 4)
1304 {
1305 $adr = new address_structure();
1306 $adr->personal = $arg_a[0];
1307 $adr->adl = $arg_a[1];
1308 $adr->mailbox = $arg_a[2];
1309 $adr->host = $arg_a[3];
1310 } else
1311 {
1312 $adr = '';
1313 }
1314 return (array($adr,$i));
1315 }
1316
1317 function parseDisposition($read,$i)
1318 {
1319 $arg_a = array();
1320 while ($read{$i} != ')')
1321 {
1322 switch ($read{$i})
1323 {
1324 case '"':
1325 $res = $this->parseQuote($read,$i);
1326 $arg_a[] = $res[0];
1327 $i = $res[1];
1328 break;
1329 case '{':
1330 $res = $this->parseLiteral($read,$i);
1331 $arg_a[] = $res[0];
1332 $i = $res[1];
1333 break;
1334 case '(':
1335 $res = $this->parseProperties($read,$i);
1336 $arg_a[] = $res[0];
1337 $i = $res[1];
1338 break;
1339 default:
1340 break;
1341 }
1342 $i++;
1343 }
1344 if (isset($arg_a[0]))
1345 {
1346 $disp = new disposition($arg_a[0]);
1347 if (isset($arg_a[1]))
1348 {
1349 $disp->properties = $arg_a[1];
1350 }
1351 }
1352 if (is_object($disp))
1353 {
1354 return (array($disp, $i));
1355 } else
1356 {
1357 return (array('',$i));
1358 }
1359 }
1360
1361 function parseLanguage($read,$i)
1362 {
1363 /* no idea how to process this one without examples */
1364 $arg_a = array();
1365 while ($read{$i} != ')')
1366 {
1367 switch ($read{$i})
1368 {
1369 case '"':
1370 $res = $this->parseQuote($read,$i);
1371 $arg_a[] = $res[0];
1372 $i = $res[1];
1373 break;
1374 case '{':
1375 $res = $this->parseLiteral($read,$i);
1376 $arg_a[] = $res[0];
1377 $i = $res[1];
1378 break;
1379 case '(':
1380 $res = $this->parseProperties($read,$i);
1381 $arg_a[] = $res[0];
1382 $i = $res[1];
1383 break;
1384 default:
1385 break;
1386 }
1387 $i++;
1388 }
1389 if (isset($arg_a[0]))
1390 {
1391 $lang = new language($arg_a[0]);
1392 if (isset($arg_a[1]))
1393 {
1394 $lang->properties = $arg_a[1];
1395 }
1396 }
1397 if (is_object($lang))
1398 {
1399 return (array($lang, $i));
1400 } else
1401 {
1402 return (array('', $i));
1403 }
1404 }
1405
1406 function parseParenthesis($read,$i)
1407 {
1408 while ($read{$i} != ')')
1409 {
1410 switch ($read{$i})
1411 {
1412 case '"':
1413 $res = $this->parseQuote($read,$i);
1414 $i = $res[1];
1415 break;
1416 case '{':
1417 $res = $this->parseLiteral($read,$i);
1418 $i = $res[1];
1419 break;
1420 case '(':
1421 $res = $this->parseParenthesis($read,$i);
1422 $i = $res[1];
1423 break;
1424 default:
1425 break;
1426 }
1427 $i++;
1428 }
1429 return $i;
1430 }
1431
1432 /* function to fill the message structure in case the bodystructure
1433 isn't available NOT FINISHED YET
1434 */
1435 function parseMessage($read, $type0, $type1)
1436 {
1437 switch ($type0)
1438 {
1439 case 'message':
1440 $rfc822_header = true;
1441 $mime_header = false;
1442 break;
1443 case 'multipart':
1444 $mime_header = true;
1445 $rfc822_header = false;
1446 break;
1447 default:
1448 return $read;
1449 }
1450
1451 for ($i=1; $i < $count; $i++)
1452 {
1453 $line = trim($body[$i]);
1454 if ( ( $mime_header || $rfc822_header) &&
1455 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) )
1456 {
1457 $bnd = $reg[1];
1458 $bndreg = $bnd;
1459 $bndreg = str_replace("\\","\\\\",$bndreg);
1460 $bndreg = str_replace("?","\\?",$bndreg);
1461 $bndreg = str_replace("+","\\+",$bndreg);
1462 $bndreg = str_replace(".","\\.",$bndreg);
1463 $bndreg = str_replace("/","\\/",$bndreg);
1464 $bndreg = str_replace("-","\\-",$bndreg);
1465 $bndreg = str_replace("(","\\(",$bndreg);
1466 $bndreg = str_replace(")","\\)",$bndreg);
1467 } elseif ( $rfc822_header && $line == '' )
1468 {
1469 $rfc822_header = false;
1470 if ($msg->type0 == 'multipart')
1471 {
1472 $mime_header = true;
1473 }
1474 }
1475
1476 if (($line{0} == '-' || $rfc822_header) && isset($boundaries[0]))
1477 {
1478 $cnt=count($boundaries)-1;
1479 $bnd = $boundaries[$cnt]['bnd'];
1480 $bndreg = $boundaries[$cnt]['bndreg'];
1481
1482 $regstr = '/^--'."($bndreg)".".*".'/';
1483 if (preg_match($regstr,$line,$reg) )
1484 {
1485 $bndlen = strlen($reg[1]);
1486 $bndend = false;
1487 if (strlen($line) > ($bndlen + 3))
1488 {
1489 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
1490 $bndend = true;
1491 }
1492 if ($bndend)
1493 {
1494 /* calc offset and return $msg */
1495 // $entStr = CalcEntity("$entStr",-1);
1496 array_pop($boundaries);
1497 $mime_header = true;
1498 $bnd_end = true;
1499 } else
1500 {
1501 $mime_header = true;
1502 $bnd_end = false;
1503 // $entStr = CalcEntity("$entStr",0);
1504 $content_indx++;
1505 }
1506 } else
1507 {
1508 if ($header)
1509 {
1510 }
1511 }
1512 }
1513 }
1514 }
1515
1516 function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html'))
1517 {
1518 $found = false;
1519 $type = $this->type0.'/'.$this->type1;
1520 if ( $type == 'multipart/alternative')
1521 {
1522 $msg = $this->findAlternativeEntity($alt_order);
1523 if (count($msg->entities) == 0)
1524 {
1525 $entity[] = $msg->entity_id;
1526 } else
1527 {
1528 $entity = $msg->findDisplayEntity($entity, $alt_order);
1529 }
1530 $found = true;
1531 } else if ( $type == 'multipart/related')
1532 {
1533 $msgs = $this->findRelatedEntity();
1534 foreach ($msgs as $msg)
1535 {
1536 if (count($msg->entities) == 0)
1537 {
1538 $entity[] = $msg->entity_id;
1539 } else
1540 {
1541 $entity = $msg->findDisplayEntity($entity,$alt_order);
1542 }
1543 }
1544 if (count($msgs) > 0) {
1545 $found = true;
1546 }
1547 } else if ($this->type0 == 'text' &&
1548 ($this->type1 == 'plain' ||
1549 $this->type1 == 'html' ||
1550 $this->type1 == 'message') &&
1551 isset($this->entity_id) )
1552 {
1553 if (count($this->entities) == 0)
1554 {
1555 if (strtolower($this->header->disposition->name) != 'attachment')
1556 {
1557 $entity[] = $this->entity_id;
1558 }
1559 }
1560 }
1561 $i = 0;
1562 if(!$found) {
1563 foreach ($this->entities as $ent) {
1564 if(strtolower($ent->header->disposition->name) != 'attachment' &&
1565 ($ent->type0 != 'message' && $ent->type1 != 'rfc822'))
1566 {
1567 $entity = $ent->findDisplayEntity($entity, $alt_order);
1568 }
1569 }
1570 }
1571 /*
1572 while ( isset($this->entities[$i]) && !$found &&
1573 (strtolower($this->entities[$i]->header->disposition->name)
1574 != 'attachment') &&
1575 ($this->entities[$i]->type0 != 'message' &&
1576 $this->entities[$i]->type1 != 'rfc822' )
1577 )
1578 {
1579 $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order);
1580 $i++;
1581 }
1582 */
1583 return( $entity );
1584 }
1585
1586 function findAlternativeEntity ($alt_order)
1587 {
1588 /* if we are dealing with alternative parts then we choose the best
1589 * viewable message supported by SM.
1590 */
1591 $best_view = 0;
1592 $entity = array();
1593 $altcount = count($alt_order);
1594 foreach($this->entities as $ent)
1595 {
1596 $type = $ent->header->type0.'/'.$ent->header->type1;
1597 if ($type == 'multipart/related')
1598 {
1599 $type = $ent->header->getParameter('type');
1600 }
1601 for ($j = $best_view; $j < $altcount; $j++)
1602 {
1603 if ($alt_order[$j] == $type && $j >= $best_view)
1604 {
1605 $best_view = $j;
1606 $entity = $ent;
1607 }
1608 }
1609 }
1610 return $entity;
1611 }
1612
1613 function findRelatedEntity ()
1614 {
1615 $msgs = array();
1616 $entcount = count($this->entities);
1617 for ($i = 0; $i < $entcount; $i++)
1618 {
1619 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
1620 if ($this->header->getParameter('type') == $type)
1621 {
1622 $msgs[] = $this->entities[$i];
1623 }
1624 }
1625 return $msgs;
1626 }
1627
1628 function getAttachments($exclude_id=array(), $result = array())
1629 {
1630 if ($this->type0 == 'message' && $this->type1 == 'rfc822')
1631 {
1632 $this = $this->entities[0];
1633 }
1634 if (count($this->entities))
1635 {
1636 foreach ($this->entities as $entity)
1637 {
1638 $exclude = false;
1639 foreach ($exclude_id as $excl)
1640 {
1641 if ($entity->entity_id === $excl)
1642 {
1643 $exclude = true;
1644 }
1645 }
1646 if (!$exclude)
1647 {
1648 if ($entity->type0 == 'multipart' &&
1649 $entity->type1 != 'related')
1650 {
1651 $result = $entity->getAttachments($exclude_id, $result);
1652 } else if ($entity->type0 != 'multipart')
1653 {
1654 $result[] = $entity;
1655 }
1656 }
1657 }
1658 } else
1659 {
1660 $exclude = false;
1661 foreach ($exclude_id as $excl)
1662 {
1663 if ($this->entity_id == $excl)
1664 {
1665 $exclude = true;
1666 }
1667 }
1668 if (!$exclude)
1669 {
1670 $result[] = $this;
1671 }
1672 }
1673 return $result;
1674 }
1675 }
1676
1677 class smime_message
1678 {
1679 }
1680
1681 class disposition
1682 {
1683 function disposition($name)
1684 {
1685 $this->name = $name;
1686 $this->properties = array();
1687 }
1688
1689 function getProperty($par)
1690 {
1691 $value = strtolower($par);
1692 if (isset($this->properties[$par]))
1693 {
1694 return $this->properties[$par];
1695 }
1696 return '';
1697 }
1698
1699 }
1700
1701 class language
1702 {
1703 function language($name)
1704 {
1705 $this->name = $name;
1706 $this->properties = array();
1707 }
1708 }
1709
1710 class content_type
1711 {
1712 var $type0='text',
1713 $type1='plain',
1714 $properties='';
1715 function content_type($type)
1716 {
1717 $pos = strpos($type,'/');
1718 if ($pos > 0)
1719 {
1720 $this->type0 = substr($type,0,$pos);
1721 $this->type1 = substr($type,$pos+1);
1722 } else
1723 {
1724 $this->type0 = $type;
1725 }
1726 $this->properties = array();
1727 }
1728 }
1729
1730 ?>