You are here

function hcaptcha_captcha_validation in hCaptcha 8

Same name and namespace in other branches
  1. 7 hcaptcha.module \hcaptcha_captcha_validation()

CAPTCHA Callback; Validates the hCaptcha code.

1 string reference to 'hcaptcha_captcha_validation'
hcaptcha_captcha in ./hcaptcha.module
Implements hook_captcha().

File

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

Code

function hcaptcha_captcha_validation($solution, $response, $element, $form_state) {
  $config = \Drupal::config('hcaptcha.settings');
  $hcaptcha_site_key = $config
    ->get('site_key');
  $hcaptcha_secret_key = $config
    ->get('secret_key');
  if (!isset($_POST['h-captcha-response']) || empty($_POST['h-captcha-response']) || empty($hcaptcha_secret_key)) {
    return false;
  }
  $captcha = new HCaptcha($hcaptcha_site_key, $hcaptcha_secret_key, array(), new Drupal8Post());
  $captcha
    ->validate($_POST['h-captcha-response'], \Drupal::request()
    ->getClientIp());
  if ($captcha
    ->isSuccess()) {

    // Verified!
    return true;
  }
  else {
    foreach ($captcha
      ->getErrors() as $error) {
      \Drupal::logger('hCaptcha')
        ->error('@error', [
        '@error' => $error,
      ]);
    }
  }
  return false;
}