From 462de2d2028617c8464f174987785a92583313be Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sun, 17 Apr 2011 05:22:03 -0400
Subject: [PATCH] - PEAR::Net_SMTP 1.5.2, fixed timeout issue (#1487843)

---
 program/include/rcube_smtp.php |    9 ++++++++-
 CHANGELOG                      |    2 +-
 program/lib/Net/SMTP.php       |   21 +++++++++++++++++----
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 56e59d7..2d57539 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- PEAR::Net_SMTP 1.5.2, fixed timeout issue (#1487843)
 - Added plugin hook for request token creation
 - Replace LDAP vars in group queries (#1487837)
 - Fix vcard folding with uncode characters (#1487868)
@@ -17,7 +18,6 @@
 - Enable TinyMCE's contextmenu (#1487014)
 - TinyMCE 3.4.1
 - Stateless request tokens. No keep-alive necessary on login page (#1487829)
-- PEAR::Net_SMTP 1.5.1
 - Allow multiple concurrent compose sessions
 - Force names of unique constraints in PostgreSQL DDL
 - Add code for prevention from IMAP connection hangs when server closes socket unexpectedly
diff --git a/program/include/rcube_smtp.php b/program/include/rcube_smtp.php
index 654c7ef..120336c 100644
--- a/program/include/rcube_smtp.php
+++ b/program/include/rcube_smtp.php
@@ -105,7 +105,7 @@
 
     $this->conn = new Net_SMTP($smtp_host, $smtp_port, $helo_host);
 
-    if($RCMAIL->config->get('smtp_debug'))
+    if ($RCMAIL->config->get('smtp_debug'))
       $this->conn->setDebug(true, array($this, 'debug_handler'));
 
     // try to connect to server and exit on failure
@@ -118,6 +118,13 @@
       return false;
     }
 
+    // workaround for timeout bug in Net_SMTP 1.5.[0-1] (#1487843)
+    if (method_exists($this->conn, 'setTimeout')
+      && ($timeout = ini_get('default_socket_timeout'))
+    ) {
+      $this->conn->setTimeout($timeout);
+    }
+
     $smtp_user = str_replace('%u', $_SESSION['username'], $CONFIG['smtp_user']);
     $smtp_pass = str_replace('%p', $RCMAIL->decrypt($_SESSION['password']), $CONFIG['smtp_pass']);
     $smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type'];
diff --git a/program/lib/Net/SMTP.php b/program/lib/Net/SMTP.php
index fef8076..0463758 100644
--- a/program/lib/Net/SMTP.php
+++ b/program/lib/Net/SMTP.php
@@ -106,6 +106,14 @@
     var $_socket = null;
 
     /**
+     * Array of socket options that will be passed to Net_Socket::connect().
+     * @see stream_context_create()
+     * @var array
+     * @access private
+     */
+    var $_socket_options = null;
+
+    /**
      * The socket I/O timeout value in seconds.
      * @var int
      * @access private
@@ -156,12 +164,13 @@
      * @param string  $localhost  The value to give when sending EHLO or HELO.
      * @param boolean $pipeling   Use SMTP command pipelining
      * @param integer $timeout    Socket I/O timeout in seconds.
+     * @param array   $socket_options Socket stream_context_create() options.
      *
      * @access  public
      * @since   1.0
      */
     function Net_SMTP($host = null, $port = null, $localhost = null,
-        $pipelining = false, $timeout = 0)
+        $pipelining = false, $timeout = 0, $socket_options = null)
     {
         if (isset($host)) {
             $this->host = $host;
@@ -175,6 +184,7 @@
         $this->pipelining = $pipelining;
 
         $this->_socket = new Net_Socket();
+        $this->_socket_options = $socket_options;
         $this->_timeout = $timeout;
 
         /* Include the Auth_SASL package.  If the package is not
@@ -405,7 +415,8 @@
     {
         $this->_greeting = null;
         $result = $this->_socket->connect($this->host, $this->port,
-                                          $persistent, $timeout);
+                                          $persistent, $timeout,
+                                          $this->_socket_options);
         if (PEAR::isError($result)) {
             return PEAR::raiseError('Failed to connect socket: ' .
                                     $result->getMessage());
@@ -417,8 +428,10 @@
          * timeout values for the initial connection (our $timeout parameter) 
          * and all other socket operations.
          */
-        if (PEAR::isError($error = $this->setTimeout($this->_timeout))) {
-            return $error;
+        if ($this->_timeout > 0) {
+            if (PEAR::isError($error = $this->setTimeout($this->_timeout))) {
+                return $error;
+            }
         }
 
         if (PEAR::isError($error = $this->_parseResponse(220))) {

--
Gitblit v1.9.1