From 46b48e6d349d48e6e211858a82589223b1364518 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 07 Jan 2009 13:29:48 -0500
Subject: [PATCH] - fix: set default auth method to 'check' not 'plain'

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

diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index f269c21..047afdd 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -73,6 +73,9 @@
 		- fix iil_C_FetchPartHeader() in some cases by use of iil_C_HandlePartBody()
 		- allow iil_C_HandlePartBody() to fetch whole message
 		- optimize iil_C_FetchHeaders() to use only one FETCH command
+		- added 4th argument to iil_Connect()
+		- allow setting rootdir and delimiter before connect
+		- support multiquota result
 
 ********************************************************/
 
@@ -476,13 +479,14 @@
 
 function iil_C_NameSpace(&$conn) {
 	global $my_prefs;
+
+	if (isset($my_prefs['rootdir']) && is_string($my_prefs['rootdir'])) {
+    		$conn->rootdir = $my_prefs['rootdir'];
+		return true;
+	}
 	
 	if (!iil_C_GetCapability($conn, 'NAMESPACE')) {
 	    return false;
-	}
-    
-	if ($my_prefs["rootdir"]) {
-	    return true;
 	}
     
 	iil_PutLine($conn->fp, "ns1 NAMESPACE");
@@ -510,12 +514,13 @@
     
 	$conn->rootdir       = $first_userspace[0];
 	$conn->delimiter     = $first_userspace[1];
-	$my_prefs["rootdir"] = substr($conn->rootdir, 0, -1);
+	$my_prefs['rootdir'] = substr($conn->rootdir, 0, -1);
+	$my_prefs['delimiter'] = $conn->delimiter;
 	
 	return true;
 }
 
-function iil_Connect($host, $user, $password) {	
+function iil_Connect($host, $user, $password, $options=null) {	
 	global $iil_error, $iil_errornum;
 	global $ICL_SSL, $ICL_PORT;
 	global $IMAP_NO_CACHE;
@@ -523,18 +528,23 @@
 	
 	$iil_error = '';
 	$iil_errornum = 0;
-	
-	//set auth method
-	$auth_method = 'plain';
-	if (func_num_args() >= 4) {
-		$auth_array = func_get_arg(3);
-		if (is_array($auth_array)) {
-			$auth_method = $auth_array['imap'];
-    		}
-		if (empty($auth_method)) {
-        		$auth_method = "plain";
-    		}
+
+	// set some imap options
+	if (is_array($options)) {
+		foreach($options as $optkey => $optval) {
+			if ($optkey == 'imap') {
+				$auth_method = $optval;
+			} else if ($optkey == 'rootdir') {
+    				$my_prefs['rootdir'] = $optval;
+			} else if ($optkey == 'delimiter') {
+    				$my_prefs['delimiter'] = $optval;
+			}
+		}
 	}
+
+	if (empty($auth_method))
+    		$auth_method = 'check';
+		
 	$message = "INITIAL: $auth_method\n";
 		
 	$result = false;
@@ -586,7 +596,7 @@
 	$conn->fp = fsockopen($host, $ICL_PORT, $errno, $errstr, 10);
 	if (!$conn->fp) {
     		$iil_error = "Could not connect to $host at port $ICL_PORT: $errstr";
-    		$iil_errornum = -1;
+    		$iil_errornum = -2;
 		return false;
 	}
 
@@ -1733,7 +1743,7 @@
 					//get timezone
 					$time_str      = substr($time_str, 0, -1);
 					$time_zone_str = substr($time_str, -5); // extract timezone
-					$time_str      = substr($time_str, 1, -6); // remove quotes
+					$time_str      = substr($time_str, 0, -5); // remove timezone
 					$time_zone     = (float)substr($time_zone_str, 1, 2); // get first two digits
 			
 					if ($time_zone_str[3] != '0') {
@@ -1746,7 +1756,7 @@
 					//calculate timestamp
                                         $timestamp     = strtotime($time_str); //return's server's time
 					$timestamp    -= $time_zone * 3600; //compensate for tz, get GMT
-					
+
 					$result[$id]->internaldate = $time_str;
 					$result[$id]->timestamp = $timestamp;
 					$result[$id]->date = $time_str;
@@ -2138,8 +2148,14 @@
  * @see iil_Connect()
  */
 function iil_C_GetHierarchyDelimiter(&$conn) {
+
+	global $my_prefs;
+	
 	if ($conn->delimiter) {
-        return $conn->delimiter;
+    		return $conn->delimiter;
+	}
+	if (!empty($my_prefs['delimiter'])) {
+    	    return ($conn->delimiter = $my_prefs['delimiter']);
 	}
     
 	$fp        = $conn->fp;
@@ -2641,31 +2657,41 @@
  * GETQUOTAROOT "INBOX"
  * QUOTAROOT INBOX user/rchijiiwa1
  * QUOTA user/rchijiiwa1 (STORAGE 654 9765)
- b OK Completed
+ * OK Completed
  */
 	$fp         = $conn->fp;
 	$result     = false;
-	$quota_line = '';
+	$quota_lines = array();
 	
-	//get line containing quota info
+	// get line(s) containing quota info
 	if (iil_PutLine($fp, 'QUOT1 GETQUOTAROOT "INBOX"')) {
 		do {
 			$line=chop(iil_ReadLine($fp, 5000));
 			if (iil_StartsWith($line, '* QUOTA ')) {
-				$quota_line = $line;
+				$quota_lines[] = $line;
         		}
 		} while (!iil_StartsWith($line, 'QUOT1', true));
 	}
 	
-	//return false if not found, parse if found
-	if (!empty($quota_line)) {
+	// return false if not found, parse if found
+	$min_free = PHP_INT_MAX;
+	foreach ($quota_lines as $key => $quota_line) {
 		$quota_line   = eregi_replace('[()]', '', $quota_line);
 		$parts        = explode(' ', $quota_line);
 		$storage_part = array_search('STORAGE', $parts);
-		if ($storage_part > 0) {
-			$result['used']    = intval($parts[$storage_part+1]);
-			$result['total']   = intval($parts[$storage_part+2]);
-			$result['percent'] = min(100, round(($result['used']/max(1,$result['total']))*100));
+		
+		if (!$storage_part) continue;
+	
+		$used	= intval($parts[$storage_part+1]);
+		$total	= intval($parts[$storage_part+2]);
+		$free	= $total - $used; 
+	
+		// return lowest available space from all quotas
+		if ($free < $min_free) { 
+		        $min_free = $free; 
+			$result['used']    = $used;
+			$result['total']   = $total;
+			$result['percent'] = min(100, round(($used/max(1,$total))*100));
 			$result['free']    = 100 - $result['percent'];
 		}
 	}

--
Gitblit v1.9.1