6 * Copyright (c) 2003 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 $filename = $this->header
->getParameter('filename');
54 $filename = $this->header
->getParameter('name');
58 $filename = 'untitled-'.$this->entity_id
;
64 function addRFC822Header($read) {
65 $header = new Rfc822Header();
66 $this->rfc822_header
= $header->parseHeader($read);
69 function getEntity($ent) {
70 $cur_ent = $this->entity_id
;
72 if (($cur_ent == '') ||
($cur_ent == '0')) {
75 $cur_ent_a = explode('.', $this->entity_id
);
77 $ent_a = explode('.', $ent);
79 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
80 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
82 $cur_ent_a = explode('.', $msg->entity_id
);
84 } else if (!isset($cur_ent_a[$i])) {
85 if (isset($msg->entities
[($ent_a[$i]-1)])) {
86 $msg = $msg->entities
[($ent_a[$i]-1)];
88 $msg = $msg->entities
[0];
91 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
92 /*this is a header for a message/rfc822 entity */
93 $msg = $msg->entities
[0];
97 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
98 /*this is a header for a message/rfc822 entity */
99 $msg = $msg->entities
[0];
102 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
103 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
104 $msg = $msg->entities
[($ent_a[$entCount]-1)];
111 function setBody($s) {
112 $this->body_part
= $s;
115 function clean_up() {
117 $msg->body_part
= '';
119 foreach ($msg->entities
as $m) {
124 function getMailbox() {
126 while (is_object($msg->parent
)) {
129 return $msg->mailbox
;
133 * Bodystructure parser, a recursive function for generating the
134 * entity-tree with all the mime-parts.
136 * It follows RFC2060 and stores all the described fields in the
141 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
144 function parseStructure($read, &$i, $sub_msg = '') {
145 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
146 if($msg) $msg->setEntIds($msg,false,0);
150 function setEntIds(&$msg,$init=false,$i=0) {
151 $iCnt = count($msg->entities
);
152 if ($init !==false) {
154 if ($msg->parent
->type0
== 'message' &&
155 $msg->parent
->type1
== 'rfc822' &&
156 $msg->type0
== 'multipart') {
160 $msg->entity_id
= "$init.$iEntSub";
162 $msg->entity_id
= $iEntSub;
169 for ($i=0;$i<$iCnt;++
$i) {
170 $msg->entities
[$i]->parent
=& $msg;
171 if (strrchr($msg->entity_id
, '.') != '.0') {
172 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
174 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
179 function parseBodyStructure($read, &$i, $sub_msg = '') {
185 $message = new Message();
188 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
189 $char = strtoupper($read{$i});
195 $msg = new Message();
196 $hdr = new MessageHeader();
197 $hdr->type0
= 'text';
198 $hdr->type1
= 'plain';
199 $hdr->encoding
= 'us-ascii';
201 $msg->header
->type0
= 'multipart';
202 $msg->type0
= 'multipart';
203 while ($read{$i} == '(') {
204 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
209 /* multipart properties */
211 $arg_a[] = $msg->parseProperties($read, $i);
215 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
217 $arg_a[] = $msg->parseDisposition($read, $i);
218 } else { /* properties */
219 $arg_a[] = $msg->parseProperties($read, $i);
224 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
226 $arg_a[]= $msg->parseLanguage($read, $i);
229 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
230 $msg->header
->type0
= $arg_a[0];
231 $msg->header
->type1
= $arg_a[1];
232 $msg->type0
= $arg_a[0];
233 $msg->type1
= $arg_a[1];
234 $rfc822_hdr = new Rfc822Header();
235 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
236 while (($i < $cnt) && ($read{$i} != '(')) {
239 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
244 $arg_a[] = $msg->parseDisposition($read, $i);
249 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
250 $arg_a[] = $msg->parseDisposition($read, $i);
252 $arg_a[] = $msg->parseLanguage($read, $i);
257 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
259 $arg_a[] = $msg->parseLanguage($read, $i);
261 $i = $msg->parseParenthesis($read, $i);
262 $arg_a[] = ''; /* not yet described in rfc2060 */
267 /* unknown argument, skip this part */
268 $i = $msg->parseParenthesis($read, $i);
275 /* inside an entity -> start processing */
276 $arg_s = $msg->parseQuote($read, $i);
279 $arg_s = strtolower($arg_s); /* type0 and type1 */
285 /* probably NIL argument */
286 $tmpnil = strtoupper(substr($read, $i, 4));
287 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
294 /* process the literal value */
295 $arg_a[] = $msg->parseLiteral($read, $i);
299 case is_numeric($read{$i}):
300 /* process integers */
301 if ($read{$i} == ' ') { break; }
303 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
304 $i +
= strlen($regs[1])-1;
311 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
313 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
314 $hdr->type0
= $arg_a[0];
315 $hdr->type1
= $arg_a[1];
317 $msg->type0
= $arg_a[0];
318 $msg->type1
= $arg_a[1];
320 if (is_array($arr)) {
321 $hdr->parameters
= $arg_a[2];
323 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
324 $hdr->description
= $arg_a[4];
325 $hdr->encoding
= strtolower($arg_a[5]);
326 $hdr->entity_id
= $msg->entity_id
;
327 $hdr->size
= $arg_a[6];
329 $hdr->lines
= $arg_a[7];
334 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
335 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
336 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
339 $hdr->type0
= 'multipart';
340 $hdr->type1
= $arg_a[0];
341 $msg->type0
= 'multipart';
342 $msg->type1
= $arg_a[0];
343 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
344 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
345 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
352 } /* parsestructure */
354 function parseProperties($read, &$i) {
355 $properties = array();
358 for (; $read{$i} != ')'; ++
$i) {
360 if ($read{$i} == '"') {
361 $arg_s = $this->parseQuote($read, $i);
362 } else if ($read{$i} == '{') {
363 $arg_s = $this->parseLiteral($read, $i);
367 if ($prop_name == '') {
368 $prop_name = strtolower($arg_s);
369 $properties[$prop_name] = '';
370 } else if ($prop_name != '') {
371 $properties[$prop_name] = $arg_s;
379 function parseEnvelope($read, &$i, $hdr) {
383 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
384 $char = strtoupper($read{$i});
387 $arg_a[] = $this->parseQuote($read, $i);
391 $arg_a[] = $this->parseLiteral($read, $i);
392 /* temp bugfix (SM 1.5 will have a working clean version)
393 too much work to implement that version right now */
398 /* probably NIL argument */
399 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
406 /* Address structure (with group support)
407 * Note: Group support is useless on SMTP connections
408 * because the protocol doesn't support it
413 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
414 if ($read{$i} == '(') {
415 $addr = $this->parseAddress($read, $i);
416 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
418 $group = $addr->mailbox
;
421 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
423 if ($a == ($j+
1)) { /* no group members */
424 $group_addr->group
= $group;
425 $group_addr->mailbox
= '';
426 $group_addr->personal
= "$group: Undisclosed recipients;";
427 $addr_a[] = $group_addr;
431 $addr->group
= $group;
443 if (count($arg_a) > 9) {
444 $d = strtr($arg_a[0], array(' ' => ' '));
445 $d = explode(' ', $d);
446 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
448 $hdr->date
= getTimeStamp($d); /* argument 1: date */
449 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
450 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
451 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
452 $hdr->replyto
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
453 $hdr->to
= $arg_a[5]; /* argument 6: to */
454 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
455 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
456 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
457 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
462 function parseLiteral($read, &$i) {
465 $iPos = strpos($read,'}',$i);
467 $lit_cnt = substr($read, $i, $iPos - $i);
468 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
469 /* Now read the literal */
470 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
472 /* temp bugfix (SM 1.5 will have a working clean version)
473 too much work to implement that version right now */
475 } else { /* should never happen */
476 $i +
= 3; /* } + \r + \n */
482 function parseQuote($read, &$i) {
486 $iPos = strpos($read,'"',$iPos);
488 if ($iPos && $read{$iPos -1} != '\\') {
489 $s = substr($read,$i,($iPos-$i));
494 if ($iPos > strlen($read)) {
501 function parseAddress($read, &$i) {
503 for (; $read{$i} != ')'; ++
$i) {
504 $char = strtoupper($read{$i});
506 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
507 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
510 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
519 if (count($arg_a) == 4) {
520 $adr = new AddressStructure();
521 $adr->personal
= $arg_a[0];
522 $adr->adl
= $arg_a[1];
523 $adr->mailbox
= $arg_a[2];
524 $adr->host
= $arg_a[3];
531 function parseDisposition($read, &$i) {
533 for (; $read{$i} != ')'; ++
$i) {
535 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
536 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
537 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
542 if (isset($arg_a[0])) {
543 $disp = new Disposition($arg_a[0]);
544 if (isset($arg_a[1])) {
545 $disp->properties
= $arg_a[1];
549 return (is_object($disp) ?
$disp : '');
552 function parseLanguage($read, &$i) {
553 /* no idea how to process this one without examples */
556 for (; $read{$i} != ')'; ++
$i) {
558 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
559 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
560 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
565 if (isset($arg_a[0])) {
566 $lang = new Language($arg_a[0]);
567 if (isset($arg_a[1])) {
568 $lang->properties
= $arg_a[1];
572 return (is_object($lang) ?
$lang : '');
575 function parseParenthesis($read, $i) {
576 for (; $read{$i} != ')'; ++
$i) {
578 case '"': $this->parseQuote($read, $i); break;
579 case '{': $this->parseLiteral($read, $i); break;
580 case '(': $this->parseProperties($read, $i); break;
587 /* Function to fill the message structure in case the */
588 /* bodystructure is not available NOT FINISHED YET */
589 function parseMessage($read, $type0, $type1) {
592 $rfc822_header = true;
593 $mime_header = false;
596 $rfc822_header = false;
599 default: return $read;
602 for ($i = 1; $i < $count; ++
$i) {
603 $line = trim($body[$i]);
604 if (($mime_header ||
$rfc822_header) &&
605 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
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 $bndreg = str_replace(")", "\\)", $bndreg);
616 } else if ($rfc822_header && $line == '') {
617 $rfc822_header = false;
618 if ($msg->type0
== 'multipart') {
623 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
624 $cnt = count($boundaries)-1;
625 $bnd = $boundaries[$cnt]['bnd'];
626 $bndreg = $boundaries[$cnt]['bndreg'];
628 $regstr = '/^--'."($bndreg)".".*".'/';
629 if (preg_match($regstr, $line, $reg)) {
630 $bndlen = strlen($reg[1]);
632 if (strlen($line) > ($bndlen +
3)) {
633 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
638 /* calc offset and return $msg */
639 //$entStr = CalcEntity("$entStr", -1);
640 array_pop($boundaries);
646 //$entStr = CalcEntity("$entStr", 0);
656 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
658 if ($this->type0
== 'multipart') {
659 if($this->type1
== 'alternative') {
660 $msg = $this->findAlternativeEntity($alt_order);
661 if (count($msg->entities
) == 0) {
662 $entity[] = $msg->entity_id
;
664 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
667 } else if ($this->type1
== 'related') { /* RFC 2387 */
668 $msgs = $this->findRelatedEntity();
669 foreach ($msgs as $msg) {
670 if (count($msg->entities
) == 0) {
671 $entity[] = $msg->entity_id
;
673 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
676 if (count($msgs) > 0) {
679 } else { /* Treat as multipart/mixed */
680 foreach ($this->entities
as $ent) {
681 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
682 (!isset($ent->header
->parameters
['filename'])) &&
683 (!isset($ent->header
->parameters
['name'])) &&
684 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
685 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
690 } else { /* If not multipart, then just compare with each entry from $alt_order */
691 $type = $this->type0
.'/'.$this->type1
;
692 // $alt_order[] = "message/rfc822";
693 foreach ($alt_order as $alt) {
694 if( ($alt == $type) && isset($this->entity_id
) ) {
695 if ((count($this->entities
) == 0) &&
696 (!isset($ent->header
->parameters
['filename'])) &&
697 (!isset($ent->header
->parameters
['name'])) &&
698 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
699 $entity[] = $this->entity_id
;
706 foreach ($this->entities
as $ent) {
707 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
708 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
709 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
714 if(!$strict && !$found) {
715 if (($this->type0
== 'text') &&
716 in_array($this->type1
, array('plain', 'html', 'message')) &&
717 isset($this->entity_id
)) {
718 if (count($this->entities
) == 0) {
719 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
720 $entity[] = $this->entity_id
;
728 function findAlternativeEntity($alt_order) {
729 /* If we are dealing with alternative parts then we */
730 /* choose the best viewable message supported by SM. */
733 foreach($this->entities
as $ent) {
734 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
735 if ($type == 'multipart/related') {
736 $type = $ent->header
->getParameter('type');
737 // Mozilla bug. Mozilla does not provide the parameter type.
738 if (!$type) $type = 'text/html';
740 $altCount = count($alt_order);
741 for ($j = $best_view; $j < $altCount; ++
$j) {
742 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
752 function findRelatedEntity() {
754 $related_type = $this->header
->getParameter('type');
755 // Mozilla bug. Mozilla does not provide the parameter type.
756 if (!$related_type) $related_type = 'text/html';
757 $entCount = count($this->entities
);
758 for ($i = 0; $i < $entCount; ++
$i) {
759 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
760 if ($related_type == $type) {
761 $msgs[] = $this->entities
[$i];
767 function getAttachments($exclude_id=array(), $result = array()) {
769 if (($this->type0 == 'message') &&
770 ($this->type1 == 'rfc822') &&
771 ($this->entity_id) ) {
772 $this = $this->entities[0];
775 if (count($this->entities
)) {
776 foreach ($this->entities
as $entity) {
778 foreach ($exclude_id as $excl) {
779 if ($entity->entity_id
=== $excl) {
785 if (($entity->type0
== 'multipart') &&
786 ($entity->type1
!= 'related')) {
787 $result = $entity->getAttachments($exclude_id, $result);
788 } else if ($entity->type0
!= 'multipart') {
795 foreach ($exclude_id as $excl) {
796 $exclude = $exclude ||
($this->entity_id
== $excl);
806 function initAttachment($type, $name, $location) {
807 $attachment = new Message();
808 $mime_header = new MessageHeader();
809 $mime_header->setParameter('name', $name);
810 $pos = strpos($type, '/');
812 $mime_header->type0
= substr($type, 0, $pos);
813 $mime_header->type1
= substr($type, $pos+
1);
815 $mime_header->type0
= $type;
817 $attachment->att_local_name
= $location;
818 $disposition = new Disposition('attachment');
819 $disposition->properties
['filename'] = $name;
820 $mime_header->disposition
= $disposition;
821 $attachment->mime_header
= $mime_header;
822 $this->entities
[]=$attachment;