update for UID support
[squirrelmail.git] / class / mime.class
CommitLineData
8f6b86cc 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
15class 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
111class 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
141class 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->parseEnvelope($read,&$i,&$hdr);
296 $i++;
297 while ($i < $cnt && $read{$i} != '(') {
298 $i++;
299 }
300 $msg->addEntity($msg->parseStructure($read,&$i));
301 }
302 break;
303 case 8:
304 $i++;
305 $arg_a[] = $msg->parseDisposition($read,&$i);
306 $arg_no++;
307 break;
308 case 9:
309 if ($arg_a[0] == 'text' ||
310 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
311 $i++;
312 $arg_a[] = $msg->parseDisposition($read,&$i);
313 } else {
314 $arg_a[] = $msg->parseLanguage($read,&$i);
315 }
316 $arg_no++;
317 break;
318 case 10:
319 if ($arg_a[0] == 'text' ||
320 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
321 $arg_a[] = $msg->parseLanguage($read,&$i);
322 } else {
323 $arg_a[] = ''; /* not yet desribed in rfc2060 */
324 }
325 $arg_no++;
326 break;
327 default:
328 /* unknown argument, skip this part */
329 $msg->parseParenthesis($read,&$i);
330 $arg_a[] = '';
331 $arg_no++;
332 break;
333 } /* switch */
334 }
335 break;
336 case '"':
337 /* inside an entity -> start processing */
338 $debug = substr($read,$i,20);
339 $arg_s = $msg->parseQuote($read,&$i);
340 $arg_no++;
341 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
342 $arg_a[] = $arg_s;
343 break;
344 case 'N':
345 /* probably NIL argument */
346 if (strtolower(substr($read,$i,3)) == 'nil') {
347 $arg_a[] = '';
348 $arg_no++;
349 $i = $i+2;
350 }
351 break;
352 case '{':
353 /* process the literal value */
354 $arg_a[] = $msg->parseLiteral($read,&$i);
355 $arg_no++;
356 break;
357 case (is_numeric($read{$i}) ):
358 /* process integers */
359 if ($read{$i} == ' ') break;
360 $arg_s = $read{$i};;
361 $i++;
362 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
363 $arg_s .= $read{$i};
364 $i++;
365 }
366 $arg_no++;
367 $arg_a[] = $arg_s;
368 break;
369 case ')':
370 if (isset($msg->type0) && $msg->type0 == 'multipart') {
371 $multipart = true;
372 } else {
373 $multipart = false;
374 }
375 if (!$multipart) {
376 if ($arg_a[0] == 'text' ||
377 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
378 $shifted_args = true;
379 } else {
380 $shifted_args = false;
381 }
382 $hdr->type0 = $arg_a[0];
383 $hdr->type1 = $arg_a[1];
384
385 $msg->type0 = $arg_a[0];
386 $msg->type1 = $arg_a[1];
387
388 $arr = $arg_a[2];
389 if (is_array($arr)) {
390 foreach($arr as $name => $value) {
391 $hdr->{$name} = $value;
392 }
393 }
394 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
395 $hdr->description = $arg_a[4];
396 $hdr->encoding = strtolower($arg_a[5]);
397 $hdr->entity_id = $msg->entity_id;
398 $hdr->size = $arg_a[6];
399 if ($shifted_args) {
400 $hdr->lines = $arg_a[7];
401 if (isset($arg_a[8])) {
402 $hdr->md5 = $arg_a[8];
403 }
404 if (isset($arg_a[9])) {
405 $hdr->disposition = $arg_a[9];
406 }
407 if (isset($arg_a[10])) {
408 $hdr->language = $arg_a[10];
409 }
410 } else {
411 if (isset($arg_a[7])) {
412 $hdr->md5 = $arg_a[7];
413 }
414 if (isset($arg_a[8])) {
415 $hdr->disposition = $arg_a[8];
416 }
417 if (isset($arg_a[9])) {
418 $hdr->language = $arg_a[9];
419 }
420 }
421 $msg->header = $hdr;
422 $arg_no = 0;
423 $i++;
424 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
425 $msg->entity_id++;
426 }
427 return $msg;
428 } else {
429 $hdr->type0 = 'multipart';
430 $hdr->type1 = $arg_a[0];
431
432 $msg->type0 = 'multipart';
433 $msg->type1 = $arg_a[0];
434 if (isset($arg_a[1])) {
435 $arr = $arg_a[1];
436 if (is_array($arr)) {
437 foreach($arr as $name => $value) {
438 $hdr->{$name} = $value;
439 }
440 }
441 }
442 if (isset($arg_a[2])) {
443 $hdr->disposition = $arg_a[2];
444 }
445 if (isset($arg_a[3])) {
446 $hdr->language = $arg_a[3];
447 }
448 $msg->header = $hdr;
449 return $msg;
450 }
451 default:
452 break;
453 } /* switch */
454 $i++;
455 } /* while */
456 } /* parsestructure */
457
458 function parseProperties($read, $i) {
459 $properties = array();
460 $arg_s = '';
461 $prop_name = '';
462 while ($read{$i} != ')') {
463 if ($read{$i} == '"') {
464 $arg_s = $this->parseQuote($read,&$i);
465 } else if ($read{$i} == '{') {
466 $arg_s = $this->parseLiteral($read,&$i);
467 }
468 if ($prop_name == '' && $arg_s) {
469 $prop_name = strtolower($arg_s);
470 $properties[$prop_name] = '';
471 $arg_s = '';
472 } elseif ($prop_name != '' && $arg_s != '') {
473 $properties[$prop_name] = $arg_s;
474 $prop_name = '';
475 $arg_s = '';
476 }
477 $i++;
478 }
479 return $properties;
480 }
481
482 function parseEnvelope($read, $i, $hdr) {
483 $arg_no = 0;
484 $arg_a = array();
485 $cnt = strlen($read);
486 while ($i< $cnt && $read{$i} != ')') {
487 $i++;
488 $char = strtoupper($read{$i});
489 switch ($char) {
490 case '"':
491 $arg_a[] = $this->parseQuote($read,&$i);
492 $arg_no++;
493 break;
494 case '{':
495 $arg_a[] = $this->parseLiteral($read,&$i);
496 $arg_no++;
497 break;
498 case 'N':
499 /* probably NIL argument */
500 if (strtolower(substr($read,$i,3)) == 'nil') {
501 $arg_a[] = '';
502 $arg_no++;
503 $i = $i+2;
504 }
505 break;
506 case '(':
507 /* Address structure
508 * With group support.
509 * Note: Group support is useless on SMTP connections
510 * because the protocol doesn't support it
511 */
512 $addr_a = array();
513 $group = '';
514 $a=0;
515 while ($i < $cnt && $read{$i} != ')') {
516 if ($read{$i} == '(') {
517 $addr = $this->parseAddress($read,&$i);
518 if ($addr->host == '' && $addr->mailbox != '') {
519 /* start of group */
520 $group = $addr->mailbox;
521 $group_addr = $addr;
522 $j = $a;
523 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
524 /* end group */
525 if ($a == $j+1) { /* no group members */
526 $group_addr->group = $group;
527 $group_addr->mailbox = '';
528 $group_addr->personal = "$group: Undisclosed recipients;";
529 $addr_a[] = $group_addr;
530 $group ='';
531 }
532 } else {
533 $addr->group = $group;
534 $addr_a[] = $addr;
535 }
536 $a++;
537 }
538 $i++;
539 }
540 $arg_a[] = $addr_a;
541 break;
542 default:
543 break;
544 }
545 $i++;
546 }
547 if (count($arg_a) > 9) {
548 /* argument 1: date */
549 $d = strtr($arg_a[0], array(' ' => ' '));
550 $d = explode(' ', $d);
551 $hdr->date = getTimeStamp($d);
552 /* argument 2: subject */
553 if (!trim($arg_a[1])) {
554 $arg_a[1]= _("(no subject)");
555 }
556 $hdr->subject = $arg_a[1];
557 /* argument 3: from */
558 $hdr->from = $arg_a[2][0];
559 /* argument 4: sender */
560 $hdr->sender = $arg_a[3][0];
561 /* argument 5: reply-to */
562 $hdr->replyto = $arg_a[4][0];
563 /* argument 6: to */
564 $hdr->to = $arg_a[5];
565 /* argument 7: cc */
566 $hdr->cc = $arg_a[6];
567 /* argument 8: bcc */
568 $hdr->bcc = $arg_a[7];
569 /* argument 9: in-reply-to */
570 $hdr->inreplyto = $arg_a[8];
571 /* argument 10: message-id */
572 $hdr->message_id = $arg_a[9];
573 }
574 }
575
576 function parseLiteral($read, $i) {
577 $lit_cnt = '';
578 $i++;
579 while ($read{$i} != '}') {
580 $lit_cnt .= $read{$i};
581 $i++;
582 }
583 $s = '';
584 for ($j = 0; $j < $lit_cnt; $j++) {
585 $i++;
586 $s .= $read{$i};
587 }
588 return $s;
589 }
590
591 function parseQuote($read, $i) {
592 $i++;
593 $s = '';
594 $cnt = strlen($read);
595 while ($i < $cnt && $read{$i} != '"') {
596 if ($read{$i} == '\\') {
597 $i++;
598 }
599 $s .= $read{$i};
600 $i++;
601 }
602 return $s;
603 }
604
605 function parseAddress($read, $i) {
606 $arg_a = array();
607 while ($read{$i} != ')') {
608 $char = strtoupper($read{$i});
609 switch ($char) {
610 case '"':
611 $arg_a[] = $this->parseQuote($read,&$i);
612 break;
613 case '{':
614 $arg_a[] = $this->parseLiteral($read,&$i);
615 break;
616 case 'N':
617 if (strtolower(substr($read,$i,3)) == 'nil') {
618 $arg_a[] = '';
619 $i = $i+2;
620 }
621 default:
622 break;
623 }
624 $i++;
625 }
626 if (count($arg_a) == 4) {
627 $adr = new address_structure();
628 $adr->personal = $arg_a[0];
629 $adr->adl = $arg_a[1];
630 $adr->mailbox = $arg_a[2];
631 $adr->host = $arg_a[3];
632 } else $adr = '';
633 return $adr;
634 }
635
636 function parseDisposition($read,$i) {
637 $arg_a = array();
638 while ($read{$i} != ')') {
639 switch ($read{$i}) {
640 case '"':
641 $arg_a[] = $this->parseQuote($read,&$i);
642 break;
643 case '{':
644 $arg_a[] = $this->parseLiteral($read,&$i);
645 break;
646 case '(':
647 $arg_a[] = $this->parseProperties($read,&$i);
648 break;
649 default:
650 break;
651 }
652 $i++;
653 }
654 if (isset($arg_a[0])) {
655 $disp = new disposition($arg_a[0]);
656 if (isset($arg_a[1])) {
657 $disp->properties = $arg_a[1];
658 }
659 }
660 if (is_object($disp)) return $disp;
661 }
662
663 function parseLanguage($read,&$i) {
664 /* no idea how to process this one without examples */
665 return '';
666 }
667
668 function parseParenthesis($read,&$i) {
669 while ($read{$i} != ')') {
670 switch ($read{$i}) {
671 case '"':
672 $this->parseQuote($read,&$i);
673 break;
674 case '{':
675 $this->parseLiteral($read,&$i);
676 break;
677 case '(':
678 $this->parseParenthesis($read,&$i);
679 break;
680 default:
681 break;
682 }
683 $i++;
684 }
685 }
686}
687
688class disposition {
689 function disposition($name) {
690 $this->name = $name;
691 $this->properties = array();
692 }
693}
694
695?>