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