You are here

function autologout_init in Automated Logout 6.2

Same name and namespace in other branches
  1. 6.4 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()

Implementation of hook_init().

File

./autologout.module, line 145
Used to automagically log out a user after a preset time, AjK May 2006

Code

function autologout_init() {

  // We have to perform a load in order to assure that the $user->autologout bits are present.
  global $user;
  if ($user->uid) {

    // $user gets modified by reference.
    autologout_user('load', array(), $user);
  }
  else {
    $user->autologout = 0;
    return;
  }

  // Check for enabled autologout and excluded users.
  if (_autologout_local_settings('enabled') && !_autologout_exclude_by_role()) {
    $timeout = (int) _autologout_local_settings('timeout');
    $nowtime = time();
    if (!isset($_SESSION['lastaccess'])) {
      $_SESSION['lastaccess'] = $nowtime;
    }

    // update lastaccess from any cache hits which wouldn't have been processes by hook_init()
    if (isset($_SESSION['autologout_hits'])) {
      foreach ($_SESSION['autologout_hits'] as $hit) {
        if ((int) $hit - (int) $_SESSION['lastaccess'] > 0) {

          // if hit wouldn't have timedout, update lastaccess
          if ((int) $hit - (int) $_SESSION['lastaccess'] < $timeout) {

            // rebase lastaccess
            $_SESSION['lastaccess'] = $hit;
          }
        }
      }
    }

    // now normal processing because the cache hits have been accounted for.
    if ($nowtime - (int) $_SESSION['lastaccess'] < $timeout) {

      // the timeout has not yet occurred.
      $_SESSION['lastaccess'] = $nowtime;
      $_SESSION['autologout_hits'] = array(
        $nowtime,
      );
      $refresh = (int) _autologout_local_settings('refresh_delta');
      if ($refresh >= 0) {
        $force_refresh = $timeout + $refresh;
        $this_header = "<meta http-equiv=\"refresh\" content=\"{$force_refresh};\" />";
        $this_head = drupal_set_html_head($this_header);
      }
    }
    else {

      // timeout occured, logout and end session
      unset($_SESSION['lastaccess']);
      if (_autologout_local_settings('use_watchdog')) {
        watchdog('autologout', 'User %name automatically logged out.', array(
          '%name' => $user->name,
        ));
      }

      // code from core(user.pages.inc), can't use it directly because we need need a custom goto
      watchdog('user', 'Session closed for %name.', array(
        '%name' => $user->name,
      ));

      // Destroy the current session:
      session_destroy();

      // Only variables can be passed by reference workaround.
      $null = NULL;
      user_module_invoke('logout', $null, $user);

      // Load the anonymous user
      $user = drupal_anonymous_user();
      $redirect_url = filter_xss_admin(_autologout_local_settings('redirect_url'));
      if ($redirect_url != '') {
        drupal_goto($redirect_url);
      }
      else {
        drupal_goto('autologout/logout', drupal_get_destination());
      }
      return;
    }
  }

  // Clear session variable if you are not using autologout function and for anonymous users.
  if (isset($_SESSION['autologout_hits']) && (!_autologout_local_settings('enabled') || $user->uid == 0)) {
    unset($_SESSION['autologout_hits']);
  }
}