From be9aacaa5296dfca63fb3a01c2dc52538d1546aa Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Sat, 17 Nov 2012 12:31:31 -0500
Subject: [PATCH] Bring back lost localization for the about page

---
 program/include/rcube_imap.php |  191 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 149 insertions(+), 42 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index c3fb448..9054b6b 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -25,10 +25,10 @@
 /**
  * Interface class for accessing an IMAP server
  *
- * @package    Mail
+ * @package    Framework
+ * @subpackage Storage
  * @author     Thomas Bruederli <roundcube@gmail.com>
  * @author     Aleksander Machniak <alec@alec.pl>
- * @version    2.0
  */
 class rcube_imap extends rcube_storage
 {
@@ -359,11 +359,11 @@
 
         return array(
             $this->search_string,
-	        $this->search_set,
-        	$this->search_charset,
-        	$this->search_sort_field,
-        	$this->search_sorted,
-	    );
+            $this->search_set,
+            $this->search_charset,
+            $this->search_sort_field,
+            $this->search_sorted,
+        );
     }
 
 
@@ -401,15 +401,56 @@
      */
     public function check_permflag($flag)
     {
-        $flag = strtoupper($flag);
-        $imap_flag = $this->conn->flags[$flag];
+        $flag       = strtoupper($flag);
+        $imap_flag  = $this->conn->flags[$flag];
+        $perm_flags = $this->get_permflags($this->folder);
 
-        if ($this->folder !== null) {
-            $this->check_connection();
+        return in_array_nocase($imap_flag, $perm_flags);
+    }
+
+
+    /**
+     * Returns PERMANENTFLAGS of the specified folder
+     *
+     * @param  string $folder Folder name
+     *
+     * @return array Flags
+     */
+    public function get_permflags($folder)
+    {
+        if (!strlen($folder)) {
+            return array();
         }
-        // @TODO: cache permanent flags (?)
+/*
+        Checking PERMANENTFLAGS is rather rare, so we disable caching of it
+        Re-think when we'll use it for more than only MDNSENT flag
 
-        return (in_array_nocase($imap_flag, $this->conn->data['PERMANENTFLAGS']));
+        $cache_key = 'mailboxes.permanentflags.' . $folder;
+        $permflags = $this->get_cache($cache_key);
+
+        if ($permflags !== null) {
+            return explode(' ', $permflags);
+        }
+*/
+        if (!$this->check_connection()) {
+            return array();
+        }
+
+        if ($this->conn->select($folder)) {
+            $permflags = $this->conn->data['PERMANENTFLAGS'];
+        }
+        else {
+            return array();
+        }
+
+        if (!is_array($permflags)) {
+            $permflags = array();
+        }
+/*
+        // Store permflags as string to limit cached object size
+        $this->update_cache($cache_key, implode(' ', $permflags));
+*/
+        return $permflags;
     }
 
 
@@ -971,8 +1012,8 @@
                 $a_msg_headers, $this->sort_field, $this->sort_order);
 
             // only return the requested part of the set
-            $a_msg_headers = array_slice(array_values($a_msg_headers),
-                $from, min($cnt-$to, $this->page_size));
+            $slice_length  = min($this->page_size, $cnt - ($to > $cnt ? $from : $to));
+            $a_msg_headers = array_slice(array_values($a_msg_headers), $from, $slice_length);
 
             if ($slice) {
                 $a_msg_headers = array_slice($a_msg_headers, -$slice, $slice);
@@ -1393,6 +1434,12 @@
             $criteria = 'UNDELETED '.$criteria;
         }
 
+        // unset CHARSET if criteria string is ASCII, this way
+        // SEARCH won't be re-sent after "unsupported charset" response
+        if ($charset && $charset != 'US-ASCII' && is_ascii($criteria)) {
+            $charset = 'US-ASCII';
+        }
+
         if ($this->threading) {
             $threads = $this->conn->thread($folder, $this->threading, $criteria, true, $charset);
 
@@ -1424,7 +1471,7 @@
         }
 
         $messages = $this->conn->search($folder,
-            ($charset ? "CHARSET $charset " : '') . $criteria, true);
+            ($charset && $charset != 'US-ASCII' ? "CHARSET $charset " : '') . $criteria, true);
 
         // Error, try with US-ASCII (some servers may support only US-ASCII)
         if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
@@ -1594,11 +1641,24 @@
                 $structure[1] = $m[2];
             }
             else {
-                return $headers;
+                // Try to parse the message using Mail_mimeDecode package
+                // We need a better solution, Mail_mimeDecode parses message
+                // in memory, which wouldn't work for very big messages,
+                // (it uses up to 10x more memory than the message size)
+                // it's also buggy and not actively developed
+                if ($headers->size && rcube_utils::mem_check($headers->size * 10)) {
+                    $raw_msg = $this->get_raw_body($uid);
+                    $struct = rcube_mime::parse_message($raw_msg);
+                }
+                else {
+                    return $headers;
+                }
             }
         }
 
-        $struct = $this->structure_part($structure, 0, '', $headers);
+        if (empty($struct)) {
+            $struct = $this->structure_part($structure, 0, '', $headers);
+        }
 
         // don't trust given content-type
         if (empty($struct->parts) && !empty($headers->ctype)) {
@@ -2014,7 +2074,7 @@
 
         if ($o_part && $o_part->size) {
             $body = $this->conn->handlePartBody($this->folder, $uid, true,
-                $part ? $part : 'TEXT', $o_part->encoding, $print, $fp);
+                $part ? $part : 'TEXT', $o_part->encoding, $print, $fp, $o_part->ctype_primary == 'text');
         }
 
         if ($fp || $print) {
@@ -2084,14 +2144,17 @@
 
     /**
      * Sends the whole message source to stdout
+     *
+     * @param int  $uid       Message UID
+     * @param bool $formatted Enables line-ending formatting
      */
-    public function print_raw_body($uid)
+    public function print_raw_body($uid, $formatted = true)
     {
         if (!$this->check_connection()) {
             return;
         }
 
-        $this->conn->handlePartBody($this->folder, $uid, true, NULL, NULL, true);
+        $this->conn->handlePartBody($this->folder, $uid, true, null, null, true, null, $formatted);
     }
 
 
@@ -2156,23 +2219,33 @@
      * @param string  $message The message source string or filename
      * @param string  $headers Headers string if $message contains only the body
      * @param boolean $is_file True if $message is a filename
+     * @param array   $flags   Message flags
+     * @param mixed   $date    Message internal date
      *
      * @return int|bool Appended message UID or True on success, False on error
      */
-    public function save_message($folder, &$message, $headers='', $is_file=false)
+    public function save_message($folder, &$message, $headers='', $is_file=false, $flags = array(), $date = null)
     {
         if (!strlen($folder)) {
             $folder = $this->folder;
         }
 
+        if (!$this->check_connection()) {
+            return false;
+        }
+
         // make sure folder exists
-        if ($this->folder_exists($folder)) {
-            if ($is_file) {
-                $saved = $this->conn->appendFromFile($folder, $message, $headers);
-            }
-            else {
-                $saved = $this->conn->append($folder, $message);
-            }
+        if (!$this->folder_exists($folder)) {
+            return false;
+        }
+
+        $date = $this->date_format($date);
+
+        if ($is_file) {
+            $saved = $this->conn->appendFromFile($folder, $message, $headers, $flags, $date);
+        }
+        else {
+            $saved = $this->conn->append($folder, $message, $flags, $date);
         }
 
         if ($saved) {
@@ -2872,11 +2945,11 @@
 
         // get list of subscribed folders
         if ((strpos($folder, '%') === false) && (strpos($folder, '*') === false)) {
-            $a_subscribed = $this->_list_folders_subscribed('', $folder . $delm . '*');
+            $a_subscribed = $this->list_folders_subscribed('', $folder . $delm . '*');
             $subscribed   = $this->folder_exists($folder, true);
         }
         else {
-            $a_subscribed = $this->_list_folders_subscribed();
+            $a_subscribed = $this->list_folders_subscribed();
             $subscribed   = in_array($folder, $a_subscribed);
         }
 
@@ -3175,8 +3248,9 @@
         $cache_key = 'mailboxes.folder-info.' . $folder;
         $cached = $this->get_cache($cache_key);
 
-        if (is_array($cached))
+        if (is_array($cached)) {
             return $cached;
+        }
 
         $acl       = $this->get_capability('ACL');
         $namespace = $this->get_namespace();
@@ -3213,10 +3287,9 @@
         $options['name']       = $folder;
         $options['attributes'] = $this->folder_attributes($folder, true);
         $options['namespace']  = $this->folder_namespace($folder);
-        $options['rights']     = $acl && !$options['is_root'] ? (array)$this->my_rights($folder) : array();
         $options['special']    = in_array($folder, $this->default_folders);
 
-        // Set 'noselect' and 'norename' flags
+        // Set 'noselect' flag
         if (is_array($options['attributes'])) {
             foreach ($options['attributes'] as $attrib) {
                 $attrib = strtolower($attrib);
@@ -3229,6 +3302,12 @@
             $options['noselect'] = true;
         }
 
+        // Get folder rights (MYRIGHTS)
+        if ($acl && ($rights = $this->my_rights($folder))) {
+            $options['rights'] = $rights;
+        }
+
+        // Set 'norename' flag
         if (!empty($options['rights'])) {
             $options['norename'] = !in_array('x', $options['rights']) && !in_array('d', $options['rights']);
 
@@ -3688,9 +3767,9 @@
     {
         if ($this->messages_caching && !$this->mcache) {
             $rcube = rcube::get_instance();
-            if ($dbh = $rcube->get_dbh()) {
+            if (($dbh = $rcube->get_dbh()) && ($userid = $rcube->get_user_id())) {
                 $this->mcache = new rcube_imap_cache(
-                    $dbh, $this, $rcube->get_user_id(), $this->options['skip_deleted']);
+                    $dbh, $this, $userid, $this->options['skip_deleted']);
             }
         }
 
@@ -3784,12 +3863,12 @@
     protected function rsort($folder, $delimiter, &$list, &$out)
     {
         while (list($key, $name) = each($list)) {
-	        if (strpos($name, $folder.$delimiter) === 0) {
-	            // set the type of folder name variable (#1485527)
-    	        $out[] = (string) $name;
-	            unset($list[$key]);
-	            $this->rsort($name, $delimiter, $list, $out);
-	        }
+            if (strpos($name, $folder.$delimiter) === 0) {
+                // set the type of folder name variable (#1485527)
+                $out[] = (string) $name;
+                unset($list[$key]);
+                $this->rsort($name, $delimiter, $list, $out);
+            }
         }
         reset($list);
     }
@@ -3909,6 +3988,29 @@
 
 
     /**
+     * Converts date string/object into IMAP date/time format
+     */
+    protected function date_format($date)
+    {
+        if (empty($date)) {
+            return null;
+        }
+
+        if (!is_object($date) || !is_a($date, 'DateTime')) {
+            try {
+                $timestamp = rcube_utils::strtotime($date);
+                $date      = new DateTime("@".$timestamp);
+            }
+            catch (Exception $e) {
+                return null;
+            }
+        }
+
+        return $date->format('d-M-Y H:i:s O');
+    }
+
+
+    /**
      * This is our own debug handler for the IMAP connection
      * @access public
      */
@@ -4017,6 +4119,11 @@
         return $this->delete_folder($folder);
     }
 
+    function clear_mailbox($folder = null)
+    {
+        return $this->clear_folder($folder);
+    }
+
     public function mailbox_exists($folder, $subscription=false)
     {
         return $this->folder_exists($folder, $subscription);

--
Gitblit v1.9.1