function restrict_ip_settings_validate in Restrict IP 7.2
Same name and namespace in other branches
- 6 restrict_ip.module \restrict_ip_settings_validate()
- 7 restrict_ip.module \restrict_ip_settings_validate()
Validation handler for restrict_ip_settings().
Determines whether or not the values entered in whitelisted IPs list are valid IP addresses.
File
- includes/
restrict_ip.pages.inc, line 186 - Holds page callbacks for the Restrict IP module.
Code
function restrict_ip_settings_validate($form, &$form_state) {
$ip_addresses = restrict_ip_sanitize_ip_list($form_state['values']['restrict_ip_address_list']);
if (count($ip_addresses)) {
foreach ($ip_addresses as $ip_address) {
$ip_address = trim($ip_address);
if ($ip_address != '::1') {
// Check if IP address is a valid singular IP address
// (ie - not a range).
if (!preg_match('~^(?:(?: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]?)$~', $ip_address) && !preg_match('~^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$~', $ip_address)) {
// IP address is not a single IP address, scheck if
// it's a range of addresses.
$pieces = explode('-', $ip_address);
// Only continue checking this IP address if .it is a range of
// addresses.
if (count($pieces) == 2) {
$start_ip = trim($pieces[0]);
if (!preg_match('~^(?:(?: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]?)$~', $start_ip)) {
form_set_error('restrict_ip_address_list', t('@ip_address is not a valid IP address.', array(
'@ip_address' => $start_ip,
)));
}
else {
$start_pieces = explode('.', $start_ip);
$start_final_chunk = (int) array_pop($start_pieces);
$end_ip = trim($pieces[1]);
$end_valid = TRUE;
if (preg_match('~^(?:(?: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]?)$~', $end_ip)) {
$end_valid = TRUE;
$end_pieces = explode('.', $end_ip);
for ($i = 0; $i < 3; $i++) {
if ((int) $start_pieces[$i] != (int) $end_pieces[$i]) {
$end_valid = FALSE;
}
}
if ($end_valid) {
$end_final_chunk = (int) array_pop($end_pieces);
if ($start_final_chunk > $end_final_chunk) {
$end_valid = FALSE;
}
}
}
elseif (!is_numeric($end_ip)) {
$end_valid = FALSE;
}
else {
if ($end_ip > 255) {
$end_valid = FALSE;
}
else {
$start_final_chunk = array_pop($start_pieces);
if ($start_final_chunk > $end_ip) {
$end_valid = FALSE;
}
}
}
if (!$end_valid) {
form_set_error('restrict_ip_address_list', t('@range is not a valid IP address range.', array(
'@range' => $ip_address,
)));
}
}
}
else {
form_set_error('restrict_ip_address_list', t('!ip_address is not a valid IP address or range of addresses.', array(
'!ip_address' => $ip_address,
)));
}
}
}
}
}
$countries = array();
foreach ($form_state['values']['restrict_ip_country_list'] as $country) {
if ($country) {
$countries[] = $country;
}
}
form_set_value($form['restrict_ip_country_list'], $countries, $form_state);
}