Speed improvements by using php internal functions and bugfix regarding
[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, $sub_msg = '') {
170 $arg_no = 0;
171 $arg_a = array();
172 if ($sub_msg) {
173 $message = $sub_msg;
174 } else {
175 $message = new Message();
176 }
177 $this = $message;
178 for ($cnt = strlen($read); $i < $cnt; ++$i) {
179 $char = strtoupper($read{$i});
180 switch ($char) {
181 case '(':
182 switch($arg_no) {
183 case 0:
184 if (!isset($msg)) {
185 $msg = new Message();
186 $hdr = new MessageHeader();
187 $hdr->type0 = 'text';
188 $hdr->type1 = 'plain';
189 $hdr->encoding = 'us-ascii';
190 $msg->entity_id = $message->calcEntity($msg);
191 } else {
192 $msg->header->type0 = 'multipart';
193 $msg->type0 = 'multipart';
194 while ($read{$i} == '(') {
195 $msg->addEntity($this->parseStructure($read, $i, $msg));
196 }
197 }
198 break;
199 case 1:
200 /* multipart properties */
201 ++$i;
202 $arg_a[] = $this->parseProperties($read, $i);
203 ++$arg_no;
204 break;
205 case 2:
206 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
207 ++$i;
208 $arg_a[] = $this->parseDisposition($read, $i);
209 } else { /* properties */
210 $arg_a[] = $this->parseProperties($read, $i);
211 }
212 ++$arg_no;
213 break;
214 case 3:
215 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
216 ++$i;
217 $arg_a[]= $this->parseLanguage($read, $i);
218 }
219 case 7:
220 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
221 $msg->header->type0 = $arg_a[0];
222 $msg->header->type1 = $arg_a[1];
223 $msg->type0 = $arg_a[0];
224 $msg->type1 = $arg_a[1];
225 $rfc822_hdr = new Rfc822Header();
226 $msg->rfc822_header = $this->parseEnvelope($read, $i, $rfc822_hdr);
227 while (($i < $cnt) && ($read{$i} != '(')) {
228 ++$i;
229 }
230 $msg->addEntity($this->parseStructure($read, $i,$msg));
231 }
232 break;
233 case 8:
234 ++$i;
235 $arg_a[] = $this->parseDisposition($read, $i);
236 ++$arg_no;
237 break;
238 case 9:
239 ++$i;
240 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
241 $arg_a[] = $this->parseDisposition($read, $i);
242 } else {
243 $arg_a[] = $this->parseLanguage($read, $i);
244 }
245 ++$arg_no;
246 break;
247 case 10:
248 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
249 ++$i;
250 $arg_a[] = $this->parseLanguage($read, $i);
251 } else {
252 $i = $this->parseParenthesis($read, $i);
253 $arg_a[] = ''; /* not yet described in rfc2060 */
254 }
255 ++$arg_no;
256 break;
257 default:
258 /* unknown argument, skip this part */
259 $i = $this->parseParenthesis($read, $i);
260 $arg_a[] = '';
261 ++$arg_no;
262 break;
263 } /* switch */
264 break;
265 case '"':
266 /* inside an entity -> start processing */
267 $arg_s = $this->parseQuote($read, $i);
268 ++$arg_no;
269 if ($arg_no < 3) {
270 $arg_s = strtolower($arg_s); /* type0 and type1 */
271 }
272 $arg_a[] = $arg_s;
273 break;
274 case 'n':
275 case 'N':
276 /* probably NIL argument */
277 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
278 $arg_a[] = '';
279 ++$arg_no;
280 $i += 2;
281 }
282 break;
283 case '{':
284 /* process the literal value */
285 $arg_s = $this->parseLiteral($read, $i);
286 ++$arg_no;
287 break;
288 case '0':
289 case is_numeric($read{$i}):
290 /* process integers */
291 if ($read{$i} == ' ') { break; }
292 ++$arg_no;
293 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
294 $i += strlen($regs[1])-1;
295 $arg_a[] = $regs[1];
296 } else {
297 $arg_a[] = 0;
298 }
299 break;
300 case ')':
301 $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
302 if (!$multipart) {
303 $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
304 $hdr->type0 = $arg_a[0];
305 $hdr->type1 = $arg_a[1];
306
307 $msg->type0 = $arg_a[0];
308 $msg->type1 = $arg_a[1];
309 $arr = $arg_a[2];
310 if (is_array($arr)) {
311 $hdr->parameters = $arg_a[2];
312 }
313 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
314 $hdr->description = $arg_a[4];
315 $hdr->encoding = strtolower($arg_a[5]);
316 $hdr->entity_id = $msg->entity_id;
317 $hdr->size = $arg_a[6];
318 if ($shifted_args) {
319 $hdr->lines = $arg_a[7];
320 $s = 1;
321 } else {
322 $s = 0;
323 }
324 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
325 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
326 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
327 $msg->header = $hdr;
328 if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) {
329 $msg->entity_id = $message->entity_id . '.1';
330 }
331 } else {
332 $hdr->type0 = 'multipart';
333 $hdr->type1 = $arg_a[0];
334 $msg->type0 = 'multipart';
335 $msg->type1 = $arg_a[0];
336 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
337 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
338 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
339 $msg->header = $hdr;
340 }
341 return $msg;
342 default: break;
343 } /* switch */
344 } /* for */
345 } /* parsestructure */
346
347 function parseProperties($read, &$i) {
348 $properties = array();
349 $prop_name = '';
350
351 for (; $read{$i} != ')'; ++$i) {
352 $arg_s = '';
353 if ($read{$i} == '"') {
354 $arg_s = $this->parseQuote($read, $i);
355 } else if ($read{$i} == '{') {
356 $arg_s = $this->parseLiteral($read, $i);
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 }
369 return $properties;
370 }
371
372 function parseEnvelope($read, &$i, $hdr) {
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 '"':
381 $arg_a[] = $this->parseQuote($read, $i);
382 ++$arg_no;
383 break;
384 case '{':
385 $arg_a[] = $this->parseLiteral($read, $i);
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} == '(') {
406 $addr = $this->parseAddress($read, $i);
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) {
435 $d = strtr($arg_a[0], array(' ' => ' '));
436 $d = explode(' ', $d);
437 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
438
439 $hdr->date = getTimeStamp($d); /* argument 1: date */
440 $hdr->subject = $arg_a[1]; /* argument 2: subject */
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 }
450 return $hdr;
451 }
452
453 function parseLiteral($read, &$i) {
454 $lit_cnt = '';
455 ++$i;
456 $iPos = strpos($read,'}',$i);
457 if ($iPos) {
458 $lit_cnt = substr($read, $i, $iPos - $i);
459 $i += strlen($lit_cnt) + 3; /* } + \r + \n */
460 $lit_cnt -= 2; /* lit_cnt includes \r\n */
461 $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
462 $i += $lit_cnt;
463 } else { /* should never happen */
464 $i += 3; /* } + \r + \n */
465 $s = '';
466 }
467 return $s;
468 }
469
470 function parseQuote($read, &$i) {
471 $s = '';
472 $iPos = ++$i;
473 while (true) {
474 $iPos = strpos($read,'"',$iPos);
475 if ($iPos === false) break;
476 if ($iPos && $read{$iPos -1} != '\\') {
477 $s = substr($read,$i,($iPos-$i));
478 $i = $iPos;
479 break;
480 }
481 }
482 return $s;
483 }
484
485 function parseAddress($read, &$i) {
486 $arg_a = array();
487 for (; $read{$i} != ')'; ++$i) {
488 $char = strtoupper($read{$i});
489 switch ($char) {
490 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
491 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
492 case 'n':
493 case 'N':
494 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
495 $arg_a[] = '';
496 $i += 2;
497 }
498 break;
499 default: break;
500 }
501 }
502
503 if (count($arg_a) == 4) {
504 $adr = new AddressStructure();
505 $adr->personal = $arg_a[0];
506 $adr->adl = $arg_a[1];
507 $adr->mailbox = $arg_a[2];
508 $adr->host = $arg_a[3];
509 } else {
510 $adr = '';
511 }
512 return $adr;
513 }
514
515 function parseDisposition($read, &$i) {
516 $arg_a = array();
517 for (; $read{$i} != ')'; ++$i) {
518 switch ($read{$i}) {
519 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
520 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
521 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
522 default: break;
523 }
524 }
525
526 if (isset($arg_a[0])) {
527 $disp = new Disposition($arg_a[0]);
528 if (isset($arg_a[1])) {
529 $disp->properties = $arg_a[1];
530 }
531 }
532
533 return (is_object($disp) ? $disp : '');
534 }
535
536 function parseLanguage($read, &$i) {
537 /* no idea how to process this one without examples */
538 $arg_a = array();
539
540 for (; $read{$i} != ')'; ++$i) {
541 switch ($read{$i}) {
542 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
543 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
544 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
545 default: break;
546 }
547 }
548
549 if (isset($arg_a[0])) {
550 $lang = new Language($arg_a[0]);
551 if (isset($arg_a[1])) {
552 $lang->properties = $arg_a[1];
553 }
554 }
555
556 return (is_object($lang) ? $lang : '');
557 }
558
559 function parseParenthesis($read, $i) {
560 for (; $read{$i} != ')'; ++$i) {
561 switch ($read{$i}) {
562 case '"': $this->parseQuote($read, $i); break;
563 case '{': $this->parseLiteral($read, $i); break;
564 case '(': $this->parseProperties($read, $i); break;
565 default: break;
566 }
567 }
568 return $i;
569 }
570
571 /* Function to fill the message structure in case the */
572 /* bodystructure is not available NOT FINISHED YET */
573 function parseMessage($read, $type0, $type1) {
574 switch ($type0) {
575 case 'message':
576 $rfc822_header = true;
577 $mime_header = false;
578 break;
579 case 'multipart':
580 $rfc822_header = false;
581 $mime_header = true;
582 break;
583 default: return $read;
584 }
585
586 for ($i = 1; $i < $count; ++$i) {
587 $line = trim($body[$i]);
588 if (($mime_header || $rfc822_header) &&
589 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
590 $bnd = $reg[1];
591 $bndreg = $bnd;
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 $bndreg = str_replace("(", "\\(", $bndreg);
599 $bndreg = str_replace(")", "\\)", $bndreg);
600 } else if ($rfc822_header && $line == '') {
601 $rfc822_header = false;
602 if ($msg->type0 == 'multipart') {
603 $mime_header = true;
604 }
605 }
606
607 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
608 $cnt = count($boundaries)-1;
609 $bnd = $boundaries[$cnt]['bnd'];
610 $bndreg = $boundaries[$cnt]['bndreg'];
611
612 $regstr = '/^--'."($bndreg)".".*".'/';
613 if (preg_match($regstr, $line, $reg)) {
614 $bndlen = strlen($reg[1]);
615 $bndend = false;
616 if (strlen($line) > ($bndlen + 3)) {
617 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
618 $bndend = true;
619 }
620 }
621 if ($bndend) {
622 /* calc offset and return $msg */
623 //$entStr = CalcEntity("$entStr", -1);
624 array_pop($boundaries);
625 $mime_header = true;
626 $bnd_end = true;
627 } else {
628 $mime_header = true;
629 $bnd_end = false;
630 //$entStr = CalcEntity("$entStr", 0);
631 ++$content_indx;
632 }
633 } else {
634 if ($header) { }
635 }
636 }
637 }
638 }
639
640 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
641 $found = false;
642 if ($this->type0 == 'multipart') {
643 if($this->type1 == 'alternative') {
644 $msg = $this->findAlternativeEntity($alt_order);
645 if (count($msg->entities) == 0) {
646 $entity[] = $msg->entity_id;
647 } else {
648 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
649 }
650 $found = true;
651 } else if ($this->type1 == 'related') { /* RFC 2387 */
652 $msgs = $this->findRelatedEntity();
653 foreach ($msgs as $msg) {
654 if (count($msg->entities) == 0) {
655 $entity[] = $msg->entity_id;
656 } else {
657 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
658 }
659 }
660 if (count($msgs) > 0) {
661 $found = true;
662 }
663 } else { /* Treat as multipart/mixed */
664 foreach ($this->entities as $ent) {
665 if((strtolower($ent->header->disposition->name) != 'attachment') &&
666 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
667 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
668 $found = true;
669 }
670 }
671 }
672 } else { /* If not multipart, then just compare with each entry from $alt_order */
673 $type = $this->type0.'/'.$this->type1;
674 foreach ($alt_order as $alt) {
675 if( ($alt == $type) && isset($this->entity_id) ) {
676 if ((count($this->entities) == 0) &&
677 (strtolower($this->header->disposition->name) != 'attachment')) {
678 $entity[] = $this->entity_id;
679 $found = true;
680 }
681 }
682 }
683 }
684 if(!$found) {
685 foreach ($this->entities as $ent) {
686 if((strtolower($ent->header->disposition->name) != 'attachment') &&
687 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
688 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
689 $found = true;
690 }
691 }
692 }
693 if(!$strict && !$found) {
694 if (($this->type0 == 'text') &&
695 in_array($this->type1, array('plain', 'html', 'message')) &&
696 isset($this->entity_id)) {
697 if (count($this->entities) == 0) {
698 if (strtolower($this->header->disposition->name) != 'attachment') {
699 $entity[] = $this->entity_id;
700 }
701 }
702 }
703 }
704
705 return $entity;
706 }
707
708 function findAlternativeEntity($alt_order) {
709 /* If we are dealing with alternative parts then we */
710 /* choose the best viewable message supported by SM. */
711 $best_view = 0;
712 $entity = array();
713 foreach($this->entities as $ent) {
714 $type = $ent->header->type0 . '/' . $ent->header->type1;
715 if ($type == 'multipart/related') {
716 $type = $ent->header->getParameter('type');
717 }
718 $altCount = count($alt_order);
719 for ($j = $best_view; $j < $altCount; ++$j) {
720 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
721 $best_view = $j;
722 $entity = $ent;
723 }
724 }
725 }
726
727 return $entity;
728 }
729
730 function findRelatedEntity() {
731 $msgs = array();
732
733 $entCount = count($this->entities);
734 for ($i = 0; $i < $entCount; ++$i) {
735 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
736 if ($this->header->getParameter('type') == $type) {
737 $msgs[] = $this->entities[$i];
738 }
739 }
740
741 return $msgs;
742 }
743
744 function getAttachments($exclude_id=array(), $result = array()) {
745 if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) {
746 $this = $this->entities[0];
747 }
748
749 if (count($this->entities)) {
750 foreach ($this->entities as $entity) {
751 $exclude = false;
752
753 foreach ($exclude_id as $excl) {
754 if ($entity->entity_id === $excl) {
755 $exclude = true;
756 }
757 }
758
759 if (!$exclude) {
760 if (($entity->type0 == 'multipart') &&
761 ($entity->type1 != 'related')) {
762 $result = $entity->getAttachments($exclude_id, $result);
763 } else if ($entity->type0 != 'multipart') {
764 $result[] = $entity;
765 }
766 }
767 }
768 } else {
769 $exclude = false;
770 foreach ($exclude_id as $excl) {
771 $exclude = $exclude || ($this->entity_id == $excl);
772 }
773
774 if (!$exclude) {
775 $result[] = $this;
776 }
777 }
778
779 return $result;
780 }
781
782 function initAttachment($type, $name, $location) {
783 $attachment = new Message();
784 $mime_header = new MessageHeader();
785 $mime_header->setParameter('name', $name);
786 $pos = strpos($type, '/');
787 if ($pos > 0) {
788 $mime_header->type0 = substr($type, 0, $pos);
789 $mime_header->type1 = substr($type, $pos+1);
790 } else {
791 $mime_header->type0 = $type;
792 }
793 $attachment->att_local_name = $location;
794 $disposition = new Disposition('attachment');
795 $disposition->properties['filename'] = $name;
796 $mime_header->disposition = $disposition;
797 $attachment->mime_header = $mime_header;
798 $this->entities[]=$attachment;
799 }
800 }
801
802 ?>