You are here

public function AutobanAnalyzeForm::buildForm in Automatic IP ban (Autoban) 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 FormInterface::buildForm

File

src/Form/AutobanAnalyzeForm.php, line 80

Class

AutobanAnalyzeForm
Analyze watchdog entries for IP addresses for ban.

Namespace

Drupal\autoban\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $rows = [];
  $header = [
    [
      'data' => $this
        ->t('Count'),
      'field' => 'cnt',
      'sort' => 'desc',
    ],
    [
      'data' => $this
        ->t('Type'),
      'field' => 'type',
    ],
    [
      'data' => $this
        ->t('Message'),
      'field' => 'message',
    ],
    $this
      ->t('Operations'),
  ];

  // Run analyze query.
  $threshold_analyze = $this
    ->config('autoban.settings')
    ->get('autoban_threshold_analyze') ?: 5;
  $dblog_type_exclude = $this
    ->config('autoban.settings')
    ->get('autoban_dblog_type_exclude') ?: "autoban\ncron\nphp\nsystem\nuser";
  $dblog_type_exclude_msg = implode(', ', explode("\n", $dblog_type_exclude));
  $result = $this
    ->getAnalyzeResult($header, $threshold_analyze, $dblog_type_exclude);
  if (count($result)) {
    $destination = $this
      ->getDestinationArray();
    $url_destination = $destination['destination'];
    foreach ($result as $item) {
      $message = $this
        ->formatMessage($item);
      $message = Unicode::truncate(Html::decodeEntities(strip_tags($message)), 256, TRUE, TRUE);
      $row = [
        $item->cnt,
        $item->type,
        $message,
      ];
      $links = [];
      $query = [
        'query' => [
          'type' => $item->type,
          'message' => Html::escape($message),
          'destination' => $url_destination,
        ],
      ];
      $links['add_rule'] = [
        'title' => $this
          ->t('Add rule'),
        'url' => Url::fromRoute('entity.autoban.add_form', [], $query),
      ];
      $links['test'] = [
        'title' => $this
          ->t('Test'),
        'url' => Url::fromRoute('autoban.test', [
          'rule' => AutobanUtils::AUTOBAN_FROM_ANALYZE,
        ], $query),
      ];
      $row[] = [
        'data' => [
          '#type' => 'operations',
          '#links' => $links,
        ],
      ];
      $rows[] = $row;
    }
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Bulk add rules'),
      '#suffix' => '<div><small>' . $this
        ->t('Automatic creation autoban rules for checked rows.') . '</small></div>',
    ];
  }
  $form['info'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Settings info'),
    '#open' => FALSE,
  ];
  $form['info']['title'] = [
    '#markup' => $this
      ->t('<label>Threshold:</label> @threshold <label>Exclude types:</label> @exclude', [
      '@threshold' => $threshold_analyze,
      '@exclude' => $dblog_type_exclude_msg,
    ]),
    '#allowed_tags' => [
      'label',
    ],
  ];
  $form['analyze_table'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => $this
      ->t('No data for ban.'),
    '#weight' => 120,
  ];
  return $form;
}