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