6 * Copyright (c) 2003-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This file contains functions needed to handle mime messages.
12 * @package squirrelmail
18 * The object that contains a message
20 * message is the object that contains messages. It is a recursive
21 * object in that through the $entities variable, it can contain
22 * more objects of type message. See documentation in mime.txt for
23 * a better description of how this works.
24 * @package squirrelmail
33 var $rfc822_header = '';
35 * MessageHeader object
38 var $mime_header = '';
57 var $entities = array();
64 * Parent message part id
81 * Message \seen status
86 * Message \answered status
91 * Message \deleted status
96 * Message \flagged status
111 * Message part offset
112 * for fetching body parts out of raw messages
117 * Message part length
118 * for fetching body parts out of raw messages
123 * Local attachment filename
124 * location where the tempory attachment
125 * is stored. For use in delivery class.
128 var $att_local_name = '';
131 * @param string $ent entity id
133 function setEnt($ent) {
134 $this->entity_id
= $ent;
138 * Add nested message part
141 function addEntity ($msg) {
142 $this->entities
[] = $msg;
146 * Get file name used for mime part
147 * @return string file name
150 function getFilename() {
152 $header = $this->header
;
153 if (is_object($header->disposition
)) {
154 $filename = $header->disposition
->getProperty('filename');
155 if (trim($filename) == '') {
156 $name = decodeHeader($header->disposition
->getProperty('name'));
158 $name = $header->getParameter('name');
160 if (!trim( $header->id
)) {
161 $filename = 'untitled-[' . $this->entity_id
. ']' ;
163 $filename = 'cid: ' . $header->id
;
173 $filename = $header->getParameter('filename');
174 if (!trim($filename)) {
175 $filename = $header->getParameter('name');
176 if (!trim($filename)) {
177 if (!trim( $header->id
)) {
178 $filename = 'untitled-[' . $this->entity_id
. ']' ;
180 $filename = 'cid: ' . $header->id
;
189 * Add header object to message object.
190 * WARNING: Unfinished code. Don't expect it to work in older sm versions.
191 * @param mixed $read array or string with message headers
192 * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object
194 function addRFC822Header($read) {
195 $header = new Rfc822Header();
196 $this->rfc822_header
= $header->parseHeader($read);
201 * @return mixed (object or string?)
203 function getEntity($ent) {
204 $cur_ent = $this->entity_id
;
206 if (($cur_ent == '') ||
($cur_ent == '0')) {
207 $cur_ent_a = array();
209 $cur_ent_a = explode('.', $this->entity_id
);
211 $ent_a = explode('.', $ent);
213 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
214 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
216 $cur_ent_a = explode('.', $msg->entity_id
);
218 } else if (!isset($cur_ent_a[$i])) {
219 if (isset($msg->entities
[($ent_a[$i]-1)])) {
220 $msg = $msg->entities
[($ent_a[$i]-1)];
222 $msg = $msg->entities
[0];
225 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
226 /*this is a header for a message/rfc822 entity */
227 $msg = $msg->entities
[0];
231 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
232 /*this is a header for a message/rfc822 entity */
233 $msg = $msg->entities
[0];
236 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
237 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
238 $msg = $msg->entities
[($ent_a[$entCount]-1)];
247 * @param string $s message body
249 function setBody($s) {
250 $this->body_part
= $s;
254 * Clean message object
256 function clean_up() {
258 $msg->body_part
= '';
260 foreach ($msg->entities
as $m) {
268 function getMailbox() {
270 while (is_object($msg->parent
)) {
273 return $msg->mailbox
;
277 * Bodystructure parser, a recursive function for generating the
278 * entity-tree with all the mime-parts.
280 * It follows RFC2060 and stores all the described fields in the
285 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
286 * @param string $read
288 * @param mixed $sub_msg
289 * @return object Message object
290 * @todo define argument and return types
292 function parseStructure($read, &$i, $sub_msg = '') {
293 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
294 if($msg) $msg->setEntIds($msg,false,0);
305 function setEntIds(&$msg,$init=false,$i=0) {
306 $iCnt = count($msg->entities
);
307 if ($init !==false) {
309 if ($msg->parent
->type0
== 'message' &&
310 $msg->parent
->type1
== 'rfc822' &&
311 $msg->type0
== 'multipart') {
315 $msg->entity_id
= "$init.$iEntSub";
317 $msg->entity_id
= $iEntSub;
324 for ($i=0;$i<$iCnt;++
$i) {
325 $msg->entities
[$i]->parent
=& $msg;
326 if (strrchr($msg->entity_id
, '.') != '.0') {
327 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
329 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
335 * @param string $read
337 * @param mixed $sub_msg
338 * @return object Message object
340 * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
342 function parseBodyStructure($read, &$i, $sub_msg = '') {
348 $message = new Message();
351 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
352 $char = strtoupper($read{$i});
358 $msg = new Message();
359 $hdr = new MessageHeader();
360 $hdr->type0
= 'text';
361 $hdr->type1
= 'plain';
362 $hdr->encoding
= 'us-ascii';
364 $msg->header
->type0
= 'multipart';
365 $msg->type0
= 'multipart';
366 while ($read{$i} == '(') {
367 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
372 /* multipart properties */
374 $arg_a[] = $msg->parseProperties($read, $i);
378 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
380 $arg_a[] = $msg->parseDisposition($read, $i);
381 } else { /* properties */
382 $arg_a[] = $msg->parseProperties($read, $i);
387 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
389 $arg_a[]= $msg->parseLanguage($read, $i);
392 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
393 $msg->header
->type0
= $arg_a[0];
394 $msg->header
->type1
= $arg_a[1];
395 $msg->type0
= $arg_a[0];
396 $msg->type1
= $arg_a[1];
397 $rfc822_hdr = new Rfc822Header();
398 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
399 while (($i < $cnt) && ($read{$i} != '(')) {
402 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
407 $arg_a[] = $msg->parseDisposition($read, $i);
412 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
413 $arg_a[] = $msg->parseDisposition($read, $i);
415 $arg_a[] = $msg->parseLanguage($read, $i);
420 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
422 $arg_a[] = $msg->parseLanguage($read, $i);
424 $i = $msg->parseParenthesis($read, $i);
425 $arg_a[] = ''; /* not yet described in rfc2060 */
430 /* unknown argument, skip this part */
431 $i = $msg->parseParenthesis($read, $i);
438 /* inside an entity -> start processing */
439 $arg_s = $msg->parseQuote($read, $i);
442 $arg_s = strtolower($arg_s); /* type0 and type1 */
448 /* probably NIL argument */
449 $tmpnil = strtoupper(substr($read, $i, 4));
450 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
457 /* process the literal value */
458 $arg_a[] = $msg->parseLiteral($read, $i);
462 case is_numeric($read{$i}):
463 /* process integers */
464 if ($read{$i} == ' ') { break; }
466 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
467 $i +
= strlen($regs[1])-1;
474 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
476 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
477 $hdr->type0
= $arg_a[0];
478 $hdr->type1
= $arg_a[1];
480 $msg->type0
= $arg_a[0];
481 $msg->type1
= $arg_a[1];
483 if (is_array($arr)) {
484 $hdr->parameters
= $arg_a[2];
486 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
487 $hdr->description
= $arg_a[4];
488 $hdr->encoding
= strtolower($arg_a[5]);
489 $hdr->entity_id
= $msg->entity_id
;
490 $hdr->size
= $arg_a[6];
492 $hdr->lines
= $arg_a[7];
497 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
498 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
499 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
502 $hdr->type0
= 'multipart';
503 $hdr->type1
= $arg_a[0];
504 $msg->type0
= 'multipart';
505 $msg->type1
= $arg_a[0];
506 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
507 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
508 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
515 } /* parsestructure */
518 * @param string $read
522 function parseProperties($read, &$i) {
523 $properties = array();
526 for (; $read{$i} != ')'; ++
$i) {
528 if ($read{$i} == '"') {
529 $arg_s = $this->parseQuote($read, $i);
530 } else if ($read{$i} == '{') {
531 $arg_s = $this->parseLiteral($read, $i);
535 if ($prop_name == '') {
536 $prop_name = strtolower($arg_s);
537 $properties[$prop_name] = '';
538 } else if ($prop_name != '') {
539 $properties[$prop_name] = $arg_s;
548 * @param string $read
550 * @param object $hdr MessageHeader object
551 * @return object MessageHeader object
553 function parseEnvelope($read, &$i, $hdr) {
557 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
558 $char = strtoupper($read{$i});
561 $arg_a[] = $this->parseQuote($read, $i);
565 $arg_a[] = $this->parseLiteral($read, $i);
566 /* temp bugfix (SM 1.5 will have a working clean version)
567 too much work to implement that version right now */
572 /* probably NIL argument */
573 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
580 /* Address structure (with group support)
581 * Note: Group support is useless on SMTP connections
582 * because the protocol doesn't support it
587 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
588 if ($read{$i} == '(') {
589 $addr = $this->parseAddress($read, $i);
590 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
592 $group = $addr->mailbox
;
595 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
597 if ($a == ($j+
1)) { /* no group members */
598 $group_addr->group
= $group;
599 $group_addr->mailbox
= '';
600 $group_addr->personal
= "$group: Undisclosed recipients;";
601 $addr_a[] = $group_addr;
605 $addr->group
= $group;
617 if (count($arg_a) > 9) {
618 $d = strtr($arg_a[0], array(' ' => ' '));
619 $d = explode(' ', $d);
620 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
622 $hdr->date
= getTimeStamp($d); /* argument 1: date */
623 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
624 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
625 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
626 $hdr->replyto
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
627 $hdr->to
= $arg_a[5]; /* argument 6: to */
628 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
629 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
630 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
631 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
637 * @param string $read
642 function parseLiteral($read, &$i) {
645 $iPos = strpos($read,'}',$i);
647 $lit_cnt = substr($read, $i, $iPos - $i);
648 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
649 /* Now read the literal */
650 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
652 /* temp bugfix (SM 1.5 will have a working clean version)
653 too much work to implement that version right now */
655 } else { /* should never happen */
656 $i +
= 3; /* } + \r + \n */
663 * @param string $read
668 function parseQuote($read, &$i) {
672 $iPos = strpos($read,'"',$iPos);
674 if ($iPos && $read{$iPos -1} != '\\') {
675 $s = substr($read,$i,($iPos-$i));
680 if ($iPos > strlen($read)) {
688 * @param string $read
690 * @return object AddressStructure object
692 function parseAddress($read, &$i) {
694 for (; $read{$i} != ')'; ++
$i) {
695 $char = strtoupper($read{$i});
697 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
698 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
701 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
710 if (count($arg_a) == 4) {
711 $adr = new AddressStructure();
712 $adr->personal
= $arg_a[0];
713 $adr->adl
= $arg_a[1];
714 $adr->mailbox
= $arg_a[2];
715 $adr->host
= $arg_a[3];
723 * @param string $read
725 * @param object Disposition object or empty string
727 function parseDisposition($read, &$i) {
729 for (; $read{$i} != ')'; ++
$i) {
731 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
732 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
733 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
738 if (isset($arg_a[0])) {
739 $disp = new Disposition($arg_a[0]);
740 if (isset($arg_a[1])) {
741 $disp->properties
= $arg_a[1];
744 return (is_object($disp) ?
$disp : '');
748 * @param string $read
750 * @return object Language object or empty string
752 function parseLanguage($read, &$i) {
753 /* no idea how to process this one without examples */
756 for (; $read{$i} != ')'; ++
$i) {
758 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
759 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
760 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
765 if (isset($arg_a[0])) {
766 $lang = new Language($arg_a[0]);
767 if (isset($arg_a[1])) {
768 $lang->properties
= $arg_a[1];
771 return (is_object($lang) ?
$lang : '');
775 * Parse message text enclosed in parenthesis
776 * @param string $read
780 function parseParenthesis($read, $i) {
781 for (; $read{$i} != ')'; ++
$i) {
783 case '"': $this->parseQuote($read, $i); break;
784 case '{': $this->parseLiteral($read, $i); break;
785 case '(': $this->parseProperties($read, $i); break;
793 * Function to fill the message structure in case the
794 * bodystructure is not available
796 * @param string $read
797 * @param string $type0 message part type
798 * @param string $type1 message part subtype
799 * @return string (only when type0 is not message or multipart)
801 function parseMessage($read, $type0, $type1) {
804 $rfc822_header = true;
805 $mime_header = false;
808 $rfc822_header = false;
811 default: return $read;
814 for ($i = 1; $i < $count; ++
$i) {
815 $line = trim($body[$i]);
816 if (($mime_header ||
$rfc822_header) &&
817 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
820 $bndreg = str_replace("\\", "\\\\", $bndreg);
821 $bndreg = str_replace("?", "\\?", $bndreg);
822 $bndreg = str_replace("+", "\\+", $bndreg);
823 $bndreg = str_replace(".", "\\.", $bndreg);
824 $bndreg = str_replace("/", "\\/", $bndreg);
825 $bndreg = str_replace("-", "\\-", $bndreg);
826 $bndreg = str_replace("(", "\\(", $bndreg);
827 $bndreg = str_replace(")", "\\)", $bndreg);
828 } else if ($rfc822_header && $line == '') {
829 $rfc822_header = false;
830 if ($msg->type0
== 'multipart') {
835 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
836 $cnt = count($boundaries)-1;
837 $bnd = $boundaries[$cnt]['bnd'];
838 $bndreg = $boundaries[$cnt]['bndreg'];
840 $regstr = '/^--'."($bndreg)".".*".'/';
841 if (preg_match($regstr, $line, $reg)) {
842 $bndlen = strlen($reg[1]);
844 if (strlen($line) > ($bndlen +
3)) {
845 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
850 /* calc offset and return $msg */
851 //$entStr = CalcEntity("$entStr", -1);
852 array_pop($boundaries);
858 //$entStr = CalcEntity("$entStr", 0);
869 * @param array $entity
870 * @param array $alt_order
871 * @param boolean $strict
874 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
876 if ($this->type0
== 'multipart') {
877 if($this->type1
== 'alternative') {
878 $msg = $this->findAlternativeEntity($alt_order);
879 if (count($msg->entities
) == 0) {
880 $entity[] = $msg->entity_id
;
882 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
885 } else if ($this->type1
== 'related') { /* RFC 2387 */
886 $msgs = $this->findRelatedEntity();
887 foreach ($msgs as $msg) {
888 if (count($msg->entities
) == 0) {
889 $entity[] = $msg->entity_id
;
891 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
894 if (count($msgs) > 0) {
897 } else { /* Treat as multipart/mixed */
898 foreach ($this->entities
as $ent) {
899 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
900 (!isset($ent->header
->parameters
['filename'])) &&
901 (!isset($ent->header
->parameters
['name'])) &&
902 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
903 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
908 } else { /* If not multipart, then just compare with each entry from $alt_order */
909 $type = $this->type0
.'/'.$this->type1
;
910 // $alt_order[] = "message/rfc822";
911 foreach ($alt_order as $alt) {
912 if( ($alt == $type) && isset($this->entity_id
) ) {
913 if ((count($this->entities
) == 0) &&
914 (!isset($this->header
->parameters
['filename'])) &&
915 (!isset($this->header
->parameters
['name'])) &&
916 isset($this->header
->disposition
) && is_object($this->header
->disposition
) &&
917 !(is_object($this->header
->disposition
) && strtolower($this->header
->disposition
->name
) == 'attachment')) {
918 $entity[] = $this->entity_id
;
925 foreach ($this->entities
as $ent) {
926 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
927 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
928 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
933 if(!$strict && !$found) {
934 if (($this->type0
== 'text') &&
935 in_array($this->type1
, array('plain', 'html', 'message')) &&
936 isset($this->entity_id
)) {
937 if (count($this->entities
) == 0) {
938 if (!is_object($this->header
->disposition
) ||
strtolower($this->header
->disposition
->name
) != 'attachment') {
939 $entity[] = $this->entity_id
;
948 * @param array $alt_order
951 function findAlternativeEntity($alt_order) {
952 /* If we are dealing with alternative parts then we */
953 /* choose the best viewable message supported by SM. */
956 foreach($this->entities
as $ent) {
957 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
958 if ($type == 'multipart/related') {
959 $type = $ent->header
->getParameter('type');
960 // Mozilla bug. Mozilla does not provide the parameter type.
961 if (!$type) $type = 'text/html';
963 $altCount = count($alt_order);
964 for ($j = $best_view; $j < $altCount; ++
$j) {
965 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
977 function findRelatedEntity() {
979 $related_type = $this->header
->getParameter('type');
980 // Mozilla bug. Mozilla does not provide the parameter type.
981 if (!$related_type) $related_type = 'text/html';
982 $entCount = count($this->entities
);
983 for ($i = 0; $i < $entCount; ++
$i) {
984 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
985 if ($related_type == $type) {
986 $msgs[] = $this->entities
[$i];
993 * @param array $exclude_id
994 * @param array $result
997 function getAttachments($exclude_id=array(), $result = array()) {
999 if (($this->type0 == 'message') &&
1000 ($this->type1 == 'rfc822') &&
1001 ($this->entity_id) ) {
1002 $this = $this->entities[0];
1005 if (count($this->entities
)) {
1006 foreach ($this->entities
as $entity) {
1008 foreach ($exclude_id as $excl) {
1009 if ($entity->entity_id
=== $excl) {
1015 if (($entity->type0
== 'multipart') &&
1016 ($entity->type1
!= 'related')) {
1017 $result = $entity->getAttachments($exclude_id, $result);
1018 } else if ($entity->type0
!= 'multipart') {
1019 $result[] = $entity;
1025 foreach ($exclude_id as $excl) {
1026 $exclude = $exclude ||
($this->entity_id
== $excl);
1037 * Add attachment to message object
1038 * @param string $type attachment type
1039 * @param string $name attachment name
1040 * @param string $location path to attachment
1042 function initAttachment($type, $name, $location) {
1043 $attachment = new Message();
1044 $mime_header = new MessageHeader();
1045 $mime_header->setParameter('name', $name);
1046 // FIXME: duplicate code. see ContentType class
1047 $pos = strpos($type, '/');
1049 $mime_header->type0
= substr($type, 0, $pos);
1050 $mime_header->type1
= substr($type, $pos+
1);
1052 $mime_header->type0
= $type;
1054 $attachment->att_local_name
= $location;
1055 $disposition = new Disposition('attachment');
1056 $disposition->properties
['filename'] = $name;
1057 $mime_header->disposition
= $disposition;
1058 $attachment->mime_header
= $mime_header;
1059 $this->entities
[]=$attachment;