From bdab2c5faf8a2b311debce0b724a0b2df02aa60c Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sun, 20 Sep 2009 06:25:14 -0400
Subject: [PATCH] - small code improvements

---
 program/include/rcube_imap.php |    2 
 program/include/rcmail.php     |    2 
 program/lib/imap.inc           |   79 ++++++++++++++++++---------------------
 3 files changed, 38 insertions(+), 45 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index b148e51..be90740 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -378,7 +378,7 @@
     // Setting root and delimiter before iil_Connect can save time detecting them
     // using NAMESPACE and LIST 
     $options = array(
-      'imap' => $this->config->get('imap_auth_type', 'check'),
+      'auth_method' => $this->config->get('imap_auth_type', 'check'),
       'delimiter' => isset($_SESSION['imap_delimiter']) ? $_SESSION['imap_delimiter'] : $this->config->get('imap_delimiter'),
       'rootdir' => isset($_SESSION['imap_root']) ? $_SESSION['imap_root'] : $this->config->get('imap_root'),
       'debug_mode' => (bool) $this->config->get('imap_debug', 0),
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 2d4792c..e094796 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -69,7 +69,7 @@
   var $search_sort_field = '';  
   var $debug_level = 1;
   var $error_code = 0;
-  var $options = array('imap' => 'check');
+  var $options = array('auth_method' => 'check');
   
   private $host, $user, $pass, $port, $ssl;
 
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index 04af1d0..f632ca9 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -303,13 +303,14 @@
 function iil_ParseResult($string) {
 	$a = explode(' ', trim($string));
 	if (count($a) >= 2) {
-		if (strcasecmp($a[1], 'OK') == 0) {
+		$res = strtoupper($a[1]);
+		if ($res == 'OK') {
 			return 0;
-		} else if (strcasecmp($a[1], 'NO') == 0) {
+		} else if ($res == 'NO') {
 			return -1;
-		} else if (strcasecmp($a[1], 'BAD') == 0) {
+		} else if ($res == 'BAD') {
 			return -2;
-		} else if (strcasecmp($a[1], 'BYE') == 0) {
+		} else if ($res == 'BYE') {
 			return -3;
 		}
 	}
@@ -555,7 +556,7 @@
 	if (is_array($options)) {
 		foreach($options as $optkey => $optval) {
 			if ($optkey == 'imap') {
-				$auth_method = $optval;
+				$auth_method = strtoupper($optval);
 			} else if ($optkey == 'rootdir') {
     				$my_prefs['rootdir'] = $optval;
 			} else if ($optkey == 'delimiter') {
@@ -567,7 +568,7 @@
 	}
 
 	if (empty($auth_method))
-    		$auth_method = 'check';
+    		$auth_method = 'CHECK';
 		
 	$message = "INITIAL: $auth_method\n";
 		
@@ -665,29 +666,23 @@
         	}
 	}
 
-	if (strcasecmp($auth_method, "check") == 0) {
+	if ($auth_method == 'CHECK') {
 		//check for supported auth methods
 		if (iil_C_GetCapability($conn, 'AUTH=CRAM-MD5') || iil_C_GetCapability($conn, 'AUTH=CRAM_MD5')) {
-			$auth_method = 'auth';
+			$auth_method = 'AUTH';
 		}
 		else {
 			//default to plain text auth
-			$auth_method = 'plain';
+			$auth_method = 'PLAIN';
 		}
 	}
 
-	if (strcasecmp($auth_method, 'auth') == 0) {
-		$conn->message .= "Trying CRAM-MD5\n";
-
+	if ($auth_method == 'AUTH') {
 		//do CRAM-MD5 authentication
 		iil_PutLine($conn->fp, "a000 AUTHENTICATE CRAM-MD5");
 		$line = trim(iil_ReadLine($conn->fp, 1024));
 
-		$conn->message .= "$line\n";
-
 		if ($line[0] == '+') {
-			$conn->message .= 'Got challenge: ' . htmlspecialchars($line) . "\n";
-
 			//got a challenge string, try CRAM-5
 			$result = iil_C_Authenticate($conn, $user, $password, substr($line,2));
         		
@@ -697,21 +692,19 @@
 	            		$iil_errornum = $conn->errorNum;
 				return false;
 			}
-			$conn->message .= "Tried CRAM-MD5: $result \n";
+			$conn->message .= "AUTH CRAM-MD5: $result\n";
 		} else {
-			$conn->message .='No challenge ('.htmlspecialchars($line)."), try plain\n";
-			$auth = 'plain';
+			$conn->message .= "AUTH CRAM-MD5: failed\n";
+			$auth_method = 'PLAIN';
 		}
 	}
 		
-	if (!$result || strcasecmp($auth, "plain") == 0) {
+	if (!$result || $auth_method == 'PLAIN') {
 		//do plain text auth
 		$result = iil_C_Login($conn, $user, $password);
-		$conn->message .= "Tried PLAIN: $result \n";
+		$conn->message .= "AUTH PLAIN: $result\n";
 	}
 		
-	$conn->message .= $auth;
-			
 	if (!is_int($result)) {
 		iil_C_Namespace($conn);
 		return $conn;
@@ -779,7 +772,7 @@
 	if (empty($mailbox)) {
 		return false;
 	}
-	if (strcmp($conn->selected, $mailbox) == 0) {
+	if ($conn->selected == $mailbox) {
 		return true;
 	}
     
@@ -788,10 +781,11 @@
 			$line = chop(iil_ReadLine($conn->fp, 300));
 			$a = explode(' ', $line);
 			if (count($a) == 3) {
-				if (strcasecmp($a[2], 'EXISTS') == 0) {
+				$token = strtoupper($a[2]);
+				if ($token == 'EXISTS') {
 					$conn->exists = (int) $a[1];
 				}
-				else if (strcasecmp($a[2], 'RECENT') == 0) {
+				else if ($token == 'RECENT') {
 					$conn->recent = (int) $a[1];
 				}
 			}
@@ -799,8 +793,6 @@
 				$conn->permanentflags = explode(' ', $match[1]);
 			}
 		} while (!iil_StartsWith($line, 'sel1', true));
-
-		$a = explode(' ', $line);
 
 		if (strcasecmp($a[1], 'OK') == 0) {
 			$conn->selected = $mailbox;
@@ -1477,13 +1469,13 @@
 				$parts_count = count($a);
 				if ($parts_count>=6) {
 					for ($i=0; $i<$parts_count; $i=$i+2) {
-						if (strcasecmp($a[$i],'UID') == 0)
+						if ($a[$i] == 'UID')
 							$result[$id]->uid = $a[$i+1];
-						else if (strcasecmp($a[$i],'RFC822.SIZE') == 0)
+						else if ($a[$i] == 'RFC822.SIZE')
 							$result[$id]->size = $a[$i+1];
-						else if (strcasecmp($a[$i],'INTERNALDATE') == 0)
+						else if ($a[$i] == 'INTERNALDATE')
 							$time_str = $a[$i+1];
-						else if (strcasecmp($a[$i],'FLAGS') == 0)
+						else if ($a[$i] == 'FLAGS')
 							$flags_str = $a[$i+1];
 					}
 
@@ -1644,23 +1636,24 @@
 				$flags_a   = explode(' ', $flags_str);
 					
 				if (is_array($flags_a)) {
-					reset($flags_a);
-					while (list(,$val)=each($flags_a)) {
-						if (strcasecmp($val,'Seen') == 0) {
+				//	reset($flags_a);
+					foreach($flags_a as $flag) {
+						$flag = strtoupper($flag);
+						if ($flag == 'SEEN') {
 						    $result[$id]->seen = true;
-						} else if (strcasecmp($val, 'Deleted') == 0) {
-						    $result[$id]->deleted=true;
-						} else if (strcasecmp($val, 'Recent') == 0) {
+						} else if ($flag == 'DELETED') {
+						    $result[$id]->deleted = true;
+						} else if ($flag == 'RECENT') {
 						    $result[$id]->recent = true;
-						} else if (strcasecmp($val, 'Answered') == 0) {
+						} else if ($flag == 'ANSWERED') {
 							$result[$id]->answered = true;
-						} else if (strcasecmp($val, '$Forwarded') == 0) {
+						} else if ($flag == '$FORWARDED') {
 							$result[$id]->forwarded = true;
-						} else if (strcasecmp($val, 'Draft') == 0) {
+						} else if ($flag == 'DRAFT') {
 							$result[$id]->is_draft = true;
-						} else if (strcasecmp($val, '$MDNSent') == 0) {
+						} else if ($flag == '$MDNSENT') {
 							$result[$id]->mdn_sent = true;
-						} else if (strcasecmp($val, 'Flagged') == 0) {
+						} else if ($flag == 'FLAGGED') {
 						         $result[$id]->flagged = true;
 						}
 					}

--
Gitblit v1.9.1