preg_match should look at field instead of value
[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 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
723 }
724
725 return $msg;
726 }
727
728 function setBody($s)
729 {
730 $this->body_part = $s;
731 }
732
733 function clean_up()
734 {
735 $msg = $this;
736 $msg->body_part = '';
737 $i=0;
738 while ( isset($msg->entities[$i]))
739 {
740 $msg->entities[$i]->clean_up();
741 $i++;
742 }
743 }
744
745 function getMailbox()
746 {
747 $msg = $this;
748 while (is_object($msg->parent))
749 {
750 $msg = $msg->parent;
751 }
752 return $msg->mailbox;
753 }
754
755 function calcEntity($msg)
756 {
757 if ($this->type0 == 'message' && $this->type1 == 'rfc822')
758 {
759 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
760 } else if (isset($this->entity_id) && $this->entity_id !='')
761 {
762 $ent_no = count($this->entities)+1;
763 $par_ent = substr($this->entity_id,-2);
764 if ($par_ent{0} == '.')
765 {
766 $par_ent = $par_ent{1};
767 }
768 if ($par_ent == '0')
769 {
770 $ent_no = count($this->entities)+1;
771 if ($ent_no > 0)
772 {
773 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
774 if ($ent)
775 {
776 $ent = $ent . ".$ent_no";
777 } else
778 {
779 $ent = $ent_no;
780 }
781 $msg->entity_id = $ent;
782 } else
783 {
784 $msg->entity_id = $ent_no;
785 }
786 } else
787 {
788 $ent = $this->entity_id . ".$ent_no";
789 $msg->entity_id = $ent;
790 }
791 } else
792 {
793 $msg->entity_id = '0';
794 }
795 return $msg->entity_id;
796 }
797
798
799 /*
800 * Bodystructure parser, a recursive function for generating the
801 * entity-tree with all the mime-parts.
802 *
803 * It follows RFC2060 and stores all the described fields in the
804 * message object.
805 *
806 * Question/Bugs:
807 *
808 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
809 *
810 */
811 function parseStructure($read, $i=0)
812 {
813 $arg_no = 0;
814 $arg_a = array();
815 $cnt = strlen($read);
816 while ($i < $cnt)
817 {
818 $char = strtoupper($read{$i});
819 switch ($char)
820 {
821 case '(':
822 if ($arg_no == 0 )
823 {
824 if (!isset($msg))
825 {
826 $msg = new message();
827 $hdr = new msg_header();
828 $hdr->type0 = 'text';
829 $hdr->type1 = 'plain';
830 $hdr->encoding = 'us-ascii';
831 $msg->entity_id = $this->calcEntity($msg);
832 } else
833 {
834 $msg->header->type0 = 'multipart';
835 $msg->type0 = 'multipart';
836 while ($read{$i} == '(')
837 {
838 $res = $msg->parseStructure($read,$i);
839 $i = $res[1];
840 $msg->addEntity($res[0]);
841 }
842 }
843 } else
844 {
845 switch ($arg_no)
846 {
847 case 1:
848 /* multipart properties */
849 $i++;
850 $res = $this->parseProperties($read,$i);
851 $arg_a[] = $res[0];
852 $i = $res[1];
853 $arg_no++;
854 break;
855 case 2:
856 if (isset($msg->type0) && $msg->type0 == 'multipart')
857 {
858 $i++;
859 $res = $msg->parseDisposition($read,$i);
860 $arg_a[] = $res[0];
861 $i = $res[1];
862 } else /* properties */
863 {
864 $res = $msg->parseProperties($read,$i);
865 $arg_a[] = $res[0];
866 $i = $res[1];
867 }
868 $arg_no++;
869 break;
870 case 3:
871 if (isset($msg->type0) && $msg->type0 == 'multipart')
872 {
873 $i++;
874 $res= $msg->parseLanguage($read,$i);
875 $arg_a[] = $res[0];
876 $i = $res[1];
877 }
878 case 7:
879 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')
880 {
881 $msg->header->type0 = $arg_a[0];
882 $msg->type0 = $arg_a[0];
883 $msg->header->type1 = $arg_a[1];
884 $msg->type1 = $arg_a[1];
885 $rfc822_hdr = new rfc822_header();
886 $res = $msg->parseEnvelope($read,$i,$rfc822_hdr);
887 $i = $res[1];
888 $msg->rfc822_header = $res[0];
889 $i++;
890 while ($i < $cnt && $read{$i} != '(')
891 {
892 $i++;
893 }
894 $res = $msg->parseStructure($read,$i);
895 $i = $res[1];
896 $msg->addEntity($res[0]);
897 }
898 break;
899 case 8:
900 $i++;
901 $res = $msg->parseDisposition($read,$i);
902 $arg_a[] = $res[0];
903 $i = $res[1];
904 $arg_no++;
905 break;
906 case 9:
907 if ($arg_a[0] == 'text' ||
908 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
909 {
910 $i++;
911 $res = $msg->parseDisposition($read,$i);
912 $arg_a[] = $res[0];
913 $i = $res[1];
914 } else
915 {
916 $i++;
917 $res = $msg->parseLanguage($read,$i);
918 $arg_a[] = $res[0];
919 $i = $res[1];
920 }
921 $arg_no++;
922 break;
923 case 10:
924 if ($arg_a[0] == 'text' ||
925 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
926 {
927 $i++;
928 $res = $msg->parseLanguage($read,$i);
929 $arg_a[] = $res[0];
930 $i = $res[1];
931 } else
932 {
933 $i = $msg->parseParenthesis($read,$i);
934 $arg_a[] = ''; /* not yet desribed in rfc2060 */
935 }
936 $arg_no++;
937 break;
938 default:
939 /* unknown argument, skip this part */
940 $i = $msg->parseParenthesis($read,$i);
941 $arg_a[] = '';
942 $arg_no++;
943 break;
944 } /* switch */
945 }
946 break;
947 case '"':
948 /* inside an entity -> start processing */
949 $debug = substr($read,$i,20);
950 $res = $msg->parseQuote($read,$i);
951 $arg_s = $res[0];
952 $i = $res[1];
953 $arg_no++;
954 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
955 $arg_a[] = $arg_s;
956 break;
957 case 'n':
958 case 'N':
959 /* probably NIL argument */
960 if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
961 strtoupper(substr($read,$i,4)) == 'NIL)')
962 {
963 $arg_a[] = '';
964 $arg_no++;
965 $i = $i+2;
966 }
967 break;
968 case '{':
969 /* process the literal value */
970 $res = $msg->parseLiteral($read,$i);
971 $arg_s = $res[0];
972 $i = $res[1];
973 $arg_no++;
974 break;
975 case (is_numeric($read{$i}) ):
976 /* process integers */
977 if ($read{$i} == ' ') break;
978 $arg_s = $read{$i};;
979 $i++;
980 while (preg_match('/^[0-9]{1}$/',$read{$i}))
981 {
982 $arg_s .= $read{$i};
983 $i++;
984 }
985 $arg_no++;
986 $arg_a[] = $arg_s;
987 break;
988 case ')':
989 if (isset($msg->type0) && $msg->type0 == 'multipart')
990 {
991 $multipart = true;
992 } else
993 {
994 $multipart = false;
995 }
996 if (!$multipart)
997 {
998 if ($arg_a[0] == 'text' ||
999 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
1000 {
1001 $shifted_args = true;
1002 } else
1003 {
1004 $shifted_args = false;
1005 }
1006 $hdr->type0 = $arg_a[0];
1007 $hdr->type1 = $arg_a[1];
1008
1009 $msg->type0 = $arg_a[0];
1010 $msg->type1 = $arg_a[1];
1011
1012 $arr = $arg_a[2];
1013 if (is_array($arr))
1014 {
1015 $hdr->parameters = $arg_a[2];
1016 }
1017 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
1018 $hdr->description = $arg_a[4];
1019 $hdr->encoding = strtolower($arg_a[5]);
1020 $hdr->entity_id = $msg->entity_id;
1021 $hdr->size = $arg_a[6];
1022 if ($shifted_args)
1023 {
1024 $hdr->lines = $arg_a[7];
1025 if (isset($arg_a[8]))
1026 {
1027 $hdr->md5 = $arg_a[8];
1028 }
1029 if (isset($arg_a[9]))
1030 {
1031 $hdr->disposition = $arg_a[9];
1032 }
1033 if (isset($arg_a[10]))
1034 {
1035 $hdr->language = $arg_a[10];
1036 }
1037 } else
1038 {
1039 if (isset($arg_a[7]))
1040 {
1041 $hdr->md5 = $arg_a[7];
1042 }
1043 if (isset($arg_a[8]))
1044 {
1045 $hdr->disposition = $arg_a[8];
1046 }
1047 if (isset($arg_a[9]))
1048 {
1049 $hdr->language = $arg_a[9];
1050 }
1051 }
1052 $msg->header = $hdr;
1053 $arg_no = 0;
1054 $i++;
1055 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart')
1056 {
1057 $msg->entity_id++;
1058 }
1059 return (array($msg, $i));
1060 } else
1061 {
1062 $hdr->type0 = 'multipart';
1063 $hdr->type1 = $arg_a[0];
1064 $msg->type0 = 'multipart';
1065 $msg->type1 = $arg_a[0];
1066 if (is_array($arg_a[1]))
1067 {
1068 $hdr->parameters = $arg_a[1];
1069 }
1070 if (isset($arg_a[2]))
1071 {
1072 $hdr->disposition = $arg_a[2];
1073 }
1074 if (isset($arg_a[3]))
1075 {
1076 $hdr->language = $arg_a[3];
1077 }
1078 $msg->header = $hdr;
1079 return (array($msg, $i));
1080 }
1081 default:
1082 break;
1083 } /* switch */
1084 $i++;
1085 } /* while */
1086 } /* parsestructure */
1087
1088 function parseProperties($read, $i)
1089 {
1090 $properties = array();
1091 $arg_s = '';
1092 $prop_name = '';
1093 while ($read{$i} != ')')
1094 {
1095 if ($read{$i} == '"')
1096 {
1097 $res = $this->parseQuote($read,$i);
1098 $arg_s = $res[0];
1099 $i = $res[1];
1100 } else if ($read{$i} == '{')
1101 {
1102 $res = $this->parseLiteral($read,$i);
1103 $arg_s = $res[0];
1104 $i = $res[1];
1105 }
1106 if ($prop_name == '' && $arg_s)
1107 {
1108 $prop_name = strtolower($arg_s);
1109 $properties[$prop_name] = '';
1110 $arg_s = '';
1111 } elseif ($prop_name != '' && $arg_s != '')
1112 {
1113 $properties[$prop_name] = $arg_s;
1114 $prop_name = '';
1115 $arg_s = '';
1116 }
1117 $i++;
1118 }
1119 return (array($properties, $i));
1120 }
1121
1122 function parseEnvelope($read, $i, $hdr)
1123 {
1124 $arg_no = 0;
1125 $arg_a = array();
1126 $cnt = strlen($read);
1127 while ($i< $cnt && $read{$i} != ')')
1128 {
1129 $i++;
1130 $char = strtoupper($read{$i});
1131 switch ($char)
1132 {
1133 case '"':
1134 $res = $this->parseQuote($read,$i);
1135 $arg_a[] = $res[0];
1136 $i = $res[1];
1137 $arg_no++;
1138 break;
1139 case '{':
1140 $res = $this->parseLiteral($read,$i);
1141 $arg_a[] = $res[0];
1142 $i = $res[1];
1143 $arg_no++;
1144 break;
1145 case 'N':
1146 /* probably NIL argument */
1147 if (strtoupper(substr($read,$i,3)) == 'NIL') {
1148 $arg_a[] = '';
1149 $arg_no++;
1150 $i = $i+2;
1151 }
1152 break;
1153 case '(':
1154 /* Address structure
1155 * With group support.
1156 * Note: Group support is useless on SMTP connections
1157 * because the protocol doesn't support it
1158 */
1159 $addr_a = array();
1160 $group = '';
1161 $a=0;
1162 while ($i < $cnt && $read{$i} != ')')
1163 {
1164 if ($read{$i} == '(')
1165 {
1166 $res = $this->parseAddress($read,$i);
1167 $addr = $res[0];
1168 $i = $res[1];
1169 if ($addr->host == '' && $addr->mailbox != '')
1170 {
1171 /* start of group */
1172 $group = $addr->mailbox;
1173 $group_addr = $addr;
1174 $j = $a;
1175 } elseif ($group && $addr->host == '' && $addr->mailbox == '')
1176 {
1177 /* end group */
1178 if ($a == $j+1) /* no group members */
1179 {
1180 $group_addr->group = $group;
1181 $group_addr->mailbox = '';
1182 $group_addr->personal = "$group: Undisclosed recipients;";
1183 $addr_a[] = $group_addr;
1184 $group ='';
1185 }
1186 } else
1187 {
1188 $addr->group = $group;
1189 $addr_a[] = $addr;
1190 }
1191 $a++;
1192 }
1193 $i++;
1194 }
1195 $arg_a[] = $addr_a;
1196 break;
1197 default:
1198 break;
1199 }
1200 $i++;
1201 }
1202 if (count($arg_a) > 9)
1203 {
1204 /* argument 1: date */
1205 $d = strtr($arg_a[0], array(' ' => ' '));
1206 $d = explode(' ', $d);
1207 $hdr->date = getTimeStamp($d);
1208 /* argument 2: subject */
1209 if (!trim($arg_a[1]))
1210 {
1211 $arg_a[1]= _("(no subject)");
1212 }
1213 $hdr->subject = $arg_a[1];
1214 /* argument 3: from */
1215 $hdr->from = $arg_a[2][0];
1216 /* argument 4: sender */
1217 $hdr->sender = $arg_a[3][0];
1218 /* argument 5: reply-to */
1219 $hdr->replyto = $arg_a[4][0];
1220 /* argument 6: to */
1221 $hdr->to = $arg_a[5];
1222 /* argument 7: cc */
1223 $hdr->cc = $arg_a[6];
1224 /* argument 8: bcc */
1225 $hdr->bcc = $arg_a[7];
1226 /* argument 9: in-reply-to */
1227 $hdr->inreplyto = $arg_a[8];
1228 /* argument 10: message-id */
1229 $hdr->message_id = $arg_a[9];
1230 }
1231 return (array($hdr,$i));
1232 }
1233
1234 function parseLiteral($read, $i)
1235 {
1236 $lit_cnt = '';
1237 $i++;
1238 while ($read{$i} != '}')
1239 {
1240 $lit_cnt .= $read{$i};
1241 $i++;
1242 }
1243 $lit_cnt +=2; /* add the { and } characters */
1244 $s = '';
1245 for ($j = 0; $j < $lit_cnt; $j++)
1246 {
1247 $i++;
1248 $s .= $read{$i};
1249 }
1250 return (array($s, $i));
1251 }
1252
1253 function parseQuote($read, $i)
1254 {
1255 $i++;
1256 $s = '';
1257 while ($read{$i} != '"')
1258 {
1259 if ($read{$i} == '\\')
1260 {
1261 $i++;
1262 }
1263 $s .= $read{$i};
1264 $i++;
1265 }
1266 return (array($s, $i));
1267 }
1268
1269 function parseAddress($read, $i)
1270 {
1271 $arg_a = array();
1272 while ($read{$i} != ')' )
1273 {
1274 $char = strtoupper($read{$i});
1275 switch ($char)
1276 {
1277 case '"':
1278 $res = $this->parseQuote($read,$i);
1279 $arg_a[] = $res[0];
1280 $i = $res[1];
1281 break;
1282 case '{':
1283 $res = $this->parseLiteral($read,$i);
1284 $arg_a[] = $res[0];
1285 $i = $res[1];
1286 break;
1287 case 'n':
1288 case 'N':
1289 if (strtoupper(substr($read,$i,3)) == 'NIL') {
1290 $arg_a[] = '';
1291 $i = $i+2;
1292 }
1293 break;
1294 default:
1295 break;
1296 }
1297 $i++;
1298 }
1299 if (count($arg_a) == 4)
1300 {
1301 $adr = new address_structure();
1302 $adr->personal = $arg_a[0];
1303 $adr->adl = $arg_a[1];
1304 $adr->mailbox = $arg_a[2];
1305 $adr->host = $arg_a[3];
1306 } else
1307 {
1308 $adr = '';
1309 }
1310 return (array($adr,$i));
1311 }
1312
1313 function parseDisposition($read,$i)
1314 {
1315 $arg_a = array();
1316 while ($read{$i} != ')')
1317 {
1318 switch ($read{$i})
1319 {
1320 case '"':
1321 $res = $this->parseQuote($read,$i);
1322 $arg_a[] = $res[0];
1323 $i = $res[1];
1324 break;
1325 case '{':
1326 $res = $this->parseLiteral($read,$i);
1327 $arg_a[] = $res[0];
1328 $i = $res[1];
1329 break;
1330 case '(':
1331 $res = $this->parseProperties($read,$i);
1332 $arg_a[] = $res[0];
1333 $i = $res[1];
1334 break;
1335 default:
1336 break;
1337 }
1338 $i++;
1339 }
1340 if (isset($arg_a[0]))
1341 {
1342 $disp = new disposition($arg_a[0]);
1343 if (isset($arg_a[1]))
1344 {
1345 $disp->properties = $arg_a[1];
1346 }
1347 }
1348 if (is_object($disp))
1349 {
1350 return (array($disp, $i));
1351 } else
1352 {
1353 return (array('',$i));
1354 }
1355 }
1356
1357 function parseLanguage($read,$i)
1358 {
1359 /* no idea how to process this one without examples */
1360 $arg_a = array();
1361 while ($read{$i} != ')')
1362 {
1363 switch ($read{$i})
1364 {
1365 case '"':
1366 $res = $this->parseQuote($read,$i);
1367 $arg_a[] = $res[0];
1368 $i = $res[1];
1369 break;
1370 case '{':
1371 $res = $this->parseLiteral($read,$i);
1372 $arg_a[] = $res[0];
1373 $i = $res[1];
1374 break;
1375 case '(':
1376 $res = $this->parseProperties($read,$i);
1377 $arg_a[] = $res[0];
1378 $i = $res[1];
1379 break;
1380 default:
1381 break;
1382 }
1383 $i++;
1384 }
1385 if (isset($arg_a[0]))
1386 {
1387 $lang = new language($arg_a[0]);
1388 if (isset($arg_a[1]))
1389 {
1390 $lang->properties = $arg_a[1];
1391 }
1392 }
1393 if (is_object($lang))
1394 {
1395 return (array($lang, $i));
1396 } else
1397 {
1398 return (array('', $i));
1399 }
1400 }
1401
1402 function parseParenthesis($read,$i)
1403 {
1404 while ($read{$i} != ')')
1405 {
1406 switch ($read{$i})
1407 {
1408 case '"':
1409 $res = $this->parseQuote($read,$i);
1410 $i = $res[1];
1411 break;
1412 case '{':
1413 $res = $this->parseLiteral($read,$i);
1414 $i = $res[1];
1415 break;
1416 case '(':
1417 $res = $this->parseParenthesis($read,$i);
1418 $i = $res[1];
1419 break;
1420 default:
1421 break;
1422 }
1423 $i++;
1424 }
1425 return $i;
1426 }
1427
1428 /* function to fill the message structure in case the bodystructure
1429 isn't available NOT FINISHED YET
1430 */
1431 function parseMessage($read, $type0, $type1)
1432 {
1433 switch ($type0)
1434 {
1435 case 'message':
1436 $rfc822_header = true;
1437 $mime_header = false;
1438 break;
1439 case 'multipart':
1440 $mime_header = true;
1441 $rfc822_header = false;
1442 break;
1443 default:
1444 return $read;
1445 }
1446
1447 for ($i=1; $i < $count; $i++)
1448 {
1449 $line = trim($body[$i]);
1450 if ( ( $mime_header || $rfc822_header) &&
1451 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) )
1452 {
1453 $bnd = $reg[1];
1454 $bndreg = $bnd;
1455 $bndreg = str_replace("\\","\\\\",$bndreg);
1456 $bndreg = str_replace("?","\\?",$bndreg);
1457 $bndreg = str_replace("+","\\+",$bndreg);
1458 $bndreg = str_replace(".","\\.",$bndreg);
1459 $bndreg = str_replace("/","\\/",$bndreg);
1460 $bndreg = str_replace("-","\\-",$bndreg);
1461 $bndreg = str_replace("(","\\(",$bndreg);
1462 $bndreg = str_replace(")","\\)",$bndreg);
1463 } elseif ( $rfc822_header && $line == '' )
1464 {
1465 $rfc822_header = false;
1466 if ($msg->type0 == 'multipart')
1467 {
1468 $mime_header = true;
1469 }
1470 }
1471
1472 if (($line{0} == '-' || $rfc822_header) && isset($boundaries[0]))
1473 {
1474 $cnt=count($boundaries)-1;
1475 $bnd = $boundaries[$cnt]['bnd'];
1476 $bndreg = $boundaries[$cnt]['bndreg'];
1477
1478 $regstr = '/^--'."($bndreg)".".*".'/';
1479 if (preg_match($regstr,$line,$reg) )
1480 {
1481 $bndlen = strlen($reg[1]);
1482 $bndend = false;
1483 if (strlen($line) > ($bndlen + 3))
1484 {
1485 if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
1486 $bndend = true;
1487 }
1488 if ($bndend)
1489 {
1490 /* calc offset and return $msg */
1491 // $entStr = CalcEntity("$entStr",-1);
1492 array_pop($boundaries);
1493 $mime_header = true;
1494 $bnd_end = true;
1495 } else
1496 {
1497 $mime_header = true;
1498 $bnd_end = false;
1499 // $entStr = CalcEntity("$entStr",0);
1500 $content_indx++;
1501 }
1502 } else
1503 {
1504 if ($header)
1505 {
1506 }
1507 }
1508 }
1509 }
1510 }
1511
1512 function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html'))
1513 {
1514 $found = false;
1515 $type = $this->type0.'/'.$this->type1;
1516 if ( $type == 'multipart/alternative')
1517 {
1518 $msg = $this->findAlternativeEntity($alt_order);
1519 if (count($msg->entities) == 0)
1520 {
1521 $entity[] = $msg->entity_id;
1522 } else
1523 {
1524 $entity = $msg->findDisplayEntity($entity, $alt_order);
1525 }
1526 $found = true;
1527 } else if ( $type == 'multipart/related')
1528 {
1529 $msgs = $this->findRelatedEntity();
1530 for ($i = 0; $i < count($msgs); $i++)
1531 {
1532 $msg = $msgs[$i];
1533 if (count($msg->entities) == 0)
1534 {
1535 $entity[] = $msg->entity_id;
1536 } else
1537 {
1538 $entity = $msg->findDisplayEntity($entity,$alt_order);
1539 }
1540 $found = true;
1541 }
1542 } else if ( $this->type0 == 'text' &&
1543 ( $this->type1 == 'plain' ||
1544 $this->type1 == 'html' ||
1545 $this->type1 == 'message') &&
1546 isset($this->entity_id) )
1547 {
1548 if (count($this->entities) == 0)
1549 {
1550 if (strtolower($this->header->disposition->name) != 'attachment')
1551 {
1552 $entity[] = $this->entity_id;
1553 }
1554 }
1555 }
1556 $i = 0;
1557 while ( isset($this->entities[$i]) && !$found &&
1558 (strtolower($this->entities[$i]->header->disposition->name)
1559 != 'attachment') &&
1560 ($this->entities[$i]->type0 != 'message' &&
1561 $this->entities[$i]->type1 != 'rfc822' )
1562 )
1563 {
1564 $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order);
1565 $i++;
1566 }
1567 return( $entity );
1568 }
1569
1570 function findAlternativeEntity ($alt_order)
1571 {
1572 /* if we are dealing with alternative parts then we choose the best
1573 * viewable message supported by SM.
1574 */
1575 $best_view = 0;
1576 $ent_id = 0;
1577 $k = 0;
1578 for ($i = 0; $i < count($this->entities); $i ++)
1579 {
1580 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
1581 if ($type == 'multipart/related')
1582 {
1583 $type = $this->entities[$i]->header->getParameter('type');
1584 }
1585 for ($j = $k; $j < count($alt_order); $j++)
1586 {
1587 if ($alt_order[$j] == $type && $j > $best_view)
1588 {
1589 $best_view = $j;
1590 $ent_id = $i;
1591 $k = $j;
1592 }
1593 }
1594 }
1595 return $this->entities[$ent_id];
1596 }
1597
1598 function findRelatedEntity ()
1599 {
1600 $msgs = array();
1601 for ($i = 0; $i < count($this->entities); $i ++)
1602 {
1603 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
1604 if ($this->header->getParameter('type') == $type)
1605 {
1606 $msgs[] = $this->entities[$i];
1607 }
1608 }
1609 return $msgs;
1610 }
1611
1612 function getAttachments($exclude_id=array(), $result = array())
1613 {
1614 if ($this->type0 == 'message' && $this->type1 == 'rfc822')
1615 {
1616 $this = $this->entities[0];
1617 }
1618 if (count($this->entities))
1619 {
1620 foreach ($this->entities as $entity)
1621 {
1622 $exclude = false;
1623 foreach ($exclude_id as $excl)
1624 {
1625 if ($entity->entity_id == $excl)
1626 {
1627 $exclude = true;
1628 }
1629 }
1630 if (!$exclude)
1631 {
1632 if ($entity->type0 == 'multipart' &&
1633 $entity->type1 != 'related')
1634 {
1635 $result = $entity->getAttachments($exclude_id, $result);
1636 } else if ($entity->type0 != 'multipart')
1637 {
1638 $result[] = $entity;
1639 }
1640 }
1641 }
1642 } else
1643 {
1644 $exclude = false;
1645 foreach ($exclude_id as $excl)
1646 {
1647 if ($this->entity_id == $excl)
1648 {
1649 $exclude = true;
1650 }
1651 }
1652 if (!$exclude)
1653 {
1654 $result[] = $this;
1655 }
1656 }
1657 return $result;
1658 }
1659 }
1660
1661 class smime_message
1662 {
1663 }
1664
1665 class disposition
1666 {
1667 function disposition($name)
1668 {
1669 $this->name = $name;
1670 $this->properties = array();
1671 }
1672 }
1673
1674 class language
1675 {
1676 function language($name)
1677 {
1678 $this->name = $name;
1679 $this->properties = array();
1680 }
1681 }
1682
1683 class content_type
1684 {
1685 var $type0='text',
1686 $type1='plain',
1687 $properties='';
1688 function content_type($type)
1689 {
1690 $pos = strpos($type,'/');
1691 if ($pos > 0)
1692 {
1693 $this->type0 = substr($type,0,$pos);
1694 $this->type1 = substr($type,$pos+1);
1695 } else
1696 {
1697 $this->type0 = $type;
1698 }
1699 $this->properties = array();
1700 }
1701 }
1702
1703 ?>