uid) {
// Get the first result from the database, we can only assign one single uid.
$result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid");
if($row = db_fetch_array($result)) {
// we found an entry in the table, so load the user
drupal_load("module", "user");
$account = user_load(array('uid' => $row['uid']));
}
// Check if loading the user was successful.
if(isset($account) && $account) {
// Take the authenticated user role away.
unset($account->roles[DRUPAL_AUTHENTICATED_RID]);
$account->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
$user = $account;
// Regenerate the session ID to prevent against session fixation attacks.
sess_regenerate();
if(variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED && !isset($_GET['ipauth_no_cache'])) {
require_once './includes/common.inc';
// Reload the page, the query string ensures that there will be a page
// cache miss and thus a fresh generated page is served.
drupal_goto(isset($_GET['q']) ? $_GET['q'] : '', 'ipauth_no_cache=' . md5(time()));
}
}
}
elseif(in_array($user->uid, ipauth_get_uids())) {
// It's one of the special ip_auth users, take the authenticated user role away.
unset($user->roles[DRUPAL_AUTHENTICATED_RID]);
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
}
} // end function ipauth_init
/**
* Display help and module information
* @param section: which path of the site we're displaying help
* @return help text for the path
*/
function ipauth_help($section) {
$output = '';
switch ($section) {
case "admin/help#ip_authenticator":
$output = '
'. t("IP based role authenticator; it assigns roles based on IP address.") .'
';
break;
}
return $output;
} // end function ipauth_help
/**
* Valid permissions for this module
* @return array An array of valid permissions
*/
function ipauth_perm() {
return array(
'access ip_authenticator content',
'administer ip_authenticator'
);
} // end function ipauth_perm
/**
* insert the administration menu
*/
function ipauth_menu($may_cache) {
global $user;
$items = array();
$ipauth_uids = ipauth_get_uids();
$is_ipauth_user = in_array($user->uid, $ipauth_uids);
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/ip_authenticator',
'description' => t('IP based role authenticator; it assigns roles based on IP address.'),
'title' => 'IP Authenticator',
'callback' => 'drupal_get_form',
'callback arguments' => array('ipauth_admin_settings'),
'access' => user_access('administer ip_authenticator'),
'type' => MENU_NORMAL_ITEM
);
$items[] = array(
'path' => 'admin/user/ip_authenticator/edit',
'title' => 'IP Authenticator -- Modify',
'callback' => 'drupal_get_form',
'callback arguments' => array('ipauth_admin_edit'),
'access' => user_access('administer ip_authenticator'),
'type' => MENU_CALLBACK
);
$items[] = array(
'path' => 'admin/user/ip_authenticator/delete',
'title' => 'IP Authenticator -- Delete',
'callback' => 'drupal_get_form',
'callback arguments' => array('ipauth_admin_delete'),
'access' => user_access('administer ip_authenticator'),
'type' => MENU_CALLBACK
);
// Following items override the access settings of some menus defined by
// user.module
if($is_ipauth_user) {
$items[] = array('path' => 'user', 'title' => t('User account'),
'callback' => 'drupal_get_form', 'callback arguments' => array('ipauth_user_login'),
'access' => !$user->uid || $is_ipauth_user,
'type' => MENU_CALLBACK);
$items[] = array('path' => 'user/login', 'title' => t('Log in'),
'callback' => 'drupal_get_form', 'callback arguments' => array('ipauth_user_login'),
'access' => !$user->uid || $is_ipauth_user,
'type' => MENU_LOCAL_TASK);
$items[] = array('path' => 'user/register', 'title' => t('Create new account'),
'callback' => 'drupal_get_form', 'callback arguments' => array('ipauth_user_register'),
'access' => (!$user->uid || $is_ipauth_user) && variable_get('user_register', 1),
'type' => MENU_LOCAL_TASK);
$items[] = array('path' => 'user/password', 'title' => t('Request new password'),
'callback' => 'drupal_get_form', 'callback arguments' => array('user_pass'),
'access' => !$user->uid || $is_ipauth_user,
'type' => MENU_LOCAL_TASK);
// Your personal page
if ($user->uid) {
$items[] = array('path' => 'user/'. $user->uid, 'title' => t('My account'),
'callback' => 'user_view', 'callback arguments' => array(arg(1)), 'access' => !$is_ipauth_user,
'type' => MENU_DYNAMIC_ITEM);
}
$items[] = array('path' => 'logout', 'title' => t('Log out'),
'access' => $user->uid && !$is_ipauth_user,
'callback' => 'user_logout',
'weight' => 10);
}
}
else {
// Add the CSS and JS for this module. We put this in !$may_cache so it is
// only added once per request.
drupal_add_css(drupal_get_path("module", "ipauth") ."/css/ipauth.admin.css", "module", "all", FALSE);
drupal_add_js(drupal_get_path("module", "ipauth") ."/js/ipauth.admin.js");
if ($is_ipauth_user && (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 0)) {
$account = user_load(array('uid' => arg(1)));
if ($user !== FALSE) {
$view_access = user_access('access user profiles');
$admin_access = user_access('administer users');
// Only admins can view blocked accounts
$view_access &= $account->status || $admin_access;
$items[] = array('path' => 'user/'. arg(1), 'title' => t('User'),
'type' => MENU_CALLBACK | MENU_CREATED_BY_ADMIN, 'callback' => 'user_view',
'callback arguments' => array(arg(1)), 'access' => $view_access);
$items[] = array('path' => 'user/'. arg(1) .'/view', 'title' => t('View'),
'access' => $view_access, 'type' => MENU_DEFAULT_LOCAL_TASK | MENU_CREATED_BY_ADMIN, 'weight' => -10);
$items[] = array('path' => 'user/'. arg(1) .'/edit', 'title' => t('Edit'),
'callback' => 'drupal_get_form', 'callback arguments' => array('user_edit'),
'access' => $admin_access, 'type' => MENU_LOCAL_TASK | MENU_CREATED_BY_ADMIN);
$items[] = array('path' => 'user/'. arg(1) .'/delete', 'title' => t('Delete'),
'callback' => 'user_edit', 'access' => $admin_access,
'type' => MENU_CALLBACK | MENU_CREATED_BY_ADMIN);
if (arg(2) == 'edit') {
if (($categories = _user_categories($account)) && (count($categories) > 1)) {
foreach ($categories as $key => $category) {
$items[] = array(
'path' => 'user/'. arg(1) .'/edit/'. $category['name'],
'title' => $category['title'],
'type' => $category['name'] == 'account' ? MENU_DEFAULT_LOCAL_TASK | MENU_CREATED_BY_ADMIN: MENU_LOCAL_TASK | MENU_CREATED_BY_ADMIN,
'weight' => $category['weight'],
'access' => $admin_access);
}
}
}
}
}
}
return $items;
} //end function ipauth_menu
/*******
* FORMS
*/
/**
* administer the ip_authenticator parameters.
*/
function ipauth_admin_settings() {
global $user;
$form['ip_list'] = array(
'#type' => 'fieldset',
'#title' => t('Current IP Authenticators'),
'#description' => t("Click on an ip range to modify the authenticator assignment. Click on a user name to edit the account. Your IP: !s", array('!s' => $_SERVER['REMOTE_ADDR'])),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#weight' => 0
);
$result = ipauth_get_ip_authenticators(null, "id, uid, ip1, ip2");
while ($row = db_fetch_object($result)) {
//this takes the mysql command, INET_NTOA out of the query string.
$row->ip1 = long2ip($row->ip1);
$row->ip2 = ($row->ip2 == 0) ? "" : long2ip($row->ip2);
$ip2_text = $row->ip2 ? " - ". $row->ip2 : "";
$account = user_load(array('uid' => $row->uid));
$ip_title = $row->ip1 . $ip2_text;
$user_title = $account->name;
$form['ip_list'][$row->id] = array(
'#prefix' => '
'
);
$form['id'] = array(
'#type' => 'hidden',
'#value' => $args[0]
);
$form['#validate']['ipauth_admin_delete_validate'] = array();
$form['#submit']['ipauth_admin_delete_submit'] = array();
$form['#redirect'] = 'admin/user/ip_authenticator';
return $form;
} // end function ipauth_admin_delete
/**
* Validate our deletion form
*/
function ipauth_admin_delete_validate($form_id, $form_values) {
if (!preg_match("/^[0-9]+\$/", $form_values["id"]))
form_set_error("id", "Please return to the ". l("admin/user/ip_authenticator", "ip_authenticator") ." page and then navigate back here. A form value was lost.");
} // end function ipauth_admin_delete_validate
/**
* Submit our deletion form
*/
function ipauth_admin_delete_submit($form_id, $form_values) {
$result = db_query("DELETE FROM {ip_authenticator} WHERE id = '%s'", $form_values['id']);
if (db_affected_rows() < 1) {
drupal_set_message("Error in execution of sql statement -- no changes made.");
}
else {
drupal_set_message("The IP Authenticator item has been deleted.");
}
drupal_goto("admin/user/ip_authenticator");
} // end function ipauth_admin_delete_submit
/**
* Implementation of hook_block().
*/
function ipauth_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
if ($op == 'list') {
$blocks[0]['info'] = t('IPAuthenticator User login');
return $blocks;
}
else if ($op == 'view') {
$block = array();
switch ($delta) {
case 0:
// For usability's sake, avoid showing two login forms on one page.
// In contrast to the user.module login block this block is shown when
// an ip_auth authenticated user is online.
if (in_array($user->uid, ipauth_get_uids()) && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
$block['subject'] = t('User login');
$block['content'] = drupal_get_form('user_login_block');
}
return $block;
}
}
}
function ipauth_user_login() {
global $user;
$uid = $user->uid;
$user->uid = 0;
$form = user_login();
$user->uid = $uid;
return $form;
}
function ipauth_user_register() {
global $user;
$uid = $user->uid;
$user->uid = 0;
$form = user_register();
$user->uid = $uid;
return $form;
}
/*********
* HELPERS
*/
/**
* Pull ip info when given an entry id
* @param entry id
* @return IPs
*/
function ipauth_get_ip_uid_info($id) {
$result = db_query("SELECT uid, ip1, ip2 FROM {ip_authenticator} WHERE id = '%s'", $id);
$row = db_fetch_array($result);
$row["ip1"] = long2ip($row["ip1"]);
$row["ip2"] = ($row["ip2"] == 0) ? "" : long2ip($row["ip2"]);
return $row;
} // end function ipauth_get_ip_role_info
/**
* Queries the database to see if any IP based role changes are found.
* @return $result - a database result set.
*/
function ipauth_get_ip_authenticators($ip = "", $return_fields = "uid") {
$sql_where_clause = ("ALL" == $ip || "all" == $ip || "*" == $ip || "" == $ip) ? "1" : "(ip1 = '%s') OR (ip1 <= '%s' AND ip2 >= '%s')";
$sql = "SELECT ". $return_fields .", id FROM {ip_authenticator} WHERE ". $sql_where_clause;
// use the php functions and not the mysql function INET_ATON and INET_NTOA functions. This will provide greater database functionality.
$long_ip = sprintf("%u", ip2long($ip));
return db_query($sql, $long_ip, $long_ip, $long_ip);
} // end function ipauth_get_ip_authenticators
function ipauth_get_uids() {
static $uids;
if(!isset($uids)) {
$sql = "SELECT DISTINCT uid FROM {ip_authenticator} WHERE 1";
$result = db_query($sql);
while($row = db_fetch_array($result)) {
$uids[] = $row['uid'];
}
}
return $uids;
}