From 90f81a6c8de5aecfa36c54cc5260d25ba883aa51 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 08 Dec 2010 07:52:04 -0500
Subject: [PATCH] - Better support for READ-ONLY and NOPERM responses handling (#1487083) - Add confirmation message on purge/expunge commands response - Fix CLOSE was called on unselected mailbox

---
 program/include/rcube_imap.php |   72 ++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index f0d1119..d1947c4 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -100,6 +100,16 @@
         'RETURN-PATH',
     );
 
+    const UNKNOWN       = 0;
+    const NOPERM        = 1;
+    const READONLY      = 2;
+    const TRYCREATE     = 3;
+    const INUSE         = 4;
+    const OVERQUOTA     = 5;
+    const ALREADYEXISTS = 6;
+    const NONEXISTENT   = 7;
+    const CONTACTADMIN  = 8;
+
 
     /**
      * Object constructor
@@ -220,7 +230,51 @@
      */
     function get_error_str()
     {
-        return ($this->conn) ? $this->conn->error : '';
+        return ($this->conn) ? $this->conn->error : null;
+    }
+
+
+    /**
+     * Returns code of last command response
+     *
+     * @return int Response code
+     */
+    function get_response_code()
+    {
+        if (!$this->conn)
+            return self::UNKNOWN;
+
+        switch ($this->conn->resultcode) {
+            case 'NOPERM':
+                return self::NOPERM;
+            case 'READ-ONLY':
+                return self::READONLY;
+            case 'TRYCREATE':
+                return self::TRYCREATE;
+            case 'INUSE':
+                return self::INUSE;
+            case 'OVERQUOTA':
+                return self::OVERQUOTA;
+            case 'ALREADYEXISTS':
+                return self::ALREADYEXISTS;
+            case 'NONEXISTENT':
+                return self::NONEXISTENT;
+            case 'CONTACTADMIN':
+                return self::CONTACTADMIN;
+            default:
+                return self::UNKNOWN;
+        }
+    }
+
+
+    /**
+     * Returns last command response
+     *
+     * @return string Response
+     */
+    function get_response_str()
+    {
+        return ($this->conn) ? $this->conn->result : null;
     }
 
 
@@ -295,9 +349,9 @@
      * @param  string $mailbox Mailbox/Folder name
      * @access public
      */
-    function select_mailbox($mailbox)
+    function select_mailbox($mailbox=null)
     {
-        $mailbox = $this->mod_mailbox($mailbox);
+        $mailbox = strlen($mailbox) ? $this->mod_mailbox($mailbox) : $this->mailbox;
 
         $selected = $this->conn->select($mailbox);
 
@@ -2769,6 +2823,18 @@
         else
             $a_uids = NULL;
 
+        // force mailbox selection and check if mailbox is writeable
+        // to prevent a situation when CLOSE is executed on closed
+        // or EXPUNGE on read-only mailbox
+        $result = $this->conn->select($mailbox);
+        if (!$result) {
+            return false;
+        }
+        if (!$this->conn->data['READ-WRITE']) {
+            $this->conn->setError(rcube_imap_generic::ERROR_READONLY, "Mailbox is read-only");
+            return false;
+        }
+
         // CLOSE(+SELECT) should be faster than EXPUNGE
         if (empty($a_uids) || $a_uids == '1:*')
             $result = $this->conn->close();

--
Gitblit v1.9.1