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() {
558 if (is_object($this->header
->disposition
)) {
559 $filename = $this->header
->disposition
->getproperty('filename');
561 $filename = $this->header
->disposition
->getproperty('name');
565 $filename = 'untitled-'.$this->entity_id
;
571 function addRFC822Header($read) {
572 $header = new rfc822_header();
573 $this->rfc822_header
= $header->parseHeader($read);
576 function getEntity($ent) {
577 $cur_ent = $this->entity_id
;
579 if (($cur_ent == '') ||
($cur_ent == '0')) {
580 $cur_ent_a = array();
582 $cur_ent_a = explode('.', $this->entity_id
);
584 $ent_a = explode('.', $ent);
586 $cnt = count($ent_a);
588 for ($i = 0; $i < $cnt -1; ++
$i) {
589 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
591 $cur_ent_a = explode('.', $msg->entity_id
);
593 } else if (!isset($cur_ent_a[$i])) {
594 if (isset($msg->entities
[($ent_a[$i]-1)])) {
595 $msg = $msg->entities
[($ent_a[$i]-1)];
597 $msg = $msg->entities
[0];
600 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
601 /*this is a header for a message/rfc822 entity */
602 $msg = $msg->entities
[0];
606 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
607 /*this is a header for a message/rfc822 entity */
608 $msg = $msg->entities
[0];
611 if (isset($msg->entities
[($ent_a[$cnt-1])-1])) {
612 if (is_object($msg->entities
[($ent_a[$cnt-1])-1])) {
613 $msg = $msg->entities
[($ent_a[$cnt-1]-1)];
620 function setBody($s) {
621 $this->body_part
= $s;
624 function clean_up() {
626 $msg->body_part
= '';
628 foreach ($msg->entities
as $m) {
633 function getMailbox() {
635 while (is_object($msg->parent
)) {
638 return $msg->mailbox
;
641 function calcEntity($msg) {
642 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
643 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
644 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
645 $ent_no = count($this->entities
)+
1;
646 $par_ent = substr($this->entity_id
, -2);
647 if ($par_ent{0} == '.') {
648 $par_ent = $par_ent{1};
650 if ($par_ent == '0') {
651 $ent_no = count($this->entities
) +
1;
653 $ent = substr($this->entity_id
, 0, strrpos($this->entity_id
, '.'));
654 $ent = ($ent ?
$ent . ".$ent_no" : $ent_no);
655 $msg->entity_id
= $ent;
657 $msg->entity_id
= $ent_no;
660 $ent = $this->entity_id
. ".$ent_no";
661 $msg->entity_id
= $ent;
664 $msg->entity_id
= '0';
667 return $msg->entity_id
;
672 * Bodystructure parser, a recursive function for generating the
673 * entity-tree with all the mime-parts.
675 * It follows RFC2060 and stores all the described fields in the
680 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
683 function parseStructure($read, $i = 0) {
686 $cnt = strlen($read);
688 for (; $i < $cnt; ++
$i) {
689 $char = strtoupper($read{$i});
695 $msg = new message();
696 $hdr = new msg_header();
697 $hdr->type0
= 'text';
698 $hdr->type1
= 'plain';
699 $hdr->encoding
= 'us-ascii';
700 $msg->entity_id
= $this->calcEntity($msg);
702 $msg->header
->type0
= 'multipart';
703 $msg->type0
= 'multipart';
704 while ($read{$i} == '(') {
705 $res = $msg->parseStructure($read, $i);
707 $msg->addEntity($res[0]);
712 /* multipart properties */
714 $res = $this->parseProperties($read, $i);
720 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
722 $res = $msg->parseDisposition($read, $i);
723 } else { /* properties */
724 $res = $msg->parseProperties($read, $i);
731 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
733 $res= $msg->parseLanguage($read, $i);
738 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
739 $msg->header
->type0
= $arg_a[0];
740 $msg->header
->type1
= $arg_a[1];
741 $msg->type0
= $arg_a[0];
742 $msg->type1
= $arg_a[1];
743 $rfc822_hdr = new rfc822_header();
744 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
745 $msg->rfc822_header
= $res[0];
747 while (($i < $cnt) && ($read{$i} != '(')) {
750 $res = $msg->parseStructure($read, $i);
752 $msg->addEntity($res[0]);
757 $res = $msg->parseDisposition($read, $i);
764 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
765 $res = $msg->parseDisposition($read, $i);
767 $res = $msg->parseLanguage($read, $i);
774 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
776 $res = $msg->parseLanguage($read, $i);
780 $i = $msg->parseParenthesis($read, $i);
781 $arg_a[] = ''; /* not yet described in rfc2060 */
786 /* unknown argument, skip this part */
787 $i = $msg->parseParenthesis($read, $i);
794 /* inside an entity -> start processing */
795 $debug = substr($read, $i, 20);
796 $res = $msg->parseQuote($read, $i);
801 $arg_s = strtolower($arg_s); /* type0 and type1 */
807 /* probably NIL argument */
808 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
815 /* process the literal value */
816 $res = $msg->parseLiteral($read, $i);
821 case is_numeric($read{$i}):
822 /* process integers */
823 if ($read{$i} == ' ') { break; }
825 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
832 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
834 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
835 $hdr->type0
= $arg_a[0];
836 $hdr->type1
= $arg_a[1];
838 $msg->type0
= $arg_a[0];
839 $msg->type1
= $arg_a[1];
841 if (is_array($arr)) {
842 $hdr->parameters
= $arg_a[2];
844 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
845 $hdr->description
= $arg_a[4];
846 $hdr->encoding
= strtolower($arg_a[5]);
847 $hdr->entity_id
= $msg->entity_id
;
848 $hdr->size
= $arg_a[6];
850 $hdr->lines
= $arg_a[7];
855 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
856 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
857 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
861 if ((substr($msg->entity_id
, -2) == '.0') && ($msg->type0
!='multipart')) {
865 $hdr->type0
= 'multipart';
866 $hdr->type1
= $arg_a[0];
867 $msg->type0
= 'multipart';
868 $msg->type1
= $arg_a[0];
869 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
870 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
871 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
875 return (array($msg, $i));
880 } /* parsestructure */
882 function parseProperties($read, $i) {
883 $properties = array();
886 for (; $read{$i} != ')'; ++
$i) {
888 if ($read{$i} == '"') {
889 $res = $this->parseQuote($read, $i);
892 } else if ($read{$i} == '{') {
893 $res = $this->parseLiteral($read, $i);
899 if ($prop_name == '') {
900 $prop_name = strtolower($arg_s);
901 $properties[$prop_name] = '';
902 } else if ($prop_name != '') {
903 $properties[$prop_name] = $arg_s;
908 return array($properties, $i);
911 function parseEnvelope($read, $i, $hdr) {
914 $cnt = strlen($read);
916 for (; ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
918 $char = strtoupper($read{$i});
921 $res = $this->parseQuote($read, $i);
927 $res = $this->parseLiteral($read, $i);
933 /* probably NIL argument */
934 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
941 /* Address structure (with group support)
942 * Note: Group support is useless on SMTP connections
943 * because the protocol doesn't support it
948 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
949 if ($read{$i} == '(') {
950 $res = $this->parseAddress($read, $i);
953 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
955 $group = $addr->mailbox
;
958 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
960 if ($a == ($j+
1)) { /* no group members */
961 $group_addr->group
= $group;
962 $group_addr->mailbox
= '';
963 $group_addr->personal
= "$group: Undisclosed recipients;";
964 $addr_a[] = $group_addr;
968 $addr->group
= $group;
980 if (count($arg_a) > 9) {
981 /* argument 1: date */
982 $d = strtr($arg_a[0], array(' ' => ' '));
983 $d = explode(' ', $d);
984 $hdr->date
= getTimeStamp($d);
986 /* argument 2: subject */
987 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
988 $hdr->subject
= $arg_a[1];
990 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
991 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
992 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
993 $hdr->to
= $arg_a[5]; /* argument 6: to */
994 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
995 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
996 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
997 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
999 return (array($hdr, $i));
1002 function parseLiteral($read, $i) {
1004 for (++
$i; $read{$i} != '}'; ++
$i) {
1005 $lit_cnt .= $read{$i};
1008 $lit_cnt +
=2; /* add the { and } characters */
1010 for ($j = 0; $j < $lit_cnt; ++
$j) {
1013 return (array($s, $i));
1016 function parseQuote($read, $i) {
1018 for (++
$i; $read{$i} != '"'; ++
$i) {
1019 if ($read{$i} == '\\') {
1024 return (array($s, $i));
1027 function parseAddress($read, $i) {
1030 for (; $read{$i} != ')'; ++
$i) {
1031 $char = strtoupper($read{$i});
1035 $res = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1041 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1050 if (count($arg_a) == 4) {
1051 $adr = new address_structure();
1052 $adr->personal
= $arg_a[0];
1053 $adr->adl
= $arg_a[1];
1054 $adr->mailbox
= $arg_a[2];
1055 $adr->host
= $arg_a[3];
1059 return (array($adr, $i));
1062 function parseDisposition($read, $i) {
1065 for (; $read{$i} != ')'; ++
$i) {
1066 switch ($read{$i}) {
1070 switch ($read{$i}) {
1071 case '"': $res = $this->parseQuote($read, $i); break;
1072 case '{': $res = $this->parseLiteral($read, $i); break;
1073 case '(': $res = $this->parseProperties($read, $i); break;
1082 if (isset($arg_a[0])) {
1083 $disp = new disposition($arg_a[0]);
1084 if (isset($arg_a[1])) {
1085 $disp->properties
= $arg_a[1];
1089 return (is_object($disp) ?
array($disp, $i) : array('', $i));
1092 function parseLanguage($read, $i) {
1093 /* no idea how to process this one without examples */
1096 for (; $read{$i} != ')'; ++
$i) {
1097 switch ($read{$i}) {
1101 switch ($read{$i}) {
1102 case '"': $res = $this->parseQuote($read, $i); break;
1103 case '{': $res = $this->parseLiteral($read, $i); break;
1104 case '(': $res = $this->parseProperties($read, $i); break;
1113 if (isset($arg_a[0])) {
1114 $lang = new language($arg_a[0]);
1115 if (isset($arg_a[1])) {
1116 $lang->properties
= $arg_a[1];
1120 return (is_object($lang) ?
array($lang, $i) : array('', $i));
1123 function parseParenthesis($read, $i) {
1124 for (; $read{$i} != ')'; ++
$i) {
1125 switch ($read{$i}) {
1129 switch ($read{$i}) {
1130 case '"': $res = $this->parseQuote($read, $i); break;
1131 case '{': $res = $this->parseLiteral($read, $i); break;
1132 case '(': $res = $this->parseProperties($read, $i); break;
1142 /* Function to fill the message structure in case the */
1143 /* bodystructure is not available NOT FINISHED YET */
1144 function parseMessage($read, $type0, $type1) {
1147 $rfc822_header = true;
1148 $mime_header = false;
1151 $rfc822_header = false;
1152 $mime_header = true;
1154 default: return $read;
1157 for ($i = 1; $i < $count; ++
$i) {
1158 $line = trim($body[$i]);
1159 if (($mime_header ||
$rfc822_header) &&
1160 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
1163 $bndreg = str_replace("\\", "\\\\", $bndreg);
1164 $bndreg = str_replace("?", "\\?", $bndreg);
1165 $bndreg = str_replace("+", "\\+", $bndreg);
1166 $bndreg = str_replace(".", "\\.", $bndreg);
1167 $bndreg = str_replace("/", "\\/", $bndreg);
1168 $bndreg = str_replace("-", "\\-", $bndreg);
1169 $bndreg = str_replace("(", "\\(", $bndreg);
1170 $bndreg = str_replace(")", "\\)", $bndreg);
1171 } else if ($rfc822_header && $line == '') {
1172 $rfc822_header = false;
1173 if ($msg->type0
== 'multipart') {
1174 $mime_header = true;
1178 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
1179 $cnt=count($boundaries)-1;
1180 $bnd = $boundaries[$cnt]['bnd'];
1181 $bndreg = $boundaries[$cnt]['bndreg'];
1183 $regstr = '/^--'."($bndreg)".".*".'/';
1184 if (preg_match($regstr, $line, $reg)) {
1185 $bndlen = strlen($reg[1]);
1187 if (strlen($line) > ($bndlen +
3)) {
1188 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
1193 /* calc offset and return $msg */
1194 //$entStr = CalcEntity("$entStr", -1);
1195 array_pop($boundaries);
1196 $mime_header = true;
1199 $mime_header = true;
1201 //$entStr = CalcEntity("$entStr", 0);
1211 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
1213 if ($this->type0
== 'multipart') {
1214 if($this->type1
== 'alternative') {
1215 $msg = $this->findAlternativeEntity($alt_order);
1216 if (count($msg->entities
) == 0) {
1217 $entity[] = $msg->entity_id
;
1219 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1222 } else if ($this->type1
== 'related') { /* RFC 2387 */
1223 $msgs = $this->findRelatedEntity();
1224 foreach ($msgs as $msg) {
1225 if (count($msg->entities
) == 0) {
1226 $entity[] = $msg->entity_id
;
1228 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1231 if (count($msgs) > 0) {
1234 } else { /* Treat as multipart/mixed */
1235 foreach ($this->entities
as $ent) {
1236 if(strtolower($ent->header
->disposition
->name
) != 'attachment' &&
1237 ($ent->type0
!= 'message' && $ent->type1
!= 'rfc822'))
1239 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1244 } else { /* If not multipart, then just compare with each entry from $alt_order */
1245 $type = $this->type0
.'/'.$this->type1
;
1246 foreach ($alt_order as $alt) {
1247 if( ($alt == $type) && isset($this->entity_id
) ) {
1248 if ( (count($this->entities
) == 0) &&
1249 (strtolower($this->header
->disposition
->name
) != 'attachment') )
1251 $entity[] = $this->entity_id
;
1258 foreach ($this->entities
as $ent) {
1259 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
1260 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1261 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1266 if(!$strict && !$found) {
1267 if ($this->type0
== 'text' &&
1268 ($this->type1
== 'plain' ||
1269 $this->type1
== 'html' ||
1270 $this->type1
== 'message') &&
1271 isset($this->entity_id
) )
1273 if (count($this->entities
) == 0) {
1274 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
1275 $entity[] = $this->entity_id
;
1284 function findAlternativeEntity($alt_order) {
1285 /* If we are dealing with alternative parts then we */
1286 /* choose the best viewable message supported by SM. */
1289 $altcount = count($alt_order);
1291 foreach($this->entities
as $ent) {
1292 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1293 if ($type == 'multipart/related') {
1294 $type = $ent->header
->getParameter('type');
1296 for ($j = $best_view; $j < $altcount; ++
$j) {
1297 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1307 function findRelatedEntity() {
1309 $entcount = count($this->entities
);
1311 for ($i = 0; $i < $entcount; ++
$i) {
1312 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1313 if ($this->header
->getParameter('type') == $type) {
1314 $msgs[] = $this->entities
[$i];
1321 function getAttachments($exclude_id=array(), $result = array()) {
1322 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
1323 $this = $this->entities
[0];
1326 if (count($this->entities
)) {
1327 foreach ($this->entities
as $entity) {
1330 foreach ($exclude_id as $excl) {
1331 if ($entity->entity_id
=== $excl) {
1337 if (($entity->type0
== 'multipart') &&
1338 ($entity->type1
!= 'related')) {
1339 $result = $entity->getAttachments($exclude_id, $result);
1340 } else if ($entity->type0
!= 'multipart') {
1341 $result[] = $entity;
1347 foreach ($exclude_id as $excl) {
1348 $exclude = $exclude ||
($this->entity_id
== $excl);
1360 class smime_message
{
1365 function disposition($name) {
1366 $this->name
= $name;
1367 $this->properties
= array();
1370 function getProperty($par) {
1371 $value = strtolower($par);
1372 if (isset($this->properties
[$par])) {
1373 return $this->properties
[$par];
1380 function language($name) {
1381 $this->name
= $name;
1382 $this->properties
= array();
1386 class content_type
{
1387 var $type0 = 'text',
1391 function content_type($type) {
1392 $pos = strpos($type, '/');
1394 $this->type0
= substr($type, 0, $pos);
1395 $this->type1
= substr($type, $pos+
1);
1397 $this->type0
= $type;
1399 $this->properties
= array();