You are here

function autologout_init in Automated Logout 7.4

Same name and namespace in other branches
  1. 6.4 autologout.module \autologout_init()
  2. 6.2 autologout.module \autologout_init()
  3. 6.3 autologout.module \autologout_init()
  4. 7.2 autologout.module \autologout_init()

Implements hook_init().

File

./autologout.module, line 353
Used to automagically log out a user after a preset time.

Code

function autologout_init() {
  global $user;
  if (empty($user->uid)) {
    if (!empty($_GET['autologout_timeout']) && $_GET['autologout_timeout'] == 1 && empty($_POST)) {
      _autologout_inactivity_message();
    }
    return;
  }

  // Check if JS should be included on this request.
  if (_autologout_prevent()) {
    return;
  }

  // Check if anything wants to be refresh only. This URL would
  // include the javascript but will keep the login alive whilst
  // that page is opened.
  $refresh_only = _autologout_refresh_only();
  $now = time();
  $timeout = _autologout_get_user_timeout();
  $timeout_padding = variable_get('autologout_padding', 20);
  $autologout_redirect_url = variable_get('autologout_redirect_url', 'user/login');
  $redirect_url = empty($autologout_redirect_url) ? current_path() : $autologout_redirect_url;
  $redirect_query = autologout_get_current_path_as_destination() + array(
    'autologout_timeout' => 1,
  );
  $no_dialog = variable_get('autologout_no_dialog', FALSE);
  $use_alt_logout_method = variable_get('autologout_use_alt_logout_method', FALSE);
  drupal_add_library('system', 'ui.dialog');

  // Provide a hook to allow the redirect URL and query to be customised
  drupal_alter('autologout_redirect_url', $redirect_url, $redirect_query);

  // Get all settings JS will need for dialog.
  $msg = t(variable_get('autologout_message', 'We are about to log you out for inactivity. If we do, you will lose any unsaved work. Do you need more time?'));
  $settings = array(
    'timeout' => $refresh_only ? $timeout * 500 : $timeout * 1000,
    'timeout_padding' => $timeout_padding * 1000,
    'message' => filter_xss($msg),
    'redirect_url' => url($redirect_url, array(
      'query' => $redirect_query,
    )),
    'title' => t('@name Alert', array(
      '@name' => variable_get('site_name', 'Drupal'),
    )),
    'refresh_only' => $refresh_only,
    'no_dialog' => $no_dialog,
    'use_alt_logout_method' => $use_alt_logout_method,
  );

  // If this is an AJAX request, then the logout redirect url should still be
  // referring to the page that generated this request
  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    global $base_url;
    $relative_url = str_replace($base_url . '/', '', $_SERVER['HTTP_REFERER']);
    $settings['redirect_url'] = url($redirect_url, array(
      'query' => array(
        'destination' => urlencode($relative_url),
      ),
      'autologout_timeout' => 1,
    ));
  }
  drupal_add_library('system', 'drupal.ajax');
  drupal_add_js(array(
    'autologout' => $settings,
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'autologout') . "/autologout.js");

  // We need a backup plan if JS is disabled.
  if (!$refresh_only && isset($_SESSION['autologout_last'])) {

    // If time since last access is > than the timeout + padding, log them out.
    if ($now - $_SESSION['autologout_last'] >= $timeout + (int) $timeout_padding) {
      _autologout_logout();

      // User has changed so force Drupal to remake decisions based on user.
      global $theme, $theme_key;
      drupal_static_reset();
      $theme = NULL;
      $theme_key = NULL;
      menu_set_custom_theme();
      drupal_theme_initialize();
      _autologout_inactivity_message();
    }
    else {
      $_SESSION['autologout_last'] = $now;
    }
  }
  else {
    $_SESSION['autologout_last'] = $now;
  }
}