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} == '\\s') {
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;
297 return ($addr_ar[0]);
300 function parseContentType($value) {
301 $pos = strpos($value, ';');
304 $type = trim(substr($value, 0, $pos));
305 $props = trim(substr($type, $pos+
1));
309 $content_type = new content_type($type);
311 $properties = $this->parseProperties($props);
312 if (!isset($properties['charset'])) {
313 $properties['charset'] = 'us-ascii';
315 $content_type->properties
= $this->parseProperties($props);
317 $this->content_type
= $content_type;
320 function parseProperties($value) {
321 $propArray = explode(';', $value);
322 $propResultArray = array();
323 foreach ($propArray as $prop) {
325 $pos = strpos($prop, '=');
327 $key = trim(substr($prop, 0, $pos));
328 $val = trim(substr($prop, $pos+
1));
329 if ($val{0} == '"') {
330 $val = substr($val, 1, -1);
332 $propResultArray[$key] = $val;
335 return $propResultArray;
338 function parseDisposition($value) {
339 $pos = strpos($value, ';');
342 $name = trim(substr($value, 0, $pos));
343 $props = trim(substr($type, $pos+
1));
347 $props_a = $this->parseProperties($props);
348 $disp = new disposition($name);
349 $disp->properties
= $props_a;
350 $this->disposition
= $disp;
353 function mlist($field, $value) {
355 $value_a = explode(',', $value);
356 foreach ($value_a as $val) {
358 if ($val{0} == '<') {
359 $val = substr($val, 1, -1);
361 if (substr($val, 0, 7) == 'mailto:') {
362 $res_a['mailto'] = substr($val, 7);
364 $res_a['href'] = $val;
367 $this->mlist
[$field] = $res_a;
371 * function to get the addres strings out of the header.
372 * Arguments: string or array of strings !
373 * example1: header->getAddr_s('to').
374 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
376 function getAddr_s($arr, $separator = ',') {
379 if (is_array($arr)) {
380 foreach($arr as $arg) {
381 if ($this->getAddr_s($arg)) {
382 $s .= $separator . $result;
385 $s = ($s ?
substr($s, 2) : $s);
387 eval('$addr = $this->' . $arr . ';') ;
388 if (is_array($addr)) {
389 foreach ($addr as $addr_o) {
390 if (is_object($addr_o)) {
391 $s .= $addr_o->getAddress() . $separator;
394 $s = substr($s, 0, -strlen($separator));
396 if (is_object($addr)) {
397 $s .= $addr->getAddress();
405 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
406 if (is_array($arg)) {
407 foreach($arg as $argument) {
408 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
411 eval('$addr = $this->' . $arg . ';') ;
412 if (is_array($addr)) {
413 foreach ($addr as $next_addr) {
414 if (is_object($next_addr)) {
415 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
416 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
418 $email = $next_addr->mailbox
;
420 $email = strtolower($email);
421 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
422 $arr[$email] = $next_addr->personal
;
427 if (is_object($addr)) {
428 $email = $addr->mailbox
;
429 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
430 $email = strtolower($email);
431 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
432 $arr[$email] = $addr->personal
;
440 function getContentType($type0, $type1) {
441 $type0 = $this->content_type
->type0
;
442 $type1 = $this->content_type
->type1
;
443 return ($this->content_type
->properties
);
448 /** msg_header contains all variables available in a bodystructure **/
449 /** entity like described in rfc2060 **/
453 $parameters = array(),
463 * returns addres_list of supplied argument
464 * arguments: array('to', 'from', ...) or just a string like 'to'.
465 * result: string: address1, addres2, ....
468 function setVar($var, $value) {
469 $this->{$var} = $value;
472 function getParameter($p) {
473 $value = strtolower($p);
474 return (isset($this->parameters
[$p]) ?
$this->parameters
[$p] : '');
477 function setParameter($parameter, $value) {
478 $this->parameters
[strtolower($parameter)] = $value;
484 class address_structure
{
491 function getAddress($full = true) {
494 if (is_object($this)) {
495 if (isset($this->host
) &&( $this->host
!= '')) {
496 $email = $this->mailbox
.'@'.$this->host
;
498 $email = $this->mailbox
;
500 if (trim($this->personal
) != '') {
502 $addr = '"' . $this->personal
. '" <' .$email.'>';
504 $addr = $this->personal
;
506 $best_dpl = $this->personal
;
511 $result = ($full ?
$addr : $best_dpl);
519 /** message is the object that contains messages. It is a recursive
520 object in that through the $entities variable, it can contain
521 more objects of type message. See documentation in mime.txt for
522 a better description of how this works.
524 var $rfc822_header = '',
530 $parent_ent, $entity,
531 $parent = '', $decoded_body='',
532 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
535 $offset = 0, /* for fetching body parts out of raw messages */
536 $length = 0; /* for fetching body parts out of raw messages */
538 function setEnt($ent) {
539 $this->entity_id
= $ent;
542 function addEntity ($msg) {
543 $msg->parent
= &$this;
544 $this->entities
[] = $msg;
547 function getFilename() {
550 if (is_object($this->header
->disposition
)) {
551 $filename = $this->header
->disposition
->getproperty('filename');
553 $filename = $this->header
->disposition
->getproperty('name');
558 $filename = 'untitled-'.$this->entity_id
;
565 function addRFC822Header($read) {
566 $header = new rfc822_header();
567 $this->rfc822_header
= $header->parseHeader($read);
570 function getEntity($ent) {
571 $cur_ent = $this->entity_id
;
573 if (($cur_ent == '') ||
( $cur_ent == '0')) {
574 $cur_ent_a = array();
576 $cur_ent_a = explode('.', $this->entity_id
);
578 $ent_a = explode('.', $ent);
580 $cnt = count($ent_a);
582 for ($i = 0; $i < $cnt -1; ++
$i) {
583 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
585 $cur_ent_a = explode('.', $msg->entity_id
);
587 } else if (!isset($cur_ent_a[$i])) {
588 if (isset($msg->entities
[($ent_a[$i]-1)])) {
589 $msg = $msg->entities
[($ent_a[$i]-1)];
591 $msg = $msg->entities
[0];
594 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
595 /*this is a header for a message/rfc822 entity */
596 $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];
605 if (isset($msg->entities
[($ent_a[$cnt-1])-1])) {
606 if (is_object($msg->entities
[($ent_a[$cnt-1])-1])) {
607 $msg = $msg->entities
[($ent_a[$cnt-1]-1)];
614 function setBody($s) {
615 $this->body_part
= $s;
618 function clean_up() {
620 $msg->body_part
= '';
622 for ($i = 0; isset($msg->entities
[$i]); ++
$i) {
623 $msg->entities
[$i]->clean_up();
627 function getMailbox() {
629 while (is_object($msg->parent
)) {
632 return $msg->mailbox
;
635 function calcEntity($msg) {
636 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
637 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
638 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
639 $ent_no = count($this->entities
)+
1;
640 $par_ent = substr($this->entity_id
, -2);
641 if ($par_ent{0} == '.') {
642 $par_ent = $par_ent{1};
644 if ($par_ent == '0') {
645 $ent_no = count($this->entities
) +
1;
647 $ent = substr($this->entity_id
, 0,strrpos($this->entity_id
, '.'));
648 $ent = ($ent ?
$ent . ".$ent_no" : $ent_no);
649 $msg->entity_id
= $ent;
651 $msg->entity_id
= $ent_no;
654 $ent = $this->entity_id
. ".$ent_no";
655 $msg->entity_id
= $ent;
658 $msg->entity_id
= '0';
661 return $msg->entity_id
;
666 * Bodystructure parser, a recursive function for generating the
667 * entity-tree with all the mime-parts.
669 * It follows RFC2060 and stores all the described fields in the
674 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
677 function parseStructure($read, $i = 0) {
680 $cnt = strlen($read);
682 for (; $i < $cnt; ++
$i) {
683 $char = strtoupper($read{$i});
688 $msg = new message();
689 $hdr = new msg_header();
690 $hdr->type0
= 'text';
691 $hdr->type1
= 'plain';
692 $hdr->encoding
= 'us-ascii';
693 $msg->entity_id
= $this->calcEntity($msg);
695 $msg->header
->type0
= 'multipart';
696 $msg->type0
= 'multipart';
697 while ($read{$i} == '(') {
698 $res = $msg->parseStructure($read, $i);
700 $msg->addEntity($res[0]);
706 /* multipart properties */
708 $res = $this->parseProperties($read, $i);
714 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
716 $res = $msg->parseDisposition($read, $i);
719 } else { /* properties */
720 $res = $msg->parseProperties($read, $i);
727 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
729 $res= $msg->parseLanguage($read, $i);
734 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
735 $msg->header
->type0
= $arg_a[0];
736 $msg->type0
= $arg_a[0];
737 $msg->header
->type1
= $arg_a[1];
738 $msg->type1
= $arg_a[1];
739 $rfc822_hdr = new rfc822_header();
740 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
742 $msg->rfc822_header
= $res[0];
743 while (($i < $cnt) && ($read{$i} != '(')) {
746 $res = $msg->parseStructure($read, $i);
748 $msg->addEntity($res[0]);
753 $res = $msg->parseDisposition($read, $i);
760 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
761 $res = $msg->parseDisposition($read, $i);
763 $res = $msg->parseLanguage($read, $i);
770 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
772 $res = $msg->parseLanguage($read, $i);
776 $i = $msg->parseParenthesis($read, $i);
777 $arg_a[] = ''; /* not yet desribed in rfc2060 */
782 /* unknown argument, skip this part */
783 $i = $msg->parseParenthesis($read, $i);
791 /* inside an entity -> start processing */
792 $debug = substr($read, $i, 20);
793 $res = $msg->parseQuote($read, $i);
798 $arg_s = strtolower($arg_s); /* type0 and type1 */
804 /* probably NIL argument */
805 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
812 /* process the literal value */
813 $res = $msg->parseLiteral($read, $i);
818 case is_numeric($read{$i}):
819 /* process integers */
820 if ($read{$i} == ' ') { break; }
822 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
829 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
831 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
832 $hdr->type0
= $arg_a[0];
833 $hdr->type1
= $arg_a[1];
835 $msg->type0
= $arg_a[0];
836 $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
);
859 if ((substr($msg->entity_id
, -2) == '.0') && ($msg->type0
!='multipart')) {
863 $hdr->type0
= 'multipart';
864 $hdr->type1
= $arg_a[0];
865 $msg->type0
= 'multipart';
866 $msg->type1
= $arg_a[0];
867 $hdr->parameters
= (is_array($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
868 $hdr->disposition
= (is_array($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
869 $hdr->language
= (is_array($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
872 return (array($msg, $i));
877 } /* parsestructure */
879 function parseProperties($read, $i) {
880 $properties = array();
884 for (; $read{$i} != ')'; ++
$i) {
885 if ($read{$i} == '"') {
886 $res = $this->parseQuote($read, $i);
889 } else if ($read{$i} == '{') {
890 $res = $this->parseLiteral($read, $i);
895 if (($prop_name == '') && $arg_s) {
896 $prop_name = strtolower($arg_s);
897 $properties[$prop_name] = '';
899 } else if (($prop_name != '') && ($arg_s != '')) {
900 $properties[$prop_name] = $arg_s;
906 return (array($properties, $i));
909 function parseEnvelope($read, $i, $hdr) {
912 $cnt = strlen($read);
914 for (; ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
916 $char = strtoupper($read{$i});
919 $res = $this->parseQuote($read, $i);
925 $res = $this->parseLiteral($read, $i);
931 /* probably NIL argument */
932 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
939 /* Address structure (with group support)
940 * Note: Group support is useless on SMTP connections
941 * because the protocol doesn't support it
946 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
947 if ($read{$i} == '(') {
948 $res = $this->parseAddress($read, $i);
951 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
953 $group = $addr->mailbox
;
956 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
958 if ($a == ($j+
1)) { /* no group members */
959 $group_addr->group
= $group;
960 $group_addr->mailbox
= '';
961 $group_addr->personal
= "$group: Undisclosed recipients;";
962 $addr_a[] = $group_addr;
966 $addr->group
= $group;
978 if (count($arg_a) > 9) {
979 /* argument 1: date */
980 $d = strtr($arg_a[0], array(' ' => ' '));
981 $d = explode(' ', $d);
982 $hdr->date
= getTimeStamp($d);
984 /* argument 2: subject */
985 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
986 $hdr->subject
= $arg_a[1];
988 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
989 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
990 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
991 $hdr->to
= $arg_a[5]; /* argument 6: to */
992 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
993 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
994 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
995 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
997 return (array($hdr, $i));
1000 function parseLiteral($read, $i) {
1002 for (++
$i; $read{$i} != '}'; ++
$i) {
1003 $lit_cnt .= $read{$i};
1006 $lit_cnt +
=2; /* add the { and } characters */
1008 for ($j = 0; $j < $lit_cnt; ++
$j) {
1011 return (array($s, $i));
1014 function parseQuote($read, $i) {
1016 for (++
$i; $read{$i} != '"'; ++
$i) {
1017 if ($read{$i} == '\\') {
1022 return (array($s, $i));
1025 function parseAddress($read, $i) {
1028 for (; $read{$i} != ')'; ++
$i) {
1029 $char = strtoupper($read{$i});
1033 $res = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
1039 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
1048 if (count($arg_a) == 4) {
1049 $adr = new address_structure();
1050 $adr->personal
= $arg_a[0];
1051 $adr->adl
= $arg_a[1];
1052 $adr->mailbox
= $arg_a[2];
1053 $adr->host
= $arg_a[3];
1057 return (array($adr, $i));
1060 function parseDisposition($read, $i) {
1063 for (; $read{$i} != ')'; ++
$i) {
1064 switch ($read{$i}) {
1068 switch ($read{$i}) {
1069 case '"': $res = $this->parseQuote($read, $i); break;
1070 case '{': $res = $this->parseLiteral($read, $i); break;
1071 case '(': $res = $this->parseProperties($read, $i); break;
1080 if (isset($arg_a[0])) {
1081 $disp = new disposition($arg_a[0]);
1082 if (isset($arg_a[1])) {
1083 $disp->properties
= $arg_a[1];
1087 return (is_object($disp) ?
array($disp, $i) : array('', $i));
1090 function parseLanguage($read, $i) {
1091 /* no idea how to process this one without examples */
1094 for (; $read{$i} != ')'; ++
$i) {
1095 switch ($read{$i}) {
1099 switch ($read{$i}) {
1100 case '"': $res = $this->parseQuote($read, $i); break;
1101 case '{': $res = $this->parseLiteral($read, $i); break;
1102 case '(': $res = $this->parseProperties($read, $i); break;
1111 if (isset($arg_a[0])) {
1112 $lang = new language($arg_a[0]);
1113 if (isset($arg_a[1])) {
1114 $lang->properties
= $arg_a[1];
1118 return (is_object($lang) ?
array($lang, $i) : array('', $i));
1121 function parseParenthesis($read, $i) {
1122 for (; $read{$i} != ')'; ++
$i) {
1123 switch ($read{$i}) {
1127 switch ($read{$i}) {
1128 case '"': $res = $this->parseQuote($read, $i); break;
1129 case '{': $res = $this->parseLiteral($read, $i); break;
1130 case '(': $res = $this->parseProperties($read, $i); break;
1140 /* Function to fill the message structure in case the */
1141 /* bodystructure is not available NOT FINISHED YET */
1142 function parseMessage($read, $type0, $type1) {
1145 $rfc822_header = true;
1146 $mime_header = false;
1149 $rfc822_header = false;
1150 $mime_header = true;
1152 default: return $read;
1155 for ($i = 1; $i < $count; ++
$i) {
1156 $line = trim($body[$i]);
1157 if (($mime_header ||
$rfc822_header) &&
1158 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
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 $bndreg = str_replace(")", "\\)", $bndreg);
1169 } else if ($rfc822_header && $line == '') {
1170 $rfc822_header = false;
1171 if ($msg->type0
== 'multipart') {
1172 $mime_header = true;
1176 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
1177 $cnt=count($boundaries)-1;
1178 $bnd = $boundaries[$cnt]['bnd'];
1179 $bndreg = $boundaries[$cnt]['bndreg'];
1181 $regstr = '/^--'."($bndreg)".".*".'/';
1182 if (preg_match($regstr, $line, $reg)) {
1183 $bndlen = strlen($reg[1]);
1185 if (strlen($line) > ($bndlen +
3)) {
1186 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
1191 /* calc offset and return $msg */
1192 //$entStr = CalcEntity("$entStr", -1);
1193 array_pop($boundaries);
1194 $mime_header = true;
1197 $mime_header = true;
1199 //$entStr = CalcEntity("$entStr", 0);
1209 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html')) {
1211 $type = $this->type0
.'/'.$this->type1
;
1212 if ($type == 'multipart/alternative') {
1213 $msg = $this->findAlternativeEntity($alt_order);
1214 if (count($msg->entities
) == 0) {
1215 $entity[] = $msg->entity_id
;
1217 $entity = $msg->findDisplayEntity($entity, $alt_order);
1220 } else if ($type == 'multipart/related') {
1221 $msgs = $this->findRelatedEntity();
1222 foreach ($msgs as $msg) {
1223 if (count($msg->entities
) == 0) {
1224 $entity[] = $msg->entity_id
;
1226 $entity = $msg->findDisplayEntity($entity, $alt_order);
1229 if (count($msgs) > 0) {
1232 } else if (($this->type0
== 'text') &&
1233 (($this->type1
== 'plain') ||
1234 ($this->type1
== 'html') ||
1235 ($this->type1
== 'message')) &&
1236 isset($this->entity_id
)) {
1237 if (count($this->entities
) == 0) {
1238 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
1239 $entity[] = $this->entity_id
;
1245 foreach ($this->entities
as $ent) {
1246 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
1247 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1248 $entity = $ent->findDisplayEntity($entity, $alt_order);
1253 /* CAN THIS BE REMOVED? IF SO, DO IT!!! */
1254 //while (isset($this->entities[$i]) && !$found &&
1255 // (strtolower($this->entities[$i]->header->disposition->name)
1256 // != 'attachment') &&
1257 // ($this->entities[$i]->type0 != 'message' &&
1258 // $this->entities[$i]->type1 != 'rfc822')
1260 // $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order);
1267 function findAlternativeEntity($alt_order) {
1268 /* If we are dealing with alternative parts then we */
1269 /* choose the best viewable message supported by SM. */
1272 $altcount = count($alt_order);
1274 foreach($this->entities
as $ent) {
1275 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1276 if ($type == 'multipart/related') {
1277 $type = $ent->header
->getParameter('type');
1279 for ($j = $best_view; $j < $altcount; ++
$j) {
1280 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1290 function findRelatedEntity() {
1292 $entcount = count($this->entities
);
1294 for ($i = 0; $i < $entcount; ++
$i) {
1295 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1296 if ($this->header
->getParameter('type') == $type) {
1297 $msgs[] = $this->entities
[$i];
1304 function getAttachments($exclude_id=array(), $result = array()) {
1305 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
1306 $this = $this->entities
[0];
1309 if (count($this->entities
)) {
1310 foreach ($this->entities
as $entity) {
1313 foreach ($exclude_id as $excl) {
1314 if ($entity->entity_id
=== $excl) {
1320 if (($entity->type0
== 'multipart') &&
1321 ($entity->type1
!= 'related')) {
1322 $result = $entity->getAttachments($exclude_id, $result);
1323 } else if ($entity->type0
!= 'multipart') {
1324 $result[] = $entity;
1330 foreach ($exclude_id as $excl) {
1331 $exclude = $exclude ||
($this->entity_id
== $excl);
1343 class smime_message
{
1348 function disposition($name) {
1349 $this->name
= $name;
1350 $this->properties
= array();
1353 function getProperty($par) {
1354 $value = strtolower($par);
1355 if (isset($this->properties
[$par])) {
1356 return $this->properties
[$par];
1363 function language($name) {
1364 $this->name
= $name;
1365 $this->properties
= array();
1369 class content_type
{
1370 var $type0 = 'text',
1374 function content_type($type) {
1375 $pos = strpos($type, '/');
1377 $this->type0
= substr($type, 0, $pos);
1378 $this->type1
= substr($type, $pos+
1);
1380 $this->type0
= $type;
1382 $this->properties
= array();