d9282f759e0e478e0216208782326a1e580af652
6 * This file contains functions needed to handle mime messages.
8 * @copyright 2003-2012 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
. ']' . '.' . strtolower($header->type1
);
166 $filename = 'cid: ' . $header->id
. '.' . strtolower($header->type1
);
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
. ']' . '.' . strtolower($header->type1
);
183 $filename = 'cid: ' . $header->id
. '.' . strtolower($header->type1
);
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';
368 $msg->header
->type0
= 'multipart';
369 $msg->type0
= 'multipart';
370 while ($read{$i} == '(') {
371 $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
376 /* multipart properties */
378 $arg_a[] = $msg->parseProperties($read, $i);
382 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
384 $arg_a[] = $msg->parseDisposition($read, $i);
385 } else { /* properties */
386 $arg_a[] = $msg->parseProperties($read, $i);
391 if (isset($msg->type0
) && ($msg->type0
== 'multipart')) {
393 $arg_a[]= $msg->parseLanguage($read, $i);
396 if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
397 $msg->header
->type0
= $arg_a[0];
398 $msg->header
->type1
= $arg_a[1];
399 $msg->type0
= $arg_a[0];
400 $msg->type1
= $arg_a[1];
401 $rfc822_hdr = new Rfc822Header();
402 $msg->rfc822_header
= $msg->parseEnvelope($read, $i, $rfc822_hdr);
403 while (($i < $cnt) && ($read{$i} != '(')) {
406 $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
411 $arg_a[] = $msg->parseDisposition($read, $i);
416 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
417 $arg_a[] = $msg->parseDisposition($read, $i);
419 $arg_a[] = $msg->parseLanguage($read, $i);
424 if (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
426 $arg_a[] = $msg->parseLanguage($read, $i);
428 $i = $msg->parseParenthesis($read, $i);
429 $arg_a[] = ''; /* not yet described in rfc2060 */
434 /* unknown argument, skip this part */
435 $i = $msg->parseParenthesis($read, $i);
442 /* inside an entity -> start processing */
443 $arg_s = $msg->parseQuote($read, $i);
446 $arg_s = strtolower($arg_s); /* type0 and type1 */
452 /* probably NIL argument */
453 $tmpnil = strtoupper(substr($read, $i, 4));
454 if ($tmpnil == 'NIL ' ||
$tmpnil == 'NIL)') {
461 /* process the literal value */
462 $arg_a[] = $msg->parseLiteral($read, $i);
466 case is_numeric($read{$i}):
467 /* process integers */
468 if ($read{$i} == ' ') { break; }
470 if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
471 $i +
= strlen($regs[1])-1;
478 $multipart = (isset($msg->type0
) && ($msg->type0
== 'multipart'));
480 $shifted_args = (($arg_a[0] == 'text') ||
(($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
481 $hdr->type0
= $arg_a[0];
482 $hdr->type1
= $arg_a[1];
484 $msg->type0
= $arg_a[0];
485 $msg->type1
= $arg_a[1];
487 if (is_array($arr)) {
488 $hdr->parameters
= $arg_a[2];
490 $hdr->id
= str_replace('<', '', str_replace('>', '', $arg_a[3]));
491 $hdr->description
= $arg_a[4];
492 $hdr->encoding
= strtolower($arg_a[5]);
493 $hdr->entity_id
= $msg->entity_id
;
494 $hdr->size
= $arg_a[6];
496 $hdr->lines
= $arg_a[7];
501 $hdr->md5
= (isset($arg_a[7+
$s]) ?
$arg_a[7+
$s] : $hdr->md5
);
502 $hdr->disposition
= (isset($arg_a[8+
$s]) ?
$arg_a[8+
$s] : $hdr->disposition
);
503 $hdr->language
= (isset($arg_a[9+
$s]) ?
$arg_a[9+
$s] : $hdr->language
);
506 $hdr->type0
= 'multipart';
507 $hdr->type1
= $arg_a[0];
508 $msg->type0
= 'multipart';
509 $msg->type1
= $arg_a[0];
510 $hdr->parameters
= (isset($arg_a[1]) ?
$arg_a[1] : $hdr->parameters
);
511 $hdr->disposition
= (isset($arg_a[2]) ?
$arg_a[2] : $hdr->disposition
);
512 $hdr->language
= (isset($arg_a[3]) ?
$arg_a[3] : $hdr->language
);
519 } /* parsestructure */
522 * @param string $read
526 function parseProperties($read, &$i) {
527 $properties = array();
530 for (; $read{$i} != ')'; ++
$i) {
532 if ($read{$i} == '"') {
533 $arg_s = $this->parseQuote($read, $i);
534 } else if ($read{$i} == '{') {
535 $arg_s = $this->parseLiteral($read, $i);
539 if ($prop_name == '') {
540 $prop_name = strtolower($arg_s);
541 $properties[$prop_name] = '';
542 } else if ($prop_name != '') {
543 $properties[$prop_name] = $arg_s;
548 return $this->handleRfc2231($properties);
552 * Joins RFC-2231 continuations, converts encoding to RFC-2047 style
553 * @param array $properties
556 function handleRfc2231($properties) {
558 /* STAGE 1: look for multi-line parameters, convert to the single line
559 form, and normalize values */
562 foreach($properties as $key=>$value) {
563 /* Look for parameters followed by "*", a number, and an optional "*"
565 if (preg_match('/^(.*\*)(\d+)(|\*)$/', $key, $matches)) {
566 unset($properties[$key]);
567 $prop_name = $matches[1];
568 if (!isset($cont[$prop_name])) $cont[$prop_name] = array();
570 /* An asterisk at the end of parameter name indicates that there
571 may be an encoding information present, and the parameter
572 value is percent-hex encoded. If parameter is not encoded, we
573 encode it to simplify further processing.
575 if ($matches[3] == '') $value = rawurlencode($value);
576 /* Use the number from parameter name as segment index */
577 $cont[$prop_name][$matches[2]] = $value;
580 foreach($cont as $key=>$values) {
581 /* Sort segments of multi-line parameters by index number. */
583 /* Join segments. We can do it safely, because:
584 - All segments are encoded.
585 - Per RFC-2231, chapter 4.1 notes.
587 $value = implode('', $values);
588 /* Add language and character set field delimiters if not present,
589 as required per RFC-2231, chapter 4.1, note #5. */
590 if (strpos($value, "'") === false) $value = "''".$value;
591 $properties[$key] = $value;
594 /* STAGE 2: Convert single line RFC-2231 encoded parameters, and
595 previously converted multi-line parameters, to RFC-2047 encoding */
597 foreach($properties as $key=>$value) {
598 if ($idx = strpos($key, '*')) {
599 unset($properties[$key]);
600 /* Extract the charset & language. */
601 $charset = substr($value,0,strpos($value,"'"));
602 $value = substr($value,strlen($charset)+
1);
603 $language = substr($value,0,strpos($value,"'"));
604 $value = substr($value,strlen($language)+
1);
605 /* No character set defaults to US-ASCII */
606 if (!$charset) $charset = 'US-ASCII';
607 if ($language) $language = '*'.$language;
608 /* Convert to RFC-2047 base64 encoded string. */
609 $properties[substr($key, 0, $idx)] = '=?'.$charset.$language.'?B?'.base64_encode(rawurldecode($value)).'?=';
616 * @param string $read
618 * @param object $hdr MessageHeader object
619 * @return object MessageHeader object
621 function parseEnvelope($read, &$i, $hdr) {
625 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
626 $char = strtoupper($read{$i});
629 $arg_a[] = $this->parseQuote($read, $i);
633 $arg_a[] = $this->parseLiteral($read, $i);
634 /* temp bugfix (SM 1.5 will have a working clean version)
635 too much work to implement that version right now */
640 /* probably NIL argument */
641 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
648 /* Address structure (with group support)
649 * Note: Group support is useless on SMTP connections
650 * because the protocol doesn't support it
655 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
656 if ($read{$i} == '(') {
657 $addr = $this->parseAddress($read, $i);
658 if (($addr->host
== '') && ($addr->mailbox
!= '')) {
660 $group = $addr->mailbox
;
663 } else if ($group && ($addr->host
== '') && ($addr->mailbox
== '')) {
665 if ($a == ($j+
1)) { /* no group members */
666 $group_addr->group
= $group;
667 $group_addr->mailbox
= '';
668 $group_addr->personal
= "$group: Undisclosed recipients;";
669 $addr_a[] = $group_addr;
673 $addr->group
= $group;
685 if (count($arg_a) > 9) {
686 $d = strtr($arg_a[0], array(' ' => ' '));
687 $d_parts = explode(' ', $d);
688 if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
690 $hdr->date
= getTimeStamp($d_parts); /* argument 1: date */
691 $hdr->date_unparsed
= strtr($d,'<>',' '); /* original date */
692 $hdr->subject
= $arg_a[1]; /* argument 2: subject */
693 $hdr->from
= is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
694 $hdr->sender
= is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
695 $hdr->reply_to
= is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
696 $hdr->to
= $arg_a[5]; /* argument 6: to */
697 $hdr->cc
= $arg_a[6]; /* argument 7: cc */
698 $hdr->bcc
= $arg_a[7]; /* argument 8: bcc */
699 $hdr->in_reply_to
= $arg_a[8]; /* argument 9: in-reply-to */
700 $hdr->message_id
= $arg_a[9]; /* argument 10: message-id */
706 * @param string $read
711 function parseLiteral($read, &$i) {
714 $iPos = strpos($read,'}',$i);
716 $lit_cnt = substr($read, $i, $iPos - $i);
717 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
718 /* Now read the literal */
719 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
721 /* temp bugfix (SM 1.5 will have a working clean version)
722 too much work to implement that version right now */
724 } else { /* should never happen */
725 $i +
= 3; /* } + \r + \n */
732 * function parseQuote
734 * This extract the string value from a quoted string. After the end-quote
735 * character is found it returns the string. The offset $i when calling
736 * this function points to the first double quote. At the end it points to
737 * The ending quote. This function takes care of escaped double quotes.
740 * initial $i end position $i
742 * @param string $read
743 * @param integer $i offset in $read
744 * @return string string inbetween the double quotes
745 * @author Marc Groot Koerkamp
747 function parseQuote($read, &$i) {
752 $iPos = strpos($read,'"',$iPos);
754 if ($iPos && $read{$iPos -1} != '\\') {
755 $s = substr($read,$i,($iPos-$i));
758 } else if ($iPos > 1 && $read{$iPos -1} == '\\' && $read{$iPos-2} == '\\') {
759 // This is an unique situation where the fast detection of the string
760 // fails. If the quote string ends with \\ then we need to iterate
761 // through the entire string to make sure we detect the unexcaped
762 // double quotes correctly.
766 for ($j=$iPosStart,$iCnt=strlen($read);$j<$iCnt;++
$j) {
770 $bEscaped = !$bEscaped;
792 if ($iPos > strlen($read)) {
800 * @param string $read
802 * @return object AddressStructure object
804 function parseAddress($read, &$i) {
806 for (; $read{$i} != ')'; ++
$i) {
807 $char = strtoupper($read{$i});
809 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
810 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
813 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
822 if (count($arg_a) == 4) {
823 $adr = new AddressStructure();
824 $adr->personal
= $arg_a[0];
825 $adr->adl
= $arg_a[1];
826 $adr->mailbox
= $arg_a[2];
827 $adr->host
= $arg_a[3];
835 * @param string $read
837 * @param object Disposition object or empty string
839 function parseDisposition($read, &$i) {
841 for (; $read{$i} != ')'; ++
$i) {
843 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
844 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
845 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
850 if (isset($arg_a[0])) {
851 $disp = new Disposition($arg_a[0]);
852 if (isset($arg_a[1])) {
853 $disp->properties
= $arg_a[1];
856 return (is_object($disp) ?
$disp : '');
860 * @param string $read
862 * @return object Language object or empty string
864 function parseLanguage($read, &$i) {
865 /* no idea how to process this one without examples */
868 for (; $read{$i} != ')'; ++
$i) {
870 case '"': $arg_a[] = $this->parseQuote($read, $i); break;
871 case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
872 case '(': $arg_a[] = $this->parseProperties($read, $i); break;
877 if (isset($arg_a[0])) {
878 $lang = new Language($arg_a[0]);
879 if (isset($arg_a[1])) {
880 $lang->properties
= $arg_a[1];
883 return (is_object($lang) ?
$lang : '');
887 * Parse message text enclosed in parenthesis
888 * @param string $read
892 function parseParenthesis($read, $i) {
893 for ($i++
; $read{$i} != ')'; ++
$i) {
895 case '"': $this->parseQuote($read, $i); break;
896 case '{': $this->parseLiteral($read, $i); break;
897 case '(': $this->parseProperties($read, $i); break;
905 * Function to fill the message structure in case the
906 * bodystructure is not available
908 * @param string $read
909 * @param string $type0 message part type
910 * @param string $type1 message part subtype
911 * @return string (only when type0 is not message or multipart)
913 function parseMessage($read, $type0, $type1) {
916 $rfc822_header = true;
917 $mime_header = false;
920 $rfc822_header = false;
923 default: return $read;
926 for ($i = 1; $i < $count; ++
$i) {
927 $line = trim($body[$i]);
928 if (($mime_header ||
$rfc822_header) &&
929 (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
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 $bndreg = str_replace(")", "\\)", $bndreg);
940 } else if ($rfc822_header && $line == '') {
941 $rfc822_header = false;
942 if ($msg->type0
== 'multipart') {
947 if ((($line{0} == '-') ||
$rfc822_header) && isset($boundaries[0])) {
948 $cnt = count($boundaries)-1;
949 $bnd = $boundaries[$cnt]['bnd'];
950 $bndreg = $boundaries[$cnt]['bndreg'];
952 $regstr = '/^--'."($bndreg)".".*".'/';
953 if (preg_match($regstr, $line, $reg)) {
954 $bndlen = strlen($reg[1]);
956 if (strlen($line) > ($bndlen +
3)) {
957 if (($line{$bndlen+
2} == '-') && ($line{$bndlen+
3} == '-')) {
962 /* calc offset and return $msg */
963 //$entStr = CalcEntity("$entStr", -1);
964 array_pop($boundaries);
970 //$entStr = CalcEntity("$entStr", 0);
981 * @param array $entity
982 * @param array $alt_order
983 * @param boolean $strict
986 function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
988 if ($this->type0
== 'multipart') {
989 if($this->type1
== 'alternative') {
990 $msg = $this->findAlternativeEntity($alt_order);
991 if ( ! is_null($msg) ) {
992 if (count($msg->entities
) == 0) {
993 $entity[] = $msg->entity_id
;
995 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
999 } else if ($this->type1
== 'related') { /* RFC 2387 */
1000 $msgs = $this->findRelatedEntity();
1001 foreach ($msgs as $msg) {
1002 if (count($msg->entities
) == 0) {
1003 $entity[] = $msg->entity_id
;
1005 $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
1008 if (count($msgs) > 0) {
1011 } else { /* Treat as multipart/mixed */
1012 foreach ($this->entities
as $ent) {
1013 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
1014 (!isset($ent->header
->parameters
['filename'])) &&
1015 (!isset($ent->header
->parameters
['name'])) &&
1016 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1017 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1022 } else { /* If not multipart, then just compare with each entry from $alt_order */
1023 $type = $this->type0
.'/'.$this->type1
;
1024 // $alt_order[] = "message/rfc822";
1025 foreach ($alt_order as $alt) {
1026 if( ($alt == $type) && isset($this->entity_id
) ) {
1027 if ((count($this->entities
) == 0) &&
1028 (!isset($this->header
->parameters
['filename'])) &&
1029 (!isset($this->header
->parameters
['name'])) &&
1030 isset($this->header
->disposition
) && is_object($this->header
->disposition
) &&
1031 !(is_object($this->header
->disposition
) && strtolower($this->header
->disposition
->name
) == 'attachment')) {
1032 $entity[] = $this->entity_id
;
1039 foreach ($this->entities
as $ent) {
1040 if(!(is_object($ent->header
->disposition
) && strtolower($ent->header
->disposition
->name
) == 'attachment') &&
1041 (($ent->type0
!= 'message') && ($ent->type1
!= 'rfc822'))) {
1042 $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
1047 if(!$strict && !$found) {
1048 if (($this->type0
== 'text') &&
1049 in_array($this->type1
, array('plain', 'html', 'message')) &&
1050 isset($this->entity_id
)) {
1051 if (count($this->entities
) == 0) {
1052 if (!is_object($this->header
->disposition
) ||
strtolower($this->header
->disposition
->name
) != 'attachment') {
1053 $entity[] = $this->entity_id
;
1062 * @param array $alt_order
1065 function findAlternativeEntity($alt_order) {
1066 /* If we are dealing with alternative parts then we */
1067 /* choose the best viewable message supported by SM. */
1070 foreach($this->entities
as $ent) {
1071 $type = $ent->header
->type0
. '/' . $ent->header
->type1
;
1072 if ($type == 'multipart/related') {
1073 $type = $ent->header
->getParameter('type');
1074 // Mozilla bug. Mozilla does not provide the parameter type.
1075 if (!$type) $type = 'text/html';
1077 $altCount = count($alt_order);
1078 for ($j = $best_view; $j < $altCount; ++
$j) {
1079 if (($alt_order[$j] == $type) && ($j >= $best_view)) {
1091 function findRelatedEntity() {
1093 $related_type = $this->header
->getParameter('type');
1094 // Mozilla bug. Mozilla does not provide the parameter type.
1095 if (!$related_type) $related_type = 'text/html';
1096 $entCount = count($this->entities
);
1097 for ($i = 0; $i < $entCount; ++
$i) {
1098 $type = $this->entities
[$i]->header
->type0
.'/'.$this->entities
[$i]->header
->type1
;
1099 if ($related_type == $type) {
1100 $msgs[] = $this->entities
[$i];
1107 * @param array $exclude_id
1108 * @param array $result
1111 function getAttachments($exclude_id=array(), $result = array()) {
1113 if (($this->type0 == 'message') &&
1114 ($this->type1 == 'rfc822') &&
1115 ($this->entity_id) ) {
1116 $this = $this->entities[0];
1119 if (count($this->entities
)) {
1120 foreach ($this->entities
as $entity) {
1122 foreach ($exclude_id as $excl) {
1123 if ($entity->entity_id
=== $excl) {
1129 if ($entity->type0
== 'multipart') {
1130 $result = $entity->getAttachments($exclude_id, $result);
1131 } else if ($entity->type0
!= 'multipart') {
1132 $result[] = $entity;
1138 foreach ($exclude_id as $excl) {
1139 $exclude = $exclude ||
($this->entity_id
== $excl);
1150 * Add attachment to message object
1151 * @param string $type attachment type
1152 * @param string $name attachment name
1153 * @param string $location path to attachment
1155 function initAttachment($type, $name, $location) {
1156 $attachment = new Message();
1157 $mime_header = new MessageHeader();
1158 $mime_header->setParameter('name', $name);
1159 // FIXME: duplicate code. see ContentType class
1160 $pos = strpos($type, '/');
1162 $mime_header->type0
= substr($type, 0, $pos);
1163 $mime_header->type1
= substr($type, $pos+
1);
1165 $mime_header->type0
= $type;
1167 $attachment->att_local_name
= $location;
1168 $disposition = new Disposition('attachment');
1169 $disposition->properties
['filename'] = $name;
1170 $mime_header->disposition
= $disposition;
1171 $attachment->mime_header
= $mime_header;
1172 $this->entities
[]=$attachment;
1176 * Delete all attachments from this object from disk.
1179 function purgeAttachments() {
1180 if ($this->att_local_name
) {
1181 global $username, $attachment_dir;
1182 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1183 if ( file_exists($hashed_attachment_dir . '/' . $this->att_local_name
) ) {
1184 unlink($hashed_attachment_dir . '/' . $this->att_local_name
);
1187 // recursively delete attachments from entities contained in this object
1188 for ($i=0, $entCount=count($this->entities
);$i< $entCount; ++
$i) {
1189 $this->entities
[$i]->purgeAttachments();