let the people be free to resize at will :)
[squirrelmail.git] / class / mime / Message.class.php
CommitLineData
19d470aa 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
14class 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(),
a56f52b9 26 $entity_id = '',
19d470aa 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);
0a408606 74
75 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++$i) {
19d470aa 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
0a408606 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)];
19d470aa 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;
21a50099 198 $arg_a[] = $this->parseProperties($read, $i);
19d470aa 199 ++$arg_no;
200 break;
201 case 2:
202 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
203 ++$i;
21a50099 204 $arg_a[] = $msg->parseDisposition($read, $i);
19d470aa 205 } else { /* properties */
21a50099 206 $arg_a[] = $msg->parseProperties($read, $i);
19d470aa 207 }
19d470aa 208 ++$arg_no;
209 break;
210 case 3:
211 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
212 ++$i;
2bf8f74a 213 $arg_a[]= $msg->parseLanguage($read, $i);
19d470aa 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();
2bf8f74a 222 $msg->rfc822_header = $msg->parseEnvelope($read, $i, $rfc822_hdr);
19d470aa 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;
21a50099 233 $arg_a[] = $msg->parseDisposition($read, $i);
19d470aa 234 ++$arg_no;
235 break;
236 case 9:
237 ++$i;
238 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
21a50099 239 $arg_a[] = $msg->parseDisposition($read, $i);
19d470aa 240 } else {
21a50099 241 $arg_a[] = $msg->parseLanguage($read, $i);
19d470aa 242 }
19d470aa 243 ++$arg_no;
244 break;
245 case 10:
246 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
247 ++$i;
21a50099 248 $arg_a[] = $msg->parseLanguage($read, $i);
19d470aa 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);
21a50099 266 $arg_s = $msg->parseQuote($read, $i);
19d470aa 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 */
21a50099 284 $arg_s = $msg->parseLiteral($read, $i);
19d470aa 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
21a50099 346 function parseProperties($read, &$i) {
19d470aa 347 $properties = array();
348 $prop_name = '';
349
350 for (; $read{$i} != ')'; ++$i) {
351 $arg_s = '';
352 if ($read{$i} == '"') {
21a50099 353 $arg_s = $this->parseQuote($read, $i);
19d470aa 354 } else if ($read{$i} == '{') {
21a50099 355 $arg_s = $this->parseLiteral($read, $i);
19d470aa 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 }
21a50099 368 return $properties;
19d470aa 369 }
370
2bf8f74a 371 function parseEnvelope($read, &$i, $hdr) {
19d470aa 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 '"':
21a50099 380 $arg_a[] = $this->parseQuote($read, $i);
19d470aa 381 ++$arg_no;
382 break;
383 case '{':
21a50099 384 $arg_a[] = $this->parseLiteral($read, $i);
19d470aa 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} == '(') {
2bf8f74a 405 $addr = $this->parseAddress($read, $i);
19d470aa 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 /* argument 1: date */
435 $d = strtr($arg_a[0], array(' ' => ' '));
436 $d = explode(' ', $d);
437 $hdr->date = getTimeStamp($d);
438
439 /* argument 2: subject */
440 $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]);
441 $hdr->subject = $arg_a[1];
442
443 $hdr->from = $arg_a[2][0]; /* argument 3: from */
444 $hdr->sender = $arg_a[3][0]; /* argument 4: sender */
445 $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */
446 $hdr->to = $arg_a[5]; /* argument 6: to */
447 $hdr->cc = $arg_a[6]; /* argument 7: cc */
448 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
449 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
450 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
451 }
2bf8f74a 452 return $hdr;
19d470aa 453 }
454
21a50099 455 function parseLiteral($read, &$i) {
19d470aa 456 $lit_cnt = '';
457 for (++$i; $read{$i} != '}'; ++$i) {
458 $lit_cnt .= $read{$i};
459 }
460
461 $lit_cnt +=2; /* add the { and } characters */
462 $s = '';
463 for ($j = 0; $j < $lit_cnt; ++$j) {
464 $s .= $read{++$i};
465 }
466 return (array($s, $i));
467 }
468
21a50099 469 function parseQuote($read, &$i) {
19d470aa 470 $s = '';
471 for (++$i; $read{$i} != '"'; ++$i) {
472 if ($read{$i} == '\\') {
473 ++$i;
474 }
475 $s .= $read{$i};
476 }
21a50099 477 return $s;
19d470aa 478 }
479
2bf8f74a 480 function parseAddress($read, &$i) {
19d470aa 481 $arg_a = array();
482
483 for (; $read{$i} != ')'; ++$i) {
484 $char = strtoupper($read{$i});
485 switch ($char) {
486 case '"':
487 case '{':
21a50099 488 $arg_a[] = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
19d470aa 489 break;
490 case 'n':
491 case 'N':
492 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
493 $arg_a[] = '';
494 $i += 2;
495 }
496 break;
497 default: break;
498 }
499 }
500
501 if (count($arg_a) == 4) {
502 $adr = new AddressStructure();
503 $adr->personal = $arg_a[0];
504 $adr->adl = $arg_a[1];
505 $adr->mailbox = $arg_a[2];
506 $adr->host = $arg_a[3];
507 } else {
508 $adr = '';
509 }
2bf8f74a 510 return $adr;
19d470aa 511 }
512
21a50099 513 function parseDisposition($read, &$i) {
19d470aa 514 $arg_a = array();
19d470aa 515 for (; $read{$i} != ')'; ++$i) {
516 switch ($read{$i}) {
21a50099 517 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
518 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
519 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
19d470aa 520 default: break;
521 }
522 }
523
524 if (isset($arg_a[0])) {
525 $disp = new Disposition($arg_a[0]);
526 if (isset($arg_a[1])) {
527 $disp->properties = $arg_a[1];
528 }
529 }
530
21a50099 531 return (is_object($disp) ? $disp : '');
19d470aa 532 }
533
21a50099 534 function parseLanguage($read, &$i) {
19d470aa 535 /* no idea how to process this one without examples */
536 $arg_a = array();
537
538 for (; $read{$i} != ')'; ++$i) {
539 switch ($read{$i}) {
21a50099 540 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
541 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
542 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
19d470aa 543 default: break;
544 }
545 }
546
547 if (isset($arg_a[0])) {
548 $lang = new Language($arg_a[0]);
549 if (isset($arg_a[1])) {
550 $lang->properties = $arg_a[1];
551 }
552 }
553
21a50099 554 return (is_object($lang) ? $lang : '');
19d470aa 555 }
556
557 function parseParenthesis($read, $i) {
558 for (; $read{$i} != ')'; ++$i) {
559 switch ($read{$i}) {
21a50099 560 case '"': $this->parseQuote($read, $i); break;
561 case '{': $this->parseLiteral($read, $i); break;
562 case '(': $this->parseProperties($read, $i); break;
19d470aa 563 default: break;
564 }
565 }
566 return $i;
567 }
568
569 /* Function to fill the message structure in case the */
570 /* bodystructure is not available NOT FINISHED YET */
571 function parseMessage($read, $type0, $type1) {
572 switch ($type0) {
573 case 'message':
574 $rfc822_header = true;
575 $mime_header = false;
576 break;
577 case 'multipart':
578 $rfc822_header = false;
579 $mime_header = true;
580 break;
581 default: return $read;
582 }
583
584 for ($i = 1; $i < $count; ++$i) {
585 $line = trim($body[$i]);
586 if (($mime_header || $rfc822_header) &&
587 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
588 $bnd = $reg[1];
589 $bndreg = $bnd;
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 $bndreg = str_replace("-", "\\-", $bndreg);
596 $bndreg = str_replace("(", "\\(", $bndreg);
597 $bndreg = str_replace(")", "\\)", $bndreg);
598 } else if ($rfc822_header && $line == '') {
599 $rfc822_header = false;
600 if ($msg->type0 == 'multipart') {
601 $mime_header = true;
602 }
603 }
604
605 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
606 $cnt = count($boundaries)-1;
607 $bnd = $boundaries[$cnt]['bnd'];
608 $bndreg = $boundaries[$cnt]['bndreg'];
609
610 $regstr = '/^--'."($bndreg)".".*".'/';
611 if (preg_match($regstr, $line, $reg)) {
612 $bndlen = strlen($reg[1]);
613 $bndend = false;
614 if (strlen($line) > ($bndlen + 3)) {
615 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
616 $bndend = true;
617 }
618 }
619 if ($bndend) {
620 /* calc offset and return $msg */
621 //$entStr = CalcEntity("$entStr", -1);
622 array_pop($boundaries);
623 $mime_header = true;
624 $bnd_end = true;
625 } else {
626 $mime_header = true;
627 $bnd_end = false;
628 //$entStr = CalcEntity("$entStr", 0);
629 ++$content_indx;
630 }
631 } else {
632 if ($header) { }
633 }
634 }
635 }
636 }
637
638 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
639 $found = false;
640 if ($this->type0 == 'multipart') {
641 if($this->type1 == 'alternative') {
642 $msg = $this->findAlternativeEntity($alt_order);
643 if (count($msg->entities) == 0) {
644 $entity[] = $msg->entity_id;
645 } else {
646 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
647 }
648 $found = true;
649 } else if ($this->type1 == 'related') { /* RFC 2387 */
650 $msgs = $this->findRelatedEntity();
651 foreach ($msgs as $msg) {
652 if (count($msg->entities) == 0) {
653 $entity[] = $msg->entity_id;
654 } else {
655 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
656 }
657 }
658 if (count($msgs) > 0) {
659 $found = true;
660 }
661 } else { /* Treat as multipart/mixed */
662 foreach ($this->entities as $ent) {
663 if((strtolower($ent->header->disposition->name) != 'attachment') &&
664 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
665 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
666 $found = true;
667 }
668 }
669 }
670 } else { /* If not multipart, then just compare with each entry from $alt_order */
671 $type = $this->type0.'/'.$this->type1;
672 foreach ($alt_order as $alt) {
673 if( ($alt == $type) && isset($this->entity_id) ) {
674 if ((count($this->entities) == 0) &&
675 (strtolower($this->header->disposition->name) != 'attachment')) {
676 $entity[] = $this->entity_id;
677 $found = true;
678 }
679 }
680 }
681 }
682 if(!$found) {
683 foreach ($this->entities as $ent) {
684 if((strtolower($ent->header->disposition->name) != 'attachment') &&
685 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
686 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
687 $found = true;
688 }
689 }
690 }
691 if(!$strict && !$found) {
692 if (($this->type0 == 'text') &&
693 in_array($this->type1, array('plain', 'html', 'message')) &&
694 isset($this->entity_id)) {
695 if (count($this->entities) == 0) {
696 if (strtolower($this->header->disposition->name) != 'attachment') {
697 $entity[] = $this->entity_id;
698 }
699 }
700 }
701 }
702
703 return $entity;
704 }
705
706 function findAlternativeEntity($alt_order) {
707 /* If we are dealing with alternative parts then we */
708 /* choose the best viewable message supported by SM. */
709 $best_view = 0;
710 $entity = array();
711 foreach($this->entities as $ent) {
712 $type = $ent->header->type0 . '/' . $ent->header->type1;
713 if ($type == 'multipart/related') {
714 $type = $ent->header->getParameter('type');
715 }
716 $altCount = count($alt_order);
717 for ($j = $best_view; $j < $altCount; ++$j) {
718 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
719 $best_view = $j;
720 $entity = $ent;
721 }
722 }
723 }
724
725 return $entity;
726 }
727
728 function findRelatedEntity() {
729 $msgs = array();
730
731 $entCount = count($this->entities);
732 for ($i = 0; $i < $entCount; ++$i) {
733 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
734 if ($this->header->getParameter('type') == $type) {
735 $msgs[] = $this->entities[$i];
736 }
737 }
738
739 return $msgs;
740 }
741
742 function getAttachments($exclude_id=array(), $result = array()) {
743 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
744 $this = $this->entities[0];
745 }
746
747 if (count($this->entities)) {
748 foreach ($this->entities as $entity) {
749 $exclude = false;
750
751 foreach ($exclude_id as $excl) {
752 if ($entity->entity_id === $excl) {
753 $exclude = true;
754 }
755 }
756
757 if (!$exclude) {
758 if (($entity->type0 == 'multipart') &&
759 ($entity->type1 != 'related')) {
760 $result = $entity->getAttachments($exclude_id, $result);
761 } else if ($entity->type0 != 'multipart') {
762 $result[] = $entity;
763 }
764 }
765 }
766 } else {
767 $exclude = false;
768 foreach ($exclude_id as $excl) {
769 $exclude = $exclude || ($this->entity_id == $excl);
770 }
771
772 if (!$exclude) {
773 $result[] = $this;
774 }
775 }
776
777 return $result;
778 }
a56f52b9 779
780 function initAttachment($type, $name, $location) {
781 $attachment = new Message();
782 $mime_header = new MessageHeader();
783 $mime_header->setParameter('name', $name);
784 $pos = strpos($type, '/');
785 if ($pos > 0) {
786 $mime_header->type0 = substr($type, 0, $pos);
787 $mime_header->type1 = substr($type, $pos+1);
788 } else {
789 $mime_header->type0 = $type;
790 }
791 $attachment->att_local_name = $location;
792 $disposition = new Disposition('attachment');
793 $disposition->properties['filename'] = $name;
794 $mime_header->disposition = $disposition;
795 $attachment->mime_header = $mime_header;
796 $this->entities[]=$attachment;
797 }
19d470aa 798}
799
800?>