From dd932bc6f9417d0e124c8d3a41ddf98c75200629 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 12 Nov 2012 21:05:30 +0100 Subject: [PATCH] Refreshing session every (SESSION_TIMEOUT - 1) minute This way the session will not expire as long as the browser window is open, preventing the SECURITY_TOKEN to become invalid due to inactivity. If the session timeout is 30 minutes (default value), a single request will be sent every 29 minutes. --- com.woltlab.wcf/template/headInclude.tpl | 3 +++ wcfsetup/install/files/js/WCF.js | 25 +++++++++++++++++++ .../lib/data/session/SessionAction.class.php | 24 ++++++++++++++++++ .../install/files/lib/system/WCF.class.php | 3 +++ .../system/session/SessionHandler.class.php | 13 ++++++++++ 5 files changed, 68 insertions(+) diff --git a/com.woltlab.wcf/template/headInclude.tpl b/com.woltlab.wcf/template/headInclude.tpl index d51756788c..9e8c0bfad9 100644 --- a/com.woltlab.wcf/template/headInclude.tpl +++ b/com.woltlab.wcf/template/headInclude.tpl @@ -119,6 +119,9 @@ } }); {/if} + {if $__sessionKeepAlive|isset} + new WCF.System.KeepAlive({@$__sessionKeepAlive}); + {/if} }); //]]> diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index c3bc2f6331..7ae1d88ba5 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -5194,6 +5194,31 @@ WCF.System.PageNavigation = { } }; +/** + * Sends periodical requests to protect the session from expiring. By default + * it will send a request 1 minute before it would expire. + * + * @param integer seconds + */ +WCF.System.KeepAlive = Class.extend({ + /** + * Initializes the WCF.System.KeepAlive class. + * + * @param integer seconds + */ + init: function(seconds) { + new WCF.PeriodicalExecuter(function() { + new WCF.Action.Proxy({ + autoSend: true, + data: { + actionName: 'keepAlive', + className: 'wcf\\data\\session\\SessionAction' + } + }); + }, (seconds * 1000)); + } +}); + /** * Default implementation for inline editors. * diff --git a/wcfsetup/install/files/lib/data/session/SessionAction.class.php b/wcfsetup/install/files/lib/data/session/SessionAction.class.php index 7a3577c2a2..20747fcb90 100644 --- a/wcfsetup/install/files/lib/data/session/SessionAction.class.php +++ b/wcfsetup/install/files/lib/data/session/SessionAction.class.php @@ -1,6 +1,8 @@ lastActivityTime == TIME_NOW) { + return; + } + + SessionHandler::getInstance()->keepAlive(); + } } diff --git a/wcfsetup/install/files/lib/system/WCF.class.php b/wcfsetup/install/files/lib/system/WCF.class.php index c33db794ac..7e9173f0d9 100644 --- a/wcfsetup/install/files/lib/system/WCF.class.php +++ b/wcfsetup/install/files/lib/system/WCF.class.php @@ -432,6 +432,9 @@ class WCF { foreach ($loadedApplications as $application) { $application->__run(); } + + // refresh the session 1 minute before it expires + self::getTPL()->assign('__sessionKeepAlive', (SESSION_TIMEOUT - 60)); } } diff --git a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php index af5eb3819a..e74bc5c16e 100644 --- a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php +++ b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php @@ -536,6 +536,19 @@ class SessionHandler extends SingletonFactory { $sessionEditor->update($data); } + /** + * Updates last activity time to protect session from expiring. + */ + public function keepAlive() { + $this->disableUpdate(); + + // update last activity time + $sessionEditor = new $this->sessionEditorClassName($this->session); + $sessionEditor->update(array( + 'lastActivityTime' => TIME_NOW + )); + } + /** * Deletes this session and it's related data. */ -- 2.20.1