Carried over modification made to file_prefs to distinguish
[squirrelmail.git] / functions / imap_general.php
index f658073f161d06c8e6c6f885328f8332e9286f5e..1268831ce3db932acb1712c1613a6035be2d28cb 100755 (executable)
@@ -12,6 +12,8 @@
  */
 
 require_once(SM_PATH . 'functions/page_header.php');
+require_once(SM_PATH . 'functions/auth.php');
+
 
 global $sqimap_session_id;
 $sqimap_session_id = 1;
@@ -31,17 +33,41 @@ function sqimap_session_id($unique_id = false) {
  * This is to allow proper session number handling.
  */
 function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
-    $sid = sqimap_session_id($unique_id);
-    fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
-    $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
-    return $read;
+    if ($imap_stream) {
+       $sid = sqimap_session_id($unique_id);
+       fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
+       $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
+       return $read;
+    } else {
+        global $squirrelmail_language, $color;
+        set_up_language($squirrelmail_language);
+        require_once(SM_PATH . 'functions/display_messages.php');
+        $string = "<b><font color=$color[2]>\n" .
+                _("ERROR : No available imapstream.") .
+                "</b></font>\n";
+        error_box($string,$color);
+       return false;
+    }
+    
 }
 
 function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
-    $sid = sqimap_session_id($unique_id);
-    fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
-    $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
-    return $read;
+    if ($imap_stream) {
+        $sid = sqimap_session_id($unique_id);
+       fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
+       $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
+       return $read;
+    } else {
+        global $squirrelmail_language, $color;
+        set_up_language($squirrelmail_language);
+        require_once(SM_PATH . 'functions/display_messages.php');
+        $string = "<b><font color=$color[2]>\n" .
+                _("ERROR : No available imapstream.") .
+                "</b></font>\n";
+        error_box($string,$color);
+       return false;
+    }
+    
 }
 
 
@@ -125,10 +151,10 @@ function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response,
            $string = "<b><font color=$color[2]>\n" .
                 _("ERROR : Could not complete request.") .
                 "</b><br>\n" .
-                _("Query:") .
-                $query . '<br>' .
+                _("Query:") . ' ' .
+                htmlspecialchars($query) . '<br>' .
                 _("Reason Given: ") .
-                $message . "</font><br>\n";
+                htmlspecialchars($message) . "</font><br>\n";
            error_box($string,$color);
             exit;
         }
@@ -139,10 +165,10 @@ function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response,
         $string = "<b><font color=$color[2]>\n" .
             _("ERROR : Bad or malformed request.") .
             "</b><br>\n" .
-            _("Query:") .
-            $query . '<br>' .
+            _("Query:") . ' '.
+            htmlspecialchars($query) . '<br>' .
             _("Server responded: ") .
-            $message . "</font><br>\n";
+            htmlspecialchars($message) . "</font><br>\n";
        error_box($string,$color);    
         exit;
     } 
@@ -181,18 +207,17 @@ function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$mes
  * will be displayed.  This function returns the imap connection handle.
  */
 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
-    global $color, $squirrelmail_language, $onetimepad;
+    global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, $imap_auth_mech;
 
     $imap_server_address = sqimap_get_user_server($imap_server_address, $username);
-
+       $host=$imap_server_address;
+       
+       if (($use_imap_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
+         /* Use TLS by prefixing "tls://" to the hostname */
+         $imap_server_address = 'tls://' . $imap_server_address;
+       }
+    
     $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
-    if ( !$imap_stream ) {
-        return false;
-    }
-    $server_info = fgets ($imap_stream, 1024);
-
-    /* Decrypt the password */
-    $password = OneTimePadDecrypt($password, $onetimepad);
 
     /* Do some error correction */
     if (!$imap_stream) {
@@ -202,19 +227,66 @@ function sqimap_login ($username, $password, $imap_server_address, $imap_port, $
            $string = sprintf (_("Error connecting to IMAP server: %s.") .
                              "<br>\r\n", $imap_server_address) .
                       "$error_number : $error_string<br>\r\n";
-           error_box($string,$color);
+           logout_error($string,$color);
         }
         exit;
     }
 
-    $query = 'LOGIN "' . quoteIMAP($username) .  '" "' . quoteIMAP($password) . '"';
-    $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
+    $server_info = fgets ($imap_stream, 1024);
+
+    /* Decrypt the password */
+    $password = OneTimePadDecrypt($password, $onetimepad);
 
-    /* If the connection was not successful, lets see why */
+       if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) {
+      // We're using some sort of authentication OTHER than plain
+         $tag=sqimap_session_id(false);
+         if ($imap_auth_mech == 'digest-md5') {
+           $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n";
+         } elseif ($imap_auth_mech == 'cram-md5') {
+           $query = $tag . " AUTHENTICATE CRAM-MD5\r\n";
+         }
+         fputs($imap_stream,$query);
+         $answer=sqimap_fgets($imap_stream);
+         // Trim the "+ " off the front
+         $response=explode(" ",$answer,3);
+         if ($response[0] == '+') {
+           // Got a challenge back
+               $challenge=$response[1];
+               if ($imap_auth_mech == 'digest-md5') {
+                 $reply = digest_md5_response($username,$password,$challenge,'imap',$host);
+               } elseif ($imap_auth_mech == 'cram-md5') {
+                 $reply = cram_md5_response($username,$password,$challenge);
+               }
+               fputs($imap_stream,$reply);
+               $read=sqimap_fgets($imap_stream);
+               if ($imap_auth_mech == 'digest-md5') {
+                 // DIGEST-MD5 has an extra step..
+                 if (substr($read,0,1) == '+') { // OK so far..
+                   fputs($imap_stream,"\r\n");
+                   $read=sqimap_fgets($imap_stream);
+                 }
+               }
+               $results=explode(" ",$read,3);
+               $response=$results[1];
+               $message=$results[2];
+      } else {
+               // Fake the response, so the error trap at the bottom will work
+               $response="BAD";
+               $message='IMAP server does not appear to support the authentication method selected.';
+               $message .= '  Please contact your system administrator.';
+      }
+    } else {
+         // Original PLAIN login code
+      $query = 'LOGIN "' . quoteIMAP($username) .  '" "' . quoteIMAP($password) . '"';
+      $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
+    }
+    
+       /* If the connection was not successful, lets see why */
     if ($response != 'OK') {
         if (!$hide) {
             if ($response != 'NO') {
                 /* "BAD" and anything else gets reported here. */
+               $message = htmlspecialchars($message);
                 set_up_language($squirrelmail_language, true);
                require_once(SM_PATH . 'functions/display_messages.php');
                 if ($response == 'BAD') {
@@ -449,20 +521,36 @@ function sqimap_append ($imap_stream, $sent_folder, $length) {
     $tmp = fgets ($imap_stream, 1024);
 }
 
-function sqimap_append_done ($imap_stream) {
+function sqimap_append_done ($imap_stream, $folder='') {
     global $squirrelmail_language, $color;
     fputs ($imap_stream, "\r\n");
     $tmp = fgets ($imap_stream, 1024);
     if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) {
         set_up_language($squirrelmail_language);
         require_once(SM_PATH . 'functions/display_messages.php');
-        $string = "<b><font color=$color[2]>\n" .
+       $reason = $regs[3];
+       if ($regs[2] == 'NO') {
+          $string = "<b><font color=$color[2]>\n" .
+                 _("ERROR : Could not append message to") ." $folder." .
+                 "</b><br>\n" .
+                 _("Server responded: ") .
+                 $reason . "<br>\n";
+          if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) {
+             $string .= _("Solution: ") . 
+           _("Remove unneccessary messages from your folder and start with your Trash folder.") 
+             ."<br>\n";
+          }
+          $string .= "</font>\n";
+          error_box($string,$color);
+       } else {
+           $string = "<b><font color=$color[2]>\n" .
                  _("ERROR : Bad or malformed request.") .
                  "</b><br>\n" .
                  _("Server responded: ") .
                  $tmp . "</font><br>\n";
-       error_box($string,$color);
-        exit;
+          error_box($string,$color);
+           exit;
+       }
     }
 }