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 $res= $msg->parseLanguage($read, $i);
217 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
218 $msg->header
->type0
= $arg_a[0];
219 $msg->header
->type1
= $arg_a[1];
220 $msg->type0
= $arg_a[0];
221 $msg->type1
= $arg_a[1];
222 $rfc822_hdr = new Rfc822Header();
223 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
224 $msg->rfc822_header
= $res[0];
226 while (($i < $cnt) && ($read{$i} != '(')) {
229 $res = $msg->parseStructure($read, $i);
231 $msg->addEntity($res[0]);
236 $arg_a[] = $msg->parseDisposition($read, $i);
241 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
242 $arg_a[] = $msg->parseDisposition($read, $i);
244 $arg_a[] = $msg->parseLanguage($read, $i);
249 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
251 $arg_a[] = $msg->parseLanguage($read, $i);
253 $i = $msg->parseParenthesis($read, $i);
254 $arg_a[] = ''; /* not yet described in rfc2060 */
259 /* unknown argument, skip this part */
260 $i = $msg->parseParenthesis($read, $i);
267 /* inside an entity -> start processing */
268 $debug = substr($read, $i, 20);
269 $arg_s = $msg->parseQuote($read, $i);
272 $arg_s = strtolower($arg_s); /* type0 and type1 */
278 /* probably NIL argument */
279 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
286 /* process the literal value */
287 $arg_s = $msg->parseLiteral($read, $i);
290 case is_numeric($read{$i}):
291 /* process integers */
292 if ($read{$i} == ' ') { break; }
294 for (++
$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++
$i) {
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
= $this->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
);
342 return (array($msg, $i));
347 } /* parsestructure */
349 function parseProperties($read, &$i) {
350 $properties = array();
353 for (; $read{$i} != ')'; ++
$i) {
355 if ($read{$i} == '"') {
356 $arg_s = $this->parseQuote($read, $i);
357 } else if ($read{$i} == '{') {
358 $arg_s = $this->parseLiteral($read, $i);
362 if ($prop_name == '') {
363 $prop_name = strtolower($arg_s);
364 $properties[$prop_name] = '';
365 } else if ($prop_name != '') {
366 $properties[$prop_name] = $arg_s;
374 function parseEnvelope($read, $i, $hdr) {
378 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
380 $char = strtoupper($read{$i});
383 $arg_a[] = $this->parseQuote($read, $i);
387 $arg_a[] = $this->parseLiteral($read, $i);
391 /* probably NIL argument */
392 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
399 /* Address structure (with group support)
400 * Note: Group support is useless on SMTP connections
401 * because the protocol doesn't support it
406 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
407 if ($read{$i} == '(') {
408 $res = $this->parseAddress($read, $i);
411 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
413 $group = $addr->mailbox
;
416 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
418 if ($a == ($j+
1)) { /* no group members */
419 $group_addr->group
= $group;
420 $group_addr->mailbox
= '';
421 $group_addr->personal
= "$group: Undisclosed recipients;";
422 $addr_a[] = $group_addr;
426 $addr->group
= $group;
438 if (count($arg_a) > 9) {
439 /* argument 1: date */
440 $d = strtr($arg_a[0], array(' ' => ' '));
441 $d = explode(' ', $d);
442 $hdr->date
= getTimeStamp($d);
444 /* argument 2: subject */
445 $arg_a[1] = (!trim($arg_a[1]) ?
_("(no subject)") : $arg_a[1]);
446 $hdr->subject
= $arg_a[1];
448 $hdr->from
= $arg_a[2][0]; /* argument 3: from */
449 $hdr->sender
= $arg_a[3][0]; /* argument 4: sender */
450 $hdr->replyto
= $arg_a[4][0]; /* argument 5: reply-to */
451 $hdr->to
= $arg_a[5]; /* argument 6: to */
452 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
453 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
454 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
455 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
457 return (array($hdr, $i));
460 function parseLiteral($read, &$i) {
462 for (++
$i; $read{$i} != '}'; ++
$i) {
463 $lit_cnt .= $read{$i};
466 $lit_cnt +
=2; /* add the { and } characters */
468 for ($j = 0; $j < $lit_cnt; ++
$j) {
471 return (array($s, $i));
474 function parseQuote($read, &$i) {
476 for (++
$i; $read{$i} != '"'; ++
$i) {
477 if ($read{$i} == '\\') {
485 function parseAddress($read, $i) {
488 for (; $read{$i} != ')'; ++
$i) {
489 $char = strtoupper($read{$i});
493 $arg_a[] = ($char == '"' ?
$this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
497 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
506 if (count($arg_a) == 4) {
507 $adr = new AddressStructure();
508 $adr->personal
= $arg_a[0];
509 $adr->adl
= $arg_a[1];
510 $adr->mailbox
= $arg_a[2];
511 $adr->host
= $arg_a[3];
515 return (array($adr, $i));
518 function parseDisposition($read, &$i) {
520 for (; $read{$i} != ')'; ++
$i) {
522 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
523 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
524 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
529 if (isset($arg_a[0])) {
530 $disp = new Disposition($arg_a[0]);
531 if (isset($arg_a[1])) {
532 $disp->properties
= $arg_a[1];
536 return (is_object($disp) ?
$disp : '');
539 function parseLanguage($read, &$i) {
540 /* no idea how to process this one without examples */
543 for (; $read{$i} != ')'; ++
$i) {
545 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
546 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
547 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
552 if (isset($arg_a[0])) {
553 $lang = new Language($arg_a[0]);
554 if (isset($arg_a[1])) {
555 $lang->properties
= $arg_a[1];
559 return (is_object($lang) ?
$lang : '');
562 function parseParenthesis($read, $i) {
563 for (; $read{$i} != ')'; ++
$i) {
565 case '"': $this->parseQuote($read, $i); break;
566 case '{': $this->parseLiteral($read, $i); break;
567 case '(': $this->parseProperties($read, $i); break;
574 /* Function to fill the message structure in case the */
575 /* bodystructure is not available NOT FINISHED YET */
576 function parseMessage($read, $type0, $type1) {
579 $rfc822_header = true;
580 $mime_header = false;
583 $rfc822_header = false;
586 default: return $read;
589 for ($i = 1; $i < $count; ++
$i) {
590 $line = trim($body[$i]);
591 if (($mime_header ||
$rfc822_header) &&
592 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
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 $bndreg = str_replace("-", "\\-", $bndreg);
601 $bndreg = str_replace("(", "\\(", $bndreg);
602 $bndreg = str_replace(")", "\\)", $bndreg);
603 } else if ($rfc822_header && $line == '') {
604 $rfc822_header = false;
605 if ($msg->type0
== 'multipart') {
610 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
611 $cnt = count($boundaries)-1;
612 $bnd = $boundaries[$cnt]['bnd'];
613 $bndreg = $boundaries[$cnt]['bndreg'];
615 $regstr = '/^--'."($bndreg)".".*".'/';
616 if (preg_match($regstr, $line, $reg)) {
617 $bndlen = strlen($reg[1]);
619 if (strlen($line) > ($bndlen +
3)) {
620 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
625 /* calc offset and return $msg */
626 //$entStr = CalcEntity("$entStr", -1);
627 array_pop($boundaries);
633 //$entStr = CalcEntity("$entStr", 0);
643 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
645 if ($this->type0
== 'multipart') {
646 if($this->type1
== 'alternative') {
647 $msg = $this->findAlternativeEntity($alt_order);
648 if (count($msg->entities
) == 0) {
649 $entity[] = $msg->entity_id
;
651 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
654 } else if ($this->type1
== 'related') { /* RFC 2387 */
655 $msgs = $this->findRelatedEntity();
656 foreach ($msgs as $msg) {
657 if (count($msg->entities
) == 0) {
658 $entity[] = $msg->entity_id
;
660 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
663 if (count($msgs) > 0) {
666 } else { /* Treat as multipart/mixed */
667 foreach ($this->entities
as $ent) {
668 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
669 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
670 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
675 } else { /* If not multipart, then just compare with each entry from $alt_order */
676 $type = $this->type0
.'/'.$this->type1
;
677 foreach ($alt_order as $alt) {
678 if( ($alt == $type) && isset($this->entity_id
) ) {
679 if ((count($this->entities
) == 0) &&
680 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
681 $entity[] = $this->entity_id
;
688 foreach ($this->entities
as $ent) {
689 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
690 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
691 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
696 if(!$strict && !$found) {
697 if (($this->type0
== 'text') &&
698 in_array($this->type1
, array('plain', 'html', 'message')) &&
699 isset($this->entity_id
)) {
700 if (count($this->entities
) == 0) {
701 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
702 $entity[] = $this->entity_id
;
711 function findAlternativeEntity($alt_order) {
712 /* If we are dealing with alternative parts then we */
713 /* choose the best viewable message supported by SM. */
716 foreach($this->entities
as $ent) {
717 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
718 if ($type == 'multipart/related') {
719 $type = $ent->header
->getParameter('type');
721 $altCount = count($alt_order);
722 for ($j = $best_view; $j < $altCount; ++
$j) {
723 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
733 function findRelatedEntity() {
736 $entCount = count($this->entities
);
737 for ($i = 0; $i < $entCount; ++
$i) {
738 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
739 if ($this->header
->getParameter('type') == $type) {
740 $msgs[] = $this->entities
[$i];
747 function getAttachments($exclude_id=array(), $result = array()) {
748 if (($this->type0
== 'message') && ($this->type1
== 'rfc822')) {
749 $this = $this->entities
[0];
752 if (count($this->entities
)) {
753 foreach ($this->entities
as $entity) {
756 foreach ($exclude_id as $excl) {
757 if ($entity->entity_id
=== $excl) {
763 if (($entity->type0
== 'multipart') &&
764 ($entity->type1
!= 'related')) {
765 $result = $entity->getAttachments($exclude_id, $result);
766 } else if ($entity->type0
!= 'multipart') {
773 foreach ($exclude_id as $excl) {
774 $exclude = $exclude ||
($this->entity_id
== $excl);