You are here

public function HostMultipleBanConfirm::submitForm in http:BL 8

Form submission 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 FormInterface::submitForm

File

src/Form/HostMultipleBanConfirm.php, line 234

Class

HostMultipleBanConfirm
Provides a multiple host blacklisting and banning confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Check if banning is enabled.
  $banningEnabled = TRUE ? \Drupal::state()
    ->get('httpbl.storage') == HTTPBL_DB_HH_DRUPAL : ($banningEnabled = FALSE);
  if ($form_state
    ->getValue('confirm') && !empty($this->hostInfo)) {
    $ban_hosts = [];
    $blacklist_hosts = [];

    /** @var \Drupal\httpbl\HostInterface[] $hosts */
    $hosts = $this->storage
      ->loadMultiple(array_keys($this->hostInfo));
    foreach ($this->hostInfo as $id => $host_ips) {
      foreach ($host_ips as $host_ip) {
        $host = $hosts[$id];
        if ($this->banManager
          ->isBanned($host_ip)) {

          // Only queue the host for blacklisting;
          $blacklist_hosts[$id] = $host;
        }
        elseif (!isset($ban_hosts[$id]) && $banningEnabled) {
          $ban_hosts[$id] = $host;
        }
        $blacklist_hosts[$id] = $host;
      }
    }
    if ($ban_hosts) {
      foreach ($ban_hosts as $ban_host) {
        $this->banManager
          ->banIp($ban_host
          ->getHostIp());
      }
      $this->logTrapper
        ->trapNotice('Banned @count hosts.', array(
        '@count' => count($ban_hosts),
      ));
      $banned_count = count($ban_hosts);
      drupal_set_message($this
        ->formatPlural($banned_count, 'Banned 1 host.', 'Banned @count hosts.'));
    }
    if ($blacklist_hosts) {
      $now = \Drupal::time()
        ->getRequestTime();
      $offset = \Drupal::state()
        ->get('httpbl.blacklist_offset') ?: 31536000;
      $timestamp = $now + $offset;
      foreach ($blacklist_hosts as $id => $blacklist_host) {
        $host = $blacklist_host;
        $host
          ->setHostStatus(HTTPBL_LIST_BLACK);
        $host
          ->setExpiry($timestamp);
        $host
          ->setSource(HTTPBL_ADMIN_SOURCE);
        $host
          ->save();
      }
      $blacklist_count = count($blacklist_hosts);
      $this->logTrapper
        ->trapNotice('Blacklisted @count hosts.', array(
        '@count' => $blacklist_count,
      ));
      drupal_set_message($this
        ->formatPlural($blacklist_count, 'Blacklisted 1 host.', 'Blacklisted @count hosts.'));
    }
    $this->tempStoreFactory
      ->get('host_multiple_ban_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('entity.host.collection');
}