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) {
69 $cnt = strlen($value);
70 for ($i = 0; $i < $cnt; $i++
) {
74 while ((++
$i < $cnt) && ($value{$i} != '"')) {
75 if ($value{$i} == '\\') {
79 $result .= $value{$i};
81 $result .= $value{$i};
85 while (($depth > 0) && (++
$i < $cnt)) {
102 $result .= $value{$i};
109 function parseField($field, $value) {
110 $field = strtolower($field);
113 $d = strtr($value, array(' ' => ' '));
114 $d = explode(' ', $d);
115 $this->date
= getTimeStamp($d);
118 $this->subject
= $value;
121 $this->from
= $this->parseAddress($value,true);
124 $this->sender
= $this->parseAddress($value);
127 $this->reply_to
= $this->parseAddress($value, true);
130 $this->to
= $this->parseAddress($value, true);
133 $this->cc
= $this->parseAddress($value, true);
136 $this->bcc
= $this->parseAddress($value, true);
139 $this->in_reply_to
= $value;
142 $this->message_id
= $value;
144 case 'disposition-notification-to':
145 $this->dnt
= $this->parseAddress($value);
148 $value = str_replace(' ', '', $value);
149 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
152 $this->parseContentType($value);
154 case 'content-disposition':
155 $this->parseDisposition($value);
159 $this->xmailer
= $value;
162 $this->priority
= $value;
165 $this->mlist('post', $value);
168 $this->mlist('reply', $value);
170 case 'list-subscribe':
171 $this->mlist('subscribe', $value);
173 case 'list-unsubscribe':
174 $this->mlist('unsubscribe', $value);
177 $this->mlist('archive', $value);
180 $this->mlist('owner', $value);
183 $this->mlist('help', $value);
186 $this->mlist('id', $value);
193 function parseAddress
194 ($address, $ar=false, $addr_ar = array(), $group = '') {
196 $j = strlen($address);
200 switch ($address{$pos}) {
201 case '"': /* get the personal name */
202 if ($address{++
$pos} == '"') {
205 while ($pos < $j && $address{$pos} != '"') {
206 if ((substr($address, $pos, 2) == '\\"') ||
207 (substr($address, $pos, 2) == '\\\\')) {
208 $name .= $address{$pos++
};
210 $name .= $address{$pos++
};
215 case '<': /* get email address */
216 $addr_start = $pos++
;
217 while ($pos < $j && $address{$pos} != '>') {
218 $addr .= $address{$pos++
};
222 case '(': /* rip off comments */
224 for (++
$pos; $pos < $j && $address{$pos} != ')'; ++
$pos) {
225 $addr .= $address{$pos};
227 $address_start = substr($address, 0, $addr_start);
228 $address_end = substr($address, $pos +
1);
229 $address = $address_start . $address_end;
230 $j = strlen($address);
231 $pos = $addr_start +
1;
233 case ',': /* we reached a delimiter */
235 $addr = substr($address, 0, $pos);
236 } else if ($name == '') {
237 $name = trim(substr($address, 0, $addr_start));
240 $at = strpos($addr, '@');
241 $addr_structure = new address_structure();
242 $addr_structure->personal
= $name;
243 $addr_structure->group
= $group;
245 $addr_structure->mailbox
= substr($addr, 0, $at);
246 $addr_structure->host
= substr($addr, $at+
1);
248 $addr_structure->mailbox
= $addr;
250 $address = trim(substr($address, $pos+
1));
251 $j = strlen($address);
255 $addr_ar[] = $addr_structure;
257 case ':': /* process the group addresses */
259 $group = substr($address, 0, $pos);
260 $address = substr($address, $pos+
1);
261 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
262 $addr_ar = $result[0];
264 $address = substr($address, $pos++
);
265 $j = strlen($address);
270 $address = substr($address, 0, $pos - 1);
280 $addr = substr($address, 0, $pos);
281 } else if ($name == '') {
282 $name = trim(substr($address, 0, $addr_start));
284 $at = strpos($addr, '@');
285 $addr_structure = new address_structure();
286 $addr_structure->group
= $group;
288 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
289 $addr_structure->host
= trim(substr($addr, $at+
1));
291 $addr_structure->mailbox
= trim($addr);
293 if ($group && $addr == '') { /* no addresses found in group */
294 $name = "$group: Undisclosed recipients;";
295 $addr_structure->personal
= $name;
296 $addr_ar[] = $addr_structure;
297 return (array($addr_ar, $pos+
1));
299 $addr_structure->personal
= $name;
300 if ($name ||
$addr) {
301 $addr_ar[] = $addr_structure;
307 return ($addr_ar[0]);
310 function parseContentType($value) {
311 $pos = strpos($value, ';');
314 $type = trim(substr($value, 0, $pos));
315 $props = trim(substr($type, $pos+
1));
319 $content_type = new content_type($type);
321 $properties = $this->parseProperties($props);
322 if (!isset($properties['charset'])) {
323 $properties['charset'] = 'us-ascii';
325 $content_type->properties
= $this->parseProperties($props);
327 $this->content_type
= $content_type;
330 function parseProperties($value) {
331 $propArray = explode(';', $value);
332 $propResultArray = array();
333 foreach ($propArray as $prop) {
335 $pos = strpos($prop, '=');
337 $key = trim(substr($prop, 0, $pos));
338 $val = trim(substr($prop, $pos+
1));
339 if ($val{0} == '"') {
340 $val = substr($val, 1, -1);
342 $propResultArray[$key] = $val;
345 return $propResultArray;
348 function parseDisposition($value) {
349 $pos = strpos($value, ';');
352 $name = trim(substr($value, 0, $pos));
353 $props = trim(substr($type, $pos+
1));
357 $props_a = $this->parseProperties($props);
358 $disp = new disposition($name);
359 $disp->properties
= $props_a;
360 $this->disposition
= $disp;
363 function mlist($field, $value) {
365 $value_a = explode(',', $value);
366 foreach ($value_a as $val) {
368 if ($val{0} == '<') {
369 $val = substr($val, 1, -1);
371 if (substr($val, 0, 7) == 'mailto:') {
372 $res_a['mailto'] = substr($val, 7);
374 $res_a['href'] = $val;
377 $this->mlist
[$field] = $res_a;
381 * function to get the addres strings out of the header.
382 * Arguments: string or array of strings !
383 * example1: header->getAddr_s('to').
384 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
386 function getAddr_s($arr, $separator = ',') {
389 if (is_array($arr)) {
390 foreach($arr as $arg) {
391 if ($this->getAddr_s($arg)) {
392 $s .= $separator . $result;
395 $s = ($s ?
substr($s, 2) : $s);
397 eval('$addr = $this->' . $arr . ';') ;
398 if (is_array($addr)) {
399 foreach ($addr as $addr_o) {
400 if (is_object($addr_o)) {
401 $s .= $addr_o->getAddress() . $separator;
404 $s = substr($s, 0, -strlen($separator));
406 if (is_object($addr)) {
407 $s .= $addr->getAddress();
414 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
415 if (is_array($arg)) {
416 foreach($arg as $argument) {
417 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
420 eval('$addr = $this->' . $arg . ';') ;
421 if (is_array($addr)) {
422 foreach ($addr as $next_addr) {
423 if (is_object($next_addr)) {
424 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
425 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
427 $email = $next_addr->mailbox
;
429 $email = strtolower($email);
430 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
431 $arr[$email] = $next_addr->personal
;
436 if (is_object($addr)) {
437 $email = $addr->mailbox
;
438 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
439 $email = strtolower($email);
440 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
441 $arr[$email] = $addr->personal
;
449 function getContentType($type0, $type1) {
450 $type0 = $this->content_type
->type0
;
451 $type1 = $this->content_type
->type1
;
452 return $this->content_type
->properties
;
457 /** msg_header contains all variables available in a bodystructure **/
458 /** entity like described in rfc2060 **/
462 $parameters = array(),
472 * returns addres_list of supplied argument
473 * arguments: array('to', 'from', ...) or just a string like 'to'.
474 * result: string: address1, addres2, ....
477 function setVar($var, $value) {
478 $this->{$var} = $value;
481 function getParameter($p) {
482 $value = strtolower($p);
483 return (isset($this->parameters
[$p]) ?
$this->parameters
[$p] : '');
486 function setParameter($parameter, $value) {
487 $this->parameters
[strtolower($parameter)] = $value;
493 class address_structure
{
500 function getAddress($full = true) {
503 if (is_object($this)) {
504 if (isset($this->host
) && ($this->host
!= '')) {
505 $email = $this->mailbox
.'@'.$this->host
;
507 $email = $this->mailbox
;
509 if (trim($this->personal
) != '') {
511 $addr = '"' . $this->personal
. '" <' .$email.'>';
513 $addr = $this->personal
;
515 $best_dpl = $this->personal
;
520 $result = ($full ?
$addr : $best_dpl);
527 /** message is the object that contains messages. It is a recursive
528 object in that through the $entities variable, it can contain
529 more objects of type message. See documentation in mime.txt for
530 a better description of how this works.
532 var $rfc822_header = '',
538 $parent_ent, $entity,
539 $parent = '', $decoded_body='',
540 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
543 $offset = 0, /* for fetching body parts out of raw messages */
544 $length = 0; /* for fetching body parts out of raw messages */
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 $cnt = count($ent_a);
586 for ($i = 0; $i < $cnt -1; ++
$i) {
587 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
589 $cur_ent_a = explode('.', $msg->entity_id
);
591 } else if (!isset($cur_ent_a[$i])) {
592 if (isset($msg->entities
[($ent_a[$i]-1)])) {
593 $msg = $msg->entities
[($ent_a[$i]-1)];
595 $msg = $msg->entities
[0];
598 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
599 /*this is a header for a message/rfc822 entity */
600 $msg = $msg->entities
[0];
604 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
605 /*this is a header for a message/rfc822 entity */
606 $msg = $msg->entities
[0];
609 if (isset($msg->entities
[($ent_a[$cnt-1])-1])) {
610 if (is_object($msg->entities
[($ent_a[$cnt-1])-1])) {
611 $msg = $msg->entities
[($ent_a[$cnt-1]-1)];
618 function setBody($s) {
619 $this->body_part
= $s;
622 function clean_up() {
624 $msg->body_part
= '';
626 foreach ($msg->entities
as $m) {
631 function getMailbox() {
633 while (is_object($msg->parent
)) {
636 return $msg->mailbox
;
639 function calcEntity($msg) {
640 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
641 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
642 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
643 $ent_no = count($this->entities
)+
1;
644 $par_ent = substr($this->entity_id
, -2);
645 if ($par_ent{0} == '.') {
646 $par_ent = $par_ent{1};
648 if ($par_ent == '0') {
649 $ent_no = count($this->entities
) +
1;
651 $ent = substr($this->entity_id
, 0, strrpos($this->entity_id
, '.'));
652 $ent = ($ent ?
$ent . ".$ent_no" : $ent_no);
653 $msg->entity_id
= $ent;
655 $msg->entity_id
= $ent_no;
658 $ent = $this->entity_id
. ".$ent_no";
659 $msg->entity_id
= $ent;
662 $msg->entity_id
= '0';
665 return $msg->entity_id
;
670 * Bodystructure parser, a recursive function for generating the
671 * entity-tree with all the mime-parts.
673 * It follows RFC2060 and stores all the described fields in the
678 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
681 function parseStructure($read, $i = 0) {
684 $cnt = strlen($read);
686 for (; $i < $cnt; ++
$i) {
687 $char = strtoupper($read{$i});
693 $msg = new message();
694 $hdr = new msg_header();
695 $hdr->type0
= 'text';
696 $hdr->type1
= 'plain';
697 $hdr->encoding
= 'us-ascii';
698 $msg->entity_id
= $this->calcEntity($msg);
700 $msg->header
->type0
= 'multipart';
701 $msg->type0
= 'multipart';
702 while ($read{$i} == '(') {
703 $res = $msg->parseStructure($read, $i);
705 $msg->addEntity($res[0]);
710 /* multipart properties */
712 $res = $this->parseProperties($read, $i);
718 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
720 $res = $msg->parseDisposition($read, $i);
721 } else { /* properties */
722 $res = $msg->parseProperties($read, $i);
729 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
731 $res= $msg->parseLanguage($read, $i);
736 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
737 $msg->header
->type0
= $arg_a[0];
738 $msg->header
->type1
= $arg_a[1];
739 $msg->type0
= $arg_a[0];
740 $msg->type1
= $arg_a[1];
741 $rfc822_hdr = new rfc822_header();
742 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
743 $msg->rfc822_header
= $res[0];
745 while (($i < $cnt) && ($read{$i} != '(')) {
748 $res = $msg->parseStructure($read, $i);
750 $msg->addEntity($res[0]);
755 $res = $msg->parseDisposition($read, $i);
762 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
763 $res = $msg->parseDisposition($read, $i);
765 $res = $msg->parseLanguage($read, $i);
772 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
774 $res = $msg->parseLanguage($read, $i);
778 $i = $msg->parseParenthesis($read, $i);
779 $arg_a[] = ''; /* not yet described in rfc2060 */
784 /* unknown argument, skip this part */
785 $i = $msg->parseParenthesis($read, $i);
792 /* inside an entity -> start processing */
793 $debug = substr($read, $i, 20);
794 $res = $msg->parseQuote($read, $i);
799 $arg_s = strtolower($arg_s); /* type0 and type1 */
805 /* probably NIL argument */
806 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
813 /* process the literal value */
814 $res = $msg->parseLiteral($read, $i);
819 case is_numeric($read{$i}):
820 /* process integers */
821 if ($read{$i} == ' ') { break; }
823 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
830 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
832 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
833 $hdr->type0
= $arg_a[0];
834 $hdr->type1
= $arg_a[1];
836 $msg->type0
= $arg_a[0];
837 $msg->type1
= $arg_a[1];
839 if (is_array($arr)) {
840 $hdr->parameters
= $arg_a[2];
842 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
843 $hdr->description
= $arg_a[4];
844 $hdr->encoding
= strtolower($arg_a[5]);
845 $hdr->entity_id
= $msg->entity_id
;
846 $hdr->size
= $arg_a[6];
848 $hdr->lines
= $arg_a[7];
853 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
854 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
855 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
857 if ((strrchr($msg->entity_id
, '.') == '.0') && ($msg->type0
!='multipart')) {
858 $msg->entity_id
= $this->entity_id
. '.1';
861 $hdr->type0
= 'multipart';
862 $hdr->type1
= $arg_a[0];
863 $msg->type0
= 'multipart';
864 $msg->type1
= $arg_a[0];
865 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
866 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
867 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
871 return (array($msg, $i));
876 } /* parsestructure */
878 function parseProperties($read, $i) {
879 $properties = array();
882 for (; $read{$i} != ')'; ++
$i) {
884 if ($read{$i} == '"') {
885 $res = $this->parseQuote($read, $i);
888 } else if ($read{$i} == '{') {
889 $res = $this->parseLiteral($read, $i);
895 if ($prop_name == '') {
896 $prop_name = strtolower($arg_s);
897 $properties[$prop_name] = '';
898 } else if ($prop_name != '') {
899 $properties[$prop_name] = $arg_s;
904 return array($properties, $i);
907 function parseEnvelope($read, $i, $hdr) {
910 $cnt = strlen($read);
912 for (; ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
914 $char = strtoupper($read{$i});
917 $res = $this->parseQuote($read, $i);
923 $res = $this->parseLiteral($read, $i);
929 /* probably NIL argument */
930 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
937 /* Address structure (with group support)
938 * Note: Group support is useless on SMTP connections
939 * because the protocol doesn't support it
944 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
945 if ($read{$i} == '(') {
946 $res = $this->parseAddress($read, $i);
949 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
951 $group = $addr->mailbox
;
954 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
956 if ($a == ($j+
1)) { /* no group members */
957 $group_addr->group
= $group;
958 $group_addr->mailbox
= '';
959 $group_addr->personal
= "$group: Undisclosed recipients;";
960 $addr_a[] = $group_addr;
964 $addr->group
= $group;
976 if (count($arg_a) > 9) {
977 /* argument 1: date */
978 $d = strtr($arg_a[0], array(' ' => ' '));
979 $d = explode(' ', $d);
980 $hdr->date
= getTimeStamp($d);
982 /* argument 2: subject */
983 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
984 $hdr->subject
= $arg_a[1];
986 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
987 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
988 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
989 $hdr->to
= $arg_a[5]; /* argument 6: to */
990 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
991 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
992 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
993 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
995 return (array($hdr, $i));
998 function parseLiteral($read, $i) {
1000 for (++
$i; $read{$i} != '}'; ++
$i) {
1001 $lit_cnt .= $read{$i};
1004 $lit_cnt +
=2; /* add the { and } characters */
1006 for ($j = 0; $j < $lit_cnt; ++
$j) {
1009 return (array($s, $i));
1012 function parseQuote($read, $i) {
1014 for (++
$i; $read{$i} != '"'; ++
$i) {
1015 if ($read{$i} == '\\') {
1020 return (array($s, $i));
1023 function parseAddress($read, $i) {
1026 for (; $read{$i} != ')'; ++
$i) {
1027 $char = strtoupper($read{$i});
1031 $res = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1037 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1046 if (count($arg_a) == 4) {
1047 $adr = new address_structure();
1048 $adr->personal
= $arg_a[0];
1049 $adr->adl
= $arg_a[1];
1050 $adr->mailbox
= $arg_a[2];
1051 $adr->host
= $arg_a[3];
1055 return (array($adr, $i));
1058 function parseDisposition($read, $i) {
1061 for (; $read{$i} != ')'; ++
$i) {
1062 switch ($read{$i}) {
1066 switch ($read{$i}) {
1067 case '"': $res = $this->parseQuote($read, $i); break;
1068 case '{': $res = $this->parseLiteral($read, $i); break;
1069 case '(': $res = $this->parseProperties($read, $i); break;
1078 if (isset($arg_a[0])) {
1079 $disp = new disposition($arg_a[0]);
1080 if (isset($arg_a[1])) {
1081 $disp->properties
= $arg_a[1];
1085 return (is_object($disp) ?
array($disp, $i) : array('', $i));
1088 function parseLanguage($read, $i) {
1089 /* no idea how to process this one without examples */
1092 for (; $read{$i} != ')'; ++
$i) {
1093 switch ($read{$i}) {
1097 switch ($read{$i}) {
1098 case '"': $res = $this->parseQuote($read, $i); break;
1099 case '{': $res = $this->parseLiteral($read, $i); break;
1100 case '(': $res = $this->parseProperties($read, $i); break;
1109 if (isset($arg_a[0])) {
1110 $lang = new language($arg_a[0]);
1111 if (isset($arg_a[1])) {
1112 $lang->properties
= $arg_a[1];
1116 return (is_object($lang) ?
array($lang, $i) : array('', $i));
1119 function parseParenthesis($read, $i) {
1120 for (; $read{$i} != ')'; ++
$i) {
1121 switch ($read{$i}) {
1125 switch ($read{$i}) {
1126 case '"': $res = $this->parseQuote($read, $i); break;
1127 case '{': $res = $this->parseLiteral($read, $i); break;
1128 case '(': $res = $this->parseProperties($read, $i); break;
1138 /* Function to fill the message structure in case the */
1139 /* bodystructure is not available NOT FINISHED YET */
1140 function parseMessage($read, $type0, $type1) {
1143 $rfc822_header = true;
1144 $mime_header = false;
1147 $rfc822_header = false;
1148 $mime_header = true;
1150 default: return $read;
1153 for ($i = 1; $i < $count; ++
$i) {
1154 $line = trim($body[$i]);
1155 if (($mime_header ||
$rfc822_header) &&
1156 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
1159 $bndreg = str_replace("\\", "\\\\", $bndreg);
1160 $bndreg = str_replace("?", "\\?", $bndreg);
1161 $bndreg = str_replace("+", "\\+", $bndreg);
1162 $bndreg = str_replace(".", "\\.", $bndreg);
1163 $bndreg = str_replace("/", "\\/", $bndreg);
1164 $bndreg = str_replace("-", "\\-", $bndreg);
1165 $bndreg = str_replace("(", "\\(", $bndreg);
1166 $bndreg = str_replace(")", "\\)", $bndreg);
1167 } else if ($rfc822_header && $line == '') {
1168 $rfc822_header = false;
1169 if ($msg->type0
== 'multipart') {
1170 $mime_header = true;
1174 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
1175 $cnt=count($boundaries)-1;
1176 $bnd = $boundaries[$cnt]['bnd'];
1177 $bndreg = $boundaries[$cnt]['bndreg'];
1179 $regstr = '/^--'."($bndreg)".".*".'/';
1180 if (preg_match($regstr, $line, $reg)) {
1181 $bndlen = strlen($reg[1]);
1183 if (strlen($line) > ($bndlen +
3)) {
1184 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
1189 /* calc offset and return $msg */
1190 //$entStr = CalcEntity("$entStr", -1);
1191 array_pop($boundaries);
1192 $mime_header = true;
1195 $mime_header = true;
1197 //$entStr = CalcEntity("$entStr", 0);
1207 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
1209 if ($this->type0
== 'multipart') {
1210 if($this->type1
== 'alternative') {
1211 $msg = $this->findAlternativeEntity($alt_order);
1212 if (count($msg->entities
) == 0) {
1213 $entity[] = $msg->entity_id
;
1215 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1218 } else if ($this->type1
== 'related') { /* RFC 2387 */
1219 $msgs = $this->findRelatedEntity();
1220 foreach ($msgs as $msg) {
1221 if (count($msg->entities
) == 0) {
1222 $entity[] = $msg->entity_id
;
1224 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1227 if (count($msgs) > 0) {
1230 } else { /* Treat as multipart/mixed */
1231 foreach ($this->entities
as $ent) {
1232 if(strtolower($ent->header
->disposition
->name
) != 'attachment' &&
1233 ($ent->type0
!= 'message' && $ent->type1
!= 'rfc822'))
1235 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1240 } else { /* If not multipart, then just compare with each entry from $alt_order */
1241 $type = $this->type0
.'/'.$this->type1
;
1242 foreach ($alt_order as $alt) {
1243 if( ($alt == $type) && isset($this->entity_id
) ) {
1244 if ( (count($this->entities
) == 0) &&
1245 (strtolower($this->header
->disposition
->name
) != 'attachment') )
1247 $entity[] = $this->entity_id
;
1254 foreach ($this->entities
as $ent) {
1255 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
1256 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1257 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1262 if(!$strict && !$found) {
1263 if ($this->type0
== 'text' &&
1264 ($this->type1
== 'plain' ||
1265 $this->type1
== 'html' ||
1266 $this->type1
== 'message') &&
1267 isset($this->entity_id
) )
1269 if (count($this->entities
) == 0) {
1270 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
1271 $entity[] = $this->entity_id
;
1280 function findAlternativeEntity($alt_order) {
1281 /* If we are dealing with alternative parts then we */
1282 /* choose the best viewable message supported by SM. */
1285 $altcount = count($alt_order);
1287 foreach($this->entities
as $ent) {
1288 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1289 if ($type == 'multipart/related') {
1290 $type = $ent->header
->getParameter('type');
1292 for ($j = $best_view; $j < $altcount; ++
$j) {
1293 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1303 function findRelatedEntity() {
1305 $entcount = count($this->entities
);
1307 for ($i = 0; $i < $entcount; ++
$i) {
1308 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1309 if ($this->header
->getParameter('type') == $type) {
1310 $msgs[] = $this->entities
[$i];
1317 function getAttachments($exclude_id=array(), $result = array()) {
1318 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
1319 $this = $this->entities
[0];
1322 if (count($this->entities
)) {
1323 foreach ($this->entities
as $entity) {
1326 foreach ($exclude_id as $excl) {
1327 if ($entity->entity_id
=== $excl) {
1333 if (($entity->type0
== 'multipart') &&
1334 ($entity->type1
!= 'related')) {
1335 $result = $entity->getAttachments($exclude_id, $result);
1336 } else if ($entity->type0
!= 'multipart') {
1337 $result[] = $entity;
1343 foreach ($exclude_id as $excl) {
1344 $exclude = $exclude ||
($this->entity_id
== $excl);
1356 class smime_message
{
1361 function disposition($name) {
1362 $this->name
= $name;
1363 $this->properties
= array();
1366 function getProperty($par) {
1367 $value = strtolower($par);
1368 if (isset($this->properties
[$par])) {
1369 return $this->properties
[$par];
1376 function language($name) {
1377 $this->name
= $name;
1378 $this->properties
= array();
1382 class content_type
{
1383 var $type0 = 'text',
1387 function content_type($type) {
1388 $pos = strpos($type, '/');
1390 $this->type0
= substr($type, 0, $pos);
1391 $this->type1
= substr($type, $pos+
1);
1393 $this->type0
= $type;
1395 $this->properties
= array();