added another fix for the odd addition of newlines
[squirrelmail.git] / functions / imap_general.php
index 2848b158b3c0281724a89dcfa364a6b27f14dc8b..d23f0c375944ce4e66fc1c28fa392af5b65079a3 100755 (executable)
 
       $read = fgets($imap_stream, 9096);
 
-      if (ereg("^\* [0-9]+ FETCH.*{([0-9]+)}", $read, $regs)) {
+      if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
          $size = $regs[1];
       } else {
          $size = 0;
       }
       
       $data = Array();
+      $total_size = 0;
       
       $continue = true;
       while ($continue) {
          while (strpos($read, "\n") === false) {
             $read .= fgets($imap_stream, 9096);
          }
+         // For debugging purposes
          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 {
+
+
+         // If we know the size, no need to look at the end parameters
+         if ($size > 0) {
+            if ($total_size == $size) {
+               $data[] = $read;
+               $read = fgets($imap_stream, 9096);
+               $read = fgets($imap_stream, 9096);
+               $continue = false;
+            } else if ($total_size > $size) {
+               $difference = $total_size - $size;
+               $total_size = $total_size - strlen($read);
+               $read = substr ($read, 0, strlen($read)-$difference);
+               $data[] = $read;
+               $junk = fgets($imap_stream, 9096);
                $continue = false;
+            } else {
+               $data[] = $read;
+               $read = fgets($imap_stream, 9096);
             }
+            $total_size += strlen($read);
          } else {
-            $data[] = $read;
-            $read = fgets ($imap_stream, 9096);
+            if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
+               $continue = false;
+            } else {
+               $data[] = $read;
+               $read = fgets ($imap_stream, 9096);
+            }
          }
       }
 
       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