From 16378fe3a255943c7b2f374443158670a0eedf86 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Mon, 03 May 2010 02:41:57 -0400
Subject: [PATCH] - preformance fix: don't load full folders list when checking for folder   existance in sendmail.inc, use internal cache to make mailbox_exists()   faster when called more than one time for the same folder

---
 program/include/rcube_imap.php  |    5 +++++
 program/steps/mail/sendmail.inc |   19 ++++++++-----------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 9db6427..6e4d989 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -2774,6 +2774,10 @@
             if ($mbox_name == 'INBOX')
                 return true;
 
+            $key = $subscription ? 'subscribed' : 'existing';
+            if (is_array($this->icache[$key]) && in_array($mbox_name, $this->icache[$key]))
+                return true;
+
             if ($subscription) {
                 $a_folders = $this->conn->listSubscribed($this->mod_mailbox(''), $mbox_name);
             }
@@ -2782,6 +2786,7 @@
 	        }
 	        
             if (is_array($a_folders) && in_array($this->mod_mailbox($mbox_name), $a_folders)) {
+                $this->icache[$key][] = $mbox_name;
                 return true;
             }
         }
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index e9d2150..aed7af9 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -566,17 +566,14 @@
 
 if ($store_target)
   {
-  // check if mailbox exists
-  if (!in_array($store_target, $IMAP->list_mailboxes()))
-    {
-      // folder may be existing but not subscribed (#1485241)
-      if (!in_array($store_target, $IMAP->list_unsubscribed()))
-        $store_folder = $IMAP->create_mailbox($store_target, TRUE);
-      else if ($IMAP->subscribe($store_target))
-        $store_folder = TRUE;
-    }
-  else
-    $store_folder = TRUE;
+  // check if folder is subscribed
+  if ($IMAP->mailbox_exists($store_target, true))
+    $store_folder = true;
+  // folder may be existing but not subscribed (#1485241)
+  else if (!$IMAP->mailbox_exists($store_target))
+    $store_folder = $IMAP->create_mailbox($store_target, true);
+  else if ($IMAP->subscribe($store_target))
+    $store_folder = true;
 
   // append message to sent box
   if ($store_folder) {

--
Gitblit v1.9.1