function restrict_ip_settings_validate in Restrict IP 7
Same name and namespace in other branches
- 6 restrict_ip.module \restrict_ip_settings_validate()
- 7.2 includes/restrict_ip.pages.inc \restrict_ip_settings_validate()
Validation function for restrict_ip_settings()
This function determines whether or not the values entered in whitelisted IPs list are valid IP addresses
File
- ./
restrict_ip.module, line 76
Code
function restrict_ip_settings_validate($form, &$form_state) {
$ip_addresses = $form_state['values']['restrict_ip_address_list'];
if (strlen(trim($ip_addresses))) {
$ip_addresses = explode(PHP_EOL, trim($form_state['values']['restrict_ip_address_list']));
foreach ($ip_addresses as $ip_address) {
if ($ip_address != '::1') {
if (!preg_match('~^\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b$~', trim($ip_address))) {
$pieces = explode('-', $ip_address);
if (count($pieces) !== 2) {
form_set_error('restrict_ip_address_list', t('!ip_address is not a valid IP address.', array(
'!ip_address' => $ip_address,
)));
}
else {
$ip1 = trim($pieces[0]);
$ip2 = trim($pieces[1]);
$both_valid = TRUE;
if (!preg_match('~^\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b$~', $ip1)) {
form_set_error('restrict_ip_address_list', t('!ip_address is not a valid IP address.', array(
'!ip_address' => $ip1,
)));
$both_valid = FALSE;
}
if (!preg_match('~^\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b$~', $ip2)) {
form_set_error('restrict_ip_address_list', t('!ip_address is not a valid IP address.', array(
'!ip_address' => $ip2,
)));
$both_valid = FALSE;
}
if ($both_valid) {
$first_parts_equal = TRUE;
$last_part_ok = TRUE;
$ip1_pieces = explode('.', $ip1);
$ip2_pieces = explode('.', $ip2);
for ($i = 0; $i < 3; $i++) {
if ($ip1_pieces[$i] != $ip2_pieces[$i]) {
$first_parts_equal = FALSE;
break;
}
}
if ($first_parts_equal) {
if ($ip2_pieces[3] <= $ip1_pieces[3]) {
$last_part_ok = FALSE;
}
}
if (!$first_parts_equal || !$last_part_ok) {
form_set_error('restrict_ip_address_list', t('@ip_address is not a valid range of IP addresses.', array(
'@ip_address' => $ip_address,
)));
}
}
}
}
}
}
}
}