6 * Copyright (c) 2003-2004 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.
12 * @package squirrelmail
16 * The object that contains a message
18 * message is the object that contains messages. It is a recursive
19 * object in that through the $entities variable, it can contain
20 * more objects of type message. See documentation in mime.txt for
21 * a better description of how this works.
22 * @package squirrelmail
25 var $rfc822_header = '',
33 $parent = '', $decoded_body='',
34 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
37 $offset = 0, /* for fetching body parts out of raw messages */
38 $length = 0, /* for fetching body parts out of raw messages */
39 $att_local_name = ''; /* location where the tempory attachment
40 is stored. For future usage in smtp.php */
42 function setEnt($ent) {
43 $this->entity_id
= $ent;
46 function addEntity ($msg) {
47 $this->entities
[] = $msg;
50 function getFilename() {
51 $filename = $this->header
->getParameter('filename');
53 $filename = $this->header
->getParameter('name');
57 $filename = 'untitled-'.$this->entity_id
;
63 function addRFC822Header($read) {
64 $header = new Rfc822Header();
65 $this->rfc822_header
= $header->parseHeader($read);
68 function getEntity($ent) {
69 $cur_ent = $this->entity_id
;
71 if (($cur_ent == '') ||
($cur_ent == '0')) {
74 $cur_ent_a = explode('.', $this->entity_id
);
76 $ent_a = explode('.', $ent);
78 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
79 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
81 $cur_ent_a = explode('.', $msg->entity_id
);
83 } else if (!isset($cur_ent_a[$i])) {
84 if (isset($msg->entities
[($ent_a[$i]-1)])) {
85 $msg = $msg->entities
[($ent_a[$i]-1)];
87 $msg = $msg->entities
[0];
90 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
91 /*this is a header for a message/rfc822 entity */
92 $msg = $msg->entities
[0];
96 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
97 /*this is a header for a message/rfc822 entity */
98 $msg = $msg->entities
[0];
101 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
102 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
103 $msg = $msg->entities
[($ent_a[$entCount]-1)];
110 function setBody($s) {
111 $this->body_part
= $s;
114 function clean_up() {
116 $msg->body_part
= '';
118 foreach ($msg->entities
as $m) {
123 function getMailbox() {
125 while (is_object($msg->parent
)) {
128 return $msg->mailbox
;
132 * Bodystructure parser, a recursive function for generating the
133 * entity-tree with all the mime-parts.
135 * It follows RFC2060 and stores all the described fields in the
140 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
143 function parseStructure($read, &$i, $sub_msg = '') {
144 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
145 if($msg) $msg->setEntIds($msg,false,0);
149 function setEntIds(&$msg,$init=false,$i=0) {
150 $iCnt = count($msg->entities
);
151 if ($init !==false) {
153 if ($msg->parent
->type0
== 'message' &&
154 $msg->parent
->type1
== 'rfc822' &&
155 $msg->type0
== 'multipart') {
159 $msg->entity_id
= "$init.$iEntSub";
161 $msg->entity_id
= $iEntSub;
168 for ($i=0;$i<$iCnt;++
$i) {
169 $msg->entities
[$i]->parent
=& $msg;
170 if (strrchr($msg->entity_id
, '.') != '.0') {
171 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
173 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
178 function parseBodyStructure($read, &$i, $sub_msg = '') {
184 $message = new Message();
187 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
188 $char = strtoupper($read{$i});
194 $msg = new Message();
195 $hdr = new MessageHeader();
196 $hdr->type0
= 'text';
197 $hdr->type1
= 'plain';
198 $hdr->encoding
= 'us-ascii';
200 $msg->header
->type0
= 'multipart';
201 $msg->type0
= 'multipart';
202 while ($read{$i} == '(') {
203 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
208 /* multipart properties */
210 $arg_a[] = $msg->parseProperties($read, $i);
214 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
216 $arg_a[] = $msg->parseDisposition($read, $i);
217 } else { /* properties */
218 $arg_a[] = $msg->parseProperties($read, $i);
223 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
225 $arg_a[]= $msg->parseLanguage($read, $i);
228 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
229 $msg->header
->type0
= $arg_a[0];
230 $msg->header
->type1
= $arg_a[1];
231 $msg->type0
= $arg_a[0];
232 $msg->type1
= $arg_a[1];
233 $rfc822_hdr = new Rfc822Header();
234 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
235 while (($i < $cnt) && ($read{$i} != '(')) {
238 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
243 $arg_a[] = $msg->parseDisposition($read, $i);
248 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
249 $arg_a[] = $msg->parseDisposition($read, $i);
251 $arg_a[] = $msg->parseLanguage($read, $i);
256 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
258 $arg_a[] = $msg->parseLanguage($read, $i);
260 $i = $msg->parseParenthesis($read, $i);
261 $arg_a[] = ''; /* not yet described in rfc2060 */
266 /* unknown argument, skip this part */
267 $i = $msg->parseParenthesis($read, $i);
274 /* inside an entity -> start processing */
275 $arg_s = $msg->parseQuote($read, $i);
278 $arg_s = strtolower($arg_s); /* type0 and type1 */
284 /* probably NIL argument */
285 $tmpnil = strtoupper(substr($read, $i, 4));
286 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
293 /* process the literal value */
294 $arg_a[] = $msg->parseLiteral($read, $i);
298 case is_numeric($read{$i}):
299 /* process integers */
300 if ($read{$i} == ' ') { break; }
302 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
303 $i +
= strlen($regs[1])-1;
310 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
312 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
313 $hdr->type0
= $arg_a[0];
314 $hdr->type1
= $arg_a[1];
316 $msg->type0
= $arg_a[0];
317 $msg->type1
= $arg_a[1];
319 if (is_array($arr)) {
320 $hdr->parameters
= $arg_a[2];
322 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
323 $hdr->description
= $arg_a[4];
324 $hdr->encoding
= strtolower($arg_a[5]);
325 $hdr->entity_id
= $msg->entity_id
;
326 $hdr->size
= $arg_a[6];
328 $hdr->lines
= $arg_a[7];
333 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
334 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
335 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
338 $hdr->type0
= 'multipart';
339 $hdr->type1
= $arg_a[0];
340 $msg->type0
= 'multipart';
341 $msg->type1
= $arg_a[0];
342 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
343 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
344 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
351 } /* parsestructure */
353 function parseProperties($read, &$i) {
354 $properties = array();
357 for (; $read{$i} != ')'; ++
$i) {
359 if ($read{$i} == '"') {
360 $arg_s = $this->parseQuote($read, $i);
361 } else if ($read{$i} == '{') {
362 $arg_s = $this->parseLiteral($read, $i);
366 if ($prop_name == '') {
367 $prop_name = strtolower($arg_s);
368 $properties[$prop_name] = '';
369 } else if ($prop_name != '') {
370 $properties[$prop_name] = $arg_s;
378 function parseEnvelope($read, &$i, $hdr) {
382 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
383 $char = strtoupper($read{$i});
386 $arg_a[] = $this->parseQuote($read, $i);
390 $arg_a[] = $this->parseLiteral($read, $i);
391 /* temp bugfix (SM 1.5 will have a working clean version)
392 too much work to implement that version right now */
397 /* probably NIL argument */
398 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
405 /* Address structure (with group support)
406 * Note: Group support is useless on SMTP connections
407 * because the protocol doesn't support it
412 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
413 if ($read{$i} == '(') {
414 $addr = $this->parseAddress($read, $i);
415 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
417 $group = $addr->mailbox
;
420 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
422 if ($a == ($j+
1)) { /* no group members */
423 $group_addr->group
= $group;
424 $group_addr->mailbox
= '';
425 $group_addr->personal
= "$group: Undisclosed recipients;";
426 $addr_a[] = $group_addr;
430 $addr->group
= $group;
442 if (count($arg_a) > 9) {
443 $d = strtr($arg_a[0], array(' ' => ' '));
444 $d = explode(' ', $d);
445 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
447 $hdr->date
= getTimeStamp($d); /* argument 1: date */
448 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
449 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
450 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
451 $hdr->replyto
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
452 $hdr->to
= $arg_a[5]; /* argument 6: to */
453 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
454 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
455 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
456 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
461 function parseLiteral($read, &$i) {
464 $iPos = strpos($read,'}',$i);
466 $lit_cnt = substr($read, $i, $iPos - $i);
467 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
468 /* Now read the literal */
469 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
471 /* temp bugfix (SM 1.5 will have a working clean version)
472 too much work to implement that version right now */
474 } else { /* should never happen */
475 $i +
= 3; /* } + \r + \n */
481 function parseQuote($read, &$i) {
485 $iPos = strpos($read,'"',$iPos);
487 if ($iPos && $read{$iPos -1} != '\\') {
488 $s = substr($read,$i,($iPos-$i));
493 if ($iPos > strlen($read)) {
500 function parseAddress($read, &$i) {
502 for (; $read{$i} != ')'; ++
$i) {
503 $char = strtoupper($read{$i});
505 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
506 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
509 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
518 if (count($arg_a) == 4) {
519 $adr = new AddressStructure();
520 $adr->personal
= $arg_a[0];
521 $adr->adl
= $arg_a[1];
522 $adr->mailbox
= $arg_a[2];
523 $adr->host
= $arg_a[3];
530 function parseDisposition($read, &$i) {
532 for (; $read{$i} != ')'; ++
$i) {
534 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
535 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
536 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
541 if (isset($arg_a[0])) {
542 $disp = new Disposition($arg_a[0]);
543 if (isset($arg_a[1])) {
544 $disp->properties
= $arg_a[1];
548 return (is_object($disp) ?
$disp : '');
551 function parseLanguage($read, &$i) {
552 /* no idea how to process this one without examples */
555 for (; $read{$i} != ')'; ++
$i) {
557 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
558 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
559 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
564 if (isset($arg_a[0])) {
565 $lang = new Language($arg_a[0]);
566 if (isset($arg_a[1])) {
567 $lang->properties
= $arg_a[1];
571 return (is_object($lang) ?
$lang : '');
574 function parseParenthesis($read, $i) {
575 for (; $read{$i} != ')'; ++
$i) {
577 case '"': $this->parseQuote($read, $i); break;
578 case '{': $this->parseLiteral($read, $i); break;
579 case '(': $this->parseProperties($read, $i); break;
586 /* Function to fill the message structure in case the */
587 /* bodystructure is not available NOT FINISHED YET */
588 function parseMessage($read, $type0, $type1) {
591 $rfc822_header = true;
592 $mime_header = false;
595 $rfc822_header = false;
598 default: return $read;
601 for ($i = 1; $i < $count; ++
$i) {
602 $line = trim($body[$i]);
603 if (($mime_header ||
$rfc822_header) &&
604 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
607 $bndreg = str_replace("\\", "\\\\", $bndreg);
608 $bndreg = str_replace("?", "\\?", $bndreg);
609 $bndreg = str_replace("+", "\\+", $bndreg);
610 $bndreg = str_replace(".", "\\.", $bndreg);
611 $bndreg = str_replace("/", "\\/", $bndreg);
612 $bndreg = str_replace("-", "\\-", $bndreg);
613 $bndreg = str_replace("(", "\\(", $bndreg);
614 $bndreg = str_replace(")", "\\)", $bndreg);
615 } else if ($rfc822_header && $line == '') {
616 $rfc822_header = false;
617 if ($msg->type0
== 'multipart') {
622 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
623 $cnt = count($boundaries)-1;
624 $bnd = $boundaries[$cnt]['bnd'];
625 $bndreg = $boundaries[$cnt]['bndreg'];
627 $regstr = '/^--'."($bndreg)".".*".'/';
628 if (preg_match($regstr, $line, $reg)) {
629 $bndlen = strlen($reg[1]);
631 if (strlen($line) > ($bndlen +
3)) {
632 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
637 /* calc offset and return $msg */
638 //$entStr = CalcEntity("$entStr", -1);
639 array_pop($boundaries);
645 //$entStr = CalcEntity("$entStr", 0);
655 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
657 if ($this->type0
== 'multipart') {
658 if($this->type1
== 'alternative') {
659 $msg = $this->findAlternativeEntity($alt_order);
660 if (count($msg->entities
) == 0) {
661 $entity[] = $msg->entity_id
;
663 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
666 } else if ($this->type1
== 'related') { /* RFC 2387 */
667 $msgs = $this->findRelatedEntity();
668 foreach ($msgs as $msg) {
669 if (count($msg->entities
) == 0) {
670 $entity[] = $msg->entity_id
;
672 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
675 if (count($msgs) > 0) {
678 } else { /* Treat as multipart/mixed */
679 foreach ($this->entities
as $ent) {
680 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
681 (!isset($ent->header
->parameters
['filename'])) &&
682 (!isset($ent->header
->parameters
['name'])) &&
683 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
684 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
689 } else { /* If not multipart, then just compare with each entry from $alt_order */
690 $type = $this->type0
.'/'.$this->type1
;
691 // $alt_order[] = "message/rfc822";
692 foreach ($alt_order as $alt) {
693 if( ($alt == $type) && isset($this->entity_id
) ) {
694 if ((count($this->entities
) == 0) &&
695 (!isset($ent->header
->parameters
['filename'])) &&
696 (!isset($ent->header
->parameters
['name'])) &&
697 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
698 $entity[] = $this->entity_id
;
705 foreach ($this->entities
as $ent) {
706 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
707 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
708 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
713 if(!$strict && !$found) {
714 if (($this->type0
== 'text') &&
715 in_array($this->type1
, array('plain', 'html', 'message')) &&
716 isset($this->entity_id
)) {
717 if (count($this->entities
) == 0) {
718 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
719 $entity[] = $this->entity_id
;
727 function findAlternativeEntity($alt_order) {
728 /* If we are dealing with alternative parts then we */
729 /* choose the best viewable message supported by SM. */
732 foreach($this->entities
as $ent) {
733 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
734 if ($type == 'multipart/related') {
735 $type = $ent->header
->getParameter('type');
736 // Mozilla bug. Mozilla does not provide the parameter type.
737 if (!$type) $type = 'text/html';
739 $altCount = count($alt_order);
740 for ($j = $best_view; $j < $altCount; ++
$j) {
741 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
751 function findRelatedEntity() {
753 $related_type = $this->header
->getParameter('type');
754 // Mozilla bug. Mozilla does not provide the parameter type.
755 if (!$related_type) $related_type = 'text/html';
756 $entCount = count($this->entities
);
757 for ($i = 0; $i < $entCount; ++
$i) {
758 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
759 if ($related_type == $type) {
760 $msgs[] = $this->entities
[$i];
766 function getAttachments($exclude_id=array(), $result = array()) {
768 if (($this->type0 == 'message') &&
769 ($this->type1 == 'rfc822') &&
770 ($this->entity_id) ) {
771 $this = $this->entities[0];
774 if (count($this->entities
)) {
775 foreach ($this->entities
as $entity) {
777 foreach ($exclude_id as $excl) {
778 if ($entity->entity_id
=== $excl) {
784 if (($entity->type0
== 'multipart') &&
785 ($entity->type1
!= 'related')) {
786 $result = $entity->getAttachments($exclude_id, $result);
787 } else if ($entity->type0
!= 'multipart') {
794 foreach ($exclude_id as $excl) {
795 $exclude = $exclude ||
($this->entity_id
== $excl);
805 function initAttachment($type, $name, $location) {
806 $attachment = new Message();
807 $mime_header = new MessageHeader();
808 $mime_header->setParameter('name', $name);
809 $pos = strpos($type, '/');
811 $mime_header->type0
= substr($type, 0, $pos);
812 $mime_header->type1
= substr($type, $pos+
1);
814 $mime_header->type0
= $type;
816 $attachment->att_local_name
= $location;
817 $disposition = new Disposition('attachment');
818 $disposition->properties
['filename'] = $name;
819 $mime_header->disposition
= $disposition;
820 $attachment->mime_header
= $mime_header;
821 $this->entities
[]=$attachment;