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 /** msg_header contains generic variables for values that **/
16 /** could be in a header. **/
18 var $type0 = '', $type1 = '', $boundary = '', $charset = '',
19 $encoding='', $size = 0, $to = array(), $from = '', $date = '',
20 $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
21 $id = 0, $mailbox = '', $description = '', $filename = '',
22 $entity_id = 0, $message_id = 0, $name = '', $priority = 3, $type = '',
23 $disposition = '', $md5='', $language='',$dnt = '', $xmailer = '';
26 * returns addres_list of supplied argument
27 * arguments: array('to', 'from', ...) or just a string like 'to'.
28 * result: string: address1, addres2, ....
31 function setVar($var, $value) {
32 $this->{$var} = $value;
35 * function to get the addres strings out of the header.
36 * Arguments: string or array of strings !
37 * example1: header->getAddr_s('to').
38 * example2: header->getAddr_s(array('to','cc','bcc'))
40 function getAddr_s($arr) {
43 foreach($arr as $arg ) {
44 $result = $this->getAddr_s($arg);
49 if ($s) $s = substr($s,2);
53 eval('$addr = $this->'.$arr.';') ;
54 if (is_array($addr)) {
55 foreach ($addr as $addr_o) {
56 if (is_object($addr_o)) {
57 $s .= $addr_o->getAddress() . ', ';
62 if (is_object($addr)) {
63 $s .= $addr->getAddress();
70 function getAddr_a($arg, $excl_arr=array(), $arr = array()) {
72 foreach($arg as $argument ) {
73 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
77 eval('$addr = $this->'.$arg.';') ;
78 if (is_array($addr)) {
79 foreach ($addr as $addr_o) {
80 if (is_object($addr_o)) {
81 if (isset($addr_o->host) && $addr_o->host !='') {
82 $email = $addr_o->mailbox.'@'.$addr_o->host;
84 $email = $addr_o->mailbox;
86 $email = strtolower($email);
87 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
88 $arr[$email] = $addr_o->personal;
93 if (is_object($addr)) {
94 if (isset($addr->host)) {
95 $email = $addr->mailbox.'@'.$addr->host;
97 $email = $addr->mailbox;
99 $email = strtolower($email);
100 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
101 $arr[$email] = $addr->personal;
110 class address_structure {
111 var $personal = '', $adl = '', $mailbox = '', $host = '', $group = '';
113 function getAddress($full=true) {
114 if (is_object($this)) {
115 if (isset($this->host) && $this->host !='') {
116 $email = '<'.$this->mailbox.'@'.$this->host.'>';
118 $email = $this->mailbox;
120 if (trim($this->personal) !='') {
122 $addr = '"' . $this->personal . '" ' .$email;
124 $addr = $this->personal;
126 $best_dpl = $this->personal;
141 /** message is the object that contains messages. It is a recursive
142 object in that through the $entities variable, it can contain
143 more objects of type message. See documentation in mime.txt for
144 a better description of how this works.
146 var $header = '', $entities = array(), $mailbox = 'INBOX', $id = 0,
147 $envelope = '', $parent_ent, $entity, $type0='', $type1='',
148 $parent = '', $decoded_body='',
149 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
152 function setEnt($ent) {
153 $this->entity_id= $ent;
156 function setBody($body) {
157 $this->decoded_body = $body;
159 function addEntity ($msg) {
160 $msg->parent = &$this;
161 $this->entities[] = $msg;
164 function addRFC822Header($read) {
165 $header = new msg_header();
166 $this->header = sqimap_parse_RFC822Header($read,$header);
169 function getEntity($ent) {
171 $cur_ent = $this->entity_id;
173 if ($cur_ent == '' || $cur_ent == '0') {
174 $cur_ent_a = array();
176 $cur_ent_a = explode('.',$this->entity_id);
178 $ent_a = explode('.',$ent);
180 $cnt = count($ent_a);
182 for ($i=0;$i<$cnt -1;$i++) {
183 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) {
185 $cur_ent_a = explode('.',$msg->entity_id);
187 } else if (!isset($cur_ent_a[$i])) {
188 if (isset($msg->entities[($ent_a[$i]-1)])) {
189 $msg = $msg->entities[($ent_a[$i]-1)];
191 $msg = $msg->entities[0];
194 if ($msg->type0 == 'message' && $msg->type1 == 'rfc822') {
195 /*this is a header for a message/rfc822 entity */
196 $msg = $msg->entities[0];
200 if ($msg->type0 == 'message' && $msg->type1 == 'rfc822') {
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)];
212 function getMailbox() {
214 while (is_object($msg->parent)) {
217 return $msg->mailbox;
221 * Bodystructure parser, a recursive function for generating the
222 * entity-tree with all the mime-parts.
224 * It follows RFC2060 and stores all the described fields in the
229 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
232 function &parseStructure($read, $i=0, $message = false) {
235 $cnt = strlen($read);
237 $char = strtoupper($read{$i});
242 $msg = new message();
243 $hdr = new msg_header();
244 $hdr->type0 = 'text';
245 $hdr->type1 = 'plain';
246 $hdr->encoding = 'us-ascii';
248 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
249 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
250 } else if (isset($this->entity_id) && $this->entity_id !='') {
251 $ent_no = count($this->entities)+1;
252 $par_ent = substr($this->entity_id,-2);
253 if ($par_ent{0} == '.') {
254 $par_ent = $par_ent{1};
256 if ($par_ent == '0') {
257 $ent_no = count($this->entities)+1;
259 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
261 $ent = $ent . ".$ent_no";
265 $msg->entity_id = $ent;
267 $msg->entity_id = $ent_no;
270 $ent = $this->entity_id . ".$ent_no";
271 $msg->entity_id = $ent;
274 $msg->entity_id = '0';
277 $msg->header->type0 = 'multipart';
278 $msg->type0 = 'multipart';
279 while ($read{$i} == '(') {
280 $msg->addEntity($msg->parseStructure($read,&$i));
286 /* multipart properties */
288 $arg_a[] = $this->parseProperties($read,&$i);
292 if (isset($msg->type0) && $msg->type0 == 'multipart') {
294 $arg_a[]= $msg->parseDisposition($read,&$i);
295 } else { /* properties */
297 $arg_a[] = $msg->parseProperties($read,&$i);
302 if (isset($msg->type0) && $msg->type0 == 'multipart') {
304 $arg_a[]= $msg->parseLanguage($read,&$i);
307 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') {
309 $msg->header->type0 = $arg_a[0];
310 $msg->type0 = $arg_a[0];
312 $msg->header->type1 = $arg_a[1];
313 $msg->type1 = $arg_a[1];
315 $msg->parseEnvelope($read,&$i,&$hdr);
317 while ($i < $cnt && $read{$i} != '(') {
320 $msg->addEntity($msg->parseStructure($read,&$i));
325 $arg_a[] = $msg->parseDisposition($read,&$i);
329 if ($arg_a[0] == 'text' ||
330 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
332 $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')) {
343 $arg_a[] = $msg->parseLanguage($read,&$i);
345 $msg->parseParenthesis($read,&$i);
346 $arg_a[] = ''; /* not yet desribed in rfc2060 */
351 /* unknown argument, skip this part */
352 $msg->parseParenthesis($read,&$i);
360 /* inside an entity -> start processing */
361 $debug = substr($read,$i,20);
362 $arg_s = $msg->parseQuote($read,&$i);
364 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
368 /* probably NIL argument */
369 if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
370 strtoupper(substr($read,$i,4)) == 'NIL)') {
377 /* process the literal value */
378 $arg_a[] = $msg->parseLiteral($read,&$i);
381 case (is_numeric($read{$i}) ):
382 /* process integers */
383 if ($read{$i} == ' ') break;
386 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
394 if (isset($msg->type0) && $msg->type0 == 'multipart') {
400 if ($arg_a[0] == 'text' ||
401 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
402 $shifted_args = true;
404 $shifted_args = false;
406 $hdr->type0 = $arg_a[0];
407 $hdr->type1 = $arg_a[1];
409 $msg->type0 = $arg_a[0];
410 $msg->type1 = $arg_a[1];
413 if (is_array($arr)) {
414 foreach($arr as $name => $value) {
415 $hdr->{$name} = $value;
418 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
419 $hdr->description = $arg_a[4];
420 $hdr->encoding = strtolower($arg_a[5]);
421 $hdr->entity_id = $msg->entity_id;
422 $hdr->size = $arg_a[6];
424 $hdr->lines = $arg_a[7];
425 if (isset($arg_a[8])) {
426 $hdr->md5 = $arg_a[8];
428 if (isset($arg_a[9])) {
429 $hdr->disposition = $arg_a[9];
431 if (isset($arg_a[10])) {
432 $hdr->language = $arg_a[10];
435 if (isset($arg_a[7])) {
436 $hdr->md5 = $arg_a[7];
438 if (isset($arg_a[8])) {
439 $hdr->disposition = $arg_a[8];
441 if (isset($arg_a[9])) {
442 $hdr->language = $arg_a[9];
448 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
453 $hdr->type0 = 'multipart';
454 $hdr->type1 = $arg_a[0];
456 $msg->type0 = 'multipart';
457 $msg->type1 = $arg_a[0];
458 if (isset($arg_a[1])) {
460 if (is_array($arr)) {
461 foreach($arr as $name => $value) {
462 $hdr->{$name} = $value;
466 if (isset($arg_a[2])) {
467 $hdr->disposition = $arg_a[2];
469 if (isset($arg_a[3])) {
470 $hdr->language = $arg_a[3];
480 } /* parsestructure */
482 function parseProperties($read, $i) {
483 $properties = array();
486 while ($read{$i} != ')') {
487 if ($read{$i} == '"') {
488 $arg_s = $this->parseQuote($read,&$i);
489 } else if ($read{$i} == '{') {
490 $arg_s = $this->parseLiteral($read,&$i);
492 if ($prop_name == '' && $arg_s) {
493 $prop_name = strtolower($arg_s);
494 $properties[$prop_name] = '';
496 } elseif ($prop_name != '' && $arg_s != '') {
497 $properties[$prop_name] = $arg_s;
506 function parseEnvelope($read, $i, $hdr) {
509 $cnt = strlen($read);
510 while ($i< $cnt && $read{$i} != ')') {
512 $char = strtoupper($read{$i});
515 $arg_a[] = $this->parseQuote($read,&$i);
519 $arg_a[] = $this->parseLiteral($read,&$i);
523 /* probably NIL argument */
524 if (strtoupper(substr($read,$i,3)) == 'NIL') {
532 * With group support.
533 * Note: Group support is useless on SMTP connections
534 * because the protocol doesn't support it
539 while ($i < $cnt && $read{$i} != ')') {
540 if ($read{$i} == '(') {
541 $addr = $this->parseAddress($read,&$i);
542 if ($addr->host == '' && $addr->mailbox != '') {
544 $group = $addr->mailbox;
547 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
549 if ($a == $j+1) { /* no group members */
550 $group_addr->group = $group;
551 $group_addr->mailbox = '';
552 $group_addr->personal = "$group: Undisclosed recipients;";
553 $addr_a[] = $group_addr;
557 $addr->group = $group;
571 if (count($arg_a) > 9) {
572 /* argument 1: date */
573 $d = strtr($arg_a[0], array(' ' => ' '));
574 $d = explode(' ', $d);
575 $hdr->date = getTimeStamp($d);
576 /* argument 2: subject */
577 if (!trim($arg_a[1])) {
578 $arg_a[1]= _("(no subject)");
580 $hdr->subject = $arg_a[1];
581 /* argument 3: from */
582 $hdr->from = $arg_a[2][0];
583 /* argument 4: sender */
584 $hdr->sender = $arg_a[3][0];
585 /* argument 5: reply-to */
586 $hdr->replyto = $arg_a[4][0];
588 $hdr->to = $arg_a[5];
590 $hdr->cc = $arg_a[6];
591 /* argument 8: bcc */
592 $hdr->bcc = $arg_a[7];
593 /* argument 9: in-reply-to */
594 $hdr->inreplyto = $arg_a[8];
595 /* argument 10: message-id */
596 $hdr->message_id = $arg_a[9];
600 function parseLiteral($read, $i) {
603 while ($read{$i} != '}') {
604 $lit_cnt .= $read{$i};
607 $lit_cnt +=2; /* add the { and } characters */
609 for ($j = 0; $j < $lit_cnt; $j++) {
616 function parseQuote($read, $i) {
619 while ($read{$i} != '"') {
620 if ($read{$i} == '\\') {
629 function parseAddress($read, $i) {
631 while ($read{$i} != ')' ) { //&& $i < count($read)) {
632 $char = strtoupper($read{$i});
635 $arg_a[] = $this->parseQuote($read,&$i);
638 $arg_a[] = $this->parseLiteral($read,&$i);
641 if (strtolower(substr($read,$i,3)) == 'nil') {
651 if (count($arg_a) == 4) {
652 $adr = new address_structure();
653 $adr->personal = $arg_a[0];
654 $adr->adl = $arg_a[1];
655 $adr->mailbox = $arg_a[2];
656 $adr->host = $arg_a[3];
663 function parseDisposition($read,&$i) {
665 while ($read{$i} != ')') {
668 $arg_a[] = $this->parseQuote($read,&$i);
671 $arg_a[] = $this->parseLiteral($read,&$i);
674 $arg_a[] = $this->parseProperties($read,&$i);
681 if (isset($arg_a[0])) {
682 $disp = new disposition($arg_a[0]);
683 if (isset($arg_a[1])) {
684 $disp->properties = $arg_a[1];
687 if (is_object($disp)) {
692 function parseLanguage($read,&$i) {
693 /* no idea how to process this one without examples */
695 while ($read{$i} != ')') {
698 $arg_a[] = $this->parseQuote($read,&$i);
701 $arg_a[] = $this->parseLiteral($read,&$i);
704 $arg_a[] = $this->parseProperties($read,&$i);
711 if (isset($arg_a[0])) {
712 $lang = new language($arg_a[0]);
713 if (isset($arg_a[1])) {
714 $lang->properties = $arg_a[1];
717 if (is_object($lang)) {
724 function parseParenthesis($read,&$i) {
725 while ($read{$i} != ')') {
728 $this->parseQuote($read,&$i);
731 $this->parseLiteral($read,&$i);
734 $this->parseParenthesis($read,&$i);
743 function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html')) {
745 $type = $this->type0.'/'.$this->type1;
746 if ( $type == 'multipart/alternative') {
747 $msg = $this->findAlternativeEntity($alt_order);
748 if (count($msg->entities) == 0) {
749 $entity[] = $msg->entity_id;
751 $msg->findDisplayEntity(&$entity, $alt_order);
754 } else if ( $type == 'multipart/related') {
755 $msgs = $this->findRelatedEntity();
756 for ($i = 0; $i < count($msgs); $i++) {
758 if (count($msg->entities) == 0) {
759 $entity[] = $msg->entity_id;
761 $msg->findDisplayEntity(&$entity,$alt_order);
765 } else if ( $this->type0 == 'text' &&
766 ( $this->type1 == 'plain' ||
767 $this->type1 == 'html' ||
768 $this->type1 == 'message') &&
769 isset($this->entity_id) ) {
770 if (count($this->entities) == 0) {
771 if (!$this->header->disposition->name == 'attachment') {
772 $entity[] = $this->entity_id;
777 while ( isset($this->entities[$i]) && !$found &&
778 !($this->entities[$i]->header->disposition->name
780 !($this->entities[$i]->type0 == 'message' &&
781 $this->entities[$i]->type1 == 'rfc822' )
784 $this->entities[$i]->findDisplayEntity(&$entity, $alt_order);
788 if ( !isset($entity[0]) ) {
794 function findAlternativeEntity ($alt_order) {
795 /* if we are dealing with alternative parts then we choose the best
796 * viewable message supported by SM.
801 for ($i = 0; $i < count($this->entities); $i ++) {
802 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
803 if ($type == 'multipart/related') {
804 $type = $this->entities[$i]->header->type;
806 for ($j = $k; $j < count($alt_order); $j++) {
807 if ($alt_order[$j] == $type && $j > $best_view) {
814 return $this->entities[$ent_id];
817 function findRelatedEntity () {
819 for ($i = 0; $i < count($this->entities); $i ++) {
820 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
821 if ($this->header->type == $type) {
822 $msgs[] = $this->entities[$i];
828 function getAttachments($exclude_id=array(), $result = array()) {
829 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
830 $this = $this->entities[0];
832 if (count($this->entities)) {
833 foreach ($this->entities as $entity) {
835 foreach ($exclude_id as $excl) {
836 if ($entity->entity_id == $excl) {
841 if ($entity->type0 == 'multipart' && $entity->type1 == 'digest') {
842 $result = $entity->getAttachments($exclude_id, $result);
843 } else if ($entity->type0 != 'multipart') {
850 foreach ($exclude_id as $excl) {
851 if ($this->entity_id == $excl) {
865 function disposition($name) {
867 $this->properties = array();
872 function language($name) {
874 $this->properties = array();