From b7c1685b2dc98cad4acdbc04d5383b204b4d9c83 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sun, 18 Jan 2009 05:58:32 -0500
Subject: [PATCH] #1484467: fix problems with backslash as imap hierarchy delimiter

---
 CHANGELOG                      |    4 ++++
 program/include/rcube_imap.php |   14 +-------------
 program/lib/imap.inc           |   13 ++++++++++---
 3 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 7a16614..6ea9747 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,10 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2009/01/18 (alec)
+----------
+- Fix problems with backslash as IMAP hierarchy delimiter (#1484467)
+
 2009/01/15 (alec)
 ----------
 - Allow absolute URLs to images in HTML messages/sigs (#1485666)
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 7da209f..0af494e 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -1568,7 +1568,6 @@
    */
   function save_message($mbox_name, &$message)
     {
-    $mbox_name = stripslashes($mbox_name);
     $mailbox = $this->_mod_mailbox($mbox_name);
 
     // make sure mailbox exists
@@ -1595,9 +1594,7 @@
    */
   function move_message($uids, $to_mbox, $from_mbox='')
     {
-    $to_mbox_in = stripslashes($to_mbox);
-    $from_mbox = stripslashes($from_mbox);
-    $to_mbox = $this->_mod_mailbox($to_mbox_in);
+    $to_mbox = $this->_mod_mailbox($to_mbox);
     $from_mbox = $from_mbox ? $this->_mod_mailbox($from_mbox) : $this->mailbox;
 
     // make sure mailbox exists
@@ -1672,7 +1669,6 @@
    */
   function delete_message($uids, $mbox_name='')
     {
-    $mbox_name = stripslashes($mbox_name);
     $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
 
     // convert the list of uids to array
@@ -1729,7 +1725,6 @@
    */
   function clear_mailbox($mbox_name=NULL)
     {
-    $mbox_name = stripslashes($mbox_name);
     $mailbox = !empty($mbox_name) ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
     $msg_count = $this->_messagecount($mailbox, 'ALL');
     
@@ -1762,7 +1757,6 @@
    */
   function expunge($mbox_name='', $clear_cache=TRUE)
     {
-    $mbox_name = stripslashes($mbox_name);
     $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
     return $this->_expunge($mailbox, $clear_cache);
     }
@@ -1881,9 +1875,6 @@
     {
     $result = FALSE;
     
-    // replace backslashes
-    $name = preg_replace('/[\\\]+/', '-', $name);
-
     // reduce mailbox name to 100 chars
     $name = substr($name, 0, 100);
 
@@ -1912,9 +1903,6 @@
     {
     $result = FALSE;
 
-    // replace backslashes
-    $name = preg_replace('/[\\\]+/', '-', $new_name);
-        
     // encode mailbox name and reduce it to 100 chars
     $name = substr($new_name, 0, 100);
 
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index 1390653..cca499b 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -53,7 +53,7 @@
 		- Removed some debuggers (echo ...)
 		File altered by Aleksander Machniak <alec@alec.pl>
 		- trim(chop()) replaced by trim()
-		- added iil_Escape() with support for " and \ in folder names
+		- added iil_Escape()/iil_UnEscape() with support for " and \ in folder names
 		- support \ character in username in iil_C_Login()
 		- fixed iil_MultLine(): use iil_ReadBytes() instead of iil_ReadLine()
 		- fixed iil_C_FetchStructureString() to handle many literal strings in response
@@ -338,6 +338,11 @@
 	return strtr($string, array('"'=>'\\"', '\\' => '\\\\')); 
 }
 
+function iil_UnEscape($string)
+{
+	return strtr($string, array('\\"'=>'"', '\\\\' => '\\')); 
+}
+
 function iil_C_GetCapability(&$conn, $name)
 {
 	if (in_array($name, $conn->capability)) {
@@ -497,6 +502,7 @@
 		$line = iil_ReadLine($conn->fp, 1024);
 		if (iil_StartsWith($line, '* NAMESPACE')) {
 			$i    = 0;
+			$line = iil_UnEscape($line);
 			$data = iil_ParseNamespace2(substr($line,11), $i, 0, 0);
 		}
 	} while (!iil_StartsWith($line, 'ns1', true));
@@ -2199,7 +2205,7 @@
 		$line=iil_ReadLine($fp, 500);
 		if ($line[0] == '*') {
 			$line = rtrim($line);
-			$a=iil_ExplodeQuotedString(' ', $line);
+			$a=iil_ExplodeQuotedString(' ', iil_UnEscape($line));
 			if ($a[0] == '*') {
 			    $delimiter = str_replace('"', '', $a[count($a)-2]);
         		}
@@ -2209,7 +2215,7 @@
 	if (strlen($delimiter)>0) {
 	    return $delimiter;
 	}
-    
+
 	//if that fails, try namespace extension
 	//try to fetch namespace data
 	iil_PutLine($conn->fp, "ns1 NAMESPACE");
@@ -2217,6 +2223,7 @@
 		$line = iil_ReadLine($conn->fp, 1024);
 		if (iil_StartsWith($line, '* NAMESPACE')) {
 			$i = 0;
+			$line = iil_UnEscape($line);
 			$data = iil_ParseNamespace2(substr($line,11), $i, 0, 0);
 		}
 	} while (!iil_StartsWith($line, 'ns1', true));

--
Gitblit v1.9.1