From d04d202234b0ba1e65b1c581acf0cbe715120dd7 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 22 Nov 2006 06:49:30 -0500
Subject: [PATCH] Remove newlines from mail headers (#1484031)

---
 program/include/rcube_imap.inc |  160 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 107 insertions(+), 53 deletions(-)

diff --git a/program/include/rcube_imap.inc b/program/include/rcube_imap.inc
index 185565c..fafef67 100644
--- a/program/include/rcube_imap.inc
+++ b/program/include/rcube_imap.inc
@@ -35,7 +35,7 @@
  *
  * @package    RoundCube Webmail
  * @author     Thomas Bruederli <roundcube@gmail.com>
- * @version    1.31
+ * @version    1.34
  * @link       http://ilohamail.org
  */
 class rcube_imap
@@ -132,11 +132,10 @@
                        'message' => $GLOBALS['iil_error']), TRUE, FALSE);
       }
 
-    // get account namespace
+    // get server properties
     if ($this->conn)
       {
       $this->_parse_capability($this->conn->capability);
-      iil_C_NameSpace($this->conn);
       
       if (!empty($this->conn->delimiter))
         $this->delimiter = $this->conn->delimiter;
@@ -883,15 +882,19 @@
     $uid = $is_uid ? $id : $this->_id2uid($id);
 
     // get cached headers
-    if ($uid && ($headers = $this->get_cached_message($mailbox.'.msg', $uid)))
+    if ($uid && ($headers = &$this->get_cached_message($mailbox.'.msg', $uid)))
       return $headers;
 
-    $msg_id = $is_uid ? $this->_uid2id($id) : $id;
-    $headers = iil_C_FetchHeader($this->conn, $mailbox, $msg_id);
+    $headers = iil_C_FetchHeader($this->conn, $mailbox, $id, $is_uid);
 
     // write headers cache
     if ($headers)
-      $this->add_message_cache($mailbox.'.msg', $msg_id, $headers);
+      {
+      if ($is_uid)
+        $this->uid_id_map[$mbox_name][$uid] = $headers->id;
+
+      $this->add_message_cache($mailbox.'.msg', $headers->id, $headers);
+      }
 
     return $headers;
     }
@@ -906,6 +909,14 @@
    */
   function &get_structure($uid)
     {
+    $cache_key = $this->mailbox.'.msg';
+    $headers = &$this->get_cached_message($cache_key, $uid, true);
+
+    // return cached message structure
+    if (is_object($headers) && is_object($headers->structure))
+      return $headers->structure;
+    
+    // resolve message sequence number
     if (!($msg_id = $this->_uid2id($uid)))
       return FALSE;
 
@@ -921,14 +932,18 @@
       
       $struct = &$this->_structure_part($structure);
       $struct->headers = get_object_vars($headers);
-      
+
       // don't trust given content-type
-      if (empty($struct->parts))
+      if (empty($struct->parts) && !empty($struct->headers['ctype']))
         {
         $struct->mime_id = '1';
         $struct->mimetype = strtolower($struct->headers['ctype']);
         list($struct->ctype_primary, $struct->ctype_secondary) = explode('/', $struct->mimetype);
         }
+
+      // write structure to cache
+      if ($this->caching_enabled)
+        $this->add_message_cache($cache_key, $msg_id, $headers, $struct);
       }
 	
 	return $struct;
@@ -997,8 +1012,9 @@
 	  $struct->size = intval($part[6]);
 
 	// read part disposition
-    $di = count($part) - 3;
-    if (is_array($part[$di]))
+    $di = count($part) - 2;
+    if ((is_array($part[$di]) && count($part[$di]) == 2 && is_array($part[$di][1])) ||
+        (is_array($part[--$di]) && count($part[$di]) == 2))
       {
       $struct->disposition = strtolower($part[$di][0]);
 
@@ -1063,7 +1079,7 @@
       
     if (is_array($part->parts))
       for ($i=0; $i<count($part->parts); $i++)
-        $this->_get_part_numbers($part->parts[$i], &$a_parts);
+        $this->_get_part_numbers($part->parts[$i], $a_parts);
     }
   
 
@@ -1109,8 +1125,14 @@
         $body = $this->mime_decode($body, $o_part->encoding);
 
       // convert charset (if text or message part)
-      if (!empty($o_part->charset) && ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message') && !stristr($body, 'charset='))
+      if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message')
+        {
+        // assume ISO-8859-1 if no charset specified
+        if (empty($o_part->charset))
+          $o_part->charset = 'ISO-8859-1';
+
         $body = rcube_charset_convert($body, $o_part->charset);
+        }
       }
 
     return $body;
@@ -1442,12 +1464,8 @@
   function get_quota()
     {
     if ($this->get_capability('QUOTA'))
-      {
-      $result = iil_C_GetQuota($this->conn);
-      if ($result["total"])
-        return sprintf("%.2fMB / %.2fMB (%.0f%%)", $result["used"] / 1000.0, $result["total"] / 1000.0, $result["percent"]);       
-      }
-
+      return iil_C_GetQuota($this->conn);
+	
     return FALSE;
     }
 
@@ -1533,6 +1551,14 @@
     // make absolute path
     $mailbox = $this->_mod_mailbox($mbox_name);
     $abs_name = $this->_mod_mailbox($name);
+    
+    // check if mailbox is subscribed
+    $a_subscribed = $this->_list_mailboxes();
+    $subscribed = in_array($mailbox, $a_subscribed);
+    
+    // unsubscribe folder
+    if ($subscribed)
+      iil_C_UnSubscribe($this->conn, $mailbox);
 
     if (strlen($abs_name))
       $result = iil_C_RenameFolder($this->conn, $mailbox, $abs_name);
@@ -1541,11 +1567,12 @@
     if ($result)
       {
       $this->clear_message_cache($mailbox.'.msg');
-      $this->clear_cache('mailboxes');
+      $this->clear_cache('mailboxes');      
       }
-      
+
     // try to subscribe it
-    $this->subscribe($name);
+    if ($result && $subscribed)
+      iil_C_Subscribe($this->conn, $abs_name);
 
     return $result ? $name : FALSE;
     }
@@ -1734,7 +1761,7 @@
       {
       $this->db->query(
         "UPDATE ".get_table_name('cache')."
-         SET    created=now(),
+         SET    created=".$this->db->now().",
                 data=?
          WHERE  user_id=?
          AND    cache_key=?",
@@ -1748,7 +1775,7 @@
       $this->db->query(
         "INSERT INTO ".get_table_name('cache')."
          (created, user_id, cache_key, data)
-         VALUES (now(), ?, ?, ?)",
+         VALUES (".$this->db->now().", ?, ?, ?)",
         $_SESSION['user_id'],
         $key,
         $data);
@@ -1842,18 +1869,16 @@
     }
 
 
-  function &get_cached_message($key, $uid, $body=FALSE)
+  function &get_cached_message($key, $uid, $struct=false)
     {
     if (!$this->caching_enabled)
       return FALSE;
 
     $internal_key = '__single_msg';
-    if ($this->caching_enabled && (!isset($this->cache[$internal_key][$uid]) || $body))
+    if ($this->caching_enabled && (!isset($this->cache[$internal_key][$uid]) ||
+        ($struct && empty($this->cache[$internal_key][$uid]->structure))))
       {
-      $sql_select = "idx, uid, headers";
-      if ($body)
-        $sql_select .= ", body";
-      
+      $sql_select = "idx, uid, headers" . ($struct ? ", structure" : '');
       $sql_result = $this->db->query(
         "SELECT $sql_select
          FROM ".get_table_name('messages')."
@@ -1863,14 +1888,12 @@
         $_SESSION['user_id'],
         $key,
         $uid);
-      
+
       if ($sql_arr = $this->db->fetch_assoc($sql_result))
         {
-        $headers = unserialize($sql_arr['headers']);
-        if (is_object($headers) && !empty($sql_arr['body']))
-          $headers->body = $sql_arr['body'];
-
-        $this->cache[$internal_key][$uid] = $headers;
+        $this->cache[$internal_key][$uid] = unserialize($sql_arr['headers']);
+        if (is_object($this->cache[$internal_key][$uid]) && !empty($sql_arr['structure']))
+          $this->cache[$internal_key][$uid]->structure = unserialize($sql_arr['structure']);
         }
       }
 
@@ -1906,25 +1929,55 @@
     }
 
 
-  function add_message_cache($key, $index, $headers)
+  function add_message_cache($key, $index, $headers, $struct=null)
     {
-    if (!$key || !is_object($headers) || empty($headers->uid))
+    if (empty($key) || !is_object($headers) || empty($headers->uid))
       return;
+      
+    // check for an existing record (probly headers are cached but structure not)
+    $sql_result = $this->db->query(
+        "SELECT message_id
+         FROM ".get_table_name('messages')."
+         WHERE  user_id=?
+         AND    cache_key=?
+         AND    uid=?
+         AND    del<>1",
+        $_SESSION['user_id'],
+        $key,
+        $headers->uid);
 
-    $this->db->query(
-      "INSERT INTO ".get_table_name('messages')."
-       (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers)
-       VALUES (?, 0, ?, now(), ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?)",
-      $_SESSION['user_id'],
-      $key,
-      $index,
-      $headers->uid,
-      (string)substr($this->decode_header($headers->subject, TRUE), 0, 128),
-      (string)substr($this->decode_header($headers->from, TRUE), 0, 128),
-      (string)substr($this->decode_header($headers->to, TRUE), 0, 128),
-      (string)substr($this->decode_header($headers->cc, TRUE), 0, 128),
-      (int)$headers->size,
-      serialize($headers));
+    // update cache record
+    if ($sql_arr = $this->db->fetch_assoc($sql_result))
+      {
+      $this->db->query(
+        "UPDATE ".get_table_name('messages')."
+         SET   idx=?, headers=?, structure=?
+         WHERE message_id=?",
+        $index,
+        serialize($headers),
+        is_object($struct) ? serialize($struct) : NULL,
+        $sql_arr['message_id']
+        );
+      }
+    else  // insert new record
+      {
+      $this->db->query(
+        "INSERT INTO ".get_table_name('messages')."
+         (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers, structure)
+         VALUES (?, 0, ?, ".$this->db->now().", ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?, ?)",
+        $_SESSION['user_id'],
+        $key,
+        $index,
+        $headers->uid,
+        (string)substr($this->decode_header($headers->subject, TRUE), 0, 128),
+        (string)substr($this->decode_header($headers->from, TRUE), 0, 128),
+        (string)substr($this->decode_header($headers->to, TRUE), 0, 128),
+        (string)substr($this->decode_header($headers->cc, TRUE), 0, 128),
+        (int)$headers->size,
+        serialize($headers),
+        is_object($struct) ? serialize($struct) : NULL
+        );
+      }
     }
     
     
@@ -2324,7 +2377,8 @@
 
   function _parse_address_list($str)
     {
-    $a = $this->_explode_quoted_string(',', $str);
+    // remove any newlines and carriage returns before
+    $a = $this->_explode_quoted_string(',', preg_replace( "/[\r\n]/", " ", $str));
     $result = array();
     
     foreach ($a as $key => $val)

--
Gitblit v1.9.1