You are here

public function HostMultipleUnbanConfirm::buildForm in http:BL 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

src/Form/HostMultipleUnbanConfirm.php, line 126

Class

HostMultipleUnbanConfirm
Provides a multiple host un-ban blacklisted confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Retrieve temporary storage.
  $this->hostInfo = $this->tempStoreFactory
    ->get('host_multiple_unban_blacklisted_confirm')
    ->get(\Drupal::currentUser()
    ->id());
  if (empty($this->hostInfo)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }

  /** @var \Drupal\httpbl\HostInterface[] $hosts */
  $hosts = $this->storage
    ->loadMultiple(array_keys($this->hostInfo));
  $items = [];

  // Prepare a list of any matching, banned IPs, so we can include the fact
  // they are already banned in the confirmation message.  Also check and
  // warn user if their own IP is in the list!
  foreach ($this->hostInfo as $id => $host_ips) {
    foreach ($host_ips as $host_ip) {
      $host = $hosts[$id];
      $host_status = $host
        ->getHostStatus();
      $key = $id . ':' . $host_ip;
      $default_key = $id . ':' . $host_ip;

      // If we have any non-blacklisted hosts, explain they will be ignored.
      if ($host_status != HTTPBL_LIST_BLACK) {

        // Check also to be certain that it is not somehow banned, anyway.
        if (!$this->banManager
          ->isBanned($host_ip)) {
          $items[$default_key] = [
            'label' => [
              '#markup' => $this
                ->t('@label - <em> is not blacklisted or banned.</em>', [
                '@label' => $host
                  ->label(),
              ]),
            ],
            'ignored hosts' => [
              '#theme' => 'item_list',
            ],
          ];
        }
        else {

          // Warn user that a "banned but not blacklisted" occurrance has
          // been found.  This should never happen unless IPs have been banned
          // through direct use of the Ban module.
          // Any "banned but not blacklisted" hosts will be un-banned, but
          // the situation warrants further attention.
          $items[$default_key] = [
            'label' => [
              '#markup' => $this
                ->t('@label - <em> is banned but not blacklisted!  Restrict access to Ban module!  This host will be un-banned.</em>', [
                '@label' => $host
                  ->label(),
              ]),
            ],
            'rescued hosts' => [
              '#theme' => 'item_list',
            ],
          ];
          $banUrl = Url::fromUri('internal:/admin/people/permissions#module-ban');
          $banUrl_options = [
            'attributes' => [
              'target' => '_blank',
            ],
          ];
          $banUrl
            ->setOptions($banUrl_options);
          $banLink = Link::fromTextAndUrl(t('review roles with access to the Ban module'), $banUrl)
            ->toString();
          $message = t('Some hosts were found <strong>banned but not blacklisted</strong>. They will be un-banned.</br>Please @ban.', [
            '@ban' => $banLink,
          ]);
          drupal_set_message($message, 'warning', FALSE);
        }
      }
      elseif (!isset($items[$default_key])) {
        $items[$key] = $host
          ->label();
      }
    }
  }
  $form['hosts'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
  $form = parent::buildForm($form, $form_state);
  return $form;
}