You are here

function spambot_user_register_form_validate in Spambot 8

Same name and namespace in other branches
  1. 7 spambot.module \spambot_user_register_form_validate()

Validate callback for user_register form.

1 string reference to 'spambot_user_register_form_validate'
spambot_add_form_protection in ./spambot.module
Form builder function to add spambot validations.

File

./spambot.module, line 294
Main module file.

Code

function spambot_user_register_form_validate(&$form, &$form_state) {
  $config = \Drupal::config('spambot.settings');
  $validation_field_names = $form['#spambot_validation'];
  $values = $form_state
    ->getValues();
  $form_errors = $form_state
    ->getErrors();
  $email_threshold = $config
    ->get('spambot_criteria_email');
  $username_threshold = $config
    ->get('spambot_criteria_username');
  $ip_threshold = $config
    ->get('spambot_criteria_ip');

  // Build request parameters according to the criteria to use.
  $request = [];
  if (!empty($values[$validation_field_names['mail']]) && $email_threshold > 0 && !spambot_check_whitelist('email', $config, $values[$validation_field_names['mail']])) {
    $request['email'] = $values[$validation_field_names['mail']];
  }
  if (!empty($values[$validation_field_names['name']]) && $username_threshold > 0 && !spambot_check_whitelist('username', $config, $values[$validation_field_names['name']])) {
    $request['username'] = $values[$validation_field_names['name']];
  }
  $ip = \Drupal::request()
    ->getClientIp();
  if ($ip_threshold > 0 && $ip != '127.0.0.1' && $validation_field_names['ip'] && !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) {
      \Drupal::logger('spambot')
        ->notice('Invalid IP address on registration: @ip. Spambot will not rely on it.', [
        '@ip' => $ip,
      ]);
    }
    else {
      $request['ip'] = $ip;
    }
  }

  // Only do a remote API request if there is anything to check.
  if ($request && !$form_errors) {
    $data = [];
    if (spambot_sfs_request($request, $data)) {
      $substitutions = [
        '@email' => $values[$validation_field_names['mail']],
        '%email' => $values[$validation_field_names['mail']],
        '@username' => $values[$validation_field_names['name']],
        '%username' => $values[$validation_field_names['name']],
        '@ip' => $ip,
        '%ip' => $ip,
      ];
      $reasons = [];
      if ($email_threshold > 0 && !empty($data['email']['appears']) && $data['email']['frequency'] >= $email_threshold) {
        $form_state
          ->setErrorByName('mail', (string) new FormattableMarkup($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('name', (string) new FormattableMarkup($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) new FormattableMarkup($config
          ->get('spambot_blocked_message_ip'), $substitutions));
        $reasons[] = t('ip=@value', [
          '@value' => $request['ip'],
        ]);
      }
      if ($reasons) {
        if ($config
          ->get('spambot_log_blocked_registration')) {
          \Drupal::logger('spambot')
            ->notice('Blocked registration: @reasons', [
            '@reasons' => implode(',', $reasons),
          ]);
          $hook_args = [
            'request' => $request,
            'reasons' => $reasons,
          ];
          \Drupal::moduleHandler()
            ->invokeAll('spambot_registration_blocked', [
            $hook_args,
          ]);
        }
        if ($delay = $config
          ->get('spambot_blacklisted_delay')) {
          sleep($delay);
        }
      }
    }
  }
}