From b25dfd0913f2ca5666124740c30156fa0a6abaef Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 23 Jun 2010 05:55:08 -0400
Subject: [PATCH] - removed PHP closing tag

---
 program/include/rcube_imap_generic.php |   65 ++++++++++++++++++++++----------
 1 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/program/include/rcube_imap_generic.php b/program/include/rcube_imap_generic.php
index 1cb4d4c..9aa8797 100644
--- a/program/include/rcube_imap_generic.php
+++ b/program/include/rcube_imap_generic.php
@@ -242,7 +242,7 @@
     }
 
     // don't use it in loops, until you exactly know what you're doing
-    private function readReply($untagged=null)
+    private function readReply(&$untagged=null)
     {
 	    do {
 		    $line = trim($this->readLine(1024));
@@ -611,14 +611,21 @@
 		    $host = $this->prefs['ssl_mode'] . '://' . $host;
 	    }
 
-	    $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, 10);
+        // Connect
+        if ($this->prefs['timeout'] > 0)
+	        $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']);
+	    else
+	        $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr);
+
 	    if (!$this->fp) {
     		$this->error    = sprintf("Could not connect to %s:%d: %s", $host, $this->prefs['port'], $errstr);
     		$this->errornum = -2;
 		    return false;
 	    }
 
-	    stream_set_timeout($this->fp, 10);
+        if ($this->prefs['timeout'] > 0)
+	        stream_set_timeout($this->fp, $this->prefs['timeout']);
+
 	    $line = trim(fgets($this->fp, 8192));
 
 	    if ($this->prefs['debug_mode'] && $line) {
@@ -709,6 +716,7 @@
             }
 		    $this->getNamespace();
             $this->logged = true;
+
 		    return true;
 	    } else {
 		    return false;
@@ -841,7 +849,6 @@
 	    } while (!$this->startsWith($line, 's ', true, true));
 
 	    $result_code = $this->parseResult($line);
-
 	    if ($result_code != 0) {
             $this->error = "Sort: $line";
             return false;
@@ -1500,7 +1507,7 @@
     {
 	    $node = array();
 	    if ($str[$begin] != '(') {
-		    $stop = $begin + strspn($str, "1234567890", $begin, $end - $begin);
+		    $stop = $begin + strspn($str, '1234567890', $begin, $end - $begin);
 		    $msg = substr($str, $begin, $stop - $begin);
 		    if ($msg == 0)
 		        return $node;
@@ -1546,30 +1553,40 @@
 
     function thread($folder, $algorithm='REFERENCES', $criteria='', $encoding='US-ASCII')
     {
+        $old_sel = $this->selected;
+
 	    if (!$this->select($folder)) {
-		    return false;
+    		return false;
+	    }
+
+        // return empty result when folder is empty and we're just after SELECT
+        if ($old_sel != $folder && !$this->exists) {
+            return array(array(), array(), array());
 	    }
 
     	$encoding  = $encoding ? trim($encoding) : 'US-ASCII';
 	    $algorithm = $algorithm ? trim($algorithm) : 'REFERENCES';
 	    $criteria  = $criteria ? 'ALL '.trim($criteria) : 'ALL';
+        $data      = '';
 
 	    if (!$this->putLineC("thrd1 THREAD $algorithm $encoding $criteria")) {
 		    return false;
 	    }
 	    do {
-		    $line = trim($this->readLine(10000));
-		    if (preg_match('/^\* THREAD/', $line)) {
-			    $str         = trim(substr($line, 8));
-	    		$depthmap    = array();
-		    	$haschildren = array();
-			    $tree = $this->parseThread($str, 0, strlen($str), null, null, 0, $depthmap, $haschildren);
+		    $line = trim($this->readLine());
+		    if ($this->startsWith($line, '* THREAD')) {
+    			$data .= substr($line, 9);
+	    	} else if (preg_match('/^[0-9() ]+$/', $line)) {
+		    	$data .= $line;
 		    }
 	    } while (!$this->startsWith($line, 'thrd1', true, true));
 
     	$result_code = $this->parseResult($line);
 	    if ($result_code == 0) {
-    	    return array($tree, $depthmap, $haschildren);
+            $depthmap    = array();
+            $haschildren = array();
+            $tree = $this->parseThread($data, 0, strlen($data), null, null, 0, $depthmap, $haschildren);
+            return array($tree, $depthmap, $haschildren);
 	    }
 
     	$this->error = "Thread: $line";
@@ -1578,8 +1595,15 @@
 
     function search($folder, $criteria, $return_uid=false)
     {
+        $old_sel = $this->selected;
+
 	    if (!$this->select($folder)) {
     		return false;
+	    }
+
+        // return empty result when folder is empty and we're just after SELECT
+        if ($old_sel != $folder && !$this->exists) {
+            return array();
 	    }
 
     	$data = '';
@@ -1752,12 +1776,11 @@
 		    	$mode = 0;
 	    }
 
-   		$reply_key = '* ' . $id;
-    	$result = false;
-
     	// format request
-		$key     = 'ftch0';
-		$request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])";
+   		$reply_key = '* ' . $id;
+		$key       = 'ftch0';
+		$request   = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])";
+
     	// send request
 		if (!$this->putLine($request)) {
 		    return false;
@@ -1769,7 +1792,8 @@
        		$a    = explode(' ', $line);
    		} while (!($end = $this->startsWith($line, $key, true)) && $a[2] != 'FETCH');
 
-   		$len = strlen($line);
+   		$len    = strlen($line);
+    	$result = false;
 
 		// handle empty "* X FETCH ()" response
     	if ($line[$len-1] == ')' && $line[$len-2] != '(') {
@@ -1865,7 +1889,7 @@
         			$line = $this->readLine(1024);
 			} while (!$this->startsWith($line, $key, true));
 
-   		if ($result) {
+   		if ($result !== false) {
 	    	if ($file) {
 		    	fwrite($file, $result);
    			} else if ($print) {
@@ -2212,4 +2236,3 @@
 
 }
 
-?>

--
Gitblit v1.9.1