You are here

public function AutobanAnalyzeForm::submitForm in Automatic IP ban (Autoban) 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/AutobanAnalyzeForm.php, line 167

Class

AutobanAnalyzeForm
Analyze watchdog entries for IP addresses for ban.

Namespace

Drupal\autoban\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $rules = [];
  foreach ($form_state
    ->getValue('analyze_table') as $key => $value) {
    if ($value != NULL) {
      $rules[$key] = [
        'type' => $form['analyze_table']['#options'][$key][1],
        'message' => $form['analyze_table']['#options'][$key][2],
      ];
    }
  }
  if (!empty($rules)) {

    // Providers list.
    $providers = [];
    $controller = $this->autoban;
    $banManagerList = $controller
      ->getBanProvidersList();
    if (!empty($banManagerList)) {
      foreach ($banManagerList as $id => $item) {
        $providers[$id] = $item['name'];
      }
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('List ban providers is empty. You have to enable at least one Autoban providers module.'), 'warning');
      return;
    }
    $provider = NULL;
    if (count($providers) == 1) {
      $provider = array_keys($providers)[0];
    }
    else {
      $last_provider = $this
        ->config('autoban.settings')
        ->get('autoban_provider_last');
      if ($last_provider && isset($providers[$last_provider])) {
        $provider = $last_provider;
      }
    }
    if (empty($provider)) {
      $provider = array_keys($providers)[0];
    }

    // Threshold.
    $threshold = $this
      ->config('autoban.settings')
      ->get('autoban_threshold_analyze') ?: 5;
    $thresholds_config = $this
      ->config('autoban.settings')
      ->get('autoban_thresholds');
    $thresholds = !empty($thresholds_config) ? explode("\n", $thresholds_config) : [
      1,
      2,
      3,
      5,
      10,
      20,
      50,
      100,
    ];
    if (!in_array($threshold, $thresholds)) {
      $threshold = max(array_filter($thresholds, function ($v) use ($threshold) {
        return $v < $threshold;
      }));
    }

    // Create automatic rules.
    foreach ($rules as $key => $value) {
      $value['provider'] = $provider;
      $value['threshold'] = $threshold;
      $value['id'] = "a_" . uniqid();
      $value['rule_type'] = AutobanUtils::AUTOBAN_RULE_AUTO;
      $autoban = $this->entityTypeManager
        ->getStorage('autoban')
        ->create($value);
      $autoban
        ->save();
      $this
        ->messenger()
        ->addMessage($this
        ->t('Create rule %label', [
        '%label' => $autoban
          ->id(),
      ]));
    }
    $this
      ->messenger()
      ->addMessage($this
      ->t('Created rules: @count', [
      '@count' => count($rules),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('No rules for generate'), 'warning');
  }
}