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