6 * Copyright (c) 2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions needed to handle mime messages.
16 * input: header_string or array
36 $more_headers = array(); /* only needed for constructing headers
38 function parseHeader($hdr) {
40 $hdr = implode('', $hdr);
43 /* First we unfold the header */
44 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
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, ':');
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);
58 $this->parseField($field, $value);
61 if ($this->content_type
== '') {
62 $this->parseContentType('text/plain; charset=us-ascii');
66 function stripComments($value) {
68 for ($i = 0, $cnt = strlen($value); $i < $cnt; $i++
) {
72 while ((++
$i < $cnt) && ($value{$i} != '"')) {
73 if ($value{$i} == '\\') {
77 $result .= $value{$i};
79 $result .= $value{$i};
83 while (($depth > 0) && (++
$i < $cnt)) {
100 $result .= $value{$i};
107 function parseField($field, $value) {
108 $field = strtolower($field);
111 $d = strtr($value, array(' ' => ' '));
112 $d = explode(' ', $d);
113 $this->date
= getTimeStamp($d);
116 $this->subject
= $value;
119 $this->from
= $this->parseAddress($value,true);
122 $this->sender
= $this->parseAddress($value);
125 $this->reply_to
= $this->parseAddress($value, true);
128 $this->to
= $this->parseAddress($value, true);
131 $this->cc
= $this->parseAddress($value, true);
134 $this->bcc
= $this->parseAddress($value, true);
137 $this->in_reply_to
= $value;
140 $this->message_id
= $value;
142 case 'disposition-notification-to':
143 $this->dnt
= $this->parseAddress($value);
146 $value = str_replace(' ', '', $value);
147 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
150 $this->parseContentType($value);
152 case 'content-disposition':
153 $this->parseDisposition($value);
157 $this->xmailer
= $value;
160 $this->priority
= $value;
163 $this->mlist('post', $value);
166 $this->mlist('reply', $value);
168 case 'list-subscribe':
169 $this->mlist('subscribe', $value);
171 case 'list-unsubscribe':
172 $this->mlist('unsubscribe', $value);
175 $this->mlist('archive', $value);
178 $this->mlist('owner', $value);
181 $this->mlist('help', $value);
184 $this->mlist('id', $value);
191 function parseAddress
192 ($address, $ar=false, $addr_ar = array(), $group = '') {
194 $j = strlen($address);
198 switch ($address{$pos}) {
199 case '"': /* get the personal name */
200 if ($address{++
$pos} == '"') {
203 while ($pos < $j && $address{$pos} != '"') {
204 if ((substr($address, $pos, 2) == '\\"') ||
205 (substr($address, $pos, 2) == '\\\\')) {
206 $name .= $address{$pos++
};
208 $name .= $address{$pos++
};
213 case '<': /* get email address */
214 $addr_start = $pos++
;
215 while ($pos < $j && $address{$pos} != '>') {
216 $addr .= $address{$pos++
};
220 case '(': /* rip off comments */
222 for (++
$pos; $pos < $j && $address{$pos} != ')'; ++
$pos) {
223 $addr .= $address{$pos};
225 $address_start = substr($address, 0, $addr_start);
226 $address_end = substr($address, $pos +
1);
227 $address = $address_start . $address_end;
228 $j = strlen($address);
229 $pos = $addr_start +
1;
231 case ',': /* we reached a delimiter */
233 $addr = substr($address, 0, $pos);
234 } else if ($name == '') {
235 $name = trim(substr($address, 0, $addr_start));
238 $at = strpos($addr, '@');
239 $addr_structure = new address_structure();
240 $addr_structure->personal
= $name;
241 $addr_structure->group
= $group;
243 $addr_structure->mailbox
= substr($addr, 0, $at);
244 $addr_structure->host
= substr($addr, $at+
1);
246 $addr_structure->mailbox
= $addr;
248 $address = trim(substr($address, $pos+
1));
249 $j = strlen($address);
253 $addr_ar[] = $addr_structure;
255 case ':': /* process the group addresses */
257 $group = substr($address, 0, $pos);
258 $address = substr($address, $pos+
1);
259 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
260 $addr_ar = $result[0];
262 $address = substr($address, $pos++
);
263 $j = strlen($address);
268 $address = substr($address, 0, $pos - 1);
278 $addr = substr($address, 0, $pos);
279 } else if ($name == '') {
280 $name = trim(substr($address, 0, $addr_start));
282 $at = strpos($addr, '@');
283 $addr_structure = new address_structure();
284 $addr_structure->group
= $group;
286 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
287 $addr_structure->host
= trim(substr($addr, $at+
1));
289 $addr_structure->mailbox
= trim($addr);
291 if ($group && $addr == '') { /* no addresses found in group */
292 $name = "$group: Undisclosed recipients;";
293 $addr_structure->personal
= $name;
294 $addr_ar[] = $addr_structure;
295 return (array($addr_ar, $pos+
1));
297 $addr_structure->personal
= $name;
298 if ($name ||
$addr) {
299 $addr_ar[] = $addr_structure;
305 return ($addr_ar[0]);
308 function parseContentType($value) {
309 $pos = strpos($value, ';');
312 $type = trim(substr($value, 0, $pos));
313 $props = trim(substr($type, $pos+
1));
317 $content_type = new content_type($type);
319 $properties = $this->parseProperties($props);
320 if (!isset($properties['charset'])) {
321 $properties['charset'] = 'us-ascii';
323 $content_type->properties
= $this->parseProperties($props);
325 $this->content_type
= $content_type;
328 function parseProperties($value) {
329 $propArray = explode(';', $value);
330 $propResultArray = array();
331 foreach ($propArray as $prop) {
333 $pos = strpos($prop, '=');
335 $key = trim(substr($prop, 0, $pos));
336 $val = trim(substr($prop, $pos+
1));
337 if ($val{0} == '"') {
338 $val = substr($val, 1, -1);
340 $propResultArray[$key] = $val;
343 return $propResultArray;
346 function parseDisposition($value) {
347 $pos = strpos($value, ';');
350 $name = trim(substr($value, 0, $pos));
351 $props = trim(substr($type, $pos+
1));
355 $props_a = $this->parseProperties($props);
356 $disp = new disposition($name);
357 $disp->properties
= $props_a;
358 $this->disposition
= $disp;
361 function mlist($field, $value) {
363 $value_a = explode(',', $value);
364 foreach ($value_a as $val) {
366 if ($val{0} == '<') {
367 $val = substr($val, 1, -1);
369 if (substr($val, 0, 7) == 'mailto:') {
370 $res_a['mailto'] = substr($val, 7);
372 $res_a['href'] = $val;
375 $this->mlist
[$field] = $res_a;
379 * function to get the addres strings out of the header.
380 * Arguments: string or array of strings !
381 * example1: header->getAddr_s('to').
382 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
384 function getAddr_s($arr, $separator = ',') {
387 if (is_array($arr)) {
388 foreach($arr as $arg) {
389 if ($this->getAddr_s($arg)) {
390 $s .= $separator . $result;
393 $s = ($s ?
substr($s, 2) : $s);
395 eval('$addr = $this->' . $arr . ';') ;
396 if (is_array($addr)) {
397 foreach ($addr as $addr_o) {
398 if (is_object($addr_o)) {
399 $s .= $addr_o->getAddress() . $separator;
402 $s = substr($s, 0, -strlen($separator));
404 if (is_object($addr)) {
405 $s .= $addr->getAddress();
412 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
413 if (is_array($arg)) {
414 foreach($arg as $argument) {
415 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
418 eval('$addr = $this->' . $arg . ';') ;
419 if (is_array($addr)) {
420 foreach ($addr as $next_addr) {
421 if (is_object($next_addr)) {
422 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
423 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
425 $email = $next_addr->mailbox
;
427 $email = strtolower($email);
428 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
429 $arr[$email] = $next_addr->personal
;
434 if (is_object($addr)) {
435 $email = $addr->mailbox
;
436 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
437 $email = strtolower($email);
438 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
439 $arr[$email] = $addr->personal
;
447 function getContentType($type0, $type1) {
448 $type0 = $this->content_type
->type0
;
449 $type1 = $this->content_type
->type1
;
450 return $this->content_type
->properties
;
455 /** msg_header contains all variables available in a bodystructure **/
456 /** entity like described in rfc2060 **/
460 $parameters = array(),
470 * returns addres_list of supplied argument
471 * arguments: array('to', 'from', ...) or just a string like 'to'.
472 * result: string: address1, addres2, ....
475 function setVar($var, $value) {
476 $this->{$var} = $value;
479 function getParameter($p) {
480 $value = strtolower($p);
481 return (isset($this->parameters
[$p]) ?
$this->parameters
[$p] : '');
484 function setParameter($parameter, $value) {
485 $this->parameters
[strtolower($parameter)] = $value;
491 class address_structure
{
498 function getAddress($full = true) {
501 if (is_object($this)) {
502 if (isset($this->host
) && ($this->host
!= '')) {
503 $email = $this->mailbox
.'@'.$this->host
;
505 $email = $this->mailbox
;
507 if (trim($this->personal
) != '') {
509 $addr = '"' . $this->personal
. '" <' .$email.'>';
511 $addr = $this->personal
;
513 $best_dpl = $this->personal
;
518 $result = ($full ?
$addr : $best_dpl);
525 /** message is the object that contains messages. It is a recursive
526 object in that through the $entities variable, it can contain
527 more objects of type message. See documentation in mime.txt for
528 a better description of how this works.
530 var $rfc822_header = '',
536 $parent_ent, $entity,
537 $parent = '', $decoded_body='',
538 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
541 $offset = 0, /* for fetching body parts out of raw messages */
542 $length = 0, /* for fetching body parts out of raw messages */
543 $att_local_name = ''; /* location where the tempory attachment
544 is stored. For future usage in smtp.php */
546 function setEnt($ent) {
547 $this->entity_id
= $ent;
550 function addEntity ($msg) {
551 $msg->parent
= &$this;
552 $this->entities
[] = $msg;
555 function getFilename() {
557 $filename = $this->header
->getParameter('filename');
559 $filename = $this->header
->getParameter('name');
563 $filename = 'untitled-'.$this->entity_id
;
569 function addRFC822Header($read) {
570 $header = new rfc822_header();
571 $this->rfc822_header
= $header->parseHeader($read);
574 function getEntity($ent) {
575 $cur_ent = $this->entity_id
;
577 if (($cur_ent == '') ||
($cur_ent == '0')) {
578 $cur_ent_a = array();
580 $cur_ent_a = explode('.', $this->entity_id
);
582 $ent_a = explode('.', $ent);
584 for ($i = 0, $cnt = count($ent_a)-1; $i < $cnt; ++
$i) {
585 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
587 $cur_ent_a = explode('.', $msg->entity_id
);
589 } else if (!isset($cur_ent_a[$i])) {
590 if (isset($msg->entities
[($ent_a[$i]-1)])) {
591 $msg = $msg->entities
[($ent_a[$i]-1)];
593 $msg = $msg->entities
[0];
596 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
597 /*this is a header for a message/rfc822 entity */
598 $msg = $msg->entities
[0];
602 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
603 /*this is a header for a message/rfc822 entity */
604 $msg = $msg->entities
[0];
607 if (isset($msg->entities
[($ent_a[$cnt])-1])) {
608 if (is_object($msg->entities
[($ent_a[$cnt])-1])) {
609 $msg = $msg->entities
[($ent_a[$cnt]-1)];
616 function setBody($s) {
617 $this->body_part
= $s;
620 function clean_up() {
622 $msg->body_part
= '';
624 foreach ($msg->entities
as $m) {
629 function getMailbox() {
631 while (is_object($msg->parent
)) {
634 return $msg->mailbox
;
637 function calcEntity($msg) {
638 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
639 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
640 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
641 $ent_no = count($this->entities
)+
1;
642 $par_ent = substr($this->entity_id
, -2);
643 if ($par_ent{0} == '.') {
644 $par_ent = $par_ent{1};
646 if ($par_ent == '0') {
647 $ent_no = count($this->entities
) +
1;
649 $ent = substr($this->entity_id
, 0, strrpos($this->entity_id
, '.'));
650 $ent = ($ent ?
$ent . ".$ent_no" : $ent_no);
651 $msg->entity_id
= $ent;
653 $msg->entity_id
= $ent_no;
656 $ent = $this->entity_id
. ".$ent_no";
657 $msg->entity_id
= $ent;
660 $msg->entity_id
= '0';
663 return $msg->entity_id
;
668 * Bodystructure parser, a recursive function for generating the
669 * entity-tree with all the mime-parts.
671 * It follows RFC2060 and stores all the described fields in the
676 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
679 function parseStructure($read, $i = 0) {
682 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
683 $char = strtoupper($read{$i});
689 $msg = new message();
690 $hdr = new msg_header();
691 $hdr->type0
= 'text';
692 $hdr->type1
= 'plain';
693 $hdr->encoding
= 'us-ascii';
694 $msg->entity_id
= $this->calcEntity($msg);
696 $msg->header
->type0
= 'multipart';
697 $msg->type0
= 'multipart';
698 while ($read{$i} == '(') {
699 $res = $msg->parseStructure($read, $i);
701 $msg->addEntity($res[0]);
706 /* multipart properties */
708 $res = $this->parseProperties($read, $i);
714 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
716 $res = $msg->parseDisposition($read, $i);
717 } else { /* properties */
718 $res = $msg->parseProperties($read, $i);
725 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
727 $res= $msg->parseLanguage($read, $i);
732 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
733 $msg->header
->type0
= $arg_a[0];
734 $msg->header
->type1
= $arg_a[1];
735 $msg->type0
= $arg_a[0];
736 $msg->type1
= $arg_a[1];
737 $rfc822_hdr = new rfc822_header();
738 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
739 $msg->rfc822_header
= $res[0];
741 while (($i < $cnt) && ($read{$i} != '(')) {
744 $res = $msg->parseStructure($read, $i);
746 $msg->addEntity($res[0]);
751 $res = $msg->parseDisposition($read, $i);
758 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
759 $res = $msg->parseDisposition($read, $i);
761 $res = $msg->parseLanguage($read, $i);
768 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
770 $res = $msg->parseLanguage($read, $i);
774 $i = $msg->parseParenthesis($read, $i);
775 $arg_a[] = ''; /* not yet described in rfc2060 */
780 /* unknown argument, skip this part */
781 $i = $msg->parseParenthesis($read, $i);
788 /* inside an entity -> start processing */
789 $debug = substr($read, $i, 20);
790 $res = $msg->parseQuote($read, $i);
795 $arg_s = strtolower($arg_s); /* type0 and type1 */
801 /* probably NIL argument */
802 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
809 /* process the literal value */
810 $res = $msg->parseLiteral($read, $i);
815 case is_numeric($read{$i}):
816 /* process integers */
817 if ($read{$i} == ' ') { break; }
819 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
826 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
828 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
829 $hdr->type0
= $arg_a[0];
830 $hdr->type1
= $arg_a[1];
832 $msg->type0
= $arg_a[0];
833 $msg->type1
= $arg_a[1];
835 if (is_array($arr)) {
836 $hdr->parameters
= $arg_a[2];
838 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
839 $hdr->description
= $arg_a[4];
840 $hdr->encoding
= strtolower($arg_a[5]);
841 $hdr->entity_id
= $msg->entity_id
;
842 $hdr->size
= $arg_a[6];
844 $hdr->lines
= $arg_a[7];
849 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
850 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
851 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
853 if ((strrchr($msg->entity_id
, '.') == '.0') && ($msg->type0
!='multipart')) {
854 $msg->entity_id
= $this->entity_id
. '.1';
857 $hdr->type0
= 'multipart';
858 $hdr->type1
= $arg_a[0];
859 $msg->type0
= 'multipart';
860 $msg->type1
= $arg_a[0];
861 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
862 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
863 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
867 return (array($msg, $i));
872 } /* parsestructure */
874 function parseProperties($read, $i) {
875 $properties = array();
878 for (; $read{$i} != ')'; ++
$i) {
880 if ($read{$i} == '"') {
881 $res = $this->parseQuote($read, $i);
884 } else if ($read{$i} == '{') {
885 $res = $this->parseLiteral($read, $i);
891 if ($prop_name == '') {
892 $prop_name = strtolower($arg_s);
893 $properties[$prop_name] = '';
894 } else if ($prop_name != '') {
895 $properties[$prop_name] = $arg_s;
900 return array($properties, $i);
903 function parseEnvelope($read, $i, $hdr) {
907 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
909 $char = strtoupper($read{$i});
912 $res = $this->parseQuote($read, $i);
918 $res = $this->parseLiteral($read, $i);
924 /* probably NIL argument */
925 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
932 /* Address structure (with group support)
933 * Note: Group support is useless on SMTP connections
934 * because the protocol doesn't support it
939 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
940 if ($read{$i} == '(') {
941 $res = $this->parseAddress($read, $i);
944 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
946 $group = $addr->mailbox
;
949 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
951 if ($a == ($j+
1)) { /* no group members */
952 $group_addr->group
= $group;
953 $group_addr->mailbox
= '';
954 $group_addr->personal
= "$group: Undisclosed recipients;";
955 $addr_a[] = $group_addr;
959 $addr->group
= $group;
971 if (count($arg_a) > 9) {
972 /* argument 1: date */
973 $d = strtr($arg_a[0], array(' ' => ' '));
974 $d = explode(' ', $d);
975 $hdr->date
= getTimeStamp($d);
977 /* argument 2: subject */
978 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
979 $hdr->subject
= $arg_a[1];
981 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
982 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
983 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
984 $hdr->to
= $arg_a[5]; /* argument 6: to */
985 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
986 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
987 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
988 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
990 return (array($hdr, $i));
993 function parseLiteral($read, $i) {
995 for (++
$i; $read{$i} != '}'; ++
$i) {
996 $lit_cnt .= $read{$i};
999 $lit_cnt +
=2; /* add the { and } characters */
1001 for ($j = 0; $j < $lit_cnt; ++
$j) {
1004 return (array($s, $i));
1007 function parseQuote($read, $i) {
1009 for (++
$i; $read{$i} != '"'; ++
$i) {
1010 if ($read{$i} == '\\') {
1015 return (array($s, $i));
1018 function parseAddress($read, $i) {
1021 for (; $read{$i} != ')'; ++
$i) {
1022 $char = strtoupper($read{$i});
1026 $res = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1032 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1041 if (count($arg_a) == 4) {
1042 $adr = new address_structure();
1043 $adr->personal
= $arg_a[0];
1044 $adr->adl
= $arg_a[1];
1045 $adr->mailbox
= $arg_a[2];
1046 $adr->host
= $arg_a[3];
1050 return (array($adr, $i));
1053 function parseDisposition($read, $i) {
1056 for (; $read{$i} != ')'; ++
$i) {
1057 switch ($read{$i}) {
1061 switch ($read{$i}) {
1062 case '"': $res = $this->parseQuote($read, $i); break;
1063 case '{': $res = $this->parseLiteral($read, $i); break;
1064 case '(': $res = $this->parseProperties($read, $i); break;
1073 if (isset($arg_a[0])) {
1074 $disp = new disposition($arg_a[0]);
1075 if (isset($arg_a[1])) {
1076 $disp->properties
= $arg_a[1];
1080 return (is_object($disp) ?
array($disp, $i) : array('', $i));
1083 function parseLanguage($read, $i) {
1084 /* no idea how to process this one without examples */
1087 for (; $read{$i} != ')'; ++
$i) {
1088 switch ($read{$i}) {
1092 switch ($read{$i}) {
1093 case '"': $res = $this->parseQuote($read, $i); break;
1094 case '{': $res = $this->parseLiteral($read, $i); break;
1095 case '(': $res = $this->parseProperties($read, $i); break;
1104 if (isset($arg_a[0])) {
1105 $lang = new language($arg_a[0]);
1106 if (isset($arg_a[1])) {
1107 $lang->properties
= $arg_a[1];
1111 return (is_object($lang) ?
array($lang, $i) : array('', $i));
1114 function parseParenthesis($read, $i) {
1115 for (; $read{$i} != ')'; ++
$i) {
1116 switch ($read{$i}) {
1120 switch ($read{$i}) {
1121 case '"': $res = $this->parseQuote($read, $i); break;
1122 case '{': $res = $this->parseLiteral($read, $i); break;
1123 case '(': $res = $this->parseProperties($read, $i); break;
1133 /* Function to fill the message structure in case the */
1134 /* bodystructure is not available NOT FINISHED YET */
1135 function parseMessage($read, $type0, $type1) {
1138 $rfc822_header = true;
1139 $mime_header = false;
1142 $rfc822_header = false;
1143 $mime_header = true;
1145 default: return $read;
1148 for ($i = 1; $i < $count; ++
$i) {
1149 $line = trim($body[$i]);
1150 if (($mime_header ||
$rfc822_header) &&
1151 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
1154 $bndreg = str_replace("\\", "\\\\", $bndreg);
1155 $bndreg = str_replace("?", "\\?", $bndreg);
1156 $bndreg = str_replace("+", "\\+", $bndreg);
1157 $bndreg = str_replace(".", "\\.", $bndreg);
1158 $bndreg = str_replace("/", "\\/", $bndreg);
1159 $bndreg = str_replace("-", "\\-", $bndreg);
1160 $bndreg = str_replace("(", "\\(", $bndreg);
1161 $bndreg = str_replace(")", "\\)", $bndreg);
1162 } else if ($rfc822_header && $line == '') {
1163 $rfc822_header = false;
1164 if ($msg->type0
== 'multipart') {
1165 $mime_header = true;
1169 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
1170 $cnt = count($boundaries)-1;
1171 $bnd = $boundaries[$cnt]['bnd'];
1172 $bndreg = $boundaries[$cnt]['bndreg'];
1174 $regstr = '/^--'."($bndreg)".".*".'/';
1175 if (preg_match($regstr, $line, $reg)) {
1176 $bndlen = strlen($reg[1]);
1178 if (strlen($line) > ($bndlen +
3)) {
1179 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
1184 /* calc offset and return $msg */
1185 //$entStr = CalcEntity("$entStr", -1);
1186 array_pop($boundaries);
1187 $mime_header = true;
1190 $mime_header = true;
1192 //$entStr = CalcEntity("$entStr", 0);
1202 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
1204 if ($this->type0
== 'multipart') {
1205 if($this->type1
== 'alternative') {
1206 $msg = $this->findAlternativeEntity($alt_order);
1207 if (count($msg->entities
) == 0) {
1208 $entity[] = $msg->entity_id
;
1210 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1213 } else if ($this->type1
== 'related') { /* RFC 2387 */
1214 $msgs = $this->findRelatedEntity();
1215 foreach ($msgs as $msg) {
1216 if (count($msg->entities
) == 0) {
1217 $entity[] = $msg->entity_id
;
1219 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1222 if (count($msgs) > 0) {
1225 } else { /* Treat as multipart/mixed */
1226 foreach ($this->entities
as $ent) {
1227 if(strtolower($ent->header
->disposition
->name
) != 'attachment' &&
1228 ($ent->type0
!= 'message' && $ent->type1
!= 'rfc822'))
1230 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1235 } else { /* If not multipart, then just compare with each entry from $alt_order */
1236 $type = $this->type0
.'/'.$this->type1
;
1237 foreach ($alt_order as $alt) {
1238 if( ($alt == $type) && isset($this->entity_id
) ) {
1239 if ( (count($this->entities
) == 0) &&
1240 (strtolower($this->header
->disposition
->name
) != 'attachment') )
1242 $entity[] = $this->entity_id
;
1249 foreach ($this->entities
as $ent) {
1250 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
1251 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1252 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1257 if(!$strict && !$found) {
1258 if ($this->type0
== 'text' &&
1259 ($this->type1
== 'plain' ||
1260 $this->type1
== 'html' ||
1261 $this->type1
== 'message') &&
1262 isset($this->entity_id
) )
1264 if (count($this->entities
) == 0) {
1265 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
1266 $entity[] = $this->entity_id
;
1275 function findAlternativeEntity($alt_order) {
1276 /* If we are dealing with alternative parts then we */
1277 /* choose the best viewable message supported by SM. */
1280 foreach($this->entities
as $ent) {
1281 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1282 if ($type == 'multipart/related') {
1283 $type = $ent->header
->getParameter('type');
1285 for ($j = $best_view, $altcount = count($alt_order); $j < $altcount; ++
$j) {
1286 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1296 function findRelatedEntity() {
1298 $entcount = count($this->entities
);
1300 for ($i = 0; $i < $entcount; ++
$i) {
1301 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1302 if ($this->header
->getParameter('type') == $type) {
1303 $msgs[] = $this->entities
[$i];
1310 function getAttachments($exclude_id=array(), $result = array()) {
1311 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
1312 $this = $this->entities
[0];
1315 if (count($this->entities
)) {
1316 foreach ($this->entities
as $entity) {
1319 foreach ($exclude_id as $excl) {
1320 if ($entity->entity_id
=== $excl) {
1326 if (($entity->type0
== 'multipart') &&
1327 ($entity->type1
!= 'related')) {
1328 $result = $entity->getAttachments($exclude_id, $result);
1329 } else if ($entity->type0
!= 'multipart') {
1330 $result[] = $entity;
1336 foreach ($exclude_id as $excl) {
1337 $exclude = $exclude ||
($this->entity_id
== $excl);
1349 class smime_message
{
1354 function disposition($name) {
1355 $this->name
= $name;
1356 $this->properties
= array();
1359 function getProperty($par) {
1360 $value = strtolower($par);
1361 if (isset($this->properties
[$par])) {
1362 return $this->properties
[$par];
1369 function language($name) {
1370 $this->name
= $name;
1371 $this->properties
= array();
1375 class content_type
{
1376 var $type0 = 'text',
1380 function content_type($type) {
1381 $pos = strpos($type, '/');
1383 $this->type0
= substr($type, 0, $pos);
1384 $this->type1
= substr($type, $pos+
1);
1386 $this->type0
= $type;
1388 $this->properties
= array();