6 * Copyright (c) 2002 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.
15 /** message is the object that contains messages. It is a recursive
16 object in that through the $entities variable, it can contain
17 more objects of type message. See documentation in mime.txt for
18 a better description of how this works.
20 var $rfc822_header = '',
28 $parent = '', $decoded_body='',
29 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
32 $offset = 0, /* for fetching body parts out of raw messages */
33 $length = 0, /* for fetching body parts out of raw messages */
34 $att_local_name = ''; /* location where the tempory attachment
35 is stored. For future usage in smtp.php */
37 function setEnt($ent) {
38 $this->entity_id
= $ent;
41 function addEntity ($msg) {
42 $msg->parent
= &$this;
43 $this->entities
[] = $msg;
46 function getFilename() {
48 $filename = $this->header
->getParameter('filename');
50 $filename = $this->header
->getParameter('name');
54 $filename = 'untitled-'.$this->entity_id
;
60 function addRFC822Header($read) {
61 $header = new Rfc822Header();
62 $this->rfc822_header
= $header->parseHeader($read);
65 function getEntity($ent) {
66 $cur_ent = $this->entity_id
;
68 if (($cur_ent == '') ||
($cur_ent == '0')) {
71 $cur_ent_a = explode('.', $this->entity_id
);
73 $ent_a = explode('.', $ent);
75 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
76 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
78 $cur_ent_a = explode('.', $msg->entity_id
);
80 } else if (!isset($cur_ent_a[$i])) {
81 if (isset($msg->entities
[($ent_a[$i]-1)])) {
82 $msg = $msg->entities
[($ent_a[$i]-1)];
84 $msg = $msg->entities
[0];
87 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
88 /*this is a header for a message/rfc822 entity */
89 $msg = $msg->entities
[0];
93 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
94 /*this is a header for a message/rfc822 entity */
95 $msg = $msg->entities
[0];
98 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
99 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
100 $msg = $msg->entities
[($ent_a[$entCount]-1)];
107 function setBody($s) {
108 $this->body_part
= $s;
111 function clean_up() {
113 $msg->body_part
= '';
115 foreach ($msg->entities
as $m) {
120 function getMailbox() {
122 while (is_object($msg->parent
)) {
125 return $msg->mailbox
;
128 function calcEntity($msg) {
129 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
130 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
131 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
132 $entCount = count($this->entities
) +
1;
133 $par_ent = substr($this->entity_id
, -2);
134 if ($par_ent{0} == '.') {
135 $par_ent = $par_ent{1};
137 if ($par_ent == '0') {
139 $ent = substr($this->entity_id
, 0, strrpos($this->entity_id
, '.'));
140 $ent = ($ent ?
$ent . '.' : '') . $entCount;
141 $msg->entity_id
= $ent;
143 $msg->entity_id
= $entCount;
146 $ent = $this->entity_id
. '.' . $entCount;
147 $msg->entity_id
= $ent;
150 $msg->entity_id
= '0';
153 return $msg->entity_id
;
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 = 0) {
172 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
173 $char = strtoupper($read{$i});
179 $msg = new Message();
180 $hdr = new MessageHeader();
181 $hdr->type0
= 'text';
182 $hdr->type1
= 'plain';
183 $hdr->encoding
= 'us-ascii';
184 $msg->entity_id
= $this->calcEntity($msg);
186 $msg->header
->type0
= 'multipart';
187 $msg->type0
= 'multipart';
188 while ($read{$i} == '(') {
189 $res = $msg->parseStructure($read, $i);
191 $msg->addEntity($res[0]);
196 /* multipart properties */
198 $arg_a[] = $this->parseProperties($read, $i);
202 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
204 $arg_a[] = $msg->parseDisposition($read, $i);
205 } else { /* properties */
206 $arg_a[] = $msg->parseProperties($read, $i);
211 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
213 $arg_a[]= $msg->parseLanguage($read, $i);
216 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
217 $msg->header
->type0
= $arg_a[0];
218 $msg->header
->type1
= $arg_a[1];
219 $msg->type0
= $arg_a[0];
220 $msg->type1
= $arg_a[1];
221 $rfc822_hdr = new Rfc822Header();
222 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
223 while (($i < $cnt) && ($read{$i} != '(')) {
226 $res = $msg->parseStructure($read, $i);
228 $msg->addEntity($res[0]);
233 $arg_a[] = $msg->parseDisposition($read, $i);
238 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
239 $arg_a[] = $msg->parseDisposition($read, $i);
241 $arg_a[] = $msg->parseLanguage($read, $i);
246 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
248 $arg_a[] = $msg->parseLanguage($read, $i);
250 $i = $msg->parseParenthesis($read, $i);
251 $arg_a[] = ''; /* not yet described in rfc2060 */
256 /* unknown argument, skip this part */
257 $i = $msg->parseParenthesis($read, $i);
264 /* inside an entity -> start processing */
265 $debug = substr($read, $i, 20);
266 $arg_s = $msg->parseQuote($read, $i);
269 $arg_s = strtolower($arg_s); /* type0 and type1 */
275 /* probably NIL argument */
276 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
283 /* process the literal value */
284 $arg_s = $msg->parseLiteral($read, $i);
287 case is_numeric($read{$i}):
288 /* process integers */
289 if ($read{$i} == ' ') { break; }
291 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
298 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
300 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
301 $hdr->type0
= $arg_a[0];
302 $hdr->type1
= $arg_a[1];
304 $msg->type0
= $arg_a[0];
305 $msg->type1
= $arg_a[1];
307 if (is_array($arr)) {
308 $hdr->parameters
= $arg_a[2];
310 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
311 $hdr->description
= $arg_a[4];
312 $hdr->encoding
= strtolower($arg_a[5]);
313 $hdr->entity_id
= $msg->entity_id
;
314 $hdr->size
= $arg_a[6];
316 $hdr->lines
= $arg_a[7];
321 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
322 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
323 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
325 if ((strrchr($msg->entity_id
, '.') == '.0') && ($msg->type0
!='multipart')) {
326 $msg->entity_id
= $this->entity_id
. '.1';
329 $hdr->type0
= 'multipart';
330 $hdr->type1
= $arg_a[0];
331 $msg->type0
= 'multipart';
332 $msg->type1
= $arg_a[0];
333 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
334 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
335 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
339 return (array($msg, $i));
344 } /* parsestructure */
346 function parseProperties($read, &$i) {
347 $properties = array();
350 for (; $read{$i} != ')'; ++
$i) {
352 if ($read{$i} == '"') {
353 $arg_s = $this->parseQuote($read, $i);
354 } else if ($read{$i} == '{') {
355 $arg_s = $this->parseLiteral($read, $i);
359 if ($prop_name == '') {
360 $prop_name = strtolower($arg_s);
361 $properties[$prop_name] = '';
362 } else if ($prop_name != '') {
363 $properties[$prop_name] = $arg_s;
371 function parseEnvelope($read, &$i, $hdr) {
375 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
377 $char = strtoupper($read{$i});
380 $arg_a[] = $this->parseQuote($read, $i);
384 $arg_a[] = $this->parseLiteral($read, $i);
388 /* probably NIL argument */
389 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
396 /* Address structure (with group support)
397 * Note: Group support is useless on SMTP connections
398 * because the protocol doesn't support it
403 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
404 if ($read{$i} == '(') {
405 $addr = $this->parseAddress($read, $i);
406 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
408 $group = $addr->mailbox
;
411 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
413 if ($a == ($j+
1)) { /* no group members */
414 $group_addr->group
= $group;
415 $group_addr->mailbox
= '';
416 $group_addr->personal
= "$group: Undisclosed recipients;";
417 $addr_a[] = $group_addr;
421 $addr->group
= $group;
433 if (count($arg_a) > 9) {
434 /* argument 1: date */
435 $d = strtr($arg_a[0], array(' ' => ' '));
436 $d = explode(' ', $d);
437 $hdr->date
= getTimeStamp($d);
439 /* argument 2: subject */
440 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
441 $hdr->subject
= $arg_a[1];
443 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
444 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
445 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
446 $hdr->to
= $arg_a[5]; /* argument 6: to */
447 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
448 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
449 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
450 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
455 function parseLiteral($read, &$i) {
457 for (++
$i; $read{$i} != '}'; ++
$i) {
458 $lit_cnt .= $read{$i};
461 $lit_cnt +
=2; /* add the { and } characters */
463 for ($j = 0; $j < $lit_cnt; ++
$j) {
466 return (array($s, $i));
469 function parseQuote($read, &$i) {
471 for (++
$i; $read{$i} != '"'; ++
$i) {
472 if ($read{$i} == '\\') {
480 function parseAddress($read, &$i) {
483 for (; $read{$i} != ')'; ++
$i) {
484 $char = strtoupper($read{$i});
488 $arg_a[] = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
492 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
501 if (count($arg_a) == 4) {
502 $adr = new AddressStructure();
503 $adr->personal
= $arg_a[0];
504 $adr->adl
= $arg_a[1];
505 $adr->mailbox
= $arg_a[2];
506 $adr->host
= $arg_a[3];
513 function parseDisposition($read, &$i) {
515 for (; $read{$i} != ')'; ++
$i) {
517 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
518 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
519 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
524 if (isset($arg_a[0])) {
525 $disp = new Disposition($arg_a[0]);
526 if (isset($arg_a[1])) {
527 $disp->properties
= $arg_a[1];
531 return (is_object($disp) ?
$disp : '');
534 function parseLanguage($read, &$i) {
535 /* no idea how to process this one without examples */
538 for (; $read{$i} != ')'; ++
$i) {
540 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
541 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
542 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
547 if (isset($arg_a[0])) {
548 $lang = new Language($arg_a[0]);
549 if (isset($arg_a[1])) {
550 $lang->properties
= $arg_a[1];
554 return (is_object($lang) ?
$lang : '');
557 function parseParenthesis($read, $i) {
558 for (; $read{$i} != ')'; ++
$i) {
560 case '"': $this->parseQuote($read, $i); break;
561 case '{': $this->parseLiteral($read, $i); break;
562 case '(': $this->parseProperties($read, $i); break;
569 /* Function to fill the message structure in case the */
570 /* bodystructure is not available NOT FINISHED YET */
571 function parseMessage($read, $type0, $type1) {
574 $rfc822_header = true;
575 $mime_header = false;
578 $rfc822_header = false;
581 default: return $read;
584 for ($i = 1; $i < $count; ++
$i) {
585 $line = trim($body[$i]);
586 if (($mime_header ||
$rfc822_header) &&
587 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
590 $bndreg = str_replace("\\", "\\\\", $bndreg);
591 $bndreg = str_replace("?", "\\?", $bndreg);
592 $bndreg = str_replace("+", "\\+", $bndreg);
593 $bndreg = str_replace(".", "\\.", $bndreg);
594 $bndreg = str_replace("/", "\\/", $bndreg);
595 $bndreg = str_replace("-", "\\-", $bndreg);
596 $bndreg = str_replace("(", "\\(", $bndreg);
597 $bndreg = str_replace(")", "\\)", $bndreg);
598 } else if ($rfc822_header && $line == '') {
599 $rfc822_header = false;
600 if ($msg->type0
== 'multipart') {
605 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
606 $cnt = count($boundaries)-1;
607 $bnd = $boundaries[$cnt]['bnd'];
608 $bndreg = $boundaries[$cnt]['bndreg'];
610 $regstr = '/^--'."($bndreg)".".*".'/';
611 if (preg_match($regstr, $line, $reg)) {
612 $bndlen = strlen($reg[1]);
614 if (strlen($line) > ($bndlen +
3)) {
615 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
620 /* calc offset and return $msg */
621 //$entStr = CalcEntity("$entStr", -1);
622 array_pop($boundaries);
628 //$entStr = CalcEntity("$entStr", 0);
638 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
640 if ($this->type0
== 'multipart') {
641 if($this->type1
== 'alternative') {
642 $msg = $this->findAlternativeEntity($alt_order);
643 if (count($msg->entities
) == 0) {
644 $entity[] = $msg->entity_id
;
646 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
649 } else if ($this->type1
== 'related') { /* RFC 2387 */
650 $msgs = $this->findRelatedEntity();
651 foreach ($msgs as $msg) {
652 if (count($msg->entities
) == 0) {
653 $entity[] = $msg->entity_id
;
655 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
658 if (count($msgs) > 0) {
661 } else { /* Treat as multipart/mixed */
662 foreach ($this->entities
as $ent) {
663 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
664 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
665 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
670 } else { /* If not multipart, then just compare with each entry from $alt_order */
671 $type = $this->type0
.'/'.$this->type1
;
672 foreach ($alt_order as $alt) {
673 if( ($alt == $type) && isset($this->entity_id
) ) {
674 if ((count($this->entities
) == 0) &&
675 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
676 $entity[] = $this->entity_id
;
683 foreach ($this->entities
as $ent) {
684 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
685 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
686 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
691 if(!$strict && !$found) {
692 if (($this->type0
== 'text') &&
693 in_array($this->type1
, array('plain', 'html', 'message')) &&
694 isset($this->entity_id
)) {
695 if (count($this->entities
) == 0) {
696 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
697 $entity[] = $this->entity_id
;
706 function findAlternativeEntity($alt_order) {
707 /* If we are dealing with alternative parts then we */
708 /* choose the best viewable message supported by SM. */
711 foreach($this->entities
as $ent) {
712 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
713 if ($type == 'multipart/related') {
714 $type = $ent->header
->getParameter('type');
716 $altCount = count($alt_order);
717 for ($j = $best_view; $j < $altCount; ++
$j) {
718 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
728 function findRelatedEntity() {
731 $entCount = count($this->entities
);
732 for ($i = 0; $i < $entCount; ++
$i) {
733 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
734 if ($this->header
->getParameter('type') == $type) {
735 $msgs[] = $this->entities
[$i];
742 function getAttachments($exclude_id=array(), $result = array()) {
743 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
744 $this = $this->entities
[0];
747 if (count($this->entities
)) {
748 foreach ($this->entities
as $entity) {
751 foreach ($exclude_id as $excl) {
752 if ($entity->entity_id
=== $excl) {
758 if (($entity->type0
== 'multipart') &&
759 ($entity->type1
!= 'related')) {
760 $result = $entity->getAttachments($exclude_id, $result);
761 } else if ($entity->type0
!= 'multipart') {
768 foreach ($exclude_id as $excl) {
769 $exclude = $exclude ||
($this->entity_id
== $excl);
780 function initAttachment($type, $name, $location) {
781 $attachment = new Message();
782 $mime_header = new MessageHeader();
783 $mime_header->setParameter('name', $name);
784 $pos = strpos($type, '/');
786 $mime_header->type0
= substr($type, 0, $pos);
787 $mime_header->type1
= substr($type, $pos+
1);
789 $mime_header->type0
= $type;
791 $attachment->att_local_name
= $location;
792 $disposition = new Disposition('attachment');
793 $disposition->properties
['filename'] = $name;
794 $mime_header->disposition
= $disposition;
795 $attachment->mime_header
= $mime_header;
796 $this->entities
[]=$attachment;