You are here

public function SpambotUserspamForm::checkSubmit in Spambot 8

Function provide functional for "Check" button.

Parameters

\Drupal\user\UserInterface|null $account: Account who will checked.

\Drupal\Core\Config\ImmutableConfig|null $config: Config for get white list ip.

1 call to SpambotUserspamForm::checkSubmit()
SpambotUserspamForm::submitForm in src/Form/SpambotUserspamForm.php

File

src/Form/SpambotUserspamForm.php, line 320

Class

SpambotUserspamForm
Settings form to save the configuration for Spambot.

Namespace

Drupal\spambot\Form

Code

public function checkSubmit($account, $config) {
  $messages = [];
  $service_down = FALSE;
  $data = [];
  $request = [
    'email' => $account
      ->getEmail(),
    'username' => $account
      ->getAccountName(),
  ];
  if (spambot_sfs_request($request, $data)) {
    if (!empty($data['email']['appears'])) {
      $messages[] = static::sfsRequestDataMessage($request, $data, 'email');
    }
    if (!empty($data['username']['appears'])) {
      $messages[] = static::sfsRequestDataMessage($request, $data, 'username');
    }

    // Check data at whitelist.
    if (spambot_check_whitelist('email', $config, $account
      ->getEmail())) {
      $messages[] = [
        'text' => $this
          ->t("This account's email address placed at your whitelist."),
        'type' => 'status',
      ];
    }
    if (spambot_check_whitelist('username', $config, $account
      ->getAccountName())) {
      $messages[] = [
        'text' => $this
          ->t("This account's username placed at your whitelist."),
        'type' => 'status',
      ];
    }
  }
  else {
    $this->messenger
      ->addMessage($this
      ->t('Error contacting service.'), 'warning');
    $service_down = TRUE;
  }

  // Check IP addresses.
  if (!$service_down) {
    $ips = spambot_account_ip_addresses($account);
    foreach ($ips as $ip) {

      // Skip the loopback interface.
      if ($ip == '127.0.0.1') {
        continue;
      }
      elseif (spambot_check_whitelist('ip', $config, $ip)) {
        $whitelist_ips[] = $ip;
        continue;
      }
      elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {
        $messages[] = [
          'text' => $this
            ->t('Invalid IP address: @ip. Spambot will not rely on it.', [
            '@ip' => $ip,
          ]),
          'type' => 'warning',
        ];
        continue;
      }
      $request = [
        'ip' => $ip,
      ];
      $data = [];
      if (spambot_sfs_request($request, $data)) {
        if (!empty($data['ip']['appears'])) {
          $messages[] = [
            'text' => $this
              ->t('An IP address !ip used by this account matches %num times.', [
              '!ip' => Link::fromTextAndUrl($ip, Url::fromUri('http://www.stopforumspam.com/search?q=' . $ip)),
              '%num' => $data['ip']['frequency'],
            ]),
            'type' => 'warning',
          ];
        }
      }
      else {
        $this->messenger
          ->addMessage($this
          ->t('Error contacting service.'), 'warning');
        break;
      }
    }
    if (!empty($whitelist_ips)) {
      $messages[] = [
        'text' => $this
          ->t('These IP addresses placed at your whitelist: %ips', [
          '%ips' => implode(', ', $whitelist_ips),
        ]),
        'type' => 'status',
      ];
    }
  }
  if ($messages) {
    foreach ($messages as $message) {
      $this->messenger
        ->addMessage($message['text'], $message['type']);
    }
  }
  else {
    $this->messenger
      ->addMessage($this
      ->t('No matches against known spammers found.'));
  }
}