6 * Copyright (c) 2003 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 $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
;
128 * Bodystructure parser, a recursive function for generating the
129 * entity-tree with all the mime-parts.
131 * It follows RFC2060 and stores all the described fields in the
136 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
139 function parseStructure($read, &$i, $sub_msg = '') {
140 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
141 if($msg) $msg->setEntIds($msg,false,0);
145 function setEntIds(&$msg,$init=false,$i=0) {
146 $iCnt = count($msg->entities
);
147 if ($init !==false) {
149 if ($msg->parent
->type0
== 'message' &&
150 $msg->parent
->type1
== 'rfc822' &&
151 $msg->type0
== 'multipart') {
155 $msg->entity_id
= "$init.$iEntSub";
157 $msg->entity_id
= $iEntSub;
164 for ($i=0;$i<$iCnt;++
$i) {
165 $msg->entities
[$i]->parent
=& $msg;
166 if (strrchr($msg->entity_id
, '.') != '.0') {
167 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
169 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
175 function parseBodyStructure($read, &$i, $sub_msg = '') {
181 $message = new Message();
184 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
185 $char = strtoupper($read{$i});
191 $msg = new Message();
192 $hdr = new MessageHeader();
193 $hdr->type0
= 'text';
194 $hdr->type1
= 'plain';
195 $hdr->encoding
= 'us-ascii';
197 $msg->header
->type0
= 'multipart';
198 $msg->type0
= 'multipart';
199 while ($read{$i} == '(') {
200 $msg->addEntity($this->parseBodyStructure($read, $i, $msg));
205 /* multipart properties */
207 $arg_a[] = $this->parseProperties($read, $i);
211 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
213 $arg_a[] = $this->parseDisposition($read, $i);
214 } else { /* properties */
215 $arg_a[] = $this->parseProperties($read, $i);
220 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
222 $arg_a[]= $this->parseLanguage($read, $i);
225 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
226 $msg->header
->type0
= $arg_a[0];
227 $msg->header
->type1
= $arg_a[1];
228 $msg->type0
= $arg_a[0];
229 $msg->type1
= $arg_a[1];
230 $rfc822_hdr = new Rfc822Header();
231 $msg->rfc822_header
= $this->parseEnvelope($read, $i, $rfc822_hdr);
232 while (($i < $cnt) && ($read{$i} != '(')) {
235 $msg->addEntity($this->parseBodyStructure($read, $i,$msg));
240 $arg_a[] = $this->parseDisposition($read, $i);
245 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
246 $arg_a[] = $this->parseDisposition($read, $i);
248 $arg_a[] = $this->parseLanguage($read, $i);
253 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
255 $arg_a[] = $this->parseLanguage($read, $i);
257 $i = $this->parseParenthesis($read, $i);
258 $arg_a[] = ''; /* not yet described in rfc2060 */
263 /* unknown argument, skip this part */
264 $i = $this->parseParenthesis($read, $i);
271 /* inside an entity -> start processing */
272 $arg_s = $this->parseQuote($read, $i);
275 $arg_s = strtolower($arg_s); /* type0 and type1 */
281 /* probably NIL argument */
282 $tmpnil = strtoupper(substr($read, $i, 4));
283 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
290 /* process the literal value */
291 $arg_a[] = $this->parseLiteral($read, $i);
295 case is_numeric($read{$i}):
296 /* process integers */
297 if ($read{$i} == ' ') { break; }
299 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
300 $i +
= strlen($regs[1])-1;
307 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
309 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
310 $hdr->type0
= $arg_a[0];
311 $hdr->type1
= $arg_a[1];
313 $msg->type0
= $arg_a[0];
314 $msg->type1
= $arg_a[1];
316 if (is_array($arr)) {
317 $hdr->parameters
= $arg_a[2];
319 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
320 $hdr->description
= $arg_a[4];
321 $hdr->encoding
= strtolower($arg_a[5]);
322 $hdr->entity_id
= $msg->entity_id
;
323 $hdr->size
= $arg_a[6];
325 $hdr->lines
= $arg_a[7];
330 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
331 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
332 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
335 $hdr->type0
= 'multipart';
336 $hdr->type1
= $arg_a[0];
337 $msg->type0
= 'multipart';
338 $msg->type1
= $arg_a[0];
339 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
340 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
341 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
348 } /* parsestructure */
350 function parseProperties($read, &$i) {
351 $properties = array();
354 for (; $read{$i} != ')'; ++
$i) {
356 if ($read{$i} == '"') {
357 $arg_s = $this->parseQuote($read, $i);
358 } else if ($read{$i} == '{') {
359 $arg_s = $this->parseLiteral($read, $i);
363 if ($prop_name == '') {
364 $prop_name = strtolower($arg_s);
365 $properties[$prop_name] = '';
366 } else if ($prop_name != '') {
367 $properties[$prop_name] = $arg_s;
375 function parseEnvelope($read, &$i, $hdr) {
379 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);
388 /* temp bugfix (SM 1.5 will have a working clean version)
389 too much work to implement that version right now */
394 /* probably NIL argument */
395 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
402 /* Address structure (with group support)
403 * Note: Group support is useless on SMTP connections
404 * because the protocol doesn't support it
409 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
410 if ($read{$i} == '(') {
411 $addr = $this->parseAddress($read, $i);
412 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
414 $group = $addr->mailbox
;
417 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
419 if ($a == ($j+
1)) { /* no group members */
420 $group_addr->group
= $group;
421 $group_addr->mailbox
= '';
422 $group_addr->personal
= "$group: Undisclosed recipients;";
423 $addr_a[] = $group_addr;
427 $addr->group
= $group;
439 if (count($arg_a) > 9) {
440 $d = strtr($arg_a[0], array(' ' => ' '));
441 $d = explode(' ', $d);
442 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
444 $hdr->date
= getTimeStamp($d); /* argument 1: date */
445 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
446 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
447 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
448 $hdr->replyto
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
449 $hdr->to
= $arg_a[5]; /* argument 6: to */
450 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
451 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
452 $hdr->inreplyto
= $arg_a[8]; /* argument 9: in-reply-to */
453 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
458 function parseLiteral($read, &$i) {
461 $iPos = strpos($read,'}',$i);
463 $lit_cnt = substr($read, $i, $iPos - $i);
464 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
465 /* Now read the literal */
466 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
468 /* temp bugfix (SM 1.5 will have a working clean version)
469 too much work to implement that version right now */
471 } else { /* should never happen */
472 $i +
= 3; /* } + \r + \n */
478 function parseQuote($read, &$i) {
482 $iPos = strpos($read,'"',$iPos);
484 if ($iPos && $read{$iPos -1} != '\\') {
485 $s = substr($read,$i,($iPos-$i));
490 if ($iPos > strlen($read)) {
497 function parseAddress($read, &$i) {
499 for (; $read{$i} != ')'; ++
$i) {
500 $char = strtoupper($read{$i});
502 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
503 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
506 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
515 if (count($arg_a) == 4) {
516 $adr = new AddressStructure();
517 $adr->personal
= $arg_a[0];
518 $adr->adl
= $arg_a[1];
519 $adr->mailbox
= $arg_a[2];
520 $adr->host
= $arg_a[3];
527 function parseDisposition($read, &$i) {
529 for (; $read{$i} != ')'; ++
$i) {
531 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
532 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
533 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
538 if (isset($arg_a[0])) {
539 $disp = new Disposition($arg_a[0]);
540 if (isset($arg_a[1])) {
541 $disp->properties
= $arg_a[1];
545 return (is_object($disp) ?
$disp : '');
548 function parseLanguage($read, &$i) {
549 /* no idea how to process this one without examples */
552 for (; $read{$i} != ')'; ++
$i) {
554 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
555 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
556 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
561 if (isset($arg_a[0])) {
562 $lang = new Language($arg_a[0]);
563 if (isset($arg_a[1])) {
564 $lang->properties
= $arg_a[1];
568 return (is_object($lang) ?
$lang : '');
571 function parseParenthesis($read, $i) {
572 for (; $read{$i} != ')'; ++
$i) {
574 case '"': $this->parseQuote($read, $i); break;
575 case '{': $this->parseLiteral($read, $i); break;
576 case '(': $this->parseProperties($read, $i); break;
583 /* Function to fill the message structure in case the */
584 /* bodystructure is not available NOT FINISHED YET */
585 function parseMessage($read, $type0, $type1) {
588 $rfc822_header = true;
589 $mime_header = false;
592 $rfc822_header = false;
595 default: return $read;
598 for ($i = 1; $i < $count; ++
$i) {
599 $line = trim($body[$i]);
600 if (($mime_header ||
$rfc822_header) &&
601 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
604 $bndreg = str_replace("\\", "\\\\", $bndreg);
605 $bndreg = str_replace("?", "\\?", $bndreg);
606 $bndreg = str_replace("+", "\\+", $bndreg);
607 $bndreg = str_replace(".", "\\.", $bndreg);
608 $bndreg = str_replace("/", "\\/", $bndreg);
609 $bndreg = str_replace("-", "\\-", $bndreg);
610 $bndreg = str_replace("(", "\\(", $bndreg);
611 $bndreg = str_replace(")", "\\)", $bndreg);
612 } else if ($rfc822_header && $line == '') {
613 $rfc822_header = false;
614 if ($msg->type0
== 'multipart') {
619 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
620 $cnt = count($boundaries)-1;
621 $bnd = $boundaries[$cnt]['bnd'];
622 $bndreg = $boundaries[$cnt]['bndreg'];
624 $regstr = '/^--'."($bndreg)".".*".'/';
625 if (preg_match($regstr, $line, $reg)) {
626 $bndlen = strlen($reg[1]);
628 if (strlen($line) > ($bndlen +
3)) {
629 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
634 /* calc offset and return $msg */
635 //$entStr = CalcEntity("$entStr", -1);
636 array_pop($boundaries);
642 //$entStr = CalcEntity("$entStr", 0);
652 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
654 if ($this->type0
== 'multipart') {
655 if($this->type1
== 'alternative') {
656 $msg = $this->findAlternativeEntity($alt_order);
657 if (count($msg->entities
) == 0) {
658 $entity[] = $msg->entity_id
;
660 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
663 } else if ($this->type1
== 'related') { /* RFC 2387 */
664 $msgs = $this->findRelatedEntity();
665 foreach ($msgs as $msg) {
666 if (count($msg->entities
) == 0) {
667 $entity[] = $msg->entity_id
;
669 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
672 if (count($msgs) > 0) {
675 } else { /* Treat as multipart/mixed */
676 foreach ($this->entities
as $ent) {
677 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
678 (!isset($ent->header
->parameters
['filename'])) &&
679 (!isset($ent->header
->parameters
['name'])) &&
680 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
681 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
686 } else { /* If not multipart, then just compare with each entry from $alt_order */
687 $type = $this->type0
.'/'.$this->type1
;
688 // $alt_order[] = "message/rfc822";
689 foreach ($alt_order as $alt) {
690 if( ($alt == $type) && isset($this->entity_id
) ) {
691 if ((count($this->entities
) == 0) &&
692 (!isset($ent->header
->parameters
['filename'])) &&
693 (!isset($ent->header
->parameters
['name'])) &&
694 (strtolower($this->header
->disposition
->name
) != 'attachment')) {
695 $entity[] = $this->entity_id
;
702 foreach ($this->entities
as $ent) {
703 if((strtolower($ent->header
->disposition
->name
) != 'attachment') &&
704 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
705 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
710 if(!$strict && !$found) {
711 if (($this->type0
== 'text') &&
712 in_array($this->type1
, array('plain', 'html', 'message')) &&
713 isset($this->entity_id
)) {
714 if (count($this->entities
) == 0) {
715 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
716 $entity[] = $this->entity_id
;
724 function findAlternativeEntity($alt_order) {
725 /* If we are dealing with alternative parts then we */
726 /* choose the best viewable message supported by SM. */
729 foreach($this->entities
as $ent) {
730 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
731 if ($type == 'multipart/related') {
732 $type = $ent->header
->getParameter('type');
734 $altCount = count($alt_order);
735 for ($j = $best_view; $j < $altCount; ++
$j) {
736 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
746 function findRelatedEntity() {
749 $entCount = count($this->entities
);
750 for ($i = 0; $i < $entCount; ++
$i) {
751 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
752 if ($this->header
->getParameter('type') == $type) {
753 $msgs[] = $this->entities
[$i];
760 function getAttachments($exclude_id=array(), $result = array()) {
762 if (($this->type0 == 'message') &&
763 ($this->type1 == 'rfc822') &&
764 ($this->entity_id) ) {
765 $this = $this->entities[0];
768 if (count($this->entities
)) {
769 foreach ($this->entities
as $entity) {
771 foreach ($exclude_id as $excl) {
772 if ($entity->entity_id
=== $excl) {
778 if (($entity->type0
== 'multipart') &&
779 ($entity->type1
!= 'related')) {
780 $result = $entity->getAttachments($exclude_id, $result);
781 } else if ($entity->type0
!= 'multipart') {
788 foreach ($exclude_id as $excl) {
789 $exclude = $exclude ||
($this->entity_id
== $excl);
799 function initAttachment($type, $name, $location) {
800 $attachment = new Message();
801 $mime_header = new MessageHeader();
802 $mime_header->setParameter('name', $name);
803 $pos = strpos($type, '/');
805 $mime_header->type0
= substr($type, 0, $pos);
806 $mime_header->type1
= substr($type, $pos+
1);
808 $mime_header->type0
= $type;
810 $attachment->att_local_name
= $location;
811 $disposition = new Disposition('attachment');
812 $disposition->properties
['filename'] = $name;
813 $mime_header->disposition
= $disposition;
814 $attachment->mime_header
= $mime_header;
815 $this->entities
[]=$attachment;