minor fix to in displaying attachments
[squirrelmail.git] / class / mime.class.php
CommitLineData
8f6b86cc 1<?php
2
3/**
1a09384c 4 * mime.class
8f6b86cc 5 *
6 * Copyright (c) 2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
27da67ae 9 *
8f6b86cc 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 = '',
319cf0d2 24 $disposition = '', $md5='', $language='',$dnt = '', $xmailer = '';
25
8f6b86cc 26 /*
27 * returns addres_list of supplied argument
28 * arguments: array('to', 'from', ...) or just a string like 'to'.
29 * result: string: address1, addres2, ....
319cf0d2 30 */
31
8f6b86cc 32 function setVar($var, $value) {
33 $this->{$var} = $value;
34 }
319cf0d2 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 */
8f6b86cc 41 function getAddr_s($arr) {
319cf0d2 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 }
8f6b86cc 69 }
319cf0d2 70
8f6b86cc 71 function getAddr_a($arg, $excl_arr=array(), $arr = array()) {
319cf0d2 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 }
8f6b86cc 108 }
109}
110
111class address_structure {
319cf0d2 112 var $personal = '', $adl = '', $mailbox = '', $host = '', $group = '';
8f6b86cc 113
319cf0d2 114 function getAddress($full=true) {
115 if (is_object($this)) {
116 if (isset($this->host) && $this->host !='') {
117 $email = '<'.$this->mailbox.'@'.$this->host.'>';
e02d0fa7 118 } else {
319cf0d2 119 $email = $this->mailbox;
e02d0fa7 120 }
319cf0d2 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 }
8f6b86cc 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='',
319cf0d2 149 $parent = '', $decoded_body='',
150 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
151 $is_mdnsent = 0;
8f6b86cc 152
153 function setEnt($ent) {
154 $this->entity_id= $ent;
155 }
319cf0d2 156
8f6b86cc 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) {
319cf0d2 166 $header = new msg_header();
167 $this->header = sqimap_parse_RFC822Header($read,$header);
8f6b86cc 168 }
169
170 function getEntity($ent) {
319cf0d2 171
8f6b86cc 172 $cur_ent = $this->entity_id;
319cf0d2 173 $msg = $this;
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);
8f6b86cc 182
319cf0d2 183 for ($i=0;$i<$cnt -1;$i++) {
184 if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) {
185 $msg = $msg->parent;
186 $cur_ent_a = explode('.',$msg->entity_id);
187 $i--;
188 } else if (!isset($cur_ent_a[$i])) {
189 if (isset($msg->entities[($ent_a[$i]-1)])) {
190 $msg = $msg->entities[($ent_a[$i]-1)];
191 } else {
192 $msg = $msg->entities[0];
193 }
194 }
08c18186 195 if ($msg->type0 == 'message' && $msg->type1 == 'rfc822') {
319cf0d2 196 /*this is a header for a message/rfc822 entity */
197 $msg = $msg->entities[0];
198 }
199 }
08c18186 200
201 if ($msg->type0 == 'message' && $msg->type1 == 'rfc822') {
319cf0d2 202 /*this is a header for a message/rfc822 entity */
203 $msg = $msg->entities[0];
204 }
8f6b86cc 205
319cf0d2 206 if (isset($msg->entities[($ent_a[$cnt-1])-1])) {
207 $msg = $msg->entities[($ent_a[$cnt-1]-1)];
8f6b86cc 208 }
08c18186 209
319cf0d2 210 return $msg;
8f6b86cc 211 }
319cf0d2 212
213 function getMailbox() {
214 $msg = $this;
215 while (is_object($msg->parent)) {
216 $msg = $msg->parent;
217 }
218 return $msg->mailbox;
219 }
220
8f6b86cc 221 /*
222 * Bodystructure parser, a recursive function for generating the
223 * entity-tree with all the mime-parts.
224 *
225 * It follows RFC2060 and stores all the described fields in the
226 * message object.
227 *
228 * Question/Bugs:
229 *
230 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
231 *
232 */
08c18186 233 function &parseStructure($read, $i=0, $message = false) {
319cf0d2 234 $arg_no = 0;
235 $arg_a = array();
236 $cnt = strlen($read);
237 while ($i < $cnt) {
238 $char = strtoupper($read{$i});
239 switch ($char) {
240 case '(':
241 if ($arg_no == 0 ) {
242 if (!isset($msg)) {
243 $msg = new message();
244 $hdr = new msg_header();
245 $hdr->type0 = 'text';
246 $hdr->type1 = 'plain';
247 $hdr->encoding = 'us-ascii';
248
249 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
250 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
251 } else if (isset($this->entity_id) && $this->entity_id !='') {
252 $ent_no = count($this->entities)+1;
253 $par_ent = substr($this->entity_id,-2);
254 if ($par_ent{0} == '.') {
255 $par_ent = $par_ent{1};
256 }
257 if ($par_ent == '0') {
258 $ent_no = count($this->entities)+1;
259 if ($ent_no > 0) {
260 $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
261 if ($ent) {
262 $ent = $ent . ".$ent_no";
263 } else {
264 $ent = $ent_no;
265 }
266 $msg->entity_id = $ent;
267 } else {
268 $msg->entity_id = $ent_no;
269 }
270 } else {
271 $ent = $this->entity_id . ".$ent_no";
272 $msg->entity_id = $ent;
273 }
274 } else {
275 $msg->entity_id = '0';
276 }
277 } else {
278 $msg->header->type0 = 'multipart';
279 $msg->type0 = 'multipart';
280 while ($read{$i} == '(') {
281 $msg->addEntity($msg->parseStructure($read,&$i));
282 }
283 }
284 } else {
285 switch ($arg_no) {
286 case 1:
287 /* multipart properties */
288 $i++;
289 $arg_a[] = $this->parseProperties($read,&$i);
290 $arg_no++;
291 break;
292 case 2:
293 if (isset($msg->type0) && $msg->type0 == 'multipart') {
294 $i++;
295 $arg_a[]= $msg->parseDisposition($read,&$i);
296 } else { /* properties */
297 /* properties */
298 $arg_a[] = $msg->parseProperties($read,&$i);
299 }
300 $arg_no++;
301 break;
08c18186 302 case 3:
303 if (isset($msg->type0) && $msg->type0 == 'multipart') {
304 $i++;
305 $arg_a[]= $msg->parseLanguage($read,&$i);
306 }
319cf0d2 307 case 7:
308 if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') {
2c761b52 309
319cf0d2 310 $msg->header->type0 = $arg_a[0];
311 $msg->type0 = $arg_a[0];
8f6b86cc 312
319cf0d2 313 $msg->header->type1 = $arg_a[1];
314 $msg->type1 = $arg_a[1];
315
316 $msg->parseEnvelope($read,&$i,&$hdr);
317 $i++;
318 while ($i < $cnt && $read{$i} != '(') {
319 $i++;
320 }
321 $msg->addEntity($msg->parseStructure($read,&$i));
322 }
323 break;
324 case 8:
325 $i++;
326 $arg_a[] = $msg->parseDisposition($read,&$i);
327 $arg_no++;
328 break;
329 case 9:
330 if ($arg_a[0] == 'text' ||
331 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
332 $i++;
333 $arg_a[] = $msg->parseDisposition($read,&$i);
334 } else {
31e0cfba 335 $i++;
319cf0d2 336 $arg_a[] = $msg->parseLanguage($read,&$i);
337 }
338 $arg_no++;
339 break;
340 case 10:
341 if ($arg_a[0] == 'text' ||
342 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
31e0cfba 343 $i++;
319cf0d2 344 $arg_a[] = $msg->parseLanguage($read,&$i);
345 } else {
31e0cfba 346 $msg->parseParenthesis($read,&$i);
319cf0d2 347 $arg_a[] = ''; /* not yet desribed in rfc2060 */
348 }
349 $arg_no++;
350 break;
351 default:
352 /* unknown argument, skip this part */
353 $msg->parseParenthesis($read,&$i);
354 $arg_a[] = '';
355 $arg_no++;
356 break;
357 } /* switch */
358 }
359 break;
360 case '"':
361 /* inside an entity -> start processing */
362 $debug = substr($read,$i,20);
363 $arg_s = $msg->parseQuote($read,&$i);
364 $arg_no++;
365 if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
366 $arg_a[] = $arg_s;
367 break;
368 case 'N':
369 /* probably NIL argument */
08c18186 370 if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
371 strtoupper(substr($read,$i,4)) == 'NIL)') {
319cf0d2 372 $arg_a[] = '';
373 $arg_no++;
374 $i = $i+2;
375 }
376 break;
377 case '{':
378 /* process the literal value */
379 $arg_a[] = $msg->parseLiteral($read,&$i);
380 $arg_no++;
381 break;
382 case (is_numeric($read{$i}) ):
383 /* process integers */
384 if ($read{$i} == ' ') break;
385 $arg_s = $read{$i};;
386 $i++;
387 while (preg_match('/\d+/',$read{$i})) { // != ' ') {
388 $arg_s .= $read{$i};
389 $i++;
390 }
391 $arg_no++;
392 $arg_a[] = $arg_s;
393 break;
394 case ')':
395 if (isset($msg->type0) && $msg->type0 == 'multipart') {
396 $multipart = true;
397 } else {
398 $multipart = false;
399 }
400 if (!$multipart) {
401 if ($arg_a[0] == 'text' ||
402 ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) {
403 $shifted_args = true;
404 } else {
405 $shifted_args = false;
406 }
407 $hdr->type0 = $arg_a[0];
408 $hdr->type1 = $arg_a[1];
8f6b86cc 409
319cf0d2 410 $msg->type0 = $arg_a[0];
411 $msg->type1 = $arg_a[1];
8f6b86cc 412
319cf0d2 413 $arr = $arg_a[2];
414 if (is_array($arr)) {
415 foreach($arr as $name => $value) {
416 $hdr->{$name} = $value;
417 }
418 }
419 $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
420 $hdr->description = $arg_a[4];
421 $hdr->encoding = strtolower($arg_a[5]);
422 $hdr->entity_id = $msg->entity_id;
423 $hdr->size = $arg_a[6];
424 if ($shifted_args) {
425 $hdr->lines = $arg_a[7];
426 if (isset($arg_a[8])) {
427 $hdr->md5 = $arg_a[8];
428 }
429 if (isset($arg_a[9])) {
430 $hdr->disposition = $arg_a[9];
431 }
432 if (isset($arg_a[10])) {
433 $hdr->language = $arg_a[10];
434 }
435 } else {
436 if (isset($arg_a[7])) {
437 $hdr->md5 = $arg_a[7];
438 }
439 if (isset($arg_a[8])) {
440 $hdr->disposition = $arg_a[8];
441 }
442 if (isset($arg_a[9])) {
443 $hdr->language = $arg_a[9];
444 }
445 }
446 $msg->header = $hdr;
447 $arg_no = 0;
448 $i++;
449 if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') {
450 $msg->entity_id++;
451 }
452 return $msg;
453 } else {
454 $hdr->type0 = 'multipart';
455 $hdr->type1 = $arg_a[0];
0fee68d1 456
319cf0d2 457 $msg->type0 = 'multipart';
458 $msg->type1 = $arg_a[0];
459 if (isset($arg_a[1])) {
460 $arr = $arg_a[1];
461 if (is_array($arr)) {
462 foreach($arr as $name => $value) {
463 $hdr->{$name} = $value;
464 }
465 }
466 }
467 if (isset($arg_a[2])) {
468 $hdr->disposition = $arg_a[2];
469 }
470 if (isset($arg_a[3])) {
471 $hdr->language = $arg_a[3];
472 }
473 $msg->header = $hdr;
474 return $msg;
475 }
476 default:
477 break;
478 } /* switch */
479 $i++;
480 } /* while */
481 } /* parsestructure */
482
8f6b86cc 483 function parseProperties($read, $i) {
319cf0d2 484 $properties = array();
485 $arg_s = '';
486 $prop_name = '';
487 while ($read{$i} != ')') {
488 if ($read{$i} == '"') {
489 $arg_s = $this->parseQuote($read,&$i);
490 } else if ($read{$i} == '{') {
491 $arg_s = $this->parseLiteral($read,&$i);
492 }
493 if ($prop_name == '' && $arg_s) {
494 $prop_name = strtolower($arg_s);
495 $properties[$prop_name] = '';
496 $arg_s = '';
497 } elseif ($prop_name != '' && $arg_s != '') {
498 $properties[$prop_name] = $arg_s;
499 $prop_name = '';
500 $arg_s = '';
501 }
502 $i++;
503 }
504 return $properties;
8f6b86cc 505 }
506
507 function parseEnvelope($read, $i, $hdr) {
508 $arg_no = 0;
319cf0d2 509 $arg_a = array();
510 $cnt = strlen($read);
511 while ($i< $cnt && $read{$i} != ')') {
512 $i++;
513 $char = strtoupper($read{$i});
514 switch ($char) {
515 case '"':
516 $arg_a[] = $this->parseQuote($read,&$i);
517 $arg_no++;
518 break;
519 case '{':
520 $arg_a[] = $this->parseLiteral($read,&$i);
521 $arg_no++;
522 break;
523 case 'N':
524 /* probably NIL argument */
08c18186 525 if (strtoupper(substr($read,$i,3)) == 'NIL') {
319cf0d2 526 $arg_a[] = '';
527 $arg_no++;
528 $i = $i+2;
529 }
530 break;
531 case '(':
532 /* Address structure
533 * With group support.
534 * Note: Group support is useless on SMTP connections
535 * because the protocol doesn't support it
536 */
537 $addr_a = array();
538 $group = '';
539 $a=0;
540 while ($i < $cnt && $read{$i} != ')') {
541 if ($read{$i} == '(') {
542 $addr = $this->parseAddress($read,&$i);
543 if ($addr->host == '' && $addr->mailbox != '') {
544 /* start of group */
545 $group = $addr->mailbox;
546 $group_addr = $addr;
547 $j = $a;
548 } elseif ($group && $addr->host == '' && $addr->mailbox == '') {
549 /* end group */
550 if ($a == $j+1) { /* no group members */
551 $group_addr->group = $group;
552 $group_addr->mailbox = '';
553 $group_addr->personal = "$group: Undisclosed recipients;";
554 $addr_a[] = $group_addr;
555 $group ='';
556 }
557 } else {
558 $addr->group = $group;
559 $addr_a[] = $addr;
560 }
561 $a++;
562 }
563 $i++;
564 }
565 $arg_a[] = $addr_a;
566 break;
567 default:
568 break;
569 }
570 $i++;
571 }
572 if (count($arg_a) > 9) {
573 /* argument 1: date */
8f6b86cc 574 $d = strtr($arg_a[0], array(' ' => ' '));
575 $d = explode(' ', $d);
576 $hdr->date = getTimeStamp($d);
319cf0d2 577 /* argument 2: subject */
578 if (!trim($arg_a[1])) {
579 $arg_a[1]= _("(no subject)");
580 }
581 $hdr->subject = $arg_a[1];
582 /* argument 3: from */
583 $hdr->from = $arg_a[2][0];
584 /* argument 4: sender */
585 $hdr->sender = $arg_a[3][0];
586 /* argument 5: reply-to */
587 $hdr->replyto = $arg_a[4][0];
588 /* argument 6: to */
589 $hdr->to = $arg_a[5];
590 /* argument 7: cc */
591 $hdr->cc = $arg_a[6];
592 /* argument 8: bcc */
593 $hdr->bcc = $arg_a[7];
594 /* argument 9: in-reply-to */
595 $hdr->inreplyto = $arg_a[8];
596 /* argument 10: message-id */
597 $hdr->message_id = $arg_a[9];
08c18186 598 }
8f6b86cc 599 }
319cf0d2 600
8f6b86cc 601 function parseLiteral($read, $i) {
319cf0d2 602 $lit_cnt = '';
603 $i++;
604 while ($read{$i} != '}') {
605 $lit_cnt .= $read{$i};
606 $i++;
607 }
608 $lit_cnt +=2; /* add the { and } characters */
609 $s = '';
610 for ($j = 0; $j < $lit_cnt; $j++) {
611 $i++;
612 $s .= $read{$i};
613 }
614 return $s;
8f6b86cc 615 }
319cf0d2 616
8f6b86cc 617 function parseQuote($read, $i) {
319cf0d2 618 $i++;
619 $s = '';
08c18186 620 while ($read{$i} != '"') {
319cf0d2 621 if ($read{$i} == '\\') {
622 $i++;
623 }
624 $s .= $read{$i};
625 $i++;
626 }
8f6b86cc 627 return $s;
628 }
319cf0d2 629
8f6b86cc 630 function parseAddress($read, $i) {
319cf0d2 631 $arg_a = array();
08c18186 632 while ($read{$i} != ')' ) { //&& $i < count($read)) {
319cf0d2 633 $char = strtoupper($read{$i});
634 switch ($char) {
635 case '"':
636 $arg_a[] = $this->parseQuote($read,&$i);
637 break;
638 case '{':
639 $arg_a[] = $this->parseLiteral($read,&$i);
640 break;
641 case 'N':
642 if (strtolower(substr($read,$i,3)) == 'nil') {
643 $arg_a[] = '';
644 $i = $i+2;
645 }
08c18186 646 break;
319cf0d2 647 default:
648 break;
649 }
650 $i++;
651 }
652 if (count($arg_a) == 4) {
653 $adr = new address_structure();
654 $adr->personal = $arg_a[0];
655 $adr->adl = $arg_a[1];
656 $adr->mailbox = $arg_a[2];
657 $adr->host = $arg_a[3];
08c18186 658 } else {
659 $adr = '';
660 }
319cf0d2 661 return $adr;
8f6b86cc 662 }
319cf0d2 663
08c18186 664 function parseDisposition($read,&$i) {
8f6b86cc 665 $arg_a = array();
666 while ($read{$i} != ')') {
319cf0d2 667 switch ($read{$i}) {
668 case '"':
669 $arg_a[] = $this->parseQuote($read,&$i);
670 break;
671 case '{':
672 $arg_a[] = $this->parseLiteral($read,&$i);
673 break;
674 case '(':
675 $arg_a[] = $this->parseProperties($read,&$i);
676 break;
677 default:
678 break;
679 }
680 $i++;
e02d0fa7 681 }
319cf0d2 682 if (isset($arg_a[0])) {
683 $disp = new disposition($arg_a[0]);
684 if (isset($arg_a[1])) {
685 $disp->properties = $arg_a[1];
686 }
687 }
08c18186 688 if (is_object($disp)) {
689 return $disp;
690 }
8f6b86cc 691 }
319cf0d2 692
8f6b86cc 693 function parseLanguage($read,&$i) {
08c18186 694 /* no idea how to process this one without examples */
31e0cfba 695 $arg_a = array();
08c18186 696 while ($read{$i} != ')') {
697 switch ($read{$i}) {
698 case '"':
31e0cfba 699 $arg_a[] = $this->parseQuote($read,&$i);
08c18186 700 break;
701 case '{':
31e0cfba 702 $arg_a[] = $this->parseLiteral($read,&$i);
08c18186 703 break;
704 case '(':
31e0cfba 705 $arg_a[] = $this->parseProperties($read,&$i);
08c18186 706 break;
707 default:
708 break;
709 }
710 $i++;
711 }
31e0cfba 712 if (isset($arg_a[0])) {
713 $lang = new language($arg_a[0]);
714 if (isset($arg_a[1])) {
715 $lang->properties = $arg_a[1];
716 }
717 }
718 if (is_object($lang)) {
719 return $lang;
720 } else {
721 return '';
722 }
8f6b86cc 723 }
319cf0d2 724
8f6b86cc 725 function parseParenthesis($read,&$i) {
726 while ($read{$i} != ')') {
319cf0d2 727 switch ($read{$i}) {
728 case '"':
729 $this->parseQuote($read,&$i);
730 break;
731 case '{':
732 $this->parseLiteral($read,&$i);
733 break;
734 case '(':
735 $this->parseParenthesis($read,&$i);
736 break;
737 default:
738 break;
739 }
740 $i++;
741 }
742 }
743
744 function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html')) {
319cf0d2 745 $found = false;
746 $type = $this->type0.'/'.$this->type1;
747 if ( $type == 'multipart/alternative') {
748 $msg = $this->findAlternativeEntity($alt_order);
749 if (count($msg->entities) == 0) {
750 $entity[] = $msg->entity_id;
751 } else {
752 $msg->findDisplayEntity(&$entity, $alt_order);
753 }
754 $found = true;
755 } else if ( $type == 'multipart/related') {
756 $msgs = $this->findRelatedEntity();
757 for ($i = 0; $i < count($msgs); $i++) {
758 $msg = $msgs[$i];
759 if (count($msg->entities) == 0) {
760 $entity[] = $msg->entity_id;
761 } else {
762 $msg->findDisplayEntity(&$entity,$alt_order);
763 }
764 $found = true;
765 }
766 } else if ( $this->type0 == 'text' &&
767 ( $this->type1 == 'plain' ||
08c18186 768 $this->type1 == 'html' ||
769 $this->type1 == 'message') &&
319cf0d2 770 isset($this->entity_id) ) {
771 if (count($this->entities) == 0) {
772 if (!$this->header->disposition->name == 'attachment') {
773 $entity[] = $this->entity_id;
774 }
775 }
776 }
777 $i = 0;
778 while ( isset($this->entities[$i]) && !$found &&
779 !($this->entities[$i]->header->disposition->name
780 == 'attachment') &&
781 !($this->entities[$i]->type0 == 'message' &&
782 $this->entities[$i]->type1 == 'rfc822' )
783 )
784 {
785 $this->entities[$i]->findDisplayEntity(&$entity, $alt_order);
786 $i++;
787 }
788
789 if ( !isset($entity[0]) ) {
790 $entity[]="";
0fee68d1 791 }
319cf0d2 792 return( $entity );
8f6b86cc 793 }
319cf0d2 794
795 function findAlternativeEntity ($alt_order) {
796 /* if we are dealing with alternative parts then we choose the best
797 * viewable message supported by SM.
798 */
799 $best_view = 0;
800 $ent_id = 0;
801 $k = 0;
802 for ($i = 0; $i < count($this->entities); $i ++) {
803 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
804 if ($type == 'multipart/related') {
805 $type = $this->entities[$i]->header->type;
806 }
807 for ($j = $k; $j < count($alt_order); $j++) {
808 if ($alt_order[$j] == $type && $j > $best_view) {
809 $best_view = $j;
810 $ent_id = $i;
811 $k = $j;
812 }
813 }
814 }
815 return $this->entities[$ent_id];
816 }
817
818 function findRelatedEntity () {
819 $msgs = array();
820 for ($i = 0; $i < count($this->entities); $i ++) {
821 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
822 if ($this->header->type == $type) {
823 $msgs[] = $this->entities[$i];
824 }
825 }
826 return $msgs;
827 }
828
829 function getAttachments($exclude_id=array(), $result = array()) {
830 if ($this->type0 == 'message' && $this->type1 == 'rfc822') {
831 $this = $this->entities[0];
832 }
833 if (count($this->entities)) {
834 foreach ($this->entities as $entity) {
835 $exclude = false;
836 foreach ($exclude_id as $excl) {
837 if ($entity->entity_id == $excl) {
838 $exclude = true;
839 }
840 }
841 if (!$exclude) {
1a09384c 842 if ($entity->type0 == 'multipart' &&
843 $entity->type1 != 'related') {
319cf0d2 844 $result = $entity->getAttachments($exclude_id, $result);
845 } else if ($entity->type0 != 'multipart') {
846 $result[] = $entity;
847 }
848 }
849 }
850 } else {
851 $exclude = false;
852 foreach ($exclude_id as $excl) {
853 if ($this->entity_id == $excl) {
854 $exclude = true;
855 }
856 }
857 if (!$exclude) {
858 $result[] = $this;
859 }
860 }
861 return $result;
862 }
863
8f6b86cc 864}
865
866class disposition {
867 function disposition($name) {
868 $this->name = $name;
869 $this->properties = array();
870 }
871}
872
31e0cfba 873class language {
874 function language($name) {
875 $this->name = $name;
876 $this->properties = array();
877 }
878}
879
8f6b86cc 880?>