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) {
171 $cur_ent = $this->entity_id;
172 if ($cur_ent == '' || $cur_ent == '0') {
173 $cur_ent_a = array();
175 $cur_ent_a = explode('.',$this->entity_id);
177 $ent_a = explode('.',$ent);
179 $cnt = count($ent_a);
181 for ($i=0;$i<$cnt -1;$i++) {
182 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) {
184 $cur_ent_a = explode('.',$msg->entity_id);
186 } else if (!isset($cur_ent_a[$i])) {
187 if (isset($msg->entities[($ent_a[$i]-1)])) {
188 $msg = $msg->entities[($ent_a[$i]-1)];
190 $msg = $msg->entities[0];
193 if ($msg->type0 == 'message') {
194 /*this is a header for a message/rfc822 entity */
195 $msg = $msg->entities[0];
198 if ($msg->type0 == 'message') {
199 /*this is a header for a message/rfc822 entity */
200 $msg = $msg->entities[0];
203 if (isset($msg->entities[($ent_a[$cnt-1])-1])) {
204 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
209 * Bodystructure parser, a recursive function for generating the
210 * entity-tree with all the mime-parts.
212 * It follows RFC2060 and stores all the described fields in the
217 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
220 function parseStructure($read, $i=0, $message = false) {
223 $cnt = strlen($read);
225 $char = strtoupper($read{$i});
230 $msg = new message();
231 $hdr = new msg_header();
232 $hdr->type0 = 'text';
233 $hdr->type1 = 'plain';
234 $hdr->encoding = 'us-ascii';
236 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
237 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
238 } else if (isset($this->entity_id) && $this->entity_id !='') {
239 $ent_no = count($this->entities)+1;
240 $par_ent = substr($this->entity_id,-2);
241 if ($par_ent{0} == '.') {
242 $par_ent = $par_ent{1};
244 if ($par_ent == '0') {
245 $ent_no = count($this->entities)+1;
247 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
249 $ent = $ent . ".$ent_no";
253 $msg->entity_id = $ent;
255 $msg->entity_id = $ent_no;
258 $ent = $this->entity_id . ".$ent_no";
259 $msg->entity_id = $ent;
262 $msg->entity_id = '0';
265 $msg->header->type0 = 'multipart';
266 $msg->type0 = 'multipart';
267 while ($read{$i} == '(') {
268 $msg->addEntity($msg->parseStructure($read,&$i));
274 /* multipart properties */
276 $arg_a[] = $this->parseProperties($read,&$i);
280 if (isset($msg->type0) && $msg->type0 == 'multipart') {
282 $arg_a[]= $msg->parseDisposition($read,&$i);
283 } else { /* properties */
285 $arg_a[] = $msg->parseProperties($read,&$i);
290 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') {
292 $msg->header->type0 = $arg_a[0];
293 $msg->type0 = $arg_a[0];
295 $msg->header->type1 = $arg_a[1];
296 $msg->type1 = $arg_a[1];
298 $msg->parseEnvelope($read,&$i,&$hdr);
300 while ($i < $cnt && $read{$i} != '(') {
303 $msg->addEntity($msg->parseStructure($read,&$i));
308 $arg_a[] = $msg->parseDisposition($read,&$i);
312 if ($arg_a[0] == 'text' ||
313 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
315 $arg_a[] = $msg->parseDisposition($read,&$i);
317 $arg_a[] = $msg->parseLanguage($read,&$i);
322 if ($arg_a[0] == 'text' ||
323 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
324 $arg_a[] = $msg->parseLanguage($read,&$i);
326 $arg_a[] = ''; /* not yet desribed in rfc2060 */
331 /* unknown argument, skip this part */
332 $msg->parseParenthesis($read,&$i);
340 /* inside an entity -> start processing */
341 $debug = substr($read,$i,20);
342 $arg_s = $msg->parseQuote($read,&$i);
344 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
348 /* probably NIL argument */
349 if (strtolower(substr($read,$i,3)) == 'nil') {
356 /* process the literal value */
357 $arg_a[] = $msg->parseLiteral($read,&$i);
360 case (is_numeric($read{$i}) ):
361 /* process integers */
362 if ($read{$i} == ' ') break;
365 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
373 if (isset($msg->type0) && $msg->type0 == 'multipart') {
379 if ($arg_a[0] == 'text' ||
380 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
381 $shifted_args = true;
383 $shifted_args = false;
385 $hdr->type0 = $arg_a[0];
386 $hdr->type1 = $arg_a[1];
388 $msg->type0 = $arg_a[0];
389 $msg->type1 = $arg_a[1];
392 if (is_array($arr)) {
393 foreach($arr as $name => $value) {
394 $hdr->{$name} = $value;
397 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
398 $hdr->description = $arg_a[4];
399 $hdr->encoding = strtolower($arg_a[5]);
400 $hdr->entity_id = $msg->entity_id;
401 $hdr->size = $arg_a[6];
403 $hdr->lines = $arg_a[7];
404 if (isset($arg_a[8])) {
405 $hdr->md5 = $arg_a[8];
407 if (isset($arg_a[9])) {
408 $hdr->disposition = $arg_a[9];
410 if (isset($arg_a[10])) {
411 $hdr->language = $arg_a[10];
414 if (isset($arg_a[7])) {
415 $hdr->md5 = $arg_a[7];
417 if (isset($arg_a[8])) {
418 $hdr->disposition = $arg_a[8];
420 if (isset($arg_a[9])) {
421 $hdr->language = $arg_a[9];
427 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
432 $hdr->type0 = 'multipart';
433 $hdr->type1 = $arg_a[0];
435 $msg->type0 = 'multipart';
436 $msg->type1 = $arg_a[0];
437 if (isset($arg_a[1])) {
439 if (is_array($arr)) {
440 foreach($arr as $name => $value) {
441 $hdr->{$name} = $value;
445 if (isset($arg_a[2])) {
446 $hdr->disposition = $arg_a[2];
448 if (isset($arg_a[3])) {
449 $hdr->language = $arg_a[3];
459 } /* parsestructure */
461 function parseProperties($read, $i) {
462 $properties = array();
465 while ($read{$i} != ')') {
466 if ($read{$i} == '"') {
467 $arg_s = $this->parseQuote($read,&$i);
468 } else if ($read{$i} == '{') {
469 $arg_s = $this->parseLiteral($read,&$i);
471 if ($prop_name == '' && $arg_s) {
472 $prop_name = strtolower($arg_s);
473 $properties[$prop_name] = '';
475 } elseif ($prop_name != '' && $arg_s != '') {
476 $properties[$prop_name] = $arg_s;
485 function parseEnvelope($read, $i, $hdr) {
488 $cnt = strlen($read);
489 while ($i< $cnt && $read{$i} != ')') {
491 $char = strtoupper($read{$i});
494 $arg_a[] = $this->parseQuote($read,&$i);
498 $arg_a[] = $this->parseLiteral($read,&$i);
502 /* probably NIL argument */
503 if (strtolower(substr($read,$i,3)) == 'nil') {
511 * With group support.
512 * Note: Group support is useless on SMTP connections
513 * because the protocol doesn't support it
518 while ($i < $cnt && $read{$i} != ')') {
519 if ($read{$i} == '(') {
520 $addr = $this->parseAddress($read,&$i);
521 if ($addr->host == '' && $addr->mailbox != '') {
523 $group = $addr->mailbox;
526 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
528 if ($a == $j+1) { /* no group members */
529 $group_addr->group = $group;
530 $group_addr->mailbox = '';
531 $group_addr->personal = "$group: Undisclosed recipients;";
532 $addr_a[] = $group_addr;
536 $addr->group = $group;
550 if (count($arg_a) > 9) {
551 /* argument 1: date */
552 $d = strtr($arg_a[0], array(' ' => ' '));
553 $d = explode(' ', $d);
554 $hdr->date = getTimeStamp($d);
555 /* argument 2: subject */
556 if (!trim($arg_a[1])) {
557 $arg_a[1]= _("(no subject)");
559 $hdr->subject = $arg_a[1];
560 /* argument 3: from */
561 $hdr->from = $arg_a[2][0];
562 /* argument 4: sender */
563 $hdr->sender = $arg_a[3][0];
564 /* argument 5: reply-to */
565 $hdr->replyto = $arg_a[4][0];
567 $hdr->to = $arg_a[5];
569 $hdr->cc = $arg_a[6];
570 /* argument 8: bcc */
571 $hdr->bcc = $arg_a[7];
572 /* argument 9: in-reply-to */
573 $hdr->inreplyto = $arg_a[8];
574 /* argument 10: message-id */
575 $hdr->message_id = $arg_a[9];
579 function parseLiteral($read, $i) {
582 while ($read{$i} != '}') {
583 $lit_cnt .= $read{$i};
586 $lit_cnt +=2; /* add the { and } characters */
588 for ($j = 0; $j < $lit_cnt; $j++) {
595 function parseQuote($read, $i) {
598 $cnt = strlen($read);
599 while ($i < $cnt && $read{$i} != '"') {
600 if ($read{$i} == '\\') {
609 function parseAddress($read, $i) {
611 while ($read{$i} != ')') {
612 $char = strtoupper($read{$i});
615 $arg_a[] = $this->parseQuote($read,&$i);
618 $arg_a[] = $this->parseLiteral($read,&$i);
621 if (strtolower(substr($read,$i,3)) == 'nil') {
630 if (count($arg_a) == 4) {
631 $adr = new address_structure();
632 $adr->personal = $arg_a[0];
633 $adr->adl = $arg_a[1];
634 $adr->mailbox = $arg_a[2];
635 $adr->host = $arg_a[3];
640 function parseDisposition($read,$i) {
642 while ($read{$i} != ')') {
645 $arg_a[] = $this->parseQuote($read,&$i);
648 $arg_a[] = $this->parseLiteral($read,&$i);
651 $arg_a[] = $this->parseProperties($read,&$i);
658 if (isset($arg_a[0])) {
659 $disp = new disposition($arg_a[0]);
660 if (isset($arg_a[1])) {
661 $disp->properties = $arg_a[1];
664 if (is_object($disp)) return $disp;
667 function parseLanguage($read,&$i) {
668 /* no idea how to process this one without examples */
672 function parseParenthesis($read,&$i) {
673 while ($read{$i} != ')') {
676 $this->parseQuote($read,&$i);
679 $this->parseLiteral($read,&$i);
682 $this->parseParenthesis($read,&$i);
693 function disposition($name) {
695 $this->properties = array();