You are here

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

Class

HostMultipleBanConfirm
Provides a multiple host blacklisting and banning confirmation form.

Namespace

Drupal\httpbl\Form

Code

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

  // Prepare default confirm text.
  $this
    ->setConfirmText();

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

  /** @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];
      $key = $id . ':' . $host_ip;
      $default_key = $id . ':' . $host_ip;

      // Check if current user's IP is in the list...
      if ($this_user == $host_ip) {

        // Alter confirm button.
        $this
          ->setConfirmText($userInList = TRUE);
        $this
          ->getConfirmText();

        // Theme a warning in the item list.
        $items[$default_key] = [
          'label' => [
            '#markup' => $this
              ->t('<strong>Your IP (@label) is in this list! YOU ARE ABOUT TO BLACKLIST YOURSELF!</strong>', [
              '@label' => $host
                ->label(),
            ]),
          ],
          'me' => [
            '#theme' => 'item_list',
          ],
        ];
      }

      // If we have any already banned hosts, we theme up some extra warning about each
      // of them.
      if ($this->banManager
        ->isBanned($host_ip)) {
        $items[$default_key] = [
          'label' => [
            '#markup' => $this
              ->t('@label - <em> Is already banned.</em>', [
              '@label' => $host
                ->label(),
            ]),
          ],
          'ban hosts' => [
            '#theme' => 'item_list',
          ],
        ];
      }
      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;
}