minor changes to the bodystructure parser
[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;
d31ee225 287 case '0':
19d470aa 288 case is_numeric($read{$i}):
289 /* process integers */
290 if ($read{$i} == ' ') { break; }
291 $arg_s = $read{$i};;
292 for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) {
293 $arg_s .= $read{$i};
294 }
295 ++$arg_no;
296 $arg_a[] = $arg_s;
297 break;
298 case ')':
299 $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
300 if (!$multipart) {
301 $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
302 $hdr->type0 = $arg_a[0];
303 $hdr->type1 = $arg_a[1];
304
305 $msg->type0 = $arg_a[0];
306 $msg->type1 = $arg_a[1];
307 $arr = $arg_a[2];
308 if (is_array($arr)) {
309 $hdr->parameters = $arg_a[2];
310 }
311 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
312 $hdr->description = $arg_a[4];
313 $hdr->encoding = strtolower($arg_a[5]);
314 $hdr->entity_id = $msg->entity_id;
315 $hdr->size = $arg_a[6];
316 if ($shifted_args) {
317 $hdr->lines = $arg_a[7];
318 $s = 1;
319 } else {
320 $s = 0;
321 }
322 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
323 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
324 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
325 $msg->header = $hdr;
326 if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) {
327 $msg->entity_id = $this->entity_id . '.1';
328 }
329 } else {
330 $hdr->type0 = 'multipart';
331 $hdr->type1 = $arg_a[0];
332 $msg->type0 = 'multipart';
333 $msg->type1 = $arg_a[0];
334 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
335 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
336 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
337 $msg->header = $hdr;
338 }
339 ++$i;
340 return (array($msg, $i));
341 default: break;
342 } /* switch */
343
344 } /* for */
345 } /* parsestructure */
346
21a50099 347 function parseProperties($read, &$i) {
19d470aa 348 $properties = array();
349 $prop_name = '';
350
351 for (; $read{$i} != ')'; ++$i) {
352 $arg_s = '';
353 if ($read{$i} == '"') {
21a50099 354 $arg_s = $this->parseQuote($read, $i);
19d470aa 355 } else if ($read{$i} == '{') {
21a50099 356 $arg_s = $this->parseLiteral($read, $i);
19d470aa 357 }
358
359 if ($arg_s != '') {
360 if ($prop_name == '') {
361 $prop_name = strtolower($arg_s);
362 $properties[$prop_name] = '';
363 } else if ($prop_name != '') {
364 $properties[$prop_name] = $arg_s;
365 $prop_name = '';
366 }
367 }
368 }
21a50099 369 return $properties;
19d470aa 370 }
371
2bf8f74a 372 function parseEnvelope($read, &$i, $hdr) {
19d470aa 373 $arg_no = 0;
374 $arg_a = array();
375
376 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
377 ++$i;
378 $char = strtoupper($read{$i});
379 switch ($char) {
380 case '"':
21a50099 381 $arg_a[] = $this->parseQuote($read, $i);
19d470aa 382 ++$arg_no;
383 break;
384 case '{':
21a50099 385 $arg_a[] = $this->parseLiteral($read, $i);
19d470aa 386 ++$arg_no;
387 break;
388 case 'N':
389 /* probably NIL argument */
390 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
391 $arg_a[] = '';
392 ++$arg_no;
393 $i += 2;
394 }
395 break;
396 case '(':
397 /* Address structure (with group support)
398 * Note: Group support is useless on SMTP connections
399 * because the protocol doesn't support it
400 */
401 $addr_a = array();
402 $group = '';
403 $a=0;
404 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
405 if ($read{$i} == '(') {
2bf8f74a 406 $addr = $this->parseAddress($read, $i);
19d470aa 407 if (($addr->host == '') && ($addr->mailbox != '')) {
408 /* start of group */
409 $group = $addr->mailbox;
410 $group_addr = $addr;
411 $j = $a;
412 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
413 /* end group */
414 if ($a == ($j+1)) { /* no group members */
415 $group_addr->group = $group;
416 $group_addr->mailbox = '';
417 $group_addr->personal = "$group: Undisclosed recipients;";
418 $addr_a[] = $group_addr;
419 $group ='';
420 }
421 } else {
422 $addr->group = $group;
423 $addr_a[] = $addr;
424 }
425 ++$a;
426 }
427 }
428 $arg_a[] = $addr_a;
429 break;
430 default: break;
431 }
432 }
433
434 if (count($arg_a) > 9) {
19d470aa 435 $d = strtr($arg_a[0], array(' ' => ' '));
436 $d = explode(' ', $d);
8750d90e 437 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
19d470aa 438
8750d90e 439 $hdr->date = getTimeStamp($d); /* argument 1: date */
440 $hdr->subject = $arg_a[1]; /* argument 2: subject */
19d470aa 441 $hdr->from = $arg_a[2][0]; /* argument 3: from */
442 $hdr->sender = $arg_a[3][0]; /* argument 4: sender */
443 $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */
444 $hdr->to = $arg_a[5]; /* argument 6: to */
445 $hdr->cc = $arg_a[6]; /* argument 7: cc */
446 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
447 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
448 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
449 }
2bf8f74a 450 return $hdr;
19d470aa 451 }
452
21a50099 453 function parseLiteral($read, &$i) {
19d470aa 454 $lit_cnt = '';
455 for (++$i; $read{$i} != '}'; ++$i) {
456 $lit_cnt .= $read{$i};
457 }
458
459 $lit_cnt +=2; /* add the { and } characters */
460 $s = '';
461 for ($j = 0; $j < $lit_cnt; ++$j) {
462 $s .= $read{++$i};
463 }
5520fad8 464 return $s;
19d470aa 465 }
466
21a50099 467 function parseQuote($read, &$i) {
19d470aa 468 $s = '';
469 for (++$i; $read{$i} != '"'; ++$i) {
470 if ($read{$i} == '\\') {
471 ++$i;
472 }
473 $s .= $read{$i};
474 }
21a50099 475 return $s;
19d470aa 476 }
477
2bf8f74a 478 function parseAddress($read, &$i) {
19d470aa 479 $arg_a = array();
480
481 for (; $read{$i} != ')'; ++$i) {
482 $char = strtoupper($read{$i});
483 switch ($char) {
484 case '"':
485 case '{':
21a50099 486 $arg_a[] = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i));
19d470aa 487 break;
488 case 'n':
489 case 'N':
490 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
491 $arg_a[] = '';
492 $i += 2;
493 }
494 break;
495 default: break;
496 }
497 }
498
499 if (count($arg_a) == 4) {
500 $adr = new AddressStructure();
501 $adr->personal = $arg_a[0];
502 $adr->adl = $arg_a[1];
503 $adr->mailbox = $arg_a[2];
504 $adr->host = $arg_a[3];
505 } else {
506 $adr = '';
507 }
2bf8f74a 508 return $adr;
19d470aa 509 }
510
21a50099 511 function parseDisposition($read, &$i) {
19d470aa 512 $arg_a = array();
19d470aa 513 for (; $read{$i} != ')'; ++$i) {
514 switch ($read{$i}) {
21a50099 515 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
516 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
517 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
19d470aa 518 default: break;
519 }
520 }
521
522 if (isset($arg_a[0])) {
523 $disp = new Disposition($arg_a[0]);
524 if (isset($arg_a[1])) {
525 $disp->properties = $arg_a[1];
526 }
527 }
528
21a50099 529 return (is_object($disp) ? $disp : '');
19d470aa 530 }
531
21a50099 532 function parseLanguage($read, &$i) {
19d470aa 533 /* no idea how to process this one without examples */
534 $arg_a = array();
535
536 for (; $read{$i} != ')'; ++$i) {
537 switch ($read{$i}) {
21a50099 538 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
539 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
540 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
19d470aa 541 default: break;
542 }
543 }
544
545 if (isset($arg_a[0])) {
546 $lang = new Language($arg_a[0]);
547 if (isset($arg_a[1])) {
548 $lang->properties = $arg_a[1];
549 }
550 }
551
21a50099 552 return (is_object($lang) ? $lang : '');
19d470aa 553 }
554
555 function parseParenthesis($read, $i) {
556 for (; $read{$i} != ')'; ++$i) {
557 switch ($read{$i}) {
21a50099 558 case '"': $this->parseQuote($read, $i); break;
559 case '{': $this->parseLiteral($read, $i); break;
560 case '(': $this->parseProperties($read, $i); break;
19d470aa 561 default: break;
562 }
563 }
564 return $i;
565 }
566
567 /* Function to fill the message structure in case the */
568 /* bodystructure is not available NOT FINISHED YET */
569 function parseMessage($read, $type0, $type1) {
570 switch ($type0) {
571 case 'message':
572 $rfc822_header = true;
573 $mime_header = false;
574 break;
575 case 'multipart':
576 $rfc822_header = false;
577 $mime_header = true;
578 break;
579 default: return $read;
580 }
581
582 for ($i = 1; $i < $count; ++$i) {
583 $line = trim($body[$i]);
584 if (($mime_header || $rfc822_header) &&
585 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
586 $bnd = $reg[1];
587 $bndreg = $bnd;
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 $bndreg = str_replace(")", "\\)", $bndreg);
596 } else if ($rfc822_header && $line == '') {
597 $rfc822_header = false;
598 if ($msg->type0 == 'multipart') {
599 $mime_header = true;
600 }
601 }
602
603 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
604 $cnt = count($boundaries)-1;
605 $bnd = $boundaries[$cnt]['bnd'];
606 $bndreg = $boundaries[$cnt]['bndreg'];
607
608 $regstr = '/^--'."($bndreg)".".*".'/';
609 if (preg_match($regstr, $line, $reg)) {
610 $bndlen = strlen($reg[1]);
611 $bndend = false;
612 if (strlen($line) > ($bndlen + 3)) {
613 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
614 $bndend = true;
615 }
616 }
617 if ($bndend) {
618 /* calc offset and return $msg */
619 //$entStr = CalcEntity("$entStr", -1);
620 array_pop($boundaries);
621 $mime_header = true;
622 $bnd_end = true;
623 } else {
624 $mime_header = true;
625 $bnd_end = false;
626 //$entStr = CalcEntity("$entStr", 0);
627 ++$content_indx;
628 }
629 } else {
630 if ($header) { }
631 }
632 }
633 }
634 }
635
636 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
637 $found = false;
638 if ($this->type0 == 'multipart') {
639 if($this->type1 == 'alternative') {
640 $msg = $this->findAlternativeEntity($alt_order);
641 if (count($msg->entities) == 0) {
642 $entity[] = $msg->entity_id;
643 } else {
644 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
645 }
646 $found = true;
647 } else if ($this->type1 == 'related') { /* RFC 2387 */
648 $msgs = $this->findRelatedEntity();
649 foreach ($msgs as $msg) {
650 if (count($msg->entities) == 0) {
651 $entity[] = $msg->entity_id;
652 } else {
653 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
654 }
655 }
656 if (count($msgs) > 0) {
657 $found = true;
658 }
659 } else { /* Treat as multipart/mixed */
660 foreach ($this->entities as $ent) {
661 if((strtolower($ent->header->disposition->name) != 'attachment') &&
662 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
663 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
664 $found = true;
665 }
666 }
667 }
668 } else { /* If not multipart, then just compare with each entry from $alt_order */
669 $type = $this->type0.'/'.$this->type1;
670 foreach ($alt_order as $alt) {
671 if( ($alt == $type) && isset($this->entity_id) ) {
672 if ((count($this->entities) == 0) &&
673 (strtolower($this->header->disposition->name) != 'attachment')) {
674 $entity[] = $this->entity_id;
675 $found = true;
676 }
677 }
678 }
679 }
680 if(!$found) {
681 foreach ($this->entities as $ent) {
682 if((strtolower($ent->header->disposition->name) != 'attachment') &&
683 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
684 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
685 $found = true;
686 }
687 }
688 }
689 if(!$strict && !$found) {
690 if (($this->type0 == 'text') &&
691 in_array($this->type1, array('plain', 'html', 'message')) &&
692 isset($this->entity_id)) {
693 if (count($this->entities) == 0) {
694 if (strtolower($this->header->disposition->name) != 'attachment') {
695 $entity[] = $this->entity_id;
696 }
697 }
698 }
699 }
700
701 return $entity;
702 }
703
704 function findAlternativeEntity($alt_order) {
705 /* If we are dealing with alternative parts then we */
706 /* choose the best viewable message supported by SM. */
707 $best_view = 0;
708 $entity = array();
709 foreach($this->entities as $ent) {
710 $type = $ent->header->type0 . '/' . $ent->header->type1;
711 if ($type == 'multipart/related') {
712 $type = $ent->header->getParameter('type');
713 }
714 $altCount = count($alt_order);
715 for ($j = $best_view; $j < $altCount; ++$j) {
716 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
717 $best_view = $j;
718 $entity = $ent;
719 }
720 }
721 }
722
723 return $entity;
724 }
725
726 function findRelatedEntity() {
727 $msgs = array();
728
729 $entCount = count($this->entities);
730 for ($i = 0; $i < $entCount; ++$i) {
731 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
732 if ($this->header->getParameter('type') == $type) {
733 $msgs[] = $this->entities[$i];
734 }
735 }
736
737 return $msgs;
738 }
739
740 function getAttachments($exclude_id=array(), $result = array()) {
741 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
742 $this = $this->entities[0];
743 }
744
745 if (count($this->entities)) {
746 foreach ($this->entities as $entity) {
747 $exclude = false;
748
749 foreach ($exclude_id as $excl) {
750 if ($entity->entity_id === $excl) {
751 $exclude = true;
752 }
753 }
754
755 if (!$exclude) {
756 if (($entity->type0 == 'multipart') &&
757 ($entity->type1 != 'related')) {
758 $result = $entity->getAttachments($exclude_id, $result);
759 } else if ($entity->type0 != 'multipart') {
760 $result[] = $entity;
761 }
762 }
763 }
764 } else {
765 $exclude = false;
766 foreach ($exclude_id as $excl) {
767 $exclude = $exclude || ($this->entity_id == $excl);
768 }
769
770 if (!$exclude) {
771 $result[] = $this;
772 }
773 }
774
775 return $result;
776 }
a56f52b9 777
778 function initAttachment($type, $name, $location) {
779 $attachment = new Message();
780 $mime_header = new MessageHeader();
781 $mime_header->setParameter('name', $name);
782 $pos = strpos($type, '/');
783 if ($pos > 0) {
784 $mime_header->type0 = substr($type, 0, $pos);
785 $mime_header->type1 = substr($type, $pos+1);
786 } else {
787 $mime_header->type0 = $type;
788 }
789 $attachment->att_local_name = $location;
790 $disposition = new Disposition('attachment');
791 $disposition->properties['filename'] = $name;
792 $mime_header->disposition = $disposition;
793 $attachment->mime_header = $mime_header;
794 $this->entities[]=$attachment;
795 }
19d470aa 796}
797
798?>