From 32ca1f9fc8a78e092565b51cc97faa724bb4e6cf Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sat, 30 May 2009 05:09:57 -0400
Subject: [PATCH] - css fixes (#1485869)

---
 program/lib/imap.inc |   56 +++++++++++++++++++++++++++++++-------------------------
 1 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index 1b8bec0..2954ecf 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -82,6 +82,8 @@
 		- use PREG instead of EREG
 		- removed caching functions
 		- handling connection startup response
+		- added UID EXPUNGE support
+		- fixed problem with double quotes and spaces in folder names in LIST and LSUB 
 
 ********************************************************/
 
@@ -253,7 +255,7 @@
 	return $line;
 }
 
-function iil_MultLine($fp, $line) {
+function iil_MultLine($fp, $line, $escape=false) {
 	$line = chop($line);
 	if (preg_match('/\{[0-9]+\}$/', $line)) {
 		$out = '';
@@ -264,7 +266,8 @@
 			$line = iil_ReadBytes($fp, $bytes); 
 			$out .= $line;
 		}
-		$line = $a[1][0] . "\"$out\"";
+
+		$line = $a[1][0] . '"' . ($escape ? iil_Escape($out) : $out) . '"';
 //		console('[...] '. $out);
 	}
 	return $line;
@@ -724,19 +727,20 @@
 }
 
 function iil_ExplodeQuotedString($delimiter, $string) {
-	$quotes = explode('"', $string);
-	while ( list($key, $val) = each($quotes)) {
-		if (($key % 2) == 1) {
-			$quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
-    		}
+	$result = array();
+	$strlen = strlen($string);
+	  
+	for ($q=$p=$i=0; $i < $strlen; $i++) {
+	        if ($string[$i] == "\"" && $string[$i-1] != "\\") {
+			$q = $q ? false : true;
+		}
+		else if (!$q && preg_match("/$delimiter/", $string[$i])) {
+			$result[] = substr($string, $p, $i - $p);
+			$p = $i + 1;
+		}
 	}
-	$string = implode('"', $quotes);
-	
-	$result = explode($delimiter, $string);
-	while ( list($key, $val) = each($result) ) {
-		$result[$key] = str_replace('_!@!_', $delimiter, $result[$key]);
-	}
-    
+
+	$result[] = substr($string, $p);
 	return $result;
 }
 
@@ -1527,7 +1531,7 @@
 		$conn->error = "Couldn't select $mailbox";
 		return false;
 	}
-		
+
 	if ($add)
 		$add = ' '.strtoupper(trim($add));
 
@@ -1863,11 +1867,13 @@
 	return $result;
 }
 
-function iil_C_Expunge(&$conn, $mailbox) {
+function iil_C_Expunge(&$conn, $mailbox, $messages=NULL) {
 
 	if (iil_C_Select($conn, $mailbox)) {
 		$c = 0;
-		iil_PutLine($conn->fp, "exp1 EXPUNGE");
+		$command = $messages ? "UID EXPUNGE $messages" : "EXPUNGE";
+
+		iil_PutLine($conn->fp, "exp1 $command");
 		do {
 			$line=chop(iil_ReadLine($conn->fp, 100));
 			if ($line[0] == '*') {
@@ -2030,12 +2036,13 @@
 }
 
 function iil_C_Move(&$conn, $messages, $from, $to) {
-    $fp = $conn->fp;
 
     if (!$from || !$to) {
         return -1;
     }
-    $r = iil_C_Copy($conn, $messages, $from,$to);
+    
+    $r = iil_C_Copy($conn, $messages, $from, $to);
+
     if ($r==0) {
         return iil_C_Delete($conn, $from, $messages);
     }
@@ -2144,7 +2151,7 @@
 	// get folder list
 	do {
 		$line = iil_ReadLine($fp, 500);
-		$line = iil_MultLine($fp, $line);
+		$line = iil_MultLine($fp, $line, true);
 
 		$a = explode(' ', $line);
 		if (($line[0] == '*') && ($a[1] == 'LIST')) {
@@ -2152,7 +2159,7 @@
         		// split one line
 			$a = iil_ExplodeQuotedString(' ', $line);
         		// last string is folder name
-			$folder = trim($a[count($a)-1], '"');
+			$folder = preg_replace(array('/^"/', '/"$/'), '', iil_UnEscape($a[count($a)-1]));
             
         		if (empty($ignore) || (!empty($ignore)
             			&& !preg_match('/'.preg_quote(ignore, '/').'/i', $folder))) {
@@ -2209,7 +2216,7 @@
 	// get folder list
 	do {
 		$line = iil_ReadLine($fp, 500);
-		$line = iil_MultLine($fp, $line);
+		$line = iil_MultLine($fp, $line, true);
 		$a    = explode(' ', $line);
         
 		if (($line[0] == '*') && ($a[1] == 'LSUB' || $a[1] == 'LIST')) {
@@ -2217,10 +2224,9 @@
             
         		// split one line
 			$a = iil_ExplodeQuotedString(' ', $line);
-            
         		// last string is folder name
-        		$folder = trim($a[count($a)-1], '"');
-            
+    			$folder = preg_replace(array('/^"/', '/"$/'), '', iil_UnEscape($a[count($a)-1]));
+        
 			if ((!in_array($folder, $folders)) && (empty($ignore)
             			|| (!empty($ignore) && !preg_match('/'.preg_quote(ignore, '/').'/i', $folder)))) {
 			    $folders[$i] = $folder;

--
Gitblit v1.9.1