You are here

function recaptcha_v3_element_captcha_process in reCAPTCHA v3 7

Need to validate recaptcha v3 token value here, because otherwise in case of failed verification it is no way how to replace captcha element type.

Parameters

$element:

$form_state:

$complete_form:

Return value

mixed

1 string reference to 'recaptcha_v3_element_captcha_process'
recaptcha_v3_element_info_alter in ./recaptcha_v3.module
Place recaptcha v3 preprocess function at the beginning, so in this way it is possible to verify captcha and in case of fail, replace it to fallback captcha challenge.

File

./recaptcha_v3.module, line 112
Verifies if user is a human without necessity to solve a CAPTCHA.

Code

function recaptcha_v3_element_captcha_process($element, &$form_state, $complete_form) {
  $parts = explode('/', $element['#captcha_type']);
  if (count($parts) === 2 && $parts[0] === 'recaptcha_v3') {
    $element['#recaptcha_v3_valid'] = FALSE;
    $element['#recaptcha_v3_token'] = FALSE;
    $element['#recaptcha_v3_action'] = _recaptcha_v3_get_action_by_id($parts[1]);
    if ($form_state['process_input']) {
      if (isset($form_state['input']['recaptcha_v3_token'])) {
        $element['#recaptcha_v3_token'] = $form_state['input']['recaptcha_v3_token'];
      }
      if ($element['#recaptcha_v3_token']) {
        $element['#recaptcha_v3_valid'] = recaptcha_v3_token_validate($element);
      }

      // Recaptcha v3 verification failed - try to find fallback challenge
      if (!$element['#recaptcha_v3_valid']) {
        $challenge = $element['#recaptcha_v3_action']['challenge'];
        $challenge = empty($challenge) ? variable_get('recaptcha_v3_captcha_default_challenge', FALSE) : $challenge;

        // If fallback challenge exist - change captcha type to this value,
        // so instead of recaptcha v3 selected fallback widget will be processed
        if (!empty($challenge)) {
          $element['#captcha_type'] = $challenge;
        }
      }
    }
  }
  return $element;
}