| 1 |
<?php |
<?php |
| 2 |
// $Id: ipauth.module,v 1.8 2009/03/17 14:21:02 jonfrancisskydiver Exp $ |
// $Id$ |
| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 5 |
* Module code for an IP based authenticator |
* Module code for an IP based authenticator |
| 18 |
|
|
| 19 |
// Also check ip auth users to see if they have changed the network so that |
// Also check ip auth users to see if they have changed the network so that |
| 20 |
// a new uid has to be assigned. |
// a new uid has to be assigned. |
| 21 |
if (!$user->uid || in_array($user->uid, ipauth_get_uids())) { |
if (!$user->uid || in_array($user->uid, ipauth_get_uids("enabled"))) { |
| 22 |
|
|
| 23 |
// Get the first result from the database, we can only assign one single uid. |
// Get the first result from the database, we can only assign one single uid. |
| 24 |
$result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid"); |
$result = ipauth_get_ip_authenticators($_SERVER['REMOTE_ADDR'], "uid"); |
| 61 |
} |
} |
| 62 |
} |
} |
| 63 |
|
|
| 64 |
if (in_array($user->uid, ipauth_get_uids())) { |
if (in_array($user->uid, ipauth_get_uids('enabled'))) { |
| 65 |
// It's one of the special ip_auth users, take the authenticated user role away. |
// It's one of the special ip_auth users, take the authenticated user role away. |
| 66 |
unset($user->roles[DRUPAL_AUTHENTICATED_RID]); |
unset($user->roles[DRUPAL_AUTHENTICATED_RID]); |
| 67 |
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; |
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; |
| 264 |
|
|
| 265 |
function ipauth_import_export($form_state) { |
function ipauth_import_export($form_state) { |
| 266 |
$form['#attributes']['enctype'] = 'multipart/form-data'; |
$form['#attributes']['enctype'] = 'multipart/form-data'; |
| 267 |
|
/* |
| 268 |
$form['export_fieldset'] = array( |
$form['export_fieldset'] = array( |
| 269 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 270 |
'#title' => t('Export IP Authenticators to a CSV file'), |
'#title' => t('Export IP Authenticators to a CSV file'), |
| 271 |
'#description' => t('This will cause you to download a CSV file containing all IP Authenticators.'), |
'#description' => t('This will cause you to download a CSV file containing all IP Authenticators.'), |
| 272 |
'#weight' => 1, |
'#weight' => 1, |
| 273 |
); |
); |
| 274 |
$form['export_fieldset']['export'] = array( |
$form['export_fieldset']['export'] = array( |
| 275 |
'#value' => l('Export', 'admin/user/ip_authenticator/data/export'), |
'#value' => l('Export', 'admin/user/ip_authenticator/data/export'), |
| 276 |
'#weight' => 1 |
'#weight' => 1 |
| 277 |
); |
); |
| 278 |
|
*/ |
| 279 |
$form['import_fieldset'] = array( |
$form['import_fieldset'] = array( |
| 280 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 281 |
'#title' => t('Import IP Authenticators from a CSV file'), |
'#title' => t('Import IP Authenticators from a CSV file'), |
| 290 |
'#description' => t('This will import all the IP Authenticator\'s in the CSV file to the database.') |
'#description' => t('This will import all the IP Authenticator\'s in the CSV file to the database.') |
| 291 |
); |
); |
| 292 |
$form['import_fieldset']['belowmarkup'] = array( |
$form['import_fieldset']['belowmarkup'] = array( |
| 293 |
'value' => t('The format of the CSV file is as follows:') .'<Br/>' . |
'#value' => t('The format of the CSV file is as follows:') .'<Br/><ul><li>' . |
| 294 |
t('"Row ID","Drupal User ID","Unsigned Int of IP","Unsigned Int of IP","description","0 for disabledm, or 1 for enabled","unix timestamp"') ."<br/>", |
t('"Row ID","Drupal User ID","IP Address","IP Address","description","0 for disabled, or 1 for enabled","unix timestamp"') ."</li></ul><br/>", |
| 295 |
|
|
| 296 |
); |
); |
| 297 |
$form['import_fieldset']['submit'] = array( |
$form['import_fieldset']['submit'] = array( |
| 344 |
array_walk($row, '_ipauth_strip_quotes'); |
array_walk($row, '_ipauth_strip_quotes'); |
| 345 |
if (_ipauth_check_import($row)) { |
if (_ipauth_check_import($row)) { |
| 346 |
$sql = "INSERT INTO {ip_authenticator} (uid, ip1, ip2, description, enabled, created) VALUES ('%s','%s','%s','%s', '%d', '%s')"; |
$sql = "INSERT INTO {ip_authenticator} (uid, ip1, ip2, description, enabled, created) VALUES ('%s','%s','%s','%s', '%d', '%s')"; |
| 347 |
db_query($sql, $row["uid"], $row["ip1"], $row["ip2"], $row["description"], $row["enabled"], $row["created"]); |
db_query($sql, $row["uid"], sprintf("%u", ip2long(trim($row['ip1']))), sprintf("%u", ip2long(trim($row['ip2']))), $row["description"], $row["enabled"], $row["created"]); |
| 348 |
} |
} |
| 349 |
} |
} |
| 350 |
|
|
| 364 |
|
|
| 365 |
$form['ip_list'] = array( |
$form['ip_list'] = array( |
| 366 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 367 |
'#title' => t('Current IP Authenticators'), |
'#title' => t('Modify the IP Authenticators'), |
| 368 |
'#description' => "<b>". t("Instructions:") ."</b><br/> ". |
'#description' => "<b>". t("Instructions:") ."</b><br/> ". |
| 369 |
t(" - Click on an ip range to modify the authenticator assignment.") ."<br/> ". |
t(" - Click on an ip range to modify the authenticator assignment.") ."<br/> ". |
| 370 |
t(" - Click on a user name to edit the account.") ."<br/> ". |
t(" - Click on a user name to edit the account.") ."<br/> ". |
| 371 |
t(" - The title fields are sortable.") ."<br/><br/>". |
t(" - The title fields are sortable."), |
|
"<b>". t("Your IP: @s", array('@s' => $_SERVER['REMOTE_ADDR'])) ."</b>". |
|
|
'<br /><br/>'. |
|
|
t('Please note that users authenticated via IP authenticator do not get permissions of the <em>authenticated user</em> role.'), |
|
| 372 |
'#collapsible' => TRUE, |
'#collapsible' => TRUE, |
| 373 |
'#collapsed' => FALSE, |
'#collapsed' => FALSE, |
| 374 |
'#weight' => 0 |
'#weight' => 0 |
| 375 |
); |
); |
| 376 |
|
|
| 377 |
$form['ip_list']['import-export'] = array( |
$form['ip_list']['import-export'] = array( |
| 378 |
'#value' => l("Import/Export", "admin/user/ip_authenticator/data"), |
'#value' => l("Export", "admin/user/ip_authenticator/data/export", array('attributes'=>array('class'=>'button'))), |
| 379 |
'#prefix' => '<div id="import-export">', |
'#prefix' => '<div id="import-export">', |
| 380 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 381 |
'#weight' => 10, |
'#weight' => 0, |
| 382 |
); |
); |
| 383 |
|
|
| 384 |
$non_blocked_users = array(); |
$non_blocked_users = array(); |
| 409 |
//and incorporated into this ipauth module. |
//and incorporated into this ipauth module. |
| 410 |
$form['ip_list']['ipauth_table'] = array( |
$form['ip_list']['ipauth_table'] = array( |
| 411 |
'#type' => 'formtable', |
'#type' => 'formtable', |
| 412 |
'#header' => array( |
'#header' => array( |
|
l(t('Active'), $_GET["q"], array('query' => array('sort_field' => 'enabled', 'order' => $order_toggle[$order]))) . ipauth_arrow($sort_field, 'enabled', $order), |
|
| 413 |
l(t('IP Range'), $_GET["q"], array('query' => array('sort_field' => 'ip1', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'ip1', $order), |
l(t('IP Range'), $_GET["q"], array('query' => array('sort_field' => 'ip1', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'ip1', $order), |
| 414 |
|
l(t('Active'), $_GET["q"], array('query' => array('sort_field' => 'enabled', 'order' => $order_toggle[$order]))) . ipauth_arrow($sort_field, 'enabled', $order), |
| 415 |
l(t('User'), $_GET["q"], array('query' => array('sort_field' => 'uid', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'uid', $order), |
l(t('User'), $_GET["q"], array('query' => array('sort_field' => 'uid', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'uid', $order), |
| 416 |
l(t('Description'), $_GET["q"], array('query' => array('sort_field' => 'description', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'description', $order), |
l(t('Description'), $_GET["q"], array('query' => array('sort_field' => 'description', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'description', $order), |
| 417 |
l(t('Last Updated'), $_GET["q"], array('query' => array('sort_field' => 'created', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'created', $order),) |
l(t('Last Updated'), $_GET["q"], array('query' => array('sort_field' => 'created', 'order' => $order_toggle[$order]))). ipauth_arrow($sort_field, 'created', $order),) |
| 446 |
'#attributes' => array('class' => ($evenodd_counter%2==1) ? "even" : "odd"), |
'#attributes' => array('class' => ($evenodd_counter%2==1) ? "even" : "odd"), |
| 447 |
); |
); |
| 448 |
|
|
| 449 |
|
$form['ip_list']['ipauth_table']['row_' .$row->id]['iprange'] = array( |
| 450 |
|
'#value' => l($ip_title, "admin/user/ip_authenticator/edit/". $row->id), |
| 451 |
|
'#prefix' => '<td>', |
| 452 |
|
'#suffix' => '</td>' |
| 453 |
|
); |
| 454 |
$form['ip_list']['ipauth_table']['row_' .$row->id]['checkbox_' .$row->id] = array( |
$form['ip_list']['ipauth_table']['row_' .$row->id]['checkbox_' .$row->id] = array( |
| 455 |
'#prefix' => '<td>', |
'#prefix' => '<td>', |
| 456 |
'#suffix' => '</td>', |
'#suffix' => '</td>', |
| 457 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 458 |
'#default_value' => ($row->enabled == TRUE) ? 1 : 0, |
'#default_value' => ($row->enabled == TRUE) ? 1 : 0, |
| 459 |
); |
); |
|
|
|
|
$form['ip_list']['ipauth_table']['row_' .$row->id]['iprange'] = array( |
|
|
'#value' => l($ip_title, "admin/user/ip_authenticator/edit/". $row->id), |
|
|
'#prefix' => '<td>', |
|
|
'#suffix' => '</td>' |
|
|
); |
|
| 460 |
$form['ip_list']['ipauth_table']['row_' .$row->id]['user'] = array( |
$form['ip_list']['ipauth_table']['row_' .$row->id]['user'] = array( |
| 461 |
'#value' => $user_title, |
'#value' => $user_title, |
| 462 |
'#prefix' => '<td>', |
'#prefix' => '<td>', |
| 492 |
|
|
| 493 |
$form['ips'] = array( |
$form['ips'] = array( |
| 494 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 495 |
'#title' => t('IP address assignment') ."<br/>", |
'#title' => t('Add an IP Authenticator') ."<br/>", |
| 496 |
|
'#description' => |
| 497 |
|
/*"<div class='ipauth_ip'><b>". t("Your IP: @s", array('@s' => $_SERVER['REMOTE_ADDR'])) ."</b></div>". |
| 498 |
|
'<br /><br/>'. */ |
| 499 |
|
t('Please note that users authenticated via IP authenticator do not get permissions of the <em>authenticated user</em> role.'), |
| 500 |
'#collapsible' => FALSE, |
'#collapsible' => FALSE, |
| 501 |
'#collapsed' => FALSE, |
'#collapsed' => FALSE, |
| 502 |
'#weight' => 0 |
'#weight' => -1 |
| 503 |
); |
); |
| 504 |
$form['ips']['checkbox_enabled'] = array( |
$form['ips']['checkbox_enabled'] = array( |
| 505 |
'#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>', |
'#prefix' => '<div class="floatLeft checkbox form-item"><label>'. t('Active') .'</label>', |
| 514 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 515 |
'#title' => 'IP 1', |
'#title' => 'IP 1', |
| 516 |
'#size' => '15', |
'#size' => '15', |
| 517 |
|
'#description' => "<div class='ipauth_ip'>". t('Your IP: ') . '<a href="javascript:void(0);" class="ip">'. $_SERVER["REMOTE_ADDR"] ."</a></div>", |
| 518 |
/* '#required' => TRUE, */ |
/* '#required' => TRUE, */ |
| 519 |
'#suffix' => '</div><div class="floatLeft" id="formSeperator"> - </div>' |
'#suffix' => '</div><div class="floatLeft" id="formSeperator"> - </div>' |
| 520 |
); |
); |
| 544 |
'#title' => t('Description of Authenticator'), |
'#title' => t('Description of Authenticator'), |
| 545 |
'#description' => t("Enter a description to better identify this authenticator"), |
'#description' => t("Enter a description to better identify this authenticator"), |
| 546 |
); |
); |
| 547 |
|
$form['ips']['submit1'] = array( |
| 548 |
|
'#type' => 'submit', |
| 549 |
|
'#value' => 'Add IP Authenticator', |
| 550 |
|
'#weight' => 1, |
| 551 |
|
); |
| 552 |
|
$form['ips']['import-export1'] = array( |
| 553 |
|
'#value' => l("Import", "admin/user/ip_authenticator/data", array('attributes'=>array('class'=>'button'))), |
| 554 |
|
'#prefix' => '<div id="import-export1">', |
| 555 |
|
'#suffix' => '</div>', |
| 556 |
|
'#weight' => 1, |
| 557 |
|
); |
| 558 |
$form['#validate'][] = 'ipauth_admin_settings_validate'; |
$form['#validate'][] = 'ipauth_admin_settings_validate'; |
| 559 |
$form['#submit'][] = 'ipauth_admin_settings_submit'; |
$form['#submit'][] = 'ipauth_admin_settings_submit'; |
| 560 |
|
|
| 561 |
return system_settings_form($form); |
$form['submit'] = array( |
| 562 |
|
'#type' => 'submit', |
| 563 |
|
'#value' => 'Save Configuration', |
| 564 |
|
'#weight' => 1, |
| 565 |
|
); |
| 566 |
|
|
| 567 |
|
return $form; //system_settings_form($form); |
| 568 |
} // end function ipauth_admin_settings |
} // end function ipauth_admin_settings |
| 569 |
|
|
| 570 |
/** |
/** |
| 737 |
'#value' => $row["description"], |
'#value' => $row["description"], |
| 738 |
'#description' => t("Enter a description to better identify this authenticator"), |
'#description' => t("Enter a description to better identify this authenticator"), |
| 739 |
); |
); |
| 740 |
|
|
| 741 |
$form['id'] = array( |
$form['id'] = array( |
| 742 |
'#type' => 'hidden', |
'#type' => 'hidden', |
| 743 |
'#value' => $args[1], |
'#value' => $args[1], |
| 871 |
// For usability's sake, avoid showing two login forms on one page. |
// For usability's sake, avoid showing two login forms on one page. |
| 872 |
// In contrast to the user.module login block this block is shown when |
// In contrast to the user.module login block this block is shown when |
| 873 |
// an ip_auth authenticated user is online. |
// an ip_auth authenticated user is online. |
| 874 |
if (in_array($user->uid, ipauth_get_uids()) && !(arg(0) == 'user' && !is_numeric(arg(1)))) { |
if (in_array($user->uid, ipauth_get_uids('enabled')) && !(arg(0) == 'user' && !is_numeric(arg(1)))) { |
| 875 |
$block['subject'] = t('User login'); |
$block['subject'] = t('User login'); |
| 876 |
$block['content'] = drupal_get_form('user_login_block'); |
$block['content'] = drupal_get_form('user_login_block'); |
| 877 |
} |
} |
| 917 |
* Get an array of all uids used by ip authenticator |
* Get an array of all uids used by ip authenticator |
| 918 |
* @return array List of all uids |
* @return array List of all uids |
| 919 |
*/ |
*/ |
| 920 |
function ipauth_get_uids() { |
function ipauth_get_uids($which = 'all') { |
| 921 |
static $uids; |
static $uids; |
| 922 |
if (!isset($uids)) { |
static $which_cached; |
| 923 |
|
|
| 924 |
|
if (!isset($uids) || !isset($which_cached[$which]) ) { |
| 925 |
$uids = array(); |
$uids = array(); |
| 926 |
$sql = "SELECT DISTINCT uid FROM {ip_authenticator} WHERE 1"; |
$which_cached[$which] = TRUE; |
| 927 |
|
if ($which == 'enabled') { |
| 928 |
|
$where = 'enabled = 1'; |
| 929 |
|
} else { |
| 930 |
|
$where = '1'; |
| 931 |
|
} |
| 932 |
|
$sql = "SELECT DISTINCT uid FROM {ip_authenticator} WHERE " .$where; |
| 933 |
|
|
| 934 |
$result = db_query($sql); |
$result = db_query($sql); |
| 935 |
while ($row = db_fetch_array($result)) { |
while ($row = db_fetch_array($result)) { |
| 936 |
$uids[] = $row['uid']; |
$uids[] = $row['uid']; |
| 1026 |
$result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, UNIX_TIMESTAMP(created) as created"); |
$result = ipauth_get_ip_authenticators(NULL, "id, uid, ip1, ip2, description, enabled, UNIX_TIMESTAMP(created) as created"); |
| 1027 |
while ($row = db_fetch_array($result)) { |
while ($row = db_fetch_array($result)) { |
| 1028 |
printf('"%s","%s","%s","%s","%s","%s","%s" |
printf('"%s","%s","%s","%s","%s","%s","%s" |
| 1029 |
', $row["id"],$row["uid"],$row["ip1"],$row["ip2"],$row["description"],$row["enabled"],$row["created"]); |
', $row["id"],$row["uid"],long2ip($row["ip1"]),long2ip($row["ip2"]),$row["description"],$row["enabled"],$row["created"]); |
| 1030 |
} |
} |
| 1031 |
|
|
| 1032 |
module_invoke_all('exit', $url); |
module_invoke_all('exit', $url); |