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);
335 $arg_a[] = $msg->parseLanguage($read,&$i);
340 if ($arg_a[0] == 'text' ||
341 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
342 $arg_a[] = $msg->parseLanguage($read,&$i);
344 $arg_a[] = ''; /* not yet desribed in rfc2060 */
349 /* unknown argument, skip this part */
350 $msg->parseParenthesis($read,&$i);
358 /* inside an entity -> start processing */
359 $debug = substr($read,$i,20);
360 $arg_s = $msg->parseQuote($read,&$i);
362 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
366 /* probably NIL argument */
367 if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
368 strtoupper(substr($read,$i,4)) == 'NIL)') {
375 /* process the literal value */
376 $arg_a[] = $msg->parseLiteral($read,&$i);
379 case (is_numeric($read{$i}) ):
380 /* process integers */
381 if ($read{$i} == ' ') break;
384 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
392 if (isset($msg->type0) && $msg->type0 == 'multipart') {
398 if ($arg_a[0] == 'text' ||
399 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
400 $shifted_args = true;
402 $shifted_args = false;
404 $hdr->type0 = $arg_a[0];
405 $hdr->type1 = $arg_a[1];
407 $msg->type0 = $arg_a[0];
408 $msg->type1 = $arg_a[1];
411 if (is_array($arr)) {
412 foreach($arr as $name => $value) {
413 $hdr->{$name} = $value;
416 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
417 $hdr->description = $arg_a[4];
418 $hdr->encoding = strtolower($arg_a[5]);
419 $hdr->entity_id = $msg->entity_id;
420 $hdr->size = $arg_a[6];
422 $hdr->lines = $arg_a[7];
423 if (isset($arg_a[8])) {
424 $hdr->md5 = $arg_a[8];
426 if (isset($arg_a[9])) {
427 $hdr->disposition = $arg_a[9];
429 if (isset($arg_a[10])) {
430 $hdr->language = $arg_a[10];
433 if (isset($arg_a[7])) {
434 $hdr->md5 = $arg_a[7];
436 if (isset($arg_a[8])) {
437 $hdr->disposition = $arg_a[8];
439 if (isset($arg_a[9])) {
440 $hdr->language = $arg_a[9];
446 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
451 $hdr->type0 = 'multipart';
452 $hdr->type1 = $arg_a[0];
454 $msg->type0 = 'multipart';
455 $msg->type1 = $arg_a[0];
456 if (isset($arg_a[1])) {
458 if (is_array($arr)) {
459 foreach($arr as $name => $value) {
460 $hdr->{$name} = $value;
464 if (isset($arg_a[2])) {
465 $hdr->disposition = $arg_a[2];
467 if (isset($arg_a[3])) {
468 $hdr->language = $arg_a[3];
478 } /* parsestructure */
480 function parseProperties($read, $i) {
481 $properties = array();
484 while ($read{$i} != ')') {
485 if ($read{$i} == '"') {
486 $arg_s = $this->parseQuote($read,&$i);
487 } else if ($read{$i} == '{') {
488 $arg_s = $this->parseLiteral($read,&$i);
490 if ($prop_name == '' && $arg_s) {
491 $prop_name = strtolower($arg_s);
492 $properties[$prop_name] = '';
494 } elseif ($prop_name != '' && $arg_s != '') {
495 $properties[$prop_name] = $arg_s;
504 function parseEnvelope($read, $i, $hdr) {
507 $cnt = strlen($read);
508 while ($i< $cnt && $read{$i} != ')') {
510 $char = strtoupper($read{$i});
513 $arg_a[] = $this->parseQuote($read,&$i);
517 $arg_a[] = $this->parseLiteral($read,&$i);
521 /* probably NIL argument */
522 if (strtoupper(substr($read,$i,3)) == 'NIL') {
530 * With group support.
531 * Note: Group support is useless on SMTP connections
532 * because the protocol doesn't support it
537 while ($i < $cnt && $read{$i} != ')') {
538 if ($read{$i} == '(') {
539 $addr = $this->parseAddress($read,&$i);
540 if ($addr->host == '' && $addr->mailbox != '') {
542 $group = $addr->mailbox;
545 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
547 if ($a == $j+1) { /* no group members */
548 $group_addr->group = $group;
549 $group_addr->mailbox = '';
550 $group_addr->personal = "$group: Undisclosed recipients;";
551 $addr_a[] = $group_addr;
555 $addr->group = $group;
569 if (count($arg_a) > 9) {
570 /* argument 1: date */
571 $d = strtr($arg_a[0], array(' ' => ' '));
572 $d = explode(' ', $d);
573 $hdr->date = getTimeStamp($d);
574 /* argument 2: subject */
575 if (!trim($arg_a[1])) {
576 $arg_a[1]= _("(no subject)");
578 $hdr->subject = $arg_a[1];
579 /* argument 3: from */
580 $hdr->from = $arg_a[2][0];
581 /* argument 4: sender */
582 $hdr->sender = $arg_a[3][0];
583 /* argument 5: reply-to */
584 $hdr->replyto = $arg_a[4][0];
586 $hdr->to = $arg_a[5];
588 $hdr->cc = $arg_a[6];
589 /* argument 8: bcc */
590 $hdr->bcc = $arg_a[7];
591 /* argument 9: in-reply-to */
592 $hdr->inreplyto = $arg_a[8];
593 /* argument 10: message-id */
594 $hdr->message_id = $arg_a[9];
598 function parseLiteral($read, $i) {
601 while ($read{$i} != '}') {
602 $lit_cnt .= $read{$i};
605 $lit_cnt +=2; /* add the { and } characters */
607 for ($j = 0; $j < $lit_cnt; $j++) {
614 function parseQuote($read, $i) {
617 while ($read{$i} != '"') {
618 if ($read{$i} == '\\') {
627 function parseAddress($read, $i) {
629 while ($read{$i} != ')' ) { //&& $i < count($read)) {
630 $char = strtoupper($read{$i});
633 $arg_a[] = $this->parseQuote($read,&$i);
636 $arg_a[] = $this->parseLiteral($read,&$i);
639 if (strtolower(substr($read,$i,3)) == 'nil') {
649 if (count($arg_a) == 4) {
650 $adr = new address_structure();
651 $adr->personal = $arg_a[0];
652 $adr->adl = $arg_a[1];
653 $adr->mailbox = $arg_a[2];
654 $adr->host = $arg_a[3];
661 function parseDisposition($read,&$i) {
663 while ($read{$i} != ')') {
666 $arg_a[] = $this->parseQuote($read,&$i);
669 $arg_a[] = $this->parseLiteral($read,&$i);
672 $arg_a[] = $this->parseProperties($read,&$i);
679 if (isset($arg_a[0])) {
680 $disp = new disposition($arg_a[0]);
681 if (isset($arg_a[1])) {
682 $disp->properties = $arg_a[1];
685 if (is_object($disp)) {
690 function parseLanguage($read,&$i) {
691 /* no idea how to process this one without examples */
693 while ($read{$i} != ')') {
696 $arg = $this->parseQuote($read,&$i);
699 $arg = $this->parseLiteral($read,&$i);
702 $arg = $this->parseProperties($read,&$i);
712 function parseParenthesis($read,&$i) {
713 while ($read{$i} != ')') {
716 $this->parseQuote($read,&$i);
719 $this->parseLiteral($read,&$i);
722 $this->parseParenthesis($read,&$i);
731 function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html')) {
733 $type = $this->type0.'/'.$this->type1;
734 if ( $type == 'multipart/alternative') {
735 $msg = $this->findAlternativeEntity($alt_order);
736 if (count($msg->entities) == 0) {
737 $entity[] = $msg->entity_id;
739 $msg->findDisplayEntity(&$entity, $alt_order);
742 } else if ( $type == 'multipart/related') {
743 $msgs = $this->findRelatedEntity();
744 for ($i = 0; $i < count($msgs); $i++) {
746 if (count($msg->entities) == 0) {
747 $entity[] = $msg->entity_id;
749 $msg->findDisplayEntity(&$entity,$alt_order);
753 } else if ( $this->type0 == 'text' &&
754 ( $this->type1 == 'plain' ||
755 $this->type1 == 'html' ||
756 $this->type1 == 'message') &&
757 isset($this->entity_id) ) {
758 if (count($this->entities) == 0) {
759 if (!$this->header->disposition->name == 'attachment') {
760 $entity[] = $this->entity_id;
765 while ( isset($this->entities[$i]) && !$found &&
766 !($this->entities[$i]->header->disposition->name
768 !($this->entities[$i]->type0 == 'message' &&
769 $this->entities[$i]->type1 == 'rfc822' )
772 $this->entities[$i]->findDisplayEntity(&$entity, $alt_order);
776 if ( !isset($entity[0]) ) {
782 function findAlternativeEntity ($alt_order) {
783 /* if we are dealing with alternative parts then we choose the best
784 * viewable message supported by SM.
789 for ($i = 0; $i < count($this->entities); $i ++) {
790 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
791 if ($type == 'multipart/related') {
792 $type = $this->entities[$i]->header->type;
794 for ($j = $k; $j < count($alt_order); $j++) {
795 if ($alt_order[$j] == $type && $j > $best_view) {
802 return $this->entities[$ent_id];
805 function findRelatedEntity () {
807 for ($i = 0; $i < count($this->entities); $i ++) {
808 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
809 if ($this->header->type == $type) {
810 $msgs[] = $this->entities[$i];
816 function getAttachments($exclude_id=array(), $result = array()) {
817 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
818 $this = $this->entities[0];
820 if (count($this->entities)) {
821 foreach ($this->entities as $entity) {
823 foreach ($exclude_id as $excl) {
824 if ($entity->entity_id == $excl) {
829 if ($entity->type0 == 'multipart' && $entity->type1 == 'digest') {
830 $result = $entity->getAttachments($exclude_id, $result);
831 } else if ($entity->type0 != 'multipart') {
838 foreach ($exclude_id as $excl) {
839 if ($this->entity_id == $excl) {
853 function disposition($name) {
855 $this->properties = array();