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