You are here

function autologout_init in Automated Logout 6.4

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

Implements hook_init().

File

./autologout.module, line 333
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', 10);
  $redirct_url = variable_get('autologout_redirect_url', 'user/login');
  $redirect_query = drupal_get_destination() . '&autologout_timeout=1';
  $no_dialog = variable_get('autologout_no_dialog', FALSE);
  $jquery_ui = module_exists('jquery_ui');
  if ($jquery_ui) {

    // If the installed jquery_ui module is less than 1.5, the jquery_ui_get_path function isn't available. In that
    // case, check the module directory for backwards compatibility. Specifically, check in
    // JQUERY_UI_MODULE_PATH/jquery.ui/themes/default/.
    $css_folder = version_compare(jquery_ui_get_version(), '1.7', '>=') ? 'base' : 'default';
    $jquery_ui_lib_path_begin = '';
    $jquery_ui_lib_path_end = '/themes/' . $css_folder . '/ui.all.css';
    $jquery_ui_module_path = drupal_get_path('module', 'jquery_ui');
    if (function_exists("jquery_ui_get_path")) {
      $jquery_ui_lib_path_begin = jquery_ui_get_path();
    }
    elseif (file_exists($jquery_ui_module_path . '/jquery.ui' . $jquery_ui_lib_path_end)) {
      $jquery_ui_lib_path_begin = $jquery_ui_module_path . '/jquery.ui';
    }
    $jquery_ui_css_full_path = $jquery_ui_lib_path_begin . $jquery_ui_lib_path_end;
    if (file_exists($jquery_ui_css_full_path)) {
      jquery_ui_add(array(
        'ui.dialog',
      ));
      drupal_add_css($jquery_ui_css_full_path);
    }
    else {
      $jquery_ui = FALSE;
      watchdog('user', 'The jquery ui css file was not found.');
    }
  }

  // Get all settings JS will need for dialog.
  $msg = t('@msg', array(
    '@msg' => variable_get('autologout_message', 'Your session is about to expire. Do you want to reset it?'),
  ));
  $settings = array(
    '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,
    )),
    'title' => t('@name Alert', array(
      '@name' => variable_get('site_name', 'Drupal'),
    )),
    'jquery_ui' => $jquery_ui,
    'refresh_only' => $refresh_only,
    'no_dialog' => $no_dialog,
  );

  // 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') {
    $relative_url = str_replace($GLOBALS['base_url'] . '/', '', $_SERVER['HTTP_REFERER']);
    $settings['redirect_url'] = url($redirct_url, array(
      'query' => 'destination=' . urlencode($relative_url) . '&autologout_timeout=1',
    ));
  }

  // Need to add 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();
      _autologout_inactivity_message();
    }
    else {
      $_SESSION['autologout_last'] = $now;
    }
  }
  else {
    $_SESSION['autologout_last'] = $now;
  }
}