855cdaf722d5f275bd86642c18903bd869210f0f
[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 $entity_id = '',
27 $parent_ent, $entity,
28 $parent = '', $decoded_body='',
29 $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
30 $is_mdnsent = 0,
31 $body_part = '',
32 $offset = 0, /* for fetching body parts out of raw messages */
33 $length = 0, /* for fetching body parts out of raw messages */
34 $att_local_name = ''; /* location where the tempory attachment
35 is stored. For future usage in smtp.php */
36
37 function setEnt($ent) {
38 $this->entity_id= $ent;
39 }
40
41 function addEntity ($msg) {
42 $msg->parent = &$this;
43 $this->entities[] = $msg;
44 }
45
46 function getFilename() {
47 $filename = '';
48 $filename = $this->header->getParameter('filename');
49 if (!$filename) {
50 $filename = $this->header->getParameter('name');
51 }
52
53 if (!$filename) {
54 $filename = 'untitled-'.$this->entity_id;
55 }
56 return $filename;
57 }
58
59
60 function addRFC822Header($read) {
61 $header = new Rfc822Header();
62 $this->rfc822_header = $header->parseHeader($read);
63 }
64
65 function getEntity($ent) {
66 $cur_ent = $this->entity_id;
67 $msg = $this;
68 if (($cur_ent == '') || ($cur_ent == '0')) {
69 $cur_ent_a = array();
70 } else {
71 $cur_ent_a = explode('.', $this->entity_id);
72 }
73 $ent_a = explode('.', $ent);
74
75 for ($i = 0,$entCount = count($ent_a) - 1; $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[$entCount])-1])) {
99 if (is_object($msg->entities[($ent_a[$entCount])-1])) {
100 $msg = $msg->entities[($ent_a[$entCount]-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 $arg_a[] = $this->parseProperties($read, $i);
199 ++$arg_no;
200 break;
201 case 2:
202 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
203 ++$i;
204 $arg_a[] = $msg->parseDisposition($read, $i);
205 } else { /* properties */
206 $arg_a[] = $msg->parseProperties($read, $i);
207 }
208 ++$arg_no;
209 break;
210 case 3:
211 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
212 ++$i;
213 $arg_a[]= $msg->parseLanguage($read, $i);
214 }
215 case 7:
216 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
217 $msg->header->type0 = $arg_a[0];
218 $msg->header->type1 = $arg_a[1];
219 $msg->type0 = $arg_a[0];
220 $msg->type1 = $arg_a[1];
221 $rfc822_hdr = new Rfc822Header();
222 $msg->rfc822_header = $msg->parseEnvelope($read, $i, $rfc822_hdr);
223 while (($i < $cnt) && ($read{$i} != '(')) {
224 ++$i;
225 }
226 $res = $msg->parseStructure($read, $i);
227 $i = $res[1];
228 $msg->addEntity($res[0]);
229 }
230 break;
231 case 8:
232 ++$i;
233 $arg_a[] = $msg->parseDisposition($read, $i);
234 ++$arg_no;
235 break;
236 case 9:
237 ++$i;
238 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
239 $arg_a[] = $msg->parseDisposition($read, $i);
240 } else {
241 $arg_a[] = $msg->parseLanguage($read, $i);
242 }
243 ++$arg_no;
244 break;
245 case 10:
246 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
247 ++$i;
248 $arg_a[] = $msg->parseLanguage($read, $i);
249 } else {
250 $i = $msg->parseParenthesis($read, $i);
251 $arg_a[] = ''; /* not yet described in rfc2060 */
252 }
253 ++$arg_no;
254 break;
255 default:
256 /* unknown argument, skip this part */
257 $i = $msg->parseParenthesis($read, $i);
258 $arg_a[] = '';
259 ++$arg_no;
260 break;
261 } /* switch */
262 break;
263 case '"':
264 /* inside an entity -> start processing */
265 $debug = substr($read, $i, 20);
266 $arg_s = $msg->parseQuote($read, $i);
267 ++$arg_no;
268 if ($arg_no < 3) {
269 $arg_s = strtolower($arg_s); /* type0 and type1 */
270 }
271 $arg_a[] = $arg_s;
272 break;
273 case 'n':
274 case 'N':
275 /* probably NIL argument */
276 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
277 $arg_a[] = '';
278 ++$arg_no;
279 $i += 2;
280 }
281 break;
282 case '{':
283 /* process the literal value */
284 $arg_s = $msg->parseLiteral($read, $i);
285 ++$arg_no;
286 break;
287 case is_numeric($read{$i}):
288 /* process integers */
289 if ($read{$i} == ' ') { break; }
290 $arg_s = $read{$i};;
291 for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) {
292 $arg_s .= $read{$i};
293 }
294 ++$arg_no;
295 $arg_a[] = $arg_s;
296 break;
297 case ')':
298 $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
299 if (!$multipart) {
300 $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
301 $hdr->type0 = $arg_a[0];
302 $hdr->type1 = $arg_a[1];
303
304 $msg->type0 = $arg_a[0];
305 $msg->type1 = $arg_a[1];
306 $arr = $arg_a[2];
307 if (is_array($arr)) {
308 $hdr->parameters = $arg_a[2];
309 }
310 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
311 $hdr->description = $arg_a[4];
312 $hdr->encoding = strtolower($arg_a[5]);
313 $hdr->entity_id = $msg->entity_id;
314 $hdr->size = $arg_a[6];
315 if ($shifted_args) {
316 $hdr->lines = $arg_a[7];
317 $s = 1;
318 } else {
319 $s = 0;
320 }
321 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
322 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
323 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
324 $msg->header = $hdr;
325 if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) {
326 $msg->entity_id = $this->entity_id . '.1';
327 }
328 } else {
329 $hdr->type0 = 'multipart';
330 $hdr->type1 = $arg_a[0];
331 $msg->type0 = 'multipart';
332 $msg->type1 = $arg_a[0];
333 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
334 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
335 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
336 $msg->header = $hdr;
337 }
338 ++$i;
339 return (array($msg, $i));
340 default: break;
341 } /* switch */
342
343 } /* for */
344 } /* parsestructure */
345
346 function parseProperties($read, &$i) {
347 $properties = array();
348 $prop_name = '';
349
350 for (; $read{$i} != ')'; ++$i) {
351 $arg_s = '';
352 if ($read{$i} == '"') {
353 $arg_s = $this->parseQuote($read, $i);
354 } else if ($read{$i} == '{') {
355 $arg_s = $this->parseLiteral($read, $i);
356 }
357
358 if ($arg_s != '') {
359 if ($prop_name == '') {
360 $prop_name = strtolower($arg_s);
361 $properties[$prop_name] = '';
362 } else if ($prop_name != '') {
363 $properties[$prop_name] = $arg_s;
364 $prop_name = '';
365 }
366 }
367 }
368 return $properties;
369 }
370
371 function parseEnvelope($read, &$i, $hdr) {
372 $arg_no = 0;
373 $arg_a = array();
374
375 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
376 ++$i;
377 $char = strtoupper($read{$i});
378 switch ($char) {
379 case '"':
380 $arg_a[] = $this->parseQuote($read, $i);
381 ++$arg_no;
382 break;
383 case '{':
384 $arg_a[] = $this->parseLiteral($read, $i);
385 ++$arg_no;
386 break;
387 case 'N':
388 /* probably NIL argument */
389 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
390 $arg_a[] = '';
391 ++$arg_no;
392 $i += 2;
393 }
394 break;
395 case '(':
396 /* Address structure (with group support)
397 * Note: Group support is useless on SMTP connections
398 * because the protocol doesn't support it
399 */
400 $addr_a = array();
401 $group = '';
402 $a=0;
403 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
404 if ($read{$i} == '(') {
405 $addr = $this->parseAddress($read, $i);
406 if (($addr->host == '') && ($addr->mailbox != '')) {
407 /* start of group */
408 $group = $addr->mailbox;
409 $group_addr = $addr;
410 $j = $a;
411 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
412 /* end group */
413 if ($a == ($j+1)) { /* no group members */
414 $group_addr->group = $group;
415 $group_addr->mailbox = '';
416 $group_addr->personal = "$group: Undisclosed recipients;";
417 $addr_a[] = $group_addr;
418 $group ='';
419 }
420 } else {
421 $addr->group = $group;
422 $addr_a[] = $addr;
423 }
424 ++$a;
425 }
426 }
427 $arg_a[] = $addr_a;
428 break;
429 default: break;
430 }
431 }
432
433 if (count($arg_a) > 9) {
434 $d = strtr($arg_a[0], array(' ' => ' '));
435 $d = explode(' ', $d);
436 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
437
438 $hdr->date = getTimeStamp($d); /* argument 1: date */
439 $hdr->subject = $arg_a[1]; /* argument 2: subject */
440 $hdr->from = $arg_a[2][0]; /* argument 3: from */
441 $hdr->sender = $arg_a[3][0]; /* argument 4: sender */
442 $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */
443 $hdr->to = $arg_a[5]; /* argument 6: to */
444 $hdr->cc = $arg_a[6]; /* argument 7: cc */
445 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
446 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
447 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
448 }
449 return $hdr;
450 }
451
452 function parseLiteral($read, &$i) {
453 $lit_cnt = '';
454 for (++$i; $read{$i} != '}'; ++$i) {
455 $lit_cnt .= $read{$i};
456 }
457
458 $lit_cnt +=2; /* add the { and } characters */
459 $s = '';
460 for ($j = 0; $j < $lit_cnt; ++$j) {
461 $s .= $read{++$i};
462 }
463 return (array($s, $i));
464 }
465
466 function parseQuote($read, &$i) {
467 $s = '';
468 for (++$i; $read{$i} != '"'; ++$i) {
469 if ($read{$i} == '\\') {
470 ++$i;
471 }
472 $s .= $read{$i};
473 }
474 return $s;
475 }
476
477 function parseAddress($read, &$i) {
478 $arg_a = array();
479
480 for (; $read{$i} != ')'; ++$i) {
481 $char = strtoupper($read{$i});
482 switch ($char) {
483 case '"':
484 case '{':
485 $arg_a[] = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
486 break;
487 case 'n':
488 case 'N':
489 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
490 $arg_a[] = '';
491 $i += 2;
492 }
493 break;
494 default: break;
495 }
496 }
497
498 if (count($arg_a) == 4) {
499 $adr = new AddressStructure();
500 $adr->personal = $arg_a[0];
501 $adr->adl = $arg_a[1];
502 $adr->mailbox = $arg_a[2];
503 $adr->host = $arg_a[3];
504 } else {
505 $adr = '';
506 }
507 return $adr;
508 }
509
510 function parseDisposition($read, &$i) {
511 $arg_a = array();
512 for (; $read{$i} != ')'; ++$i) {
513 switch ($read{$i}) {
514 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
515 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
516 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
517 default: break;
518 }
519 }
520
521 if (isset($arg_a[0])) {
522 $disp = new Disposition($arg_a[0]);
523 if (isset($arg_a[1])) {
524 $disp->properties = $arg_a[1];
525 }
526 }
527
528 return (is_object($disp) ? $disp : '');
529 }
530
531 function parseLanguage($read, &$i) {
532 /* no idea how to process this one without examples */
533 $arg_a = array();
534
535 for (; $read{$i} != ')'; ++$i) {
536 switch ($read{$i}) {
537 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
538 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
539 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
540 default: break;
541 }
542 }
543
544 if (isset($arg_a[0])) {
545 $lang = new Language($arg_a[0]);
546 if (isset($arg_a[1])) {
547 $lang->properties = $arg_a[1];
548 }
549 }
550
551 return (is_object($lang) ? $lang : '');
552 }
553
554 function parseParenthesis($read, $i) {
555 for (; $read{$i} != ')'; ++$i) {
556 switch ($read{$i}) {
557 case '"': $this->parseQuote($read, $i); break;
558 case '{': $this->parseLiteral($read, $i); break;
559 case '(': $this->parseProperties($read, $i); break;
560 default: break;
561 }
562 }
563 return $i;
564 }
565
566 /* Function to fill the message structure in case the */
567 /* bodystructure is not available NOT FINISHED YET */
568 function parseMessage($read, $type0, $type1) {
569 switch ($type0) {
570 case 'message':
571 $rfc822_header = true;
572 $mime_header = false;
573 break;
574 case 'multipart':
575 $rfc822_header = false;
576 $mime_header = true;
577 break;
578 default: return $read;
579 }
580
581 for ($i = 1; $i < $count; ++$i) {
582 $line = trim($body[$i]);
583 if (($mime_header || $rfc822_header) &&
584 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
585 $bnd = $reg[1];
586 $bndreg = $bnd;
587 $bndreg = str_replace("\\", "\\\\", $bndreg);
588 $bndreg = str_replace("?", "\\?", $bndreg);
589 $bndreg = str_replace("+", "\\+", $bndreg);
590 $bndreg = str_replace(".", "\\.", $bndreg);
591 $bndreg = str_replace("/", "\\/", $bndreg);
592 $bndreg = str_replace("-", "\\-", $bndreg);
593 $bndreg = str_replace("(", "\\(", $bndreg);
594 $bndreg = str_replace(")", "\\)", $bndreg);
595 } else if ($rfc822_header && $line == '') {
596 $rfc822_header = false;
597 if ($msg->type0 == 'multipart') {
598 $mime_header = true;
599 }
600 }
601
602 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
603 $cnt = count($boundaries)-1;
604 $bnd = $boundaries[$cnt]['bnd'];
605 $bndreg = $boundaries[$cnt]['bndreg'];
606
607 $regstr = '/^--'."($bndreg)".".*".'/';
608 if (preg_match($regstr, $line, $reg)) {
609 $bndlen = strlen($reg[1]);
610 $bndend = false;
611 if (strlen($line) > ($bndlen + 3)) {
612 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
613 $bndend = true;
614 }
615 }
616 if ($bndend) {
617 /* calc offset and return $msg */
618 //$entStr = CalcEntity("$entStr", -1);
619 array_pop($boundaries);
620 $mime_header = true;
621 $bnd_end = true;
622 } else {
623 $mime_header = true;
624 $bnd_end = false;
625 //$entStr = CalcEntity("$entStr", 0);
626 ++$content_indx;
627 }
628 } else {
629 if ($header) { }
630 }
631 }
632 }
633 }
634
635 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
636 $found = false;
637 if ($this->type0 == 'multipart') {
638 if($this->type1 == 'alternative') {
639 $msg = $this->findAlternativeEntity($alt_order);
640 if (count($msg->entities) == 0) {
641 $entity[] = $msg->entity_id;
642 } else {
643 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
644 }
645 $found = true;
646 } else if ($this->type1 == 'related') { /* RFC 2387 */
647 $msgs = $this->findRelatedEntity();
648 foreach ($msgs as $msg) {
649 if (count($msg->entities) == 0) {
650 $entity[] = $msg->entity_id;
651 } else {
652 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
653 }
654 }
655 if (count($msgs) > 0) {
656 $found = true;
657 }
658 } else { /* Treat as multipart/mixed */
659 foreach ($this->entities as $ent) {
660 if((strtolower($ent->header->disposition->name) != 'attachment') &&
661 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
662 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
663 $found = true;
664 }
665 }
666 }
667 } else { /* If not multipart, then just compare with each entry from $alt_order */
668 $type = $this->type0.'/'.$this->type1;
669 foreach ($alt_order as $alt) {
670 if( ($alt == $type) && isset($this->entity_id) ) {
671 if ((count($this->entities) == 0) &&
672 (strtolower($this->header->disposition->name) != 'attachment')) {
673 $entity[] = $this->entity_id;
674 $found = true;
675 }
676 }
677 }
678 }
679 if(!$found) {
680 foreach ($this->entities as $ent) {
681 if((strtolower($ent->header->disposition->name) != 'attachment') &&
682 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
683 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
684 $found = true;
685 }
686 }
687 }
688 if(!$strict && !$found) {
689 if (($this->type0 == 'text') &&
690 in_array($this->type1, array('plain', 'html', 'message')) &&
691 isset($this->entity_id)) {
692 if (count($this->entities) == 0) {
693 if (strtolower($this->header->disposition->name) != 'attachment') {
694 $entity[] = $this->entity_id;
695 }
696 }
697 }
698 }
699
700 return $entity;
701 }
702
703 function findAlternativeEntity($alt_order) {
704 /* If we are dealing with alternative parts then we */
705 /* choose the best viewable message supported by SM. */
706 $best_view = 0;
707 $entity = array();
708 foreach($this->entities as $ent) {
709 $type = $ent->header->type0 . '/' . $ent->header->type1;
710 if ($type == 'multipart/related') {
711 $type = $ent->header->getParameter('type');
712 }
713 $altCount = count($alt_order);
714 for ($j = $best_view; $j < $altCount; ++$j) {
715 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
716 $best_view = $j;
717 $entity = $ent;
718 }
719 }
720 }
721
722 return $entity;
723 }
724
725 function findRelatedEntity() {
726 $msgs = array();
727
728 $entCount = count($this->entities);
729 for ($i = 0; $i < $entCount; ++$i) {
730 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
731 if ($this->header->getParameter('type') == $type) {
732 $msgs[] = $this->entities[$i];
733 }
734 }
735
736 return $msgs;
737 }
738
739 function getAttachments($exclude_id=array(), $result = array()) {
740 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
741 $this = $this->entities[0];
742 }
743
744 if (count($this->entities)) {
745 foreach ($this->entities as $entity) {
746 $exclude = false;
747
748 foreach ($exclude_id as $excl) {
749 if ($entity->entity_id === $excl) {
750 $exclude = true;
751 }
752 }
753
754 if (!$exclude) {
755 if (($entity->type0 == 'multipart') &&
756 ($entity->type1 != 'related')) {
757 $result = $entity->getAttachments($exclude_id, $result);
758 } else if ($entity->type0 != 'multipart') {
759 $result[] = $entity;
760 }
761 }
762 }
763 } else {
764 $exclude = false;
765 foreach ($exclude_id as $excl) {
766 $exclude = $exclude || ($this->entity_id == $excl);
767 }
768
769 if (!$exclude) {
770 $result[] = $this;
771 }
772 }
773
774 return $result;
775 }
776
777 function initAttachment($type, $name, $location) {
778 $attachment = new Message();
779 $mime_header = new MessageHeader();
780 $mime_header->setParameter('name', $name);
781 $pos = strpos($type, '/');
782 if ($pos > 0) {
783 $mime_header->type0 = substr($type, 0, $pos);
784 $mime_header->type1 = substr($type, $pos+1);
785 } else {
786 $mime_header->type0 = $type;
787 }
788 $attachment->att_local_name = $location;
789 $disposition = new Disposition('attachment');
790 $disposition->properties['filename'] = $name;
791 $mime_header->disposition = $disposition;
792 $attachment->mime_header = $mime_header;
793 $this->entities[]=$attachment;
794 }
795 }
796
797 ?>