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.
15 /** message is the object that contains messages. It is a recursive
16 object in that through the $entities variable, it can contain
17 more objects of type message. See documentation in mime.txt for
18 a better description of how this works.
20 var $rfc822_header = '',
27 $parent = '', $decoded_body='',
28 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
31 $offset = 0, /* for fetching body parts out of raw messages */
32 $length = 0, /* for fetching body parts out of raw messages */
33 $att_local_name = ''; /* location where the tempory attachment
34 is stored. For future usage in smtp.php */
36 function setEnt($ent) {
37 $this->entity_id
= $ent;
40 function addEntity ($msg) {
41 $msg->parent
= &$this;
42 $this->entities
[] = $msg;
45 function getFilename() {
47 $filename = $this->header
->getParameter('filename');
49 $filename = $this->header
->getParameter('name');
53 $filename = 'untitled-'.$this->entity_id
;
59 function addRFC822Header($read) {
60 $header = new Rfc822Header();
61 $this->rfc822_header
= $header->parseHeader($read);
64 function getEntity($ent) {
65 $cur_ent = $this->entity_id
;
67 if (($cur_ent == '') ||
($cur_ent == '0')) {
70 $cur_ent_a = explode('.', $this->entity_id
);
72 $ent_a = explode('.', $ent);
74 $entCount = count($ent_a) - 1;
75 for ($i = 0; $i < $entCount; ++
$i) {
76 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
78 $cur_ent_a = explode('.', $msg->entity_id
);
80 } else if (!isset($cur_ent_a[$i])) {
81 if (isset($msg->entities
[($ent_a[$i]-1)])) {
82 $msg = $msg->entities
[($ent_a[$i]-1)];
84 $msg = $msg->entities
[0];
87 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
88 /*this is a header for a message/rfc822 entity */
89 $msg = $msg->entities
[0];
93 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
94 /*this is a header for a message/rfc822 entity */
95 $msg = $msg->entities
[0];
98 if (isset($msg->entities
[($ent_a[$cnt])-1])) {
99 if (is_object($msg->entities
[($ent_a[$cnt])-1])) {
100 $msg = $msg->entities
[($ent_a[$cnt]-1)];
107 function setBody($s) {
108 $this->body_part
= $s;
111 function clean_up() {
113 $msg->body_part
= '';
115 foreach ($msg->entities
as $m) {
120 function getMailbox() {
122 while (is_object($msg->parent
)) {
125 return $msg->mailbox
;
128 function calcEntity($msg) {
129 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
130 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
131 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
132 $entCount = count($this->entities
) +
1;
133 $par_ent = substr($this->entity_id
, -2);
134 if ($par_ent{0} == '.') {
135 $par_ent = $par_ent{1};
137 if ($par_ent == '0') {
139 $ent = substr($this->entity_id
, 0, strrpos($this->entity_id
, '.'));
140 $ent = ($ent ?
$ent . '.' : '') . $entCount;
141 $msg->entity_id
= $ent;
143 $msg->entity_id
= $entCount;
146 $ent = $this->entity_id
. '.' . $entCount;
147 $msg->entity_id
= $ent;
150 $msg->entity_id
= '0';
153 return $msg->entity_id
;
158 * Bodystructure parser, a recursive function for generating the
159 * entity-tree with all the mime-parts.
161 * It follows RFC2060 and stores all the described fields in the
166 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
169 function parseStructure($read, $i = 0) {
172 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
173 $char = strtoupper($read{$i});
179 $msg = new Message();
180 $hdr = new MessageHeader();
181 $hdr->type0
= 'text';
182 $hdr->type1
= 'plain';
183 $hdr->encoding
= 'us-ascii';
184 $msg->entity_id
= $this->calcEntity($msg);
186 $msg->header
->type0
= 'multipart';
187 $msg->type0
= 'multipart';
188 while ($read{$i} == '(') {
189 $res = $msg->parseStructure($read, $i);
191 $msg->addEntity($res[0]);
196 /* multipart properties */
198 $res = $this->parseProperties($read, $i);
204 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
206 $res = $msg->parseDisposition($read, $i);
207 } else { /* properties */
208 $res = $msg->parseProperties($read, $i);
215 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
217 $res= $msg->parseLanguage($read, $i);
222 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
223 $msg->header
->type0
= $arg_a[0];
224 $msg->header
->type1
= $arg_a[1];
225 $msg->type0
= $arg_a[0];
226 $msg->type1
= $arg_a[1];
227 $rfc822_hdr = new Rfc822Header();
228 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
229 $msg->rfc822_header
= $res[0];
231 while (($i < $cnt) && ($read{$i} != '(')) {
234 $res = $msg->parseStructure($read, $i);
236 $msg->addEntity($res[0]);
241 $res = $msg->parseDisposition($read, $i);
248 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
249 $res = $msg->parseDisposition($read, $i);
251 $res = $msg->parseLanguage($read, $i);
258 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
260 $res = $msg->parseLanguage($read, $i);
264 $i = $msg->parseParenthesis($read, $i);
265 $arg_a[] = ''; /* not yet described in rfc2060 */
270 /* unknown argument, skip this part */
271 $i = $msg->parseParenthesis($read, $i);
278 /* inside an entity -> start processing */
279 $debug = substr($read, $i, 20);
280 $res = $msg->parseQuote($read, $i);
285 $arg_s = strtolower($arg_s); /* type0 and type1 */
291 /* probably NIL argument */
292 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
299 /* process the literal value */
300 $res = $msg->parseLiteral($read, $i);
305 case is_numeric($read{$i}):
306 /* process integers */
307 if ($read{$i} == ' ') { break; }
309 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
316 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
318 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
319 $hdr->type0
= $arg_a[0];
320 $hdr->type1
= $arg_a[1];
322 $msg->type0
= $arg_a[0];
323 $msg->type1
= $arg_a[1];
325 if (is_array($arr)) {
326 $hdr->parameters
= $arg_a[2];
328 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
329 $hdr->description
= $arg_a[4];
330 $hdr->encoding
= strtolower($arg_a[5]);
331 $hdr->entity_id
= $msg->entity_id
;
332 $hdr->size
= $arg_a[6];
334 $hdr->lines
= $arg_a[7];
339 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
340 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
341 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
343 if ((strrchr($msg->entity_id
, '.') == '.0') && ($msg->type0
!='multipart')) {
344 $msg->entity_id
= $this->entity_id
. '.1';
347 $hdr->type0
= 'multipart';
348 $hdr->type1
= $arg_a[0];
349 $msg->type0
= 'multipart';
350 $msg->type1
= $arg_a[0];
351 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
352 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
353 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
357 return (array($msg, $i));
362 } /* parsestructure */
364 function parseProperties($read, $i) {
365 $properties = array();
368 for (; $read{$i} != ')'; ++
$i) {
370 if ($read{$i} == '"') {
371 $res = $this->parseQuote($read, $i);
374 } else if ($read{$i} == '{') {
375 $res = $this->parseLiteral($read, $i);
381 if ($prop_name == '') {
382 $prop_name = strtolower($arg_s);
383 $properties[$prop_name] = '';
384 } else if ($prop_name != '') {
385 $properties[$prop_name] = $arg_s;
390 return array($properties, $i);
393 function parseEnvelope($read, $i, $hdr) {
397 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
399 $char = strtoupper($read{$i});
402 $res = $this->parseQuote($read, $i);
408 $res = $this->parseLiteral($read, $i);
414 /* probably NIL argument */
415 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
422 /* Address structure (with group support)
423 * Note: Group support is useless on SMTP connections
424 * because the protocol doesn't support it
429 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
430 if ($read{$i} == '(') {
431 $res = $this->parseAddress($read, $i);
434 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
436 $group = $addr->mailbox
;
439 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
441 if ($a == ($j+
1)) { /* no group members */
442 $group_addr->group
= $group;
443 $group_addr->mailbox
= '';
444 $group_addr->personal
= "$group: Undisclosed recipients;";
445 $addr_a[] = $group_addr;
449 $addr->group
= $group;
461 if (count($arg_a) > 9) {
462 /* argument 1: date */
463 $d = strtr($arg_a[0], array(' ' => ' '));
464 $d = explode(' ', $d);
465 $hdr->date
= getTimeStamp($d);
467 /* argument 2: subject */
468 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
469 $hdr->subject
= $arg_a[1];
471 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
472 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
473 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
474 $hdr->to
= $arg_a[5]; /* argument 6: to */
475 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
476 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
477 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
478 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
480 return (array($hdr, $i));
483 function parseLiteral($read, $i) {
485 for (++
$i; $read{$i} != '}'; ++
$i) {
486 $lit_cnt .= $read{$i};
489 $lit_cnt +
=2; /* add the { and } characters */
491 for ($j = 0; $j < $lit_cnt; ++
$j) {
494 return (array($s, $i));
497 function parseQuote($read, $i) {
499 for (++
$i; $read{$i} != '"'; ++
$i) {
500 if ($read{$i} == '\\') {
505 return (array($s, $i));
508 function parseAddress($read, $i) {
511 for (; $read{$i} != ')'; ++
$i) {
512 $char = strtoupper($read{$i});
516 $res = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
522 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
531 if (count($arg_a) == 4) {
532 $adr = new AddressStructure();
533 $adr->personal
= $arg_a[0];
534 $adr->adl
= $arg_a[1];
535 $adr->mailbox
= $arg_a[2];
536 $adr->host
= $arg_a[3];
540 return (array($adr, $i));
543 function parseDisposition($read, $i) {
546 for (; $read{$i} != ')'; ++
$i) {
552 case '"': $res = $this->parseQuote($read, $i); break;
553 case '{': $res = $this->parseLiteral($read, $i); break;
554 case '(': $res = $this->parseProperties($read, $i); break;
563 if (isset($arg_a[0])) {
564 $disp = new Disposition($arg_a[0]);
565 if (isset($arg_a[1])) {
566 $disp->properties
= $arg_a[1];
570 return (is_object($disp) ?
array($disp, $i) : array('', $i));
573 function parseLanguage($read, $i) {
574 /* no idea how to process this one without examples */
577 for (; $read{$i} != ')'; ++
$i) {
583 case '"': $res = $this->parseQuote($read, $i); break;
584 case '{': $res = $this->parseLiteral($read, $i); break;
585 case '(': $res = $this->parseProperties($read, $i); break;
594 if (isset($arg_a[0])) {
595 $lang = new Language($arg_a[0]);
596 if (isset($arg_a[1])) {
597 $lang->properties
= $arg_a[1];
601 return (is_object($lang) ?
array($lang, $i) : array('', $i));
604 function parseParenthesis($read, $i) {
605 for (; $read{$i} != ')'; ++
$i) {
611 case '"': $res = $this->parseQuote($read, $i); break;
612 case '{': $res = $this->parseLiteral($read, $i); break;
613 case '(': $res = $this->parseProperties($read, $i); break;
623 /* Function to fill the message structure in case the */
624 /* bodystructure is not available NOT FINISHED YET */
625 function parseMessage($read, $type0, $type1) {
628 $rfc822_header = true;
629 $mime_header = false;
632 $rfc822_header = false;
635 default: return $read;
638 for ($i = 1; $i < $count; ++
$i) {
639 $line = trim($body[$i]);
640 if (($mime_header ||
$rfc822_header) &&
641 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
644 $bndreg = str_replace("\\", "\\\\", $bndreg);
645 $bndreg = str_replace("?", "\\?", $bndreg);
646 $bndreg = str_replace("+", "\\+", $bndreg);
647 $bndreg = str_replace(".", "\\.", $bndreg);
648 $bndreg = str_replace("/", "\\/", $bndreg);
649 $bndreg = str_replace("-", "\\-", $bndreg);
650 $bndreg = str_replace("(", "\\(", $bndreg);
651 $bndreg = str_replace(")", "\\)", $bndreg);
652 } else if ($rfc822_header && $line == '') {
653 $rfc822_header = false;
654 if ($msg->type0
== 'multipart') {
659 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
660 $cnt = count($boundaries)-1;
661 $bnd = $boundaries[$cnt]['bnd'];
662 $bndreg = $boundaries[$cnt]['bndreg'];
664 $regstr = '/^--'."($bndreg)".".*".'/';
665 if (preg_match($regstr, $line, $reg)) {
666 $bndlen = strlen($reg[1]);
668 if (strlen($line) > ($bndlen +
3)) {
669 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
674 /* calc offset and return $msg */
675 //$entStr = CalcEntity("$entStr", -1);
676 array_pop($boundaries);
682 //$entStr = CalcEntity("$entStr", 0);
692 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
694 if ($this->type0
== 'multipart') {
695 if($this->type1
== 'alternative') {
696 $msg = $this->findAlternativeEntity($alt_order);
697 if (count($msg->entities
) == 0) {
698 $entity[] = $msg->entity_id
;
700 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
703 } else if ($this->type1
== 'related') { /* RFC 2387 */
704 $msgs = $this->findRelatedEntity();
705 foreach ($msgs as $msg) {
706 if (count($msg->entities
) == 0) {
707 $entity[] = $msg->entity_id
;
709 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
712 if (count($msgs) > 0) {
715 } else { /* Treat as multipart/mixed */
716 foreach ($this->entities
as $ent) {
717 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
718 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
719 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
724 } else { /* If not multipart, then just compare with each entry from $alt_order */
725 $type = $this->type0
.'/'.$this->type1
;
726 foreach ($alt_order as $alt) {
727 if( ($alt == $type) && isset($this->entity_id
) ) {
728 if ((count($this->entities
) == 0) &&
729 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
730 $entity[] = $this->entity_id
;
737 foreach ($this->entities
as $ent) {
738 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
739 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
740 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
745 if(!$strict && !$found) {
746 if (($this->type0
== 'text') &&
747 in_array($this->type1
, array('plain', 'html', 'message')) &&
748 isset($this->entity_id
)) {
749 if (count($this->entities
) == 0) {
750 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
751 $entity[] = $this->entity_id
;
760 function findAlternativeEntity($alt_order) {
761 /* If we are dealing with alternative parts then we */
762 /* choose the best viewable message supported by SM. */
765 foreach($this->entities
as $ent) {
766 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
767 if ($type == 'multipart/related') {
768 $type = $ent->header
->getParameter('type');
770 $altCount = count($alt_order);
771 for ($j = $best_view; $j < $altCount; ++
$j) {
772 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
782 function findRelatedEntity() {
785 $entCount = count($this->entities
);
786 for ($i = 0; $i < $entCount; ++
$i) {
787 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
788 if ($this->header
->getParameter('type') == $type) {
789 $msgs[] = $this->entities
[$i];
796 function getAttachments($exclude_id=array(), $result = array()) {
797 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
798 $this = $this->entities
[0];
801 if (count($this->entities
)) {
802 foreach ($this->entities
as $entity) {
805 foreach ($exclude_id as $excl) {
806 if ($entity->entity_id
=== $excl) {
812 if (($entity->type0
== 'multipart') &&
813 ($entity->type1
!= 'related')) {
814 $result = $entity->getAttachments($exclude_id, $result);
815 } else if ($entity->type0
!= 'multipart') {
822 foreach ($exclude_id as $excl) {
823 $exclude = $exclude ||
($this->entity_id
== $excl);