e55fccdeb3dac673a3a0f6e7589f2ed7ca98acc7
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
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() {
52 $header = $this->header
;
53 if (is_object($header->disposition
)) {
54 $filename = $header->disposition
->getProperty('filename');
55 if (trim($filename) == '') {
56 $name = decodeHeader($header->disposition
->getProperty('name'));
58 $name = $header->getParameter('name');
60 if (!trim( $header->id
)) {
61 $filename = 'untitled-[' . $this->entity_id
. ']' ;
63 $filename = 'cid: ' . $header->id
;
73 $filename = $header->getParameter('filename');
74 if (!trim($filename)) {
75 $filename = $header->getParameter('name');
76 if (!trim($filename)) {
77 if (!trim( $header->id
)) {
78 $filename = 'untitled-[' . $this->entity_id
. ']' ;
80 $filename = 'cid: ' . $header->id
;
89 function addRFC822Header($read) {
90 $header = new Rfc822Header();
91 $this->rfc822_header
= $header->parseHeader($read);
94 function getEntity($ent) {
95 $cur_ent = $this->entity_id
;
97 if (($cur_ent == '') ||
($cur_ent == '0')) {
100 $cur_ent_a = explode('.', $this->entity_id
);
102 $ent_a = explode('.', $ent);
104 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
105 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
107 $cur_ent_a = explode('.', $msg->entity_id
);
109 } else if (!isset($cur_ent_a[$i])) {
110 if (isset($msg->entities
[($ent_a[$i]-1)])) {
111 $msg = $msg->entities
[($ent_a[$i]-1)];
113 $msg = $msg->entities
[0];
116 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
117 /*this is a header for a message/rfc822 entity */
118 $msg = $msg->entities
[0];
122 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
123 /*this is a header for a message/rfc822 entity */
124 $msg = $msg->entities
[0];
127 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
128 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
129 $msg = $msg->entities
[($ent_a[$entCount]-1)];
136 function setBody($s) {
137 $this->body_part
= $s;
140 function clean_up() {
142 $msg->body_part
= '';
144 foreach ($msg->entities
as $m) {
149 function getMailbox() {
151 while (is_object($msg->parent
)) {
154 return $msg->mailbox
;
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, $sub_msg = '') {
170 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
171 if($msg) $msg->setEntIds($msg,false,0);
175 function setEntIds(&$msg,$init=false,$i=0) {
176 $iCnt = count($msg->entities
);
177 if ($init !==false) {
179 if ($msg->parent
->type0
== 'message' &&
180 $msg->parent
->type1
== 'rfc822' &&
181 $msg->type0
== 'multipart') {
185 $msg->entity_id
= "$init.$iEntSub";
187 $msg->entity_id
= $iEntSub;
194 for ($i=0;$i<$iCnt;++
$i) {
195 $msg->entities
[$i]->parent
=& $msg;
196 if (strrchr($msg->entity_id
, '.') != '.0') {
197 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
199 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
204 function parseBodyStructure($read, &$i, $sub_msg = '') {
210 $message = new Message();
213 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
214 $char = strtoupper($read{$i});
220 $msg = new Message();
221 $hdr = new MessageHeader();
222 $hdr->type0
= 'text';
223 $hdr->type1
= 'plain';
224 $hdr->encoding
= 'us-ascii';
226 $msg->header
->type0
= 'multipart';
227 $msg->type0
= 'multipart';
228 while ($read{$i} == '(') {
229 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
234 /* multipart properties */
236 $arg_a[] = $msg->parseProperties($read, $i);
240 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
242 $arg_a[] = $msg->parseDisposition($read, $i);
243 } else { /* properties */
244 $arg_a[] = $msg->parseProperties($read, $i);
249 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
251 $arg_a[]= $msg->parseLanguage($read, $i);
254 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
255 $msg->header
->type0
= $arg_a[0];
256 $msg->header
->type1
= $arg_a[1];
257 $msg->type0
= $arg_a[0];
258 $msg->type1
= $arg_a[1];
259 $rfc822_hdr = new Rfc822Header();
260 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
261 while (($i < $cnt) && ($read{$i} != '(')) {
264 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
269 $arg_a[] = $msg->parseDisposition($read, $i);
274 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
275 $arg_a[] = $msg->parseDisposition($read, $i);
277 $arg_a[] = $msg->parseLanguage($read, $i);
282 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
284 $arg_a[] = $msg->parseLanguage($read, $i);
286 $i = $msg->parseParenthesis($read, $i);
287 $arg_a[] = ''; /* not yet described in rfc2060 */
292 /* unknown argument, skip this part */
293 $i = $msg->parseParenthesis($read, $i);
300 /* inside an entity -> start processing */
301 $arg_s = $msg->parseQuote($read, $i);
304 $arg_s = strtolower($arg_s); /* type0 and type1 */
310 /* probably NIL argument */
311 $tmpnil = strtoupper(substr($read, $i, 4));
312 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
319 /* process the literal value */
320 $arg_a[] = $msg->parseLiteral($read, $i);
324 case is_numeric($read{$i}):
325 /* process integers */
326 if ($read{$i} == ' ') { break; }
328 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
329 $i +
= strlen($regs[1])-1;
336 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
338 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
339 $hdr->type0
= $arg_a[0];
340 $hdr->type1
= $arg_a[1];
342 $msg->type0
= $arg_a[0];
343 $msg->type1
= $arg_a[1];
345 if (is_array($arr)) {
346 $hdr->parameters
= $arg_a[2];
348 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
349 $hdr->description
= $arg_a[4];
350 $hdr->encoding
= strtolower($arg_a[5]);
351 $hdr->entity_id
= $msg->entity_id
;
352 $hdr->size
= $arg_a[6];
354 $hdr->lines
= $arg_a[7];
359 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
360 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
361 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
364 $hdr->type0
= 'multipart';
365 $hdr->type1
= $arg_a[0];
366 $msg->type0
= 'multipart';
367 $msg->type1
= $arg_a[0];
368 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
369 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
370 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
377 } /* parsestructure */
379 function parseProperties($read, &$i) {
380 $properties = array();
383 for (; $read{$i} != ')'; ++
$i) {
385 if ($read{$i} == '"') {
386 $arg_s = $this->parseQuote($read, $i);
387 } else if ($read{$i} == '{') {
388 $arg_s = $this->parseLiteral($read, $i);
392 if ($prop_name == '') {
393 $prop_name = strtolower($arg_s);
394 $properties[$prop_name] = '';
395 } else if ($prop_name != '') {
396 $properties[$prop_name] = $arg_s;
404 function parseEnvelope($read, &$i, $hdr) {
408 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
409 $char = strtoupper($read{$i});
412 $arg_a[] = $this->parseQuote($read, $i);
416 $arg_a[] = $this->parseLiteral($read, $i);
417 /* temp bugfix (SM 1.5 will have a working clean version)
418 too much work to implement that version right now */
423 /* probably NIL argument */
424 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
431 /* Address structure (with group support)
432 * Note: Group support is useless on SMTP connections
433 * because the protocol doesn't support it
438 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
439 if ($read{$i} == '(') {
440 $addr = $this->parseAddress($read, $i);
441 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
443 $group = $addr->mailbox
;
446 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
448 if ($a == ($j+
1)) { /* no group members */
449 $group_addr->group
= $group;
450 $group_addr->mailbox
= '';
451 $group_addr->personal
= "$group: Undisclosed recipients;";
452 $addr_a[] = $group_addr;
456 $addr->group
= $group;
468 if (count($arg_a) > 9) {
469 $d = strtr($arg_a[0], array(' ' => ' '));
470 $d = explode(' ', $d);
471 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
473 $hdr->date
= getTimeStamp($d); /* argument 1: date */
474 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
475 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
476 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
477 $hdr->replyto
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
478 $hdr->to
= $arg_a[5]; /* argument 6: to */
479 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
480 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
481 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
482 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
487 function parseLiteral($read, &$i) {
490 $iPos = strpos($read,'}',$i);
492 $lit_cnt = substr($read, $i, $iPos - $i);
493 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
494 /* Now read the literal */
495 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
497 /* temp bugfix (SM 1.5 will have a working clean version)
498 too much work to implement that version right now */
500 } else { /* should never happen */
501 $i +
= 3; /* } + \r + \n */
507 function parseQuote($read, &$i) {
511 $iPos = strpos($read,'"',$iPos);
513 if ($iPos && $read{$iPos -1} != '\\') {
514 $s = substr($read,$i,($iPos-$i));
519 if ($iPos > strlen($read)) {
526 function parseAddress($read, &$i) {
528 for (; $read{$i} != ')'; ++
$i) {
529 $char = strtoupper($read{$i});
531 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
532 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
535 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
544 if (count($arg_a) == 4) {
545 $adr = new AddressStructure();
546 $adr->personal
= $arg_a[0];
547 $adr->adl
= $arg_a[1];
548 $adr->mailbox
= $arg_a[2];
549 $adr->host
= $arg_a[3];
556 function parseDisposition($read, &$i) {
558 for (; $read{$i} != ')'; ++
$i) {
560 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
561 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
562 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
567 if (isset($arg_a[0])) {
568 $disp = new Disposition($arg_a[0]);
569 if (isset($arg_a[1])) {
570 $disp->properties
= $arg_a[1];
574 return (is_object($disp) ?
$disp : '');
577 function parseLanguage($read, &$i) {
578 /* no idea how to process this one without examples */
581 for (; $read{$i} != ')'; ++
$i) {
583 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
584 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
585 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
590 if (isset($arg_a[0])) {
591 $lang = new Language($arg_a[0]);
592 if (isset($arg_a[1])) {
593 $lang->properties
= $arg_a[1];
597 return (is_object($lang) ?
$lang : '');
600 function parseParenthesis($read, $i) {
601 for (; $read{$i} != ')'; ++
$i) {
603 case '"': $this->parseQuote($read, $i); break;
604 case '{': $this->parseLiteral($read, $i); break;
605 case '(': $this->parseProperties($read, $i); break;
612 /* Function to fill the message structure in case the */
613 /* bodystructure is not available NOT FINISHED YET */
614 function parseMessage($read, $type0, $type1) {
617 $rfc822_header = true;
618 $mime_header = false;
621 $rfc822_header = false;
624 default: return $read;
627 for ($i = 1; $i < $count; ++
$i) {
628 $line = trim($body[$i]);
629 if (($mime_header ||
$rfc822_header) &&
630 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
633 $bndreg = str_replace("\\", "\\\\", $bndreg);
634 $bndreg = str_replace("?", "\\?", $bndreg);
635 $bndreg = str_replace("+", "\\+", $bndreg);
636 $bndreg = str_replace(".", "\\.", $bndreg);
637 $bndreg = str_replace("/", "\\/", $bndreg);
638 $bndreg = str_replace("-", "\\-", $bndreg);
639 $bndreg = str_replace("(", "\\(", $bndreg);
640 $bndreg = str_replace(")", "\\)", $bndreg);
641 } else if ($rfc822_header && $line == '') {
642 $rfc822_header = false;
643 if ($msg->type0
== 'multipart') {
648 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
649 $cnt = count($boundaries)-1;
650 $bnd = $boundaries[$cnt]['bnd'];
651 $bndreg = $boundaries[$cnt]['bndreg'];
653 $regstr = '/^--'."($bndreg)".".*".'/';
654 if (preg_match($regstr, $line, $reg)) {
655 $bndlen = strlen($reg[1]);
657 if (strlen($line) > ($bndlen +
3)) {
658 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
663 /* calc offset and return $msg */
664 //$entStr = CalcEntity("$entStr", -1);
665 array_pop($boundaries);
671 //$entStr = CalcEntity("$entStr", 0);
681 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
683 if ($this->type0
== 'multipart') {
684 if($this->type1
== 'alternative') {
685 $msg = $this->findAlternativeEntity($alt_order);
686 if (count($msg->entities
) == 0) {
687 $entity[] = $msg->entity_id
;
689 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
692 } else if ($this->type1
== 'related') { /* RFC 2387 */
693 $msgs = $this->findRelatedEntity();
694 foreach ($msgs as $msg) {
695 if (count($msg->entities
) == 0) {
696 $entity[] = $msg->entity_id
;
698 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
701 if (count($msgs) > 0) {
704 } else { /* Treat as multipart/mixed */
705 foreach ($this->entities
as $ent) {
706 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
707 (!isset($ent->header
->parameters
['filename'])) &&
708 (!isset($ent->header
->parameters
['name'])) &&
709 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
710 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
715 } else { /* If not multipart, then just compare with each entry from $alt_order */
716 $type = $this->type0
.'/'.$this->type1
;
717 // $alt_order[] = "message/rfc822";
718 foreach ($alt_order as $alt) {
719 if( ($alt == $type) && isset($this->entity_id
) ) {
720 if ((count($this->entities
) == 0) &&
721 (!isset($ent->header
->parameters
['filename'])) &&
722 (!isset($ent->header
->parameters
['name'])) &&
723 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
724 $entity[] = $this->entity_id
;
731 foreach ($this->entities
as $ent) {
732 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
733 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
734 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
739 if(!$strict && !$found) {
740 if (($this->type0
== 'text') &&
741 in_array($this->type1
, array('plain', 'html', 'message')) &&
742 isset($this->entity_id
)) {
743 if (count($this->entities
) == 0) {
744 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
745 $entity[] = $this->entity_id
;
753 function findAlternativeEntity($alt_order) {
754 /* If we are dealing with alternative parts then we */
755 /* choose the best viewable message supported by SM. */
758 foreach($this->entities
as $ent) {
759 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
760 if ($type == 'multipart/related') {
761 $type = $ent->header
->getParameter('type');
762 // Mozilla bug. Mozilla does not provide the parameter type.
763 if (!$type) $type = 'text/html';
765 $altCount = count($alt_order);
766 for ($j = $best_view; $j < $altCount; ++
$j) {
767 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
777 function findRelatedEntity() {
779 $related_type = $this->header
->getParameter('type');
780 // Mozilla bug. Mozilla does not provide the parameter type.
781 if (!$related_type) $related_type = 'text/html';
782 $entCount = count($this->entities
);
783 for ($i = 0; $i < $entCount; ++
$i) {
784 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
785 if ($related_type == $type) {
786 $msgs[] = $this->entities
[$i];
792 function getAttachments($exclude_id=array(), $result = array()) {
794 if (($this->type0 == 'message') &&
795 ($this->type1 == 'rfc822') &&
796 ($this->entity_id) ) {
797 $this = $this->entities[0];
800 if (count($this->entities
)) {
801 foreach ($this->entities
as $entity) {
803 foreach ($exclude_id as $excl) {
804 if ($entity->entity_id
=== $excl) {
810 if (($entity->type0
== 'multipart') &&
811 ($entity->type1
!= 'related')) {
812 $result = $entity->getAttachments($exclude_id, $result);
813 } else if ($entity->type0
!= 'multipart') {
820 foreach ($exclude_id as $excl) {
821 $exclude = $exclude ||
($this->entity_id
== $excl);
831 function initAttachment($type, $name, $location) {
832 $attachment = new Message();
833 $mime_header = new MessageHeader();
834 $mime_header->setParameter('name', $name);
835 $pos = strpos($type, '/');
837 $mime_header->type0
= substr($type, 0, $pos);
838 $mime_header->type1
= substr($type, $pos+
1);
840 $mime_header->type0
= $type;
842 $attachment->att_local_name
= $location;
843 $disposition = new Disposition('attachment');
844 $disposition->properties
['filename'] = $name;
845 $mime_header->disposition
= $disposition;
846 $attachment->mime_header
= $mime_header;
847 $this->entities
[]=$attachment;