You are here

public function HttpblWhitelistForm::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/HttpblWhitelistForm.php, line 119

Class

HttpblWhitelistForm
Displays banned IP addresses.

Namespace

Drupal\httpbl\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $ip = $this
    ->getRequest()
    ->getClientIP();
  $project_link = $this->httpblEvaluator
    ->projectLink($ip);
  $values = $form_state
    ->getValues();

  // If the forbidden areas have any value, visitor has failed the challenge.
  if ($values['block'] || $values['leave']) {

    // Kill any white-listed session for this visitor.
    if (isset($_SESSION['httpbl_status'])) {
      unset($_SESSION['httpbl_status']);
    }

    // If we are storing visitor lookup results...
    if (\Drupal::state()
      ->get('httpbl.storage') == HTTPBL_DB_HH || \Drupal::state()
      ->get('httpbl.storage') == HTTPBL_DB_HH_DRUPAL) {

      // Update them from greylisted to blacklisted (they will also be auto-
      // banned or not, per configuration option).
      $this->httpblEvaluator
        ->updateIpLocalStatus($ip, HTTPBL_LIST_BLACK, $offset = \Drupal::state()
        ->get('httpbl.blacklist_offset'));
      \Drupal::state()
        ->get('httpbl.blacklist_offset');

      // Get the blacklist date offset and prepare a readable date interval for
      // a message to user.
      $offset = \Drupal::state()
        ->get('httpbl.blacklist_offset');
      $return_date = \Drupal::service('date.formatter')
        ->formatInterval($offset, $granularity = 2, $langcode = NULL);

      // Log the visitor failure.

      //$this->logTrapper->trapNotice('@ip blacklisted for @return_date for failing session white-list challenge.', ['@ip' => $ip, '@return_date' => $return_date]);
      $this->logTrapper
        ->trapNotice('@ip blacklisted for @return_date for failing session white-list challenge. Source: @source.', [
        '@ip' => $ip,
        '@return_date' => $return_date,
        '@source' => HTTPBL_CHALLENGE_FAILURE,
        'link' => $project_link,
      ]);

      // Build failed/blacklisted response to visitor.  It will say how long
      // they've been blacklisted for (the configured amount of time).
      $failureResponse = $this->httpblResponse
        ->challengeFailureBlacklisted($ip, $return_date);
      print $failureResponse;

      // Buh-bye!
      exit;
    }
    else {

      // Not storing visitor lookups.  Visitor will remain greylisted (per
      // Project Honepot results) and in white-list challenge purgatory. So,
      // simply inform them of the failure.
      $this->logTrapper
        ->trapWarning('@ip failed session white-list request.  Source: @source.', [
        '@ip' => $ip,
        '@source' => HTTPBL_CHALLENGE_FAILURE,
        'link' => $project_link,
      ]);
      $failureResponse = $this->httpblResponse
        ->challengeFailurePurgatory();
      print $failureResponse;
      exit;
    }
  }

  // This challenge was a success.  Visitor will be session white-listed on submit.
  $this->logTrapper
    ->trapNotice('@ip success at white-list challenge. Source: @source.', [
    '@ip' => $ip,
    '@source' => HTTPBL_CHALLENGE_SUCCESS,
    'link' => $project_link,
  ]);
}