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