fix for expunge with UID in the sid
[squirrelmail.git] / class / mime.class
1 <?php
2
3 /**
4 * mime.class
5 *
6 * Copyright (c) 2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 *
10 * This contains functions needed to handle mime messages.
11 *
12 * $Id$
13 */
14
15 class msg_header {
16 /** msg_header contains generic variables for values that **/
17 /** could be in a header. **/
18
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 = '';
25
26 /*
27 * returns addres_list of supplied argument
28 * arguments: array('to', 'from', ...) or just a string like 'to'.
29 * result: string: address1, addres2, ....
30 */
31
32 function setVar($var, $value) {
33 $this->{$var} = $value;
34 }
35 /*
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'))
40 */
41 function getAddr_s($arr) {
42 if (is_array($arr)) {
43 $s = '';
44 foreach($arr as $arg ) {
45 $result = $this->getAddr_s($arg);
46 if ($result) {
47 $s .= ', ' . $result;
48 }
49 }
50 if ($s) $s = substr($s,2);
51 return $s;
52 } else {
53 $s = '';
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() . ', ';
59 }
60 }
61 $s = substr($s,0,-2);
62 } else {
63 if (is_object($addr)) {
64 $s .= $addr->getAddress();
65 }
66 }
67 return $s;
68 }
69 }
70
71 function getAddr_a($arg, $excl_arr=array(), $arr = array()) {
72 if (is_array($arg)) {
73 foreach($arg as $argument ) {
74 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
75 }
76 return $arr;
77 } else {
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;
84 } else {
85 $email = $addr_o->mailbox;
86 }
87 $email = strtolower($email);
88 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
89 $arr[$email] = $addr_o->personal;
90 }
91 }
92 }
93 } else {
94 if (is_object($addr)) {
95 if (isset($addr->host)) {
96 $email = $addr->mailbox.'@'.$addr->host;
97 } else {
98 $email = $addr->mailbox;
99 }
100 $email = strtolower($email);
101 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
102 $arr[$email] = $addr->personal;
103 }
104 }
105 }
106 return $arr;
107 }
108 }
109 }
110
111 class address_structure {
112 var $personal = '', $adl = '', $mailbox = '', $host = '', $group = '';
113
114 function getAddress($full=true) {
115 if (is_object($this)) {
116 if (isset($this->host) && $this->host !='') {
117 $email = '<'.$this->mailbox.'@'.$this->host.'>';
118 } else {
119 $email = $this->mailbox;
120 }
121 if (trim($this->personal) !='') {
122 if ($email) {
123 $addr = '"' . $this->personal . '" ' .$email;
124 } else {
125 $addr = $this->personal;
126 }
127 $best_dpl = $this->personal;
128 } else {
129 $addr = $email;
130 $best_dpl = $email;
131 }
132 if ($full) {
133 return $addr;
134 } else {
135 return $best_dpl;
136 }
137 } else return '';
138 }
139 }
140
141 class message {
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.
146 **/
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,
151 $is_mdnsent = 0;
152
153 function setEnt($ent) {
154 $this->entity_id= $ent;
155 }
156
157 function setBody($body) {
158 $this->decoded_body = $body;
159 }
160 function addEntity ($msg) {
161 $msg->parent = &$this;
162 $this->entities[] = $msg;
163 }
164
165 function addRFC822Header($read) {
166 $header = new msg_header();
167 $this->header = sqimap_parse_RFC822Header($read,$header);
168 }
169
170 function getEntity($ent) {
171 $cur_ent = $this->entity_id;
172 if ($cur_ent == '' || $cur_ent == '0') {
173 $cur_ent_a = array();
174 } else {
175 $cur_ent_a = explode('.',$this->entity_id);
176 }
177 $ent_a = explode('.',$ent);
178
179 $cnt = count($ent_a);
180 $msg = $this;
181 for ($i=0;$i<$cnt -1;$i++) {
182 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) {
183 $msg = $msg->parent;
184 $cur_ent_a = explode('.',$msg->entity_id);
185 $i--;
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)];
189 } else {
190 $msg = $msg->entities[0];
191 }
192 }
193 if ($msg->type0 == 'message') {
194 /*this is a header for a message/rfc822 entity */
195 $msg = $msg->entities[0];
196 }
197 }
198 if ($msg->type0 == 'message') {
199 /*this is a header for a message/rfc822 entity */
200 $msg = $msg->entities[0];
201 }
202
203 if (isset($msg->entities[($ent_a[$cnt-1])-1])) {
204 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
205 }
206 return $msg;
207 }
208 /*
209 * Bodystructure parser, a recursive function for generating the
210 * entity-tree with all the mime-parts.
211 *
212 * It follows RFC2060 and stores all the described fields in the
213 * message object.
214 *
215 * Question/Bugs:
216 *
217 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
218 *
219 */
220 function parseStructure($read, $i=0, $message = false) {
221 $arg_no = 0;
222 $arg_a = array();
223 $cnt = strlen($read);
224 while ($i < $cnt) {
225 $char = strtoupper($read{$i});
226 switch ($char) {
227 case '(':
228 if ($arg_no == 0 ) {
229 if (!isset($msg)) {
230 $msg = new message();
231 $hdr = new msg_header();
232 $hdr->type0 = 'text';
233 $hdr->type1 = 'plain';
234 $hdr->encoding = 'us-ascii';
235
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};
243 }
244 if ($par_ent == '0') {
245 $ent_no = count($this->entities)+1;
246 if ($ent_no > 0) {
247 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
248 if ($ent) {
249 $ent = $ent . ".$ent_no";
250 } else {
251 $ent = $ent_no;
252 }
253 $msg->entity_id = $ent;
254 } else {
255 $msg->entity_id = $ent_no;
256 }
257 } else {
258 $ent = $this->entity_id . ".$ent_no";
259 $msg->entity_id = $ent;
260 }
261 } else {
262 $msg->entity_id = '0';
263 }
264 } else {
265 $msg->header->type0 = 'multipart';
266 $msg->type0 = 'multipart';
267 while ($read{$i} == '(') {
268 $msg->addEntity($msg->parseStructure($read,&$i));
269 }
270 }
271 } else {
272 switch ($arg_no) {
273 case 1:
274 /* multipart properties */
275 $i++;
276 $arg_a[] = $this->parseProperties($read,&$i);
277 $arg_no++;
278 break;
279 case 2:
280 if (isset($msg->type0) && $msg->type0 == 'multipart') {
281 $i++;
282 $arg_a[]= $msg->parseDisposition($read,&$i);
283 } else { /* properties */
284 /* properties */
285 $arg_a[] = $msg->parseProperties($read,&$i);
286 }
287 $arg_no++;
288 break;
289 case 7:
290 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') {
291
292 $msg->header->type0 = $arg_a[0];
293 $msg->type0 = $arg_a[0];
294
295 $msg->header->type1 = $arg_a[1];
296 $msg->type1 = $arg_a[1];
297
298 $msg->parseEnvelope($read,&$i,&$hdr);
299 $i++;
300 while ($i < $cnt && $read{$i} != '(') {
301 $i++;
302 }
303 $msg->addEntity($msg->parseStructure($read,&$i));
304 }
305 break;
306 case 8:
307 $i++;
308 $arg_a[] = $msg->parseDisposition($read,&$i);
309 $arg_no++;
310 break;
311 case 9:
312 if ($arg_a[0] == 'text' ||
313 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
314 $i++;
315 $arg_a[] = $msg->parseDisposition($read,&$i);
316 } else {
317 $arg_a[] = $msg->parseLanguage($read,&$i);
318 }
319 $arg_no++;
320 break;
321 case 10:
322 if ($arg_a[0] == 'text' ||
323 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
324 $arg_a[] = $msg->parseLanguage($read,&$i);
325 } else {
326 $arg_a[] = ''; /* not yet desribed in rfc2060 */
327 }
328 $arg_no++;
329 break;
330 default:
331 /* unknown argument, skip this part */
332 $msg->parseParenthesis($read,&$i);
333 $arg_a[] = '';
334 $arg_no++;
335 break;
336 } /* switch */
337 }
338 break;
339 case '"':
340 /* inside an entity -> start processing */
341 $debug = substr($read,$i,20);
342 $arg_s = $msg->parseQuote($read,&$i);
343 $arg_no++;
344 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
345 $arg_a[] = $arg_s;
346 break;
347 case 'N':
348 /* probably NIL argument */
349 if (strtolower(substr($read,$i,3)) == 'nil') {
350 $arg_a[] = '';
351 $arg_no++;
352 $i = $i+2;
353 }
354 break;
355 case '{':
356 /* process the literal value */
357 $arg_a[] = $msg->parseLiteral($read,&$i);
358 $arg_no++;
359 break;
360 case (is_numeric($read{$i}) ):
361 /* process integers */
362 if ($read{$i} == ' ') break;
363 $arg_s = $read{$i};;
364 $i++;
365 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
366 $arg_s .= $read{$i};
367 $i++;
368 }
369 $arg_no++;
370 $arg_a[] = $arg_s;
371 break;
372 case ')':
373 if (isset($msg->type0) && $msg->type0 == 'multipart') {
374 $multipart = true;
375 } else {
376 $multipart = false;
377 }
378 if (!$multipart) {
379 if ($arg_a[0] == 'text' ||
380 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
381 $shifted_args = true;
382 } else {
383 $shifted_args = false;
384 }
385 $hdr->type0 = $arg_a[0];
386 $hdr->type1 = $arg_a[1];
387
388 $msg->type0 = $arg_a[0];
389 $msg->type1 = $arg_a[1];
390
391 $arr = $arg_a[2];
392 if (is_array($arr)) {
393 foreach($arr as $name => $value) {
394 $hdr->{$name} = $value;
395 }
396 }
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];
402 if ($shifted_args) {
403 $hdr->lines = $arg_a[7];
404 if (isset($arg_a[8])) {
405 $hdr->md5 = $arg_a[8];
406 }
407 if (isset($arg_a[9])) {
408 $hdr->disposition = $arg_a[9];
409 }
410 if (isset($arg_a[10])) {
411 $hdr->language = $arg_a[10];
412 }
413 } else {
414 if (isset($arg_a[7])) {
415 $hdr->md5 = $arg_a[7];
416 }
417 if (isset($arg_a[8])) {
418 $hdr->disposition = $arg_a[8];
419 }
420 if (isset($arg_a[9])) {
421 $hdr->language = $arg_a[9];
422 }
423 }
424 $msg->header = $hdr;
425 $arg_no = 0;
426 $i++;
427 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
428 $msg->entity_id++;
429 }
430 return $msg;
431 } else {
432 $hdr->type0 = 'multipart';
433 $hdr->type1 = $arg_a[0];
434
435 $msg->type0 = 'multipart';
436 $msg->type1 = $arg_a[0];
437 if (isset($arg_a[1])) {
438 $arr = $arg_a[1];
439 if (is_array($arr)) {
440 foreach($arr as $name => $value) {
441 $hdr->{$name} = $value;
442 }
443 }
444 }
445 if (isset($arg_a[2])) {
446 $hdr->disposition = $arg_a[2];
447 }
448 if (isset($arg_a[3])) {
449 $hdr->language = $arg_a[3];
450 }
451 $msg->header = $hdr;
452 return $msg;
453 }
454 default:
455 break;
456 } /* switch */
457 $i++;
458 } /* while */
459 } /* parsestructure */
460
461 function parseProperties($read, $i) {
462 $properties = array();
463 $arg_s = '';
464 $prop_name = '';
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);
470 }
471 if ($prop_name == '' && $arg_s) {
472 $prop_name = strtolower($arg_s);
473 $properties[$prop_name] = '';
474 $arg_s = '';
475 } elseif ($prop_name != '' && $arg_s != '') {
476 $properties[$prop_name] = $arg_s;
477 $prop_name = '';
478 $arg_s = '';
479 }
480 $i++;
481 }
482 return $properties;
483 }
484
485 function parseEnvelope($read, $i, $hdr) {
486 $arg_no = 0;
487 $arg_a = array();
488 $cnt = strlen($read);
489 while ($i< $cnt && $read{$i} != ')') {
490 $i++;
491 $char = strtoupper($read{$i});
492 switch ($char) {
493 case '"':
494 $arg_a[] = $this->parseQuote($read,&$i);
495 $arg_no++;
496 break;
497 case '{':
498 $arg_a[] = $this->parseLiteral($read,&$i);
499 $arg_no++;
500 break;
501 case 'N':
502 /* probably NIL argument */
503 if (strtolower(substr($read,$i,3)) == 'nil') {
504 $arg_a[] = '';
505 $arg_no++;
506 $i = $i+2;
507 }
508 break;
509 case '(':
510 /* Address structure
511 * With group support.
512 * Note: Group support is useless on SMTP connections
513 * because the protocol doesn't support it
514 */
515 $addr_a = array();
516 $group = '';
517 $a=0;
518 while ($i < $cnt && $read{$i} != ')') {
519 if ($read{$i} == '(') {
520 $addr = $this->parseAddress($read,&$i);
521 if ($addr->host == '' && $addr->mailbox != '') {
522 /* start of group */
523 $group = $addr->mailbox;
524 $group_addr = $addr;
525 $j = $a;
526 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
527 /* end group */
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;
533 $group ='';
534 }
535 } else {
536 $addr->group = $group;
537 $addr_a[] = $addr;
538 }
539 $a++;
540 }
541 $i++;
542 }
543 $arg_a[] = $addr_a;
544 break;
545 default:
546 break;
547 }
548 $i++;
549 }
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)");
558 }
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];
566 /* argument 6: to */
567 $hdr->to = $arg_a[5];
568 /* argument 7: cc */
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];
576 }
577 }
578
579 function parseLiteral($read, $i) {
580 $lit_cnt = '';
581 $i++;
582 while ($read{$i} != '}') {
583 $lit_cnt .= $read{$i};
584 $i++;
585 }
586 $lit_cnt +=2; /* add the { and } characters */
587 $s = '';
588 for ($j = 0; $j < $lit_cnt; $j++) {
589 $i++;
590 $s .= $read{$i};
591 }
592 return $s;
593 }
594
595 function parseQuote($read, $i) {
596 $i++;
597 $s = '';
598 $cnt = strlen($read);
599 while ($i < $cnt && $read{$i} != '"') {
600 if ($read{$i} == '\\') {
601 $i++;
602 }
603 $s .= $read{$i};
604 $i++;
605 }
606 return $s;
607 }
608
609 function parseAddress($read, $i) {
610 $arg_a = array();
611 while ($read{$i} != ')') {
612 $char = strtoupper($read{$i});
613 switch ($char) {
614 case '"':
615 $arg_a[] = $this->parseQuote($read,&$i);
616 break;
617 case '{':
618 $arg_a[] = $this->parseLiteral($read,&$i);
619 break;
620 case 'N':
621 if (strtolower(substr($read,$i,3)) == 'nil') {
622 $arg_a[] = '';
623 $i = $i+2;
624 }
625 default:
626 break;
627 }
628 $i++;
629 }
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];
636 } else $adr = '';
637 return $adr;
638 }
639
640 function parseDisposition($read,$i) {
641 $arg_a = array();
642 while ($read{$i} != ')') {
643 switch ($read{$i}) {
644 case '"':
645 $arg_a[] = $this->parseQuote($read,&$i);
646 break;
647 case '{':
648 $arg_a[] = $this->parseLiteral($read,&$i);
649 break;
650 case '(':
651 $arg_a[] = $this->parseProperties($read,&$i);
652 break;
653 default:
654 break;
655 }
656 $i++;
657 }
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];
662 }
663 }
664 if (is_object($disp)) return $disp;
665 }
666
667 function parseLanguage($read,&$i) {
668 /* no idea how to process this one without examples */
669 return '';
670 }
671
672 function parseParenthesis($read,&$i) {
673 while ($read{$i} != ')') {
674 switch ($read{$i}) {
675 case '"':
676 $this->parseQuote($read,&$i);
677 break;
678 case '{':
679 $this->parseLiteral($read,&$i);
680 break;
681 case '(':
682 $this->parseParenthesis($read,&$i);
683 break;
684 default:
685 break;
686 }
687 $i++;
688 }
689 }
690 }
691
692 class disposition {
693 function disposition($name) {
694 $this->name = $name;
695 $this->properties = array();
696 }
697 }
698
699 ?>