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 $d = strtr($arg_a[0], array(' ' => ' '));
435 $d = explode(' ', $d);
436 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
438 $hdr->date
= getTimeStamp($d); /* argument 1: date */
439 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
440 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
441 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
442 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
443 $hdr->to
= $arg_a[5]; /* argument 6: to */
444 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
445 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
446 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
447 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
452 function parseLiteral($read, &$i) {
454 for (++
$i; $read{$i} != '}'; ++
$i) {
455 $lit_cnt .= $read{$i};
458 $lit_cnt +
=2; /* add the { and } characters */
460 for ($j = 0; $j < $lit_cnt; ++
$j) {
463 return (array($s, $i));
466 function parseQuote($read, &$i) {
468 for (++
$i; $read{$i} != '"'; ++
$i) {
469 if ($read{$i} == '\\') {
477 function parseAddress($read, &$i) {
480 for (; $read{$i} != ')'; ++
$i) {
481 $char = strtoupper($read{$i});
485 $arg_a[] = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
489 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
498 if (count($arg_a) == 4) {
499 $adr = new AddressStructure();
500 $adr->personal
= $arg_a[0];
501 $adr->adl
= $arg_a[1];
502 $adr->mailbox
= $arg_a[2];
503 $adr->host
= $arg_a[3];
510 function parseDisposition($read, &$i) {
512 for (; $read{$i} != ')'; ++
$i) {
514 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
515 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
516 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
521 if (isset($arg_a[0])) {
522 $disp = new Disposition($arg_a[0]);
523 if (isset($arg_a[1])) {
524 $disp->properties
= $arg_a[1];
528 return (is_object($disp) ?
$disp : '');
531 function parseLanguage($read, &$i) {
532 /* no idea how to process this one without examples */
535 for (; $read{$i} != ')'; ++
$i) {
537 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
538 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
539 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
544 if (isset($arg_a[0])) {
545 $lang = new Language($arg_a[0]);
546 if (isset($arg_a[1])) {
547 $lang->properties
= $arg_a[1];
551 return (is_object($lang) ?
$lang : '');
554 function parseParenthesis($read, $i) {
555 for (; $read{$i} != ')'; ++
$i) {
557 case '"': $this->parseQuote($read, $i); break;
558 case '{': $this->parseLiteral($read, $i); break;
559 case '(': $this->parseProperties($read, $i); break;
566 /* Function to fill the message structure in case the */
567 /* bodystructure is not available NOT FINISHED YET */
568 function parseMessage($read, $type0, $type1) {
571 $rfc822_header = true;
572 $mime_header = false;
575 $rfc822_header = false;
578 default: return $read;
581 for ($i = 1; $i < $count; ++
$i) {
582 $line = trim($body[$i]);
583 if (($mime_header ||
$rfc822_header) &&
584 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
587 $bndreg = str_replace("\\", "\\\\", $bndreg);
588 $bndreg = str_replace("?", "\\?", $bndreg);
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 } else if ($rfc822_header && $line == '') {
596 $rfc822_header = false;
597 if ($msg->type0
== 'multipart') {
602 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
603 $cnt = count($boundaries)-1;
604 $bnd = $boundaries[$cnt]['bnd'];
605 $bndreg = $boundaries[$cnt]['bndreg'];
607 $regstr = '/^--'."($bndreg)".".*".'/';
608 if (preg_match($regstr, $line, $reg)) {
609 $bndlen = strlen($reg[1]);
611 if (strlen($line) > ($bndlen +
3)) {
612 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
617 /* calc offset and return $msg */
618 //$entStr = CalcEntity("$entStr", -1);
619 array_pop($boundaries);
625 //$entStr = CalcEntity("$entStr", 0);
635 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
637 if ($this->type0
== 'multipart') {
638 if($this->type1
== 'alternative') {
639 $msg = $this->findAlternativeEntity($alt_order);
640 if (count($msg->entities
) == 0) {
641 $entity[] = $msg->entity_id
;
643 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
646 } else if ($this->type1
== 'related') { /* RFC 2387 */
647 $msgs = $this->findRelatedEntity();
648 foreach ($msgs as $msg) {
649 if (count($msg->entities
) == 0) {
650 $entity[] = $msg->entity_id
;
652 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
655 if (count($msgs) > 0) {
658 } else { /* Treat as multipart/mixed */
659 foreach ($this->entities
as $ent) {
660 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
661 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
662 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
667 } else { /* If not multipart, then just compare with each entry from $alt_order */
668 $type = $this->type0
.'/'.$this->type1
;
669 foreach ($alt_order as $alt) {
670 if( ($alt == $type) && isset($this->entity_id
) ) {
671 if ((count($this->entities
) == 0) &&
672 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
673 $entity[] = $this->entity_id
;
680 foreach ($this->entities
as $ent) {
681 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
682 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
683 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
688 if(!$strict && !$found) {
689 if (($this->type0
== 'text') &&
690 in_array($this->type1
, array('plain', 'html', 'message')) &&
691 isset($this->entity_id
)) {
692 if (count($this->entities
) == 0) {
693 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
694 $entity[] = $this->entity_id
;
703 function findAlternativeEntity($alt_order) {
704 /* If we are dealing with alternative parts then we */
705 /* choose the best viewable message supported by SM. */
708 foreach($this->entities
as $ent) {
709 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
710 if ($type == 'multipart/related') {
711 $type = $ent->header
->getParameter('type');
713 $altCount = count($alt_order);
714 for ($j = $best_view; $j < $altCount; ++
$j) {
715 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
725 function findRelatedEntity() {
728 $entCount = count($this->entities
);
729 for ($i = 0; $i < $entCount; ++
$i) {
730 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
731 if ($this->header
->getParameter('type') == $type) {
732 $msgs[] = $this->entities
[$i];
739 function getAttachments($exclude_id=array(), $result = array()) {
740 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
741 $this = $this->entities
[0];
744 if (count($this->entities
)) {
745 foreach ($this->entities
as $entity) {
748 foreach ($exclude_id as $excl) {
749 if ($entity->entity_id
=== $excl) {
755 if (($entity->type0
== 'multipart') &&
756 ($entity->type1
!= 'related')) {
757 $result = $entity->getAttachments($exclude_id, $result);
758 } else if ($entity->type0
!= 'multipart') {
765 foreach ($exclude_id as $excl) {
766 $exclude = $exclude ||
($this->entity_id
== $excl);
777 function initAttachment($type, $name, $location) {
778 $attachment = new Message();
779 $mime_header = new MessageHeader();
780 $mime_header->setParameter('name', $name);
781 $pos = strpos($type, '/');
783 $mime_header->type0
= substr($type, 0, $pos);
784 $mime_header->type1
= substr($type, $pos+
1);
786 $mime_header->type0
= $type;
788 $attachment->att_local_name
= $location;
789 $disposition = new Disposition('attachment');
790 $disposition->properties
['filename'] = $name;
791 $mime_header->disposition
= $disposition;
792 $attachment->mime_header
= $mime_header;
793 $this->entities
[]=$attachment;