this looks better ;)
[squirrelmail.git] / functions / imap_general.php
index 5e755b5d2ada1399555777cfafab99d2f12da6d3..3c812805b4c31d0babc35bee1023fdba16be9f2d 100755 (executable)
@@ -3,13 +3,14 @@
 /**
  * imap_general.php
  *
- * Copyright (c) 1999-2003 The SquirrelMail Project Team
+ * Copyright (c) 1999-2004 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This implements all functions that do general imap functions.
  *
- * $Id$
+ * @version $Id$
  * @package squirrelmail
+ * @subpackage imap
  */
 
 /** Includes.. */
@@ -17,17 +18,15 @@ require_once(SM_PATH . 'functions/page_header.php');
 require_once(SM_PATH . 'functions/auth.php');
 
 
-global $sqimap_session_id;
-$sqimap_session_id = 1;
-
 /**
  * Generates a new session ID by incrementing the last one used;
  * this ensures that each command has a unique ID.
  * @param bool unique_id
  * @return string IMAP session id of the form 'A000'.
  */
-function sqimap_session_id($unique_id = false) {
-    global $data_dir, $username, $sqimap_session_id;
+function sqimap_session_id($unique_id = FALSE) {
+    static $sqimap_session_id = 1;
+
     if (!$unique_id) {
         return( sprintf("A%03d", $sqimap_session_id++) );
     } else {
@@ -73,10 +72,15 @@ function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response,
    
         $read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response,
                                   $message, $query,$filter,$outputstream,$no_return);
+        if (empty($read)) {    //Imap server dropped its connection
+            $response = '';
+            $message = '';
+            return false;
+        }
         /* retrieve the response and the message */
         $response = $response[$tag];
         $message  = $message[$tag];
-        
+    
         if (!empty($read[$tag])) {
             return $read[$tag][0];
         } else {
@@ -280,11 +284,12 @@ function sqimap_read_data_list($imap_stream, $tag, $handle_errors,
  * Function to display an error related to an IMAP-query.
  * @param string title the caption of the error box
  * @param string query the query that went wrong
- * @param string message_title
- * @param string message the error message
+ * @param string message_title optional message title
+ * @param string message optional error message
+ * @param string $link an optional link to try again
  * @return void
  */
-function sqimap_error_box($title, $query = '', $message_title = '', $message = '')
+function sqimap_error_box($title, $query = '', $message_title = '', $message = '', $link = '')
 {
     global $color, $squirrelmail_language;
 
@@ -301,6 +306,8 @@ function sqimap_error_box($title, $query = '', $message_title = '', $message = '
     if ($message != '')
         $string .= htmlspecialchars($message);
     $string .= "</font><br>\n";
+    if ($link != '')
+        $string .= $link;
     error_box($string,$color);
 }
 
@@ -316,6 +323,7 @@ function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
     $read = '';
     if (!is_array($message)) $message = array();
     if (!is_array($response)) $response = array();
+    $aResponse = '';
     $resultlist = array();
     $data = array();
     $read = sqimap_fgets($imap_stream);
@@ -470,8 +478,10 @@ function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
     /* error processing in case $read is false */
     if ($read === false) {
         unset($data);
-        sqimap_error_box(_("ERROR : Connection dropped by imap-server."), $query);
-        exit;
+        if ($handle_errors) {
+            sqimap_error_box(_("ERROR : Connection dropped by imap-server."), $query);
+            exit;
+        }
     }
     
     /* Set $resultlist array */
@@ -485,7 +495,6 @@ function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
     /* Return result or handle errors */
     if ($handle_errors == false) {
         return $aResponse;
-        return( $resultlist ); //?? Why this?
     }
     switch ($response[$tag]) {
     case 'OK':
@@ -557,10 +566,10 @@ function sqimap_read_data ($imap_stream, $tag_uid, $handle_errors,
 function sqimap_create_stream($server,$port,$tls=false) {
     global $username, $use_imap_tls;
 
-    if ($use_imap_tls == true) {
+    if ($tls == true) {
         if ((check_php_version(4,3)) and (extension_loaded('openssl'))) {
             /* Use TLS by prefixing "tls://" to the hostname */
-            $server = 'tls://' . $imap_server_address;
+            $server = 'tls://' . $server;
         } else {
             require_once(SM_PATH . 'functions/display_messages.php');
             $string = "Unable to connect to IMAP server!<br>TLS is enabled, but this " .
@@ -764,6 +773,8 @@ function sqimap_capability($imap_stream, $capability='') {
         for ($i=2; $i < count($c); $i++) {
             $cap_list = explode('=', $c[$i]);
             if (isset($cap_list[1])) {
+                // FIX ME. capabilities can occure multiple times.
+                // THREAD=REFERENCES THREAD=ORDEREDSUBJECT
                 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
             } else {
                 $sqimap_capabilities[$cap_list[0]] = TRUE;