View source
<?php
function login_history_user($op, &$edit, &$account) {
if ($op == '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;
}
$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);
}
}
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;
}
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');
}
function login_history_block($op = 'list', $delta = '', $edit = array()) {
switch ($op) {
case 'list':
$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;
}
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;
}
}
}
}
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);
}