one more identity action to process
[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 file contains functions needed to handle mime messages.
10 *
11 * @version $Id$
12 * @package squirrelmail
13 * @subpackage mime
14 * @since 1.3.2
15 */
16
17 /**
18 * The object that contains a message
19 *
20 * message is the object that contains messages. It is a recursive
21 * object in that through the $entities variable, it can contain
22 * more objects of type message. See documentation in mime.txt for
23 * a better description of how this works.
24 * @package squirrelmail
25 * @subpackage mime
26 * @since 1.3.0
27 */
28 class Message {
29 /**
30 * rfc822header object
31 * @var object
32 */
33 var $rfc822_header = '';
34 /**
35 * MessageHeader object
36 * @var object
37 */
38 var $mime_header = '';
39 /**
40 * @var mixed
41 */
42 var $flags = '';
43 /**
44 * Media type
45 * @var string
46 */
47 var $type0='';
48 /**
49 * Media subtype
50 * @var string
51 */
52 var $type1='';
53 /**
54 * Nested mime parts
55 * @var array
56 */
57 var $entities = array();
58 /**
59 * Message part id
60 * @var string
61 */
62 var $entity_id = '';
63 /**
64 * Parent message part id
65 * @var string
66 */
67 var $parent_ent;
68 /**
69 * @var mixed
70 */
71 var $entity;
72 /**
73 * @var mixed
74 */
75 var $parent = '';
76 /**
77 * @var string
78 */
79 var $decoded_body='';
80 /**
81 * Message \seen status
82 * @var boolean
83 */
84 var $is_seen = 0;
85 /**
86 * Message \answered status
87 * @var boolean
88 */
89 var $is_answered = 0;
90 /**
91 * Message \deleted status
92 * @var boolean
93 */
94 var $is_deleted = 0;
95 /**
96 * Message \flagged status
97 * @var boolean
98 */
99 var $is_flagged = 0;
100 /**
101 * Message mdn status
102 * @var boolean
103 */
104 var $is_mdnsent = 0;
105 /**
106 * Message text body
107 * @var string
108 */
109 var $body_part = '';
110 /**
111 * Message part offset
112 * for fetching body parts out of raw messages
113 * @var integer
114 */
115 var $offset = 0;
116 /**
117 * Message part length
118 * for fetching body parts out of raw messages
119 * @var integer
120 */
121 var $length = 0;
122 /**
123 * Local attachment filename
124 * location where the tempory attachment
125 * is stored. For use in delivery class.
126 * @var string
127 */
128 var $att_local_name = '';
129
130 /**
131 * @param string $ent entity id
132 */
133 function setEnt($ent) {
134 $this->entity_id= $ent;
135 }
136
137 /**
138 * Add nested message part
139 * @param object $msg
140 */
141 function addEntity ($msg) {
142 $this->entities[] = $msg;
143 }
144
145 /**
146 * Get file name used for mime part
147 * @return string file name
148 * @since 1.3.2
149 */
150 function getFilename() {
151 $filename = '';
152 $header = $this->header;
153 if (is_object($header->disposition)) {
154 $filename = $header->disposition->getProperty('filename');
155 if (trim($filename) == '') {
156 $name = decodeHeader($header->disposition->getProperty('name'));
157 if (!trim($name)) {
158 $name = $header->getParameter('name');
159 if(!trim($name)) {
160 if (!trim( $header->id )) {
161 $filename = 'untitled-[' . $this->entity_id . ']' ;
162 } else {
163 $filename = 'cid: ' . $header->id;
164 }
165 } else {
166 $filename = $name;
167 }
168 } else {
169 $filename = $name;
170 }
171 }
172 } else {
173 $filename = $header->getParameter('filename');
174 if (!trim($filename)) {
175 $filename = $header->getParameter('name');
176 if (!trim($filename)) {
177 if (!trim( $header->id )) {
178 $filename = 'untitled-[' . $this->entity_id . ']' ;
179 } else {
180 $filename = 'cid: ' . $header->id;
181 }
182 }
183 }
184 }
185 return $filename;
186 }
187
188 /**
189 * Add header object to message object.
190 * WARNING: Unfinished code. Don't expect it to work in older sm versions.
191 * @param mixed $read array or string with message headers
192 * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object
193 */
194 function addRFC822Header($read) {
195 $header = new Rfc822Header();
196 $this->rfc822_header = $header->parseHeader($read);
197 }
198
199 /**
200 * @param string $ent
201 * @return mixed (object or string?)
202 */
203 function getEntity($ent) {
204 $cur_ent = $this->entity_id;
205 $msg = $this;
206 if (($cur_ent == '') || ($cur_ent == '0')) {
207 $cur_ent_a = array();
208 } else {
209 $cur_ent_a = explode('.', $this->entity_id);
210 }
211 $ent_a = explode('.', $ent);
212
213 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++$i) {
214 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
215 $msg = $msg->parent;
216 $cur_ent_a = explode('.', $msg->entity_id);
217 --$i;
218 } else if (!isset($cur_ent_a[$i])) {
219 if (isset($msg->entities[($ent_a[$i]-1)])) {
220 $msg = $msg->entities[($ent_a[$i]-1)];
221 } else {
222 $msg = $msg->entities[0];
223 }
224 }
225 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
226 /*this is a header for a message/rfc822 entity */
227 $msg = $msg->entities[0];
228 }
229 }
230
231 if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
232 /*this is a header for a message/rfc822 entity */
233 $msg = $msg->entities[0];
234 }
235
236 if (isset($msg->entities[($ent_a[$entCount])-1])) {
237 if (is_object($msg->entities[($ent_a[$entCount])-1])) {
238 $msg = $msg->entities[($ent_a[$entCount]-1)];
239 }
240 }
241
242 return $msg;
243 }
244
245 /**
246 * Set message body
247 * @param string $s message body
248 */
249 function setBody($s) {
250 $this->body_part = $s;
251 }
252
253 /**
254 * Clean message object
255 */
256 function clean_up() {
257 $msg = $this;
258 $msg->body_part = '';
259
260 foreach ($msg->entities as $m) {
261 $m->clean_up();
262 }
263 }
264
265 /**
266 * @return string
267 */
268 function getMailbox() {
269 $msg = $this;
270 while (is_object($msg->parent)) {
271 $msg = $msg->parent;
272 }
273 return $msg->mailbox;
274 }
275
276 /*
277 * Bodystructure parser, a recursive function for generating the
278 * entity-tree with all the mime-parts.
279 *
280 * It follows RFC2060 and stores all the described fields in the
281 * message object.
282 *
283 * Question/Bugs:
284 *
285 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
286 * @param string $read
287 * @param integer $i
288 * @param mixed $sub_msg
289 * @return object Message object
290 * @todo define argument and return types
291 */
292 function parseStructure($read, &$i, $sub_msg = '') {
293 $msg = Message::parseBodyStructure($read, $i, $sub_msg);
294 if($msg) $msg->setEntIds($msg,false,0);
295 return $msg;
296 }
297
298 /**
299 * @param object $msg
300 * @param mixed $init
301 * @param integer $i
302 * @todo document me
303 * @since 1.4.0
304 */
305 function setEntIds(&$msg,$init=false,$i=0) {
306 $iCnt = count($msg->entities);
307 if ($init !==false) {
308 $iEntSub = $i+1;
309 if ($msg->parent->type0 == 'message' &&
310 $msg->parent->type1 == 'rfc822' &&
311 $msg->type0 == 'multipart') {
312 $iEntSub = '0';
313 }
314 if ($init) {
315 $msg->entity_id = "$init.$iEntSub";
316 } else {
317 $msg->entity_id = $iEntSub;
318 }
319 } else if ($iCnt) {
320 $msg->entity_id='0';
321 } else {
322 $msg->entity_id='1';
323 }
324 for ($i=0;$i<$iCnt;++$i) {
325 $msg->entities[$i]->parent =& $msg;
326 if (strrchr($msg->entity_id, '.') != '.0') {
327 $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->entity_id,$i);
328 } else {
329 $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->parent->entity_id,$i);
330 }
331 }
332 }
333
334 /**
335 * @param string $read
336 * @param integer $i
337 * @param mixed $sub_msg
338 * @return object Message object
339 * @todo document me
340 * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
341 */
342 function parseBodyStructure($read, &$i, $sub_msg = '') {
343 $arg_no = 0;
344 $arg_a = array();
345 if ($sub_msg) {
346 $message = $sub_msg;
347 } else {
348 $message = new Message();
349 }
350
351 for ($cnt = strlen($read); $i < $cnt; ++$i) {
352 $char = strtoupper($read{$i});
353 switch ($char) {
354 case '(':
355 switch($arg_no) {
356 case 0:
357 if (!isset($msg)) {
358 $msg = new Message();
359 $hdr = new MessageHeader();
360 $hdr->type0 = 'text';
361 $hdr->type1 = 'plain';
362 $hdr->encoding = 'us-ascii';
363 } else {
364 $msg->header->type0 = 'multipart';
365 $msg->type0 = 'multipart';
366 while ($read{$i} == '(') {
367 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
368 }
369 }
370 break;
371 case 1:
372 /* multipart properties */
373 ++$i;
374 $arg_a[] = $msg->parseProperties($read, $i);
375 ++$arg_no;
376 break;
377 case 2:
378 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
379 ++$i;
380 $arg_a[] = $msg->parseDisposition($read, $i);
381 } else { /* properties */
382 $arg_a[] = $msg->parseProperties($read, $i);
383 }
384 ++$arg_no;
385 break;
386 case 3:
387 if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
388 ++$i;
389 $arg_a[]= $msg->parseLanguage($read, $i);
390 }
391 case 7:
392 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
393 $msg->header->type0 = $arg_a[0];
394 $msg->header->type1 = $arg_a[1];
395 $msg->type0 = $arg_a[0];
396 $msg->type1 = $arg_a[1];
397 $rfc822_hdr = new Rfc822Header();
398 $msg->rfc822_header = $msg->parseEnvelope($read, $i, $rfc822_hdr);
399 while (($i < $cnt) && ($read{$i} != '(')) {
400 ++$i;
401 }
402 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
403 }
404 break;
405 case 8:
406 ++$i;
407 $arg_a[] = $msg->parseDisposition($read, $i);
408 ++$arg_no;
409 break;
410 case 9:
411 ++$i;
412 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
413 $arg_a[] = $msg->parseDisposition($read, $i);
414 } else {
415 $arg_a[] = $msg->parseLanguage($read, $i);
416 }
417 ++$arg_no;
418 break;
419 case 10:
420 if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
421 ++$i;
422 $arg_a[] = $msg->parseLanguage($read, $i);
423 } else {
424 $i = $msg->parseParenthesis($read, $i);
425 $arg_a[] = ''; /* not yet described in rfc2060 */
426 }
427 ++$arg_no;
428 break;
429 default:
430 /* unknown argument, skip this part */
431 $i = $msg->parseParenthesis($read, $i);
432 $arg_a[] = '';
433 ++$arg_no;
434 break;
435 } /* switch */
436 break;
437 case '"':
438 /* inside an entity -> start processing */
439 $arg_s = $msg->parseQuote($read, $i);
440 ++$arg_no;
441 if ($arg_no < 3) {
442 $arg_s = strtolower($arg_s); /* type0 and type1 */
443 }
444 $arg_a[] = $arg_s;
445 break;
446 case 'n':
447 case 'N':
448 /* probably NIL argument */
449 $tmpnil = strtoupper(substr($read, $i, 4));
450 if ($tmpnil == 'NIL ' || $tmpnil == 'NIL)') {
451 $arg_a[] = '';
452 ++$arg_no;
453 $i += 2;
454 }
455 break;
456 case '{':
457 /* process the literal value */
458 $arg_a[] = $msg->parseLiteral($read, $i);
459 ++$arg_no;
460 break;
461 case '0':
462 case is_numeric($read{$i}):
463 /* process integers */
464 if ($read{$i} == ' ') { break; }
465 ++$arg_no;
466 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
467 $i += strlen($regs[1])-1;
468 $arg_a[] = $regs[1];
469 } else {
470 $arg_a[] = 0;
471 }
472 break;
473 case ')':
474 $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
475 if (!$multipart) {
476 $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
477 $hdr->type0 = $arg_a[0];
478 $hdr->type1 = $arg_a[1];
479
480 $msg->type0 = $arg_a[0];
481 $msg->type1 = $arg_a[1];
482 $arr = $arg_a[2];
483 if (is_array($arr)) {
484 $hdr->parameters = $arg_a[2];
485 }
486 $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
487 $hdr->description = $arg_a[4];
488 $hdr->encoding = strtolower($arg_a[5]);
489 $hdr->entity_id = $msg->entity_id;
490 $hdr->size = $arg_a[6];
491 if ($shifted_args) {
492 $hdr->lines = $arg_a[7];
493 $s = 1;
494 } else {
495 $s = 0;
496 }
497 $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
498 $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
499 $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
500 $msg->header = $hdr;
501 } else {
502 $hdr->type0 = 'multipart';
503 $hdr->type1 = $arg_a[0];
504 $msg->type0 = 'multipart';
505 $msg->type1 = $arg_a[0];
506 $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
507 $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
508 $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
509 $msg->header = $hdr;
510 }
511 return $msg;
512 default: break;
513 } /* switch */
514 } /* for */
515 } /* parsestructure */
516
517 /**
518 * @param string $read
519 * @param integer $i
520 * @return array
521 */
522 function parseProperties($read, &$i) {
523 $properties = array();
524 $prop_name = '';
525
526 for (; $read{$i} != ')'; ++$i) {
527 $arg_s = '';
528 if ($read{$i} == '"') {
529 $arg_s = $this->parseQuote($read, $i);
530 } else if ($read{$i} == '{') {
531 $arg_s = $this->parseLiteral($read, $i);
532 }
533
534 if ($arg_s != '') {
535 if ($prop_name == '') {
536 $prop_name = strtolower($arg_s);
537 $properties[$prop_name] = '';
538 } else if ($prop_name != '') {
539 $properties[$prop_name] = $arg_s;
540 $prop_name = '';
541 }
542 }
543 }
544 return $properties;
545 }
546
547 /**
548 * @param string $read
549 * @param integer $i
550 * @param object $hdr MessageHeader object
551 * @return object MessageHeader object
552 */
553 function parseEnvelope($read, &$i, $hdr) {
554 $arg_no = 0;
555 $arg_a = array();
556 ++$i;
557 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
558 $char = strtoupper($read{$i});
559 switch ($char) {
560 case '"':
561 $arg_a[] = $this->parseQuote($read, $i);
562 ++$arg_no;
563 break;
564 case '{':
565 $arg_a[] = $this->parseLiteral($read, $i);
566 /* temp bugfix (SM 1.5 will have a working clean version)
567 too much work to implement that version right now */
568 // --$i;
569 ++$arg_no;
570 break;
571 case 'N':
572 /* probably NIL argument */
573 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
574 $arg_a[] = '';
575 ++$arg_no;
576 $i += 2;
577 }
578 break;
579 case '(':
580 /* Address structure (with group support)
581 * Note: Group support is useless on SMTP connections
582 * because the protocol doesn't support it
583 */
584 $addr_a = array();
585 $group = '';
586 $a=0;
587 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
588 if ($read{$i} == '(') {
589 $addr = $this->parseAddress($read, $i);
590 if (($addr->host == '') && ($addr->mailbox != '')) {
591 /* start of group */
592 $group = $addr->mailbox;
593 $group_addr = $addr;
594 $j = $a;
595 } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
596 /* end group */
597 if ($a == ($j+1)) { /* no group members */
598 $group_addr->group = $group;
599 $group_addr->mailbox = '';
600 $group_addr->personal = "$group: Undisclosed recipients;";
601 $addr_a[] = $group_addr;
602 $group ='';
603 }
604 } else {
605 $addr->group = $group;
606 $addr_a[] = $addr;
607 }
608 ++$a;
609 }
610 }
611 $arg_a[] = $addr_a;
612 break;
613 default: break;
614 }
615 }
616
617 if (count($arg_a) > 9) {
618 $d = strtr($arg_a[0], array(' ' => ' '));
619 $d = explode(' ', $d);
620 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
621
622 $hdr->date = getTimeStamp($d); /* argument 1: date */
623 $hdr->subject = $arg_a[1]; /* argument 2: subject */
624 $hdr->from = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */
625 $hdr->sender = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */
626 $hdr->replyto = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */
627 $hdr->to = $arg_a[5]; /* argument 6: to */
628 $hdr->cc = $arg_a[6]; /* argument 7: cc */
629 $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
630 $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
631 $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
632 }
633 return $hdr;
634 }
635
636 /**
637 * @param string $read
638 * @param integer $i
639 * @return string
640 * @todo document me
641 */
642 function parseLiteral($read, &$i) {
643 $lit_cnt = '';
644 ++$i;
645 $iPos = strpos($read,'}',$i);
646 if ($iPos) {
647 $lit_cnt = substr($read, $i, $iPos - $i);
648 $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */
649 /* Now read the literal */
650 $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
651 $i += $lit_cnt;
652 /* temp bugfix (SM 1.5 will have a working clean version)
653 too much work to implement that version right now */
654 --$i;
655 } else { /* should never happen */
656 $i += 3; /* } + \r + \n */
657 $s = '';
658 }
659 return $s;
660 }
661
662 /**
663 * @param string $read
664 * @param integer $i
665 * @return string
666 * @todo document me
667 */
668 function parseQuote($read, &$i) {
669 $s = '';
670 $iPos = ++$i;
671 while (true) {
672 $iPos = strpos($read,'"',$iPos);
673 if (!$iPos) break;
674 if ($iPos && $read{$iPos -1} != '\\') {
675 $s = substr($read,$i,($iPos-$i));
676 $i = $iPos;
677 break;
678 }
679 ++$iPos;
680 if ($iPos > strlen($read)) {
681 break;
682 }
683 }
684 return $s;
685 }
686
687 /**
688 * @param string $read
689 * @param integer $i
690 * @return object AddressStructure object
691 */
692 function parseAddress($read, &$i) {
693 $arg_a = array();
694 for (; $read{$i} != ')'; ++$i) {
695 $char = strtoupper($read{$i});
696 switch ($char) {
697 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
698 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
699 case 'n':
700 case 'N':
701 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
702 $arg_a[] = '';
703 $i += 2;
704 }
705 break;
706 default: break;
707 }
708 }
709
710 if (count($arg_a) == 4) {
711 $adr = new AddressStructure();
712 $adr->personal = $arg_a[0];
713 $adr->adl = $arg_a[1];
714 $adr->mailbox = $arg_a[2];
715 $adr->host = $arg_a[3];
716 } else {
717 $adr = '';
718 }
719 return $adr;
720 }
721
722 /**
723 * @param string $read
724 * @param integer $i
725 * @param object Disposition object or empty string
726 */
727 function parseDisposition($read, &$i) {
728 $arg_a = array();
729 for (; $read{$i} != ')'; ++$i) {
730 switch ($read{$i}) {
731 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
732 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
733 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
734 default: break;
735 }
736 }
737
738 if (isset($arg_a[0])) {
739 $disp = new Disposition($arg_a[0]);
740 if (isset($arg_a[1])) {
741 $disp->properties = $arg_a[1];
742 }
743 }
744 return (is_object($disp) ? $disp : '');
745 }
746
747 /**
748 * @param string $read
749 * @param integer $i
750 * @return object Language object or empty string
751 */
752 function parseLanguage($read, &$i) {
753 /* no idea how to process this one without examples */
754 $arg_a = array();
755
756 for (; $read{$i} != ')'; ++$i) {
757 switch ($read{$i}) {
758 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
759 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
760 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
761 default: break;
762 }
763 }
764
765 if (isset($arg_a[0])) {
766 $lang = new Language($arg_a[0]);
767 if (isset($arg_a[1])) {
768 $lang->properties = $arg_a[1];
769 }
770 }
771 return (is_object($lang) ? $lang : '');
772 }
773
774 /**
775 * Parse message text enclosed in parenthesis
776 * @param string $read
777 * @param integer $i
778 * @return integer
779 */
780 function parseParenthesis($read, $i) {
781 for (; $read{$i} != ')'; ++$i) {
782 switch ($read{$i}) {
783 case '"': $this->parseQuote($read, $i); break;
784 case '{': $this->parseLiteral($read, $i); break;
785 case '(': $this->parseProperties($read, $i); break;
786 default: break;
787 }
788 }
789 return $i;
790 }
791
792 /**
793 * Function to fill the message structure in case the
794 * bodystructure is not available
795 * NOT FINISHED YET
796 * @param string $read
797 * @param string $type0 message part type
798 * @param string $type1 message part subtype
799 * @return string (only when type0 is not message or multipart)
800 */
801 function parseMessage($read, $type0, $type1) {
802 switch ($type0) {
803 case 'message':
804 $rfc822_header = true;
805 $mime_header = false;
806 break;
807 case 'multipart':
808 $rfc822_header = false;
809 $mime_header = true;
810 break;
811 default: return $read;
812 }
813
814 for ($i = 1; $i < $count; ++$i) {
815 $line = trim($body[$i]);
816 if (($mime_header || $rfc822_header) &&
817 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
818 $bnd = $reg[1];
819 $bndreg = $bnd;
820 $bndreg = str_replace("\\", "\\\\", $bndreg);
821 $bndreg = str_replace("?", "\\?", $bndreg);
822 $bndreg = str_replace("+", "\\+", $bndreg);
823 $bndreg = str_replace(".", "\\.", $bndreg);
824 $bndreg = str_replace("/", "\\/", $bndreg);
825 $bndreg = str_replace("-", "\\-", $bndreg);
826 $bndreg = str_replace("(", "\\(", $bndreg);
827 $bndreg = str_replace(")", "\\)", $bndreg);
828 } else if ($rfc822_header && $line == '') {
829 $rfc822_header = false;
830 if ($msg->type0 == 'multipart') {
831 $mime_header = true;
832 }
833 }
834
835 if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
836 $cnt = count($boundaries)-1;
837 $bnd = $boundaries[$cnt]['bnd'];
838 $bndreg = $boundaries[$cnt]['bndreg'];
839
840 $regstr = '/^--'."($bndreg)".".*".'/';
841 if (preg_match($regstr, $line, $reg)) {
842 $bndlen = strlen($reg[1]);
843 $bndend = false;
844 if (strlen($line) > ($bndlen + 3)) {
845 if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
846 $bndend = true;
847 }
848 }
849 if ($bndend) {
850 /* calc offset and return $msg */
851 //$entStr = CalcEntity("$entStr", -1);
852 array_pop($boundaries);
853 $mime_header = true;
854 $bnd_end = true;
855 } else {
856 $mime_header = true;
857 $bnd_end = false;
858 //$entStr = CalcEntity("$entStr", 0);
859 ++$content_indx;
860 }
861 } else {
862 if ($header) { }
863 }
864 }
865 }
866 }
867
868 /**
869 * @param array $entity
870 * @param array $alt_order
871 * @param boolean $strict
872 * @return array
873 */
874 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
875 $found = false;
876 if ($this->type0 == 'multipart') {
877 if($this->type1 == 'alternative') {
878 $msg = $this->findAlternativeEntity($alt_order);
879 if (count($msg->entities) == 0) {
880 $entity[] = $msg->entity_id;
881 } else {
882 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
883 }
884 $found = true;
885 } else if ($this->type1 == 'related') { /* RFC 2387 */
886 $msgs = $this->findRelatedEntity();
887 foreach ($msgs as $msg) {
888 if (count($msg->entities) == 0) {
889 $entity[] = $msg->entity_id;
890 } else {
891 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
892 }
893 }
894 if (count($msgs) > 0) {
895 $found = true;
896 }
897 } else { /* Treat as multipart/mixed */
898 foreach ($this->entities as $ent) {
899 if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
900 (!isset($ent->header->parameters['filename'])) &&
901 (!isset($ent->header->parameters['name'])) &&
902 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
903 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
904 $found = true;
905 }
906 }
907 }
908 } else { /* If not multipart, then just compare with each entry from $alt_order */
909 $type = $this->type0.'/'.$this->type1;
910 // $alt_order[] = "message/rfc822";
911 foreach ($alt_order as $alt) {
912 if( ($alt == $type) && isset($this->entity_id) ) {
913 if ((count($this->entities) == 0) &&
914 (!isset($this->header->parameters['filename'])) &&
915 (!isset($this->header->parameters['name'])) &&
916 isset($this->header->disposition) && is_object($this->header->disposition) &&
917 !(is_object($this->header->disposition) && strtolower($this->header->disposition->name) == 'attachment')) {
918 $entity[] = $this->entity_id;
919 $found = true;
920 }
921 }
922 }
923 }
924 if(!$found) {
925 foreach ($this->entities as $ent) {
926 if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
927 (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
928 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
929 $found = true;
930 }
931 }
932 }
933 if(!$strict && !$found) {
934 if (($this->type0 == 'text') &&
935 in_array($this->type1, array('plain', 'html', 'message')) &&
936 isset($this->entity_id)) {
937 if (count($this->entities) == 0) {
938 if (!is_object($this->header->disposition) || strtolower($this->header->disposition->name) != 'attachment') {
939 $entity[] = $this->entity_id;
940 }
941 }
942 }
943 }
944 return $entity;
945 }
946
947 /**
948 * @param array $alt_order
949 * @return array
950 */
951 function findAlternativeEntity($alt_order) {
952 /* If we are dealing with alternative parts then we */
953 /* choose the best viewable message supported by SM. */
954 $best_view = 0;
955 $entity = array();
956 foreach($this->entities as $ent) {
957 $type = $ent->header->type0 . '/' . $ent->header->type1;
958 if ($type == 'multipart/related') {
959 $type = $ent->header->getParameter('type');
960 // Mozilla bug. Mozilla does not provide the parameter type.
961 if (!$type) $type = 'text/html';
962 }
963 $altCount = count($alt_order);
964 for ($j = $best_view; $j < $altCount; ++$j) {
965 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
966 $best_view = $j;
967 $entity = $ent;
968 }
969 }
970 }
971 return $entity;
972 }
973
974 /**
975 * @return array
976 */
977 function findRelatedEntity() {
978 $msgs = array();
979 $related_type = $this->header->getParameter('type');
980 // Mozilla bug. Mozilla does not provide the parameter type.
981 if (!$related_type) $related_type = 'text/html';
982 $entCount = count($this->entities);
983 for ($i = 0; $i < $entCount; ++$i) {
984 $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
985 if ($related_type == $type) {
986 $msgs[] = $this->entities[$i];
987 }
988 }
989 return $msgs;
990 }
991
992 /**
993 * @param array $exclude_id
994 * @param array $result
995 * @return array
996 */
997 function getAttachments($exclude_id=array(), $result = array()) {
998 /*
999 if (($this->type0 == 'message') &&
1000 ($this->type1 == 'rfc822') &&
1001 ($this->entity_id) ) {
1002 $this = $this->entities[0];
1003 }
1004 */
1005 if (count($this->entities)) {
1006 foreach ($this->entities as $entity) {
1007 $exclude = false;
1008 foreach ($exclude_id as $excl) {
1009 if ($entity->entity_id === $excl) {
1010 $exclude = true;
1011 }
1012 }
1013
1014 if (!$exclude) {
1015 if (($entity->type0 == 'multipart') &&
1016 ($entity->type1 != 'related')) {
1017 $result = $entity->getAttachments($exclude_id, $result);
1018 } else if ($entity->type0 != 'multipart') {
1019 $result[] = $entity;
1020 }
1021 }
1022 }
1023 } else {
1024 $exclude = false;
1025 foreach ($exclude_id as $excl) {
1026 $exclude = $exclude || ($this->entity_id == $excl);
1027 }
1028
1029 if (!$exclude) {
1030 $result[] = $this;
1031 }
1032 }
1033 return $result;
1034 }
1035
1036 /**
1037 * Add attachment to message object
1038 * @param string $type attachment type
1039 * @param string $name attachment name
1040 * @param string $location path to attachment
1041 */
1042 function initAttachment($type, $name, $location) {
1043 $attachment = new Message();
1044 $mime_header = new MessageHeader();
1045 $mime_header->setParameter('name', $name);
1046 // FIXME: duplicate code. see ContentType class
1047 $pos = strpos($type, '/');
1048 if ($pos > 0) {
1049 $mime_header->type0 = substr($type, 0, $pos);
1050 $mime_header->type1 = substr($type, $pos+1);
1051 } else {
1052 $mime_header->type0 = $type;
1053 }
1054 $attachment->att_local_name = $location;
1055 $disposition = new Disposition('attachment');
1056 $disposition->properties['filename'] = $name;
1057 $mime_header->disposition = $disposition;
1058 $attachment->mime_header = $mime_header;
1059 $this->entities[]=$attachment;
1060 }
1061 }
1062
1063 ?>