6 * This file contains functions needed to handle mime messages.
8 * @copyright © 2003-2007 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 \deleted status
95 * Message \flagged status
110 * Message part offset
111 * for fetching body parts out of raw messages
116 * Message part length
117 * for fetching body parts out of raw messages
122 * Local attachment filename location where the tempory attachment is
123 * stored. For use in delivery class.
126 var $att_local_name = '';
129 * @param string $ent entity id
131 function setEnt($ent) {
132 $this->entity_id
= $ent;
136 * Add nested message part
139 function addEntity ($msg) {
140 $this->entities
[] = $msg;
144 * Get file name used for mime part
145 * @return string file name
148 function getFilename() {
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'));
156 $name = $header->getParameter('name');
158 if (!trim( $header->id
)) {
159 $filename = 'untitled-[' . $this->entity_id
. ']' ;
161 $filename = 'cid: ' . $header->id
;
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
. ']' ;
178 $filename = 'cid: ' . $header->id
;
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
192 function addRFC822Header($read) {
193 $header = new Rfc822Header();
194 $this->rfc822_header
= $header->parseHeader($read);
199 * @return mixed (object or string?)
201 function getEntity($ent) {
202 $cur_ent = $this->entity_id
;
204 if (($cur_ent == '') ||
($cur_ent == '0')) {
205 $cur_ent_a = array();
207 $cur_ent_a = explode('.', $this->entity_id
);
209 $ent_a = explode('.', $ent);
211 for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++
$i) {
212 if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
214 $cur_ent_a = explode('.', $msg->entity_id
);
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)];
220 $msg = $msg->entities
[0];
223 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
224 /*this is a header for a message/rfc822 entity */
225 $msg = $msg->entities
[0];
229 if (($msg->type0
== 'message') && ($msg->type1
== 'rfc822')) {
230 /*this is a header for a message/rfc822 entity */
231 $msg = $msg->entities
[0];
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)];
245 * @param string $s message body
247 function setBody($s) {
248 $this->body_part
= $s;
252 * Clean message object
254 function clean_up() {
256 $msg->body_part
= '';
258 foreach ($msg->entities
as $m) {
266 function getMailbox() {
268 while (is_object($msg->parent
)) {
271 return $msg->mailbox
;
275 * Bodystructure parser, a recursive function for generating the
276 * entity-tree with all the mime-parts.
278 * It follows RFC2060 and stores all the described fields in the
283 * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
284 * @param string $read
286 * @param mixed $sub_msg
287 * @return object Message object
288 * @todo define argument and return types
290 function parseStructure($read, &$i, $sub_msg = '') {
291 $msg = Message
::parseBodyStructure($read, $i, $sub_msg);
292 if($msg) $msg->setEntIds($msg,false,0);
303 function setEntIds(&$msg,$init=false,$i=0) {
304 $iCnt = count($msg->entities
);
305 if ($init !==false) {
307 if ($msg->parent
->type0
== 'message' &&
308 $msg->parent
->type1
== 'rfc822' &&
309 $msg->type0
== 'multipart') {
313 $msg->entity_id
= "$init.$iEntSub";
315 $msg->entity_id
= $iEntSub;
322 for ($i=0;$i<$iCnt;++
$i) {
323 $msg->entities
[$i]->parent
=& $msg;
324 if (strrchr($msg->entity_id
, '.') != '.0') {
325 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->entity_id
,$i);
327 $msg->entities
[$i]->setEntIds($msg->entities
[$i],$msg->parent
->entity_id
,$i);
333 * @param string $read
335 * @param mixed $sub_msg
336 * @return object Message object
338 * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
340 function parseBodyStructure($read, &$i, $sub_msg = '') {
346 $message = new Message();
349 for ($cnt = strlen($read); $i < $cnt; ++
$i) {
350 $char = strtoupper($read{$i});
356 $msg = new Message();
357 $hdr = new MessageHeader();
358 $hdr->type0
= 'text';
359 $hdr->type1
= 'plain';
360 $hdr->encoding
= '7bit';
362 $msg->header
->type0
= 'multipart';
363 $msg->type0
= 'multipart';
364 while ($read{$i} == '(') {
365 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
370 /* multipart properties */
372 $arg_a[] = $msg->parseProperties($read, $i);
376 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
378 $arg_a[] = $msg->parseDisposition($read, $i);
379 } else { /* properties */
380 $arg_a[] = $msg->parseProperties($read, $i);
385 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
387 $arg_a[]= $msg->parseLanguage($read, $i);
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();
396 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
397 while (($i < $cnt) && ($read{$i} != '(')) {
400 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
405 $arg_a[] = $msg->parseDisposition($read, $i);
410 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
411 $arg_a[] = $msg->parseDisposition($read, $i);
413 $arg_a[] = $msg->parseLanguage($read, $i);
418 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
420 $arg_a[] = $msg->parseLanguage($read, $i);
422 $i = $msg->parseParenthesis($read, $i);
423 $arg_a[] = ''; /* not yet described in rfc2060 */
428 /* unknown argument, skip this part */
429 $i = $msg->parseParenthesis($read, $i);
436 /* inside an entity -> start processing */
437 $arg_s = $msg->parseQuote($read, $i);
440 $arg_s = strtolower($arg_s); /* type0 and type1 */
446 /* probably NIL argument */
447 $tmpnil = strtoupper(substr($read, $i, 4));
448 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
455 /* process the literal value */
456 $arg_a[] = $msg->parseLiteral($read, $i);
460 case is_numeric($read{$i}):
461 /* process integers */
462 if ($read{$i} == ' ') { break; }
464 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
465 $i +
= strlen($regs[1])-1;
472 $multipart = (isset($msg->type0
) && ($msg->type0
== '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];
478 $msg->type0
= $arg_a[0];
479 $msg->type1
= $arg_a[1];
481 if (is_array($arr)) {
482 $hdr->parameters
= $arg_a[2];
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];
490 $hdr->lines
= $arg_a[7];
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
);
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
);
513 } /* parsestructure */
516 * @param string $read
520 function parseProperties($read, &$i) {
521 $properties = array();
524 for (; $read{$i} != ')'; ++
$i) {
526 if ($read{$i} == '"') {
527 $arg_s = $this->parseQuote($read, $i);
528 } else if ($read{$i} == '{') {
529 $arg_s = $this->parseLiteral($read, $i);
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;
542 return $this->handleRfc2231($properties);
546 * Joins RFC-2231 continuations, converts encoding to RFC-2047 style
547 * @param array $properties
550 function handleRfc2231($properties) {
552 /* STAGE 1: look for multi-line parameters, convert to the single line
553 form, and normalize values */
556 foreach($properties as $key=>$value) {
557 /* Look for parameters followed by "*", a number, and an optional "*"
559 if (preg_match('/^(.*\*)(\d+)(|\*)$/', $key, $matches)) {
560 unset($properties[$key]);
561 $prop_name = $matches[1];
562 if (!isset($cont[$prop_name])) $cont[$prop_name] = array();
564 /* An asterisk at the end of parameter name indicates that there
565 may be an encoding information present, and the parameter
566 value is percent-hex encoded. If parameter is not encoded, we
567 encode it to simplify further processing.
569 if ($matches[3] == '') $value = rawurlencode($value);
570 /* Use the number from parameter name as segment index */
571 $cont[$prop_name][$matches[2]] = $value;
574 foreach($cont as $key=>$values) {
575 /* Sort segments of multi-line parameters by index number. */
577 /* Join segments. We can do it safely, because:
578 - All segments are encoded.
579 - Per RFC-2231, chapter 4.1 notes.
581 $value = implode('', $values);
582 /* Add language and character set field delimiters if not present,
583 as required per RFC-2231, chapter 4.1, note #5. */
584 if (strpos($value, "'") === false) $value = "''".$value;
585 $properties[$key] = $value;
588 /* STAGE 2: Convert single line RFC-2231 encoded parameters, and
589 previously converted multi-line parameters, to RFC-2047 encoding */
591 foreach($properties as $key=>$value) {
592 if ($idx = strpos($key, '*')) {
593 unset($properties[$key]);
594 /* Extract the charset & language. */
595 $charset = substr($value,0,strpos($value,"'"));
596 $value = substr($value,strlen($charset)+
1);
597 $language = substr($value,0,strpos($value,"'"));
598 $value = substr($value,strlen($language)+
1);
599 /* No character set defaults to US-ASCII */
600 if (!$charset) $charset = 'US-ASCII';
601 if ($language) $language = '*'.$language;
602 /* Convert to RFC-2047 base64 encoded string. */
603 $properties[substr($key, 0, $idx)] = '=?'.$charset.$language.'?B?'.base64_encode(rawurldecode($value)).'?=';
610 * @param string $read
612 * @param object $hdr MessageHeader object
613 * @return object MessageHeader object
615 function parseEnvelope($read, &$i, $hdr) {
619 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
620 $char = strtoupper($read{$i});
623 $arg_a[] = $this->parseQuote($read, $i);
627 $arg_a[] = $this->parseLiteral($read, $i);
628 /* temp bugfix (SM 1.5 will have a working clean version)
629 too much work to implement that version right now */
634 /* probably NIL argument */
635 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
642 /* Address structure (with group support)
643 * Note: Group support is useless on SMTP connections
644 * because the protocol doesn't support it
649 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
650 if ($read{$i} == '(') {
651 $addr = $this->parseAddress($read, $i);
652 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
654 $group = $addr->mailbox
;
657 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
659 if ($a == ($j+
1)) { /* no group members */
660 $group_addr->group
= $group;
661 $group_addr->mailbox
= '';
662 $group_addr->personal
= "$group: Undisclosed recipients;";
663 $addr_a[] = $group_addr;
667 $addr->group
= $group;
679 if (count($arg_a) > 9) {
680 $d = strtr($arg_a[0], array(' ' => ' '));
681 $d_parts = explode(' ', $d);
682 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
684 $hdr->date
= getTimeStamp($d_parts); /* argument 1: date */
685 $hdr->date_unparsed
= strtr($d,'<>',' '); /* original date */
686 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
687 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
688 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
689 $hdr->reply_to
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
690 $hdr->to
= $arg_a[5]; /* argument 6: to */
691 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
692 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
693 $hdr->in_reply_to
= $arg_a[8]; /* argument 9: in-reply-to */
694 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
700 * @param string $read
705 function parseLiteral($read, &$i) {
708 $iPos = strpos($read,'}',$i);
710 $lit_cnt = substr($read, $i, $iPos - $i);
711 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
712 /* Now read the literal */
713 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
715 /* temp bugfix (SM 1.5 will have a working clean version)
716 too much work to implement that version right now */
718 } else { /* should never happen */
719 $i +
= 3; /* } + \r + \n */
726 * function parseQuote
728 * This extract the string value from a quoted string. After the end-quote
729 * character is found it returns the string. The offset $i when calling
730 * this function points to the first double quote. At the end it points to
731 * The ending quote. This function takes care of escaped double quotes.
734 * initial $i end position $i
736 * @param string $read
737 * @param integer $i offset in $read
738 * @return string string inbetween the double quotes
739 * @author Marc Groot Koerkamp
741 function parseQuote($read, &$i) {
746 $iPos = strpos($read,'"',$iPos);
748 if ($iPos && $read{$iPos -1} != '\\') {
749 $s = substr($read,$i,($iPos-$i));
752 } else if ($iPos > 1 && $read{$iPos -1} == '\\' && $read{$iPos-2} == '\\') {
753 // This is an unique situation where the fast detection of the string
754 // fails. If the quote string ends with \\ then we need to iterate
755 // through the entire string to make sure we detect the unexcaped
756 // double quotes correctly.
760 for ($j=$iPosStart,$iCnt=strlen($read);$j<$iCnt;++
$j) {
764 $bEscaped = !$bEscaped;
786 if ($iPos > strlen($read)) {
794 * @param string $read
796 * @return object AddressStructure object
798 function parseAddress($read, &$i) {
800 for (; $read{$i} != ')'; ++
$i) {
801 $char = strtoupper($read{$i});
803 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
804 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
807 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
816 if (count($arg_a) == 4) {
817 $adr = new AddressStructure();
818 $adr->personal
= $arg_a[0];
819 $adr->adl
= $arg_a[1];
820 $adr->mailbox
= $arg_a[2];
821 $adr->host
= $arg_a[3];
829 * @param string $read
831 * @param object Disposition object or empty string
833 function parseDisposition($read, &$i) {
835 for (; $read{$i} != ')'; ++
$i) {
837 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
838 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
839 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
844 if (isset($arg_a[0])) {
845 $disp = new Disposition($arg_a[0]);
846 if (isset($arg_a[1])) {
847 $disp->properties
= $arg_a[1];
850 return (is_object($disp) ?
$disp : '');
854 * @param string $read
856 * @return object Language object or empty string
858 function parseLanguage($read, &$i) {
859 /* no idea how to process this one without examples */
862 for (; $read{$i} != ')'; ++
$i) {
864 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
865 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
866 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
871 if (isset($arg_a[0])) {
872 $lang = new Language($arg_a[0]);
873 if (isset($arg_a[1])) {
874 $lang->properties
= $arg_a[1];
877 return (is_object($lang) ?
$lang : '');
881 * Parse message text enclosed in parenthesis
882 * @param string $read
886 function parseParenthesis($read, $i) {
887 for ($i++
; $read{$i} != ')'; ++
$i) {
889 case '"': $this->parseQuote($read, $i); break;
890 case '{': $this->parseLiteral($read, $i); break;
891 case '(': $this->parseProperties($read, $i); break;
899 * Function to fill the message structure in case the
900 * bodystructure is not available
902 * @param string $read
903 * @param string $type0 message part type
904 * @param string $type1 message part subtype
905 * @return string (only when type0 is not message or multipart)
907 function parseMessage($read, $type0, $type1) {
910 $rfc822_header = true;
911 $mime_header = false;
914 $rfc822_header = false;
917 default: return $read;
920 for ($i = 1; $i < $count; ++
$i) {
921 $line = trim($body[$i]);
922 if (($mime_header ||
$rfc822_header) &&
923 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
926 $bndreg = str_replace("\\", "\\\\", $bndreg);
927 $bndreg = str_replace("?", "\\?", $bndreg);
928 $bndreg = str_replace("+", "\\+", $bndreg);
929 $bndreg = str_replace(".", "\\.", $bndreg);
930 $bndreg = str_replace("/", "\\/", $bndreg);
931 $bndreg = str_replace("-", "\\-", $bndreg);
932 $bndreg = str_replace("(", "\\(", $bndreg);
933 $bndreg = str_replace(")", "\\)", $bndreg);
934 } else if ($rfc822_header && $line == '') {
935 $rfc822_header = false;
936 if ($msg->type0
== 'multipart') {
941 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
942 $cnt = count($boundaries)-1;
943 $bnd = $boundaries[$cnt]['bnd'];
944 $bndreg = $boundaries[$cnt]['bndreg'];
946 $regstr = '/^--'."($bndreg)".".*".'/';
947 if (preg_match($regstr, $line, $reg)) {
948 $bndlen = strlen($reg[1]);
950 if (strlen($line) > ($bndlen +
3)) {
951 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
956 /* calc offset and return $msg */
957 //$entStr = CalcEntity("$entStr", -1);
958 array_pop($boundaries);
964 //$entStr = CalcEntity("$entStr", 0);
975 * @param array $entity
976 * @param array $alt_order
977 * @param boolean $strict
980 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
982 if ($this->type0
== 'multipart') {
983 if($this->type1
== 'alternative') {
984 $msg = $this->findAlternativeEntity($alt_order);
985 if (count($msg->entities
) == 0) {
986 $entity[] = $msg->entity_id
;
988 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
991 } else if ($this->type1
== 'related') { /* RFC 2387 */
992 $msgs = $this->findRelatedEntity();
993 foreach ($msgs as $msg) {
994 if (count($msg->entities
) == 0) {
995 $entity[] = $msg->entity_id
;
997 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1000 if (count($msgs) > 0) {
1003 } else { /* Treat as multipart/mixed */
1004 foreach ($this->entities
as $ent) {
1005 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
1006 (!isset($ent->header
->parameters
['filename'])) &&
1007 (!isset($ent->header
->parameters
['name'])) &&
1008 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1009 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1014 } else { /* If not multipart, then just compare with each entry from $alt_order */
1015 $type = $this->type0
.'/'.$this->type1
;
1016 // $alt_order[] = "message/rfc822";
1017 foreach ($alt_order as $alt) {
1018 if( ($alt == $type) && isset($this->entity_id
) ) {
1019 if ((count($this->entities
) == 0) &&
1020 (!isset($this->header
->parameters
['filename'])) &&
1021 (!isset($this->header
->parameters
['name'])) &&
1022 isset($this->header
->disposition
) && is_object($this->header
->disposition
) &&
1023 !(is_object($this->header
->disposition
) && strtolower($this->header
->disposition
->name
) == 'attachment')) {
1024 $entity[] = $this->entity_id
;
1031 foreach ($this->entities
as $ent) {
1032 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
1033 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1034 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1039 if(!$strict && !$found) {
1040 if (($this->type0
== 'text') &&
1041 in_array($this->type1
, array('plain', 'html', 'message')) &&
1042 isset($this->entity_id
)) {
1043 if (count($this->entities
) == 0) {
1044 if (!is_object($this->header
->disposition
) ||
strtolower($this->header
->disposition
->name
) != 'attachment') {
1045 $entity[] = $this->entity_id
;
1054 * @param array $alt_order
1057 function findAlternativeEntity($alt_order) {
1058 /* If we are dealing with alternative parts then we */
1059 /* choose the best viewable message supported by SM. */
1062 foreach($this->entities
as $ent) {
1063 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1064 if ($type == 'multipart/related') {
1065 $type = $ent->header
->getParameter('type');
1066 // Mozilla bug. Mozilla does not provide the parameter type.
1067 if (!$type) $type = 'text/html';
1069 $altCount = count($alt_order);
1070 for ($j = $best_view; $j < $altCount; ++
$j) {
1071 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1083 function findRelatedEntity() {
1085 $related_type = $this->header
->getParameter('type');
1086 // Mozilla bug. Mozilla does not provide the parameter type.
1087 if (!$related_type) $related_type = 'text/html';
1088 $entCount = count($this->entities
);
1089 for ($i = 0; $i < $entCount; ++
$i) {
1090 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1091 if ($related_type == $type) {
1092 $msgs[] = $this->entities
[$i];
1099 * @param array $exclude_id
1100 * @param array $result
1103 function getAttachments($exclude_id=array(), $result = array()) {
1105 if (($this->type0 == 'message') &&
1106 ($this->type1 == 'rfc822') &&
1107 ($this->entity_id) ) {
1108 $this = $this->entities[0];
1111 if (count($this->entities
)) {
1112 foreach ($this->entities
as $entity) {
1114 foreach ($exclude_id as $excl) {
1115 if ($entity->entity_id
=== $excl) {
1121 if (($entity->type0
== 'multipart') &&
1122 ($entity->type1
!= 'related')) {
1123 $result = $entity->getAttachments($exclude_id, $result);
1124 } else if ($entity->type0
!= 'multipart') {
1125 $result[] = $entity;
1131 foreach ($exclude_id as $excl) {
1132 $exclude = $exclude ||
($this->entity_id
== $excl);
1143 * Add attachment to message object
1144 * @param string $type attachment type
1145 * @param string $name attachment name
1146 * @param string $location path to attachment
1148 function initAttachment($type, $name, $location) {
1149 $attachment = new Message();
1150 $mime_header = new MessageHeader();
1151 $mime_header->setParameter('name', $name);
1152 // FIXME: duplicate code. see ContentType class
1153 $pos = strpos($type, '/');
1155 $mime_header->type0
= substr($type, 0, $pos);
1156 $mime_header->type1
= substr($type, $pos+
1);
1158 $mime_header->type0
= $type;
1160 $attachment->att_local_name
= $location;
1161 $disposition = new Disposition('attachment');
1162 $disposition->properties
['filename'] = $name;
1163 $mime_header->disposition
= $disposition;
1164 $attachment->mime_header
= $mime_header;
1165 $this->entities
[]=$attachment;
1169 * Delete all attachments from this object from disk.
1172 function purgeAttachments() {
1173 if ($this->att_local_name
) {
1174 global $username, $attachment_dir;
1175 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1176 if ( file_exists($hashed_attachment_dir . '/' . $this->att_local_name
) ) {
1177 unlink($hashed_attachment_dir . '/' . $this->att_local_name
);
1180 // recursively delete attachments from entities contained in this object
1181 for ($i=0, $entCount=count($this->entities
);$i< $entCount; ++
$i) {
1182 $this->entities
[$i]->purgeAttachments();