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 = '',
27 $parent = '', $decoded_body='',
28 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
31 $offset = 0, /* for fetching body parts out of raw messages */
32 $length = 0, /* for fetching body parts out of raw messages */
33 $att_local_name = ''; /* location where the tempory attachment
34 is stored. For future usage in smtp.php */
36 function setEnt($ent) {
37 $this->entity_id
= $ent;
40 function addEntity ($msg) {
41 $msg->parent
= &$this;
42 $this->entities
[] = $msg;
45 function getFilename() {
47 $filename = $this->header
->getParameter('filename');
49 $filename = $this->header
->getParameter('name');
53 $filename = 'untitled-'.$this->entity_id
;
59 function addRFC822Header($read) {
60 $header = new Rfc822Header();
61 $this->rfc822_header
= $header->parseHeader($read);
64 function getEntity($ent) {
65 $cur_ent = $this->entity_id
;
67 if (($cur_ent == '') ||
($cur_ent == '0')) {
70 $cur_ent_a = explode('.', $this->entity_id
);
72 $ent_a = explode('.', $ent);
74 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
75 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
77 $cur_ent_a = explode('.', $msg->entity_id
);
79 } else if (!isset($cur_ent_a[$i])) {
80 if (isset($msg->entities
[($ent_a[$i]-1)])) {
81 $msg = $msg->entities
[($ent_a[$i]-1)];
83 $msg = $msg->entities
[0];
86 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
87 /*this is a header for a message/rfc822 entity */
88 $msg = $msg->entities
[0];
92 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
93 /*this is a header for a message/rfc822 entity */
94 $msg = $msg->entities
[0];
97 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
98 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
99 $msg = $msg->entities
[($ent_a[$entCount]-1)];
106 function setBody($s) {
107 $this->body_part
= $s;
110 function clean_up() {
112 $msg->body_part
= '';
114 foreach ($msg->entities
as $m) {
119 function getMailbox() {
121 while (is_object($msg->parent
)) {
124 return $msg->mailbox
;
127 function calcEntity($msg) {
128 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
129 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
130 } else if (isset($this->entity_id
) && ($this->entity_id
!= '')) {
131 $entCount = count($this->entities
) +
1;
132 $par_ent = substr($this->entity_id
, -2);
133 if ($par_ent{0} == '.') {
134 $par_ent = $par_ent{1};
136 if ($par_ent == '0') {
138 $ent = substr($this->entity_id
, 0, strrpos($this->entity_id
, '.'));
139 $ent = ($ent ?
$ent . '.' : '') . $entCount;
140 $msg->entity_id
= $ent;
142 $msg->entity_id
= $entCount;
145 $ent = $this->entity_id
. '.' . $entCount;
146 $msg->entity_id
= $ent;
149 $msg->entity_id
= '0';
152 return $msg->entity_id
;
157 * Bodystructure parser, a recursive function for generating the
158 * entity-tree with all the mime-parts.
160 * It follows RFC2060 and stores all the described fields in the
165 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
168 function parseStructure($read, $i = 0) {
171 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
172 $char = strtoupper($read{$i});
178 $msg = new Message();
179 $hdr = new MessageHeader();
180 $hdr->type0
= 'text';
181 $hdr->type1
= 'plain';
182 $hdr->encoding
= 'us-ascii';
183 $msg->entity_id
= $this->calcEntity($msg);
185 $msg->header
->type0
= 'multipart';
186 $msg->type0
= 'multipart';
187 while ($read{$i} == '(') {
188 $res = $msg->parseStructure($read, $i);
190 $msg->addEntity($res[0]);
195 /* multipart properties */
197 $arg_a[] = $this->parseProperties($read, $i);
201 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
203 $arg_a[] = $msg->parseDisposition($read, $i);
204 } else { /* properties */
205 $arg_a[] = $msg->parseProperties($read, $i);
210 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
212 $arg_a[]= $msg->parseLanguage($read, $i);
215 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
216 $msg->header
->type0
= $arg_a[0];
217 $msg->header
->type1
= $arg_a[1];
218 $msg->type0
= $arg_a[0];
219 $msg->type1
= $arg_a[1];
220 $rfc822_hdr = new Rfc822Header();
221 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
222 while (($i < $cnt) && ($read{$i} != '(')) {
225 $res = $msg->parseStructure($read, $i);
227 $msg->addEntity($res[0]);
232 $arg_a[] = $msg->parseDisposition($read, $i);
237 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
238 $arg_a[] = $msg->parseDisposition($read, $i);
240 $arg_a[] = $msg->parseLanguage($read, $i);
245 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
247 $arg_a[] = $msg->parseLanguage($read, $i);
249 $i = $msg->parseParenthesis($read, $i);
250 $arg_a[] = ''; /* not yet described in rfc2060 */
255 /* unknown argument, skip this part */
256 $i = $msg->parseParenthesis($read, $i);
263 /* inside an entity -> start processing */
264 $debug = substr($read, $i, 20);
265 $arg_s = $msg->parseQuote($read, $i);
268 $arg_s = strtolower($arg_s); /* type0 and type1 */
274 /* probably NIL argument */
275 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
282 /* process the literal value */
283 $arg_s = $msg->parseLiteral($read, $i);
286 case is_numeric($read{$i}):
287 /* process integers */
288 if ($read{$i} == ' ') { break; }
290 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
297 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
299 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
300 $hdr->type0
= $arg_a[0];
301 $hdr->type1
= $arg_a[1];
303 $msg->type0
= $arg_a[0];
304 $msg->type1
= $arg_a[1];
306 if (is_array($arr)) {
307 $hdr->parameters
= $arg_a[2];
309 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
310 $hdr->description
= $arg_a[4];
311 $hdr->encoding
= strtolower($arg_a[5]);
312 $hdr->entity_id
= $msg->entity_id
;
313 $hdr->size
= $arg_a[6];
315 $hdr->lines
= $arg_a[7];
320 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
321 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
322 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
324 if ((strrchr($msg->entity_id
, '.') == '.0') && ($msg->type0
!='multipart')) {
325 $msg->entity_id
= $this->entity_id
. '.1';
328 $hdr->type0
= 'multipart';
329 $hdr->type1
= $arg_a[0];
330 $msg->type0
= 'multipart';
331 $msg->type1
= $arg_a[0];
332 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
333 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
334 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
338 return (array($msg, $i));
343 } /* parsestructure */
345 function parseProperties($read, &$i) {
346 $properties = array();
349 for (; $read{$i} != ')'; ++
$i) {
351 if ($read{$i} == '"') {
352 $arg_s = $this->parseQuote($read, $i);
353 } else if ($read{$i} == '{') {
354 $arg_s = $this->parseLiteral($read, $i);
358 if ($prop_name == '') {
359 $prop_name = strtolower($arg_s);
360 $properties[$prop_name] = '';
361 } else if ($prop_name != '') {
362 $properties[$prop_name] = $arg_s;
370 function parseEnvelope($read, &$i, $hdr) {
374 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
376 $char = strtoupper($read{$i});
379 $arg_a[] = $this->parseQuote($read, $i);
383 $arg_a[] = $this->parseLiteral($read, $i);
387 /* probably NIL argument */
388 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
395 /* Address structure (with group support)
396 * Note: Group support is useless on SMTP connections
397 * because the protocol doesn't support it
402 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
403 if ($read{$i} == '(') {
404 $addr = $this->parseAddress($read, $i);
405 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
407 $group = $addr->mailbox
;
410 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
412 if ($a == ($j+
1)) { /* no group members */
413 $group_addr->group
= $group;
414 $group_addr->mailbox
= '';
415 $group_addr->personal
= "$group: Undisclosed recipients;";
416 $addr_a[] = $group_addr;
420 $addr->group
= $group;
432 if (count($arg_a) > 9) {
433 /* argument 1: date */
434 $d = strtr($arg_a[0], array(' ' => ' '));
435 $d = explode(' ', $d);
436 $hdr->date
= getTimeStamp($d);
438 /* argument 2: subject */
439 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
440 $hdr->subject
= $arg_a[1];
442 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
443 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
444 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
445 $hdr->to
= $arg_a[5]; /* argument 6: to */
446 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
447 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
448 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
449 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
454 function parseLiteral($read, &$i) {
456 for (++
$i; $read{$i} != '}'; ++
$i) {
457 $lit_cnt .= $read{$i};
460 $lit_cnt +
=2; /* add the { and } characters */
462 for ($j = 0; $j < $lit_cnt; ++
$j) {
465 return (array($s, $i));
468 function parseQuote($read, &$i) {
470 for (++
$i; $read{$i} != '"'; ++
$i) {
471 if ($read{$i} == '\\') {
479 function parseAddress($read, &$i) {
482 for (; $read{$i} != ')'; ++
$i) {
483 $char = strtoupper($read{$i});
487 $arg_a[] = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
491 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
500 if (count($arg_a) == 4) {
501 $adr = new AddressStructure();
502 $adr->personal
= $arg_a[0];
503 $adr->adl
= $arg_a[1];
504 $adr->mailbox
= $arg_a[2];
505 $adr->host
= $arg_a[3];
512 function parseDisposition($read, &$i) {
514 for (; $read{$i} != ')'; ++
$i) {
516 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
517 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
518 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
523 if (isset($arg_a[0])) {
524 $disp = new Disposition($arg_a[0]);
525 if (isset($arg_a[1])) {
526 $disp->properties
= $arg_a[1];
530 return (is_object($disp) ?
$disp : '');
533 function parseLanguage($read, &$i) {
534 /* no idea how to process this one without examples */
537 for (; $read{$i} != ')'; ++
$i) {
539 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
540 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
541 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
546 if (isset($arg_a[0])) {
547 $lang = new Language($arg_a[0]);
548 if (isset($arg_a[1])) {
549 $lang->properties
= $arg_a[1];
553 return (is_object($lang) ?
$lang : '');
556 function parseParenthesis($read, $i) {
557 for (; $read{$i} != ')'; ++
$i) {
559 case '"': $this->parseQuote($read, $i); break;
560 case '{': $this->parseLiteral($read, $i); break;
561 case '(': $this->parseProperties($read, $i); break;
568 /* Function to fill the message structure in case the */
569 /* bodystructure is not available NOT FINISHED YET */
570 function parseMessage($read, $type0, $type1) {
573 $rfc822_header = true;
574 $mime_header = false;
577 $rfc822_header = false;
580 default: return $read;
583 for ($i = 1; $i < $count; ++
$i) {
584 $line = trim($body[$i]);
585 if (($mime_header ||
$rfc822_header) &&
586 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
589 $bndreg = str_replace("\\", "\\\\", $bndreg);
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 } else if ($rfc822_header && $line == '') {
598 $rfc822_header = false;
599 if ($msg->type0
== 'multipart') {
604 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
605 $cnt = count($boundaries)-1;
606 $bnd = $boundaries[$cnt]['bnd'];
607 $bndreg = $boundaries[$cnt]['bndreg'];
609 $regstr = '/^--'."($bndreg)".".*".'/';
610 if (preg_match($regstr, $line, $reg)) {
611 $bndlen = strlen($reg[1]);
613 if (strlen($line) > ($bndlen +
3)) {
614 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
619 /* calc offset and return $msg */
620 //$entStr = CalcEntity("$entStr", -1);
621 array_pop($boundaries);
627 //$entStr = CalcEntity("$entStr", 0);
637 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
639 if ($this->type0
== 'multipart') {
640 if($this->type1
== 'alternative') {
641 $msg = $this->findAlternativeEntity($alt_order);
642 if (count($msg->entities
) == 0) {
643 $entity[] = $msg->entity_id
;
645 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
648 } else if ($this->type1
== 'related') { /* RFC 2387 */
649 $msgs = $this->findRelatedEntity();
650 foreach ($msgs as $msg) {
651 if (count($msg->entities
) == 0) {
652 $entity[] = $msg->entity_id
;
654 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
657 if (count($msgs) > 0) {
660 } else { /* Treat as multipart/mixed */
661 foreach ($this->entities
as $ent) {
662 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
663 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
664 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
669 } else { /* If not multipart, then just compare with each entry from $alt_order */
670 $type = $this->type0
.'/'.$this->type1
;
671 foreach ($alt_order as $alt) {
672 if( ($alt == $type) && isset($this->entity_id
) ) {
673 if ((count($this->entities
) == 0) &&
674 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
675 $entity[] = $this->entity_id
;
682 foreach ($this->entities
as $ent) {
683 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
684 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
685 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
690 if(!$strict && !$found) {
691 if (($this->type0
== 'text') &&
692 in_array($this->type1
, array('plain', 'html', 'message')) &&
693 isset($this->entity_id
)) {
694 if (count($this->entities
) == 0) {
695 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
696 $entity[] = $this->entity_id
;
705 function findAlternativeEntity($alt_order) {
706 /* If we are dealing with alternative parts then we */
707 /* choose the best viewable message supported by SM. */
710 foreach($this->entities
as $ent) {
711 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
712 if ($type == 'multipart/related') {
713 $type = $ent->header
->getParameter('type');
715 $altCount = count($alt_order);
716 for ($j = $best_view; $j < $altCount; ++
$j) {
717 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
727 function findRelatedEntity() {
730 $entCount = count($this->entities
);
731 for ($i = 0; $i < $entCount; ++
$i) {
732 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
733 if ($this->header
->getParameter('type') == $type) {
734 $msgs[] = $this->entities
[$i];
741 function getAttachments($exclude_id=array(), $result = array()) {
742 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
743 $this = $this->entities
[0];
746 if (count($this->entities
)) {
747 foreach ($this->entities
as $entity) {
750 foreach ($exclude_id as $excl) {
751 if ($entity->entity_id
=== $excl) {
757 if (($entity->type0
== 'multipart') &&
758 ($entity->type1
!= 'related')) {
759 $result = $entity->getAttachments($exclude_id, $result);
760 } else if ($entity->type0
!= 'multipart') {
767 foreach ($exclude_id as $excl) {
768 $exclude = $exclude ||
($this->entity_id
== $excl);