You are here

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

Class

HostMultipleGreylistConfirm
Provides a multiple host grey-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)) {
    $greylist_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 grey-listed
        if ($host_status != HTTPBL_LIST_GREY) {

          // Queue the host for grey-listing;
          $greylist_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 ($greylist_hosts) {
      $now = \Drupal::time()
        ->getRequestTime();
      $offset = \Drupal::state()
        ->get('httpbl.greylist_offset') ?: 86400;
      $timestamp = $now + $offset;
      foreach ($greylist_hosts as $id => $greylist_host) {
        $host = $greylist_host;
        $host
          ->setHostStatus(HTTPBL_LIST_GREY);
        $host
          ->setExpiry($timestamp);
        $host
          ->setSource(HTTPBL_ADMIN_SOURCE);
        $host
          ->save();
      }
      $greylist_count = count($greylist_hosts);
      $this->logTrapper
        ->trapNotice('Grey-listed @count hosts.', array(
        '@count' => $greylist_count,
      ));
      drupal_set_message($this
        ->formatPlural($greylist_count, 'Grey-listed 1 host.', 'Grey-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 grey-listed. There was nothing to do.', 'warning');
    }
    $this->tempStoreFactory
      ->get('host_multiple_greylist_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('entity.host.collection');
}