6 * This file contains functions needed to handle mime messages.
8 * @copyright © 2003-2009 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
17 * The object that contains a message.
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
23 * @package squirrelmail
32 var $rfc822_header = '';
34 * MessageHeader object
37 var $mime_header = '';
56 var $entities = array();
63 * Parent message part id
80 * Message \seen status
85 * Message \answered status
90 * Message forward status
93 var $is_forwarded = 0;
95 * Message \deleted status
100 * Message \flagged status
115 * Message part offset
116 * for fetching body parts out of raw messages
121 * Message part length
122 * for fetching body parts out of raw messages
127 * Local attachment filename location where the tempory attachment is
128 * stored. For use in delivery class.
131 var $att_local_name = '';
134 * @param string $ent entity id
136 function setEnt($ent) {
137 $this->entity_id
= $ent;
141 * Add nested message part
144 function addEntity ($msg) {
145 $this->entities
[] = $msg;
149 * Get file name used for mime part
150 * @return string file name
153 function getFilename() {
155 $header = $this->header
;
156 if (is_object($header->disposition
)) {
157 $filename = $header->disposition
->getProperty('filename');
158 if (trim($filename) == '') {
159 $name = decodeHeader($header->disposition
->getProperty('name'));
161 $name = $header->getParameter('name');
163 if (!trim( $header->id
)) {
164 $filename = 'untitled-[' . $this->entity_id
. ']' ;
166 $filename = 'cid: ' . $header->id
;
176 $filename = $header->getParameter('filename');
177 if (!trim($filename)) {
178 $filename = $header->getParameter('name');
179 if (!trim($filename)) {
180 if (!trim( $header->id
)) {
181 $filename = 'untitled-[' . $this->entity_id
. ']' ;
183 $filename = 'cid: ' . $header->id
;
192 * Add header object to message object.
193 * WARNING: Unfinished code. Don't expect it to work in older sm versions.
194 * @param mixed $read array or string with message headers
195 * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object
197 function addRFC822Header($read) {
198 $header = new Rfc822Header();
199 $this->rfc822_header
= $header->parseHeader($read);
204 * @return mixed (object or string?)
206 function getEntity($ent) {
207 $cur_ent = $this->entity_id
;
209 if (($cur_ent == '') ||
($cur_ent == '0')) {
210 $cur_ent_a = array();
212 $cur_ent_a = explode('.', $this->entity_id
);
214 $ent_a = explode('.', $ent);
216 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
217 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
219 $cur_ent_a = explode('.', $msg->entity_id
);
221 } else if (!isset($cur_ent_a[$i])) {
222 if (isset($msg->entities
[($ent_a[$i]-1)])) {
223 $msg = $msg->entities
[($ent_a[$i]-1)];
225 $msg = $msg->entities
[0];
228 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
229 /*this is a header for a message/rfc822 entity */
230 $msg = $msg->entities
[0];
234 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
235 /*this is a header for a message/rfc822 entity */
236 $msg = $msg->entities
[0];
239 if (isset($msg->entities
[($ent_a[$entCount])-1])) {
240 if (is_object($msg->entities
[($ent_a[$entCount])-1])) {
241 $msg = $msg->entities
[($ent_a[$entCount]-1)];
250 * @param string $s message body
252 function setBody($s) {
253 $this->body_part
= $s;
257 * Clean message object
259 function clean_up() {
261 $msg->body_part
= '';
263 foreach ($msg->entities
as $m) {
271 function getMailbox() {
273 while (is_object($msg->parent
)) {
276 return $msg->mailbox
;
280 * Bodystructure parser, a recursive function for generating the
281 * entity-tree with all the mime-parts.
283 * It follows RFC2060 and stores all the described fields in the
288 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
289 * @param string $read
291 * @param mixed $sub_msg
292 * @return object Message object
293 * @todo define argument and return types
295 function parseStructure($read, &$i, $sub_msg = '') {
296 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
297 if($msg) $msg->setEntIds($msg,false,0);
308 function setEntIds(&$msg,$init=false,$i=0) {
309 $iCnt = count($msg->entities
);
310 if ($init !==false) {
312 if ($msg->parent
->type0
== 'message' &&
313 $msg->parent
->type1
== 'rfc822' &&
314 $msg->type0
== 'multipart') {
318 $msg->entity_id
= "$init.$iEntSub";
320 $msg->entity_id
= $iEntSub;
327 for ($i=0;$i<$iCnt;++
$i) {
328 $msg->entities
[$i]->parent
=& $msg;
329 if (strrchr($msg->entity_id
, '.') != '.0') {
330 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
332 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
338 * @param string $read
340 * @param mixed $sub_msg
341 * @return object Message object
343 * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
345 function parseBodyStructure($read, &$i, $sub_msg = '') {
351 $message = new Message();
354 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
355 $char = strtoupper($read{$i});
361 $msg = new Message();
362 $hdr = new MessageHeader();
363 $hdr->type0
= 'text';
364 $hdr->type1
= 'plain';
365 $hdr->encoding
= '7bit';
367 $msg->header
->type0
= 'multipart';
368 $msg->type0
= 'multipart';
369 while ($read{$i} == '(') {
370 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
375 /* multipart properties */
377 $arg_a[] = $msg->parseProperties($read, $i);
381 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
383 $arg_a[] = $msg->parseDisposition($read, $i);
384 } else { /* properties */
385 $arg_a[] = $msg->parseProperties($read, $i);
390 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
392 $arg_a[]= $msg->parseLanguage($read, $i);
395 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
396 $msg->header
->type0
= $arg_a[0];
397 $msg->header
->type1
= $arg_a[1];
398 $msg->type0
= $arg_a[0];
399 $msg->type1
= $arg_a[1];
400 $rfc822_hdr = new Rfc822Header();
401 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
402 while (($i < $cnt) && ($read{$i} != '(')) {
405 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
410 $arg_a[] = $msg->parseDisposition($read, $i);
415 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
416 $arg_a[] = $msg->parseDisposition($read, $i);
418 $arg_a[] = $msg->parseLanguage($read, $i);
423 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
425 $arg_a[] = $msg->parseLanguage($read, $i);
427 $i = $msg->parseParenthesis($read, $i);
428 $arg_a[] = ''; /* not yet described in rfc2060 */
433 /* unknown argument, skip this part */
434 $i = $msg->parseParenthesis($read, $i);
441 /* inside an entity -> start processing */
442 $arg_s = $msg->parseQuote($read, $i);
445 $arg_s = strtolower($arg_s); /* type0 and type1 */
451 /* probably NIL argument */
452 $tmpnil = strtoupper(substr($read, $i, 4));
453 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
460 /* process the literal value */
461 $arg_a[] = $msg->parseLiteral($read, $i);
465 case is_numeric($read{$i}):
466 /* process integers */
467 if ($read{$i} == ' ') { break; }
469 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
470 $i +
= strlen($regs[1])-1;
477 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
479 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
480 $hdr->type0
= $arg_a[0];
481 $hdr->type1
= $arg_a[1];
483 $msg->type0
= $arg_a[0];
484 $msg->type1
= $arg_a[1];
486 if (is_array($arr)) {
487 $hdr->parameters
= $arg_a[2];
489 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
490 $hdr->description
= $arg_a[4];
491 $hdr->encoding
= strtolower($arg_a[5]);
492 $hdr->entity_id
= $msg->entity_id
;
493 $hdr->size
= $arg_a[6];
495 $hdr->lines
= $arg_a[7];
500 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
501 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
502 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
505 $hdr->type0
= 'multipart';
506 $hdr->type1
= $arg_a[0];
507 $msg->type0
= 'multipart';
508 $msg->type1
= $arg_a[0];
509 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
510 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
511 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
518 } /* parsestructure */
521 * @param string $read
525 function parseProperties($read, &$i) {
526 $properties = array();
529 for (; $read{$i} != ')'; ++
$i) {
531 if ($read{$i} == '"') {
532 $arg_s = $this->parseQuote($read, $i);
533 } else if ($read{$i} == '{') {
534 $arg_s = $this->parseLiteral($read, $i);
538 if ($prop_name == '') {
539 $prop_name = strtolower($arg_s);
540 $properties[$prop_name] = '';
541 } else if ($prop_name != '') {
542 $properties[$prop_name] = $arg_s;
547 return $this->handleRfc2231($properties);
551 * Joins RFC-2231 continuations, converts encoding to RFC-2047 style
552 * @param array $properties
555 function handleRfc2231($properties) {
557 /* STAGE 1: look for multi-line parameters, convert to the single line
558 form, and normalize values */
561 foreach($properties as $key=>$value) {
562 /* Look for parameters followed by "*", a number, and an optional "*"
564 if (preg_match('/^(.*\*)(\d+)(|\*)$/', $key, $matches)) {
565 unset($properties[$key]);
566 $prop_name = $matches[1];
567 if (!isset($cont[$prop_name])) $cont[$prop_name] = array();
569 /* An asterisk at the end of parameter name indicates that there
570 may be an encoding information present, and the parameter
571 value is percent-hex encoded. If parameter is not encoded, we
572 encode it to simplify further processing.
574 if ($matches[3] == '') $value = rawurlencode($value);
575 /* Use the number from parameter name as segment index */
576 $cont[$prop_name][$matches[2]] = $value;
579 foreach($cont as $key=>$values) {
580 /* Sort segments of multi-line parameters by index number. */
582 /* Join segments. We can do it safely, because:
583 - All segments are encoded.
584 - Per RFC-2231, chapter 4.1 notes.
586 $value = implode('', $values);
587 /* Add language and character set field delimiters if not present,
588 as required per RFC-2231, chapter 4.1, note #5. */
589 if (strpos($value, "'") === false) $value = "''".$value;
590 $properties[$key] = $value;
593 /* STAGE 2: Convert single line RFC-2231 encoded parameters, and
594 previously converted multi-line parameters, to RFC-2047 encoding */
596 foreach($properties as $key=>$value) {
597 if ($idx = strpos($key, '*')) {
598 unset($properties[$key]);
599 /* Extract the charset & language. */
600 $charset = substr($value,0,strpos($value,"'"));
601 $value = substr($value,strlen($charset)+
1);
602 $language = substr($value,0,strpos($value,"'"));
603 $value = substr($value,strlen($language)+
1);
604 /* No character set defaults to US-ASCII */
605 if (!$charset) $charset = 'US-ASCII';
606 if ($language) $language = '*'.$language;
607 /* Convert to RFC-2047 base64 encoded string. */
608 $properties[substr($key, 0, $idx)] = '=?'.$charset.$language.'?B?'.base64_encode(rawurldecode($value)).'?=';
615 * @param string $read
617 * @param object $hdr MessageHeader object
618 * @return object MessageHeader object
620 function parseEnvelope($read, &$i, $hdr) {
624 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
625 $char = strtoupper($read{$i});
628 $arg_a[] = $this->parseQuote($read, $i);
632 $arg_a[] = $this->parseLiteral($read, $i);
633 /* temp bugfix (SM 1.5 will have a working clean version)
634 too much work to implement that version right now */
639 /* probably NIL argument */
640 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
647 /* Address structure (with group support)
648 * Note: Group support is useless on SMTP connections
649 * because the protocol doesn't support it
654 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
655 if ($read{$i} == '(') {
656 $addr = $this->parseAddress($read, $i);
657 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
659 $group = $addr->mailbox
;
662 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
664 if ($a == ($j+
1)) { /* no group members */
665 $group_addr->group
= $group;
666 $group_addr->mailbox
= '';
667 $group_addr->personal
= "$group: Undisclosed recipients;";
668 $addr_a[] = $group_addr;
672 $addr->group
= $group;
684 if (count($arg_a) > 9) {
685 $d = strtr($arg_a[0], array(' ' => ' '));
686 $d_parts = explode(' ', $d);
687 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
689 $hdr->date
= getTimeStamp($d_parts); /* argument 1: date */
690 $hdr->date_unparsed
= strtr($d,'<>',' '); /* original date */
691 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
692 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
693 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
694 $hdr->reply_to
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
695 $hdr->to
= $arg_a[5]; /* argument 6: to */
696 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
697 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
698 $hdr->in_reply_to
= $arg_a[8]; /* argument 9: in-reply-to */
699 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
705 * @param string $read
710 function parseLiteral($read, &$i) {
713 $iPos = strpos($read,'}',$i);
715 $lit_cnt = substr($read, $i, $iPos - $i);
716 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
717 /* Now read the literal */
718 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
720 /* temp bugfix (SM 1.5 will have a working clean version)
721 too much work to implement that version right now */
723 } else { /* should never happen */
724 $i +
= 3; /* } + \r + \n */
731 * function parseQuote
733 * This extract the string value from a quoted string. After the end-quote
734 * character is found it returns the string. The offset $i when calling
735 * this function points to the first double quote. At the end it points to
736 * The ending quote. This function takes care of escaped double quotes.
739 * initial $i end position $i
741 * @param string $read
742 * @param integer $i offset in $read
743 * @return string string inbetween the double quotes
744 * @author Marc Groot Koerkamp
746 function parseQuote($read, &$i) {
751 $iPos = strpos($read,'"',$iPos);
753 if ($iPos && $read{$iPos -1} != '\\') {
754 $s = substr($read,$i,($iPos-$i));
757 } else if ($iPos > 1 && $read{$iPos -1} == '\\' && $read{$iPos-2} == '\\') {
758 // This is an unique situation where the fast detection of the string
759 // fails. If the quote string ends with \\ then we need to iterate
760 // through the entire string to make sure we detect the unexcaped
761 // double quotes correctly.
765 for ($j=$iPosStart,$iCnt=strlen($read);$j<$iCnt;++
$j) {
769 $bEscaped = !$bEscaped;
791 if ($iPos > strlen($read)) {
799 * @param string $read
801 * @return object AddressStructure object
803 function parseAddress($read, &$i) {
805 for (; $read{$i} != ')'; ++
$i) {
806 $char = strtoupper($read{$i});
808 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
809 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
812 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
821 if (count($arg_a) == 4) {
822 $adr = new AddressStructure();
823 $adr->personal
= $arg_a[0];
824 $adr->adl
= $arg_a[1];
825 $adr->mailbox
= $arg_a[2];
826 $adr->host
= $arg_a[3];
834 * @param string $read
836 * @param object Disposition object or empty string
838 function parseDisposition($read, &$i) {
840 for (; $read{$i} != ')'; ++
$i) {
842 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
843 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
844 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
849 if (isset($arg_a[0])) {
850 $disp = new Disposition($arg_a[0]);
851 if (isset($arg_a[1])) {
852 $disp->properties
= $arg_a[1];
855 return (is_object($disp) ?
$disp : '');
859 * @param string $read
861 * @return object Language object or empty string
863 function parseLanguage($read, &$i) {
864 /* no idea how to process this one without examples */
867 for (; $read{$i} != ')'; ++
$i) {
869 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
870 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
871 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
876 if (isset($arg_a[0])) {
877 $lang = new Language($arg_a[0]);
878 if (isset($arg_a[1])) {
879 $lang->properties
= $arg_a[1];
882 return (is_object($lang) ?
$lang : '');
886 * Parse message text enclosed in parenthesis
887 * @param string $read
891 function parseParenthesis($read, $i) {
892 for ($i++
; $read{$i} != ')'; ++
$i) {
894 case '"': $this->parseQuote($read, $i); break;
895 case '{': $this->parseLiteral($read, $i); break;
896 case '(': $this->parseProperties($read, $i); break;
904 * Function to fill the message structure in case the
905 * bodystructure is not available
907 * @param string $read
908 * @param string $type0 message part type
909 * @param string $type1 message part subtype
910 * @return string (only when type0 is not message or multipart)
912 function parseMessage($read, $type0, $type1) {
915 $rfc822_header = true;
916 $mime_header = false;
919 $rfc822_header = false;
922 default: return $read;
925 for ($i = 1; $i < $count; ++
$i) {
926 $line = trim($body[$i]);
927 if (($mime_header ||
$rfc822_header) &&
928 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
931 $bndreg = str_replace("\\", "\\\\", $bndreg);
932 $bndreg = str_replace("?", "\\?", $bndreg);
933 $bndreg = str_replace("+", "\\+", $bndreg);
934 $bndreg = str_replace(".", "\\.", $bndreg);
935 $bndreg = str_replace("/", "\\/", $bndreg);
936 $bndreg = str_replace("-", "\\-", $bndreg);
937 $bndreg = str_replace("(", "\\(", $bndreg);
938 $bndreg = str_replace(")", "\\)", $bndreg);
939 } else if ($rfc822_header && $line == '') {
940 $rfc822_header = false;
941 if ($msg->type0
== 'multipart') {
946 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
947 $cnt = count($boundaries)-1;
948 $bnd = $boundaries[$cnt]['bnd'];
949 $bndreg = $boundaries[$cnt]['bndreg'];
951 $regstr = '/^--'."($bndreg)".".*".'/';
952 if (preg_match($regstr, $line, $reg)) {
953 $bndlen = strlen($reg[1]);
955 if (strlen($line) > ($bndlen +
3)) {
956 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
961 /* calc offset and return $msg */
962 //$entStr = CalcEntity("$entStr", -1);
963 array_pop($boundaries);
969 //$entStr = CalcEntity("$entStr", 0);
980 * @param array $entity
981 * @param array $alt_order
982 * @param boolean $strict
985 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
987 if ($this->type0
== 'multipart') {
988 if($this->type1
== 'alternative') {
989 $msg = $this->findAlternativeEntity($alt_order);
990 if (count($msg->entities
) == 0) {
991 $entity[] = $msg->entity_id
;
993 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
996 } else if ($this->type1
== 'related') { /* RFC 2387 */
997 $msgs = $this->findRelatedEntity();
998 foreach ($msgs as $msg) {
999 if (count($msg->entities
) == 0) {
1000 $entity[] = $msg->entity_id
;
1002 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1005 if (count($msgs) > 0) {
1008 } else { /* Treat as multipart/mixed */
1009 foreach ($this->entities
as $ent) {
1010 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
1011 (!isset($ent->header
->parameters
['filename'])) &&
1012 (!isset($ent->header
->parameters
['name'])) &&
1013 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1014 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1019 } else { /* If not multipart, then just compare with each entry from $alt_order */
1020 $type = $this->type0
.'/'.$this->type1
;
1021 // $alt_order[] = "message/rfc822";
1022 foreach ($alt_order as $alt) {
1023 if( ($alt == $type) && isset($this->entity_id
) ) {
1024 if ((count($this->entities
) == 0) &&
1025 (!isset($this->header
->parameters
['filename'])) &&
1026 (!isset($this->header
->parameters
['name'])) &&
1027 isset($this->header
->disposition
) && is_object($this->header
->disposition
) &&
1028 !(is_object($this->header
->disposition
) && strtolower($this->header
->disposition
->name
) == 'attachment')) {
1029 $entity[] = $this->entity_id
;
1036 foreach ($this->entities
as $ent) {
1037 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
1038 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1039 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1044 if(!$strict && !$found) {
1045 if (($this->type0
== 'text') &&
1046 in_array($this->type1
, array('plain', 'html', 'message')) &&
1047 isset($this->entity_id
)) {
1048 if (count($this->entities
) == 0) {
1049 if (!is_object($this->header
->disposition
) ||
strtolower($this->header
->disposition
->name
) != 'attachment') {
1050 $entity[] = $this->entity_id
;
1059 * @param array $alt_order
1062 function findAlternativeEntity($alt_order) {
1063 /* If we are dealing with alternative parts then we */
1064 /* choose the best viewable message supported by SM. */
1067 foreach($this->entities
as $ent) {
1068 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1069 if ($type == 'multipart/related') {
1070 $type = $ent->header
->getParameter('type');
1071 // Mozilla bug. Mozilla does not provide the parameter type.
1072 if (!$type) $type = 'text/html';
1074 $altCount = count($alt_order);
1075 for ($j = $best_view; $j < $altCount; ++
$j) {
1076 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1088 function findRelatedEntity() {
1090 $related_type = $this->header
->getParameter('type');
1091 // Mozilla bug. Mozilla does not provide the parameter type.
1092 if (!$related_type) $related_type = 'text/html';
1093 $entCount = count($this->entities
);
1094 for ($i = 0; $i < $entCount; ++
$i) {
1095 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1096 if ($related_type == $type) {
1097 $msgs[] = $this->entities
[$i];
1104 * @param array $exclude_id
1105 * @param array $result
1108 function getAttachments($exclude_id=array(), $result = array()) {
1110 if (($this->type0 == 'message') &&
1111 ($this->type1 == 'rfc822') &&
1112 ($this->entity_id) ) {
1113 $this = $this->entities[0];
1116 if (count($this->entities
)) {
1117 foreach ($this->entities
as $entity) {
1119 foreach ($exclude_id as $excl) {
1120 if ($entity->entity_id
=== $excl) {
1126 if (($entity->type0
== 'multipart') &&
1127 ($entity->type1
!= 'related')) {
1128 $result = $entity->getAttachments($exclude_id, $result);
1129 } else if ($entity->type0
!= 'multipart') {
1130 $result[] = $entity;
1136 foreach ($exclude_id as $excl) {
1137 $exclude = $exclude ||
($this->entity_id
== $excl);
1148 * Add attachment to message object
1149 * @param string $type attachment type
1150 * @param string $name attachment name
1151 * @param string $location path to attachment
1153 function initAttachment($type, $name, $location) {
1154 $attachment = new Message();
1155 $mime_header = new MessageHeader();
1156 $mime_header->setParameter('name', $name);
1157 // FIXME: duplicate code. see ContentType class
1158 $pos = strpos($type, '/');
1160 $mime_header->type0
= substr($type, 0, $pos);
1161 $mime_header->type1
= substr($type, $pos+
1);
1163 $mime_header->type0
= $type;
1165 $attachment->att_local_name
= $location;
1166 $disposition = new Disposition('attachment');
1167 $disposition->properties
['filename'] = $name;
1168 $mime_header->disposition
= $disposition;
1169 $attachment->mime_header
= $mime_header;
1170 $this->entities
[]=$attachment;
1174 * Delete all attachments from this object from disk.
1177 function purgeAttachments() {
1178 if ($this->att_local_name
) {
1179 global $username, $attachment_dir;
1180 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1181 if ( file_exists($hashed_attachment_dir . '/' . $this->att_local_name
) ) {
1182 unlink($hashed_attachment_dir . '/' . $this->att_local_name
);
1185 // recursively delete attachments from entities contained in this object
1186 for ($i=0, $entCount=count($this->entities
);$i< $entCount; ++
$i) {
1187 $this->entities
[$i]->purgeAttachments();