Fichier

src/Plugin/WebformHandler/SpamBotWebformHandler.php, line 71

Classe

SpamBotWebformHandler
Webform validate handler.

Namespace

Drupal\spambot\Plugin\WebformHandler

Code

public function validateForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    $config = $this->configFactory
        ->get('spambot.settings');
    if ($form_state->getErrors()) {
        return;
    }
    $email_threshold = $config->get('spambot_criteria_email');
    $username_threshold = $config->get('spambot_criteria_username');
    $ip_threshold = $config->get('spambot_criteria_ip');
    $data = $webform_submission->getData();
    $request = [];
    if ($this->configuration['email_field'] && $data[$this->configuration['email_field']]) {
        $request['email'] = $data[$this->configuration['email_field']];
    }
    if ($this->configuration['username_field'] && $data[$this->configuration['username_field']]) {
        $request['username'] = $data[$this->configuration['username_field']];
    }
    $ip = $this->request
        ->getClientIp();
    if ($ip_threshold > 0 && $ip != '127.0.0.1' && !spambot_check_whitelist('ip', $config, $ip)) {
        // Make sure we have a valid IPv4 address (API doesn't support IPv6 yet).
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) === FALSE) {
            $this->getLogger('spambot')
                ->notice('Invalid IP address on webform: @ip. Spambot will not rely on it.', [
                '@ip' => $ip,
            ]);
        }
        else {
            $request['ip'] = $ip;
        }
    }
    $data = [];
    if (spambot_sfs_request($request, $data)) {
        $substitutions = [
            '@email' => $request['email'] ?? '',
            '%email' => $request['email'] ?? '',
            '@username' => $request['username'] ?? '',
            '%username' => $request['username'] ?? '',
            '@ip' => $ip,
            '%ip' => $ip,
        ];
        $reasons = [];
        if ($email_threshold > 0 && !empty($data['email']['appears']) && $data['email']['frequency'] >= $email_threshold) {
            $form_state->setErrorByName($this->configuration['email_field'], (string) $this->t($config->get('spambot_blocked_message_email'), $substitutions));
            $reasons[] = t('email=@value', [
                '@value' => $request['email'],
            ]);
        }
        if ($username_threshold > 0 && !empty($data['username']['appears']) && $data['username']['frequency'] >= $username_threshold) {
            $form_state->setErrorByName($this->configuration['username_field'], (string) $this->t($config->get('spambot_blocked_message_username'), $substitutions));
            $reasons[] = t('username=@value', [
                '@value' => $request['username'],
            ]);
        }
        if ($ip_threshold > 0 && !empty($data['ip']['appears']) && $data['ip']['frequency'] >= $ip_threshold) {
            $form_state->setErrorByName('', (string) $this->t($config->get('spambot_blocked_message_ip'), $substitutions));
            $reasons[] = t('ip=@value', [
                '@value' => $request['ip'],
            ]);
        }
        if ($reasons) {
            if ($config->get('spambot_log_blocked_registration')) {
                $this->getLogger('spambot')
                    ->notice('Blocked webform submission: @reasons', [
                    '@reasons' => implode(',', $reasons),
                ]);
                $hook_args = [
                    'request' => $request,
                    'reasons' => $reasons,
                ];
                $this->moduleHandler
                    ->invokeAll('spambot_registration_blocked', [
                    $hook_args,
                ]);
            }
            if ($delay = $config->get('spambot_blacklisted_delay')) {
                sleep($delay);
            }
        }
    }
}