From e99b3f2e6e83c38d959e52e09fd833ec90fd39b2 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Tue, 20 Sep 2011 15:17:04 -0400
Subject: [PATCH] - Fix listing messages on servers without SORT and THREAD command

---
 program/include/rcube_imap.php |   53 ++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index d14a643..03dc90a 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -176,12 +176,13 @@
         $attempt = 0;
         do {
             $data = rcmail::get_instance()->plugins->exec_hook('imap_connect',
-                array('host' => $host, 'user' => $user, 'attempt' => ++$attempt));
+                array_merge($this->options, array('host' => $host, 'user' => $user,
+                    'attempt' => ++$attempt)));
 
             if (!empty($data['pass']))
                 $pass = $data['pass'];
 
-            $this->conn->connect($data['host'], $data['user'], $pass, $this->options);
+            $this->conn->connect($data['host'], $data['user'], $pass, $data);
         } while(!$this->conn->connected() && $data['retry']);
 
         $this->host = $data['host'];
@@ -426,7 +427,7 @@
     function set_search_set($str=null, $msgs=null, $charset=null, $sort_field=null, $threads=false, $sorted=false)
     {
         if (is_array($str) && $msgs == null)
-            list($str, $msgs, $charset, $sort_field, $threads) = $str;
+            list($str, $msgs, $charset, $sort_field, $threads, $sorted) = $str;
         if ($msgs === false)
             $msgs = array();
         else if ($msgs != null && !is_array($msgs))
@@ -813,7 +814,7 @@
             $mailbox = $this->mailbox;
         }
 
-        return $this->_list_headers($mailbox, $page, $sort_field, $sort_order, false, $slice);
+        return $this->_list_headers($mailbox, $page, $sort_field, $sort_order, $slice);
     }
 
 
@@ -910,7 +911,7 @@
         }
         // fetch specified header for all messages and sort
         else if ($msg_index = $this->conn->fetchHeaderIndex($mailbox, "1:*",
-            $this->sort_field, $this->skip_deleted, true)
+            $this->sort_field, $this->skip_deleted)
         ) {
             asort($msg_index); // ASC
             $msg_index = array_keys($msg_index);
@@ -922,7 +923,7 @@
                 $msg_index = array_slice($msg_index, ($this->sort_order == 'DESC' ? 0 : -$slice), $slice);
 
             // fetch reqested headers from server
-            $a_msg_headers = $this->fetch_headers($mailbox, $msg_index, true);
+            $a_msg_headers = $this->fetch_headers($mailbox, $msg_index);
         }
 
         // return empty array if no messages found
@@ -1086,7 +1087,7 @@
 
             if (!empty($parents)) {
                 $headers[$idx]->parent_uid = end($parents);
-                if (!$header->seen)
+                if (empty($header->flags['SEEN']))
                     $headers[$parents[0]]->unread_children++;
             }
             array_push($parents, $header->uid);
@@ -1504,7 +1505,10 @@
         // use message index sort as default sorting
         if (!$sort_field) {
             if ($this->skip_deleted) {
-                $a_index = $this->_search_index($mailbox, 'ALL');
+                $a_index = $this->conn->search($mailbox, 'ALL UNDELETED');
+                // I didn't found that SEARCH should return sorted IDs
+                if (is_array($a_index))
+                    sort($a_index);
             } else if ($max = $this->_messagecount($mailbox)) {
                 $a_index = range(1, $max);
             }
@@ -2387,13 +2391,16 @@
         }
 
         // convert charset (if text or message part)
-        if ($body && !$skip_charset_conv &&
-            preg_match('/^(text|message)$/', $o_part->ctype_primary)
-        ) {
-            if (!$o_part->charset || strtoupper($o_part->charset) == 'US-ASCII') {
-                $o_part->charset = $this->default_charset;
+        if ($body && preg_match('/^(text|message)$/', $o_part->ctype_primary)) {
+            // Remove NULL characters (#1486189)
+            $body = str_replace("\x00", '', $body);
+
+           if (!$skip_charset_conv) {
+                if (!$o_part->charset || strtoupper($o_part->charset) == 'US-ASCII') {
+                    $o_part->charset = $this->default_charset;
+                }
+                $body = rcube_charset_convert($body, $o_part->charset);
             }
-            $body = rcube_charset_convert($body, $o_part->charset);
         }
 
         return $body;
@@ -3418,6 +3425,8 @@
         if ($this->conn->selected != $mailbox) {
             if ($this->conn->select($mailbox))
                 $this->mailbox = $mailbox;
+            else
+                return null;
         }
 
         $data = $this->conn->data;
@@ -3497,7 +3506,8 @@
         }
 
         if (!empty($options['rights'])) {
-            $options['norename'] = !in_array('x', $options['rights']);
+            $options['norename'] = !in_array('x', $options['rights']) && !in_array('d', $options['rights']);
+
             if (!$options['noselect']) {
                 $options['noselect'] = !in_array('r', $options['rights']);
             }
@@ -3513,6 +3523,19 @@
 
 
     /**
+     * Synchronizes messages cache.
+     *
+     * @param string $mailbox Folder name
+     */
+    public function mailbox_sync($mailbox)
+    {
+        if ($mcache = $this->get_mcache_engine()) {
+            $mcache->synchronize($mailbox);
+        }
+    }
+
+
+    /**
      * Get message header names for rcube_imap_generic::fetchHeader(s)
      *
      * @return string Space-separated list of header names

--
Gitblit v1.9.1