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, $sub_msg = '') {
175 $message = new Message();
178 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
179 $char = strtoupper($read{$i});
185 $msg = new Message();
186 $hdr = new MessageHeader();
187 $hdr->type0
= 'text';
188 $hdr->type1
= 'plain';
189 $hdr->encoding
= 'us-ascii';
190 $msg->entity_id
= $message->calcEntity($msg);
192 $msg->header
->type0
= 'multipart';
193 $msg->type0
= 'multipart';
194 while ($read{$i} == '(') {
195 $msg->addEntity($this->parseStructure($read, $i, $msg));
200 /* multipart properties */
202 $arg_a[] = $this->parseProperties($read, $i);
206 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
208 $arg_a[] = $this->parseDisposition($read, $i);
209 } else { /* properties */
210 $arg_a[] = $this->parseProperties($read, $i);
215 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
217 $arg_a[]= $this->parseLanguage($read, $i);
220 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
221 $msg->header
->type0
= $arg_a[0];
222 $msg->header
->type1
= $arg_a[1];
223 $msg->type0
= $arg_a[0];
224 $msg->type1
= $arg_a[1];
225 $rfc822_hdr = new Rfc822Header();
226 $msg->rfc822_header
= $this->parseEnvelope($read, $i, $rfc822_hdr);
227 while (($i < $cnt) && ($read{$i} != '(')) {
230 $msg->addEntity($this->parseStructure($read, $i,$msg));
235 $arg_a[] = $this->parseDisposition($read, $i);
240 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
241 $arg_a[] = $this->parseDisposition($read, $i);
243 $arg_a[] = $this->parseLanguage($read, $i);
248 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
250 $arg_a[] = $this->parseLanguage($read, $i);
252 $i = $this->parseParenthesis($read, $i);
253 $arg_a[] = ''; /* not yet described in rfc2060 */
258 /* unknown argument, skip this part */
259 $i = $this->parseParenthesis($read, $i);
266 /* inside an entity -> start processing */
267 $arg_s = $this->parseQuote($read, $i);
270 $arg_s = strtolower($arg_s); /* type0 and type1 */
276 /* probably NIL argument */
277 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
284 /* process the literal value */
285 $arg_s = $this->parseLiteral($read, $i);
289 case is_numeric($read{$i}):
290 /* process integers */
291 if ($read{$i} == ' ') { break; }
293 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
294 $i +
= strlen($regs[1])-1;
301 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
303 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
304 $hdr->type0
= $arg_a[0];
305 $hdr->type1
= $arg_a[1];
307 $msg->type0
= $arg_a[0];
308 $msg->type1
= $arg_a[1];
310 if (is_array($arr)) {
311 $hdr->parameters
= $arg_a[2];
313 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
314 $hdr->description
= $arg_a[4];
315 $hdr->encoding
= strtolower($arg_a[5]);
316 $hdr->entity_id
= $msg->entity_id
;
317 $hdr->size
= $arg_a[6];
319 $hdr->lines
= $arg_a[7];
324 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
325 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
326 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
328 if ((strrchr($msg->entity_id
, '.') == '.0') && ($msg->type0
!='multipart')) {
329 $msg->entity_id
= $message->entity_id
. '.1';
332 $hdr->type0
= 'multipart';
333 $hdr->type1
= $arg_a[0];
334 $msg->type0
= 'multipart';
335 $msg->type1
= $arg_a[0];
336 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
337 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
338 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
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) {
456 $iPos = strpos($read,'}',$i);
458 $lit_cnt = substr($read, $i, $iPos - $i);
459 $i +
= strlen($lit_cnt) +
3; /* } + \r + \n */
460 $lit_cnt -= 2; /* lit_cnt includes \r\n */
461 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
463 } else { /* should never happen */
464 $i +
= 3; /* } + \r + \n */
470 function parseQuote($read, &$i) {
474 $iPos = strpos($read,'"',$iPos);
475 if ($iPos === false) break;
476 if ($iPos && $read{$iPos -1} != '\\') {
477 $s = substr($read,$i,($iPos-$i));
485 function parseAddress($read, &$i) {
487 for (; $read{$i} != ')'; ++
$i) {
488 $char = strtoupper($read{$i});
490 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
491 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
494 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
503 if (count($arg_a) == 4) {
504 $adr = new AddressStructure();
505 $adr->personal
= $arg_a[0];
506 $adr->adl
= $arg_a[1];
507 $adr->mailbox
= $arg_a[2];
508 $adr->host
= $arg_a[3];
515 function parseDisposition($read, &$i) {
517 for (; $read{$i} != ')'; ++
$i) {
519 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
520 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
521 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
526 if (isset($arg_a[0])) {
527 $disp = new Disposition($arg_a[0]);
528 if (isset($arg_a[1])) {
529 $disp->properties
= $arg_a[1];
533 return (is_object($disp) ?
$disp : '');
536 function parseLanguage($read, &$i) {
537 /* no idea how to process this one without examples */
540 for (; $read{$i} != ')'; ++
$i) {
542 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
543 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
544 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
549 if (isset($arg_a[0])) {
550 $lang = new Language($arg_a[0]);
551 if (isset($arg_a[1])) {
552 $lang->properties
= $arg_a[1];
556 return (is_object($lang) ?
$lang : '');
559 function parseParenthesis($read, $i) {
560 for (; $read{$i} != ')'; ++
$i) {
562 case '"': $this->parseQuote($read, $i); break;
563 case '{': $this->parseLiteral($read, $i); break;
564 case '(': $this->parseProperties($read, $i); break;
571 /* Function to fill the message structure in case the */
572 /* bodystructure is not available NOT FINISHED YET */
573 function parseMessage($read, $type0, $type1) {
576 $rfc822_header = true;
577 $mime_header = false;
580 $rfc822_header = false;
583 default: return $read;
586 for ($i = 1; $i < $count; ++
$i) {
587 $line = trim($body[$i]);
588 if (($mime_header ||
$rfc822_header) &&
589 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
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 $bndreg = str_replace("(", "\\(", $bndreg);
599 $bndreg = str_replace(")", "\\)", $bndreg);
600 } else if ($rfc822_header && $line == '') {
601 $rfc822_header = false;
602 if ($msg->type0
== 'multipart') {
607 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
608 $cnt = count($boundaries)-1;
609 $bnd = $boundaries[$cnt]['bnd'];
610 $bndreg = $boundaries[$cnt]['bndreg'];
612 $regstr = '/^--'."($bndreg)".".*".'/';
613 if (preg_match($regstr, $line, $reg)) {
614 $bndlen = strlen($reg[1]);
616 if (strlen($line) > ($bndlen +
3)) {
617 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
622 /* calc offset and return $msg */
623 //$entStr = CalcEntity("$entStr", -1);
624 array_pop($boundaries);
630 //$entStr = CalcEntity("$entStr", 0);
640 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
642 if ($this->type0
== 'multipart') {
643 if($this->type1
== 'alternative') {
644 $msg = $this->findAlternativeEntity($alt_order);
645 if (count($msg->entities
) == 0) {
646 $entity[] = $msg->entity_id
;
648 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
651 } else if ($this->type1
== 'related') { /* RFC 2387 */
652 $msgs = $this->findRelatedEntity();
653 foreach ($msgs as $msg) {
654 if (count($msg->entities
) == 0) {
655 $entity[] = $msg->entity_id
;
657 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
660 if (count($msgs) > 0) {
663 } else { /* Treat as multipart/mixed */
664 foreach ($this->entities
as $ent) {
665 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
666 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
667 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
672 } else { /* If not multipart, then just compare with each entry from $alt_order */
673 $type = $this->type0
.'/'.$this->type1
;
674 foreach ($alt_order as $alt) {
675 if( ($alt == $type) && isset($this->entity_id
) ) {
676 if ((count($this->entities
) == 0) &&
677 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
678 $entity[] = $this->entity_id
;
685 foreach ($this->entities
as $ent) {
686 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
687 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
688 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
693 if(!$strict && !$found) {
694 if (($this->type0
== 'text') &&
695 in_array($this->type1
, array('plain', 'html', 'message')) &&
696 isset($this->entity_id
)) {
697 if (count($this->entities
) == 0) {
698 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
699 $entity[] = $this->entity_id
;
708 function findAlternativeEntity($alt_order) {
709 /* If we are dealing with alternative parts then we */
710 /* choose the best viewable message supported by SM. */
713 foreach($this->entities
as $ent) {
714 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
715 if ($type == 'multipart/related') {
716 $type = $ent->header
->getParameter('type');
718 $altCount = count($alt_order);
719 for ($j = $best_view; $j < $altCount; ++
$j) {
720 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
730 function findRelatedEntity() {
733 $entCount = count($this->entities
);
734 for ($i = 0; $i < $entCount; ++
$i) {
735 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
736 if ($this->header
->getParameter('type') == $type) {
737 $msgs[] = $this->entities
[$i];
744 function getAttachments($exclude_id=array(), $result = array()) {
745 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
746 $this = $this->entities
[0];
749 if (count($this->entities
)) {
750 foreach ($this->entities
as $entity) {
753 foreach ($exclude_id as $excl) {
754 if ($entity->entity_id
=== $excl) {
760 if (($entity->type0
== 'multipart') &&
761 ($entity->type1
!= 'related')) {
762 $result = $entity->getAttachments($exclude_id, $result);
763 } else if ($entity->type0
!= 'multipart') {
770 foreach ($exclude_id as $excl) {
771 $exclude = $exclude ||
($this->entity_id
== $excl);
782 function initAttachment($type, $name, $location) {
783 $attachment = new Message();
784 $mime_header = new MessageHeader();
785 $mime_header->setParameter('name', $name);
786 $pos = strpos($type, '/');
788 $mime_header->type0
= substr($type, 0, $pos);
789 $mime_header->type1
= substr($type, $pos+
1);
791 $mime_header->type0
= $type;
793 $attachment->att_local_name
= $location;
794 $disposition = new Disposition('attachment');
795 $disposition->properties
['filename'] = $name;
796 $mime_header->disposition
= $disposition;
797 $attachment->mime_header
= $mime_header;
798 $this->entities
[]=$attachment;