You are here

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

Class

HostMultipleWhitelistConfirm
Provides a multiple host white-listing (and un-banning) confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('confirm') && !empty($this->hostInfo)) {
    $whitelist_hosts = [];
    $unban_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];
        $host_status = $host
          ->getHostStatus();

        // If this host is banned...
        if ($this->banManager
          ->isBanned($host_ip)) {

          // Queue the host for un-banning;
          $unban_hosts[$id] = $host;
        }

        // If this host is not already white-listed
        if ($host_status != HTTPBL_LIST_SAFE) {

          // Queue the host for white-listing;
          $whitelist_hosts[$id] = $host;
        }
      }
    }
    if ($unban_hosts) {
      foreach ($unban_hosts as $unban_host) {
        $this->banManager
          ->unbanIp($unban_host
          ->getHostIp());
      }
      $this->logTrapper
        ->trapNotice('Un-banned @count hosts.', array(
        '@count' => count($unban_hosts),
      ));
      $unbanned_count = count($unban_hosts);
      drupal_set_message($this
        ->formatPlural($unbanned_count, 'Un-banned 1 host.', 'Un-banned @count hosts.'));
    }
    if ($whitelist_hosts) {
      $now = \Drupal::time()
        ->getRequestTime();
      $offset = \Drupal::state()
        ->get('httpbl.safe_offset') ?: 10800;
      $timestamp = $now + $offset;
      foreach ($whitelist_hosts as $id => $whitelist_host) {
        $host = $whitelist_host;
        $host
          ->setHostStatus(HTTPBL_LIST_SAFE);
        $host
          ->setExpiry($timestamp);
        $host
          ->setSource(HTTPBL_ADMIN_SOURCE);
        $host
          ->save();
      }
      $whitelist_count = count($whitelist_hosts);
      $this->logTrapper
        ->trapNotice('White-listed @count hosts.', array(
        '@count' => $whitelist_count,
      ));
      drupal_set_message($this
        ->formatPlural($whitelist_count, 'White-listed 1 host.', 'White-listed @count hosts.'));
    }
    else {

      // Let user know if there was nothing to do.
      drupal_set_message('No hosts were found banned.  One or more were found already white-listed. There was nothing to do.', 'warning');
    }
    $this->tempStoreFactory
      ->get('host_multiple_whitelist_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('entity.host.collection');
}