You are here

autologout.module in Automated Logout 6.3

Used to automagically log out a user after a certain time.

File

autologout.module
View source
<?php

/**
 * @file
 * Used to automagically log out a user after a certain time.
 */

/**
 * Implementation of hook_help().
 */
function autologout_help($path, $arg) {
  switch ($path) {
    case 'admin/help#autologout':
      $output = '<p>' . t('The <em>Automated Logout</em> module allows you to force users to log out after a given amount of time. You can configure this in the <a href="@usersettings">User settings administration</a>.', array(
        '@usersettings' => url('admin/user/settings'),
      )) . '</p>';
      $output .= '<p>' . t('If you have the <a href="@jstimer">Javascript timer module</a> enabled, the <a href="@automatedlogoutblock">Automated Logout block</a> will have a live countdown timer.', array(
        '@automatedlogoutblock' => url('admin/build/block'),
        '@jstimer' => 'http://drupal.org/project/jstimer',
      )) . '</p>';
      return $output;
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function autologout_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_admin_settings') {
    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');
      }
    }
    $form['autologout'] = array(
      '#type' => 'fieldset',
      '#title' => t('Automated Logout'),
      '#weight' => -1,
    );
    $form['autologout']['autologout_seconds'] = array(
      '#type' => 'textfield',
      '#size' => 6,
      '#title' => t('Time until forced logout (seconds)'),
      '#description' => t('Force the user to logout after the given amount of time in seconds (0=disabled).'),
      '#default_value' => variable_get('autologout_seconds', 0),
    );
    $form['#validate'][] = 'autologout_form_validate';
    $form['#submit'][] = 'autologout_form_submit';
  }
}
function autologout_form_validate($form, &$form_state) {
  $autologout_seconds = $form_state['values']['autologout_seconds'];
  if ($autologout_seconds != 0 && $autologout_seconds < 15) {
    form_set_error('autologout_seconds', t('The automated logout module requires the forced logout value to be greater than 15 seconds.', array(
      '%add-child' => t('Add child page'),
    )));
  }
}
function autologout_form_submit($form, &$form_state) {

  // Update the session's last access.
  // #509590, if you wait on the admin page and set a small timeout, you will be logged out.
  $_SESSION['autologout_lastaccess'] = time();
}

/**
 * Implementation of hook_init().
 */
function autologout_init() {
  global $user;
  global $base_root;
  if ($user->uid > 0) {

    // See if automatic logout is enabled.
    $seconds = variable_get('autologout_seconds', 0);
    if ($seconds > 0) {

      // Check the session's last access.
      if (!isset($_SESSION['autologout_lastaccess']) || (int) $_SESSION['autologout_lastaccess'] > time() - $seconds) {

        // Update the session's last access.
        $_SESSION['autologout_lastaccess'] = time();
        drupal_set_html_head("<meta http-equiv='refresh' content='{$seconds}';url='{$base_root}'>");
      }
      else {

        // Force the user to log out.
        // 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();
        drupal_goto('autologout/logout');
      }
    }
  }
}
function autologout_menu() {
  $items['autologout/logout'] = array(
    'title' => t('Automated logout'),
    'page callback' => 'autologout_logout',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_block().
 */
function autologout_block($op = 'list', $delta = 0, $edit = array()) {

  // The $op parameter determines what piece of information is being requested.
  switch ($op) {
    case 'list':
      $blocks['autologout'] = array(
        'info' => t('Automated Logout'),
      );
      return $blocks;
      break;
    case 'view':
      $block = array();
      switch ($delta) {
        case 'autologout':
          $block['subject'] = t('Automated Logout');
          $block['content'] = theme('autologout_block');
          break;
      }
      return $block;
  }
}

/**
 * Implementation of hook_theme().
 */
function autologout_theme() {
  return array(
    'autologout_block' => array(),
  );
}

/**
 * Themes the automated logout block.
 */
function theme_autologout_block() {
  global $user;
  $seconds = (int) variable_get('autologout_seconds', 0);
  if ($user->uid != 0 && $seconds > 0) {
    if (module_exists('jstimer') && module_exists('jst_timer')) {
      $content = theme('jstimer', 'jst_timer', array(
        'interval' => $seconds,
        'format_txt' => t('You will be logged out in ') . ' %hours%:%mins%:%secs%',
        'complete' => t('You have been automatically logged out due to inactivity.'),
        'no_js_txt' => t('You will be logged out in !time.', array(
          '!time' => format_interval($seconds),
        )),
      ));
    }
    return $content;
  }
}
function autologout_logout() {
  global $user;
  if ($user->uid == 0) {
    print theme('page', t('You have been automatically logged out due to inactivity.'));
  }
  else {
    drupal_goto();
  }
}

Functions

Namesort descending Description
autologout_block Implementation of hook_block().
autologout_form_alter Implementation of hook_form_alter().
autologout_form_submit
autologout_form_validate
autologout_help Implementation of hook_help().
autologout_init Implementation of hook_init().
autologout_logout
autologout_menu
autologout_theme Implementation of hook_theme().
theme_autologout_block Themes the automated logout block.