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' => '
', '#suffix' => '
', '#value' => l($ip_title, "admin/user/ip_authenticator/edit/". $row->id)." authenticates to: ".l($user_title, "user/". $row->uid. "/edit") ); } $form['ips'] = array( '#type' => 'fieldset', '#title' => t('IP address assignment') ."
", '#collapsible' => FALSE, '#collapsed' => FALSE, '#weight' => 0 ); $form['ips']['ip1'] = array( '#prefix' => '
', '#type' => 'textfield', '#title' => 'IP 1', '#size' => '15', '#required' => TRUE, '#suffix' => '
   -   
' ); $form['ips']['ip2'] = array( '#prefix' => '
', '#type' => 'textfield', '#title' => 'IP 2', '#description' => 'Leave blank for
individual IPs', '#size' => '15', '#suffix' => '
       
', ); $form['ips']['username'] = array( '#prefix' => '
', '#type' => 'textfield', '#maxlength' => 60, '#title' => t('Assign to User'), '#autocomplete_path' => 'user/autocomplete', '#description' => "Select a user to assign the ips", '#suffix' => '
', ); $form['#validate']['ipauth_admin_settings_validate'] = array(); $form['#submit']['ipauth_admin_settings_submit'] = array(); return system_settings_form($form); } // end function ipauth_admin_settings /** * Validate our admin settings */ function ipauth_admin_settings_validate($form_id, $form_values) { if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", $form_values['ip1'])) { form_set_error('ip1', "You must enter an ip address in the first field."); } if ((trim($form_values['ip2']) != "") && (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_values['ip2'])))) { form_set_error('ip2', "You must enter a valid IP address in the second field, or leave the second field blank."); } } //end function ipauth_admin_settings_validate /** * Submit our admin settings */ function ipauth_admin_settings_submit($form_id, $form_values) { if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_values['ip2']))) { $ip2 = trim($form_values["ip2"]); } else { $ip2 = sprintf("%u", ip2long(trim($form_values['ip2']))); } $sql = "INSERT INTO {ip_authenticator} (ip1, ip2, uid) VALUES ('%s','%s','%d')"; $account = user_load(array('name' => $form_values['username'])); db_query($sql, sprintf("%u", ip2long(trim($form_values['ip1']))), $ip2, $account->uid); if (db_affected_rows() == 0) { drupal_set_message(mysql_error(), 'error'); } } // end function ipauth_admin_settings_submit /** * Form for editing an authentication entry */ function ipauth_admin_edit() { $args = func_get_args(); if (!preg_match("/^[0-9]+\$/", $args[0])) { drupal_set_message("Please select your authenticator again"); drupal_goto('admin/user/ip_authenticator'); } $row = ipauth_get_ip_uid_info($args[0]); $account = user_load(array('uid' => $row['uid'])); $form['ips'] = array( '#type' => 'fieldset', '#title' => t('IP address assignment'), '#collapsible' => FALSE, '#collapsed' => FALSE, '#weight' => 0 ); $form['ips']['ip1'] = array( '#prefix' => '
', '#type' => 'textfield', '#title' => 'IP 1', '#size' => '15', '#default_value' => $row["ip1"], '#required' => TRUE, '#suffix' => '
   -   
' ); $form['ips']['ip2'] = array( '#prefix' => '
', '#type' => 'textfield', '#title' => 'IP 2', '#description' => 'Leave blank for
individual IPs', '#size' => '15', '#default_value' => $row["ip2"], '#suffix' => '
       
', ); $form['ips']['username'] = array( '#prefix' => '
', '#type' => 'textfield', '#maxlength' => 60, '#title' => t('Assign to User'), '#autocomplete_path' => 'user/autocomplete', '#default_value' => $account->name, '#description' => "Select a role to assign the ips", '#suffix' => '
', ); $form['id'] = array( '#type' => 'hidden', '#value' => $args[0], ); $form['#validate']['ipauth_admin_settings_validate'] = array(); $form['#submit']['ipauth_admin_edit_submit'] = array(); $form['#redirect'] = 'admin/user/ip_authenticator'; return system_settings_form($form); } // end function ipauth_admin_edit /** * Submit our edit form */ function ipauth_admin_edit_submit($form_id, $form_values) { if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_values['ip2'])) ) { $ip2_long = trim($form_values['ip2']); } else { $ip2_long = trim(sprintf("%u", ip2long($form_values['ip2']))); } $account = user_load(array('name' => $form_values['username'])); $sql = "UPDATE {ip_authenticator} SET ip1 = '%s', ip2 = '%s', uid = '%s' WHERE id = '%s'"; db_query($sql, sprintf("%u", ip2long(trim($form_values['ip1']))), $ip2_long, $account->uid, $form_values['id']); if (db_affected_rows() == 0) { drupal_set_message(mysql_error(), 'error'); } } // end function ipauth_admin_edit_submit /** * Our deletion function */ function ipauth_admin_delete() { $args = func_get_args(); if (!preg_match("/^[0-9]+\$/", $args[0])) { drupal_set_message("Please select your authenticator again"); drupal_goto("admin/user/ip_authenticator"); } $row = ipauth_get_ip_uid_info($args[0]); $account = user_load(array('uid' => $row['uid'])); $ip2_text = ($row["ip2"] != "") ? " - ". $row["ip2"] : ""; $title = "
  • " . $row['ip1'] . $ip2_text ." authenticates to: ". $account->name; $form["markup"] = array( '#prefix' => "
    ", '#suffix' => "", '#value' => "Are you certain that you wish to delete this ip authenticator? " ); $form["accept"] = array( '#type' => 'submit', '#value' => 'Delete', '#prefix' => '
    ', '#suffix' => '
           
    ', ); $form['cancel'] = array( '#prefix' => '
    ', '#value' => '', '#suffix' => '
    ' ); $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; }