You are here

login_history.module in Login History 6

Same filename and directory in other branches
  1. 8 login_history.module
  2. 7 login_history.module

File

login_history.module
View source
<?php

/**
 * Implements hook_user().
 */
function login_history_user($op, &$edit, &$account) {
  if ($op == 'login') {

    // Is this a one-time login?
    $request_array = explode('/', $_GET['q']);
    if ($request_array[0] == 'user' && isset($request_array[1]) && $request_array[1] == 'reset' && $request_array[5] == 'login') {
      $one_time = 1;
    }
    else {
      $one_time = 0;
    }

    // Now save the user's current login timestamp to login_history.
    $user_agent = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];
    db_query("INSERT INTO {login_history} (uid, login, hostname, one_time, user_agent) VALUES (%d, %d, '%s', %d, '%s')", $account->uid, $account->login, ip_address(), $one_time, $user_agent);
  }
}

/**
 * Implements hook_menu().
 * Define menu items and page callbacks.
 *
 * @return
 *   An array of menu items.
 */
function login_history_menu() {
  $items = array();
  $items['admin/reports/login-history'] = array(
    'title' => 'Login history',
    'description' => 'Shows previous login information for site users. Useful for troubleshooting and monitoring.',
    'page callback' => 'login_history_report_callback',
    'access arguments' => array(
      'administer users',
    ),
    'file' => 'includes/login_history.pages.inc',
  );
  $items['user/%user/login_history'] = array(
    'title' => 'Login history',
    'description' => '',
    'page callback' => 'login_history_report_callback',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'login_history_access_user_history_page',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/login_history.pages.inc',
  );
  return $items;
}

/**
 * Implements hook_perm().
 */
function login_history_perm() {
  return array(
    'view own login history',
    'view all login histories',
  );
}
function login_history_access_user_history_page($account) {
  return $account->uid == $GLOBALS['user']->uid && user_access('view own login history') || user_access('view all login histories');
}

/**
 * Implements hook_block().
 */
function login_history_block($op = 'list', $delta = '', $edit = array()) {
  switch ($op) {
    case 'list':

      // Show their last login info.
      $blocks['login_history_last']['info'] = t("Last login");
      $blocks['login_history_last']['cache'] = BLOCK_CACHE_PER_USER;
      return $blocks;
    case 'view':
      switch ($delta) {
        case 'login_history_last':
          if (user_is_anonymous()) {
            return;
          }

          // Retrieve a list of new users who have subsequently accessed the site successfully.
          if ($last_login = login_history_last_login()) {
            $hostname = $last_login->hostname == ip_address() ? t('this IP address') : $last_login->hostname;
            $user_agent = $last_login->user_agent == $_SERVER['HTTP_USER_AGENT'] ? t('this browser') : $last_login->user_agent;
            $output = '<p>' . t('You last logged in from @hostname using @user_agent.', array(
              '@hostname' => $hostname,
              '@user_agent' => $user_agent,
            )) . '</p>';
            if (user_access('view own login history')) {
              global $user;
              $output .= '<span class="read-more">' . t('<a href="@link">View your login history.</a>', array(
                '@link' => url('user/' . $user->uid . '/login_history'),
              )) . '</span>';
            }
            $block['subject'] = t('Last login');
            $block['content'] = $output;
            return $block;
          }
      }
  }
}

/**
 * Provide data about the last login for a user.
 *
 * @param $account Optional user object. The only thing that matters is the uid.
 */
function login_history_last_login($account = NULL) {
  if (user_is_anonymous()) {
    return;
  }
  if (empty($account)) {
    global $user;
    $account = $user;
  }
  $last_login = db_query("SELECT login, hostname, one_time, user_agent\n                   FROM {login_history}\n                   WHERE uid = %d\n                   ORDER BY login DESC\n                   LIMIT 1, 2", array(
    $account->uid,
  ));
  return db_fetch_object($last_login);
}

Functions

Namesort descending Description
login_history_access_user_history_page
login_history_block Implements hook_block().
login_history_last_login Provide data about the last login for a user.
login_history_menu Implements hook_menu(). Define menu items and page callbacks.
login_history_perm Implements hook_perm().
login_history_user Implements hook_user().