You are here

public function HttpblConfigForm::validateForm in http:BL 8

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/HttpblConfigForm.php, line 262

Class

HttpblConfigForm
Defines a form that configures httpbl settings.

Namespace

Drupal\httpbl\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $key = $values['httpbl_accesskey'];
  if ($values['httpbl_check'] && !$key) {
    $form_state
      ->setErrorByName('httpbl_accesskey', $this
      ->t('You must enter a valid access key to enable blacklist checks.'));
  }
  if ($values['httpbl_footer'] && !$values['httpbl_link']) {
    $form_state
      ->setErrorByName('httpbl_link', $this
      ->t('You must enter a link to be able to add it to the page bottom.'));
  }
  if ($values['httpbl_check'] > HTTPBL_CHECK_NONE && $key) {

    // Key should be 12 lowercase alpha characters.
    // There's no unicode allowed, so we're not using drupal_strlen().
    // ereg is deprecated.  Now using preg_grep instead?
    if (preg_grep('/[^a-z]/', array(
      $key,
    )) || strlen($key) != 12) {
      $form_state
        ->setErrorByName('httpbl_accesskey', $this
        ->t('Your access key is formatted incorrectly.'));
    }
    elseif (!count($form_state
      ->getErrors())) {

      // Do a test lookup (with known result).
      // Not sure we are really testing a valid key?
      $evaluator = \Drupal::service('httpbl.evaluator');
      $lookup = $evaluator
        ->httpbl_dnslookup('127.1.80.1', $key);
      if (!$lookup || $lookup['threat'] != 80) {
        $form_state
          ->setErrorByName('httpbl_accesskey', $this
          ->t('Testcase failed. This either means that your access key is incorrect or that there is a problem in your DNS system.'));
      }
      else {
        drupal_set_message(t('Http:BL tested access to Project Honeypot and completed successfully.'));
      }
    }
  }

  // If Auto-ban storage selected, ensure there is a Ban Service.
  if ($values['httpbl_storage'] == HTTPBL_DB_HH_DRUPAL && !\Drupal::hasService('ban.ip_manager')) {
    \Drupal::service('module_installer')
      ->install([
      'ban',
    ]);
  }
  if ($values['httpbl_check'] > HTTPBL_CHECK_NONE && $values['httpbl_storage'] == HTTPBL_DB_HH_DRUPAL && \Drupal::hasService('ban.ip_manager')) {
    drupal_set_message(t('Auto-banning is enabled!'));
  }

  // Set error message if configured for page checking and Internal Page
  // Cache service is detected.
  if ($values['httpbl_check'] == HTTPBL_CHECK_ALL && \Drupal::hasService('http_middleware.page_cache')) {
    drupal_set_message(t('Core extension Internal Page Cache (page_cache) has been detected. Using HttpBL for evaluating page requests is allowed but NOT RECOMMENDED when Internal Page Cache is enabled.'), 'error');
  }
}