public function ConfigForm::validateForm in Restrict IP 8
Same name and namespace in other branches
- 8.2 src/Form/ConfigForm.php \Drupal\restrict_ip\Form\ConfigForm::validateForm()
- 3.x src/Form/ConfigForm.php \Drupal\restrict_ip\Form\ConfigForm::validateForm()
Form validation handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormBase::validateForm
File
- src/
Form/ ConfigForm.php, line 239
Class
Namespace
Drupal\restrict_ip\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
$ip_addresses = $this->restrictIpService
->cleanIpAddressInput($form_state
->getValue('address_list'));
if (count($ip_addresses)) {
foreach ($ip_addresses as $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, so we need to check if it's a range of addresses
$pieces = explode('-', $ip_address);
// We only need to 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_state
->setError($form['restrict_ip_address_list'], $this
->t('@ip_address is not a valid IP address.', [
'@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_state
->setError($form['restrict_ip_address_list'], $this
->t('@range is not a valid IP address range.', [
'@range' => $ip_address,
]));
}
}
}
else {
$form_state
->setError($form['restrict_ip_address_list'], $this
->t('@ip_address is not a valid IP address or range of addresses.', [
'@ip_address' => $ip_address,
]));
}
}
}
}
}
$page_whitelist = $form_state
->getValue('page_whitelist');
$page_whitelist = trim($page_whitelist);
if (strlen($page_whitelist)) {
$pages = [];
$paths = explode(PHP_EOL, $page_whitelist);
foreach ($paths as $path) {
$path = trim($path);
if (strlen($path)) {
if (!preg_match('/^\\//', $path)) {
$path = '/' . $path;
}
$pages[] = strtolower($path);
}
}
$form_state
->setValue('page_whitelist', $pages);
}
else {
$form_state
->setValue('page_whitelist', []);
}
$page_blacklist = $form_state
->getValue('page_blacklist');
$page_blacklist = trim($page_blacklist);
if (strlen($page_blacklist)) {
$pages = [];
$paths = explode(PHP_EOL, $page_blacklist);
foreach ($paths as $path) {
$path = trim($path);
if (strlen($path)) {
if (!preg_match('/^\\//', $path)) {
$path = '/' . $path;
}
$pages[] = strtolower($path);
}
}
$form_state
->setValue('page_blacklist', $pages);
}
else {
$form_state
->setValue('page_blacklist', []);
}
}