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) {
75 while ($value{$i} != '"') {
76 if ($value{$i} == '\\') {
80 $result .= $value{$i};
81 if (++
$i > $cnt) { break; }
83 $result .= $value{$i};
86 while ($value{$i} != ')') {
87 $i +
= ($value{$i} == '\\' ?
2 : 1);
91 $result .= $value{$i};
98 function parseField($field, $value) {
99 $field = strtolower($field);
102 $d = strtr($value, array(' ' => ' '));
103 $d = explode(' ', $d);
104 $this->date
= getTimeStamp($d);
107 $this->subject
= $value;
110 $this->from
= $this->parseAddress($value,true);
113 $this->sender
= $this->parseAddress($value);
116 $this->reply_to
= $this->parseAddress($value, true);
119 $this->to
= $this->parseAddress($value, true);
122 $this->cc
= $this->parseAddress($value, true);
125 $this->bcc
= $this->parseAddress($value, true);
128 $this->in_reply_to
= $value;
131 $this->message_id
= $value;
133 case 'disposition-notification-to':
134 $this->dnt
= $this->parseAddress($value);
137 $value = str_replace(' ', '', $value);
138 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
141 $this->parseContentType($value);
143 case 'content-disposition':
144 $this->parseDisposition($value);
148 $this->xmailer
= $value;
151 $this->priority
= $value;
154 $this->mlist('post', $value);
157 $this->mlist('reply', $value);
159 case 'list-subscribe':
160 $this->mlist('subscribe', $value);
162 case 'list-unsubscribe':
163 $this->mlist('unsubscribe', $value);
166 $this->mlist('archive', $value);
169 $this->mlist('owner', $value);
172 $this->mlist('help', $value);
175 $this->mlist('id', $value);
182 function parseAddress
183 ($address, $ar=false, $addr_ar = array(), $group = '') {
185 $j = strlen($address);
189 switch ($address{$pos}) {
190 case '"': /* get the personal name */
191 if ($address{++
$pos} == '"') {
194 while ($pos < $j && $address{$pos} != '"') {
195 if ((substr($address, $pos, 2) == '\\"') ||
196 (substr($address, $pos, 2) == '\\\\')) {
197 $name .= $address{$pos++
};
199 $name .= $address{$pos++
};
204 case '<': /* get email address */
205 $addr_start = $pos++
;
206 while ($pos < $j && $address{$pos} != '>') {
207 $addr .= $address{$pos++
};
211 case '(': /* rip off comments */
213 for (++
$pos; $pos < $j && $address{$pos} != ')'; ++
$pos) {
214 $addr .= $address{$pos};
216 $address_start = substr($address, 0, $addr_start);
217 $address_end = substr($address, $pos +
1);
218 $address = $address_start . $address_end;
219 $j = strlen($address);
220 $pos = $addr_start +
1;
222 case ',': /* we reached a delimiter */
224 $addr = substr($address, 0, $pos);
225 } else if ($name == '') {
226 $name = trim(substr($address, 0, $addr_start));
229 $at = strpos($addr, '@');
230 $addr_structure = new address_structure();
231 $addr_structure->personal
= $name;
232 $addr_structure->group
= $group;
234 $addr_structure->mailbox
= substr($addr, 0, $at);
235 $addr_structure->host
= substr($addr, $at+
1);
237 $addr_structure->mailbox
= $addr;
239 $address = trim(substr($address, $pos+
1));
240 $j = strlen($address);
244 $addr_ar[] = $addr_structure;
246 case ':': /* process the group addresses */
248 $group = substr($address, 0, $pos);
249 $address = substr($address, $pos+
1);
250 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
251 $addr_ar = $result[0];
253 $address = substr($address, $pos++
);
254 $j = strlen($address);
259 $address = substr($address, 0, $pos - 1);
269 $addr = substr($address, 0, $pos);
270 } else if ($name == '') {
271 $name = trim(substr($address, 0, $addr_start));
273 $at = strpos($addr, '@');
274 $addr_structure = new address_structure();
275 $addr_structure->group
= $group;
277 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
278 $addr_structure->host
= trim(substr($addr, $at+
1));
280 $addr_structure->mailbox
= trim($addr);
282 if ($group && $addr == '') { /* no addresses found in group */
283 $name = "$group: Undisclosed recipients;";
284 $addr_structure->personal
= $name;
285 $addr_ar[] = $addr_structure;
286 return (array($addr_ar, $pos+
1));
288 $addr_structure->personal
= $name;
289 if ($name ||
$addr) {
290 $addr_ar[] = $addr_structure;
296 return ($addr_ar[0]);
299 function parseContentType($value) {
300 $pos = strpos($value, ';');
303 $type = trim(substr($value, 0, $pos));
304 $props = trim(substr($type, $pos+
1));
308 $content_type = new content_type($type);
310 $properties = $this->parseProperties($props);
311 if (!isset($properties['charset'])) {
312 $properties['charset'] = 'us-ascii';
314 $content_type->properties
= $this->parseProperties($props);
316 $this->content_type
= $content_type;
319 function parseProperties($value) {
320 $propArray = explode(';', $value);
321 $propResultArray = array();
322 foreach ($propArray as $prop) {
324 $pos = strpos($prop, '=');
326 $key = trim(substr($prop, 0, $pos));
327 $val = trim(substr($prop, $pos+
1));
328 if ($val{0} == '"') {
329 $val = substr($val, 1, -1);
331 $propResultArray[$key] = $val;
334 return $propResultArray;
337 function parseDisposition($value) {
338 $pos = strpos($value, ';');
341 $name = trim(substr($value, 0, $pos));
342 $props = trim(substr($type, $pos+
1));
346 $props_a = $this->parseProperties($props);
347 $disp = new disposition($name);
348 $disp->properties
= $props_a;
349 $this->disposition
= $disp;
352 function mlist($field, $value) {
354 $value_a = explode(',', $value);
355 foreach ($value_a as $val) {
357 if ($val{0} == '<') {
358 $val = substr($val, 1, -1);
360 if (substr($val, 0, 7) == 'mailto:') {
361 $res_a['mailto'] = substr($val, 7);
363 $res_a['href'] = $val;
366 $this->mlist
[$field] = $res_a;
370 * function to get the addres strings out of the header.
371 * Arguments: string or array of strings !
372 * example1: header->getAddr_s('to').
373 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
375 function getAddr_s($arr, $separator = ',') {
378 if (is_array($arr)) {
379 foreach($arr as $arg) {
380 if ($this->getAddr_s($arg)) {
381 $s .= $separator . $result;
384 $s = ($s ?
substr($s, 2) : $s);
386 eval('$addr = $this->' . $arr . ';') ;
387 if (is_array($addr)) {
388 foreach ($addr as $addr_o) {
389 if (is_object($addr_o)) {
390 $s .= $addr_o->getAddress() . $separator;
393 $s = substr($s, 0, -strlen($separator));
395 if (is_object($addr)) {
396 $s .= $addr->getAddress();
404 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
405 if (is_array($arg)) {
406 foreach($arg as $argument) {
407 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
410 eval('$addr = $this->' . $arg . ';') ;
411 if (is_array($addr)) {
412 foreach ($addr as $next_addr) {
413 if (is_object($next_addr)) {
414 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
415 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
417 $email = $next_addr->mailbox
;
419 $email = strtolower($email);
420 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
421 $arr[$email] = $next_addr->personal
;
426 if (is_object($addr)) {
427 $email = $addr->mailbox
;
428 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
429 $email = strtolower($email);
430 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
431 $arr[$email] = $addr->personal
;
439 function getContentType($type0, $type1) {
440 $type0 = $this->content_type
->type0
;
441 $type1 = $this->content_type
->type1
;
442 return $this->content_type
->properties
;
447 /** msg_header contains all variables available in a bodystructure **/
448 /** entity like described in rfc2060 **/
452 $parameters = array(),
462 * returns addres_list of supplied argument
463 * arguments: array('to', 'from', ...) or just a string like 'to'.
464 * result: string: address1, addres2, ....
467 function setVar($var, $value) {
468 $this->{$var} = $value;
471 function getParameter($p) {
472 $value = strtolower($p);
473 return (isset($this->parameters
[$p]) ?
$this->parameters
[$p] : '');
476 function setParameter($parameter, $value) {
477 $this->parameters
[strtolower($parameter)] = $value;
483 class address_structure
{
490 function getAddress($full = true) {
493 if (is_object($this)) {
494 if (isset($this->host
) &&( $this->host
!= '')) {
495 $email = $this->mailbox
.'@'.$this->host
;
497 $email = $this->mailbox
;
499 if (trim($this->personal
) != '') {
501 $addr = '"' . $this->personal
. '" <' .$email.'>';
503 $addr = $this->personal
;
505 $best_dpl = $this->personal
;
510 $result = ($full ?
$addr : $best_dpl);
518 /** message is the object that contains messages. It is a recursive
519 object in that through the $entities variable, it can contain
520 more objects of type message. See documentation in mime.txt for
521 a better description of how this works.
523 var $rfc822_header = '',
529 $parent_ent, $entity,
530 $parent = '', $decoded_body='',
531 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
534 $offset = 0, /* for fetching body parts out of raw messages */
535 $length = 0; /* for fetching body parts out of raw messages */
537 function setEnt($ent) {
538 $this->entity_id
= $ent;
541 function addEntity ($msg) {
542 $msg->parent
= &$this;
543 $this->entities
[] = $msg;
546 function getFilename() {
549 if (is_object($this->header
->disposition
)) {
550 $filename = $this->header
->disposition
->getproperty('filename');
552 $filename = $this->header
->disposition
->getproperty('name');
557 $filename = 'untitled-'.$this->entity_id
;
564 function addRFC822Header($read) {
565 $header = new rfc822_header();
566 $this->rfc822_header
= $header->parseHeader($read);
569 function getEntity($ent) {
570 $cur_ent = $this->entity_id
;
572 if (($cur_ent == '') ||
( $cur_ent == '0')) {
573 $cur_ent_a = array();
575 $cur_ent_a = explode('.', $this->entity_id
);
577 $ent_a = explode('.', $ent);
579 $cnt = count($ent_a);
581 for ($i = 0; $i < $cnt -1; ++
$i) {
582 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
584 $cur_ent_a = explode('.', $msg->entity_id
);
586 } else if (!isset($cur_ent_a[$i])) {
587 if (isset($msg->entities
[($ent_a[$i]-1)])) {
588 $msg = $msg->entities
[($ent_a[$i]-1)];
590 $msg = $msg->entities
[0];
593 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
594 /*this is a header for a message/rfc822 entity */
595 $msg = $msg->entities
[0];
599 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
600 /*this is a header for a message/rfc822 entity */
601 $msg = $msg->entities
[0];
604 if (isset($msg->entities
[($ent_a[$cnt-1])-1])) {
605 if (is_object($msg->entities
[($ent_a[$cnt-1])-1])) {
606 $msg = $msg->entities
[($ent_a[$cnt-1]-1)];
613 function setBody($s) {
614 $this->body_part
= $s;
617 function clean_up() {
619 $msg->body_part
= '';
621 for ($i = 0; isset($msg->entities
[$i]); ++
$i) {
622 $msg->entities
[$i]->clean_up();
626 function getMailbox() {
628 while (is_object($msg->parent
)) {
631 return $msg->mailbox
;
634 function calcEntity($msg) {
635 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
636 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
637 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
638 $ent_no = count($this->entities
)+
1;
639 $par_ent = substr($this->entity_id
, -2);
640 if ($par_ent{0} == '.') {
641 $par_ent = $par_ent{1};
643 if ($par_ent == '0') {
644 $ent_no = count($this->entities
) +
1;
646 $ent = substr($this->entity_id
, 0,strrpos($this->entity_id
, '.'));
647 $ent = ($ent ?
$ent . ".$ent_no" : $ent_no);
648 $msg->entity_id
= $ent;
650 $msg->entity_id
= $ent_no;
653 $ent = $this->entity_id
. ".$ent_no";
654 $msg->entity_id
= $ent;
657 $msg->entity_id
= '0';
660 return $msg->entity_id
;
665 * Bodystructure parser, a recursive function for generating the
666 * entity-tree with all the mime-parts.
668 * It follows RFC2060 and stores all the described fields in the
673 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
676 function parseStructure($read, $i = 0) {
679 $cnt = strlen($read);
681 for (; $i < $cnt; ++
$i) {
682 $char = strtoupper($read{$i});
687 $msg = new message();
688 $hdr = new msg_header();
689 $hdr->type0
= 'text';
690 $hdr->type1
= 'plain';
691 $hdr->encoding
= 'us-ascii';
692 $msg->entity_id
= $this->calcEntity($msg);
694 $msg->header
->type0
= 'multipart';
695 $msg->type0
= 'multipart';
696 while ($read{$i} == '(') {
697 $res = $msg->parseStructure($read, $i);
699 $msg->addEntity($res[0]);
705 /* multipart properties */
707 $res = $this->parseProperties($read, $i);
713 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
715 $res = $msg->parseDisposition($read, $i);
718 } else { /* properties */
719 $res = $msg->parseProperties($read, $i);
726 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
728 $res= $msg->parseLanguage($read, $i);
733 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
734 $msg->header
->type0
= $arg_a[0];
735 $msg->type0
= $arg_a[0];
736 $msg->header
->type1
= $arg_a[1];
737 $msg->type1
= $arg_a[1];
738 $rfc822_hdr = new rfc822_header();
739 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
741 $msg->rfc822_header
= $res[0];
742 while (($i < $cnt) && ($read{$i} != '(')) {
745 $res = $msg->parseStructure($read, $i);
747 $msg->addEntity($res[0]);
752 $res = $msg->parseDisposition($read, $i);
759 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
760 $res = $msg->parseDisposition($read, $i);
762 $res = $msg->parseLanguage($read, $i);
769 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
771 $res = $msg->parseLanguage($read, $i);
775 $i = $msg->parseParenthesis($read, $i);
776 $arg_a[] = ''; /* not yet desribed in rfc2060 */
781 /* unknown argument, skip this part */
782 $i = $msg->parseParenthesis($read, $i);
790 /* inside an entity -> start processing */
791 $debug = substr($read, $i, 20);
792 $res = $msg->parseQuote($read, $i);
797 $arg_s = strtolower($arg_s); /* type0 and type1 */
803 /* probably NIL argument */
804 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
811 /* process the literal value */
812 $res = $msg->parseLiteral($read, $i);
817 case is_numeric($read{$i}):
818 /* process integers */
819 if ($read{$i} == ' ') { break; }
821 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
828 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
830 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
831 $hdr->type0
= $arg_a[0];
832 $hdr->type1
= $arg_a[1];
834 $msg->type0
= $arg_a[0];
835 $msg->type1
= $arg_a[1];
838 if (is_array($arr)) {
839 $hdr->parameters
= $arg_a[2];
841 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
842 $hdr->description
= $arg_a[4];
843 $hdr->encoding
= strtolower($arg_a[5]);
844 $hdr->entity_id
= $msg->entity_id
;
845 $hdr->size
= $arg_a[6];
847 $hdr->lines
= $arg_a[7];
852 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
853 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
854 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
858 if ((substr($msg->entity_id
, -2) == '.0') && ($msg->type0
!='multipart')) {
862 $hdr->type0
= 'multipart';
863 $hdr->type1
= $arg_a[0];
864 $msg->type0
= 'multipart';
865 $msg->type1
= $arg_a[0];
866 $hdr->parameters
= (is_array($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
867 $hdr->disposition
= (is_array($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
868 $hdr->language
= (is_array($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
871 return (array($msg, $i));
876 } /* parsestructure */
878 function parseProperties($read, $i) {
879 $properties = array();
883 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);
894 if (($prop_name == '') && $arg_s) {
895 $prop_name = strtolower($arg_s);
896 $properties[$prop_name] = '';
898 } else if (($prop_name != '') && ($arg_s != '')) {
899 $properties[$prop_name] = $arg_s;
905 return (array($properties, $i));
908 function parseEnvelope($read, $i, $hdr) {
911 $cnt = strlen($read);
913 for (; ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
915 $char = strtoupper($read{$i});
918 $res = $this->parseQuote($read, $i);
924 $res = $this->parseLiteral($read, $i);
930 /* probably NIL argument */
931 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
938 /* Address structure (with group support)
939 * Note: Group support is useless on SMTP connections
940 * because the protocol doesn't support it
945 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
946 if ($read{$i} == '(') {
947 $res = $this->parseAddress($read, $i);
950 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
952 $group = $addr->mailbox
;
955 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
957 if ($a == ($j+
1)) { /* no group members */
958 $group_addr->group
= $group;
959 $group_addr->mailbox
= '';
960 $group_addr->personal
= "$group: Undisclosed recipients;";
961 $addr_a[] = $group_addr;
965 $addr->group
= $group;
977 if (count($arg_a) > 9) {
978 /* argument 1: date */
979 $d = strtr($arg_a[0], array(' ' => ' '));
980 $d = explode(' ', $d);
981 $hdr->date
= getTimeStamp($d);
983 /* argument 2: subject */
984 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
985 $hdr->subject
= $arg_a[1];
987 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
988 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
989 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
990 $hdr->to
= $arg_a[5]; /* argument 6: to */
991 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
992 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
993 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
994 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
996 return (array($hdr, $i));
999 function parseLiteral($read, $i) {
1001 for (++
$i; $read{$i} != '}'; ++
$i) {
1002 $lit_cnt .= $read{$i};
1005 $lit_cnt +
=2; /* add the { and } characters */
1007 for ($j = 0; $j < $lit_cnt; ++
$j) {
1010 return (array($s, $i));
1013 function parseQuote($read, $i) {
1015 for (++
$i; $read{$i} != '"'; ++
$i) {
1016 if ($read{$i} == '\\') {
1021 return (array($s, $i));
1024 function parseAddress($read, $i) {
1027 for (; $read{$i} != ')'; ++
$i) {
1028 $char = strtoupper($read{$i});
1032 $res = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1038 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1047 if (count($arg_a) == 4) {
1048 $adr = new address_structure();
1049 $adr->personal
= $arg_a[0];
1050 $adr->adl
= $arg_a[1];
1051 $adr->mailbox
= $arg_a[2];
1052 $adr->host
= $arg_a[3];
1056 return (array($adr, $i));
1059 function parseDisposition($read, $i) {
1062 for (; $read{$i} != ')'; ++
$i) {
1063 switch ($read{$i}) {
1067 switch ($read{$i}) {
1068 case '"': $res = $this->parseQuote($read, $i); break;
1069 case '{': $res = $this->parseLiteral($read, $i); break;
1070 case '(': $res = $this->parseProperties($read, $i); break;
1079 if (isset($arg_a[0])) {
1080 $disp = new disposition($arg_a[0]);
1081 if (isset($arg_a[1])) {
1082 $disp->properties
= $arg_a[1];
1086 return (is_object($disp) ?
array($disp, $i) : array('', $i));
1089 function parseLanguage($read, $i) {
1090 /* no idea how to process this one without examples */
1093 for (; $read{$i} != ')'; ++
$i) {
1094 switch ($read{$i}) {
1098 switch ($read{$i}) {
1099 case '"': $res = $this->parseQuote($read, $i); break;
1100 case '{': $res = $this->parseLiteral($read, $i); break;
1101 case '(': $res = $this->parseProperties($read, $i); break;
1110 if (isset($arg_a[0])) {
1111 $lang = new language($arg_a[0]);
1112 if (isset($arg_a[1])) {
1113 $lang->properties
= $arg_a[1];
1117 return (is_object($lang) ?
array($lang, $i) : array('', $i));
1120 function parseParenthesis($read, $i) {
1121 for (; $read{$i} != ')'; ++
$i) {
1122 switch ($read{$i}) {
1126 switch ($read{$i}) {
1127 case '"': $res = $this->parseQuote($read, $i); break;
1128 case '{': $res = $this->parseLiteral($read, $i); break;
1129 case '(': $res = $this->parseProperties($read, $i); break;
1139 /* Function to fill the message structure in case the */
1140 /* bodystructure is not available NOT FINISHED YET */
1141 function parseMessage($read, $type0, $type1) {
1144 $rfc822_header = true;
1145 $mime_header = false;
1148 $rfc822_header = false;
1149 $mime_header = true;
1151 default: return $read;
1154 for ($i = 1; $i < $count; ++
$i) {
1155 $line = trim($body[$i]);
1156 if (($mime_header ||
$rfc822_header) &&
1157 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
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 $bndreg = str_replace(")", "\\)", $bndreg);
1168 } else if ($rfc822_header && $line == '') {
1169 $rfc822_header = false;
1170 if ($msg->type0
== 'multipart') {
1171 $mime_header = true;
1175 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
1176 $cnt=count($boundaries)-1;
1177 $bnd = $boundaries[$cnt]['bnd'];
1178 $bndreg = $boundaries[$cnt]['bndreg'];
1180 $regstr = '/^--'."($bndreg)".".*".'/';
1181 if (preg_match($regstr, $line, $reg)) {
1182 $bndlen = strlen($reg[1]);
1184 if (strlen($line) > ($bndlen +
3)) {
1185 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
1190 /* calc offset and return $msg */
1191 //$entStr = CalcEntity("$entStr", -1);
1192 array_pop($boundaries);
1193 $mime_header = true;
1196 $mime_header = true;
1198 //$entStr = CalcEntity("$entStr", 0);
1208 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html')) {
1210 $type = $this->type0
.'/'.$this->type1
;
1211 if ($type == 'multipart/alternative') {
1212 $msg = $this->findAlternativeEntity($alt_order);
1213 if (count($msg->entities
) == 0) {
1214 $entity[] = $msg->entity_id
;
1216 $entity = $msg->findDisplayEntity($entity, $alt_order);
1219 } else if ($type == 'multipart/related') {
1220 $msgs = $this->findRelatedEntity();
1221 foreach ($msgs as $msg) {
1222 if (count($msg->entities
) == 0) {
1223 $entity[] = $msg->entity_id
;
1225 $entity = $msg->findDisplayEntity($entity, $alt_order);
1228 if (count($msgs) > 0) {
1231 } else if (($this->type0
== 'text') &&
1232 (($this->type1
== 'plain') ||
1233 ($this->type1
== 'html') ||
1234 ($this->type1
== 'message')) &&
1235 isset($this->entity_id
)) {
1236 if (count($this->entities
) == 0) {
1237 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
1238 $entity[] = $this->entity_id
;
1244 foreach ($this->entities
as $ent) {
1245 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
1246 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1247 $entity = $ent->findDisplayEntity($entity, $alt_order);
1252 /* CAN THIS BE REMOVED? IF SO, DO IT!!! */
1253 //while (isset($this->entities[$i]) && !$found &&
1254 // (strtolower($this->entities[$i]->header->disposition->name)
1255 // != 'attachment') &&
1256 // ($this->entities[$i]->type0 != 'message' &&
1257 // $this->entities[$i]->type1 != 'rfc822')
1259 // $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order);
1266 function findAlternativeEntity($alt_order) {
1267 /* If we are dealing with alternative parts then we */
1268 /* choose the best viewable message supported by SM. */
1271 $altcount = count($alt_order);
1273 foreach($this->entities
as $ent) {
1274 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1275 if ($type == 'multipart/related') {
1276 $type = $ent->header
->getParameter('type');
1278 for ($j = $best_view; $j < $altcount; ++
$j) {
1279 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1289 function findRelatedEntity() {
1291 $entcount = count($this->entities
);
1293 for ($i = 0; $i < $entcount; ++
$i) {
1294 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1295 if ($this->header
->getParameter('type') == $type) {
1296 $msgs[] = $this->entities
[$i];
1303 function getAttachments($exclude_id=array(), $result = array()) {
1304 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
1305 $this = $this->entities
[0];
1308 if (count($this->entities
)) {
1309 foreach ($this->entities
as $entity) {
1312 foreach ($exclude_id as $excl) {
1313 if ($entity->entity_id
=== $excl) {
1319 if (($entity->type0
== 'multipart') &&
1320 ($entity->type1
!= 'related')) {
1321 $result = $entity->getAttachments($exclude_id, $result);
1322 } else if ($entity->type0
!= 'multipart') {
1323 $result[] = $entity;
1329 foreach ($exclude_id as $excl) {
1330 $exclude = $exclude ||
($this->entity_id
== $excl);
1342 class smime_message
{
1347 function disposition($name) {
1348 $this->name
= $name;
1349 $this->properties
= array();
1352 function getProperty($par) {
1353 $value = strtolower($par);
1354 if (isset($this->properties
[$par])) {
1355 return $this->properties
[$par];
1362 function language($name) {
1363 $this->name
= $name;
1364 $this->properties
= array();
1368 class content_type
{
1369 var $type0 = 'text',
1373 function content_type($type) {
1374 $pos = strpos($type, '/');
1376 $this->type0
= substr($type, 0, $pos);
1377 $this->type1
= substr($type, $pos+
1);
1379 $this->type0
= $type;
1381 $this->properties
= array();