Better stylesheet ordering
[squirrelmail.git] / class / mime / Message.class.php
index 1480534f8d05ffd60be3711a544a3a4303f93e30..1c48300195cf2784ef12b84aac97f1aaf19ad2ea 100644 (file)
 /**
  * Message.class.php
  *
- * Copyright (c) 2003 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
+ * This file contains functions needed to handle mime messages.
  *
-  This contains functions needed to handle mime messages.
- *
- * $Id$
+ * @copyright © 2003-2006 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @version $Id$
+ * @package squirrelmail
+ * @subpackage mime
+ * @since 1.3.2
  */
 
+/**
+ * The object that contains a message.
+ *
+ * message is the object that contains messages. It is a recursive object in
+ * that through the $entities variable, it can contain more objects of type
+ * message. See documentation in mime.txt for a better description of how this
+ * works.
+ * @package squirrelmail
+ * @subpackage mime
+ * @since 1.3.0
+ */
 class Message {
-    /** message is the object that contains messages.  It is a recursive
-        object in that through the $entities variable, it can contain
-        more objects of type message.  See documentation in mime.txt for
-        a better description of how this works.
-    **/
-    var $rfc822_header = '',
-        $mime_header = '',
-        $flags = '',
-        $type0='',
-        $type1='',
-        $entities = array(),
-        $entity_id = '',
-        $parent_ent, $entity,
-        $parent = '', $decoded_body='',
-        $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
-        $is_mdnsent = 0,
-        $body_part = '',
-        $offset = 0,  /* for fetching body parts out of raw messages */
-        $length = 0,  /* for fetching body parts out of raw messages */
-        $att_local_name = ''; /* location where the tempory attachment
-                             is stored. For future usage in smtp.php */
+    /**
+     * rfc822header object
+     * @var object
+     */
+    var $rfc822_header = '';
+    /**
+     * MessageHeader object
+     * @var object
+     */
+    var $mime_header = '';
+    /**
+     * @var mixed
+     */
+    var $flags = '';
+    /**
+     * Media type
+     * @var string
+     */
+    var $type0='';
+    /**
+     * Media subtype
+     * @var string
+     */
+    var $type1='';
+    /**
+     * Nested mime parts
+     * @var array
+     */
+    var $entities = array();
+    /**
+     * Message part id
+     * @var string
+     */
+    var $entity_id = '';
+    /**
+     * Parent message part id
+     * @var string
+     */
+    var $parent_ent;
+    /**
+     * @var mixed
+     */
+    var $entity;
+    /**
+     * @var mixed
+     */
+    var $parent = '';
+    /**
+     * @var string
+     */
+    var $decoded_body='';
+    /**
+     * Message \seen status
+     * @var boolean
+     */
+    var $is_seen = 0;
+    /**
+     * Message \answered status
+     * @var boolean
+     */
+    var $is_answered = 0;
+    /**
+     * Message \deleted status
+     * @var boolean
+     */
+    var $is_deleted = 0;
+    /**
+     * Message \flagged status
+     * @var boolean
+     */
+    var $is_flagged = 0;
+    /**
+     * Message mdn status
+     * @var boolean
+     */
+    var $is_mdnsent = 0;
+    /**
+     * Message text body
+     * @var string
+     */
+    var $body_part = '';
+    /**
+     * Message part offset
+     * for fetching body parts out of raw messages
+     * @var integer
+     */
+    var $offset = 0;
+    /**
+     * Message part length
+     * for fetching body parts out of raw messages
+     * @var integer
+     */
+    var $length = 0;
+    /**
+     * Local attachment filename location where the tempory attachment is
+     * stored. For use in delivery class.
+     * @var string
+     */
+    var $att_local_name = '';
 
+    /**
+     * @param string $ent entity id
+     */
     function setEnt($ent) {
         $this->entity_id= $ent;
     }
 
+    /**
+     * Add nested message part
+     * @param object $msg
+     */
     function addEntity ($msg) {
         $this->entities[] = $msg;
     }
 
+    /**
+     * Get file name used for mime part
+     * @return string file name
+     * @since 1.3.2
+     */
     function getFilename() {
-        $filename = '';
-        $filename = $this->header->getParameter('filename');
-        if (!$filename) {
-            $filename = $this->header->getParameter('name');
-        }
-
-        if (!$filename) {
-            $filename = 'untitled-'.$this->entity_id;
-        }
-        return $filename;
+         $filename = '';
+         $header = $this->header;
+         if (is_object($header->disposition)) {
+              $filename = $header->disposition->getProperty('filename');
+              if (trim($filename) == '') {
+                  $name = decodeHeader($header->disposition->getProperty('name'));
+                  if (!trim($name)) {
+                      $name = $header->getParameter('name');
+                      if(!trim($name)) {
+                          if (!trim( $header->id )) {
+                              $filename = 'untitled-[' . $this->entity_id . ']' ;
+                          } else {
+                              $filename = 'cid: ' . $header->id;
+                          }
+                      } else {
+                          $filename = $name;
+                      }
+                  } else {
+                      $filename = $name;
+                  }
+              }
+         } else {
+              $filename = $header->getParameter('filename');
+              if (!trim($filename)) {
+                  $filename = $header->getParameter('name');
+                  if (!trim($filename)) {
+                      if (!trim( $header->id )) {
+                          $filename = 'untitled-[' . $this->entity_id . ']' ;
+                      } else {
+                          $filename = 'cid: ' . $header->id;
+                      }
+                  }
+              }
+         }
+         return $filename;
     }
 
-
+    /**
+     * Add header object to message object.
+     * WARNING: Unfinished code. Don't expect it to work in older sm versions.
+     * @param mixed $read array or string with message headers
+     * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object
+     */
     function addRFC822Header($read) {
         $header = new Rfc822Header();
         $this->rfc822_header = $header->parseHeader($read);
     }
 
+    /**
+     * @param string $ent
+     * @return mixed (object or string?)
+     */
     function getEntity($ent) {
         $cur_ent = $this->entity_id;
         $msg = $this;
@@ -70,7 +207,7 @@ class Message {
             $cur_ent_a = explode('.', $this->entity_id);
         }
         $ent_a = explode('.', $ent);
-        
+
         for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++$i) {
             if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
                 $msg = $msg->parent;
@@ -103,10 +240,17 @@ class Message {
         return $msg;
     }
 
+    /**
+     * Set message body
+     * @param string $s message body
+     */
     function setBody($s) {
         $this->body_part = $s;
     }
 
+    /**
+     * Clean message object
+     */
     function clean_up() {
         $msg = $this;
         $msg->body_part = '';
@@ -116,6 +260,9 @@ class Message {
         }
     }
 
+    /**
+     * @return string
+     */
     function getMailbox() {
         $msg = $this;
         while (is_object($msg->parent)) {
@@ -134,19 +281,30 @@ class Message {
      * Question/Bugs:
      *
      * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
-     *
+     * @param string $read
+     * @param integer $i
+     * @param mixed $sub_msg
+     * @return object Message object
+     * @todo define argument and return types
      */
     function parseStructure($read, &$i, $sub_msg = '') {
         $msg = Message::parseBodyStructure($read, $i, $sub_msg);
         if($msg) $msg->setEntIds($msg,false,0);
         return $msg;
     }
-    
+
+    /**
+     * @param object $msg
+     * @param mixed $init
+     * @param integer $i
+     * @todo document me
+     * @since 1.4.0
+     */
     function setEntIds(&$msg,$init=false,$i=0) {
         $iCnt = count($msg->entities);
         if ($init !==false) {
             $iEntSub = $i+1;
-            if ($msg->parent->type0 == 'message' && 
+            if ($msg->parent->type0 == 'message' &&
                 $msg->parent->type1 == 'rfc822' &&
                 $msg->type0 == 'multipart') {
                 $iEntSub = '0';
@@ -162,8 +320,8 @@ class Message {
             $msg->entity_id='1';
         }
         for ($i=0;$i<$iCnt;++$i) {
-            $msg->entities[$i]->parent =& $msg;    
-            if (strrchr($msg->entity_id, '.') != '.0') {    
+            $msg->entities[$i]->parent =& $msg;
+            if (strrchr($msg->entity_id, '.') != '.0') {
                 $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->entity_id,$i);
             } else {
                 $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->parent->entity_id,$i);
@@ -171,6 +329,14 @@ class Message {
         }
     }
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @param mixed $sub_msg
+     * @return object Message object
+     * @todo document me
+     * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
+     */
     function parseBodyStructure($read, &$i, $sub_msg = '') {
         $arg_no = 0;
         $arg_a  = array();
@@ -179,7 +345,7 @@ class Message {
         } else {
             $message = new Message();
         }
-        $this = $message;    
+
         for ($cnt = strlen($read); $i < $cnt; ++$i) {
             $char = strtoupper($read{$i});
             switch ($char) {
@@ -196,29 +362,29 @@ class Message {
                                 $msg->header->type0 = 'multipart';
                                 $msg->type0 = 'multipart';
                                 while ($read{$i} == '(') {
-                                    $msg->addEntity($this->parseBodyStructure($read, $i, $msg));
+                                    $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
                                 }
                             }
                             break;
                         case 1:
                             /* multipart properties */
                             ++$i;
-                            $arg_a[] = $this->parseProperties($read, $i);
+                            $arg_a[] = $msg->parseProperties($read, $i);
                             ++$arg_no;
                             break;
                         case 2:
                             if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
                                 ++$i;
-                                $arg_a[] = $this->parseDisposition($read, $i);
+                                $arg_a[] = $msg->parseDisposition($read, $i);
                             } else { /* properties */
-                                $arg_a[] = $this->parseProperties($read, $i);
+                                $arg_a[] = $msg->parseProperties($read, $i);
                             }
                             ++$arg_no;
                             break;
                         case 3:
                             if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
                                 ++$i;
-                                $arg_a[]= $this->parseLanguage($read, $i);
+                                $arg_a[]= $msg->parseLanguage($read, $i);
                             }
                         case 7:
                             if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
@@ -227,40 +393,40 @@ class Message {
                                 $msg->type0 = $arg_a[0];
                                 $msg->type1 = $arg_a[1];
                                 $rfc822_hdr = new Rfc822Header();
-                                $msg->rfc822_header = $this->parseEnvelope($read, $i, $rfc822_hdr);
+                                $msg->rfc822_header = $msg->parseEnvelope($read, $i, $rfc822_hdr);
                                 while (($i < $cnt) && ($read{$i} != '(')) {
                                     ++$i;
                                 }
-                                $msg->addEntity($this->parseBodyStructure($read, $i,$msg));
+                                $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
                             }
                             break;
                         case 8:
                             ++$i;
-                            $arg_a[] = $this->parseDisposition($read, $i);
+                            $arg_a[] = $msg->parseDisposition($read, $i);
                             ++$arg_no;
                             break;
                         case 9:
                             ++$i;
                             if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
-                                $arg_a[] = $this->parseDisposition($read, $i);
+                                $arg_a[] = $msg->parseDisposition($read, $i);
                             } else {
-                                $arg_a[] = $this->parseLanguage($read, $i);
+                                $arg_a[] = $msg->parseLanguage($read, $i);
                             }
                             ++$arg_no;
                             break;
                        case 10:
                            if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
                                ++$i;
-                               $arg_a[] = $this->parseLanguage($read, $i);
+                               $arg_a[] = $msg->parseLanguage($read, $i);
                            } else {
-                               $i = $this->parseParenthesis($read, $i);
+                               $i = $msg->parseParenthesis($read, $i);
                                $arg_a[] = ''; /* not yet described in rfc2060 */
                            }
                            ++$arg_no;
                            break;
                        default:
                            /* unknown argument, skip this part */
-                           $i = $this->parseParenthesis($read, $i);
+                           $i = $msg->parseParenthesis($read, $i);
                            $arg_a[] = '';
                            ++$arg_no;
                            break;
@@ -268,7 +434,7 @@ class Message {
                    break;
                 case '"':
                     /* inside an entity -> start processing */
-                    $arg_s = $this->parseQuote($read, $i);
+                    $arg_s = $msg->parseQuote($read, $i);
                     ++$arg_no;
                     if ($arg_no < 3) {
                         $arg_s = strtolower($arg_s); /* type0 and type1 */
@@ -287,10 +453,10 @@ class Message {
                     break;
                 case '{':
                     /* process the literal value */
-                    $arg_a[] = $this->parseLiteral($read, $i);
+                    $arg_a[] = $msg->parseLiteral($read, $i);
                     ++$arg_no;
                     break;
-        case '0':    
+        case '0':
                 case is_numeric($read{$i}):
                     /* process integers */
                     if ($read{$i} == ' ') { break; }
@@ -346,6 +512,11 @@ class Message {
         } /* for */
     } /* parsestructure */
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @return array
+     */
     function parseProperties($read, &$i) {
         $properties = array();
         $prop_name = '';
@@ -371,6 +542,12 @@ class Message {
         return $properties;
     }
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @param object $hdr MessageHeader object
+     * @return object MessageHeader object
+     */
     function parseEnvelope($read, &$i, $hdr) {
         $arg_no = 0;
         $arg_a = array();
@@ -438,7 +615,7 @@ class Message {
         if (count($arg_a) > 9) {
             $d = strtr($arg_a[0], array('  ' => ' '));
             $d = explode(' ', $d);
-        if (!$arg_a[1]) $arg_1[1] = _("(no subject)");        
+        if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
 
             $hdr->date = getTimeStamp($d); /* argument 1: date */
             $hdr->subject = $arg_a[1];     /* argument 2: subject */
@@ -454,6 +631,12 @@ class Message {
         return $hdr;
     }
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @return string
+     * @todo document me
+     */
     function parseLiteral($read, &$i) {
         $lit_cnt = '';
         ++$i;
@@ -474,6 +657,12 @@ class Message {
         return $s;
     }
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @return string
+     * @todo document me
+     */
     function parseQuote($read, &$i) {
         $s = '';
         $iPos = ++$i;
@@ -493,6 +682,11 @@ class Message {
         return $s;
     }
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @return object AddressStructure object
+     */
     function parseAddress($read, &$i) {
         $arg_a = array();
         for (; $read{$i} != ')'; ++$i) {
@@ -523,6 +717,11 @@ class Message {
         return $adr;
     }
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @param object Disposition object or empty string
+     */
     function parseDisposition($read, &$i) {
         $arg_a = array();
         for (; $read{$i} != ')'; ++$i) {
@@ -540,10 +739,14 @@ class Message {
                 $disp->properties = $arg_a[1];
             }
         }
-
         return (is_object($disp) ? $disp : '');
     }
 
+    /**
+     * @param string $read
+     * @param integer $i
+     * @return object Language object or empty string
+     */
     function parseLanguage($read, &$i) {
         /* no idea how to process this one without examples */
         $arg_a = array();
@@ -563,10 +766,15 @@ class Message {
                 $lang->properties = $arg_a[1];
             }
         }
-
         return (is_object($lang) ? $lang : '');
     }
 
+    /**
+     * Parse message text enclosed in parenthesis
+     * @param string $read
+     * @param integer $i
+     * @return integer
+     */
     function parseParenthesis($read, $i) {
         for (; $read{$i} != ')'; ++$i) {
             switch ($read{$i}) {
@@ -579,8 +787,15 @@ class Message {
         return $i;
     }
 
-    /* Function to fill the message structure in case the */
-    /* bodystructure is not available NOT FINISHED YET    */
+    /**
+     * Function to fill the message structure in case the
+     * bodystructure is not available
+     * NOT FINISHED YET
+     * @param string $read
+     * @param string $type0 message part type
+     * @param string $type1 message part subtype
+     * @return string (only when type0 is not message or multipart)
+     */
     function parseMessage($read, $type0, $type1) {
         switch ($type0) {
             case 'message':
@@ -648,6 +863,12 @@ class Message {
         }
     }
 
+    /**
+     * @param array $entity
+     * @param array $alt_order
+     * @param boolean $strict
+     * @return array
+     */
     function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
         $found = false;
         if ($this->type0 == 'multipart') {
@@ -673,10 +894,10 @@ class Message {
                 }
             } else { /* Treat as multipart/mixed */
                 foreach ($this->entities as $ent) {
-                    if((strtolower($ent->header->disposition->name) != 'attachment') &&
-                (!isset($ent->header->parameters['filename'])) &&
-                (!isset($ent->header->parameters['name'])) &&
-                       (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
+                    if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
+                            (!isset($ent->header->parameters['filename'])) &&
+                            (!isset($ent->header->parameters['name'])) &&
+                            (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
                         $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
                         $found = true;
                     }
@@ -687,10 +908,11 @@ class Message {
 //        $alt_order[] = "message/rfc822";
             foreach ($alt_order as $alt) {
                 if( ($alt == $type) && isset($this->entity_id) ) {
-                    if ((count($this->entities) == 0) && 
-                (!isset($ent->header->parameters['filename'])) &&
-                (!isset($ent->header->parameters['name'])) &&
-                        (strtolower($this->header->disposition->name) != 'attachment')) {
+                    if ((count($this->entities) == 0) &&
+                            (!isset($this->header->parameters['filename'])) &&
+                            (!isset($this->header->parameters['name'])) &&
+                            isset($this->header->disposition) && is_object($this->header->disposition) &&
+                            !(is_object($this->header->disposition) && strtolower($this->header->disposition->name) == 'attachment')) {
                         $entity[] = $this->entity_id;
                         $found = true;
                     }
@@ -699,7 +921,7 @@ class Message {
         }
         if(!$found) {
             foreach ($this->entities as $ent) {
-                if((strtolower($ent->header->disposition->name) != 'attachment') &&
+                if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
                    (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
                     $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
                     $found = true;
@@ -711,7 +933,7 @@ class Message {
                 in_array($this->type1, array('plain', 'html', 'message')) &&
                 isset($this->entity_id)) {
                 if (count($this->entities) == 0) {
-                    if (strtolower($this->header->disposition->name) != 'attachment') {
+                    if (!is_object($this->header->disposition) || strtolower($this->header->disposition->name) != 'attachment') {
                         $entity[] = $this->entity_id;
                     }
                 }
@@ -720,6 +942,10 @@ class Message {
         return $entity;
     }
 
+    /**
+     * @param array $alt_order
+     * @return array
+     */
     function findAlternativeEntity($alt_order) {
         /* If we are dealing with alternative parts then we  */
         /* choose the best viewable message supported by SM. */
@@ -740,10 +966,12 @@ class Message {
                 }
             }
         }
-
         return $entity;
     }
 
+    /**
+     * @return array
+     */
     function findRelatedEntity() {
         $msgs = array();
         $related_type = $this->header->getParameter('type');
@@ -759,9 +987,14 @@ class Message {
         return $msgs;
     }
 
+    /**
+     * @param array $exclude_id
+     * @param array $result
+     * @return array
+     */
     function getAttachments($exclude_id=array(), $result = array()) {
 /*
-        if (($this->type0 == 'message') && 
+        if (($this->type0 == 'message') &&
         ($this->type1 == 'rfc822') &&
         ($this->entity_id) ) {
             $this = $this->entities[0];
@@ -797,11 +1030,18 @@ class Message {
         }
         return $result;
     }
-    
+
+    /**
+     * Add attachment to message object
+     * @param string $type attachment type
+     * @param string $name attachment name
+     * @param string $location path to attachment
+     */
     function initAttachment($type, $name, $location) {
         $attachment = new Message();
         $mime_header = new MessageHeader();
         $mime_header->setParameter('name', $name);
+        // FIXME: duplicate code. see ContentType class
         $pos = strpos($type, '/');
         if ($pos > 0) {
             $mime_header->type0 = substr($type, 0, $pos);
@@ -816,6 +1056,18 @@ class Message {
         $attachment->mime_header = $mime_header;
         $this->entities[]=$attachment;
     }
-}
 
-?>
+    /**
+     * Delete all attachments from this object from disk.
+     * @since 1.5.1
+     */
+    function purgeAttachments() {
+        if ($this->att_local_name && file_exists($this->att_local_name)) {
+            unlink($this->att_local_name);
+        }
+        // recursively delete attachments from entities contained in this object
+        for ($i=0, $entCount=count($this->entities);$i< $entCount; ++$i) {
+            $this->entities[$i]->purgeAttachments();
+        }
+    }
+}