function _services_ipauth_ips_validate in Services IP Authentication 7
Same name and namespace in other branches
- 6 services_ipauth.inc \_services_ipauth_ips_validate()
#element_validate callback to validate IP addresses (one per line).
See also
_services_ipauth_security_settings()
1 string reference to '_services_ipauth_ips_validate'
- _services_ipauth_security_settings in ./
services_ipauth.inc - Security settings form.
File
- ./
services_ipauth.inc, line 83 - Provides callbacks for the IP Authentication module.
Code
function _services_ipauth_ips_validate($element, &$form_state) {
// Take user input.
$ip_addresses_input = $element['#value'];
// Convert user input to an associative array with non-empty unique values.
$ip_addresses = drupal_map_assoc(array_filter(array_map('trim', array_unique(explode("\n", $ip_addresses_input)))));
// Definition array of filter_var_array() can not have numeric keys. Change
// the key of the numeric values so that they are interpreted as a string.
foreach ($ip_addresses as $key => $address) {
if (is_numeric($key)) {
unset($ip_addresses[$key]);
$ip_addresses[$key . 'x'] = $address;
}
}
// Create the definition array for filter_var_array().
$definition = array_combine(array_keys($ip_addresses), array_fill(0, count($ip_addresses), array(
'filter' => FILTER_VALIDATE_IP,
)));
// filter_var_array() returns FALSE on failure, or an array with a result for
// each key of the filtered array. If the result does not contain FALSE, all
// elements of the array passed validation. array_filter() filters out values
// that, when converted to boolean, equal FALSE. So if the filtered result
// equals the normal result, all elements passed validation.
if (!($result = filter_var_array($ip_addresses, $definition)) || array_filter($result) != $result) {
$invalids = array_diff_key($ip_addresses, array_filter($result));
foreach ($invalids as $key => $invalid) {
if (services_ipauth_is_valid_ip_range($invalid)) {
unset($invalids[$key]);
}
}
if (!empty($invalids)) {
form_error($element, t('Incorrect IP address format for following entries: @list', array(
'@list' => theme('item_list', $invalids),
)));
}
}
// Set the value to an array, this way it is easier to work with the value
// that will be stored in the $settings array.
form_set_value($element, array_values($ip_addresses), $form_state);
}