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);
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;
144 /** message is the object that contains messages. It is a recursive
145 object in that through the $entities variable, it can contain
146 more objects of type message. See documentation in mime.txt for
147 a better description of how this works.
149 var $header = '', $entities = array(), $mailbox = 'INBOX', $id = 0,
150 $envelope = '', $parent_ent, $entity, $type0='', $type1='',
151 $parent = '', $decoded_body='',
152 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
155 function setEnt($ent) {
156 $this->entity_id= $ent;
159 function setBody($body) {
160 $this->decoded_body = $body;
162 function addEntity ($msg) {
163 $msg->parent = &$this;
164 $this->entities[] = $msg;
167 function addRFC822Header($read) {
168 $header = new msg_header();
169 $this->header = sqimap_parse_RFC822Header($read,$header);
172 function getEntity($ent) {
173 $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);
182 $cnt = $cnt - 1; /* this is how we use the count, so we'll save some time */
184 for ($i=0;$i<$cnt;$i++) {
185 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) {
187 $cur_ent_a = explode('.',$msg->entity_id);
189 } else if (!isset($cur_ent_a[$i])) {
190 if (isset($msg->entities[($ent_a[$i]-1)])) {
191 $msg = $msg->entities[($ent_a[$i]-1)];
193 $msg = $msg->entities[0];
196 if ($msg->type0 == 'message') {
197 /*this is a header for a message/rfc822 entity */
198 $msg = $msg->entities[0];
201 if ($msg->type0 == 'message') {
202 /*this is a header for a message/rfc822 entity */
203 $msg = $msg->entities[0];
206 if (isset($msg->entities[($ent_a[$cnt])-1])) {
207 $msg = $msg->entities[($ent_a[$cnt]-1)];
212 * Bodystructure parser, a recursive function for generating the
213 * entity-tree with all the mime-parts.
215 * It follows RFC2060 and stores all the described fields in the
220 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
223 function parseStructure($read, $i=0, $message = false) {
226 $cnt = strlen($read);
227 for (;$i < $cnt;$i++) {
228 $char = strtoupper($read{$i});
233 $msg = new message();
234 $hdr = new msg_header();
235 $hdr->type0 = 'text';
236 $hdr->type1 = 'plain';
237 $hdr->encoding = 'us-ascii';
239 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
240 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
241 } else if (isset($this->entity_id) && $this->entity_id !='') {
242 $ent_no = count($this->entities)+1;
243 $par_ent = substr($this->entity_id,-2);
244 if ($par_ent{0} == '.') {
245 $par_ent = $par_ent{1};
247 if ($par_ent == '0') {
248 $ent_no = count($this->entities)+1;
250 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
252 $ent = $ent . ".$ent_no";
256 $msg->entity_id = $ent;
258 $msg->entity_id = $ent_no;
261 $ent = $this->entity_id . ".$ent_no";
262 $msg->entity_id = $ent;
265 $msg->entity_id = '0';
268 $msg->header->type0 = 'multipart';
269 $msg->type0 = 'multipart';
270 while ($read{$i} == '(') {
271 $msg->addEntity($msg->parseStructure($read,&$i));
277 /* multipart properties */
279 $arg_a[] = $this->parseProperties($read,&$i);
283 if (isset($msg->type0) && $msg->type0 == 'multipart') {
285 $arg_a[]= $msg->parseDisposition($read,&$i);
288 $arg_a[] = $msg->parseProperties($read,&$i);
293 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') {
294 $msg->header->type0 = $arg_a[0];
295 $msg->type0 = $arg_a[0];
296 $msg->header->type1 = $arg_a[1];
297 $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);
345 $arg_s = strtolower($arg_s); /* type0 and type1 */
350 /* probably NIL argument */
351 if (strtolower(substr($read,$i,3)) == 'nil') {
358 /* process the literal value */
359 $arg_a[] = $msg->parseLiteral($read,&$i);
362 case (is_numeric($read{$i}) ):
363 /* process integers */
364 if ($read{$i} == ' ') {
369 while (preg_match('/\d+/',$read{$i})) { /* != ' ') {*/
377 if (isset($msg->type0) && $msg->type0 == 'multipart') {
383 if ($arg_a[0] == 'text' ||
384 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
385 $shifted_args = true;
387 $shifted_args = false;
389 $hdr->type0 = $arg_a[0];
390 $hdr->type1 = $arg_a[1];
392 $msg->type0 = $arg_a[0];
393 $msg->type1 = $arg_a[1];
396 if (is_array($arr)) {
397 foreach($arr as $name => $value) {
398 $hdr->{$name} = $value;
401 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
402 $hdr->description = $arg_a[4];
403 $hdr->encoding = strtolower($arg_a[5]);
404 $hdr->entity_id = $msg->entity_id;
405 $hdr->size = $arg_a[6];
407 $hdr->lines = $arg_a[7];
408 if (isset($arg_a[8])) {
409 $hdr->md5 = $arg_a[8];
411 if (isset($arg_a[9])) {
412 $hdr->disposition = $arg_a[9];
414 if (isset($arg_a[10])) {
415 $hdr->language = $arg_a[10];
418 if (isset($arg_a[7])) {
419 $hdr->md5 = $arg_a[7];
421 if (isset($arg_a[8])) {
422 $hdr->disposition = $arg_a[8];
424 if (isset($arg_a[9])) {
425 $hdr->language = $arg_a[9];
431 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
436 $hdr->type0 = 'multipart';
437 $hdr->type1 = $arg_a[0];
439 $msg->type0 = 'multipart';
440 $msg->type1 = $arg_a[0];
441 if (isset($arg_a[1])) {
443 if (is_array($arr)) {
444 foreach($arr as $name => $value) {
445 $hdr->{$name} = $value;
449 if (isset($arg_a[2])) {
450 $hdr->disposition = $arg_a[2];
452 if (isset($arg_a[3])) {
453 $hdr->language = $arg_a[3];
462 } /* parsestructure */
464 function parseProperties($read, $i) {
465 $properties = array();
468 while ($read{$i} != ')') {
469 if ($read{$i} == '"') {
470 $arg_s = $this->parseQuote($read,&$i);
471 } else if ($read{$i} == '{') {
472 $arg_s = $this->parseLiteral($read,&$i);
474 if ($prop_name == '' && $arg_s) {
475 $prop_name = strtolower($arg_s);
476 $properties[$prop_name] = '';
478 } elseif ($prop_name != '' && $arg_s != '') {
479 $properties[$prop_name] = $arg_s;
488 function parseEnvelope($read, $i, $hdr) {
491 $cnt = strlen($read);
492 while ($i < $cnt && $read{$i} != ')') {
494 $char = strtoupper($read{$i});
497 $arg_a[] = $this->parseQuote($read,&$i);
501 $arg_a[] = $this->parseLiteral($read,&$i);
505 /* probably NIL argument */
506 if (strtolower(substr($read,$i,3)) == 'nil') {
514 * With group support.
515 * Note: Group support is useless on SMTP connections
516 * because the protocol doesn't support it
521 while ($i < $cnt && $read{$i} != ')') {
522 if ($read{$i} == '(') {
523 $addr = $this->parseAddress($read,&$i);
524 if ($addr->host == '' && $addr->mailbox != '') {
526 $group = $addr->mailbox;
529 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
531 if ($a == $j+1) { /* no group members */
532 $group_addr->group = $group;
533 $group_addr->mailbox = '';
534 $group_addr->personal = "$group: Undisclosed recipients;";
535 $addr_a[] = $group_addr;
539 $addr->group = $group;
553 if (count($arg_a) > 9) {
554 /* argument 1: date */
555 $d = strtr($arg_a[0], array(' ' => ' '));
556 $d = explode(' ', $d);
557 $hdr->date = getTimeStamp($d);
558 /* argument 2: subject */
559 if (!trim($arg_a[1])) {
560 $arg_a[1]= _("(no subject)");
562 $hdr->subject = $arg_a[1];
563 /* argument 3: from */
564 $hdr->from = $arg_a[2][0];
565 /* argument 4: sender */
566 $hdr->sender = $arg_a[3][0];
567 /* argument 5: reply-to */
568 $hdr->replyto = $arg_a[4][0];
570 $hdr->to = $arg_a[5];
572 $hdr->cc = $arg_a[6];
573 /* argument 8: bcc */
574 $hdr->bcc = $arg_a[7];
575 /* argument 9: in-reply-to */
576 $hdr->inreplyto = $arg_a[8];
577 /* argument 10: message-id */
578 $hdr->message_id = $arg_a[9];
582 function parseLiteral($read, $i) {
585 while ($read{$i} != '}') {
586 $lit_cnt .= $read{$i};
589 $lit_cnt +=2; /* add the { and } characters */
591 for ($j = 0; $j < $lit_cnt; $j++) {
598 function parseQuote($read, $i) {
601 $cnt = strlen($read);
602 while ($i < $cnt && $read{$i} != '"') {
603 if ($read{$i} == '\\') {
612 function parseAddress($read, $i) {
614 while ($read{$i} != ')') {
615 $char = strtoupper($read{$i});
618 $arg_a[] = $this->parseQuote($read,&$i);
621 $arg_a[] = $this->parseLiteral($read,&$i);
624 if (strtolower(substr($read,$i,3)) == 'nil') {
633 if (count($arg_a) == 4) {
634 $adr = new address_structure();
635 $adr->personal = $arg_a[0];
636 $adr->adl = $arg_a[1];
637 $adr->mailbox = $arg_a[2];
638 $adr->host = $arg_a[3];
645 function parseDisposition($read,$i) {
647 while ($read{$i} != ')') {
650 $arg_a[] = $this->parseQuote($read,&$i);
653 $arg_a[] = $this->parseLiteral($read,&$i);
656 $arg_a[] = $this->parseProperties($read,&$i);
663 if (isset($arg_a[0])) {
664 $disp = new disposition($arg_a[0]);
665 if (isset($arg_a[1])) {
666 $disp->properties = $arg_a[1];
669 if (is_object($disp)) {
674 function parseLanguage($read,&$i) {
675 /* no idea how to process this one without examples */
679 function parseParenthesis($read,&$i) {
680 while ($read{$i} != ')') {
683 $this->parseQuote($read,&$i);
686 $this->parseLiteral($read,&$i);
689 $this->parseParenthesis($read,&$i);
700 function disposition($name) {
702 $this->properties = array();