| 1 |
<?php |
<?php |
| 2 |
// $Id: ipauth.admin.inc,v 1.5 2008/10/17 14:57:42 jonfrancisskydiver Exp $ |
drupal_add_css(drupal_get_path("module", "ipauth") ."/css/ipauth.admin.css"); |
| 3 |
// Modified 2008/09/15 antonio.spadial |
drupal_add_js(drupal_get_path("module", "ipauth") ."/js/ipauth.admin.js"); |
|
// Modified 2008/10/17 jonfrancisskydiver |
|
|
|
|
|
/*********************************************************************************** |
|
|
* C H A N G E S |
|
|
* 1. second if statement in function ipauth_validate(...) used $form_values instead of $form_state |
|
|
* |
|
|
***********************************************************************************/ |
|
|
|
|
|
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"); |
|
|
|
|
|
|
|
|
/** |
|
|
* administer the ip_authenticator parameters. |
|
|
*/ |
|
|
function ipauth_admin($form_state) { |
|
|
global $user; |
|
| 4 |
|
|
|
$form['ip_list'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('Current IP Authenticators') ."<br/>", |
|
|
'#description' => t('Click on a link to modify the authenticator assignment. Your IP: !s', array('!s' => $_SERVER['REMOTE_ADDR'])), |
|
|
'#collapsible' => FALSE, |
|
|
'#callapsed' => FALSE, |
|
|
'#weight' => 0 |
|
|
); |
|
|
|
|
|
$roles = user_roles(); |
|
|
$result = _get_ip_authenticators(null, "id, roles, ip1, ip2, description"); |
|
|
$i=0; |
|
|
while ($row = db_fetch_array($result)) { |
|
|
|
|
|
$row["ip1"] = long2ip($row["ip1"]); //this takes the mysql command, INET_NTOA out of the query string. |
|
|
|
|
|
if (0 == $row["ip2"]) |
|
|
$row["ip2"] = ""; |
|
|
else |
|
|
$row["ip2"] = long2ip($row["ip2"]); |
|
|
|
|
|
$ip2_text = ($row["ip2"]!="") ? " - " . $row["ip2"] : ""; |
|
|
$title = $row['ip1'] . $ip2_text ." authenticates to: ". $roles[$row["roles"]]; |
|
|
|
|
|
if ("" != trim($row["description"])) { |
|
|
$title .= "; Description: ". $row["description"]; |
|
|
} |
|
|
|
|
|
$form['ip_list'][$row["id"]] = array( |
|
|
'#prefix' => '<div>', |
|
|
'#suffix' => '</div>', |
|
|
'#value' => l($title, "admin/user/ip_authenticator/edit/". $row["id"]) |
|
|
); |
|
|
} |
|
|
|
|
|
$form['ips'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('IP address assignment. Your IP: !s', array('!s' => $_SERVER['REMOTE_ADDR'])), |
|
|
'#collapsible' => FALSE, |
|
|
'#callapsed' => FALSE, |
|
|
'#weight' => 0 |
|
|
); |
|
|
|
|
|
$form['ips']['ip1'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'textfield', |
|
|
'#title' => 'IP 1', |
|
|
'#size' => '15', |
|
|
'#required' => TRUE, |
|
|
'#suffix' => '</div>' |
|
|
); |
|
|
$form['ips']['seperator'] = array( |
|
|
'#prefix' => '<div class="floatLeft" id="formSeperator">', |
|
|
'#value' => ' - ', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['ip2'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'textfield', |
|
|
'#title' => 'IP 2', |
|
|
'#description' => 'Leave blank for<br/>individual IPs', |
|
|
'#size' => '15', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['seperator1'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#value' => ' ', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['role'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'select', |
|
|
'#title' => t('Assign to Role'), |
|
|
'#options' => $roles, |
|
|
'#description' => "Select a role to assign the ips", |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['seperator2'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#value' => ' ', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['description'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Description'), |
|
|
'#description' => "Provides a descriptive name of the authenticator", |
|
|
'#maxlength' => '255', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
|
|
|
$form['#validate'][] = 'ipauth_validate'; |
|
|
$form['#submit'][] = 'ipauth_submit'; |
|
|
|
|
|
return system_settings_form($form); |
|
|
} |
|
|
|
|
|
function ipauth_validate($form, &$form_state) { |
|
|
|
|
|
if (!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/x", $form_state['values']['ip1'])) { |
|
|
form_set_error('ip1', "You must enter an ip address in the first field."); |
|
|
} |
|
|
|
|
|
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'])) ) { |
|
|
form_set_error('ip2', "You must enter a valid IP address in the second field, or leave the second field blank."); |
|
|
} |
|
|
} |
|
|
|
|
|
function ipauth_submit($form, &$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['values']['ip2'])) ) |
|
|
$ip2 = trim($form_values["values"]["ip2"]); |
|
|
else |
|
|
$ip2 = sprintf("%u", ip2long(trim($form_values["values"]['ip2']))); |
|
|
|
|
|
$sql = "INSERT INTO {ip_authenticator} (ip1, ip2, roles, description) VALUES ('%s','%s','%s','%s')"; |
|
|
|
|
|
db_query($sql, sprintf("%u", ip2long(trim($form_values['values']['ip1']))), $ip2, $form_values['values']['role'], $form_values["values"]["description"]); |
|
|
|
|
|
} |
|
|
|
|
|
function _get_ip_role_info($id) { |
|
|
|
|
|
$result = db_query("SELECT roles, ip1, ip2, description FROM {ip_authenticator} WHERE id = '%s'", $id); |
|
|
$row = db_fetch_array($result); |
|
|
$row["ip1"] = long2ip($row["ip1"]); |
|
|
|
|
|
if ($row["ip2"] == 0) |
|
|
$row["ip2"] = ""; |
|
|
else |
|
|
$row["ip2"] = long2ip($row["ip2"]); |
|
|
|
|
|
return $row; |
|
|
} |
|
|
|
|
|
function ipauth_admin_edit($arg, $arg1, $arg3) { |
|
|
|
|
|
if (!preg_match("/^[0-9]+\$/", $arg3)) { |
|
|
drupal_set_message("Please select your authenticator again"); |
|
|
header("Location: /admin/user/ip_authenticator"); |
|
|
exit; |
|
|
} |
|
|
|
|
|
$row = _get_ip_role_info($arg3); |
|
|
|
|
|
$form['ips'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('IP address assignment'), |
|
|
'#collapsible' => FALSE, |
|
|
'#callapsed' => FALSE, |
|
|
'#weight' => 0 |
|
|
); |
|
|
|
|
|
$form['ips']['ip1'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'textfield', |
|
|
'#title' => 'IP 1', |
|
|
'#size' => '15', |
|
|
'#default_value' => $row["ip1"], |
|
|
'#required' => TRUE, |
|
|
'#suffix' => '</div>' |
|
|
); |
|
|
$form['ips']['seperator'] = array( |
|
|
'#prefix' => '<div class="floatLeft" id="formSeperator">', |
|
|
'#value' => ' - ', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['ip2'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'textfield', |
|
|
'#title' => 'IP 2', |
|
|
'#description' => 'Leave blank for<br/>individual IPs', |
|
|
'#size' => '15', |
|
|
'#default_value' => $row["ip2"], |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['seperator1'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#value' => ' ', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['role'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'select', |
|
|
'#title' => t('Assign to Role'), |
|
|
'#options' => user_roles(), |
|
|
'#default_value' => $row["roles"], |
|
|
'#description' => "Select a role to assign<Br/> the ips", |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['seperator2'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#value' => ' ', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['ips']['description'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Description'), |
|
|
'#description' => "Provides a descriptive name<br/> of the authenticator", |
|
|
'#maxlength' => '255', |
|
|
'#size' => 25, |
|
|
'#suffix' => '</div>', |
|
|
'#default_value' => $row["description"], |
|
|
); |
|
|
$form['ips']['delete'] = array( |
|
|
'#prefix' => '<div class="floatLeft" id="deleteLink">', |
|
|
'#value' => ' '. l("delete", "admin/user/ip_authenticator/delete/" . $arg3), |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['id'] = array( |
|
|
'#type' => 'hidden', |
|
|
'#value' => $arg3 |
|
|
); |
|
|
$form['#validate'][] = 'ipauth_validate'; |
|
|
$form['#submit'][] = 'ipauth_edit_submit'; |
|
|
$form['#redirect'] = 'admin/user/ip_authenticator'; |
|
|
|
|
|
return system_settings_form($form); |
|
|
} |
|
|
|
|
|
|
|
|
function ipauth_edit_submit($form, &$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['values']['ip2'])) ) |
|
|
$ip2_long = trim($form_values['values']['ip2']); |
|
|
else |
|
|
$ip2_long = sprintf("%u", ip2long(trim($form_values['values']['ip2']))); |
|
|
|
|
|
$sql = "UPDATE {ip_authenticator} SET ip1 = '%s', ip2 = '%s', roles = '%s', description = '%s' WHERE id = '%s'"; |
|
|
|
|
|
db_query($sql, sprintf("%u", ip2long(trim($form_values['values']['ip1']))), $ip2_long, $form_values['values']['role'], $form_values['values']['description'], $form_values['values']['id']); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function ipauth_admin_delete($arg, $arg1, $arg3) { |
|
|
|
|
|
if (!preg_match("/^[0-9]+\$/", $arg3)) { |
|
|
drupal_set_message("Please select your authenticator again"); |
|
|
header("Location: /admin/user/ip_authenticator"); |
|
|
exit; |
|
|
} |
|
|
|
|
|
$row = _get_ip_role_info($arg3); |
|
|
$roles = user_roles(); |
|
|
|
|
|
$ip2_text = ($row["ip2"] != "") ? " - ". $row["ip2"] : ""; |
|
|
$title = "<li>" . $row['ip1'] . $ip2_text ." authenticates to: ". $roles[$row["roles"]]; |
|
|
|
|
|
if ("" != trim($row["description"])) { |
|
|
$title .= "; Description: ". $row["description"]; |
|
|
} |
|
|
|
|
|
$form["markup"] = array( |
|
|
'#prefix' => "<div id='confirmbox'>", |
|
|
'#suffix' => "", |
|
|
'#value' => "Are you certain that you wish to delete this ip authenticator? <ul>". $title ."</ul>" |
|
|
); |
|
|
|
|
|
$form["accept"] = array( |
|
|
'#type' => 'submit', |
|
|
'#value' => 'Delete', |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#suffix' => '</div>' |
|
|
); |
|
|
$form['seperator1'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#value' => ' ', |
|
|
'#suffix' => '</div>', |
|
|
); |
|
|
$form['cancel'] = array( |
|
|
'#prefix' => '<div class="floatLeft">', |
|
|
'#value' => '<input type="button" value="Cancel" id="cancel" class="form-submit" />', |
|
|
'#suffix' => '</div></div>' |
|
|
); |
|
|
$form['id'] = array( |
|
|
'#type' => 'hidden', |
|
|
'#value' => $arg3 |
|
|
); |
|
|
|
|
|
$form['#submit'][] = 'ipauth_admin_delete_submit'; |
|
|
$form['#redirect'] = 'admin/user/ip_authenticator'; |
|
|
$form['#validate'][] = 'ip_authentiator_admin_delete_validate'; |
|
|
return $form; |
|
|
} |
|
|
|
|
|
function ip_authentiator_admin_delete_validate($form, &$form_values) { |
|
|
if (!preg_match("/^[0-9]+\$/", $form_values["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."); |
|
|
} |
|
|
|
|
|
function ipauth_admin_delete_submit($form, &$form_values) { |
|
|
$result = db_query("DELETE FROM {ip_authenticator} WHERE id = '%s'", $form_values['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."); |
|
|
} |
|
|
} |
|
| 5 |
|
?> |