You are here

function spambot_user_register_validate in Spambot 6.3

Validate the user_register form

1 string reference to 'spambot_user_register_validate'
spambot_form_alter in ./spambot.module
Implementation of hook_form_alter()

File

./spambot.module, line 54

Code

function spambot_user_register_validate($form, &$form_state) {
  $email_threshold = variable_get('spambot_criteria_email', 1);
  $username_threshold = variable_get('spambot_criteria_username', 0);
  $ip_threshold = variable_get('spambot_criteria_ip', 20);

  // Build request parameters according to the criteria to use
  $request = array();
  if (!empty($form_state['values']['mail']) && $email_threshold > 0) {
    $request['email'] = $form_state['values']['mail'];
  }
  if (!empty($form_state['values']['name']) && $username_threshold > 0) {
    $request['username'] = $form_state['values']['name'];
  }
  if ($ip_threshold > 0) {
    $ip = ip_address();

    // Don't check the loopback interface
    if ($ip != '127.0.0.1') {

      // Make sure we have a valid IPv4 address (API doesn't support IPv6 yet)
      if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {
        watchdog('spambot', 'Invalid IP address on registration: @ip. Spambot will not rely on it.', array(
          '@ip' => $ip,
        ));
      }
      else {
        $request['ip'] = $ip;
      }
    }
  }

  // Only do a remote API request if there is anything to check
  if (count($request)) {
    $data = array();
    if (spambot_sfs_request($request, $data)) {
      $substitutions = array(
        '@email' => $form_state['values']['mail'],
        '%email' => $form_state['values']['mail'],
        '@username' => $form_state['values']['name'],
        '%username' => $form_state['values']['name'],
        '@ip' => ip_address(),
        '%ip' => ip_address(),
      );
      $reasons = array();
      if ($email_threshold > 0 && !empty($data['email']['appears']) && $data['email']['frequency'] >= $email_threshold) {
        form_set_error('mail', t(variable_get('spambot_blocked_message_email', t(SPAMBOT_DEFAULT_BLOCKED_MESSAGE)), $substitutions));
        $reasons[] = t('email=@value', array(
          '@value' => $request['email'],
        ));
      }
      if ($username_threshold > 0 && !empty($data['username']['appears']) && $data['username']['frequency'] >= $username_threshold) {
        form_set_error('name', t(variable_get('spambot_blocked_message_username', t(SPAMBOT_DEFAULT_BLOCKED_MESSAGE)), $substitutions));
        $reasons[] = t('username=@value', array(
          '@value' => $request['username'],
        ));
      }
      if ($ip_threshold > 0 && !empty($data['ip']['appears']) && $data['ip']['frequency'] >= $ip_threshold) {
        form_set_error('', t(variable_get('spambot_blocked_message_ip', t(SPAMBOT_DEFAULT_BLOCKED_MESSAGE)), $substitutions));
        $reasons[] = t('ip=@value', array(
          '@value' => $request['ip'],
        ));
      }
      if (count($reasons)) {
        watchdog('spambot', t('Blocked registration: @reasons', array(
          '@reasons' => join(',', $reasons),
        )));

        // Slow them down if configured
        $delay = variable_get('spambot_blacklisted_delay', 0);
        if ($delay) {
          sleep($delay);
        }
      }
    }
  }
}