6 * This file contains functions needed to handle mime messages.
8 * @copyright © 2003-2005 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
17 * The object that contains a message
19 * message is the object that contains messages. It is a recursive
20 * object in that through the $entities variable, it can contain
21 * more objects of type message. See documentation in mime.txt for
22 * a better description of how this works.
23 * @package squirrelmail
32 var $rfc822_header = '';
34 * MessageHeader object
37 var $mime_header = '';
56 var $entities = array();
63 * Parent message part id
80 * Message \seen status
85 * Message \answered status
90 * Message \deleted status
95 * Message \flagged status
110 * Message part offset
111 * for fetching body parts out of raw messages
116 * Message part length
117 * for fetching body parts out of raw messages
122 * Local attachment filename
123 * location where the tempory attachment
124 * is stored. For use in delivery class.
127 var $att_local_name = '';
130 * @param string $ent entity id
132 function setEnt($ent) {
133 $this->entity_id
= $ent;
137 * Add nested message part
140 function addEntity ($msg) {
141 $this->entities
[] = $msg;
145 * Get file name used for mime part
146 * @return string file name
149 function getFilename() {
151 $header = $this->header
;
152 if (is_object($header->disposition
)) {
153 $filename = $header->disposition
->getProperty('filename');
154 if (trim($filename) == '') {
155 $name = decodeHeader($header->disposition
->getProperty('name'));
157 $name = $header->getParameter('name');
159 if (!trim( $header->id
)) {
160 $filename = 'untitled-[' . $this->entity_id
. ']' ;
162 $filename = 'cid: ' . $header->id
;
172 $filename = $header->getParameter('filename');
173 if (!trim($filename)) {
174 $filename = $header->getParameter('name');
175 if (!trim($filename)) {
176 if (!trim( $header->id
)) {
177 $filename = 'untitled-[' . $this->entity_id
. ']' ;
179 $filename = 'cid: ' . $header->id
;
188 * Add header object to message object.
189 * WARNING: Unfinished code. Don't expect it to work in older sm versions.
190 * @param mixed $read array or string with message headers
191 * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object
193 function addRFC822Header($read) {
194 $header = new Rfc822Header();
195 $this->rfc822_header
= $header->parseHeader($read);
200 * @return mixed (object or string?)
202 function getEntity($ent) {
203 $cur_ent = $this->entity_id
;
205 if (($cur_ent == '') ||
($cur_ent == '0')) {
206 $cur_ent_a = array();
208 $cur_ent_a = explode('.', $this->entity_id
);
210 $ent_a = explode('.', $ent);
212 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
213 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
215 $cur_ent_a = explode('.', $msg->entity_id
);
217 } else if (!isset($cur_ent_a[$i])) {
218 if (isset($msg->entities
[($ent_a[$i]-1)])) {
219 $msg = $msg->entities
[($ent_a[$i]-1)];
221 $msg = $msg->entities
[0];
224 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
225 /*this is a header for a message/rfc822 entity */
226 $msg = $msg->entities
[0];
230 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
231 /*this is a header for a message/rfc822 entity */
232 $msg = $msg->entities
[0];
235 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
236 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
237 $msg = $msg->entities
[($ent_a[$entCount]-1)];
246 * @param string $s message body
248 function setBody($s) {
249 $this->body_part
= $s;
253 * Clean message object
255 function clean_up() {
257 $msg->body_part
= '';
259 foreach ($msg->entities
as $m) {
267 function getMailbox() {
269 while (is_object($msg->parent
)) {
272 return $msg->mailbox
;
276 * Bodystructure parser, a recursive function for generating the
277 * entity-tree with all the mime-parts.
279 * It follows RFC2060 and stores all the described fields in the
284 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
285 * @param string $read
287 * @param mixed $sub_msg
288 * @return object Message object
289 * @todo define argument and return types
291 function parseStructure($read, &$i, $sub_msg = '') {
292 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
293 if($msg) $msg->setEntIds($msg,false,0);
304 function setEntIds(&$msg,$init=false,$i=0) {
305 $iCnt = count($msg->entities
);
306 if ($init !==false) {
308 if ($msg->parent
->type0
== 'message' &&
309 $msg->parent
->type1
== 'rfc822' &&
310 $msg->type0
== 'multipart') {
314 $msg->entity_id
= "$init.$iEntSub";
316 $msg->entity_id
= $iEntSub;
323 for ($i=0;$i<$iCnt;++
$i) {
324 $msg->entities
[$i]->parent
=& $msg;
325 if (strrchr($msg->entity_id
, '.') != '.0') {
326 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
328 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
334 * @param string $read
336 * @param mixed $sub_msg
337 * @return object Message object
339 * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
341 function parseBodyStructure($read, &$i, $sub_msg = '') {
347 $message = new Message();
350 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
351 $char = strtoupper($read{$i});
357 $msg = new Message();
358 $hdr = new MessageHeader();
359 $hdr->type0
= 'text';
360 $hdr->type1
= 'plain';
361 $hdr->encoding
= 'us-ascii';
363 $msg->header
->type0
= 'multipart';
364 $msg->type0
= 'multipart';
365 while ($read{$i} == '(') {
366 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
371 /* multipart properties */
373 $arg_a[] = $msg->parseProperties($read, $i);
377 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
379 $arg_a[] = $msg->parseDisposition($read, $i);
380 } else { /* properties */
381 $arg_a[] = $msg->parseProperties($read, $i);
386 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
388 $arg_a[]= $msg->parseLanguage($read, $i);
391 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
392 $msg->header
->type0
= $arg_a[0];
393 $msg->header
->type1
= $arg_a[1];
394 $msg->type0
= $arg_a[0];
395 $msg->type1
= $arg_a[1];
396 $rfc822_hdr = new Rfc822Header();
397 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
398 while (($i < $cnt) && ($read{$i} != '(')) {
401 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
406 $arg_a[] = $msg->parseDisposition($read, $i);
411 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
412 $arg_a[] = $msg->parseDisposition($read, $i);
414 $arg_a[] = $msg->parseLanguage($read, $i);
419 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
421 $arg_a[] = $msg->parseLanguage($read, $i);
423 $i = $msg->parseParenthesis($read, $i);
424 $arg_a[] = ''; /* not yet described in rfc2060 */
429 /* unknown argument, skip this part */
430 $i = $msg->parseParenthesis($read, $i);
437 /* inside an entity -> start processing */
438 $arg_s = $msg->parseQuote($read, $i);
441 $arg_s = strtolower($arg_s); /* type0 and type1 */
447 /* probably NIL argument */
448 $tmpnil = strtoupper(substr($read, $i, 4));
449 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
456 /* process the literal value */
457 $arg_a[] = $msg->parseLiteral($read, $i);
461 case is_numeric($read{$i}):
462 /* process integers */
463 if ($read{$i} == ' ') { break; }
465 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
466 $i +
= strlen($regs[1])-1;
473 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
475 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
476 $hdr->type0
= $arg_a[0];
477 $hdr->type1
= $arg_a[1];
479 $msg->type0
= $arg_a[0];
480 $msg->type1
= $arg_a[1];
482 if (is_array($arr)) {
483 $hdr->parameters
= $arg_a[2];
485 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
486 $hdr->description
= $arg_a[4];
487 $hdr->encoding
= strtolower($arg_a[5]);
488 $hdr->entity_id
= $msg->entity_id
;
489 $hdr->size
= $arg_a[6];
491 $hdr->lines
= $arg_a[7];
496 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
497 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
498 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
501 $hdr->type0
= 'multipart';
502 $hdr->type1
= $arg_a[0];
503 $msg->type0
= 'multipart';
504 $msg->type1
= $arg_a[0];
505 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
506 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
507 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
514 } /* parsestructure */
517 * @param string $read
521 function parseProperties($read, &$i) {
522 $properties = array();
525 for (; $read{$i} != ')'; ++
$i) {
527 if ($read{$i} == '"') {
528 $arg_s = $this->parseQuote($read, $i);
529 } else if ($read{$i} == '{') {
530 $arg_s = $this->parseLiteral($read, $i);
534 if ($prop_name == '') {
535 $prop_name = strtolower($arg_s);
536 $properties[$prop_name] = '';
537 } else if ($prop_name != '') {
538 $properties[$prop_name] = $arg_s;
547 * @param string $read
549 * @param object $hdr MessageHeader object
550 * @return object MessageHeader object
552 function parseEnvelope($read, &$i, $hdr) {
556 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
557 $char = strtoupper($read{$i});
560 $arg_a[] = $this->parseQuote($read, $i);
564 $arg_a[] = $this->parseLiteral($read, $i);
565 /* temp bugfix (SM 1.5 will have a working clean version)
566 too much work to implement that version right now */
571 /* probably NIL argument */
572 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
579 /* Address structure (with group support)
580 * Note: Group support is useless on SMTP connections
581 * because the protocol doesn't support it
586 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
587 if ($read{$i} == '(') {
588 $addr = $this->parseAddress($read, $i);
589 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
591 $group = $addr->mailbox
;
594 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
596 if ($a == ($j+
1)) { /* no group members */
597 $group_addr->group
= $group;
598 $group_addr->mailbox
= '';
599 $group_addr->personal
= "$group: Undisclosed recipients;";
600 $addr_a[] = $group_addr;
604 $addr->group
= $group;
616 if (count($arg_a) > 9) {
617 $d = strtr($arg_a[0], array(' ' => ' '));
618 $d = explode(' ', $d);
619 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
621 $hdr->date
= getTimeStamp($d); /* argument 1: date */
622 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
623 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
624 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
625 $hdr->replyto
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
626 $hdr->to
= $arg_a[5]; /* argument 6: to */
627 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
628 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
629 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
630 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
636 * @param string $read
641 function parseLiteral($read, &$i) {
644 $iPos = strpos($read,'}',$i);
646 $lit_cnt = substr($read, $i, $iPos - $i);
647 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
648 /* Now read the literal */
649 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
651 /* temp bugfix (SM 1.5 will have a working clean version)
652 too much work to implement that version right now */
654 } else { /* should never happen */
655 $i +
= 3; /* } + \r + \n */
662 * @param string $read
667 function parseQuote($read, &$i) {
671 $iPos = strpos($read,'"',$iPos);
673 if ($iPos && $read{$iPos -1} != '\\') {
674 $s = substr($read,$i,($iPos-$i));
679 if ($iPos > strlen($read)) {
687 * @param string $read
689 * @return object AddressStructure object
691 function parseAddress($read, &$i) {
693 for (; $read{$i} != ')'; ++
$i) {
694 $char = strtoupper($read{$i});
696 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
697 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
700 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
709 if (count($arg_a) == 4) {
710 $adr = new AddressStructure();
711 $adr->personal
= $arg_a[0];
712 $adr->adl
= $arg_a[1];
713 $adr->mailbox
= $arg_a[2];
714 $adr->host
= $arg_a[3];
722 * @param string $read
724 * @param object Disposition object or empty string
726 function parseDisposition($read, &$i) {
728 for (; $read{$i} != ')'; ++
$i) {
730 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
731 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
732 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
737 if (isset($arg_a[0])) {
738 $disp = new Disposition($arg_a[0]);
739 if (isset($arg_a[1])) {
740 $disp->properties
= $arg_a[1];
743 return (is_object($disp) ?
$disp : '');
747 * @param string $read
749 * @return object Language object or empty string
751 function parseLanguage($read, &$i) {
752 /* no idea how to process this one without examples */
755 for (; $read{$i} != ')'; ++
$i) {
757 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
758 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
759 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
764 if (isset($arg_a[0])) {
765 $lang = new Language($arg_a[0]);
766 if (isset($arg_a[1])) {
767 $lang->properties
= $arg_a[1];
770 return (is_object($lang) ?
$lang : '');
774 * Parse message text enclosed in parenthesis
775 * @param string $read
779 function parseParenthesis($read, $i) {
780 for (; $read{$i} != ')'; ++
$i) {
782 case '"': $this->parseQuote($read, $i); break;
783 case '{': $this->parseLiteral($read, $i); break;
784 case '(': $this->parseProperties($read, $i); break;
792 * Function to fill the message structure in case the
793 * bodystructure is not available
795 * @param string $read
796 * @param string $type0 message part type
797 * @param string $type1 message part subtype
798 * @return string (only when type0 is not message or multipart)
800 function parseMessage($read, $type0, $type1) {
803 $rfc822_header = true;
804 $mime_header = false;
807 $rfc822_header = false;
810 default: return $read;
813 for ($i = 1; $i < $count; ++
$i) {
814 $line = trim($body[$i]);
815 if (($mime_header ||
$rfc822_header) &&
816 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
819 $bndreg = str_replace("\\", "\\\\", $bndreg);
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 } else if ($rfc822_header && $line == '') {
828 $rfc822_header = false;
829 if ($msg->type0
== 'multipart') {
834 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
835 $cnt = count($boundaries)-1;
836 $bnd = $boundaries[$cnt]['bnd'];
837 $bndreg = $boundaries[$cnt]['bndreg'];
839 $regstr = '/^--'."($bndreg)".".*".'/';
840 if (preg_match($regstr, $line, $reg)) {
841 $bndlen = strlen($reg[1]);
843 if (strlen($line) > ($bndlen +
3)) {
844 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
849 /* calc offset and return $msg */
850 //$entStr = CalcEntity("$entStr", -1);
851 array_pop($boundaries);
857 //$entStr = CalcEntity("$entStr", 0);
868 * @param array $entity
869 * @param array $alt_order
870 * @param boolean $strict
873 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
875 if ($this->type0
== 'multipart') {
876 if($this->type1
== 'alternative') {
877 $msg = $this->findAlternativeEntity($alt_order);
878 if (count($msg->entities
) == 0) {
879 $entity[] = $msg->entity_id
;
881 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
884 } else if ($this->type1
== 'related') { /* RFC 2387 */
885 $msgs = $this->findRelatedEntity();
886 foreach ($msgs as $msg) {
887 if (count($msg->entities
) == 0) {
888 $entity[] = $msg->entity_id
;
890 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
893 if (count($msgs) > 0) {
896 } else { /* Treat as multipart/mixed */
897 foreach ($this->entities
as $ent) {
898 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
899 (!isset($ent->header
->parameters
['filename'])) &&
900 (!isset($ent->header
->parameters
['name'])) &&
901 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
902 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
907 } else { /* If not multipart, then just compare with each entry from $alt_order */
908 $type = $this->type0
.'/'.$this->type1
;
909 // $alt_order[] = "message/rfc822";
910 foreach ($alt_order as $alt) {
911 if( ($alt == $type) && isset($this->entity_id
) ) {
912 if ((count($this->entities
) == 0) &&
913 (!isset($this->header
->parameters
['filename'])) &&
914 (!isset($this->header
->parameters
['name'])) &&
915 isset($this->header
->disposition
) && is_object($this->header
->disposition
) &&
916 !(is_object($this->header
->disposition
) && strtolower($this->header
->disposition
->name
) == 'attachment')) {
917 $entity[] = $this->entity_id
;
924 foreach ($this->entities
as $ent) {
925 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
926 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
927 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
932 if(!$strict && !$found) {
933 if (($this->type0
== 'text') &&
934 in_array($this->type1
, array('plain', 'html', 'message')) &&
935 isset($this->entity_id
)) {
936 if (count($this->entities
) == 0) {
937 if (!is_object($this->header
->disposition
) ||
strtolower($this->header
->disposition
->name
) != 'attachment') {
938 $entity[] = $this->entity_id
;
947 * @param array $alt_order
950 function findAlternativeEntity($alt_order) {
951 /* If we are dealing with alternative parts then we */
952 /* choose the best viewable message supported by SM. */
955 foreach($this->entities
as $ent) {
956 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
957 if ($type == 'multipart/related') {
958 $type = $ent->header
->getParameter('type');
959 // Mozilla bug. Mozilla does not provide the parameter type.
960 if (!$type) $type = 'text/html';
962 $altCount = count($alt_order);
963 for ($j = $best_view; $j < $altCount; ++
$j) {
964 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
976 function findRelatedEntity() {
978 $related_type = $this->header
->getParameter('type');
979 // Mozilla bug. Mozilla does not provide the parameter type.
980 if (!$related_type) $related_type = 'text/html';
981 $entCount = count($this->entities
);
982 for ($i = 0; $i < $entCount; ++
$i) {
983 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
984 if ($related_type == $type) {
985 $msgs[] = $this->entities
[$i];
992 * @param array $exclude_id
993 * @param array $result
996 function getAttachments($exclude_id=array(), $result = array()) {
998 if (($this->type0 == 'message') &&
999 ($this->type1 == 'rfc822') &&
1000 ($this->entity_id) ) {
1001 $this = $this->entities[0];
1004 if (count($this->entities
)) {
1005 foreach ($this->entities
as $entity) {
1007 foreach ($exclude_id as $excl) {
1008 if ($entity->entity_id
=== $excl) {
1014 if (($entity->type0
== 'multipart') &&
1015 ($entity->type1
!= 'related')) {
1016 $result = $entity->getAttachments($exclude_id, $result);
1017 } else if ($entity->type0
!= 'multipart') {
1018 $result[] = $entity;
1024 foreach ($exclude_id as $excl) {
1025 $exclude = $exclude ||
($this->entity_id
== $excl);
1036 * Add attachment to message object
1037 * @param string $type attachment type
1038 * @param string $name attachment name
1039 * @param string $location path to attachment
1041 function initAttachment($type, $name, $location) {
1042 $attachment = new Message();
1043 $mime_header = new MessageHeader();
1044 $mime_header->setParameter('name', $name);
1045 // FIXME: duplicate code. see ContentType class
1046 $pos = strpos($type, '/');
1048 $mime_header->type0
= substr($type, 0, $pos);
1049 $mime_header->type1
= substr($type, $pos+
1);
1051 $mime_header->type0
= $type;
1053 $attachment->att_local_name
= $location;
1054 $disposition = new Disposition('attachment');
1055 $disposition->properties
['filename'] = $name;
1056 $mime_header->disposition
= $disposition;
1057 $attachment->mime_header
= $mime_header;
1058 $this->entities
[]=$attachment;