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') {
196 /*this is a header for a message/rfc822 entity */
197 $msg = $msg->entities[0];
200 if ($msg->type0 == 'message') {
201 /*this is a header for a message/rfc822 entity */
202 $msg = $msg->entities[0];
205 if (isset($msg->entities[($ent_a[$cnt-1])-1])) {
206 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
211 function getMailbox() {
213 while (is_object($msg->parent)) {
216 return $msg->mailbox;
220 * Bodystructure parser, a recursive function for generating the
221 * entity-tree with all the mime-parts.
223 * It follows RFC2060 and stores all the described fields in the
228 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
231 function parseStructure($read, $i=0, $message = false) {
234 $cnt = strlen($read);
236 $char = strtoupper($read{$i});
241 $msg = new message();
242 $hdr = new msg_header();
243 $hdr->type0 = 'text';
244 $hdr->type1 = 'plain';
245 $hdr->encoding = 'us-ascii';
247 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
248 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
249 } else if (isset($this->entity_id) && $this->entity_id !='') {
250 $ent_no = count($this->entities)+1;
251 $par_ent = substr($this->entity_id,-2);
252 if ($par_ent{0} == '.') {
253 $par_ent = $par_ent{1};
255 if ($par_ent == '0') {
256 $ent_no = count($this->entities)+1;
258 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
260 $ent = $ent . ".$ent_no";
264 $msg->entity_id = $ent;
266 $msg->entity_id = $ent_no;
269 $ent = $this->entity_id . ".$ent_no";
270 $msg->entity_id = $ent;
273 $msg->entity_id = '0';
276 $msg->header->type0 = 'multipart';
277 $msg->type0 = 'multipart';
278 while ($read{$i} == '(') {
279 $msg->addEntity($msg->parseStructure($read,&$i));
285 /* multipart properties */
287 $arg_a[] = $this->parseProperties($read,&$i);
291 if (isset($msg->type0) && $msg->type0 == 'multipart') {
293 $arg_a[]= $msg->parseDisposition($read,&$i);
294 } else { /* properties */
296 $arg_a[] = $msg->parseProperties($read,&$i);
301 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') {
303 $msg->header->type0 = $arg_a[0];
304 $msg->type0 = $arg_a[0];
306 $msg->header->type1 = $arg_a[1];
307 $msg->type1 = $arg_a[1];
309 $msg->parseEnvelope($read,&$i,&$hdr);
311 while ($i < $cnt && $read{$i} != '(') {
314 $msg->addEntity($msg->parseStructure($read,&$i));
319 $arg_a[] = $msg->parseDisposition($read,&$i);
323 if ($arg_a[0] == 'text' ||
324 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
326 $arg_a[] = $msg->parseDisposition($read,&$i);
328 $arg_a[] = $msg->parseLanguage($read,&$i);
333 if ($arg_a[0] == 'text' ||
334 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
335 $arg_a[] = $msg->parseLanguage($read,&$i);
337 $arg_a[] = ''; /* not yet desribed in rfc2060 */
342 /* unknown argument, skip this part */
343 $msg->parseParenthesis($read,&$i);
351 /* inside an entity -> start processing */
352 $debug = substr($read,$i,20);
353 $arg_s = $msg->parseQuote($read,&$i);
355 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
359 /* probably NIL argument */
360 if (strtolower(substr($read,$i,3)) == 'nil') {
367 /* process the literal value */
368 $arg_a[] = $msg->parseLiteral($read,&$i);
371 case (is_numeric($read{$i}) ):
372 /* process integers */
373 if ($read{$i} == ' ') break;
376 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
384 if (isset($msg->type0) && $msg->type0 == 'multipart') {
390 if ($arg_a[0] == 'text' ||
391 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
392 $shifted_args = true;
394 $shifted_args = false;
396 $hdr->type0 = $arg_a[0];
397 $hdr->type1 = $arg_a[1];
399 $msg->type0 = $arg_a[0];
400 $msg->type1 = $arg_a[1];
403 if (is_array($arr)) {
404 foreach($arr as $name => $value) {
405 $hdr->{$name} = $value;
408 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
409 $hdr->description = $arg_a[4];
410 $hdr->encoding = strtolower($arg_a[5]);
411 $hdr->entity_id = $msg->entity_id;
412 $hdr->size = $arg_a[6];
414 $hdr->lines = $arg_a[7];
415 if (isset($arg_a[8])) {
416 $hdr->md5 = $arg_a[8];
418 if (isset($arg_a[9])) {
419 $hdr->disposition = $arg_a[9];
421 if (isset($arg_a[10])) {
422 $hdr->language = $arg_a[10];
425 if (isset($arg_a[7])) {
426 $hdr->md5 = $arg_a[7];
428 if (isset($arg_a[8])) {
429 $hdr->disposition = $arg_a[8];
431 if (isset($arg_a[9])) {
432 $hdr->language = $arg_a[9];
438 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
443 $hdr->type0 = 'multipart';
444 $hdr->type1 = $arg_a[0];
446 $msg->type0 = 'multipart';
447 $msg->type1 = $arg_a[0];
448 if (isset($arg_a[1])) {
450 if (is_array($arr)) {
451 foreach($arr as $name => $value) {
452 $hdr->{$name} = $value;
456 if (isset($arg_a[2])) {
457 $hdr->disposition = $arg_a[2];
459 if (isset($arg_a[3])) {
460 $hdr->language = $arg_a[3];
470 } /* parsestructure */
472 function parseProperties($read, $i) {
473 $properties = array();
476 while ($read{$i} != ')') {
477 if ($read{$i} == '"') {
478 $arg_s = $this->parseQuote($read,&$i);
479 } else if ($read{$i} == '{') {
480 $arg_s = $this->parseLiteral($read,&$i);
482 if ($prop_name == '' && $arg_s) {
483 $prop_name = strtolower($arg_s);
484 $properties[$prop_name] = '';
486 } elseif ($prop_name != '' && $arg_s != '') {
487 $properties[$prop_name] = $arg_s;
496 function parseEnvelope($read, $i, $hdr) {
499 $cnt = strlen($read);
500 while ($i< $cnt && $read{$i} != ')') {
502 $char = strtoupper($read{$i});
505 $arg_a[] = $this->parseQuote($read,&$i);
509 $arg_a[] = $this->parseLiteral($read,&$i);
513 /* probably NIL argument */
514 if (strtolower(substr($read,$i,3)) == 'nil') {
522 * With group support.
523 * Note: Group support is useless on SMTP connections
524 * because the protocol doesn't support it
529 while ($i < $cnt && $read{$i} != ')') {
530 if ($read{$i} == '(') {
531 $addr = $this->parseAddress($read,&$i);
532 if ($addr->host == '' && $addr->mailbox != '') {
534 $group = $addr->mailbox;
537 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
539 if ($a == $j+1) { /* no group members */
540 $group_addr->group = $group;
541 $group_addr->mailbox = '';
542 $group_addr->personal = "$group: Undisclosed recipients;";
543 $addr_a[] = $group_addr;
547 $addr->group = $group;
561 if (count($arg_a) > 9) {
562 /* argument 1: date */
563 $d = strtr($arg_a[0], array(' ' => ' '));
564 $d = explode(' ', $d);
565 $hdr->date = getTimeStamp($d);
566 /* argument 2: subject */
567 if (!trim($arg_a[1])) {
568 $arg_a[1]= _("(no subject)");
570 $hdr->subject = $arg_a[1];
571 /* argument 3: from */
572 $hdr->from = $arg_a[2][0];
573 /* argument 4: sender */
574 $hdr->sender = $arg_a[3][0];
575 /* argument 5: reply-to */
576 $hdr->replyto = $arg_a[4][0];
578 $hdr->to = $arg_a[5];
580 $hdr->cc = $arg_a[6];
581 /* argument 8: bcc */
582 $hdr->bcc = $arg_a[7];
583 /* argument 9: in-reply-to */
584 $hdr->inreplyto = $arg_a[8];
585 /* argument 10: message-id */
586 $hdr->message_id = $arg_a[9];
590 function parseLiteral($read, $i) {
593 while ($read{$i} != '}') {
594 $lit_cnt .= $read{$i};
597 $lit_cnt +=2; /* add the { and } characters */
599 for ($j = 0; $j < $lit_cnt; $j++) {
606 function parseQuote($read, $i) {
609 $cnt = strlen($read);
610 while ($i < $cnt && $read{$i} != '"') {
611 if ($read{$i} == '\\') {
620 function parseAddress($read, $i) {
622 while ($read{$i} != ')') {
623 $char = strtoupper($read{$i});
626 $arg_a[] = $this->parseQuote($read,&$i);
629 $arg_a[] = $this->parseLiteral($read,&$i);
632 if (strtolower(substr($read,$i,3)) == 'nil') {
641 if (count($arg_a) == 4) {
642 $adr = new address_structure();
643 $adr->personal = $arg_a[0];
644 $adr->adl = $arg_a[1];
645 $adr->mailbox = $arg_a[2];
646 $adr->host = $arg_a[3];
651 function parseDisposition($read,$i) {
653 while ($read{$i} != ')') {
656 $arg_a[] = $this->parseQuote($read,&$i);
659 $arg_a[] = $this->parseLiteral($read,&$i);
662 $arg_a[] = $this->parseProperties($read,&$i);
669 if (isset($arg_a[0])) {
670 $disp = new disposition($arg_a[0]);
671 if (isset($arg_a[1])) {
672 $disp->properties = $arg_a[1];
675 if (is_object($disp)) return $disp;
678 function parseLanguage($read,&$i) {
679 /* no idea how to process this one without examples */
683 function parseParenthesis($read,&$i) {
684 while ($read{$i} != ')') {
687 $this->parseQuote($read,&$i);
690 $this->parseLiteral($read,&$i);
693 $this->parseParenthesis($read,&$i);
702 function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html')) {
705 $type = $this->type0.'/'.$this->type1;
706 if ( $type == 'multipart/alternative') {
707 $msg = $this->findAlternativeEntity($alt_order);
708 if (count($msg->entities) == 0) {
709 $entity[] = $msg->entity_id;
711 $msg->findDisplayEntity(&$entity, $alt_order);
714 } else if ( $type == 'multipart/related') {
715 $msgs = $this->findRelatedEntity();
716 for ($i = 0; $i < count($msgs); $i++) {
718 if (count($msg->entities) == 0) {
719 $entity[] = $msg->entity_id;
721 $msg->findDisplayEntity(&$entity,$alt_order);
725 } else if ( $this->type0 == 'text' &&
726 ( $this->type1 == 'plain' ||
727 $this->type1 == 'html' ) &&
728 isset($this->entity_id) ) {
729 if (count($this->entities) == 0) {
730 if (!$this->header->disposition->name == 'attachment') {
731 $entity[] = $this->entity_id;
736 while ( isset($this->entities[$i]) && !$found &&
737 !($this->entities[$i]->header->disposition->name
739 !($this->entities[$i]->type0 == 'message' &&
740 $this->entities[$i]->type1 == 'rfc822' )
743 $this->entities[$i]->findDisplayEntity(&$entity, $alt_order);
747 if ( !isset($entity[0]) ) {
753 function findAlternativeEntity ($alt_order) {
754 /* if we are dealing with alternative parts then we choose the best
755 * viewable message supported by SM.
760 for ($i = 0; $i < count($this->entities); $i ++) {
761 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
762 if ($type == 'multipart/related') {
763 $type = $this->entities[$i]->header->type;
765 for ($j = $k; $j < count($alt_order); $j++) {
766 if ($alt_order[$j] == $type && $j > $best_view) {
773 return $this->entities[$ent_id];
776 function findRelatedEntity () {
778 for ($i = 0; $i < count($this->entities); $i ++) {
779 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
780 if ($this->header->type == $type) {
781 $msgs[] = $this->entities[$i];
787 function getAttachments($exclude_id=array(), $result = array()) {
788 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
789 $this = $this->entities[0];
791 if (count($this->entities)) {
792 foreach ($this->entities as $entity) {
794 foreach ($exclude_id as $excl) {
795 if ($entity->entity_id == $excl) {
800 if ($entity->type0 == 'multipart' && $entity->type1 == 'digest') {
801 $result = $entity->getAttachments($exclude_id, $result);
802 } else if ($entity->type0 != 'multipart') {
809 foreach ($exclude_id as $excl) {
810 if ($this->entity_id == $excl) {
824 function disposition($name) {
826 $this->properties = array();