3e59af701511b619d0ead6ee251dca03f48c63f8
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);
288 case is_numeric($read{$i}):
289 /* process integers */
290 if ($read{$i} == ' ') { break; }
292 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
299 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
301 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
302 $hdr->type0
= $arg_a[0];
303 $hdr->type1
= $arg_a[1];
305 $msg->type0
= $arg_a[0];
306 $msg->type1
= $arg_a[1];
308 if (is_array($arr)) {
309 $hdr->parameters
= $arg_a[2];
311 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
312 $hdr->description
= $arg_a[4];
313 $hdr->encoding
= strtolower($arg_a[5]);
314 $hdr->entity_id
= $msg->entity_id
;
315 $hdr->size
= $arg_a[6];
317 $hdr->lines
= $arg_a[7];
322 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
323 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
324 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
326 if ((strrchr($msg->entity_id
, '.') == '.0') && ($msg->type0
!='multipart')) {
327 $msg->entity_id
= $this->entity_id
. '.1';
330 $hdr->type0
= 'multipart';
331 $hdr->type1
= $arg_a[0];
332 $msg->type0
= 'multipart';
333 $msg->type1
= $arg_a[0];
334 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
335 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
336 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
340 return (array($msg, $i));
345 } /* parsestructure */
347 function parseProperties($read, &$i) {
348 $properties = array();
351 for (; $read{$i} != ')'; ++
$i) {
353 if ($read{$i} == '"') {
354 $arg_s = $this->parseQuote($read, $i);
355 } else if ($read{$i} == '{') {
356 $arg_s = $this->parseLiteral($read, $i);
360 if ($prop_name == '') {
361 $prop_name = strtolower($arg_s);
362 $properties[$prop_name] = '';
363 } else if ($prop_name != '') {
364 $properties[$prop_name] = $arg_s;
372 function parseEnvelope($read, &$i, $hdr) {
376 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
378 $char = strtoupper($read{$i});
381 $arg_a[] = $this->parseQuote($read, $i);
385 $arg_a[] = $this->parseLiteral($read, $i);
389 /* probably NIL argument */
390 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
397 /* Address structure (with group support)
398 * Note: Group support is useless on SMTP connections
399 * because the protocol doesn't support it
404 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
405 if ($read{$i} == '(') {
406 $addr = $this->parseAddress($read, $i);
407 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
409 $group = $addr->mailbox
;
412 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
414 if ($a == ($j+
1)) { /* no group members */
415 $group_addr->group
= $group;
416 $group_addr->mailbox
= '';
417 $group_addr->personal
= "$group: Undisclosed recipients;";
418 $addr_a[] = $group_addr;
422 $addr->group
= $group;
434 if (count($arg_a) > 9) {
435 $d = strtr($arg_a[0], array(' ' => ' '));
436 $d = explode(' ', $d);
437 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
439 $hdr->date
= getTimeStamp($d); /* argument 1: date */
440 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
441 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
442 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
443 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
444 $hdr->to
= $arg_a[5]; /* argument 6: to */
445 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
446 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
447 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
448 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
453 function parseLiteral($read, &$i) {
455 for (++
$i; $read{$i} != '}'; ++
$i) {
456 $lit_cnt .= $read{$i};
459 $lit_cnt +
=2; /* add the { and } characters */
461 for ($j = 0; $j < $lit_cnt; ++
$j) {
467 function parseQuote($read, &$i) {
469 for (++
$i; $read{$i} != '"'; ++
$i) {
470 if ($read{$i} == '\\') {
478 function parseAddress($read, &$i) {
481 for (; $read{$i} != ')'; ++
$i) {
482 $char = strtoupper($read{$i});
486 $arg_a[] = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
490 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
499 if (count($arg_a) == 4) {
500 $adr = new AddressStructure();
501 $adr->personal
= $arg_a[0];
502 $adr->adl
= $arg_a[1];
503 $adr->mailbox
= $arg_a[2];
504 $adr->host
= $arg_a[3];
511 function parseDisposition($read, &$i) {
513 for (; $read{$i} != ')'; ++
$i) {
515 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
516 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
517 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
522 if (isset($arg_a[0])) {
523 $disp = new Disposition($arg_a[0]);
524 if (isset($arg_a[1])) {
525 $disp->properties
= $arg_a[1];
529 return (is_object($disp) ?
$disp : '');
532 function parseLanguage($read, &$i) {
533 /* no idea how to process this one without examples */
536 for (; $read{$i} != ')'; ++
$i) {
538 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
539 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
540 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
545 if (isset($arg_a[0])) {
546 $lang = new Language($arg_a[0]);
547 if (isset($arg_a[1])) {
548 $lang->properties
= $arg_a[1];
552 return (is_object($lang) ?
$lang : '');
555 function parseParenthesis($read, $i) {
556 for (; $read{$i} != ')'; ++
$i) {
558 case '"': $this->parseQuote($read, $i); break;
559 case '{': $this->parseLiteral($read, $i); break;
560 case '(': $this->parseProperties($read, $i); break;
567 /* Function to fill the message structure in case the */
568 /* bodystructure is not available NOT FINISHED YET */
569 function parseMessage($read, $type0, $type1) {
572 $rfc822_header = true;
573 $mime_header = false;
576 $rfc822_header = false;
579 default: return $read;
582 for ($i = 1; $i < $count; ++
$i) {
583 $line = trim($body[$i]);
584 if (($mime_header ||
$rfc822_header) &&
585 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
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 $bndreg = str_replace(")", "\\)", $bndreg);
596 } else if ($rfc822_header && $line == '') {
597 $rfc822_header = false;
598 if ($msg->type0
== 'multipart') {
603 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
604 $cnt = count($boundaries)-1;
605 $bnd = $boundaries[$cnt]['bnd'];
606 $bndreg = $boundaries[$cnt]['bndreg'];
608 $regstr = '/^--'."($bndreg)".".*".'/';
609 if (preg_match($regstr, $line, $reg)) {
610 $bndlen = strlen($reg[1]);
612 if (strlen($line) > ($bndlen +
3)) {
613 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
618 /* calc offset and return $msg */
619 //$entStr = CalcEntity("$entStr", -1);
620 array_pop($boundaries);
626 //$entStr = CalcEntity("$entStr", 0);
636 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
638 if ($this->type0
== 'multipart') {
639 if($this->type1
== 'alternative') {
640 $msg = $this->findAlternativeEntity($alt_order);
641 if (count($msg->entities
) == 0) {
642 $entity[] = $msg->entity_id
;
644 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
647 } else if ($this->type1
== 'related') { /* RFC 2387 */
648 $msgs = $this->findRelatedEntity();
649 foreach ($msgs as $msg) {
650 if (count($msg->entities
) == 0) {
651 $entity[] = $msg->entity_id
;
653 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
656 if (count($msgs) > 0) {
659 } else { /* Treat as multipart/mixed */
660 foreach ($this->entities
as $ent) {
661 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
662 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
663 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
668 } else { /* If not multipart, then just compare with each entry from $alt_order */
669 $type = $this->type0
.'/'.$this->type1
;
670 foreach ($alt_order as $alt) {
671 if( ($alt == $type) && isset($this->entity_id
) ) {
672 if ((count($this->entities
) == 0) &&
673 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
674 $entity[] = $this->entity_id
;
681 foreach ($this->entities
as $ent) {
682 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
683 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
684 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
689 if(!$strict && !$found) {
690 if (($this->type0
== 'text') &&
691 in_array($this->type1
, array('plain', 'html', 'message')) &&
692 isset($this->entity_id
)) {
693 if (count($this->entities
) == 0) {
694 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
695 $entity[] = $this->entity_id
;
704 function findAlternativeEntity($alt_order) {
705 /* If we are dealing with alternative parts then we */
706 /* choose the best viewable message supported by SM. */
709 foreach($this->entities
as $ent) {
710 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
711 if ($type == 'multipart/related') {
712 $type = $ent->header
->getParameter('type');
714 $altCount = count($alt_order);
715 for ($j = $best_view; $j < $altCount; ++
$j) {
716 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
726 function findRelatedEntity() {
729 $entCount = count($this->entities
);
730 for ($i = 0; $i < $entCount; ++
$i) {
731 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
732 if ($this->header
->getParameter('type') == $type) {
733 $msgs[] = $this->entities
[$i];
740 function getAttachments($exclude_id=array(), $result = array()) {
741 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
742 $this = $this->entities
[0];
745 if (count($this->entities
)) {
746 foreach ($this->entities
as $entity) {
749 foreach ($exclude_id as $excl) {
750 if ($entity->entity_id
=== $excl) {
756 if (($entity->type0
== 'multipart') &&
757 ($entity->type1
!= 'related')) {
758 $result = $entity->getAttachments($exclude_id, $result);
759 } else if ($entity->type0
!= 'multipart') {
766 foreach ($exclude_id as $excl) {
767 $exclude = $exclude ||
($this->entity_id
== $excl);
778 function initAttachment($type, $name, $location) {
779 $attachment = new Message();
780 $mime_header = new MessageHeader();
781 $mime_header->setParameter('name', $name);
782 $pos = strpos($type, '/');
784 $mime_header->type0
= substr($type, 0, $pos);
785 $mime_header->type1
= substr($type, $pos+
1);
787 $mime_header->type0
= $type;
789 $attachment->att_local_name
= $location;
790 $disposition = new Disposition('attachment');
791 $disposition->properties
['filename'] = $name;
792 $mime_header->disposition
= $disposition;
793 $attachment->mime_header
= $mime_header;
794 $this->entities
[]=$attachment;