View source
<?php
function ip_login_init() {
global $user;
if (!$user->uid && variable_get('ip_login_enabled', 1) && !$_SESSION['ip_login_complete']) {
ip_login_login($_SERVER['REMOTE_ADDR']);
}
}
function ip_login_menu() {
$items['admin/settings/iplogin'] = array(
'title' => t('IP Login Settings'),
'description' => t('configure IP login settings'),
'access callback' => 'user_access',
'access arguments' => array(
'administer site configuration',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ip_login_admin_settings',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function ip_login_admin_settings() {
$result = db_query("SELECT DISTINCT name FROM {profile_fields}");
while ($field = db_fetch_object($result)) {
$fields[$field->name] = $field->name;
}
if (!$fields || !variable_get('ip_login_profile_ip_field', '')) {
drupal_set_message(t('You can use the !profile_module to add a text field to the user profile to store the user ip address.', array(
'!profile_module' => l('profile module admin', 'admin/user/profile/add/textfield'),
)));
}
$form['info'] = array(
'#type' => 'markup',
'#value' => 'This module allows users to login automatically by IP address. The IP address value stored in a user\'s profile, using the \'profile\' module, is used to authenticate and login that user when visiting from that address. Addresses like 203.123.456.789 and also ranges like 203.123.456.* are supported.',
);
$form['ip_login_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('IP Login enabled'),
'#description' => t('Check to enable IP Login, uncheck to disable. If unchecked no user will be able to login via IP address.'),
'#default_value' => variable_get('ip_login_enabled', 1),
);
$form['ip_login_profile_ip_field'] = array(
'#type' => 'select',
'#title' => t('Profile module IP address field'),
'#description' => t('Select the profile field containing the user IP address.'),
'#options' => $fields,
'#default_value' => variable_get('ip_login_profile_ip_field', 'profile_ip'),
);
return system_settings_form($form);
}
function ip_login_login($ip) {
$ip_range = ip_login_get_range($ip);
$profile_field = variable_get('ip_login_profile_ip_field', 'profile_ip');
$result = db_fetch_object(db_query("SELECT pv.uid, pv.value FROM {profile_values} pv\n INNER JOIN {profile_fields} pf ON pv.fid = pf.fid\n WHERE pf.name = '%s' AND (pv.value = '%s' OR pv.value = '%s')", $profile_field, $ip, $ip_range));
if ($result && function_exists("user_load")) {
if ($account = user_load(array(
'uid' => $result->uid,
'status' => 1,
))) {
global $user;
$user = $account;
$message = t('Welcome %name. You are now logged into %sitename.', array(
'%name' => $user->name,
'%sitename' => variable_get('site_name', 'this website'),
));
drupal_set_message($message);
$message = t('Session opened for %name.', array(
'%name' => $user->name,
));
watchdog('user', $message);
db_query("UPDATE {users} SET login = %d WHERE uid = %d", time(), $user->uid);
sess_regenerate();
}
$_SESSION['ip_login_complete'] = 1;
}
else {
return false;
}
}
function ip_login_get_range($ip) {
if ($ip) {
$ip = explode('.', $ip);
array_pop($ip);
$ip = implode('.', $ip) . '.*';
}
return $ip;
}