| 1 |
<?php |
<?php |
| 2 |
// $Id: ipauth.module,v 1.6 2008/10/17 14:57:42 jonfrancisskydiver Exp $ |
// $Id$ |
| 3 |
// Modified 2008/09/15 antonio.spadial |
/** |
| 4 |
// Modified 2008/10/17 jonfrancisskydiver |
* @file |
| 5 |
|
* Module code for an IP based authenticator |
| 6 |
/*********************************************************************************** |
* @author C/S Group - Jonathan T. Francis |
| 7 |
* C H A N G E S |
*/ |
| 8 |
* 1. 'global $user;' is not used in function get_ip_authentication(...) |
|
| 9 |
* 2. Switch <= and >= in $sql_where_clause in function get_ip_authentication(...) |
/******* |
| 10 |
* |
* HOOKS |
| 11 |
***********************************************************************************/ |
*/ |
| 12 |
|
|
| 13 |
/** |
/** |
| 14 |
* Detect which roles to apply based on the viewer's IP address |
* Detect which uid to apply based on the viewer's IP address |
| 15 |
*/ |
*/ |
| 16 |
function ipauth_init() { |
function ipauth_boot() { |
| 17 |
global $user; |
global $user; |
|
|
|
|
// Get all of the roles |
|
|
$roles = user_roles(); |
|
| 18 |
|
|
| 19 |
// Get the list of roles to assign based off of the IP |
// Also check ip auth users to see if they have changed the network so that |
| 20 |
$result = _get_ip_authenticators($_SERVER['REMOTE_ADDR'], "roles"); |
// a new uid has to be assigned. |
| 21 |
// Loop through the results and apply the new roles. |
if (!$user->uid || in_array($user->uid, ipauth_get_uids())) { |
| 22 |
while ( $row = db_fetch_array($result) ) { |
|
| 23 |
$user->roles[$row['roles']] = $roles[$row['roles']]; |
// Get the first result from the database, we can only assign one single uid. |
| 24 |
} |
$result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid"); |
| 25 |
|
|
| 26 |
|
if ($row = db_fetch_array($result)) { |
| 27 |
|
// we found an entry in the table, so load the user |
| 28 |
|
drupal_load("module", "user"); |
| 29 |
|
$account = user_load(array('uid' => $row['uid'])); |
| 30 |
|
} |
| 31 |
|
else { |
| 32 |
|
$account = drupal_anonymous_user(); |
| 33 |
|
} |
| 34 |
|
// Check if loading the user was successful and only execute the following |
| 35 |
|
// code if the user has changed |
| 36 |
|
if (isset($account) && $account && $account->uid != $user->uid) { |
| 37 |
|
$user = $account; |
| 38 |
|
|
| 39 |
|
// Regenerate the session ID to prevent against session fixation attacks. |
| 40 |
|
sess_regenerate(); |
| 41 |
|
|
| 42 |
} // end function ipauth_init |
if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED && !isset($_GET['ipauth_no_cache'])) { |
| 43 |
|
// Reload the page, the query string ensures that there will be a page |
| 44 |
|
// cache miss and thus a fresh generated page is served. |
| 45 |
|
|
| 46 |
|
$url = url($_GET["q"], 'ipauth_no_cache='.md5(time()), NULL, TRUE); |
| 47 |
|
|
| 48 |
|
// Remove newlines from the URL to avoid header injection attacks. |
| 49 |
|
$url = str_replace(array("\n", "\r"), '', $url); |
| 50 |
|
|
| 51 |
|
// Before the redirect, allow modules to react to the end of the page request. |
| 52 |
|
module_invoke_all('exit', $url); |
| 53 |
|
|
| 54 |
|
// Even though session_write_close() is registered as a shutdown function, we |
| 55 |
|
// need all session data written to the database before redirecting. |
| 56 |
|
session_write_close(); |
| 57 |
|
|
| 58 |
|
header('Location: '. $url, TRUE, 302); |
| 59 |
|
exit(); |
| 60 |
|
} |
| 61 |
|
} |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
if (in_array($user->uid, ipauth_get_uids())) { |
| 65 |
|
// It's one of the special ip_auth users, take the authenticated user role away. |
| 66 |
|
unset($user->roles[DRUPAL_AUTHENTICATED_RID]); |
| 67 |
|
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; |
| 68 |
|
|
| 69 |
|
if (!_ipauth_path_allowed()) { |
| 70 |
|
drupal_access_denied(); |
| 71 |
|
// Allow modules to react to the end of the page request. |
| 72 |
|
module_invoke_all('exit'); |
| 73 |
|
exit(); |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
// User.module will redirect logged in users visiting the "/user" page to |
| 77 |
|
// "/user/$user->uid". We don't want that to happen for ip auth users. |
| 78 |
|
if ($_GET['q'] == 'user') { |
| 79 |
|
$_GET['q'] = 'user/login'; |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
// Logout if password reset page is requested |
| 83 |
|
if (substr($_GET['q'], 0, 11) == 'user/reset/') { |
| 84 |
|
$user = drupal_anonymous_user(); |
| 85 |
|
} |
| 86 |
|
} |
| 87 |
|
} // end function hook_boot |
| 88 |
|
|
| 89 |
/** |
/** |
| 90 |
* Display help and module information |
* Display help and module information |
| 91 |
* @param path which path of the site we're displaying help |
* @param section: which path of the site we're displaying help |
|
* @param arg array that holds the current path as would be returned from arg() function |
|
| 92 |
* @return help text for the path |
* @return help text for the path |
| 93 |
*/ |
*/ |
| 94 |
function ipauth_help($path, $arg) { |
function ipauth_help($path, $arg) { |
| 95 |
$output = ''; |
$output = ''; |
| 96 |
switch ($path) { |
switch ($path) { |
| 97 |
case "admin/help#ip_authenticator": |
case "admin/help#ip_authenticator": |
| 98 |
$output = '<p>'. t("IP based role authenticator; it assigns roles based on IP address.") .'</p>'; |
$output = '<p>'. t("IP based role authenticator; it assigns user account based on IP address.") .'</p>'; |
| 99 |
break; |
break; |
| 100 |
} |
} |
| 101 |
return $output; |
return $output; |
| 102 |
} // function ipauth_help |
} // function ipauth_help |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
/** |
/** |
| 108 |
* Valid permissions for this module |
* Valid permissions for this module |
| 109 |
* @return array An array of valid permissions for the weatherfacti module |
* @return array An array of valid permissions for the weatherfacti module |
| 110 |
*/ |
*/ |
|
|
|
| 111 |
function ipauth_perm() { |
function ipauth_perm() { |
| 112 |
return array( |
return array( |
| 113 |
'access ip_authenticator content', |
'access ip_authenticator content', |
| 114 |
'administer ip_authenticator'); |
'administer ip_authenticator'); |
| 115 |
} // end function ipauth_perm |
} // end function ipauth_perm |
| 116 |
|
|
|
|
|
|
|
|
|
|
|
| 117 |
/** |
/** |
| 118 |
* Insert the administration menu |
* insert the administration menu |
| 119 |
*/ |
*/ |
| 120 |
function ipauth_menu() { |
function ipauth_menu() { |
| 121 |
|
|
| 122 |
$items = array(); |
$items = array(); |
| 123 |
|
|
| 124 |
$items['admin/user/ip_authenticator'] = array( |
$items['admin/user/ip_authenticator'] = array( |
| 125 |
'title' => 'IP Authenticator', |
'title' => 'IP Authenticator', |
| 126 |
'description' => 'Assigns an IP address a role.', |
'description' => 'Assigns an IP address a user account.', |
| 127 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 128 |
'page arguments' => array('ipauth_admin'), |
'page arguments' => array('ipauth_admin_settings'), |
| 129 |
'access callback' => 'user_access', |
'access callback' => 'user_access', |
| 130 |
'access arguments' => Array('administer ip_authenticator'), |
'access arguments' => Array('administer ip_authenticator'), |
| 131 |
'file' => 'ipauth.admin.inc', |
'file' => 'ipauth.admin.inc', |
|
'file path' => drupal_get_path('module', 'ip_authenticator'), |
|
| 132 |
'type' => MENU_NORMAL_ITEM |
'type' => MENU_NORMAL_ITEM |
| 133 |
); |
); |
|
|
|
| 134 |
$items['admin/user/ip_authenticator/edit'] = array( |
$items['admin/user/ip_authenticator/edit'] = array( |
| 135 |
'title' => 'IP Authenticator -- Modify', |
'title' => t('IP Authenticator -- Modify'), |
| 136 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 137 |
'page arguments' => array('ipauth_admin_edit', 1), |
'page arguments' => array('ipauth_admin_edit'), |
| 138 |
'access callback' => 'user_access', |
'access callback' => 'user_access', |
| 139 |
'access arguments' => Array('administer ip_authenticator'), |
'access arguments' => Array('administer ip_authenticator'), |
| 140 |
'file' => 'ipauth.admin.inc', |
'file' => 'ipauth.admin.inc', |
|
'file path' => drupal_get_path('module', 'ip_authenticator'), |
|
| 141 |
'type' => MENU_CALLBACK |
'type' => MENU_CALLBACK |
| 142 |
); |
); |
|
|
|
| 143 |
$items['admin/user/ip_authenticator/delete'] = array( |
$items['admin/user/ip_authenticator/delete'] = array( |
| 144 |
'title' => 'IP Authenticator -- Delete', |
'title' => t('IP Authenticator -- Delete'), |
| 145 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 146 |
'page arguments' => array('ipauth_admin_delete', 1), |
'page arguments' => array('ipauth_admin_delete'), |
| 147 |
|
'access callback' => 'user_access', |
| 148 |
|
'access arguments' => Array('administer ip_authenticator'), |
| 149 |
|
'file' => 'ipauth.admin.inc', |
| 150 |
|
'type' => MENU_CALLBACK |
| 151 |
|
); |
| 152 |
|
$items['admin/user/ip_authenticator/data'] = array( |
| 153 |
|
'title' => t('IP Authenticator -- Import/Export'), |
| 154 |
|
'page callback' => 'drupal_get_form', |
| 155 |
|
'page arguments' => array('ipauth_import_export'), |
| 156 |
'access callback' => 'user_access', |
'access callback' => 'user_access', |
| 157 |
'access arguments' => Array('administer ip_authenticator'), |
'access arguments' => Array('administer ip_authenticator'), |
| 158 |
'file' => 'ipauth.admin.inc', |
'file' => 'ipauth.admin.inc', |
|
'file path' => drupal_get_path('module', 'ip_authenticator'), |
|
| 159 |
'type' => MENU_CALLBACK |
'type' => MENU_CALLBACK |
| 160 |
); |
); |
| 161 |
|
$items['admin/user/ip_authenticator/data/export'] = array( |
| 162 |
|
'title' => t('IP Authenticator -- Import/Export'), |
| 163 |
|
'page callback' => 'ipauth_export', |
| 164 |
|
'page arguments' => array('all'), |
| 165 |
|
'access callback' => 'user_access', |
| 166 |
|
'access arguments' => Array('administer ip_authenticator'), |
| 167 |
|
'file' => 'ipauth.admin.inc', |
| 168 |
|
'type' => MENU_CALLBACK |
| 169 |
|
); |
| 170 |
|
$items['admin/user/ip_authenticator/data/import'] = array( |
| 171 |
|
'title' => t('IP Authenticator -- Import'), |
| 172 |
|
'page callback' => 'ipauth_import', |
| 173 |
|
'page arguments' => array('all'), |
| 174 |
|
'access callback' => 'user_access', |
| 175 |
|
'access arguments' => Array('administer ip_authenticator'), |
| 176 |
|
'file' => 'ipauth.admin.inc', |
| 177 |
|
'type' => MENU_CALLBACK |
| 178 |
|
); |
| 179 |
|
// Following items override the access settings of some menus defined by |
| 180 |
|
// user.module. The access callback and arguments needed to be altered |
| 181 |
|
// to take into account the IP Authentication |
| 182 |
|
$items['user'] = array( |
| 183 |
|
'title' => 'User account', |
| 184 |
|
'page callback' => 'ipauth_user_page', |
| 185 |
|
'access callback' => 'is_ipauth_user', |
| 186 |
|
'access arguments' => array(FALSE, TRUE, TRUE), |
| 187 |
|
'type' => MENU_CALLBACK, |
| 188 |
|
'file' => '../../../../modules/user/user.pages.inc', |
| 189 |
|
); |
| 190 |
|
|
| 191 |
|
$items['user/login'] = array( |
| 192 |
|
'title' => 'Log in', |
| 193 |
|
'access callback' => 'is_ipauth_user', |
| 194 |
|
'access arguments' => array(FALSE, TRUE, TRUE), |
| 195 |
|
'page callback' => 'ipauth_user_page', |
| 196 |
|
'type' => MENU_DEFAULT_LOCAL_TASK, |
| 197 |
|
); |
| 198 |
|
|
| 199 |
|
$items['user/register'] = array( |
| 200 |
|
'title' => 'Create new account', |
| 201 |
|
'page callback' => 'drupal_get_form', |
| 202 |
|
'page arguments' => array('ipauth_user_register'), |
| 203 |
|
'access callback' => 'is_ipauth_user', |
| 204 |
|
'access arguments' => array(FALSE, variable_get('user_register', 1), TRUE), |
| 205 |
|
'type' => MENU_LOCAL_TASK, |
| 206 |
|
'file' => '../../../../modules/user/user.pages.inc', |
| 207 |
|
); |
| 208 |
|
$items['user/password'] = array( |
| 209 |
|
'title' => 'Request new password', |
| 210 |
|
'page callback' => 'drupal_get_form', |
| 211 |
|
'page arguments' => array('user_pass'), |
| 212 |
|
'access callback' => 'is_ipauth_user', |
| 213 |
|
'access arguments' => array(FALSE, TRUE, TRUE), |
| 214 |
|
'type' => MENU_LOCAL_TASK, |
| 215 |
|
'file' => '../../../../modules/user/user.pages.inc', |
| 216 |
|
); |
| 217 |
|
|
| 218 |
|
// Your personal page |
| 219 |
|
$items['user/%user_uid_optional'] = array( |
| 220 |
|
'title' => 'My account', |
| 221 |
|
'title callback' => 'user_page_title', |
| 222 |
|
'title arguments' => array(1), |
| 223 |
|
'page callback' => 'user_view', |
| 224 |
|
'page arguments' => array(1), |
| 225 |
|
'access callback' => 'display_logout', |
| 226 |
|
'parent' => '', |
| 227 |
|
'file' => '../../../../modules/user/user.pages.inc', |
| 228 |
|
); |
| 229 |
|
|
| 230 |
|
$items['user/%user/view'] = array( |
| 231 |
|
'title' => 'View', |
| 232 |
|
'type' => MENU_DEFAULT_LOCAL_TASK, |
| 233 |
|
'weight' => -10, |
| 234 |
|
); |
| 235 |
|
|
| 236 |
|
$items['logout'] = array( |
| 237 |
|
'title' => 'Log out', |
| 238 |
|
'page callback' => 'user_logout', |
| 239 |
|
'access callback' => 'display_logout', |
| 240 |
|
'weight' => 10, |
| 241 |
|
'file' => '../../../../modules/user/user.pages.inc', |
| 242 |
|
'type' => MENU_DYNAMIC_ITEM |
| 243 |
|
); |
| 244 |
|
|
| 245 |
return $items; |
return $items; |
| 246 |
} //end function ipauth_menu |
} //end function ipauth_menu |
| 247 |
|
|
| 248 |
|
/** |
| 249 |
|
* Check whether the path is allowed for ip auth users. |
| 250 |
|
* @return boolean Is the requested path allowed? |
| 251 |
|
*/ |
| 252 |
|
function _ipauth_path_allowed() { |
| 253 |
|
$path = $_GET['q']; |
| 254 |
|
$args = explode('/', $path); |
| 255 |
|
// Deny access to all "/user" pages of the currently logged in ipauth user |
| 256 |
|
// except if he is an admin. |
| 257 |
|
return !( ($args[0] == 'user' && $args[1] == $GLOBALS['user']->uid) && !user_access('administer users') ); |
| 258 |
|
} |
| 259 |
|
|
| 260 |
|
|
| 261 |
|
/******* |
| 262 |
|
* FORMS |
| 263 |
|
*/ |
| 264 |
|
|
| 265 |
|
function ipauth_import_export($form_state) { |
| 266 |
|
$form['#attributes']['enctype'] = 'multipart/form-data'; |
| 267 |
|
|
| 268 |
|
$form['export_fieldset'] = array( |
| 269 |
|
'#type' => 'fieldset', |
| 270 |
|
'#title' => t('Export IP Authenticators to a CVS file'), |
| 271 |
|
'#description' => t('This will cause you to download a CVS file containing all IP Authenticators.'), |
| 272 |
|
'#weight' => 1, |
| 273 |
|
); |
| 274 |
|
$form['export_fieldset']['export'] = array( |
| 275 |
|
'#value' => l('Export', 'admin/user/ip_authenticator/data/export'), |
| 276 |
|
'#weight' => 1 |
| 277 |
|
); |
| 278 |
|
$form['import_fieldset'] = array( |
| 279 |
|
'#type' => 'fieldset', |
| 280 |
|
'#title' => t('Import IP Authenticators from a CVS file'), |
| 281 |
|
'#description' => t('This import function simply appends authenticators. The feature doesn\'t care about duplicate entries.'). '<br>' . |
| 282 |
|
t('The format of the CVS file is as follows: '), |
| 283 |
|
'#weight' => 0 |
| 284 |
|
); |
| 285 |
|
$form['import_fieldset']['import'] = array( |
| 286 |
|
'#type' => 'file', |
| 287 |
|
'#title' => t('Upload the CVS file'), |
| 288 |
|
'#size' => 48, |
| 289 |
|
'#description' => t('This will import all the IP Authenticator\'s in the CVS file to the database.') |
| 290 |
|
); |
| 291 |
|
$form['import_fieldset']['submit'] = array( |
| 292 |
|
'#type' => 'submit', |
| 293 |
|
'#value' => 'Import' |
| 294 |
|
); |
| 295 |
|
|
| 296 |
|
$form['#redirect'] = 'admin/user/ip_authenticator'; |
| 297 |
|
|
| 298 |
|
return $form; |
| 299 |
|
} |
| 300 |
|
|
| 301 |
|
function _ipauth_strip_quotes(&$item, $key) { |
| 302 |
|
$item = trim($item, "\""); |
| 303 |
|
} |
| 304 |
|
|
| 305 |
|
function _ipauth_check_import($row) { |
| 306 |
|
if ($row == array()) { |
| 307 |
|
return TRUE; |
| 308 |
|
} |
| 309 |
|
|
| 310 |
|
/* |
| 311 |
|
if (preg_match("/^[0-9]+$/", $row["id"]) && |
| 312 |
|
preg_match("/^[0-9]+$/", $row["uid"]) && |
| 313 |
|
preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", $row["ip1"]) && |
| 314 |
|
(strlen($row["ip2"])>0 && preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", $row["ip2"]) ) && |
| 315 |
|
preg_match("/[a-zA-Z0-9 \-\'\"\&\]/") |
| 316 |
|
) { |
| 317 |
|
return TRUE; |
| 318 |
|
} |
| 319 |
|
*/ |
| 320 |
|
return TRUE; |
| 321 |
|
} |
| 322 |
|
|
| 323 |
|
function ipauth_import_export_submit($form_id, &$form_state) { |
| 324 |
|
|
| 325 |
|
//define your limits for the submission here |
| 326 |
|
$limits = array ( 'extensions' => 'cvs' ); |
| 327 |
|
|
| 328 |
|
$validators = array( |
| 329 |
|
'file_validate_extensions' => array($limits['extensions']), |
| 330 |
|
); |
| 331 |
|
|
| 332 |
|
// Save new file uploads. |
| 333 |
|
if ($file = file_save_upload('import', $validators, file_directory_path())) { |
| 334 |
|
$cvs_file = file($file->filepath); |
| 335 |
|
//id, uid, ip1, ip2, description, enabled, UNIX_TIMESTAMP(created) as created |
| 336 |
|
foreach ($cvs_file AS $cvs_file_line) { |
| 337 |
|
list($row["id"], $row["uid"], $row["ip1"], $row["ip2"], $row["description"], $row["enabled"], $row["created"]) = split(",", $cvs_file_line); |
| 338 |
|
array_walk($row, '_ipauth_strip_quotes'); |
| 339 |
|
if (_ipauth_check_import($row)) { |
| 340 |
|
$sql = "INSERT INTO {ip_authenticator} (uid, ip1, ip2, description, enabled, created) VALUES ('%s','%s','%s','%s', '%d', '%s')"; |
| 341 |
|
db_query($sql, $row["uid"], $row["ip1"], $row["ip2"], $row["description"], $row["enabled"], $row["created"]); |
| 342 |
|
} |
| 343 |
|
} |
| 344 |
|
|
| 345 |
|
drupal_set_message("The CVS file as been successfully imported."); |
| 346 |
|
|
| 347 |
|
//need to remove the file: http://api.drupal.org/api/file/includes/file.inc/6 |
| 348 |
|
file_delete($file->filepath); |
| 349 |
|
|
| 350 |
|
} |
| 351 |
|
} |
| 352 |
|
|
| 353 |
|
/** |
| 354 |
|
* administer the ip_authenticator parameters. |
| 355 |
|
*/ |
| 356 |
|
function ipauth_admin_settings($form_state) { |
| 357 |
|
global $user; |
| 358 |
|
|
| 359 |
|
$form['ip_list'] = array( |
| 360 |
|
'#type' => 'fieldset', |
| 361 |
|
'#title' => t('Current IP Authenticators'), |
| 362 |
|
'#description' => "<b>". t("Instructions:") ."</b><br/> ". |
| 363 |
|
t(" - Click on an ip range to modify the authenticator assignment.") ."<br/> ". |
| 364 |
|
t(" - Click on a user name to edit the account.") ."<br/> ". |
| 365 |
|
t(" - The title fields are sortable.") ."<br/><br/>". |
| 366 |
|
"<b>". t("Your IP: @s", array('@s' => $_SERVER['REMOTE_ADDR'])) ."</b>". |
| 367 |
|
'<br /><br/>'. |
| 368 |
|
t('Please note that users authenticated via IP authenticator do not get permissions of the <em>authenticated user</em> role.'), |
| 369 |
|
'#collapsible' => TRUE, |
| 370 |
|
'#collapsed' => FALSE, |
| 371 |
|
'#weight' => 0 |
| 372 |
|
); |
| 373 |
|
|
| 374 |
|
$form['ip_list']['import-export'] = array( |
| 375 |
|
'#value' => l("Import/Export", "admin/user/ip_authenticator/data"), |
| 376 |
|
'#prefix' => '<div id="import-export">', |
| 377 |
|
'#suffix' => '</div>', |
| 378 |
|
'#weight' => 10, |
| 379 |
|
); |
| 380 |
|
|
| 381 |
|
$non_blocked_users = array(); |
| 382 |
|
|
| 383 |
|
// Figure out which field to sort by. |
| 384 |
|
if (isset($_GET["sort_field"]) && preg_match("!^[a-zA-Z0-9_]+$!", $_GET["sort_field"])) { |
| 385 |
|
$sort_field = $_GET["sort_field"]; |
| 386 |
|
} else { |
| 387 |
|
$sort_field = "created"; |
| 388 |
|
} |
| 389 |
|
|
| 390 |
|
// How are we ordering the fields, Asc or Desc? |
| 391 |
|
if (isset($_GET["order"]) && preg_match("!^asc|desc$!", $_GET["order"])) |
| 392 |
|
$order = $_GET["order"]; |
| 393 |
|
else |
| 394 |
|
$order= "asc"; |
| 395 |
|
|
| 396 |
|
// Used to toggle between asc and desc. |
| 397 |
|
$order_toggle["asc"] = "desc"; |
| 398 |
|
$order_toggle["desc"] = "asc"; |
| 399 |
|
|
| 400 |
|
// Get all of the IP Authenticators in the specified order. |
| 401 |
|
$result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, DATE_FORMAT(created,'%m/%%d/%Y %l:%%i%p') as created", $sort_field, $order); |
| 402 |
|
|
| 403 |
|
//The theming function used to theme the table with the FAPI element is inspired from: |
| 404 |
|
// http://drupal.org/project/formtable. |
| 405 |
|
//That project currently only supports a Drupal 5 Module; That module has been ported |
| 406 |
|
//and incorporated into this ipauth module. |
| 407 |
|
$form['ip_list']['ipauth_table'] = array( |
| 408 |
|
'#type' => 'formtable', |
| 409 |
|
'#header' => array( |
| 410 |
|
l(t('Active'), $_GET["q"], array('query' => array('sort_field' => 'enabled', 'order' => $order_toggle[$order]))) . ipauth_arrow($sort_field, 'enabled', $order), |
| 411 |
|
l(t('IP Range'), $_GET["q"], array('query' => array('sort_field' => 'ip1', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'ip1', $order), |
| 412 |
|
l(t('User'), $_GET["q"], array('query' => array('sort_field' => 'uid', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'uid', $order), |
| 413 |
|
l(t('Description'), $_GET["q"], array('query' => array('sort_field' => 'description', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'description', $order), |
| 414 |
|
l(t('Last Updated'), $_GET["q"], array('query' => array('sort_field' => 'created', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'created', $order),) |
| 415 |
|
); |
| 416 |
|
|
| 417 |
|
|
| 418 |
|
$row = db_fetch_object($result); // Get the next IP Authenticator |
| 419 |
|
$evenodd_counter=0; // used for the table stripping (even and odd CSS classes). |
| 420 |
|
|
| 421 |
|
if ($row) { |
| 422 |
|
do { |
| 423 |
|
$evenodd_counter++; |
| 424 |
|
//this takes the mysql command, INET_NTOA out of the query string. |
| 425 |
|
$row->ip1 = long2ip($row->ip1); |
| 426 |
|
$row->ip2 = ($row->ip2 == 0) ? "" : long2ip($row->ip2); |
| 427 |
|
$ip2_text = $row->ip2 ? " - ". $row->ip2 : ""; |
| 428 |
|
$account = user_load(array('uid' => $row->uid)); |
| 429 |
|
$ip_title = $row->ip1 . $ip2_text; |
| 430 |
|
if ($account) { |
| 431 |
|
$user_title = l($account->name, "user/". $row->uid ."/edit"); |
| 432 |
|
} |
| 433 |
|
else { |
| 434 |
|
$user_title = t('The user account does not exist. Please delete this rule.'); |
| 435 |
|
} |
| 436 |
|
|
| 437 |
|
if (!empty($account) && !user_is_blocked($account->name) && !in_array($account->name, $non_blocked_users)) { |
| 438 |
|
$non_blocked_users[] = array('name' => $account->name, 'uid' => $account->uid); |
| 439 |
|
} |
| 440 |
|
|
| 441 |
|
$form['ip_list']['ipauth_table']['row_' .$row->id] = array( |
| 442 |
|
'#type' => 'formrow', |
| 443 |
|
'#attributes' => array('class' => ($evenodd_counter%2==1) ? "even" : "odd"), |
| 444 |
|
); |
| 445 |
|
|
| 446 |
|
$form['ip_list']['ipauth_table']['row_' .$row->id]['checkbox_' .$row->id] = array( |
| 447 |
|
'#prefix' => '<td>', |
| 448 |
|
'#suffix' => '</td>', |
| 449 |
|
'#type' => 'checkbox', |
| 450 |
|
'#default_value' => ($row->enabled == TRUE) ? 1 : 0, |
| 451 |
|
); |
| 452 |
|
|
| 453 |
|
$form['ip_list']['ipauth_table']['row_' .$row->id]['iprange'] = array( |
| 454 |
|
'#value' => l($ip_title, "admin/user/ip_authenticator/edit/". $row->id), |
| 455 |
|
'#prefix' => '<td>', |
| 456 |
|
'#suffix' => '</td>' |
| 457 |
|
); |
| 458 |
|
$form['ip_list']['ipauth_table']['row_' .$row->id]['user'] = array( |
| 459 |
|
'#value' => $user_title, |
| 460 |
|
'#prefix' => '<td>', |
| 461 |
|
'#suffix' => '</td>' |
| 462 |
|
); |
| 463 |
|
$form['ip_list']['ipauth_table']['row_' .$row->id]['description'] = array( |
| 464 |
|
'#value' => ($row->description=="") ? " " : $row->description, |
| 465 |
|
'#prefix' => '<td>', |
| 466 |
|
'#suffix' => '</td>' |
| 467 |
|
); |
| 468 |
|
$form['ip_list']['ipauth_table']['row_' .$row->id]['created'] = array( |
| 469 |
|
'#value' => $row->created, |
| 470 |
|
'#prefix' => '<td>', |
| 471 |
|
'#suffix' => '</td>' |
| 472 |
|
); |
| 473 |
|
} while($row = db_fetch_object($result));// END while ($row = db_fetch_object($result)) |
| 474 |
|
} else { |
| 475 |
|
$form['ip_list']['no_authenticators'] = array( |
| 476 |
|
'#value' => t("There are no IP Authenticators to list."), |
| 477 |
|
); |
| 478 |
|
} |
| 479 |
|
|
| 480 |
|
if (!empty($non_blocked_users)) { |
| 481 |
|
$error_text = t("The following users are not blocked but used by IP Authenticator. |
| 482 |
|
Please set their status to blocked on the users' settings pages |
| 483 |
|
and don't forget to clear the menu cache afterwards."); |
| 484 |
|
foreach ($non_blocked_users as $blocked_user) { |
| 485 |
|
$error_text .= '<br />'; |
| 486 |
|
$error_text .= l($blocked_user['name'], 'user/'. $blocked_user['uid'] .'/edit'); |
| 487 |
|
} |
| 488 |
|
drupal_set_message($error_text, 'error'); |
| 489 |
|
} |
| 490 |
|
|
| 491 |
|
$form['ips'] = array( |
| 492 |
|
'#type' => 'fieldset', |
| 493 |
|
'#title' => t('IP address assignment') ."<br/>", |
| 494 |
|
'#collapsible' => FALSE, |
| 495 |
|
'#collapsed' => FALSE, |
| 496 |
|
'#weight' => 0 |
| 497 |
|
); |
| 498 |
|
$form['ips']['checkbox_enabled'] = array( |
| 499 |
|
'#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>', |
| 500 |
|
'#suffix' => '</div>', |
| 501 |
|
'#type' => 'checkbox', |
| 502 |
|
'#description' => 'Enable/Disable', |
| 503 |
|
'#id' => 'enable_disable_checkbox', |
| 504 |
|
'#default_value' => '0', |
| 505 |
|
); |
| 506 |
|
$form['ips']['ip1'] = array( |
| 507 |
|
'#prefix' => '<div class="floatLeft">', |
| 508 |
|
'#type' => 'textfield', |
| 509 |
|
'#title' => 'IP 1', |
| 510 |
|
'#size' => '15', |
| 511 |
|
/* '#required' => TRUE, */ |
| 512 |
|
'#suffix' => '</div><div class="floatLeft" id="formSeperator"> - </div>' |
| 513 |
|
); |
| 514 |
|
|
| 515 |
|
$form['ips']['ip2'] = array( |
| 516 |
|
'#prefix' => '<div class="floatLeft">', |
| 517 |
|
'#type' => 'textfield', |
| 518 |
|
'#title' => 'IP 2', |
| 519 |
|
'#description' => t('Leave blank for<br/>individual IPs'), |
| 520 |
|
'#size' => '15', |
| 521 |
|
'#suffix' => '</div><div class="floatLeft" id="formSeperator"> </div>', |
| 522 |
|
); |
| 523 |
|
|
| 524 |
|
$form['ips']['username'] = array( |
| 525 |
|
'#prefix' => '<div class="floatLeft">', |
| 526 |
|
'#type' => 'textfield', |
| 527 |
|
'#maxlength' => 60, |
| 528 |
|
'#title' => t('Assign to User'), |
| 529 |
|
'#autocomplete_path' => 'user/autocomplete', |
| 530 |
|
'#description' => t("Select a user to assign the IPs to"), |
| 531 |
|
/* '#required' => TRUE,*/ |
| 532 |
|
'#suffix' => '</div>', |
| 533 |
|
); |
| 534 |
|
$form['ips']['ipauth_desc'] = array( |
| 535 |
|
'#prefix' => '<br style="clear:both" />', |
| 536 |
|
'#type' => 'textfield', |
| 537 |
|
'#title' => t('Description of Authenticator'), |
| 538 |
|
'#description' => t("Enter a description to better identify this authenticator"), |
| 539 |
|
); |
| 540 |
|
|
| 541 |
|
$form['#validate'][] = 'ipauth_admin_settings_validate'; |
| 542 |
|
$form['#submit'][] = 'ipauth_admin_settings_submit'; |
| 543 |
|
|
| 544 |
|
return system_settings_form($form); |
| 545 |
|
} // end function ipauth_admin_settings |
| 546 |
|
|
| 547 |
|
/** |
| 548 |
|
* Validate our admin settings |
| 549 |
|
*/ |
| 550 |
|
function ipauth_admin_settings_validate($form_id, &$form_state) { |
| 551 |
|
|
| 552 |
|
// IF IP1 contains a value but doesn't match the IP address format, then display an error. |
| 553 |
|
if (trim($form_state['values']['ip1']) != "" && !preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", $form_state['values']['ip1'])) { |
| 554 |
|
form_set_error('ip1', t("You must enter an ip address in the first field.")); |
| 555 |
|
} |
| 556 |
|
|
| 557 |
|
// If IP2 contains a value, but doesn't match an IP address, then display an error. |
| 558 |
|
if ((trim($form_state['values']['ip2']) != "") && (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_state['values']['ip2'])))) { |
| 559 |
|
form_set_error('ip2', t("You must enter a valid IP address in the second field, or leave the second field blank.")); |
| 560 |
|
} |
| 561 |
|
|
| 562 |
|
// if IP1 contains a value and the user name is blank or the user is not blocked, then |
| 563 |
|
if ( trim($form_state['values']['ip1']) != "" && (trim($form_state['values']['username']) == "" || !user_is_blocked($form_state['values']['username']))) { |
| 564 |
|
|
| 565 |
|
if (trim($form_state['values']['username']) != "") { |
| 566 |
|
$account = user_load(array('name' => $form_state['values']['username'])); |
| 567 |
|
if ($account) { |
| 568 |
|
form_set_error('username', t('Users that are to be used by the IP Authenticator have to be blocked on |
| 569 |
|
their <a href="@settings-page">settings page</a>.', array('@settings-page' => url("user/$account->uid/edit")))); |
| 570 |
|
} |
| 571 |
|
else { |
| 572 |
|
form_set_error('username', t('There was an error while loading the user.')); |
| 573 |
|
} |
| 574 |
|
} else { |
| 575 |
|
form_set_error('username', t('The username must contain a value.')); |
| 576 |
|
} |
| 577 |
|
} |
| 578 |
|
|
| 579 |
|
$checkbox_keys = array_keys($form_state['values']); |
| 580 |
|
foreach ($checkbox_keys as $key => $value) { |
| 581 |
|
if (strpos($key, 'checkbox_') !== FALSE && ($value != 0 && $value != 1 ) ) { |
| 582 |
|
form_set_error($key, t('Checkbox value must be either checked or not checked.')); |
| 583 |
|
} |
| 584 |
|
} |
| 585 |
|
|
| 586 |
|
} //end function ipauth_admin_settings_validate |
| 587 |
|
|
| 588 |
|
/** |
| 589 |
|
* Submit our admin settings |
| 590 |
|
*/ |
| 591 |
|
function ipauth_admin_settings_submit($form_id, &$form_state) { |
| 592 |
|
if (trim($form_state['values']['ip1']) != "") { |
| 593 |
|
if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_state['values']['ip2']))) { |
| 594 |
|
$ip2 = trim($form_state['values']["ip2"]); |
| 595 |
|
} |
| 596 |
|
else { |
| 597 |
|
$ip2 = sprintf("%u", ip2long(trim($form_state['values']['ip2']))); |
| 598 |
|
} |
| 599 |
|
|
| 600 |
|
$sql = "INSERT INTO {ip_authenticator} (ip1, ip2, uid, description, enabled, created) VALUES ('%s','%s','%d','%s', '%d', NOW())"; |
| 601 |
|
$account = user_load(array('name' => $form_state['values']['username'])); |
| 602 |
|
|
| 603 |
|
if ($account) { |
| 604 |
|
db_query($sql, sprintf("%u", ip2long(trim($form_state['values']['ip1']))), $ip2, $account->uid, $form_state['values']['ipauth_desc'], $form_state['values']['checkbox_enabled']); |
| 605 |
|
if (db_affected_rows() == 0) { |
| 606 |
|
drupal_set_message(mysql_error(), 'error'); |
| 607 |
|
} |
| 608 |
|
// empty the menu cache for the user |
| 609 |
|
cache_clear_all($account->uid .':', 'cache_menu', TRUE); |
| 610 |
|
} |
| 611 |
|
else { |
| 612 |
|
drupal_set_message(t('Error while loading the user'), 'error'); |
| 613 |
|
} |
| 614 |
|
|
| 615 |
|
ipauth_update_checkboxes($form_state); |
| 616 |
|
} else { |
| 617 |
|
ipauth_update_checkboxes($form_state); |
| 618 |
|
} |
| 619 |
|
} // end function ipauth_admin_settings_submit |
| 620 |
|
|
| 621 |
|
/** |
| 622 |
|
* This function loops through all input fields and updates just the |
| 623 |
|
* checkbox field status' in the database. |
| 624 |
|
*/ |
| 625 |
|
function ipauth_update_checkboxes(&$form_state) { |
| 626 |
|
|
| 627 |
|
// Get a list of all input field names |
| 628 |
|
$checkbox_keys = array_keys($form_state['values']); |
| 629 |
|
|
| 630 |
|
//loop through all input fields |
| 631 |
|
foreach ($checkbox_keys as $key) { |
| 632 |
|
|
| 633 |
|
//if the input field name ($key) contains, checkbox_, and the value is either zero or one, then update the |
| 634 |
|
// database with the checkbox input field's value. |
| 635 |
|
if ($key != "checkbox_enabled" && strpos($key, "checkbox_") !== FALSE && ($form_state['values'][$key] == 0 || $form_state['values'][$key] == 1) ) { |
| 636 |
|
//update sql statement |
| 637 |
|
$sql = "UPDATE {ip_authenticator} SET enabled = '%d' WHERE id = '%s'"; |
| 638 |
|
$id = substr($key, strrpos($key, '_')+1); |
| 639 |
|
|
| 640 |
|
db_query($sql, $form_state['values'][$key], $id); |
| 641 |
|
} |
| 642 |
|
} |
| 643 |
|
} |
| 644 |
|
|
| 645 |
|
/** |
| 646 |
|
* Form for editing an authentication entry |
| 647 |
|
*/ |
| 648 |
|
function ipauth_admin_edit($form_state) { |
| 649 |
|
$args = func_get_args(); |
| 650 |
|
|
| 651 |
|
if (!preg_match("/^[0-9]+\$/", $args[1])) { |
| 652 |
|
drupal_set_message(t("Please select your authenticator again")); |
| 653 |
|
drupal_goto('admin/user/ip_authenticator'); |
| 654 |
|
} |
| 655 |
|
|
| 656 |
|
$row = ipauth_get_ip_uid_info($args[1]); |
| 657 |
|
if ($account = user_load(array('uid' => $row['uid']))) { |
| 658 |
|
$username = $account->name; |
| 659 |
|
} |
| 660 |
|
else { |
| 661 |
|
$username = ''; |
| 662 |
|
} |
| 663 |
|
|
| 664 |
|
$form['ips'] = array( |
| 665 |
|
'#type' => 'fieldset', |
| 666 |
|
'#title' => t('IP address assignment'), |
| 667 |
|
'#collapsible' => FALSE, |
| 668 |
|
'#collapsed' => FALSE, |
| 669 |
|
'#weight' => 0 |
| 670 |
|
); |
| 671 |
|
$form['ips']['checkbox_enabled'] = array( |
| 672 |
|
'#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>', |
| 673 |
|
'#suffix' => '</div>', |
| 674 |
|
'#type' => 'checkbox', |
| 675 |
|
'#description' => 'Enable/Disable', |
| 676 |
|
'#id' => 'enable_disable_checkbox', |
| 677 |
|
'#default_value' => ($row["enabled"] == TRUE) ? 1 : 0, |
| 678 |
|
); |
| 679 |
|
$form['ips']['ip1'] = array( |
| 680 |
|
'#prefix' => '<div class="floatLeft">', |
| 681 |
|
'#type' => 'textfield', |
| 682 |
|
'#title' => 'IP 1', |
| 683 |
|
'#size' => '15', |
| 684 |
|
'#default_value' => $row["ip1"], |
| 685 |
|
'#required' => TRUE, |
| 686 |
|
'#suffix' => '</div><div class="floatLeft" id="formSeperator"> - </div>' |
| 687 |
|
); |
| 688 |
|
|
| 689 |
|
$form['ips']['ip2'] = array( |
| 690 |
|
'#prefix' => '<div class="floatLeft">', |
| 691 |
|
'#type' => 'textfield', |
| 692 |
|
'#title' => 'IP 2', |
| 693 |
|
'#description' => t('Leave blank for<br/>individual IPs'), |
| 694 |
|
'#size' => '15', |
| 695 |
|
'#default_value' => $row["ip2"], |
| 696 |
|
'#suffix' => '</div><div class="floatLeft"> </div>', |
| 697 |
|
); |
| 698 |
|
|
| 699 |
|
$form['ips']['username'] = array( |
| 700 |
|
'#prefix' => '<div class="floatLeft">', |
| 701 |
|
'#type' => 'textfield', |
| 702 |
|
'#maxlength' => 60, |
| 703 |
|
'#title' => t('Assign to User'), |
| 704 |
|
'#autocomplete_path' => 'user/autocomplete', |
| 705 |
|
'#default_value' => $username, |
| 706 |
|
'#required' => TRUE, |
| 707 |
|
'#description' => t("Select a user to assign the IPs to"), |
| 708 |
|
'#suffix' => '</div><div class="floatLeft" id="deleteLink"> '. l("delete", "admin/user/ip_authenticator/delete/". $args[1]) .'</div>', |
| 709 |
|
); |
| 710 |
|
$form['ips']['ipauth_desc'] = array( |
| 711 |
|
'#prefix' => '<br style="clear:both" />', |
| 712 |
|
'#type' => 'textfield', |
| 713 |
|
'#title' => t('Description of Authenticator'), |
| 714 |
|
'#value' => $row["description"], |
| 715 |
|
'#description' => t("Enter a description to better identify this authenticator"), |
| 716 |
|
); |
| 717 |
|
$form['id'] = array( |
| 718 |
|
'#type' => 'hidden', |
| 719 |
|
'#value' => $args[1], |
| 720 |
|
); |
| 721 |
|
|
| 722 |
|
$form['#validate'][] = 'ipauth_admin_settings_validate'; |
| 723 |
|
$form['#submit'][] ='ipauth_admin_edit_submit'; |
| 724 |
|
$form['#redirect'] = 'admin/user/ip_authenticator'; |
| 725 |
|
|
| 726 |
|
return system_settings_form($form); |
| 727 |
|
} // end function ipauth_admin_edit |
| 728 |
|
|
| 729 |
|
/** |
| 730 |
|
* Submit our edit form |
| 731 |
|
*/ |
| 732 |
|
function ipauth_admin_edit_submit($form_id, &$form_state) { |
| 733 |
|
|
| 734 |
|
if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", trim($form_state['values']['ip2'])) ) { |
| 735 |
|
$ip2_long = trim($form_state['values']['ip2']); |
| 736 |
|
} |
| 737 |
|
else { |
| 738 |
|
$ip2_long = sprintf("%u", ip2long(trim($form_state['values']['ip2']))); |
| 739 |
|
} |
| 740 |
|
if ($account = user_load(array('name' => $form_state['values']['username']))) { |
| 741 |
|
$sql = "UPDATE {ip_authenticator} SET ip1 = '%s', ip2 = '%s', uid = '%s', description = '%s', enabled = '%d', created = NOW() WHERE id = '%s'"; |
| 742 |
|
db_query($sql, sprintf("%u", ip2long(trim($form_state['values']['ip1']))), $ip2_long, $account->uid, $form_state['clicked_button']['#post']['ipauth_desc'], $form_state['values']['checkbox_enabled'], $form_state['values']['id']); |
| 743 |
|
if (db_affected_rows() == 0) { |
| 744 |
|
drupal_set_message(mysql_error(), 'error'); |
| 745 |
|
} |
| 746 |
|
|
| 747 |
|
// empty the menu cache for the user |
| 748 |
|
cache_clear_all($account->uid .':', 'cache_menu', TRUE); |
| 749 |
|
} |
| 750 |
|
else { |
| 751 |
|
drupal_set_message(t('Error while loading the user'), 'error'); |
| 752 |
|
} |
| 753 |
|
|
| 754 |
|
} // end function ipauth_admin_edit_submit |
| 755 |
|
|
| 756 |
|
/** |
| 757 |
|
* Our deletion function |
| 758 |
|
*/ |
| 759 |
|
function ipauth_admin_delete($form_state) { |
| 760 |
|
$args = func_get_args(); |
| 761 |
|
if (!preg_match("/^[0-9]+\$/", $args[1])) { |
| 762 |
|
drupal_set_message(t("Please select your authenticator again")); |
| 763 |
|
drupal_goto("admin/user/ip_authenticator"); |
| 764 |
|
} |
| 765 |
|
|
| 766 |
|
$row = ipauth_get_ip_uid_info($args[1]); |
| 767 |
|
if ($account = user_load(array('uid' => $row['uid']))) { |
| 768 |
|
$username = $account->name; |
| 769 |
|
} |
| 770 |
|
else { |
| 771 |
|
$account->name = t('Error while loading the user'); |
| 772 |
|
} |
| 773 |
|
|
| 774 |
|
$ip2_text = ($row["ip2"] != "") ? " - ". $row["ip2"] : ""; |
| 775 |
|
$title = "<li>" . t('@IPrange authenticates to: @username', |
| 776 |
|
array('@IPrange' => $row['ip1'] . $ip2_text, |
| 777 |
|
'@username' => $username)); |
| 778 |
|
|
| 779 |
|
$form["markup"] = array( |
| 780 |
|
'#prefix' => "<div id='confirmbox'>", |
| 781 |
|
'#suffix' => "", |
| 782 |
|
'#value' => t("Are you certain that you wish to delete this ip authenticator?") ." <ul>". $title ."</ul>" |
| 783 |
|
); |
| 784 |
|
|
| 785 |
|
$form["accept"] = array( |
| 786 |
|
'#type' => 'submit', |
| 787 |
|
'#value' => 'Delete', |
| 788 |
|
'#prefix' => '<div class="floatLeft">', |
| 789 |
|
'#suffix' => '</div><div class="floatLeft"> </div>', |
| 790 |
|
); |
| 791 |
|
|
| 792 |
|
$form['cancel'] = array( |
| 793 |
|
'#prefix' => '<div class="floatLeft">', |
| 794 |
|
'#value' => '<input type="button" value="Cancel" id="cancel">', |
| 795 |
|
'#suffix' => '</div></div>' |
| 796 |
|
); |
| 797 |
|
|
| 798 |
|
$form['id'] = array( |
| 799 |
|
'#type' => 'hidden', |
| 800 |
|
'#value' => $args[1] |
| 801 |
|
); |
| 802 |
|
|
| 803 |
|
$form['#validate'][] = 'ipauth_admin_delete_validate'; |
| 804 |
|
$form['#submit'][] ='ipauth_admin_delete_submit'; |
| 805 |
|
$form['#redirect'] = 'admin/user/ip_authenticator'; |
| 806 |
|
return $form; |
| 807 |
|
} // end function ipauth_admin_delete |
| 808 |
|
|
| 809 |
|
/** |
| 810 |
|
* Validate our deletion form |
| 811 |
|
*/ |
| 812 |
|
function ipauth_admin_delete_validate($form_id, &$form_state) { |
| 813 |
|
if (!preg_match("/^[0-9]+\$/", $form_state['values']["id"])) |
| 814 |
|
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."); |
| 815 |
|
} // end function ipauth_admin_delete_validate |
| 816 |
|
|
| 817 |
|
/** |
| 818 |
|
* Submit our deletion form |
| 819 |
|
*/ |
| 820 |
|
function ipauth_admin_delete_submit($form_id, &$form_state) { |
| 821 |
|
$result = db_query("DELETE FROM {ip_authenticator} WHERE id = '%s'", $form_state['values']['id']); |
| 822 |
|
if (db_affected_rows() < 1) { |
| 823 |
|
drupal_set_message(t("Error in execution of sql statement -- no changes made.")); |
| 824 |
|
} |
| 825 |
|
else { |
| 826 |
|
drupal_set_message(t("The IP Authenticator item has been deleted.")); |
| 827 |
|
} |
| 828 |
|
drupal_goto("admin/user/ip_authenticator"); |
| 829 |
|
} // end function ipauth_admin_delete_submit |
| 830 |
|
|
| 831 |
|
|
| 832 |
|
/** |
| 833 |
|
* Implementation of hook_block(). |
| 834 |
|
*/ |
| 835 |
|
|
| 836 |
|
function ipauth_block($op = 'list', $delta = 0, $edit = array()) { |
| 837 |
|
global $user; |
| 838 |
|
if ($op == 'list') { |
| 839 |
|
$blocks[0]['info'] = t('IPAuthenticator User login'); |
| 840 |
|
return $blocks; |
| 841 |
|
} |
| 842 |
|
else if ($op == 'view') { |
| 843 |
|
$block = array(); |
| 844 |
|
|
| 845 |
|
switch ($delta) { |
| 846 |
|
case 0: |
| 847 |
|
// For usability's sake, avoid showing two login forms on one page. |
| 848 |
|
// In contrast to the user.module login block this block is shown when |
| 849 |
|
// an ip_auth authenticated user is online. |
| 850 |
|
if (in_array($user->uid, ipauth_get_uids()) && !(arg(0) == 'user' && !is_numeric(arg(1)))) { |
| 851 |
|
$block['subject'] = t('User login'); |
| 852 |
|
$block['content'] = drupal_get_form('user_login_block'); |
| 853 |
|
} |
| 854 |
|
return $block; |
| 855 |
|
} |
| 856 |
|
} |
| 857 |
|
} |
| 858 |
|
|
| 859 |
|
/********* |
| 860 |
|
* HELPERS |
| 861 |
|
*/ |
| 862 |
|
|
| 863 |
|
/** |
| 864 |
|
* Pull ip info when given an entry id |
| 865 |
|
* @param entry id |
| 866 |
|
* @return IPs |
| 867 |
|
*/ |
| 868 |
|
function ipauth_get_ip_uid_info($id) { |
| 869 |
|
$result = db_query("SELECT uid, ip1, ip2, description, enabled FROM {ip_authenticator} WHERE id = '%s'", $id); |
| 870 |
|
$row = db_fetch_array($result); |
| 871 |
|
$row["ip1"] = long2ip($row["ip1"]); |
| 872 |
|
$row["ip2"] = ($row["ip2"] == 0) ? "" : long2ip($row["ip2"]); |
| 873 |
|
return $row; |
| 874 |
|
} // end function ipauth_get_ip_role_info |
| 875 |
|
|
| 876 |
/** |
/** |
| 877 |
* Queries the database to see if any IP based role changes are found. |
* Queries the database to see if any IP based role changes are found. |
| 878 |
* @return $result - a database result set. |
* @return $result - a database result set. |
| 879 |
*/ |
*/ |
| 880 |
function _get_ip_authenticators($ip = "", $return_fields="roles") { |
function ipauth_get_ip_authenticators($ip = "", $return_fields = "uid", $sort_field = NULL, $sort_order = NULL) { |
| 881 |
|
$sql_where_clause = ("ALL" == $ip || "all" == $ip || "*" == $ip || "" == $ip) ? "1=1" : "((ip1 = '%s') OR (ip1 <> 0 AND ip1 <= '%s' AND ip2 >= '%s')) AND enabled <> 0"; |
| 882 |
|
$sql_order_by = ($sort_field != "") ? " ORDER BY ".$sort_field." ".$sort_order : ""; |
| 883 |
|
|
| 884 |
|
$sql = "SELECT ". $return_fields .", id FROM {ip_authenticator} WHERE ". $sql_where_clause . $sql_order_by; |
| 885 |
|
|
| 886 |
|
// use the php functions and not the mysql function INET_ATON and INET_NTOA functions. This will provide greater database functionality. |
| 887 |
|
$long_ip = sprintf("%u", ip2long($ip)); |
| 888 |
|
return db_query($sql, $long_ip, $long_ip, $long_ip); |
| 889 |
|
} // end function ipauth_get_ip_authenticators |
| 890 |
|
|
| 891 |
|
|
| 892 |
|
/** |
| 893 |
|
* Get an array of all uids used by ip authenticator |
| 894 |
|
* @return array List of all uids |
| 895 |
|
*/ |
| 896 |
|
function ipauth_get_uids() { |
| 897 |
|
static $uids; |
| 898 |
|
if (!isset($uids)) { |
| 899 |
|
$uids = array(); |
| 900 |
|
$sql = "SELECT DISTINCT uid FROM {ip_authenticator} WHERE 1"; |
| 901 |
|
$result = db_query($sql); |
| 902 |
|
while ($row = db_fetch_array($result)) { |
| 903 |
|
$uids[] = $row['uid']; |
| 904 |
|
} |
| 905 |
|
} |
| 906 |
|
return $uids; |
| 907 |
|
} |
| 908 |
|
|
| 909 |
|
|
| 910 |
|
/** |
| 911 |
|
* Prepare a login form as if the anonymous user was requesting it. |
| 912 |
|
* @return $form |
| 913 |
|
*/ |
| 914 |
|
function ipauth_user_page() { |
| 915 |
|
global $user; |
| 916 |
|
|
| 917 |
|
$ipauth_uid = $user->uid; |
| 918 |
|
$user->uid = 0; |
| 919 |
|
$user_login_form = drupal_get_form('user_login'); |
| 920 |
|
$user->uid = $ipauth_uid; |
| 921 |
|
//$form['#validate'] = array('user_login_validate' => array()); |
| 922 |
|
//$form['#submit'] = array('user_login_submit' => array()); |
| 923 |
|
return $user_login_form; |
| 924 |
|
} |
| 925 |
|
|
| 926 |
|
|
| 927 |
|
|
| 928 |
|
/** |
| 929 |
|
* Prepare a user register form as if the anonymous user was requesting it. |
| 930 |
|
* @return $form |
| 931 |
|
*/ |
| 932 |
|
function ipauth_user_register() { |
| 933 |
|
global $user; |
| 934 |
|
$uid = $user->uid; |
| 935 |
|
$user->uid = 0; |
| 936 |
|
$form = user_register(); |
| 937 |
|
$user->uid = $uid; |
| 938 |
|
$form['#validate'] = array('user_register_validate' => array()); |
| 939 |
|
$form['#submit'] = array('user_register_submit' => array()); |
| 940 |
|
return $form; |
| 941 |
|
} |
| 942 |
|
|
| 943 |
|
/** |
| 944 |
|
* This function determines if the user is IP authenticated. |
| 945 |
|
* |
| 946 |
|
* @param $not: a boolean value: used to not (!) the result |
| 947 |
|
* @param $and: a boolean value: used to optionally make the result false if not already |
| 948 |
|
* @param $test_uid: a boolean value: used to check to see if the uid is defined and has a value |
| 949 |
|
* @param $test_uid_not: a boolean value to negate the result of if the uid is defined and has a value |
| 950 |
|
* @param $output some debug text. |
| 951 |
|
* |
| 952 |
|
* @return a boolean value of it to allow permissions to a menu item. |
| 953 |
|
*/ |
| 954 |
|
function is_ipauth_user($not = FALSE, $and = TRUE, $test_uid = FALSE, $test_uid_not = FALSE, $debug_text=NULL) { |
| 955 |
|
static $ipauth_uids; |
| 956 |
|
static $result; |
| 957 |
|
|
| 958 |
|
$uid = $GLOBALS['user']->uid; |
| 959 |
|
|
| 960 |
|
if (!isset($ipauth_uids)) { |
| 961 |
|
$ipauth_uids = ipauth_get_uids(); |
| 962 |
|
} |
| 963 |
|
if (!isset($result)) { |
| 964 |
|
$result = in_array($uid, $ipauth_uids); |
| 965 |
|
} |
| 966 |
|
|
| 967 |
|
if ($test_uid && !$test_uid_not) { |
| 968 |
|
$result = ($result) || (bool)$uid; |
| 969 |
|
} |
| 970 |
|
|
| 971 |
|
if ($test_uid && $test_uid_not) { |
| 972 |
|
$result = $result || !(bool)$uid; |
| 973 |
|
} |
| 974 |
|
|
| 975 |
|
if ($not) { |
| 976 |
|
return !$result && $and; |
| 977 |
|
} |
| 978 |
|
|
| 979 |
|
return $result && $and; |
| 980 |
|
} |
| 981 |
|
|
| 982 |
|
/** |
| 983 |
|
* This function will return to the browser a CVS file containing all IP Authenticators. |
| 984 |
|
* @param <string> $which_ones this is optional and reserved for future use. |
| 985 |
|
*/ |
| 986 |
|
function ipauth_export($which_ones) { |
| 987 |
|
header("Pragma: public"); |
| 988 |
|
header("Expires: 0"); |
| 989 |
|
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
| 990 |
|
header("Cache-Control: private",false); |
| 991 |
|
header("Content-Type: application/zip"); |
| 992 |
|
header("Content-Disposition: attachment; filename=IP_Authenticator_Export_".date("Y-m-d").".cvs;" ); |
| 993 |
|
$result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, UNIX_TIMESTAMP(created) as created"); |
| 994 |
|
while ($row = db_fetch_array($result)) { |
| 995 |
|
printf('"%s","%s","%s","%s","%s","%s","%s" |
| 996 |
|
', $row["id"],$row["uid"],$row["ip1"],$row["ip2"],$row["description"],$row["enabled"],$row["created"]); |
| 997 |
|
} |
| 998 |
|
|
| 999 |
|
module_invoke_all('exit', $url); |
| 1000 |
|
|
| 1001 |
|
// Even though session_write_close() is registered as a shutdown function, we |
| 1002 |
|
// need all session data written to the database before redirecting. |
| 1003 |
|
session_write_close(); |
| 1004 |
|
exit; |
| 1005 |
|
} |
| 1006 |
|
|
| 1007 |
|
/** |
| 1008 |
|
* If the user is not IP Authenticated and is logged in, |
| 1009 |
|
* then display the logout link |
| 1010 |
|
* |
| 1011 |
|
* @return boolean value |
| 1012 |
|
*/ |
| 1013 |
|
function display_logout() { |
| 1014 |
|
$ip_user = is_ipauth_user(FALSE, TRUE); // Is this an ip authenticated user? |
| 1015 |
|
|
| 1016 |
|
return !$ip_user && (bool)$GLOBALS['user']->uid; |
| 1017 |
|
} |
| 1018 |
|
|
| 1019 |
|
/** |
| 1020 |
|
* Determins which arrow image to theme |
| 1021 |
|
*/ |
| 1022 |
|
function ipauth_arrow($sort_field, $active_field, $order) { |
| 1023 |
|
static $image_down; |
| 1024 |
|
static $image_up; |
| 1025 |
|
|
| 1026 |
|
if ($sort_field == $active_field) { |
| 1027 |
|
if ($order == "desc") { |
| 1028 |
|
if (!$image_down){ |
| 1029 |
|
$image_down = " ". theme("image", 'misc/arrow-asc.png','asc','asc'); |
| 1030 |
|
} |
| 1031 |
|
return $image_down; |
| 1032 |
|
} else { |
| 1033 |
|
if (!$image_up) { |
| 1034 |
|
$image_up = " ". theme("image", 'misc/arrow-desc.png','desc','desc'); |
| 1035 |
|
} |
| 1036 |
|
return $image_up; |
| 1037 |
|
} |
| 1038 |
|
} |
| 1039 |
|
return ""; |
| 1040 |
|
} |
| 1041 |
|
|
| 1042 |
|
|
| 1043 |
|
/************ |
| 1044 |
|
* These are theming function to enhance drupal 6.x FAPI |
| 1045 |
|
* to include form elements inside a form api table structure. |
| 1046 |
|
* |
| 1047 |
|
*/ |
| 1048 |
|
|
| 1049 |
|
/** |
| 1050 |
|
* Implementation of hook_elements(). |
| 1051 |
|
*/ |
| 1052 |
|
function formtable_elements() { |
| 1053 |
|
$types = array(); |
| 1054 |
|
|
| 1055 |
|
$types['formtable'] = array( |
| 1056 |
|
'#input' => TRUE, |
| 1057 |
|
); |
| 1058 |
|
|
| 1059 |
|
$types['formrow'] = array( |
| 1060 |
|
'#input' => TRUE, |
| 1061 |
|
); |
| 1062 |
|
|
| 1063 |
|
return $types; |
| 1064 |
|
} |
| 1065 |
|
|
| 1066 |
|
/** |
| 1067 |
|
* Implemntation of hook_theme |
| 1068 |
|
*/ |
| 1069 |
|
function ipauth_theme() { |
| 1070 |
|
$themes=array(); |
| 1071 |
|
$themes['formtable'] = array( |
| 1072 |
|
'argument' => array('element' => NULL) |
| 1073 |
|
); |
| 1074 |
|
$themes['formrow'] = array( |
| 1075 |
|
'argument' => array('element' => NULL) |
| 1076 |
|
); |
| 1077 |
|
return $themes; |
| 1078 |
|
} |
| 1079 |
|
|
| 1080 |
|
/** |
| 1081 |
|
* Format a formtable element. |
| 1082 |
|
* |
| 1083 |
|
* @param $element |
| 1084 |
|
* An associative array containing the properties of the element. |
| 1085 |
|
* Properties used: attributes, title, description, caption, children, header |
| 1086 |
|
* @return |
| 1087 |
|
* A themed HTML string representing the form table. |
| 1088 |
|
*/ |
| 1089 |
|
function theme_formtable($element) { |
| 1090 |
|
|
| 1091 |
switch(strtolower($ip)) { |
//(theme_table doesn't work here because $element['#children'] is already rendered as HTML) |
| 1092 |
case "all": |
|
| 1093 |
case "*": |
$output = "<div class=\"form-table\">\n"; |
| 1094 |
case "": |
if ($element['#title']) { |
| 1095 |
$sql_where_clause = "1"; |
$output .= "<span class=\"table-title\">". $element['#title'] .':</span>'; |
| 1096 |
break; |
} |
| 1097 |
default: |
|
| 1098 |
$sql_where_clause = "(ip1 = '%s') OR (ip2 <> 0 AND ip1 <= '%s' AND ip2 >= '%s')"; |
if ($element['#description']) { |
| 1099 |
break; |
$output .= "\n<div class=\"description\">". $element['#description'] ."</div>\n"; |
| 1100 |
} |
} |
| 1101 |
|
|
| 1102 |
$sql = "SELECT ". $return_fields .", id FROM {ip_authenticator} WHERE ". $sql_where_clause; |
$output .= "\n<table id=\"". $element['#id'] .'"'. drupal_attributes($element['#attributes']) .">\n"; |
| 1103 |
$long_ip = sprintf("%u", ip2long($ip)); // use the php functions and not the mysql function INET_ATON and INET_NTOA. This will provide greater database functionality. |
if ($element['#caption']) { |
| 1104 |
|
$output .= "\t<caption>". $element['#caption'] ."</caption>\n"; |
| 1105 |
|
} |
| 1106 |
|
|
| 1107 |
return db_query($sql, $long_ip, $long_ip, $long_ip); |
if ($element['#header']) { |
| 1108 |
} // end function _get_ip_authenticators |
|
| 1109 |
|
$output .= "\t<thead>\n\t\t<tr>\n"; |
| 1110 |
|
foreach ($element['#header'] as $col_header) { |
| 1111 |
|
$output .= "\t\t\t<th>". $col_header ."</th>\n"; |
| 1112 |
|
} |
| 1113 |
|
$output .= "\t\t</tr>\n\t</thead>\n"; |
| 1114 |
|
|
| 1115 |
|
} |
| 1116 |
|
$output .= "\t<tbody>\n"; |
| 1117 |
|
$output .= $element['#children']; |
| 1118 |
|
$output .= "\t</tbody>\n"; |
| 1119 |
|
$output .= "</table>\n</div>\n"; |
| 1120 |
|
|
| 1121 |
|
return $output; |
| 1122 |
|
|
| 1123 |
|
} |
| 1124 |
|
|
| 1125 |
|
/** |
| 1126 |
|
* Format a formrow element. |
| 1127 |
|
* |
| 1128 |
|
* @param $element |
| 1129 |
|
* An associative array containing the properties of the element. |
| 1130 |
|
* Properties used: attributes, children. |
| 1131 |
|
* |
| 1132 |
|
* Set 'class' key in the attributes array to 'even' or 'odd' for zebra tables. |
| 1133 |
|
* |
| 1134 |
|
* @return |
| 1135 |
|
* A themed HTML string representing the form row. |
| 1136 |
|
*/ |
| 1137 |
|
function theme_formrow($element) { |
| 1138 |
|
return "\t<tr id=\"". $element['#id'] ."\"". drupal_attributes($element['#attributes']) .'>'. $element['#children'] ."</tr>\n"; |
| 1139 |
|
} |