added capability to detect if imap server supports searching on specific
[squirrelmail.git] / functions / imap_general.php
index 7222eed933ae1f16fe911f4acfa5a09939c6e7db..496e2dd84bc13e314bdebf1cd9341afb330b354f 100755 (executable)
@@ -3,6 +3,8 @@
     **  imap.php
     **
     **  This implements all functions that do general imap functions.
+    **
+    **  $Id$
     **/
 
    $imap_general_debug = false;
    function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
       global $color, $squirrelmail_language, $imap_general_debug;
 
-      $keep_going = true;
       $read = fgets($imap_stream, 9096);
-      while ($keep_going)
-      {
-          // Continue if needed for this single line
-          while (strpos($read, "\n") === false)
-         {
-             $read .= fgets($imap_stream, 9096);
-         }
-         
-         if ($imap_general_debug)
-         {
-             echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
-             flush();
-         }
-
-          if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs))
-         {
-             // Test if this is really the last line.
-             // Very much a hack, but what else can I do?
-             socket_set_blocking($imap_stream, false);
-             $read_next = fgets($imap_stream, 2);
-             socket_set_blocking($imap_stream, true);
-             
-             if ($read_next == "")
-             {
-                 $keep_going = 0;
-             }
-         }
-         else
-         {
-             $read_next = fgets($imap_stream, 9096);
-         }
 
-          $data[] = $read;
-         $read = $read_next;
+      if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
+         $size = $regs[1];
+      } else {
+         $size = 0;
+      }
+      
+      $data = Array();
+      
+      $continue = true;
+      while ($continue) {
+         // Continue if needed for this single line
+         while (strpos($read, "\n") === false) {
+            $read .= fgets($imap_stream, 9096);
+         }
+         if ($imap_general_debug) {
+            echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
+            flush();
+         }
+         
+         if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
+            if ($size) {
+               $dt = $data;
+               $dt[0] = $dt[count($dt)-1] = "";
+               $d = implode ("", $dt);
+               if (strlen($d) >= $size) {
+                  $continue = false;
+               } else {
+                  $data[] = $read;
+                  $read = fgets ($imap_stream, 9096);
+               }
+            } else {
+               $continue = false;
+            }
+         } else {
+            $data[] = $read;
+            $read = fgets ($imap_stream, 9096);
+         }
       }
 
       $response = $regs[1];
@@ -60,7 +65,7 @@
 
       if ($handle_errors == false)
           return $data;
-         
+     
       if ($response == "NO") {
          // ignore this error from m$ exchange, it is not fatal (aka bug)
          if (!ereg("command resulted in",$message)) { 
@@ -93,7 +98,7 @@
       global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
 
       $imap_stream = fsockopen ($imap_server_address, $imap_port,
-         &$error_number, &$error_string, 15);
+         $error_number, $error_string, 15);
       $server_info = fgets ($imap_stream, 1024);
       
       // Decrypt the password
@@ -204,8 +209,11 @@ function sqimap_capability($imap_stream, $capability) {
 
                $c = explode(' ', $read[0]);
                for ($i=2; $i < count($c); $i++) {
-                       list($k, $v) = explode('=', $c[$i]);
-                       $sqimap_capabilities[$k] = ($v)?$v:TRUE;
+                       $cap_list = explode('=', $c[$i]);
+                       if (isset($cap_list[1]))
+                           $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
+                       else
+                           $sqimap_capabilities[$cap_list[0]] = TRUE;
                }
        }
        return $sqimap_capabilities[$capability];