Same name and namespace in other branches
  1. 8.x-1.x spambot.module \spambot_user_register_form_validate() 1 comment

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 233

Code

function spambot_user_register_form_validate(&$form, &$form_state) {
    $validation_field_names = $form['#spambot_validation'];
    $values = $form_state['values'];
    $form_errors = form_get_errors();
    $email_threshold = variable_get('spambot_criteria_email', SPAMBOT_DEFAULT_CRITERIA_EMAIL);
    $username_threshold = variable_get('spambot_criteria_username', SPAMBOT_DEFAULT_CRITERIA_USERNAME);
    $ip_threshold = variable_get('spambot_criteria_ip', SPAMBOT_DEFAULT_CRITERIA_IP);
    // Build request parameters according to the criteria to use.
    $request = array();
    if (!empty($values[$validation_field_names['mail']]) && $email_threshold > 0 && !spambot_check_whitelist('email', $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', $values[$validation_field_names['name']])) {
        $request['username'] = $values[$validation_field_names['name']];
    }
    $ip = ip_address();
    if ($ip_threshold > 0 && $ip != '127.0.0.1' && $validation_field_names['ip'] && !spambot_check_whitelist('ip', $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) {
            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 ($request && !$form_errors) {
        $data = array();
        if (spambot_sfs_request($request, $data)) {
            $substitutions = array(
                '@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 = array();
            if ($email_threshold > 0 && !empty($data['email']['appears']) && $data['email']['frequency'] >= $email_threshold) {
                form_set_error('mail', format_string(variable_get('spambot_blocked_message_email', 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', format_string(variable_get('spambot_blocked_message_username', 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('', format_string(variable_get('spambot_blocked_message_ip', SPAMBOT_DEFAULT_BLOCKED_MESSAGE), $substitutions));
                $reasons[] = t('ip=@value', array(
                    '@value' => $request['ip'],
                ));
            }
            if ($reasons) {
                if (variable_get('spambot_log_blocked_registration', TRUE)) {
                    watchdog('spambot', 'Blocked registration: @reasons', array(
                        '@reasons' => implode(',', $reasons),
                    ));
                    $hook_args = array(
                        'request' => $request,
                        'reasons' => $reasons,
                    );
                    module_invoke_all('spambot_registration_blocked', $hook_args);
                }
                // Slow them down if configured.
                if ($delay = variable_get('spambot_blacklisted_delay', SPAMBOT_DEFAULT_DELAY)) {
                    sleep($delay);
                }
            }
        }
    }
}