From d6584f72069cb0a4efd8cd195795a9db72dce9e7 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Tue, 19 Jan 2010 08:17:48 -0500
Subject: [PATCH] - Use PLAIN auth when CRAM fails and imap_auth_type='check' (#1486371)

---
 CHANGELOG            |    1 +
 program/lib/imap.inc |   29 +++++++++++++++--------------
 2 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 154332b..2a2da45 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Use PLAIN auth when CRAM fails and imap_auth_type='check' (#1486371)
 - Fix removal of <title> tag from HTML messages (#1486432)
 - Fix 'force_https' to specified port when URL contains a port number (#1486411)
 - Fix to-text converting of HTML entities inside b/strong/th/hX tags (#1486422)
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index 17225c1..61d49b4 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -647,7 +647,7 @@
 		$conn->capability = explode(' ', strtoupper($matches[1]));
 	}
 
-	$conn->message .= $line . "\n";
+	$conn->message .= $line;
 
 	// TLS connection
 	if ($ICL_SSL == 'tls' && iil_C_GetCapability($conn, 'STARTTLS')) {
@@ -672,46 +672,47 @@
         	}
 	}
 
+	$orig_method = $auth_method;
+
 	if ($auth_method == 'CHECK') {
-		//check for supported auth methods
+		// check for supported auth methods
 		if (iil_C_GetCapability($conn, 'AUTH=CRAM-MD5') || iil_C_GetCapability($conn, 'AUTH=CRAM_MD5')) {
 			$auth_method = 'AUTH';
 		}
 		else {
-			//default to plain text auth
+			// default to plain text auth
 			$auth_method = 'PLAIN';
 		}
 	}
 
 	if ($auth_method == 'AUTH') {
-		//do CRAM-MD5 authentication
+		// do CRAM-MD5 authentication
 		iil_PutLine($conn->fp, "a000 AUTHENTICATE CRAM-MD5");
 		$line = trim(iil_ReadLine($conn->fp, 1024));
 
 		if ($line[0] == '+') {
-			//got a challenge string, try CRAM-5
+			// got a challenge string, try CRAM-MD5
 			$result = iil_C_Authenticate($conn, $user, $password, substr($line,2));
         		
 			// stop if server sent BYE response
-			if($result == -3) {
+			if ($result == -3) {
 	            		$iil_error = $conn->error;
 	            		$iil_errornum = $conn->errorNum;
 				return false;
 			}
-			$conn->message .= "AUTH CRAM-MD5: $result\n";
-		} else {
-			$conn->message .= "AUTH CRAM-MD5: failed\n";
+		}
+		
+		if (!is_resource($result) && $orig_method == 'CHECK') {
 			$auth_method = 'PLAIN';
 		}
 	}
 		
-	if (!$result || $auth_method == 'PLAIN') {
-		//do plain text auth
+	if ($auth_method == 'PLAIN') {
+		// do plain text auth
 		$result = iil_C_Login($conn, $user, $password);
-		$conn->message .= "AUTH PLAIN: $result\n";
 	}
-		
-	if (!is_int($result)) {
+
+	if (is_resource($result)) {
 		iil_C_Namespace($conn);
 		return $conn;
 	} else {

--
Gitblit v1.9.1