You are here

public function IPRangesFormController::validate in IP Ranges 8

File

lib/Drupal/ip_ranges/IPRangesFormController.php, line 112

Class

IPRangesFormController

Namespace

Drupal\ip_ranges

Code

public function validate(array $form, array &$form_state) {
  $ip_lower = trim($form_state['values']['ip_lower']);
  $ip_higher = isset($form_state['values']['ip_higher']) ? trim($form_state['values']['ip_higher']) : NULL;
  $type = $form_state['values']['type'];
  if (filter_var($ip_lower, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
    \Drupal::formBuilder()
      ->setErrorByName('ip_lower', $form_state, t("IP(/range start) is not a valid IP address."));
  }
  elseif ($ip_higher && filter_var($ip_higher, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
    \Drupal::formBuilder()
      ->setErrorByName('ip_higher', $form_state, t("IP range end is not a valid IP address."));
  }
  elseif (ip2long($ip_lower) <= ip2long($this->ownIp) && ip2long($ip_higher) >= ip2long($this->ownIp) || ip2long($ip_lower) == ip2long($this->ownIp) || ip2long($ip_higher) == ip2long($this->ownIp)) {

    // Own ip cannot be blacklisted, but whitelisting is ok.
    if ($type == 0) {
      \Drupal::formBuilder()
        ->setErrorByName('', $form_state, t("You may not block your own IP address"));
    }
  }
  parent::validate($form, $form_state);
}