6 * Copyright (c) 2003-2005 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
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
29 var $rfc822_header = '',
37 $parent = '', $decoded_body='',
38 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
41 $offset = 0, /* for fetching body parts out of raw messages */
42 $length = 0, /* for fetching body parts out of raw messages */
43 $att_local_name = ''; /* location where the tempory attachment
44 is stored. For future usage in smtp.php */
47 * @param mixed $ent entity id
49 function setEnt($ent) {
50 $this->entity_id
= $ent;
56 function addEntity ($msg) {
57 $this->entities
[] = $msg;
61 * Get file name used for mime part
62 * @return string file name
65 function getFilename() {
67 $header = $this->header
;
68 if (is_object($header->disposition
)) {
69 $filename = $header->disposition
->getProperty('filename');
70 if (trim($filename) == '') {
71 $name = decodeHeader($header->disposition
->getProperty('name'));
73 $name = $header->getParameter('name');
75 if (!trim( $header->id
)) {
76 $filename = 'untitled-[' . $this->entity_id
. ']' ;
78 $filename = 'cid: ' . $header->id
;
88 $filename = $header->getParameter('filename');
89 if (!trim($filename)) {
90 $filename = $header->getParameter('name');
91 if (!trim($filename)) {
92 if (!trim( $header->id
)) {
93 $filename = 'untitled-[' . $this->entity_id
. ']' ;
95 $filename = 'cid: ' . $header->id
;
104 * Add header object to message object.
105 * WARNING: Unfinished code. Don't expect it to work in older sm versions.
106 * @param mixed $read array or string with message headers
107 * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object
109 function addRFC822Header($read) {
110 $header = new Rfc822Header();
111 $this->rfc822_header
= $header->parseHeader($read);
117 function getEntity($ent) {
118 $cur_ent = $this->entity_id
;
120 if (($cur_ent == '') ||
($cur_ent == '0')) {
121 $cur_ent_a = array();
123 $cur_ent_a = explode('.', $this->entity_id
);
125 $ent_a = explode('.', $ent);
127 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
128 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
130 $cur_ent_a = explode('.', $msg->entity_id
);
132 } else if (!isset($cur_ent_a[$i])) {
133 if (isset($msg->entities
[($ent_a[$i]-1)])) {
134 $msg = $msg->entities
[($ent_a[$i]-1)];
136 $msg = $msg->entities
[0];
139 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
140 /*this is a header for a message/rfc822 entity */
141 $msg = $msg->entities
[0];
145 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
146 /*this is a header for a message/rfc822 entity */
147 $msg = $msg->entities
[0];
150 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
151 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
152 $msg = $msg->entities
[($ent_a[$entCount]-1)];
161 * @param string $s message body
163 function setBody($s) {
164 $this->body_part
= $s;
168 * Clean message object
170 function clean_up() {
172 $msg->body_part
= '';
174 foreach ($msg->entities
as $m) {
182 function getMailbox() {
184 while (is_object($msg->parent
)) {
187 return $msg->mailbox
;
191 * Bodystructure parser, a recursive function for generating the
192 * entity-tree with all the mime-parts.
194 * It follows RFC2060 and stores all the described fields in the
199 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
200 * @param string $read
202 * @param mixed $sub_msg
203 * @return object Message object
204 * @todo define argument and return types
206 function parseStructure($read, &$i, $sub_msg = '') {
207 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
208 if($msg) $msg->setEntIds($msg,false,0);
219 function setEntIds(&$msg,$init=false,$i=0) {
220 $iCnt = count($msg->entities
);
221 if ($init !==false) {
223 if ($msg->parent
->type0
== 'message' &&
224 $msg->parent
->type1
== 'rfc822' &&
225 $msg->type0
== 'multipart') {
229 $msg->entity_id
= "$init.$iEntSub";
231 $msg->entity_id
= $iEntSub;
238 for ($i=0;$i<$iCnt;++
$i) {
239 $msg->entities
[$i]->parent
=& $msg;
240 if (strrchr($msg->entity_id
, '.') != '.0') {
241 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
243 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
249 * @param string $read
251 * @param mixed $sub_msg
252 * @return object Message object
254 * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
256 function parseBodyStructure($read, &$i, $sub_msg = '') {
262 $message = new Message();
265 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
266 $char = strtoupper($read{$i});
272 $msg = new Message();
273 $hdr = new MessageHeader();
274 $hdr->type0
= 'text';
275 $hdr->type1
= 'plain';
276 $hdr->encoding
= 'us-ascii';
278 $msg->header
->type0
= 'multipart';
279 $msg->type0
= 'multipart';
280 while ($read{$i} == '(') {
281 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
286 /* multipart properties */
288 $arg_a[] = $msg->parseProperties($read, $i);
292 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
294 $arg_a[] = $msg->parseDisposition($read, $i);
295 } else { /* properties */
296 $arg_a[] = $msg->parseProperties($read, $i);
301 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
303 $arg_a[]= $msg->parseLanguage($read, $i);
306 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
307 $msg->header
->type0
= $arg_a[0];
308 $msg->header
->type1
= $arg_a[1];
309 $msg->type0
= $arg_a[0];
310 $msg->type1
= $arg_a[1];
311 $rfc822_hdr = new Rfc822Header();
312 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
313 while (($i < $cnt) && ($read{$i} != '(')) {
316 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
321 $arg_a[] = $msg->parseDisposition($read, $i);
326 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
327 $arg_a[] = $msg->parseDisposition($read, $i);
329 $arg_a[] = $msg->parseLanguage($read, $i);
334 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
336 $arg_a[] = $msg->parseLanguage($read, $i);
338 $i = $msg->parseParenthesis($read, $i);
339 $arg_a[] = ''; /* not yet described in rfc2060 */
344 /* unknown argument, skip this part */
345 $i = $msg->parseParenthesis($read, $i);
352 /* inside an entity -> start processing */
353 $arg_s = $msg->parseQuote($read, $i);
356 $arg_s = strtolower($arg_s); /* type0 and type1 */
362 /* probably NIL argument */
363 $tmpnil = strtoupper(substr($read, $i, 4));
364 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
371 /* process the literal value */
372 $arg_a[] = $msg->parseLiteral($read, $i);
376 case is_numeric($read{$i}):
377 /* process integers */
378 if ($read{$i} == ' ') { break; }
380 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
381 $i +
= strlen($regs[1])-1;
388 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
390 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
391 $hdr->type0
= $arg_a[0];
392 $hdr->type1
= $arg_a[1];
394 $msg->type0
= $arg_a[0];
395 $msg->type1
= $arg_a[1];
397 if (is_array($arr)) {
398 $hdr->parameters
= $arg_a[2];
400 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
401 $hdr->description
= $arg_a[4];
402 $hdr->encoding
= strtolower($arg_a[5]);
403 $hdr->entity_id
= $msg->entity_id
;
404 $hdr->size
= $arg_a[6];
406 $hdr->lines
= $arg_a[7];
411 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
412 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
413 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
416 $hdr->type0
= 'multipart';
417 $hdr->type1
= $arg_a[0];
418 $msg->type0
= 'multipart';
419 $msg->type1
= $arg_a[0];
420 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
421 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
422 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
429 } /* parsestructure */
432 * @param string $read
436 function parseProperties($read, &$i) {
437 $properties = array();
440 for (; $read{$i} != ')'; ++
$i) {
442 if ($read{$i} == '"') {
443 $arg_s = $this->parseQuote($read, $i);
444 } else if ($read{$i} == '{') {
445 $arg_s = $this->parseLiteral($read, $i);
449 if ($prop_name == '') {
450 $prop_name = strtolower($arg_s);
451 $properties[$prop_name] = '';
452 } else if ($prop_name != '') {
453 $properties[$prop_name] = $arg_s;
462 * @param string $read
464 * @param object $hdr MessageHeader object
465 * @return object MessageHeader object
467 function parseEnvelope($read, &$i, $hdr) {
471 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
472 $char = strtoupper($read{$i});
475 $arg_a[] = $this->parseQuote($read, $i);
479 $arg_a[] = $this->parseLiteral($read, $i);
480 /* temp bugfix (SM 1.5 will have a working clean version)
481 too much work to implement that version right now */
486 /* probably NIL argument */
487 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
494 /* Address structure (with group support)
495 * Note: Group support is useless on SMTP connections
496 * because the protocol doesn't support it
501 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
502 if ($read{$i} == '(') {
503 $addr = $this->parseAddress($read, $i);
504 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
506 $group = $addr->mailbox
;
509 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
511 if ($a == ($j+
1)) { /* no group members */
512 $group_addr->group
= $group;
513 $group_addr->mailbox
= '';
514 $group_addr->personal
= "$group: Undisclosed recipients;";
515 $addr_a[] = $group_addr;
519 $addr->group
= $group;
531 if (count($arg_a) > 9) {
532 $d = strtr($arg_a[0], array(' ' => ' '));
533 $d = explode(' ', $d);
534 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
536 $hdr->date
= getTimeStamp($d); /* argument 1: date */
537 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
538 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
539 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
540 $hdr->replyto
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
541 $hdr->to
= $arg_a[5]; /* argument 6: to */
542 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
543 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
544 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
545 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
551 * @param string $read
556 function parseLiteral($read, &$i) {
559 $iPos = strpos($read,'}',$i);
561 $lit_cnt = substr($read, $i, $iPos - $i);
562 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
563 /* Now read the literal */
564 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
566 /* temp bugfix (SM 1.5 will have a working clean version)
567 too much work to implement that version right now */
569 } else { /* should never happen */
570 $i +
= 3; /* } + \r + \n */
577 * @param string $read
582 function parseQuote($read, &$i) {
586 $iPos = strpos($read,'"',$iPos);
588 if ($iPos && $read{$iPos -1} != '\\') {
589 $s = substr($read,$i,($iPos-$i));
594 if ($iPos > strlen($read)) {
602 * @param string $read
604 * @return object AddressStructure object
606 function parseAddress($read, &$i) {
608 for (; $read{$i} != ')'; ++
$i) {
609 $char = strtoupper($read{$i});
611 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
612 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
615 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
624 if (count($arg_a) == 4) {
625 $adr = new AddressStructure();
626 $adr->personal
= $arg_a[0];
627 $adr->adl
= $arg_a[1];
628 $adr->mailbox
= $arg_a[2];
629 $adr->host
= $arg_a[3];
637 * @param string $read
639 * @param object Disposition object or empty string
641 function parseDisposition($read, &$i) {
643 for (; $read{$i} != ')'; ++
$i) {
645 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
646 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
647 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
652 if (isset($arg_a[0])) {
653 $disp = new Disposition($arg_a[0]);
654 if (isset($arg_a[1])) {
655 $disp->properties
= $arg_a[1];
659 return (is_object($disp) ?
$disp : '');
663 * @param string $read
665 * @return object Language object or empty string
667 function parseLanguage($read, &$i) {
668 /* no idea how to process this one without examples */
671 for (; $read{$i} != ')'; ++
$i) {
673 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
674 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
675 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
680 if (isset($arg_a[0])) {
681 $lang = new Language($arg_a[0]);
682 if (isset($arg_a[1])) {
683 $lang->properties
= $arg_a[1];
687 return (is_object($lang) ?
$lang : '');
691 * Parse message text enclosed in parenthesis
692 * @param string $read
696 function parseParenthesis($read, $i) {
697 for (; $read{$i} != ')'; ++
$i) {
699 case '"': $this->parseQuote($read, $i); break;
700 case '{': $this->parseLiteral($read, $i); break;
701 case '(': $this->parseProperties($read, $i); break;
709 * Function to fill the message structure in case the
710 * bodystructure is not available
712 * @param string $read
713 * @param string $type0 message part type
714 * @param string $type1 message part subtype
717 function parseMessage($read, $type0, $type1) {
720 $rfc822_header = true;
721 $mime_header = false;
724 $rfc822_header = false;
727 default: return $read;
730 for ($i = 1; $i < $count; ++
$i) {
731 $line = trim($body[$i]);
732 if (($mime_header ||
$rfc822_header) &&
733 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
736 $bndreg = str_replace("\\", "\\\\", $bndreg);
737 $bndreg = str_replace("?", "\\?", $bndreg);
738 $bndreg = str_replace("+", "\\+", $bndreg);
739 $bndreg = str_replace(".", "\\.", $bndreg);
740 $bndreg = str_replace("/", "\\/", $bndreg);
741 $bndreg = str_replace("-", "\\-", $bndreg);
742 $bndreg = str_replace("(", "\\(", $bndreg);
743 $bndreg = str_replace(")", "\\)", $bndreg);
744 } else if ($rfc822_header && $line == '') {
745 $rfc822_header = false;
746 if ($msg->type0
== 'multipart') {
751 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
752 $cnt = count($boundaries)-1;
753 $bnd = $boundaries[$cnt]['bnd'];
754 $bndreg = $boundaries[$cnt]['bndreg'];
756 $regstr = '/^--'."($bndreg)".".*".'/';
757 if (preg_match($regstr, $line, $reg)) {
758 $bndlen = strlen($reg[1]);
760 if (strlen($line) > ($bndlen +
3)) {
761 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
766 /* calc offset and return $msg */
767 //$entStr = CalcEntity("$entStr", -1);
768 array_pop($boundaries);
774 //$entStr = CalcEntity("$entStr", 0);
785 * @param array $entity
786 * @param array $alt_order
787 * @param boolean $strict
790 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
792 if ($this->type0
== 'multipart') {
793 if($this->type1
== 'alternative') {
794 $msg = $this->findAlternativeEntity($alt_order);
795 if (count($msg->entities
) == 0) {
796 $entity[] = $msg->entity_id
;
798 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
801 } else if ($this->type1
== 'related') { /* RFC 2387 */
802 $msgs = $this->findRelatedEntity();
803 foreach ($msgs as $msg) {
804 if (count($msg->entities
) == 0) {
805 $entity[] = $msg->entity_id
;
807 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
810 if (count($msgs) > 0) {
813 } else { /* Treat as multipart/mixed */
814 foreach ($this->entities
as $ent) {
815 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
816 (!isset($ent->header
->parameters
['filename'])) &&
817 (!isset($ent->header
->parameters
['name'])) &&
818 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
819 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
824 } else { /* If not multipart, then just compare with each entry from $alt_order */
825 $type = $this->type0
.'/'.$this->type1
;
826 // $alt_order[] = "message/rfc822";
827 foreach ($alt_order as $alt) {
828 if( ($alt == $type) && isset($this->entity_id
) ) {
829 if ((count($this->entities
) == 0) &&
830 (!isset($this->header
->parameters
['filename'])) &&
831 (!isset($this->header
->parameters
['name'])) &&
832 isset($this->header
->disposition
) && is_object($this->header
->disposition
) &&
833 !(is_object($this->header
->disposition
) && strtolower($this->header
->disposition
->name
) == 'attachment')) {
834 $entity[] = $this->entity_id
;
841 foreach ($this->entities
as $ent) {
842 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
843 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
844 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
849 if(!$strict && !$found) {
850 if (($this->type0
== 'text') &&
851 in_array($this->type1
, array('plain', 'html', 'message')) &&
852 isset($this->entity_id
)) {
853 if (count($this->entities
) == 0) {
854 if (!is_object($this->header
->disposition
) ||
strtolower($this->header
->disposition
->name
) != 'attachment') {
855 $entity[] = $this->entity_id
;
864 * @param array $alt_order
867 function findAlternativeEntity($alt_order) {
868 /* If we are dealing with alternative parts then we */
869 /* choose the best viewable message supported by SM. */
872 foreach($this->entities
as $ent) {
873 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
874 if ($type == 'multipart/related') {
875 $type = $ent->header
->getParameter('type');
876 // Mozilla bug. Mozilla does not provide the parameter type.
877 if (!$type) $type = 'text/html';
879 $altCount = count($alt_order);
880 for ($j = $best_view; $j < $altCount; ++
$j) {
881 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
894 function findRelatedEntity() {
896 $related_type = $this->header
->getParameter('type');
897 // Mozilla bug. Mozilla does not provide the parameter type.
898 if (!$related_type) $related_type = 'text/html';
899 $entCount = count($this->entities
);
900 for ($i = 0; $i < $entCount; ++
$i) {
901 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
902 if ($related_type == $type) {
903 $msgs[] = $this->entities
[$i];
910 * @param array $exclude_id
911 * @param array $result
914 function getAttachments($exclude_id=array(), $result = array()) {
916 if (($this->type0 == 'message') &&
917 ($this->type1 == 'rfc822') &&
918 ($this->entity_id) ) {
919 $this = $this->entities[0];
922 if (count($this->entities
)) {
923 foreach ($this->entities
as $entity) {
925 foreach ($exclude_id as $excl) {
926 if ($entity->entity_id
=== $excl) {
932 if (($entity->type0
== 'multipart') &&
933 ($entity->type1
!= 'related')) {
934 $result = $entity->getAttachments($exclude_id, $result);
935 } else if ($entity->type0
!= 'multipart') {
942 foreach ($exclude_id as $excl) {
943 $exclude = $exclude ||
($this->entity_id
== $excl);
954 * Add attachment to message object
955 * @param string $type attachment type
956 * @param string $name attachment name
957 * @param string $location path to attachment
959 function initAttachment($type, $name, $location) {
960 $attachment = new Message();
961 $mime_header = new MessageHeader();
962 $mime_header->setParameter('name', $name);
963 // FIXME: duplicate code. see ContentType class
964 $pos = strpos($type, '/');
966 $mime_header->type0
= substr($type, 0, $pos);
967 $mime_header->type1
= substr($type, $pos+
1);
969 $mime_header->type0
= $type;
971 $attachment->att_local_name
= $location;
972 $disposition = new Disposition('attachment');
973 $disposition->properties
['filename'] = $name;
974 $mime_header->disposition
= $disposition;
975 $attachment->mime_header
= $mime_header;
976 $this->entities
[]=$attachment;