diff --git a/autologout.js b/autologout.js index 1d06c40..613ef9d 100644 --- a/autologout.js +++ b/autologout.js @@ -1,4 +1,5 @@ (function ($) { + Drupal.behaviors.autologout = { attach: function(context, settings) { @@ -12,6 +13,18 @@ if (context == document) { + Drupal.ajax.prototype.refreshAutologout = function() { + $.ajax(this.options); + t = setTimeout(init, Drupal.settings.autologout.timeout); + activity = false; + return false; + }; + + Drupal.ajax['refresh.autologout'] = new Drupal.ajax(null, $(document.body), { + url: '/autologout_ahah_set_last/ajax/', + event: null + }); + 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); @@ -164,21 +177,8 @@ } 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..d4fa500 100644 --- a/autologout.module +++ b/autologout.module @@ -48,6 +48,7 @@ function autologout_menu() { 'page callback' => 'autologout_ahah_set_last', 'access callback' => 'user_is_logged_in', 'type' => MENU_CALLBACK, + 'theme callback' => 'ajax_base_page_theme', ); $items['autologout_ajax_get_time_left'] = array( @@ -61,6 +62,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 = (int) variable_get('autologout_timeout', 1800); + $timeformat = filter_xss_admin(variable_get('autologout_jstimer_format', '%hours%:%mins%:%secs%')); + + $markup = "
+ + +
"; + + 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 +329,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 @@ -255,6 +361,8 @@ function autologout_init() { '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"); @@ -342,8 +450,20 @@ function autologout_ahah_logout() { /** * Callback to reset the last access session variable. */ -function autologout_ahah_set_last() { +function autologout_ahah_set_last($action = '') { $_SESSION['autologout_last'] = time(); + + // Reset the timer. + $markup = autologout_create_timer(); + $commands = array(); + $commands[] = ajax_command_replace('#timer', $markup); + + $command_return = array('#type' => 'ajax', '#commands' => $commands); + + if ($action == 'ajax') { + return ajax_deliver($command_return); + } + return $command_return; } /** @@ -461,3 +581,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; +}