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