fix for getEntity
[squirrelmail.git] / class / mime / Message.class.php
1 <?php
2
3 /**
4 * Message.class.php
5 *
6 * Copyright (c) 2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions needed to handle mime messages.
10 *
11 * $Id$
12 */
13
14 class Message {
15 /** message is the object that contains messages. It is a recursive
16 object in that through the $entities variable, it can contain
17 more objects of type message. See documentation in mime.txt for
18 a better description of how this works.
19 **/
20 var $rfc822_header = '',
21 $mime_header = '',
22 $flags = '',
23 $type0='',
24 $type1='',
25 $entities = array(),
26 $parent_ent, $entity,
27 $parent = '', $decoded_body='',
28 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
29 $is_mdnsent = 0,
30 $body_part = '',
31 $offset = 0, /* for fetching body parts out of raw messages */
32 $length = 0, /* for fetching body parts out of raw messages */
33 $att_local_name = ''; /* location where the tempory attachment
34 is stored. For future usage in smtp.php */
35
36 function setEnt($ent) {
37 $this->entity_id= $ent;
38 }
39
40 function addEntity ($msg) {
41 $msg->parent = &$this;
42 $this->entities[] = $msg;
43 }
44
45 function getFilename() {
46 $filename = '';
47 $filename = $this->header->getParameter('filename');
48 if (!$filename) {
49 $filename = $this->header->getParameter('name');
50 }
51
52 if (!$filename) {
53 $filename = 'untitled-'.$this->entity_id;
54 }
55 return $filename;
56 }
57
58
59 function addRFC822Header($read) {
60 $header = new Rfc822Header();
61 $this->rfc822_header = $header->parseHeader($read);
62 }
63
64 function getEntity($ent) {
65 $cur_ent = $this->entity_id;
66 $msg = $this;
67 if (($cur_ent == '') || ($cur_ent == '0')) {
68 $cur_ent_a = array();
69 } else {
70 $cur_ent_a = explode('.', $this->entity_id);
71 }
72 $ent_a = explode('.', $ent);
73
74 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++$i) {
75 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
76 $msg = $msg->parent;
77 $cur_ent_a = explode('.', $msg->entity_id);
78 --$i;
79 } else if (!isset($cur_ent_a[$i])) {
80 if (isset($msg->entities[($ent_a[$i]-1)])) {
81 $msg = $msg->entities[($ent_a[$i]-1)];
82 } else {
83 $msg = $msg->entities[0];
84 }
85 }
86 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
87 /*this is a header for a message/rfc822 entity */
88 $msg = $msg->entities[0];
89 }
90 }
91
92 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
93 /*this is a header for a message/rfc822 entity */
94 $msg = $msg->entities[0];
95 }
96
97 if (isset($msg->entities[($ent_a[$entCount])-1])) {
98 if (is_object($msg->entities[($ent_a[$entCount])-1])) {
99 $msg = $msg->entities[($ent_a[$entCount]-1)];
100 }
101 }
102
103 return $msg;
104 }
105
106 function setBody($s) {
107 $this->body_part = $s;
108 }
109
110 function clean_up() {
111 $msg = $this;
112 $msg->body_part = '';
113
114 foreach ($msg->entities as $m) {
115 $m->clean_up();
116 }
117 }
118
119 function getMailbox() {
120 $msg = $this;
121 while (is_object($msg->parent)) {
122 $msg = $msg->parent;
123 }
124 return $msg->mailbox;
125 }
126
127 function calcEntity($msg) {
128 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
129 $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
130 } else if (isset($this->entity_id) && ($this->entity_id != '')) {
131 $entCount = count($this->entities) + 1;
132 $par_ent = substr($this->entity_id, -2);
133 if ($par_ent{0} == '.') {
134 $par_ent = $par_ent{1};
135 }
136 if ($par_ent == '0') {
137 if ($entCount > 0) {
138 $ent = substr($this->entity_id, 0, strrpos($this->entity_id, '.'));
139 $ent = ($ent ? $ent . '.' : '') . $entCount;
140 $msg->entity_id = $ent;
141 } else {
142 $msg->entity_id = $entCount;
143 }
144 } else {
145 $ent = $this->entity_id . '.' . $entCount;
146 $msg->entity_id = $ent;
147 }
148 } else {
149 $msg->entity_id = '0';
150 }
151
152 return $msg->entity_id;
153 }
154
155
156 /*
157 * Bodystructure parser, a recursive function for generating the
158 * entity-tree with all the mime-parts.
159 *
160 * It follows RFC2060 and stores all the described fields in the
161 * message object.
162 *
163 * Question/Bugs:
164 *
165 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
166 *
167 */
168 function parseStructure($read, $i = 0) {
169 $arg_no = 0;
170 $arg_a = array();
171 for ($cnt = strlen($read); $i < $cnt; ++$i) {
172 $char = strtoupper($read{$i});
173 switch ($char) {
174 case '(':
175 switch($arg_no) {
176 case 0:
177 if (!isset($msg)) {
178 $msg = new Message();
179 $hdr = new MessageHeader();
180 $hdr->type0 = 'text';
181 $hdr->type1 = 'plain';
182 $hdr->encoding = 'us-ascii';
183 $msg->entity_id = $this->calcEntity($msg);
184 } else {
185 $msg->header->type0 = 'multipart';
186 $msg->type0 = 'multipart';
187 while ($read{$i} == '(') {
188 $res = $msg->parseStructure($read, $i);
189 $i = $res[1];
190 $msg->addEntity($res[0]);
191 }
192 }
193 break;
194 case 1:
195 /* multipart properties */
196 ++$i;
197 $res = $this->parseProperties($read, $i);
198 $arg_a[] = $res[0];
199 $i = $res[1];
200 ++$arg_no;
201 break;
202 case 2:
203 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
204 ++$i;
205 $res = $msg->parseDisposition($read, $i);
206 } else { /* properties */
207 $res = $msg->parseProperties($read, $i);
208 }
209 $arg_a[] = $res[0];
210 $i = $res[1];
211 ++$arg_no;
212 break;
213 case 3:
214 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
215 ++$i;
216 $res= $msg->parseLanguage($read, $i);
217 $arg_a[] = $res[0];
218 $i = $res[1];
219 }
220 case 7:
221 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
222 $msg->header->type0 = $arg_a[0];
223 $msg->header->type1 = $arg_a[1];
224 $msg->type0 = $arg_a[0];
225 $msg->type1 = $arg_a[1];
226 $rfc822_hdr = new Rfc822Header();
227 $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
228 $msg->rfc822_header = $res[0];
229 $i = $res[1] + 1;
230 while (($i < $cnt) && ($read{$i} != '(')) {
231 ++$i;
232 }
233 $res = $msg->parseStructure($read, $i);
234 $i = $res[1];
235 $msg->addEntity($res[0]);
236 }
237 break;
238 case 8:
239 ++$i;
240 $res = $msg->parseDisposition($read, $i);
241 $arg_a[] = $res[0];
242 $i = $res[1];
243 ++$arg_no;
244 break;
245 case 9:
246 ++$i;
247 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
248 $res = $msg->parseDisposition($read, $i);
249 } else {
250 $res = $msg->parseLanguage($read, $i);
251 }
252 $arg_a[] = $res[0];
253 $i = $res[1];
254 ++$arg_no;
255 break;
256 case 10:
257 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
258 ++$i;
259 $res = $msg->parseLanguage($read, $i);
260 $arg_a[] = $res[0];
261 $i = $res[1];
262 } else {
263 $i = $msg->parseParenthesis($read, $i);
264 $arg_a[] = ''; /* not yet described in rfc2060 */
265 }
266 ++$arg_no;
267 break;
268 default:
269 /* unknown argument, skip this part */
270 $i = $msg->parseParenthesis($read, $i);
271 $arg_a[] = '';
272 ++$arg_no;
273 break;
274 } /* switch */
275 break;
276 case '"':
277 /* inside an entity -> start processing */
278 $debug = substr($read, $i, 20);
279 $res = $msg->parseQuote($read, $i);
280 $arg_s = $res[0];
281 $i = $res[1];
282 ++$arg_no;
283 if ($arg_no < 3) {
284 $arg_s = strtolower($arg_s); /* type0 and type1 */
285 }
286 $arg_a[] = $arg_s;
287 break;
288 case 'n':
289 case 'N':
290 /* probably NIL argument */
291 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
292 $arg_a[] = '';
293 ++$arg_no;
294 $i += 2;
295 }
296 break;
297 case '{':
298 /* process the literal value */
299 $res = $msg->parseLiteral($read, $i);
300 $arg_s = $res[0];
301 $i = $res[1];
302 ++$arg_no;
303 break;
304 case is_numeric($read{$i}):
305 /* process integers */
306 if ($read{$i} == ' ') { break; }
307 $arg_s = $read{$i};;
308 for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) {
309 $arg_s .= $read{$i};
310 }
311 ++$arg_no;
312 $arg_a[] = $arg_s;
313 break;
314 case ')':
315 $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
316 if (!$multipart) {
317 $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
318 $hdr->type0 = $arg_a[0];
319 $hdr->type1 = $arg_a[1];
320
321 $msg->type0 = $arg_a[0];
322 $msg->type1 = $arg_a[1];
323 $arr = $arg_a[2];
324 if (is_array($arr)) {
325 $hdr->parameters = $arg_a[2];
326 }
327 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
328 $hdr->description = $arg_a[4];
329 $hdr->encoding = strtolower($arg_a[5]);
330 $hdr->entity_id = $msg->entity_id;
331 $hdr->size = $arg_a[6];
332 if ($shifted_args) {
333 $hdr->lines = $arg_a[7];
334 $s = 1;
335 } else {
336 $s = 0;
337 }
338 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
339 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
340 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
341 $msg->header = $hdr;
342 if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) {
343 $msg->entity_id = $this->entity_id . '.1';
344 }
345 } else {
346 $hdr->type0 = 'multipart';
347 $hdr->type1 = $arg_a[0];
348 $msg->type0 = 'multipart';
349 $msg->type1 = $arg_a[0];
350 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
351 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
352 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
353 $msg->header = $hdr;
354 }
355 ++$i;
356 return (array($msg, $i));
357 default: break;
358 } /* switch */
359
360 } /* for */
361 } /* parsestructure */
362
363 function parseProperties($read, $i) {
364 $properties = array();
365 $prop_name = '';
366
367 for (; $read{$i} != ')'; ++$i) {
368 $arg_s = '';
369 if ($read{$i} == '"') {
370 $res = $this->parseQuote($read, $i);
371 $arg_s = $res[0];
372 $i = $res[1];
373 } else if ($read{$i} == '{') {
374 $res = $this->parseLiteral($read, $i);
375 $arg_s = $res[0];
376 $i = $res[1];
377 }
378
379 if ($arg_s != '') {
380 if ($prop_name == '') {
381 $prop_name = strtolower($arg_s);
382 $properties[$prop_name] = '';
383 } else if ($prop_name != '') {
384 $properties[$prop_name] = $arg_s;
385 $prop_name = '';
386 }
387 }
388 }
389 return array($properties, $i);
390 }
391
392 function parseEnvelope($read, $i, $hdr) {
393 $arg_no = 0;
394 $arg_a = array();
395
396 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
397 ++$i;
398 $char = strtoupper($read{$i});
399 switch ($char) {
400 case '"':
401 $res = $this->parseQuote($read, $i);
402 $arg_a[] = $res[0];
403 $i = $res[1];
404 ++$arg_no;
405 break;
406 case '{':
407 $res = $this->parseLiteral($read, $i);
408 $arg_a[] = $res[0];
409 $i = $res[1];
410 ++$arg_no;
411 break;
412 case 'N':
413 /* probably NIL argument */
414 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
415 $arg_a[] = '';
416 ++$arg_no;
417 $i += 2;
418 }
419 break;
420 case '(':
421 /* Address structure (with group support)
422 * Note: Group support is useless on SMTP connections
423 * because the protocol doesn't support it
424 */
425 $addr_a = array();
426 $group = '';
427 $a=0;
428 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
429 if ($read{$i} == '(') {
430 $res = $this->parseAddress($read, $i);
431 $addr = $res[0];
432 $i = $res[1];
433 if (($addr->host == '') && ($addr->mailbox != '')) {
434 /* start of group */
435 $group = $addr->mailbox;
436 $group_addr = $addr;
437 $j = $a;
438 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
439 /* end group */
440 if ($a == ($j+1)) { /* no group members */
441 $group_addr->group = $group;
442 $group_addr->mailbox = '';
443 $group_addr->personal = "$group: Undisclosed recipients;";
444 $addr_a[] = $group_addr;
445 $group ='';
446 }
447 } else {
448 $addr->group = $group;
449 $addr_a[] = $addr;
450 }
451 ++$a;
452 }
453 }
454 $arg_a[] = $addr_a;
455 break;
456 default: break;
457 }
458 }
459
460 if (count($arg_a) > 9) {
461 /* argument 1: date */
462 $d = strtr($arg_a[0], array(' ' => ' '));
463 $d = explode(' ', $d);
464 $hdr->date = getTimeStamp($d);
465
466 /* argument 2: subject */
467 $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]);
468 $hdr->subject = $arg_a[1];
469
470 $hdr->from = $arg_a[2][0]; /* argument 3: from */
471 $hdr->sender = $arg_a[3][0]; /* argument 4: sender */
472 $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */
473 $hdr->to = $arg_a[5]; /* argument 6: to */
474 $hdr->cc = $arg_a[6]; /* argument 7: cc */
475 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
476 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
477 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
478 }
479 return (array($hdr, $i));
480 }
481
482 function parseLiteral($read, $i) {
483 $lit_cnt = '';
484 for (++$i; $read{$i} != '}'; ++$i) {
485 $lit_cnt .= $read{$i};
486 }
487
488 $lit_cnt +=2; /* add the { and } characters */
489 $s = '';
490 for ($j = 0; $j < $lit_cnt; ++$j) {
491 $s .= $read{++$i};
492 }
493 return (array($s, $i));
494 }
495
496 function parseQuote($read, $i) {
497 $s = '';
498 for (++$i; $read{$i} != '"'; ++$i) {
499 if ($read{$i} == '\\') {
500 ++$i;
501 }
502 $s .= $read{$i};
503 }
504 return (array($s, $i));
505 }
506
507 function parseAddress($read, $i) {
508 $arg_a = array();
509
510 for (; $read{$i} != ')'; ++$i) {
511 $char = strtoupper($read{$i});
512 switch ($char) {
513 case '"':
514 case '{':
515 $res = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
516 $arg_a[] = $res[0];
517 $i = $res[1];
518 break;
519 case 'n':
520 case 'N':
521 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
522 $arg_a[] = '';
523 $i += 2;
524 }
525 break;
526 default: break;
527 }
528 }
529
530 if (count($arg_a) == 4) {
531 $adr = new AddressStructure();
532 $adr->personal = $arg_a[0];
533 $adr->adl = $arg_a[1];
534 $adr->mailbox = $arg_a[2];
535 $adr->host = $arg_a[3];
536 } else {
537 $adr = '';
538 }
539 return (array($adr, $i));
540 }
541
542 function parseDisposition($read, $i) {
543 $arg_a = array();
544
545 for (; $read{$i} != ')'; ++$i) {
546 switch ($read{$i}) {
547 case '"':
548 case '{':
549 case '(':
550 switch ($read{$i}) {
551 case '"': $res = $this->parseQuote($read, $i); break;
552 case '{': $res = $this->parseLiteral($read, $i); break;
553 case '(': $res = $this->parseProperties($read, $i); break;
554 }
555 $arg_a[] = $res[0];
556 $i = $res[1];
557 break;
558 default: break;
559 }
560 }
561
562 if (isset($arg_a[0])) {
563 $disp = new Disposition($arg_a[0]);
564 if (isset($arg_a[1])) {
565 $disp->properties = $arg_a[1];
566 }
567 }
568
569 return (is_object($disp) ? array($disp, $i) : array('', $i));
570 }
571
572 function parseLanguage($read, $i) {
573 /* no idea how to process this one without examples */
574 $arg_a = array();
575
576 for (; $read{$i} != ')'; ++$i) {
577 switch ($read{$i}) {
578 case '"':
579 case '{':
580 case '(':
581 switch ($read{$i}) {
582 case '"': $res = $this->parseQuote($read, $i); break;
583 case '{': $res = $this->parseLiteral($read, $i); break;
584 case '(': $res = $this->parseProperties($read, $i); break;
585 }
586 $arg_a[] = $res[0];
587 $i = $res[1];
588 break;
589 default: break;
590 }
591 }
592
593 if (isset($arg_a[0])) {
594 $lang = new Language($arg_a[0]);
595 if (isset($arg_a[1])) {
596 $lang->properties = $arg_a[1];
597 }
598 }
599
600 return (is_object($lang) ? array($lang, $i) : array('', $i));
601 }
602
603 function parseParenthesis($read, $i) {
604 for (; $read{$i} != ')'; ++$i) {
605 switch ($read{$i}) {
606 case '"':
607 case '{':
608 case '(':
609 switch ($read{$i}) {
610 case '"': $res = $this->parseQuote($read, $i); break;
611 case '{': $res = $this->parseLiteral($read, $i); break;
612 case '(': $res = $this->parseProperties($read, $i); break;
613 }
614 $i = $res[1];
615 break;
616 default: break;
617 }
618 }
619 return $i;
620 }
621
622 /* Function to fill the message structure in case the */
623 /* bodystructure is not available NOT FINISHED YET */
624 function parseMessage($read, $type0, $type1) {
625 switch ($type0) {
626 case 'message':
627 $rfc822_header = true;
628 $mime_header = false;
629 break;
630 case 'multipart':
631 $rfc822_header = false;
632 $mime_header = true;
633 break;
634 default: return $read;
635 }
636
637 for ($i = 1; $i < $count; ++$i) {
638 $line = trim($body[$i]);
639 if (($mime_header || $rfc822_header) &&
640 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
641 $bnd = $reg[1];
642 $bndreg = $bnd;
643 $bndreg = str_replace("\\", "\\\\", $bndreg);
644 $bndreg = str_replace("?", "\\?", $bndreg);
645 $bndreg = str_replace("+", "\\+", $bndreg);
646 $bndreg = str_replace(".", "\\.", $bndreg);
647 $bndreg = str_replace("/", "\\/", $bndreg);
648 $bndreg = str_replace("-", "\\-", $bndreg);
649 $bndreg = str_replace("(", "\\(", $bndreg);
650 $bndreg = str_replace(")", "\\)", $bndreg);
651 } else if ($rfc822_header && $line == '') {
652 $rfc822_header = false;
653 if ($msg->type0 == 'multipart') {
654 $mime_header = true;
655 }
656 }
657
658 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
659 $cnt = count($boundaries)-1;
660 $bnd = $boundaries[$cnt]['bnd'];
661 $bndreg = $boundaries[$cnt]['bndreg'];
662
663 $regstr = '/^--'."($bndreg)".".*".'/';
664 if (preg_match($regstr, $line, $reg)) {
665 $bndlen = strlen($reg[1]);
666 $bndend = false;
667 if (strlen($line) > ($bndlen + 3)) {
668 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
669 $bndend = true;
670 }
671 }
672 if ($bndend) {
673 /* calc offset and return $msg */
674 //$entStr = CalcEntity("$entStr", -1);
675 array_pop($boundaries);
676 $mime_header = true;
677 $bnd_end = true;
678 } else {
679 $mime_header = true;
680 $bnd_end = false;
681 //$entStr = CalcEntity("$entStr", 0);
682 ++$content_indx;
683 }
684 } else {
685 if ($header) { }
686 }
687 }
688 }
689 }
690
691 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
692 $found = false;
693 if ($this->type0 == 'multipart') {
694 if($this->type1 == 'alternative') {
695 $msg = $this->findAlternativeEntity($alt_order);
696 if (count($msg->entities) == 0) {
697 $entity[] = $msg->entity_id;
698 } else {
699 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
700 }
701 $found = true;
702 } else if ($this->type1 == 'related') { /* RFC 2387 */
703 $msgs = $this->findRelatedEntity();
704 foreach ($msgs as $msg) {
705 if (count($msg->entities) == 0) {
706 $entity[] = $msg->entity_id;
707 } else {
708 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
709 }
710 }
711 if (count($msgs) > 0) {
712 $found = true;
713 }
714 } else { /* Treat as multipart/mixed */
715 foreach ($this->entities as $ent) {
716 if((strtolower($ent->header->disposition->name) != 'attachment') &&
717 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
718 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
719 $found = true;
720 }
721 }
722 }
723 } else { /* If not multipart, then just compare with each entry from $alt_order */
724 $type = $this->type0.'/'.$this->type1;
725 foreach ($alt_order as $alt) {
726 if( ($alt == $type) && isset($this->entity_id) ) {
727 if ((count($this->entities) == 0) &&
728 (strtolower($this->header->disposition->name) != 'attachment')) {
729 $entity[] = $this->entity_id;
730 $found = true;
731 }
732 }
733 }
734 }
735 if(!$found) {
736 foreach ($this->entities as $ent) {
737 if((strtolower($ent->header->disposition->name) != 'attachment') &&
738 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
739 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
740 $found = true;
741 }
742 }
743 }
744 if(!$strict && !$found) {
745 if (($this->type0 == 'text') &&
746 in_array($this->type1, array('plain', 'html', 'message')) &&
747 isset($this->entity_id)) {
748 if (count($this->entities) == 0) {
749 if (strtolower($this->header->disposition->name) != 'attachment') {
750 $entity[] = $this->entity_id;
751 }
752 }
753 }
754 }
755
756 return $entity;
757 }
758
759 function findAlternativeEntity($alt_order) {
760 /* If we are dealing with alternative parts then we */
761 /* choose the best viewable message supported by SM. */
762 $best_view = 0;
763 $entity = array();
764 foreach($this->entities as $ent) {
765 $type = $ent->header->type0 . '/' . $ent->header->type1;
766 if ($type == 'multipart/related') {
767 $type = $ent->header->getParameter('type');
768 }
769 $altCount = count($alt_order);
770 for ($j = $best_view; $j < $altCount; ++$j) {
771 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
772 $best_view = $j;
773 $entity = $ent;
774 }
775 }
776 }
777
778 return $entity;
779 }
780
781 function findRelatedEntity() {
782 $msgs = array();
783
784 $entCount = count($this->entities);
785 for ($i = 0; $i < $entCount; ++$i) {
786 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
787 if ($this->header->getParameter('type') == $type) {
788 $msgs[] = $this->entities[$i];
789 }
790 }
791
792 return $msgs;
793 }
794
795 function getAttachments($exclude_id=array(), $result = array()) {
796 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
797 $this = $this->entities[0];
798 }
799
800 if (count($this->entities)) {
801 foreach ($this->entities as $entity) {
802 $exclude = false;
803
804 foreach ($exclude_id as $excl) {
805 if ($entity->entity_id === $excl) {
806 $exclude = true;
807 }
808 }
809
810 if (!$exclude) {
811 if (($entity->type0 == 'multipart') &&
812 ($entity->type1 != 'related')) {
813 $result = $entity->getAttachments($exclude_id, $result);
814 } else if ($entity->type0 != 'multipart') {
815 $result[] = $entity;
816 }
817 }
818 }
819 } else {
820 $exclude = false;
821 foreach ($exclude_id as $excl) {
822 $exclude = $exclude || ($this->entity_id == $excl);
823 }
824
825 if (!$exclude) {
826 $result[] = $this;
827 }
828 }
829
830 return $result;
831 }
832 }
833
834 ?>