1530f7f164a0759d7af24b260e8e7941a30856ad
[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 $s = '';
43 if (is_array($arr)) {
44 foreach($arr as $arg ) {
45 $result = $this->getAddr_s($arg);
46 if ($result) {
47 $s .= ', ' . $result;
48 }
49 }
50 if ($s) {
51 $s = substr($s,2);
52 }
53 } else {
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 }
68 return $s;
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 {
138 return '';
139 }
140 }
141 }
142
143 class message {
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.
148 **/
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,
153 $is_mdnsent = 0;
154
155 function setEnt($ent) {
156 $this->entity_id= $ent;
157 }
158
159 function setBody($body) {
160 $this->decoded_body = $body;
161 }
162 function addEntity ($msg) {
163 $msg->parent = &$this;
164 $this->entities[] = $msg;
165 }
166
167 function addRFC822Header($read) {
168 $header = new msg_header();
169 $this->header = sqimap_parse_RFC822Header($read,$header);
170 }
171
172 function getEntity($ent) {
173 $cur_ent = $this->entity_id;
174 if ($cur_ent == '' || $cur_ent == '0') {
175 $cur_ent_a = array();
176 } else {
177 $cur_ent_a = explode('.',$this->entity_id);
178 }
179 $ent_a = explode('.',$ent);
180
181 $cnt = count($ent_a);
182 $cnt = $cnt - 1; /* this is how we use the count, so we'll save some time */
183 $msg = $this;
184 for ($i=0;$i<$cnt;$i++) {
185 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) {
186 $msg = $msg->parent;
187 $cur_ent_a = explode('.',$msg->entity_id);
188 $i--;
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)];
192 } else {
193 $msg = $msg->entities[0];
194 }
195 }
196 if ($msg->type0 == 'message') {
197 /*this is a header for a message/rfc822 entity */
198 $msg = $msg->entities[0];
199 }
200 }
201 if ($msg->type0 == 'message') {
202 /*this is a header for a message/rfc822 entity */
203 $msg = $msg->entities[0];
204 }
205
206 if (isset($msg->entities[($ent_a[$cnt])-1])) {
207 $msg = $msg->entities[($ent_a[$cnt]-1)];
208 }
209 return $msg;
210 }
211 /*
212 * Bodystructure parser, a recursive function for generating the
213 * entity-tree with all the mime-parts.
214 *
215 * It follows RFC2060 and stores all the described fields in the
216 * message object.
217 *
218 * Question/Bugs:
219 *
220 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
221 *
222 */
223 function parseStructure($read, $i=0, $message = false) {
224 $arg_no = 0;
225 $arg_a = array();
226 $cnt = strlen($read);
227 for (;$i < $cnt;$i++) {
228 $char = strtoupper($read{$i});
229 switch ($char) {
230 case '(':
231 if ($arg_no == 0 ) {
232 if (!isset($msg)) {
233 $msg = new message();
234 $hdr = new msg_header();
235 $hdr->type0 = 'text';
236 $hdr->type1 = 'plain';
237 $hdr->encoding = 'us-ascii';
238
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};
246 }
247 if ($par_ent == '0') {
248 $ent_no = count($this->entities)+1;
249 if ($ent_no > 0) {
250 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
251 if ($ent) {
252 $ent = $ent . ".$ent_no";
253 } else {
254 $ent = $ent_no;
255 }
256 $msg->entity_id = $ent;
257 } else {
258 $msg->entity_id = $ent_no;
259 }
260 } else {
261 $ent = $this->entity_id . ".$ent_no";
262 $msg->entity_id = $ent;
263 }
264 } else {
265 $msg->entity_id = '0';
266 }
267 } else {
268 $msg->header->type0 = 'multipart';
269 $msg->type0 = 'multipart';
270 while ($read{$i} == '(') {
271 $msg->addEntity($msg->parseStructure($read,&$i));
272 }
273 }
274 } else {
275 switch ($arg_no) {
276 case 1:
277 /* multipart properties */
278 $i++;
279 $arg_a[] = $this->parseProperties($read,&$i);
280 $arg_no++;
281 break;
282 case 2:
283 if (isset($msg->type0) && $msg->type0 == 'multipart') {
284 $i++;
285 $arg_a[]= $msg->parseDisposition($read,&$i);
286 } else {
287 /* properties */
288 $arg_a[] = $msg->parseProperties($read,&$i);
289 }
290 $arg_no++;
291 break;
292 case 7:
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);
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) {
345 $arg_s = strtolower($arg_s); /* type0 and type1 */
346 }
347 $arg_a[] = $arg_s;
348 break;
349 case 'N':
350 /* probably NIL argument */
351 if (strtolower(substr($read,$i,3)) == 'nil') {
352 $arg_a[] = '';
353 $arg_no++;
354 $i = $i+2;
355 }
356 break;
357 case '{':
358 /* process the literal value */
359 $arg_a[] = $msg->parseLiteral($read,&$i);
360 $arg_no++;
361 break;
362 case (is_numeric($read{$i}) ):
363 /* process integers */
364 if ($read{$i} == ' ') {
365 break;
366 }
367 $arg_s = $read{$i};;
368 $i++;
369 while (preg_match('/\d+/',$read{$i})) { /* != ' ') {*/
370 $arg_s .= $read{$i};
371 $i++;
372 }
373 $arg_no++;
374 $arg_a[] = $arg_s;
375 break;
376 case ')':
377 if (isset($msg->type0) && $msg->type0 == 'multipart') {
378 $multipart = true;
379 } else {
380 $multipart = false;
381 }
382 if (!$multipart) {
383 if ($arg_a[0] == 'text' ||
384 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
385 $shifted_args = true;
386 } else {
387 $shifted_args = false;
388 }
389 $hdr->type0 = $arg_a[0];
390 $hdr->type1 = $arg_a[1];
391
392 $msg->type0 = $arg_a[0];
393 $msg->type1 = $arg_a[1];
394
395 $arr = $arg_a[2];
396 if (is_array($arr)) {
397 foreach($arr as $name => $value) {
398 $hdr->{$name} = $value;
399 }
400 }
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];
406 if ($shifted_args) {
407 $hdr->lines = $arg_a[7];
408 if (isset($arg_a[8])) {
409 $hdr->md5 = $arg_a[8];
410 }
411 if (isset($arg_a[9])) {
412 $hdr->disposition = $arg_a[9];
413 }
414 if (isset($arg_a[10])) {
415 $hdr->language = $arg_a[10];
416 }
417 } else {
418 if (isset($arg_a[7])) {
419 $hdr->md5 = $arg_a[7];
420 }
421 if (isset($arg_a[8])) {
422 $hdr->disposition = $arg_a[8];
423 }
424 if (isset($arg_a[9])) {
425 $hdr->language = $arg_a[9];
426 }
427 }
428 $msg->header = $hdr;
429 $arg_no = 0;
430 $i++;
431 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
432 $msg->entity_id++;
433 }
434 return $msg;
435 } else {
436 $hdr->type0 = 'multipart';
437 $hdr->type1 = $arg_a[0];
438
439 $msg->type0 = 'multipart';
440 $msg->type1 = $arg_a[0];
441 if (isset($arg_a[1])) {
442 $arr = $arg_a[1];
443 if (is_array($arr)) {
444 foreach($arr as $name => $value) {
445 $hdr->{$name} = $value;
446 }
447 }
448 }
449 if (isset($arg_a[2])) {
450 $hdr->disposition = $arg_a[2];
451 }
452 if (isset($arg_a[3])) {
453 $hdr->language = $arg_a[3];
454 }
455 $msg->header = $hdr;
456 return $msg;
457 }
458 default:
459 break;
460 } /* switch */
461 } /* for */
462 } /* parsestructure */
463
464 function parseProperties($read, $i) {
465 $properties = array();
466 $arg_s = '';
467 $prop_name = '';
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);
473 }
474 if ($prop_name == '' && $arg_s) {
475 $prop_name = strtolower($arg_s);
476 $properties[$prop_name] = '';
477 $arg_s = '';
478 } elseif ($prop_name != '' && $arg_s != '') {
479 $properties[$prop_name] = $arg_s;
480 $prop_name = '';
481 $arg_s = '';
482 }
483 $i++;
484 }
485 return $properties;
486 }
487
488 function parseEnvelope($read, $i, $hdr) {
489 $arg_no = 0;
490 $arg_a = array();
491 $cnt = strlen($read);
492 while ($i < $cnt && $read{$i} != ')') {
493 $i++;
494 $char = strtoupper($read{$i});
495 switch ($char) {
496 case '"':
497 $arg_a[] = $this->parseQuote($read,&$i);
498 $arg_no++;
499 break;
500 case '{':
501 $arg_a[] = $this->parseLiteral($read,&$i);
502 $arg_no++;
503 break;
504 case 'N':
505 /* probably NIL argument */
506 if (strtolower(substr($read,$i,3)) == 'nil') {
507 $arg_a[] = '';
508 $arg_no++;
509 $i = $i+2;
510 }
511 break;
512 case '(':
513 /* Address structure
514 * With group support.
515 * Note: Group support is useless on SMTP connections
516 * because the protocol doesn't support it
517 */
518 $addr_a = array();
519 $group = '';
520 $a=0;
521 while ($i < $cnt && $read{$i} != ')') {
522 if ($read{$i} == '(') {
523 $addr = $this->parseAddress($read,&$i);
524 if ($addr->host == '' && $addr->mailbox != '') {
525 /* start of group */
526 $group = $addr->mailbox;
527 $group_addr = $addr;
528 $j = $a;
529 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
530 /* end group */
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;
536 $group ='';
537 }
538 } else {
539 $addr->group = $group;
540 $addr_a[] = $addr;
541 }
542 $a++;
543 }
544 $i++;
545 }
546 $arg_a[] = $addr_a;
547 break;
548 default:
549 break;
550 }
551 $i++;
552 }
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)");
561 }
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];
569 /* argument 6: to */
570 $hdr->to = $arg_a[5];
571 /* argument 7: cc */
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];
579 }
580 }
581
582 function parseLiteral($read, $i) {
583 $lit_cnt = '';
584 $i++;
585 while ($read{$i} != '}') {
586 $lit_cnt .= $read{$i};
587 $i++;
588 }
589 $lit_cnt +=2; /* add the { and } characters */
590 $s = '';
591 for ($j = 0; $j < $lit_cnt; $j++) {
592 $i++;
593 $s .= $read{$i};
594 }
595 return $s;
596 }
597
598 function parseQuote($read, $i) {
599 $i++;
600 $s = '';
601 $cnt = strlen($read);
602 while ($i < $cnt && $read{$i} != '"') {
603 if ($read{$i} == '\\') {
604 $i++;
605 }
606 $s .= $read{$i};
607 $i++;
608 }
609 return $s;
610 }
611
612 function parseAddress($read, $i) {
613 $arg_a = array();
614 while ($read{$i} != ')') {
615 $char = strtoupper($read{$i});
616 switch ($char) {
617 case '"':
618 $arg_a[] = $this->parseQuote($read,&$i);
619 break;
620 case '{':
621 $arg_a[] = $this->parseLiteral($read,&$i);
622 break;
623 case 'N':
624 if (strtolower(substr($read,$i,3)) == 'nil') {
625 $arg_a[] = '';
626 $i = $i+2;
627 }
628 default:
629 break;
630 }
631 $i++;
632 }
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];
639 } else {
640 $adr = '';
641 }
642 return $adr;
643 }
644
645 function parseDisposition($read,$i) {
646 $arg_a = array();
647 while ($read{$i} != ')') {
648 switch ($read{$i}) {
649 case '"':
650 $arg_a[] = $this->parseQuote($read,&$i);
651 break;
652 case '{':
653 $arg_a[] = $this->parseLiteral($read,&$i);
654 break;
655 case '(':
656 $arg_a[] = $this->parseProperties($read,&$i);
657 break;
658 default:
659 break;
660 }
661 $i++;
662 }
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];
667 }
668 }
669 if (is_object($disp)) {
670 return $disp;
671 }
672 }
673
674 function parseLanguage($read,&$i) {
675 /* no idea how to process this one without examples */
676 return '';
677 }
678
679 function parseParenthesis($read,&$i) {
680 while ($read{$i} != ')') {
681 switch ($read{$i}) {
682 case '"':
683 $this->parseQuote($read,&$i);
684 break;
685 case '{':
686 $this->parseLiteral($read,&$i);
687 break;
688 case '(':
689 $this->parseParenthesis($read,&$i);
690 break;
691 default:
692 break;
693 }
694 $i++;
695 }
696 }
697 }
698
699 class disposition {
700 function disposition($name) {
701 $this->name = $name;
702 $this->properties = array();
703 }
704 }
705
706 ?>