diff --git a/autologout.js b/autologout.js
index 1d06c40..591a4ff 100644
--- a/autologout.js
+++ b/autologout.js
@@ -1,53 +1,71 @@
(function ($) {
+
Drupal.behaviors.autologout = {
attach: function(context, settings) {
+ if (context != document) {
+ return;
+ }
+
var paddingTimer;
var t;
var theDialog;
+ var localSettings;
// Activity is a boolean used to detect a user has
// interacted with the page.
var activity;
- if (context == document) {
+ // Prevent settings being overriden by ajax callbacks by cloning the settings.
+ localSettings = jQuery.extend(true, {}, settings.autologout);
- if (Drupal.settings.autologout.refresh_only) {
- // On pages that cannot be logged out of don't start the logout countdown.
- t = setTimeout(keepAlive, Drupal.settings.autologout.timeout);
- }
- else {
- // Set no activity to start with.
- activity = false;
+ Drupal.ajax.prototype.refreshAutologout = function() {
+ $.ajax(this.options);
+ t = setTimeout(init, localSettings.timeout);
+ activity = false;
+ return false;
+ };
- // Bind formUpdated events to preventAutoLogout event.
- $('body').bind('formUpdated', function(event) {
- $(event.target).trigger('preventAutologout');
- });
+ Drupal.ajax['refresh.autologout'] = new Drupal.ajax(null, $(document.body), {
+ url: '/autologout_ahah_set_last/ajax/',
+ event: null
+ });
- // Support for CKEditor.
- if (typeof CKEDITOR !== 'undefined') {
- CKEDITOR.on('instanceCreated', function(e) {
- e.editor.on('contentDom', function() {
- e.editor.document.on('keyup', function(event) {
- // Keyup event in ckeditor should prevent autologout.
- $(e.editor.element.$).trigger('preventAutologout');
- }
- );
- });
- });
- }
+ if (localSettings.refresh_only) {
+ // On pages that cannot be logged out of don't start the logout countdown.
+ t = setTimeout(keepAlive, localSettings.timeout);
+ }
+ else {
+ // Set no activity to start with.
+ activity = false;
- $('body').bind('preventAutologout', function(event) {
- // When the preventAutologout event fires
- // we set activity to true.
- activity = true;
- });
+ // Bind formUpdated events to preventAutoLogout event.
+ $('body').bind('formUpdated', function(event) {
+ $(event.target).trigger('preventAutologout');
+ });
- // On pages where the user can be logged out, set the timer to popup
- // and log them out.
- t = setTimeout(init, Drupal.settings.autologout.timeout);
+ // Support for CKEditor.
+ if (typeof CKEDITOR !== 'undefined') {
+ CKEDITOR.on('instanceCreated', function(e) {
+ e.editor.on('contentDom', function() {
+ e.editor.document.on('keyup', function(event) {
+ // Keyup event in ckeditor should prevent autologout.
+ $(e.editor.element.$).trigger('preventAutologout');
+ }
+ );
+ });
+ });
}
+
+ $('body').bind('preventAutologout', function(event) {
+ // When the preventAutologout event fires
+ // we set activity to true.
+ activity = true;
+ });
+
+ // On pages where the user can be logged out, set the timer to popup
+ // and log them out.
+ t = setTimeout(init, localSettings.timeout);
}
function init() {
@@ -61,7 +79,7 @@
// The user has not been active, ask them if they want to stay logged in
// and start the logout timer.
- paddingTimer = setTimeout(confirmLogout, Drupal.settings.autologout.timeout_padding);
+ paddingTimer = setTimeout(confirmLogout, localSettings.timeout_padding);
// While the countdown timer is going, lookup the remaining time. If there
// is more time remaining (i.e. a user is navigating in another tab), then
@@ -79,10 +97,10 @@
}
},
error: function(XMLHttpRequest, textStatus) {
- if (XMLHttpRequest.status == 403) {
- window.location = Drupal.settings.autologout.redirect_url;
- }
- }
+ if (XMLHttpRequest.status == 403) {
+ window.location = localSettings.redirect_url;
+ }
+ }
});
}
}
@@ -90,15 +108,15 @@
function keepAlive() {
$.ajax({
url: Drupal.settings.basePath + "autologout_ahah_set_last",
- type: "POST",
- success: function() {
- // After keeping the connection alive, start the timer again.
- t = setTimeout(keepAlive, Drupal.settings.autologout.timeout);
- },
- error: function(XMLHttpRequest, textStatus) {
- if (XMLHttpRequest.status == 403) {
- window.location = Drupal.settings.autologout.redirect_url;
- }
+ type: "POST",
+ success: function() {
+ // After keeping the connection alive, start the timer again.
+ t = setTimeout(keepAlive, localSettings.timeout);
+ },
+ error: function(XMLHttpRequest, textStatus) {
+ if (XMLHttpRequest.status == 403) {
+ window.location = localSettings.redirect_url;
+ }
}
});
}
@@ -116,12 +134,12 @@
logout();
};
- return $('
' + Drupal.settings.autologout.message + '
').dialog({
+ return $(' ' + localSettings.message + '
').dialog({
modal: true,
closeOnEscape: false,
width: "auto",
dialogClass: 'autologout-dialog',
- title: Drupal.settings.autologout.title,
+ title: localSettings.title,
buttons: buttons,
close: function(event, ui) {
logout();
@@ -153,32 +171,19 @@
url: Drupal.settings.basePath + "autologout_ahah_logout",
type: "POST",
success: function() {
- window.location = Drupal.settings.autologout.redirect_url;
+ window.location = localSettings.redirect_url;
},
error: function(XMLHttpRequest, textStatus) {
if (XMLHttpRequest.status == 403) {
- window.location = Drupal.settings.autologout.redirect_url;
+ window.location = localSettings.redirect_url;
}
}
});
}
function refresh() {
- $.ajax({
- url: Drupal.settings.basePath + "autologout_ahah_set_last",
- type: "POST",
- success: function() {
- t = setTimeout(init, Drupal.settings.autologout.timeout);
- activity = false;
- },
- error: function(XMLHttpRequest, textStatus) {
- if (XMLHttpRequest.status == 403) {
- window.location = Drupal.settings.autologout.redirect_url;
- }
- }
- });
+ Drupal.ajax['refresh.autologout'].refreshAutologout();
}
-
}
};
})(jQuery);
diff --git a/autologout.module b/autologout.module
index ff90b5c..0cbc29b 100644
--- a/autologout.module
+++ b/autologout.module
@@ -48,6 +48,8 @@ function autologout_menu() {
'page callback' => 'autologout_ahah_set_last',
'access callback' => 'user_is_logged_in',
'type' => MENU_CALLBACK,
+ 'theme callback' => 'ajax_base_page_theme',
+ 'delivery callback' => 'ajax_deliver',
);
$items['autologout_ajax_get_time_left'] = array(
@@ -61,6 +63,113 @@ function autologout_menu() {
}
/**
+ * Implements hook_block_info().
+ */
+function autologout_block_info() {
+ $blocks = array();
+
+ $blocks['info'] = array(
+ 'info' => t('Automated Logout info'),
+ );
+
+ return $blocks;
+}
+
+/**
+ * Implements hook_block_view().
+ */
+function autologout_block_view($delta = '') {
+ global $user;
+
+ $block = array();
+
+ if (_autologout_prevent()) {
+ // Don't display the block if the user is not going
+ // to be logged out on this page.
+ return;
+ }
+
+ $block['subject'] = filter_xss_admin(variable_get('autologout_block_title', t('Autologout warning block')));
+
+ if (module_exists('jstimer') && module_exists('jst_timer')) {
+ $block['content'] = array(drupal_get_form('autologout_create_block_form'));
+ }
+ else {
+ $timeout = (int) variable_get('autologout_timeout', 1800);
+ $block['content'] = t('You will be logged out in !time if this page is not refreshed before then.', array('!time' => format_interval($timeout)));
+ }
+
+ return $block;
+}
+
+/**
+ * Drupal reset timer form on timer block.
+ */
+function autologout_create_block_form() {
+
+ $markup = autologout_create_timer();
+
+ $form['test'] = array(
+ '#type' => 'button',
+ '#value' => t('Reset Timeout'),
+ '#weight' => 1,
+ '#limit_validation_errors' => FALSE,
+ '#executes_submit_callback' => FALSE,
+ '#ajax' => array(
+ 'callback' => 'autologout_ahah_set_last',
+ ),
+ );
+
+ $form['timer'] = array(
+ '#type' => 'markup',
+ '#value' => $markup,
+ '#markup' => $markup,
+ );
+
+ return $form;
+}
+
+/**
+ * Get the timer HTML markup.
+ *
+ * @return string
+ * HTML to insert a countdown timer.
+ */
+function autologout_create_timer() {
+ // @TODO - should be a theme function?
+ $refresh = (int) variable_get('autologout_refresh_delta', 0);
+ $redirect_url = filter_xss_admin(variable_get('autologout_redirect_url', 'user/login'));
+ $timeout = _autologout_get_user_timeout();
+ $timeformat = filter_xss_admin(variable_get('autologout_jstimer_format', '%hours%:%mins%:%secs%'));
+
+ $markup = "
+ $timeout
+ $timeformat
+
";
+
+ return render($markup);
+}
+
+/**
+ * Implements hook_block_configure().
+ */
+function autologout_block_configure($delta = '') {
+ $block = array();
+
+ if (module_exists('jstimer')) {
+ if (!module_exists('jst_timer')) {
+ drupal_set_message(t('The "Widget: timer" module must also be enabled for the dynamic countdown to work in the automated logout block.'), 'error');
+ }
+
+ if (variable_get('jstimer_js_load_option', 0) != 1) {
+ drupal_set_message(t('The Javascript timer module\'s "Javascript load options" setting should be set to "Every page" for the dynamic countdown to work in the automated logout block.'), 'error');
+ }
+ }
+
+ return $block;
+}
+
+/**
* Implements hook_help().
*/
function autologout_help($path, $arg) {
@@ -221,10 +330,8 @@ function autologout_init() {
}
// Check if JS should be included on this request.
- foreach (module_invoke_all('autologout_prevent') as $prevent) {
- if (!empty($prevent)) {
- return;
- }
+ if (_autologout_prevent()) {
+ return;
}
// Check if anything wants to be refresh only. This URL would
@@ -241,7 +348,7 @@ function autologout_init() {
$now = time();
$timeout = _autologout_get_user_timeout();
$timeout_padding = variable_get('autologout_padding', 10);
- $redirct_url = variable_get('autologout_redirect_url', 'user/login');
+ $redirect_url = variable_get('autologout_redirect_url', 'user/login');
$redirect_query = drupal_get_destination() + array('autologout_timeout' => 1);
drupal_add_library('system', 'ui.dialog');
@@ -251,10 +358,12 @@ function autologout_init() {
'timeout' => $refresh_only ? ($timeout * 500) : ($timeout * 1000),
'timeout_padding' => $timeout_padding * 1000,
'message' => t('@msg', array('@msg' => $msg)),
- 'redirect_url' => url($redirct_url, array('query' => $redirect_query)),
+ 'redirect_url' => url($redirect_url, array('query' => $redirect_query)),
'title' => t('@name Alert', array('@name' => variable_get('site_name', 'Drupal'))),
'refresh_only' => $refresh_only,
);
+
+ drupal_add_library('system', 'drupal.ajax');
drupal_add_js(array('autologout' => $settings), 'setting');
drupal_add_js(drupal_get_path('module', 'autologout') . "/autologout.js");
@@ -290,7 +399,7 @@ function autologout_autologout_prevent() {
// Don't include autologout JS checks on ajax callbacks.
$paths = array(
- 'ajax',
+ 'system',
'autologout_ajax_get_time_left',
'autologout_ahah_logout',
'autologout_ahah_set_last',
@@ -344,6 +453,13 @@ function autologout_ahah_logout() {
*/
function autologout_ahah_set_last() {
$_SESSION['autologout_last'] = time();
+
+ // Reset the timer.
+ $markup = autologout_create_timer();
+ $commands = array();
+ $commands[] = ajax_command_replace('#timer', $markup);
+
+ return array('#type' => 'ajax', '#commands' => $commands);
}
/**
@@ -461,3 +577,20 @@ function _autologout_inactivity_message() {
drupal_set_message(t($message));
}
}
+
+/**
+ * Determine if autologout should be prevented.
+ *
+ * @return bool
+ * TRUE if there is a reason not to autologout
+ * the current user on the current page.
+ */
+function _autologout_prevent() {
+ foreach (module_invoke_all('autologout_prevent') as $prevent) {
+ if (!empty($prevent)) {
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}