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});
694 $msg = new message();
695 $hdr = new msg_header();
696 $hdr->type0
= 'text';
697 $hdr->type1
= 'plain';
698 $hdr->encoding
= 'us-ascii';
699 $msg->entity_id
= $this->calcEntity($msg);
701 $msg->header
->type0
= 'multipart';
702 $msg->type0
= 'multipart';
703 while ($read{$i} == '(') {
704 $res = $msg->parseStructure($read, $i);
706 $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);
725 } else { /* properties */
726 $res = $msg->parseProperties($read, $i);
733 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
735 $res= $msg->parseLanguage($read, $i);
740 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
741 $msg->header
->type0
= $arg_a[0];
742 $msg->type0
= $arg_a[0];
743 $msg->header
->type1
= $arg_a[1];
744 $msg->type1
= $arg_a[1];
745 $rfc822_hdr = new rfc822_header();
746 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
748 $msg->rfc822_header
= $res[0];
749 while (($i < $cnt) && ($read{$i} != '(')) {
752 $res = $msg->parseStructure($read, $i);
754 $msg->addEntity($res[0]);
759 $res = $msg->parseDisposition($read, $i);
766 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
767 $res = $msg->parseDisposition($read, $i);
769 $res = $msg->parseLanguage($read, $i);
776 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
778 $res = $msg->parseLanguage($read, $i);
782 $i = $msg->parseParenthesis($read, $i);
783 $arg_a[] = ''; /* not yet desribed in rfc2060 */
788 /* unknown argument, skip this part */
789 $i = $msg->parseParenthesis($read, $i);
797 /* inside an entity -> start processing */
798 $debug = substr($read, $i, 20);
799 $res = $msg->parseQuote($read, $i);
804 $arg_s = strtolower($arg_s); /* type0 and type1 */
810 /* probably NIL argument */
811 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
818 /* process the literal value */
819 $res = $msg->parseLiteral($read, $i);
824 case is_numeric($read{$i}):
825 /* process integers */
826 if ($read{$i} == ' ') { break; }
828 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
835 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
837 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
838 $hdr->type0
= $arg_a[0];
839 $hdr->type1
= $arg_a[1];
841 $msg->type0
= $arg_a[0];
842 $msg->type1
= $arg_a[1];
845 if (is_array($arr)) {
846 $hdr->parameters
= $arg_a[2];
848 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
849 $hdr->description
= $arg_a[4];
850 $hdr->encoding
= strtolower($arg_a[5]);
851 $hdr->entity_id
= $msg->entity_id
;
852 $hdr->size
= $arg_a[6];
854 $hdr->lines
= $arg_a[7];
859 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
860 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
861 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
865 if ((substr($msg->entity_id
, -2) == '.0') && ($msg->type0
!='multipart')) {
869 $hdr->type0
= 'multipart';
870 $hdr->type1
= $arg_a[0];
871 $msg->type0
= 'multipart';
872 $msg->type1
= $arg_a[0];
873 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
874 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
875 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
878 return (array($msg, $i));
883 } /* parsestructure */
885 function parseProperties($read, $i) {
886 $properties = array();
890 for (; $read{$i} != ')'; ++
$i) {
891 if ($read{$i} == '"') {
892 $res = $this->parseQuote($read, $i);
895 } else if ($read{$i} == '{') {
896 $res = $this->parseLiteral($read, $i);
901 if (($prop_name == '') && $arg_s) {
902 $prop_name = strtolower($arg_s);
903 $properties[$prop_name] = '';
905 } else if (($prop_name != '') && ($arg_s != '')) {
906 $properties[$prop_name] = $arg_s;
912 return (array($properties, $i));
915 function parseEnvelope($read, $i, $hdr) {
918 $cnt = strlen($read);
920 for (; ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
922 $char = strtoupper($read{$i});
925 $res = $this->parseQuote($read, $i);
931 $res = $this->parseLiteral($read, $i);
937 /* probably NIL argument */
938 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
945 /* Address structure (with group support)
946 * Note: Group support is useless on SMTP connections
947 * because the protocol doesn't support it
952 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
953 if ($read{$i} == '(') {
954 $res = $this->parseAddress($read, $i);
957 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
959 $group = $addr->mailbox
;
962 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
964 if ($a == ($j+
1)) { /* no group members */
965 $group_addr->group
= $group;
966 $group_addr->mailbox
= '';
967 $group_addr->personal
= "$group: Undisclosed recipients;";
968 $addr_a[] = $group_addr;
972 $addr->group
= $group;
984 if (count($arg_a) > 9) {
985 /* argument 1: date */
986 $d = strtr($arg_a[0], array(' ' => ' '));
987 $d = explode(' ', $d);
988 $hdr->date
= getTimeStamp($d);
990 /* argument 2: subject */
991 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
992 $hdr->subject
= $arg_a[1];
994 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
995 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
996 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
997 $hdr->to
= $arg_a[5]; /* argument 6: to */
998 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
999 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
1000 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
1001 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
1003 return (array($hdr, $i));
1006 function parseLiteral($read, $i) {
1008 for (++
$i; $read{$i} != '}'; ++
$i) {
1009 $lit_cnt .= $read{$i};
1012 $lit_cnt +
=2; /* add the { and } characters */
1014 for ($j = 0; $j < $lit_cnt; ++
$j) {
1017 return (array($s, $i));
1020 function parseQuote($read, $i) {
1022 for (++
$i; $read{$i} != '"'; ++
$i) {
1023 if ($read{$i} == '\\') {
1028 return (array($s, $i));
1031 function parseAddress($read, $i) {
1034 for (; $read{$i} != ')'; ++
$i) {
1035 $char = strtoupper($read{$i});
1039 $res = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1045 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1054 if (count($arg_a) == 4) {
1055 $adr = new address_structure();
1056 $adr->personal
= $arg_a[0];
1057 $adr->adl
= $arg_a[1];
1058 $adr->mailbox
= $arg_a[2];
1059 $adr->host
= $arg_a[3];
1063 return (array($adr, $i));
1066 function parseDisposition($read, $i) {
1069 for (; $read{$i} != ')'; ++
$i) {
1070 switch ($read{$i}) {
1074 switch ($read{$i}) {
1075 case '"': $res = $this->parseQuote($read, $i); break;
1076 case '{': $res = $this->parseLiteral($read, $i); break;
1077 case '(': $res = $this->parseProperties($read, $i); break;
1086 if (isset($arg_a[0])) {
1087 $disp = new disposition($arg_a[0]);
1088 if (isset($arg_a[1])) {
1089 $disp->properties
= $arg_a[1];
1093 return (is_object($disp) ?
array($disp, $i) : array('', $i));
1096 function parseLanguage($read, $i) {
1097 /* no idea how to process this one without examples */
1100 for (; $read{$i} != ')'; ++
$i) {
1101 switch ($read{$i}) {
1105 switch ($read{$i}) {
1106 case '"': $res = $this->parseQuote($read, $i); break;
1107 case '{': $res = $this->parseLiteral($read, $i); break;
1108 case '(': $res = $this->parseProperties($read, $i); break;
1117 if (isset($arg_a[0])) {
1118 $lang = new language($arg_a[0]);
1119 if (isset($arg_a[1])) {
1120 $lang->properties
= $arg_a[1];
1124 return (is_object($lang) ?
array($lang, $i) : array('', $i));
1127 function parseParenthesis($read, $i) {
1128 for (; $read{$i} != ')'; ++
$i) {
1129 switch ($read{$i}) {
1133 switch ($read{$i}) {
1134 case '"': $res = $this->parseQuote($read, $i); break;
1135 case '{': $res = $this->parseLiteral($read, $i); break;
1136 case '(': $res = $this->parseProperties($read, $i); break;
1146 /* Function to fill the message structure in case the */
1147 /* bodystructure is not available NOT FINISHED YET */
1148 function parseMessage($read, $type0, $type1) {
1151 $rfc822_header = true;
1152 $mime_header = false;
1155 $rfc822_header = false;
1156 $mime_header = true;
1158 default: return $read;
1161 for ($i = 1; $i < $count; ++
$i) {
1162 $line = trim($body[$i]);
1163 if (($mime_header ||
$rfc822_header) &&
1164 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
1167 $bndreg = str_replace("\\", "\\\\", $bndreg);
1168 $bndreg = str_replace("?", "\\?", $bndreg);
1169 $bndreg = str_replace("+", "\\+", $bndreg);
1170 $bndreg = str_replace(".", "\\.", $bndreg);
1171 $bndreg = str_replace("/", "\\/", $bndreg);
1172 $bndreg = str_replace("-", "\\-", $bndreg);
1173 $bndreg = str_replace("(", "\\(", $bndreg);
1174 $bndreg = str_replace(")", "\\)", $bndreg);
1175 } else if ($rfc822_header && $line == '') {
1176 $rfc822_header = false;
1177 if ($msg->type0
== 'multipart') {
1178 $mime_header = true;
1182 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
1183 $cnt=count($boundaries)-1;
1184 $bnd = $boundaries[$cnt]['bnd'];
1185 $bndreg = $boundaries[$cnt]['bndreg'];
1187 $regstr = '/^--'."($bndreg)".".*".'/';
1188 if (preg_match($regstr, $line, $reg)) {
1189 $bndlen = strlen($reg[1]);
1191 if (strlen($line) > ($bndlen +
3)) {
1192 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
1197 /* calc offset and return $msg */
1198 //$entStr = CalcEntity("$entStr", -1);
1199 array_pop($boundaries);
1200 $mime_header = true;
1203 $mime_header = true;
1205 //$entStr = CalcEntity("$entStr", 0);
1215 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
1217 if ($this->type0
== 'multipart') {
1218 if($this->type1
== 'alternative') {
1219 $msg = $this->findAlternativeEntity($alt_order);
1220 if (count($msg->entities
) == 0) {
1221 $entity[] = $msg->entity_id
;
1223 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1226 } else if ($this->type1
== 'related') { /* RFC 2387 */
1227 $msgs = $this->findRelatedEntity();
1228 foreach ($msgs as $msg) {
1229 if (count($msg->entities
) == 0) {
1230 $entity[] = $msg->entity_id
;
1232 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1235 if (count($msgs) > 0) {
1238 } else { /* Treat as multipart/mixed */
1239 foreach ($this->entities
as $ent) {
1240 if(strtolower($ent->header
->disposition
->name
) != 'attachment' &&
1241 ($ent->type0
!= 'message' && $ent->type1
!= 'rfc822'))
1243 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1248 } else { /* If not multipart, then just compare with each entry from $alt_order */
1249 $type = $this->type0
.'/'.$this->type1
;
1250 foreach ($alt_order as $alt) {
1251 if( ($alt == $type) && isset($this->entity_id
) ) {
1252 if ( (count($this->entities
) == 0) &&
1253 (strtolower($this->header
->disposition
->name
) != 'attachment') )
1255 $entity[] = $this->entity_id
;
1262 foreach ($this->entities
as $ent) {
1263 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
1264 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1265 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1270 if(!$strict && !$found) {
1271 if ($this->type0
== 'text' &&
1272 ($this->type1
== 'plain' ||
1273 $this->type1
== 'html' ||
1274 $this->type1
== 'message') &&
1275 isset($this->entity_id
) )
1277 if (count($this->entities
) == 0) {
1278 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
1279 $entity[] = $this->entity_id
;
1288 function findAlternativeEntity($alt_order) {
1289 /* If we are dealing with alternative parts then we */
1290 /* choose the best viewable message supported by SM. */
1293 $altcount = count($alt_order);
1295 foreach($this->entities
as $ent) {
1296 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1297 if ($type == 'multipart/related') {
1298 $type = $ent->header
->getParameter('type');
1300 for ($j = $best_view; $j < $altcount; ++
$j) {
1301 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1311 function findRelatedEntity() {
1313 $entcount = count($this->entities
);
1315 for ($i = 0; $i < $entcount; ++
$i) {
1316 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1317 if ($this->header
->getParameter('type') == $type) {
1318 $msgs[] = $this->entities
[$i];
1325 function getAttachments($exclude_id=array(), $result = array()) {
1326 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
1327 $this = $this->entities
[0];
1330 if (count($this->entities
)) {
1331 foreach ($this->entities
as $entity) {
1334 foreach ($exclude_id as $excl) {
1335 if ($entity->entity_id
=== $excl) {
1341 if (($entity->type0
== 'multipart') &&
1342 ($entity->type1
!= 'related')) {
1343 $result = $entity->getAttachments($exclude_id, $result);
1344 } else if ($entity->type0
!= 'multipart') {
1345 $result[] = $entity;
1351 foreach ($exclude_id as $excl) {
1352 $exclude = $exclude ||
($this->entity_id
== $excl);
1364 class smime_message
{
1369 function disposition($name) {
1370 $this->name
= $name;
1371 $this->properties
= array();
1374 function getProperty($par) {
1375 $value = strtolower($par);
1376 if (isset($this->properties
[$par])) {
1377 return $this->properties
[$par];
1384 function language($name) {
1385 $this->name
= $name;
1386 $this->properties
= array();
1390 class content_type
{
1391 var $type0 = 'text',
1395 function content_type($type) {
1396 $pos = strpos($type, '/');
1398 $this->type0
= substr($type, 0, $pos);
1399 $this->type1
= substr($type, $pos+
1);
1401 $this->type0
= $type;
1403 $this->properties
= array();