From c442f822fb9b961f7a92930e572edb52159391d3 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 10 Nov 2012 06:04:38 -0500
Subject: [PATCH] Simplify keep-alive action. Now the interval is based on session_lifetime, which means it's executed only if needed for session keeping (reset interval on every action).

---
 program/include/rcube.php         |   30 +-------------
 program/include/rcmail.php        |   11 +----
 program/include/rcube_session.php |   20 ---------
 program/js/app.js                 |   29 +++++++-------
 4 files changed, 22 insertions(+), 68 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 3728e5d..a755aa8 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -94,9 +94,6 @@
     // create user object
     $this->set_user(new rcube_user($_SESSION['user_id']));
 
-    // configure session (after user config merge!)
-    $this->session_configure();
-
     // set task and action properties
     $this->set_task(rcube_utils::get_input_value('_task', rcube_utils::INPUT_GPC));
     $this->action = asciiwords(rcube_utils::get_input_value('_action', rcube_utils::INPUT_GPC));
@@ -320,10 +317,9 @@
     if (!($this->output instanceof rcube_output_html))
       $this->output = new rcube_output_html($this->task, $framed);
 
-    // set keep-alive/check-recent interval
-    if ($this->session && ($keep_alive = $this->session->get_keep_alive())) {
-      $this->output->set_env('keep_alive', $keep_alive);
-    }
+    // set keep-alive interval
+    $this->output->set_env('keep_alive', $this->config->get('keep_alive', 0));
+    $this->output->set_env('session_lifetime', $this->config->get('session_lifetime', 0) * 60);
 
     if ($framed) {
       $this->comm_path .= '&_framed=1';
@@ -522,7 +518,6 @@
       // Configure environment
       $this->set_user($user);
       $this->set_storage_prop();
-      $this->session_configure();
 
       // fix some old settings according to namespace prefix
       $this->fix_namespace_settings($user);
diff --git a/program/include/rcube.php b/program/include/rcube.php
index 0e40b3c..9c1a6d8 100644
--- a/program/include/rcube.php
+++ b/program/include/rcube.php
@@ -434,37 +434,13 @@
         $this->session->register_gc_handler(array($this, 'temp_gc'));
         $this->session->register_gc_handler(array($this, 'cache_gc'));
 
+        $this->session->set_secret($this->config->get('des_key') . dirname($_SERVER['SCRIPT_NAME']));
+        $this->session->set_ip_check($this->config->get('ip_check'));
+
         // start PHP session (if not in CLI mode)
         if ($_SERVER['REMOTE_ADDR']) {
             session_start();
         }
-    }
-
-
-    /**
-     * Configure session object internals
-     */
-    public function session_configure()
-    {
-        if (!$this->session) {
-            return;
-        }
-
-        $lifetime   = $this->config->get('session_lifetime', 0) * 60;
-        $keep_alive = $this->config->get('keep_alive');
-
-        // set keep-alive/check-recent interval
-        if ($keep_alive) {
-            // be sure that it's less than session lifetime
-            if ($lifetime) {
-                $keep_alive = min($keep_alive, $lifetime - 30);
-            }
-            $keep_alive = max(60, $keep_alive);
-            $this->session->set_keep_alive($keep_alive);
-        }
-
-        $this->session->set_secret($this->config->get('des_key') . dirname($_SERVER['SCRIPT_NAME']));
-        $this->session->set_ip_check($this->config->get('ip_check'));
     }
 
 
diff --git a/program/include/rcube_session.php b/program/include/rcube_session.php
index 6192466..c71efa2 100644
--- a/program/include/rcube_session.php
+++ b/program/include/rcube_session.php
@@ -43,7 +43,6 @@
   private $secret = '';
   private $ip_check = false;
   private $logging = false;
-  private $keep_alive = 0;
   private $memcache;
 
   /**
@@ -525,24 +524,6 @@
       $this->now = $now - ($now % ($this->lifetime / 2));
   }
 
-  /**
-   * Setter for keep_alive interval
-   */
-  public function set_keep_alive($keep_alive)
-  {
-    $this->keep_alive = $keep_alive;
-
-    if ($this->lifetime < $keep_alive)
-        $this->set_lifetime($keep_alive + 30);
-  }
-
-  /**
-   * Getter for keep_alive interval
-   */
-  public function get_keep_alive()
-  {
-    return $this->keep_alive;
-  }
 
   /**
    * Getter for remote IP saved with this session
@@ -552,6 +533,7 @@
     return $this->ip;
   }
 
+
   /**
    * Setter for cookie encryption secret
    */
diff --git a/program/js/app.js b/program/js/app.js
index 7764c6c..f372c0f 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -952,9 +952,6 @@
           break;
         }
 
-        // re-set keep-alive timeout
-        this.start_keepalive();
-
         this.submit_messageform(true);
         break;
 
@@ -6077,6 +6074,9 @@
       $('<a>').attr('href', url).appendTo(document.body).get(0).click();
     else
       target.location.href = url;
+
+    // reset keep-alive interval
+    this.start_keepalive();
   };
 
   // send a http request to the server
@@ -6105,6 +6105,9 @@
       success: function(data){ ref.http_response(data); },
       error: function(o, status, err) { ref.http_error(o, status, err, lock, action); }
     });
+
+    // reset keep-alive interval
+    this.start_keepalive();
   };
 
   // send a http POST request to the server
@@ -6137,6 +6140,9 @@
       success: function(data){ ref.http_response(data); },
       error: function(o, status, err) { ref.http_error(o, status, err, lock, action); }
     });
+
+    // reset keep-alive interval
+    this.start_keepalive();
   };
 
   // aborts ajax request
@@ -6264,6 +6270,9 @@
 
     this.triggerEvent('responseafter', {response: response});
     this.triggerEvent('responseafter'+response.action, {response: response});
+
+    // reset keep-alive interval
+    this.start_keepalive();
   };
 
   // handle HTTP request errors
@@ -6288,8 +6297,6 @@
     // re-send keep-alive requests after 30 seconds
     if (action == 'keep-alive')
       setTimeout(function(){ ref.keep_alive(); ref.start_keepalive(); }, 30000);
-    else if (action == 'check-recent')
-      setTimeout(function(){ ref.check_for_recent(false); ref.start_keepalive(); }, 30000);
   };
 
   // post the given form to a hidden iframe
@@ -6459,20 +6466,16 @@
     }
   };
 
-
-  // starts interval for keep-alive/check-recent signal
+  // starts interval for keep-alive signal
   this.start_keepalive = function()
   {
-    if (!this.env.keep_alive || this.env.framed)
+    if (!this.env.session_lifetime || this.env.framed || this.task == 'login' || this.env.action == 'print')
       return;
 
     if (this._int)
       clearInterval(this._int);
 
-    if (this.task == 'mail' && this.gui_objects.mailboxlist)
-      this._int = setInterval(function(){ ref.check_for_recent(false); }, this.env.keep_alive * 1000);
-    else if (this.task != 'login' && this.env.action != 'print')
-      this._int = setInterval(function(){ ref.keep_alive(); }, this.env.keep_alive * 1000);
+    this._int = setInterval(function(){ ref.keep_alive(); }, this.env.session_lifetime * 0.5 * 1000);
   };
 
   // sends keep-alive signal
@@ -6493,8 +6496,6 @@
     if (refresh) {
       lock = this.set_busy(true, 'checkingmail');
       url._refresh = 1;
-      // reset check-recent interval
-      this.start_keepalive();
     }
 
     if (this.gui_objects.messagelist)

--
Gitblit v1.9.1