Improved entity_id calculation.
[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 $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 $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
175 function parseBodyStructure($read, &$i, $sub_msg = '') {
176 $arg_no = 0;
177 $arg_a = array();
178 if ($sub_msg) {
179 $message = $sub_msg;
180 } else {
181 $message = new Message();
182 }
183 $this = $message;
184 for ($cnt = strlen($read); $i < $cnt; ++$i) {
185 $char = strtoupper($read{$i});
186 switch ($char) {
187 case '(':
188 switch($arg_no) {
189 case 0:
190 if (!isset($msg)) {
191 $msg = new Message();
192 $hdr = new MessageHeader();
193 $hdr->type0 = 'text';
194 $hdr->type1 = 'plain';
195 $hdr->encoding = 'us-ascii';
196 } else {
197 $msg->header->type0 = 'multipart';
198 $msg->type0 = 'multipart';
199 while ($read{$i} == '(') {
200 $msg->addEntity($this->parseBodyStructure($read, $i, $msg));
201 }
202 }
203 break;
204 case 1:
205 /* multipart properties */
206 ++$i;
207 $arg_a[] = $this->parseProperties($read, $i);
208 ++$arg_no;
209 break;
210 case 2:
211 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
212 ++$i;
213 $arg_a[] = $this->parseDisposition($read, $i);
214 } else { /* properties */
215 $arg_a[] = $this->parseProperties($read, $i);
216 }
217 ++$arg_no;
218 break;
219 case 3:
220 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
221 ++$i;
222 $arg_a[]= $this->parseLanguage($read, $i);
223 }
224 case 7:
225 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
226 $msg->header->type0 = $arg_a[0];
227 $msg->header->type1 = $arg_a[1];
228 $msg->type0 = $arg_a[0];
229 $msg->type1 = $arg_a[1];
230 $rfc822_hdr = new Rfc822Header();
231 $msg->rfc822_header = $this->parseEnvelope($read, $i, $rfc822_hdr);
232 while (($i < $cnt) && ($read{$i} != '(')) {
233 ++$i;
234 }
235 $msg->addEntity($this->parseBodyStructure($read, $i,$msg));
236 }
237 break;
238 case 8:
239 ++$i;
240 $arg_a[] = $this->parseDisposition($read, $i);
241 ++$arg_no;
242 break;
243 case 9:
244 ++$i;
245 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
246 $arg_a[] = $this->parseDisposition($read, $i);
247 } else {
248 $arg_a[] = $this->parseLanguage($read, $i);
249 }
250 ++$arg_no;
251 break;
252 case 10:
253 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
254 ++$i;
255 $arg_a[] = $this->parseLanguage($read, $i);
256 } else {
257 $i = $this->parseParenthesis($read, $i);
258 $arg_a[] = ''; /* not yet described in rfc2060 */
259 }
260 ++$arg_no;
261 break;
262 default:
263 /* unknown argument, skip this part */
264 $i = $this->parseParenthesis($read, $i);
265 $arg_a[] = '';
266 ++$arg_no;
267 break;
268 } /* switch */
269 break;
270 case '"':
271 /* inside an entity -> start processing */
272 $arg_s = $this->parseQuote($read, $i);
273 ++$arg_no;
274 if ($arg_no < 3) {
275 $arg_s = strtolower($arg_s); /* type0 and type1 */
276 }
277 $arg_a[] = $arg_s;
278 break;
279 case 'n':
280 case 'N':
281 /* probably NIL argument */
282 if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
283 $arg_a[] = '';
284 ++$arg_no;
285 $i += 2;
286 }
287 break;
288 case '{':
289 /* process the literal value */
290 $arg_s = $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
378 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
379 ++$i;
380 $char = strtoupper($read{$i});
381 switch ($char) {
382 case '"':
383 $arg_a[] = $this->parseQuote($read, $i);
384 ++$arg_no;
385 break;
386 case '{':
387 $arg_a[] = $this->parseLiteral($read, $i);
388 ++$arg_no;
389 break;
390 case 'N':
391 /* probably NIL argument */
392 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
393 $arg_a[] = '';
394 ++$arg_no;
395 $i += 2;
396 }
397 break;
398 case '(':
399 /* Address structure (with group support)
400 * Note: Group support is useless on SMTP connections
401 * because the protocol doesn't support it
402 */
403 $addr_a = array();
404 $group = '';
405 $a=0;
406 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
407 if ($read{$i} == '(') {
408 $addr = $this->parseAddress($read, $i);
409 if (($addr->host == '') && ($addr->mailbox != '')) {
410 /* start of group */
411 $group = $addr->mailbox;
412 $group_addr = $addr;
413 $j = $a;
414 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
415 /* end group */
416 if ($a == ($j+1)) { /* no group members */
417 $group_addr->group = $group;
418 $group_addr->mailbox = '';
419 $group_addr->personal = "$group: Undisclosed recipients;";
420 $addr_a[] = $group_addr;
421 $group ='';
422 }
423 } else {
424 $addr->group = $group;
425 $addr_a[] = $addr;
426 }
427 ++$a;
428 }
429 }
430 $arg_a[] = $addr_a;
431 break;
432 default: break;
433 }
434 }
435
436 if (count($arg_a) > 9) {
437 $d = strtr($arg_a[0], array(' ' => ' '));
438 $d = explode(' ', $d);
439 if (!$arg_a[1]) $arg_1[1] = _("(no subject)");
440
441 $hdr->date = getTimeStamp($d); /* argument 1: date */
442 $hdr->subject = $arg_a[1]; /* argument 2: subject */
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 }
452 return $hdr;
453 }
454
455 function parseLiteral($read, &$i) {
456 $lit_cnt = '';
457 ++$i;
458 $iPos = strpos($read,'}',$i);
459 if ($iPos) {
460 $lit_cnt = substr($read, $i, $iPos - $i);
461 $i += strlen($lit_cnt) + 3; /* } + \r + \n */
462 $lit_cnt -= 2; /* lit_cnt includes \r\n */
463 $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
464 $i += $lit_cnt;
465 } else { /* should never happen */
466 $i += 3; /* } + \r + \n */
467 $s = '';
468 }
469 return $s;
470 }
471
472 function parseQuote($read, &$i) {
473 $s = '';
474 $iPos = ++$i;
475 while (true) {
476 $iPos = strpos($read,'"',$iPos);
477 if ($iPos === false) break;
478 if ($iPos && $read{$iPos -1} != '\\') {
479 $s = substr($read,$i,($iPos-$i));
480 $i = $iPos;
481 break;
482 }
483 }
484 return $s;
485 }
486
487 function parseAddress($read, &$i) {
488 $arg_a = array();
489 for (; $read{$i} != ')'; ++$i) {
490 $char = strtoupper($read{$i});
491 switch ($char) {
492 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
493 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
494 case 'n':
495 case 'N':
496 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
497 $arg_a[] = '';
498 $i += 2;
499 }
500 break;
501 default: break;
502 }
503 }
504
505 if (count($arg_a) == 4) {
506 $adr = new AddressStructure();
507 $adr->personal = $arg_a[0];
508 $adr->adl = $arg_a[1];
509 $adr->mailbox = $arg_a[2];
510 $adr->host = $arg_a[3];
511 } else {
512 $adr = '';
513 }
514 return $adr;
515 }
516
517 function parseDisposition($read, &$i) {
518 $arg_a = array();
519 for (; $read{$i} != ')'; ++$i) {
520 switch ($read{$i}) {
521 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
522 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
523 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
524 default: break;
525 }
526 }
527
528 if (isset($arg_a[0])) {
529 $disp = new Disposition($arg_a[0]);
530 if (isset($arg_a[1])) {
531 $disp->properties = $arg_a[1];
532 }
533 }
534
535 return (is_object($disp) ? $disp : '');
536 }
537
538 function parseLanguage($read, &$i) {
539 /* no idea how to process this one without examples */
540 $arg_a = array();
541
542 for (; $read{$i} != ')'; ++$i) {
543 switch ($read{$i}) {
544 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
545 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
546 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
547 default: break;
548 }
549 }
550
551 if (isset($arg_a[0])) {
552 $lang = new Language($arg_a[0]);
553 if (isset($arg_a[1])) {
554 $lang->properties = $arg_a[1];
555 }
556 }
557
558 return (is_object($lang) ? $lang : '');
559 }
560
561 function parseParenthesis($read, $i) {
562 for (; $read{$i} != ')'; ++$i) {
563 switch ($read{$i}) {
564 case '"': $this->parseQuote($read, $i); break;
565 case '{': $this->parseLiteral($read, $i); break;
566 case '(': $this->parseProperties($read, $i); break;
567 default: break;
568 }
569 }
570 return $i;
571 }
572
573 /* Function to fill the message structure in case the */
574 /* bodystructure is not available NOT FINISHED YET */
575 function parseMessage($read, $type0, $type1) {
576 switch ($type0) {
577 case 'message':
578 $rfc822_header = true;
579 $mime_header = false;
580 break;
581 case 'multipart':
582 $rfc822_header = false;
583 $mime_header = true;
584 break;
585 default: return $read;
586 }
587
588 for ($i = 1; $i < $count; ++$i) {
589 $line = trim($body[$i]);
590 if (($mime_header || $rfc822_header) &&
591 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
592 $bnd = $reg[1];
593 $bndreg = $bnd;
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 $bndreg = str_replace("(", "\\(", $bndreg);
601 $bndreg = str_replace(")", "\\)", $bndreg);
602 } else if ($rfc822_header && $line == '') {
603 $rfc822_header = false;
604 if ($msg->type0 == 'multipart') {
605 $mime_header = true;
606 }
607 }
608
609 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
610 $cnt = count($boundaries)-1;
611 $bnd = $boundaries[$cnt]['bnd'];
612 $bndreg = $boundaries[$cnt]['bndreg'];
613
614 $regstr = '/^--'."($bndreg)".".*".'/';
615 if (preg_match($regstr, $line, $reg)) {
616 $bndlen = strlen($reg[1]);
617 $bndend = false;
618 if (strlen($line) > ($bndlen + 3)) {
619 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
620 $bndend = true;
621 }
622 }
623 if ($bndend) {
624 /* calc offset and return $msg */
625 //$entStr = CalcEntity("$entStr", -1);
626 array_pop($boundaries);
627 $mime_header = true;
628 $bnd_end = true;
629 } else {
630 $mime_header = true;
631 $bnd_end = false;
632 //$entStr = CalcEntity("$entStr", 0);
633 ++$content_indx;
634 }
635 } else {
636 if ($header) { }
637 }
638 }
639 }
640 }
641
642 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
643 $found = false;
644 if ($this->type0 == 'multipart') {
645 if($this->type1 == 'alternative') {
646 $msg = $this->findAlternativeEntity($alt_order);
647 if (count($msg->entities) == 0) {
648 $entity[] = $msg->entity_id;
649 } else {
650 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
651 }
652 $found = true;
653 } else if ($this->type1 == 'related') { /* RFC 2387 */
654 $msgs = $this->findRelatedEntity();
655 foreach ($msgs as $msg) {
656 if (count($msg->entities) == 0) {
657 $entity[] = $msg->entity_id;
658 } else {
659 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
660 }
661 }
662 if (count($msgs) > 0) {
663 $found = true;
664 }
665 } else { /* Treat as multipart/mixed */
666 foreach ($this->entities as $ent) {
667 if((strtolower($ent->header->disposition->name) != 'attachment') &&
668 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
669 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
670 $found = true;
671 }
672 }
673 }
674 } else { /* If not multipart, then just compare with each entry from $alt_order */
675 $type = $this->type0.'/'.$this->type1;
676 // $alt_order[] = "message/rfc822";
677 foreach ($alt_order as $alt) {
678 if( ($alt == $type) && isset($this->entity_id) ) {
679 if ((count($this->entities) == 0) &&
680 (strtolower($this->header->disposition->name) != 'attachment')) {
681 $entity[] = $this->entity_id;
682 $found = true;
683 }
684 }
685 }
686 }
687 if(!$found) {
688 foreach ($this->entities as $ent) {
689 if((strtolower($ent->header->disposition->name) != 'attachment') &&
690 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
691 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
692 $found = true;
693 }
694 }
695
696 }
697 if(!$strict && !$found) {
698 if (($this->type0 == 'text') &&
699 in_array($this->type1, array('plain', 'html', 'message')) &&
700 isset($this->entity_id)) {
701 if (count($this->entities) == 0) {
702 if (strtolower($this->header->disposition->name) != 'attachment') {
703 $entity[] = $this->entity_id;
704 }
705 }
706 }
707 }
708 return $entity;
709 }
710
711 function findAlternativeEntity($alt_order) {
712 /* If we are dealing with alternative parts then we */
713 /* choose the best viewable message supported by SM. */
714 $best_view = 0;
715 $entity = array();
716 foreach($this->entities as $ent) {
717 $type = $ent->header->type0 . '/' . $ent->header->type1;
718 if ($type == 'multipart/related') {
719 $type = $ent->header->getParameter('type');
720 }
721 $altCount = count($alt_order);
722 for ($j = $best_view; $j < $altCount; ++$j) {
723 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
724 $best_view = $j;
725 $entity = $ent;
726 }
727 }
728 }
729
730 return $entity;
731 }
732
733 function findRelatedEntity() {
734 $msgs = array();
735
736 $entCount = count($this->entities);
737 for ($i = 0; $i < $entCount; ++$i) {
738 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
739 if ($this->header->getParameter('type') == $type) {
740 $msgs[] = $this->entities[$i];
741 }
742 }
743
744 return $msgs;
745 }
746
747 function getAttachments($exclude_id=array(), $result = array()) {
748 /*
749 if (($this->type0 == 'message') &&
750 ($this->type1 == 'rfc822') &&
751 ($this->entity_id) ) {
752 $this = $this->entities[0];
753 }
754 */
755 if (count($this->entities)) {
756 foreach ($this->entities as $entity) {
757 $exclude = false;
758 foreach ($exclude_id as $excl) {
759 if ($entity->entity_id === $excl) {
760 $exclude = true;
761 }
762 }
763
764 if (!$exclude) {
765 if (($entity->type0 == 'multipart') &&
766 ($entity->type1 != 'related')) {
767 $result = $entity->getAttachments($exclude_id, $result);
768 } else if ($entity->type0 != 'multipart') {
769 $result[] = $entity;
770 }
771 }
772 }
773 } else {
774 $exclude = false;
775 foreach ($exclude_id as $excl) {
776 $exclude = $exclude || ($this->entity_id == $excl);
777 }
778
779 if (!$exclude) {
780 $result[] = $this;
781 }
782 }
783 return $result;
784 }
785
786 function initAttachment($type, $name, $location) {
787 $attachment = new Message();
788 $mime_header = new MessageHeader();
789 $mime_header->setParameter('name', $name);
790 $pos = strpos($type, '/');
791 if ($pos > 0) {
792 $mime_header->type0 = substr($type, 0, $pos);
793 $mime_header->type1 = substr($type, $pos+1);
794 } else {
795 $mime_header->type0 = $type;
796 }
797 $attachment->att_local_name = $location;
798 $disposition = new Disposition('attachment');
799 $disposition->properties['filename'] = $name;
800 $mime_header->disposition = $disposition;
801 $attachment->mime_header = $mime_header;
802 $this->entities[]=$attachment;
803 }
804 }
805
806 ?>