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