6 * Copyright (c) 2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
10 * This contains functions needed to handle mime messages.
16 /** msg_header contains generic variables for values that **/
17 /** could be in a header. **/
19 var $type0 = '', $type1 = '', $boundary = '', $charset = '',
20 $encoding='', $size = 0, $to = array(), $from = '', $date = '',
21 $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
22 $id = 0, $mailbox = '', $description = '', $filename = '',
23 $entity_id = 0, $message_id = 0, $name = '', $priority = 3, $type = '',
24 $disposition = '', $md5='', $language='',$dnt = '', $xmailer = '';
27 * returns addres_list of supplied argument
28 * arguments: array('to', 'from', ...) or just a string like 'to'.
29 * result: string: address1, addres2, ....
32 function setVar($var, $value) {
33 $this->{$var} = $value;
36 * function to get the addres strings out of the header.
37 * Arguments: string or array of strings !
38 * example1: header->getAddr_s('to').
39 * example2: header->getAddr_s(array('to','cc','bcc'))
41 function getAddr_s($arr) {
44 foreach($arr as $arg ) {
45 $result = $this->getAddr_s($arg);
50 if ($s) $s = substr($s,2);
54 eval('$addr = $this->'.$arr.';') ;
55 if (is_array($addr)) {
56 foreach ($addr as $addr_o) {
57 if (is_object($addr_o)) {
58 $s .= $addr_o->getAddress() . ', ';
63 if (is_object($addr)) {
64 $s .= $addr->getAddress();
71 function getAddr_a($arg, $excl_arr=array(), $arr = array()) {
73 foreach($arg as $argument ) {
74 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
78 eval('$addr = $this->'.$arg.';') ;
79 if (is_array($addr)) {
80 foreach ($addr as $addr_o) {
81 if (is_object($addr_o)) {
82 if (isset($addr_o->host
) && $addr_o->host
!='') {
83 $email = $addr_o->mailbox
.'@'.$addr_o->host
;
85 $email = $addr_o->mailbox
;
87 $email = strtolower($email);
88 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
89 $arr[$email] = $addr_o->personal
;
94 if (is_object($addr)) {
95 if (isset($addr->host
)) {
96 $email = $addr->mailbox
.'@'.$addr->host
;
98 $email = $addr->mailbox
;
100 $email = strtolower($email);
101 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
102 $arr[$email] = $addr->personal
;
111 class address_structure
{
112 var $personal = '', $adl = '', $mailbox = '', $host = '', $group = '';
114 function getAddress($full=true) {
115 if (is_object($this)) {
116 if (isset($this->host
) && $this->host
!='') {
117 $email = '<'.$this->mailbox
.'@'.$this->host
.'>';
119 $email = $this->mailbox
;
121 if (trim($this->personal
) !='') {
123 $addr = '"' . $this->personal
. '" ' .$email;
125 $addr = $this->personal
;
127 $best_dpl = $this->personal
;
142 /** message is the object that contains messages. It is a recursive
143 object in that through the $entities variable, it can contain
144 more objects of type message. See documentation in mime.txt for
145 a better description of how this works.
147 var $header = '', $entities = array(), $mailbox = 'INBOX', $id = 0,
148 $envelope = '', $parent_ent, $entity, $type0='', $type1='',
149 $parent = '', $decoded_body='',
150 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
153 function setEnt($ent) {
154 $this->entity_id
= $ent;
157 function setBody($body) {
158 $this->decoded_body
= $body;
160 function addEntity ($msg) {
161 $msg->parent
= &$this;
162 $this->entities
[] = $msg;
165 function addRFC822Header($read) {
166 $header = new msg_header();
167 $this->header
= sqimap_parse_RFC822Header($read,$header);
170 function getEntity($ent) {
172 $cur_ent = $this->entity_id
;
174 if ($cur_ent == '' ||
$cur_ent == '0') {
175 $cur_ent_a = array();
177 $cur_ent_a = explode('.',$this->entity_id
);
179 $ent_a = explode('.',$ent);
181 $cnt = count($ent_a);
183 for ($i=0;$i<$cnt -1;$i++
) {
184 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) {
186 $cur_ent_a = explode('.',$msg->entity_id
);
188 } else if (!isset($cur_ent_a[$i])) {
189 if (isset($msg->entities
[($ent_a[$i]-1)])) {
190 $msg = $msg->entities
[($ent_a[$i]-1)];
192 $msg = $msg->entities
[0];
195 if ($msg->type0
== 'message' && $msg->type1
== 'rfc822') {
196 /*this is a header for a message/rfc822 entity */
197 $msg = $msg->entities
[0];
201 if ($msg->type0
== 'message' && $msg->type1
== 'rfc822') {
202 /*this is a header for a message/rfc822 entity */
203 $msg = $msg->entities
[0];
206 if (isset($msg->entities
[($ent_a[$cnt-1])-1])) {
207 $msg = $msg->entities
[($ent_a[$cnt-1]-1)];
213 function getMailbox() {
215 while (is_object($msg->parent
)) {
218 return $msg->mailbox
;
222 * Bodystructure parser, a recursive function for generating the
223 * entity-tree with all the mime-parts.
225 * It follows RFC2060 and stores all the described fields in the
230 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
233 function &parseStructure($read, $i=0, $message = false) {
236 $cnt = strlen($read);
238 $char = strtoupper($read{$i});
243 $msg = new message();
244 $hdr = new msg_header();
245 $hdr->type0
= 'text';
246 $hdr->type1
= 'plain';
247 $hdr->encoding
= 'us-ascii';
249 if ($this->type0
== 'message' && $this->type1
== 'rfc822') {
250 $msg->entity_id
= $this->entity_id
.'.0'; /* header of message/rfc822 */
251 } else if (isset($this->entity_id
) && $this->entity_id
!='') {
252 $ent_no = count($this->entities
)+
1;
253 $par_ent = substr($this->entity_id
,-2);
254 if ($par_ent{0} == '.') {
255 $par_ent = $par_ent{1};
257 if ($par_ent == '0') {
258 $ent_no = count($this->entities
)+
1;
260 $ent = substr($this->entity_id
,0,strrpos($this->entity_id
,'.'));
262 $ent = $ent . ".$ent_no";
266 $msg->entity_id
= $ent;
268 $msg->entity_id
= $ent_no;
271 $ent = $this->entity_id
. ".$ent_no";
272 $msg->entity_id
= $ent;
275 $msg->entity_id
= '0';
278 $msg->header
->type0
= 'multipart';
279 $msg->type0
= 'multipart';
280 while ($read{$i} == '(') {
281 $msg->addEntity($msg->parseStructure($read,&$i));
287 /* multipart properties */
289 $arg_a[] = $this->parseProperties($read,&$i);
293 if (isset($msg->type0
) && $msg->type0
== 'multipart') {
295 $arg_a[]= $msg->parseDisposition($read,&$i);
296 } else { /* properties */
298 $arg_a[] = $msg->parseProperties($read,&$i);
303 if (isset($msg->type0
) && $msg->type0
== 'multipart') {
305 $arg_a[]= $msg->parseLanguage($read,&$i);
308 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') {
310 $msg->header
->type0
= $arg_a[0];
311 $msg->type0
= $arg_a[0];
313 $msg->header
->type1
= $arg_a[1];
314 $msg->type1
= $arg_a[1];
316 $msg->parseEnvelope($read,&$i,&$hdr);
318 while ($i < $cnt && $read{$i} != '(') {
321 $msg->addEntity($msg->parseStructure($read,&$i));
326 $arg_a[] = $msg->parseDisposition($read,&$i);
330 if ($arg_a[0] == 'text' ||
331 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
333 $arg_a[] = $msg->parseDisposition($read,&$i);
336 $arg_a[] = $msg->parseLanguage($read,&$i);
341 if ($arg_a[0] == 'text' ||
342 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
344 $arg_a[] = $msg->parseLanguage($read,&$i);
346 $msg->parseParenthesis($read,&$i);
347 $arg_a[] = ''; /* not yet desribed in rfc2060 */
352 /* unknown argument, skip this part */
353 $msg->parseParenthesis($read,&$i);
361 /* inside an entity -> start processing */
362 $debug = substr($read,$i,20);
363 $arg_s = $msg->parseQuote($read,&$i);
365 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
369 /* probably NIL argument */
370 if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
371 strtoupper(substr($read,$i,4)) == 'NIL)') {
378 /* process the literal value */
379 $arg_a[] = $msg->parseLiteral($read,&$i);
382 case (is_numeric($read{$i}) ):
383 /* process integers */
384 if ($read{$i} == ' ') break;
387 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
395 if (isset($msg->type0
) && $msg->type0
== 'multipart') {
401 if ($arg_a[0] == 'text' ||
402 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
403 $shifted_args = true;
405 $shifted_args = false;
407 $hdr->type0
= $arg_a[0];
408 $hdr->type1
= $arg_a[1];
410 $msg->type0
= $arg_a[0];
411 $msg->type1
= $arg_a[1];
414 if (is_array($arr)) {
415 foreach($arr as $name => $value) {
416 $hdr->{$name} = $value;
419 $hdr->id
= str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
420 $hdr->description
= $arg_a[4];
421 $hdr->encoding
= strtolower($arg_a[5]);
422 $hdr->entity_id
= $msg->entity_id
;
423 $hdr->size
= $arg_a[6];
425 $hdr->lines
= $arg_a[7];
426 if (isset($arg_a[8])) {
427 $hdr->md5
= $arg_a[8];
429 if (isset($arg_a[9])) {
430 $hdr->disposition
= $arg_a[9];
432 if (isset($arg_a[10])) {
433 $hdr->language
= $arg_a[10];
436 if (isset($arg_a[7])) {
437 $hdr->md5
= $arg_a[7];
439 if (isset($arg_a[8])) {
440 $hdr->disposition
= $arg_a[8];
442 if (isset($arg_a[9])) {
443 $hdr->language
= $arg_a[9];
449 if (substr($msg->entity_id
,-2) == '.0' && $msg->type0
!='multipart') {
454 $hdr->type0
= 'multipart';
455 $hdr->type1
= $arg_a[0];
457 $msg->type0
= 'multipart';
458 $msg->type1
= $arg_a[0];
459 if (isset($arg_a[1])) {
461 if (is_array($arr)) {
462 foreach($arr as $name => $value) {
463 $hdr->{$name} = $value;
467 if (isset($arg_a[2])) {
468 $hdr->disposition
= $arg_a[2];
470 if (isset($arg_a[3])) {
471 $hdr->language
= $arg_a[3];
481 } /* parsestructure */
483 function parseProperties($read, $i) {
484 $properties = array();
487 while ($read{$i} != ')') {
488 if ($read{$i} == '"') {
489 $arg_s = $this->parseQuote($read,&$i);
490 } else if ($read{$i} == '{') {
491 $arg_s = $this->parseLiteral($read,&$i);
493 if ($prop_name == '' && $arg_s) {
494 $prop_name = strtolower($arg_s);
495 $properties[$prop_name] = '';
497 } elseif ($prop_name != '' && $arg_s != '') {
498 $properties[$prop_name] = $arg_s;
507 function parseEnvelope($read, $i, $hdr) {
510 $cnt = strlen($read);
511 while ($i< $cnt && $read{$i} != ')') {
513 $char = strtoupper($read{$i});
516 $arg_a[] = $this->parseQuote($read,&$i);
520 $arg_a[] = $this->parseLiteral($read,&$i);
524 /* probably NIL argument */
525 if (strtoupper(substr($read,$i,3)) == 'NIL') {
533 * With group support.
534 * Note: Group support is useless on SMTP connections
535 * because the protocol doesn't support it
540 while ($i < $cnt && $read{$i} != ')') {
541 if ($read{$i} == '(') {
542 $addr = $this->parseAddress($read,&$i);
543 if ($addr->host
== '' && $addr->mailbox
!= '') {
545 $group = $addr->mailbox
;
548 } elseif ($group && $addr->host
== '' && $addr->mailbox
== '') {
550 if ($a == $j+
1) { /* no group members */
551 $group_addr->group
= $group;
552 $group_addr->mailbox
= '';
553 $group_addr->personal
= "$group: Undisclosed recipients;";
554 $addr_a[] = $group_addr;
558 $addr->group
= $group;
572 if (count($arg_a) > 9) {
573 /* argument 1: date */
574 $d = strtr($arg_a[0], array(' ' => ' '));
575 $d = explode(' ', $d);
576 $hdr->date
= getTimeStamp($d);
577 /* argument 2: subject */
578 if (!trim($arg_a[1])) {
579 $arg_a[1]= _("(no subject)");
581 $hdr->subject
= $arg_a[1];
582 /* argument 3: from */
583 $hdr->from
= $arg_a[2][0];
584 /* argument 4: sender */
585 $hdr->sender
= $arg_a[3][0];
586 /* argument 5: reply-to */
587 $hdr->replyto
= $arg_a[4][0];
589 $hdr->to
= $arg_a[5];
591 $hdr->cc
= $arg_a[6];
592 /* argument 8: bcc */
593 $hdr->bcc
= $arg_a[7];
594 /* argument 9: in-reply-to */
595 $hdr->inreplyto
= $arg_a[8];
596 /* argument 10: message-id */
597 $hdr->message_id
= $arg_a[9];
601 function parseLiteral($read, $i) {
604 while ($read{$i} != '}') {
605 $lit_cnt .= $read{$i};
608 $lit_cnt +
=2; /* add the { and } characters */
610 for ($j = 0; $j < $lit_cnt; $j++
) {
617 function parseQuote($read, $i) {
620 while ($read{$i} != '"') {
621 if ($read{$i} == '\\') {
630 function parseAddress($read, $i) {
632 while ($read{$i} != ')' ) { //&& $i < count($read)) {
633 $char = strtoupper($read{$i});
636 $arg_a[] = $this->parseQuote($read,&$i);
639 $arg_a[] = $this->parseLiteral($read,&$i);
642 if (strtolower(substr($read,$i,3)) == 'nil') {
652 if (count($arg_a) == 4) {
653 $adr = new address_structure();
654 $adr->personal
= $arg_a[0];
655 $adr->adl
= $arg_a[1];
656 $adr->mailbox
= $arg_a[2];
657 $adr->host
= $arg_a[3];
664 function parseDisposition($read,&$i) {
666 while ($read{$i} != ')') {
669 $arg_a[] = $this->parseQuote($read,&$i);
672 $arg_a[] = $this->parseLiteral($read,&$i);
675 $arg_a[] = $this->parseProperties($read,&$i);
682 if (isset($arg_a[0])) {
683 $disp = new disposition($arg_a[0]);
684 if (isset($arg_a[1])) {
685 $disp->properties
= $arg_a[1];
688 if (is_object($disp)) {
693 function parseLanguage($read,&$i) {
694 /* no idea how to process this one without examples */
696 while ($read{$i} != ')') {
699 $arg_a[] = $this->parseQuote($read,&$i);
702 $arg_a[] = $this->parseLiteral($read,&$i);
705 $arg_a[] = $this->parseProperties($read,&$i);
712 if (isset($arg_a[0])) {
713 $lang = new language($arg_a[0]);
714 if (isset($arg_a[1])) {
715 $lang->properties
= $arg_a[1];
718 if (is_object($lang)) {
725 function parseParenthesis($read,&$i) {
726 while ($read{$i} != ')') {
729 $this->parseQuote($read,&$i);
732 $this->parseLiteral($read,&$i);
735 $this->parseParenthesis($read,&$i);
744 function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html')) {
746 $type = $this->type0
.'/'.$this->type1
;
747 if ( $type == 'multipart/alternative') {
748 $msg = $this->findAlternativeEntity($alt_order);
749 if (count($msg->entities
) == 0) {
750 $entity[] = $msg->entity_id
;
752 $msg->findDisplayEntity(&$entity, $alt_order);
755 } else if ( $type == 'multipart/related') {
756 $msgs = $this->findRelatedEntity();
757 for ($i = 0; $i < count($msgs); $i++
) {
759 if (count($msg->entities
) == 0) {
760 $entity[] = $msg->entity_id
;
762 $msg->findDisplayEntity(&$entity,$alt_order);
766 } else if ( $this->type0
== 'text' &&
767 ( $this->type1
== 'plain' ||
768 $this->type1
== 'html' ||
769 $this->type1
== 'message') &&
770 isset($this->entity_id
) ) {
771 if (count($this->entities
) == 0) {
772 if (strtolower($this->header
->disposition
->name
) != 'attachment') {
773 echo $this->header
->disposition
->name
;
774 $entity[] = $this->entity_id
;
779 while ( isset($this->entities
[$i]) && !$found &&
780 (strtolower($this->entities
[$i]->header
->disposition
->name
)
782 ($this->entities
[$i]->type0
!= 'message' &&
783 $this->entities
[$i]->type1
!= 'rfc822' )
786 $this->entities
[$i]->findDisplayEntity(&$entity, $alt_order);
790 if ( !isset($entity[0]) ) {
796 function findAlternativeEntity ($alt_order) {
797 /* if we are dealing with alternative parts then we choose the best
798 * viewable message supported by SM.
803 for ($i = 0; $i < count($this->entities
); $i ++
) {
804 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
805 if ($type == 'multipart/related') {
806 $type = $this->entities
[$i]->header
->type
;
808 for ($j = $k; $j < count($alt_order); $j++
) {
809 if ($alt_order[$j] == $type && $j > $best_view) {
816 return $this->entities
[$ent_id];
819 function findRelatedEntity () {
821 for ($i = 0; $i < count($this->entities
); $i ++
) {
822 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
823 if ($this->header
->type
== $type) {
824 $msgs[] = $this->entities
[$i];
830 function getAttachments($exclude_id=array(), $result = array()) {
831 if ($this->type0
== 'message' && $this->type1
== 'rfc822') {
832 $this = $this->entities
[0];
834 if (count($this->entities
)) {
835 foreach ($this->entities
as $entity) {
837 foreach ($exclude_id as $excl) {
838 if ($entity->entity_id
== $excl) {
843 if ($entity->type0
== 'multipart' &&
844 $entity->type1
!= 'related') {
845 $result = $entity->getAttachments($exclude_id, $result);
846 } else if ($entity->type0
!= 'multipart') {
853 foreach ($exclude_id as $excl) {
854 if ($this->entity_id
== $excl) {
868 function disposition($name) {
870 $this->properties
= array();
875 function language($name) {
877 $this->properties
= array();