You are here

function google_captcha_captcha in Google Captcha 7

Implements hook_captcha().

File

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

Code

function google_captcha_captcha($op, $captcha_type = '') {
  switch ($op) {
    case 'list':
      return array(
        'Google Captcha',
      );
    case 'generate':
      $captcha = array();
      if ($captcha_type == 'Google Captcha') {
        $google_captcha_site_key = variable_get('google_captcha_site_key', FALSE);

        // Test if captcha can be used, falling back to Math if it is not
        // configured, the library won't load, or the server is down.
        if (!$google_captcha_site_key || !_google_captcha_load_library()) {
          $args = func_get_args();
          return captcha_captcha('generate', 'Math', $args);
        }

        // Create the form. Captcha requires TRUE to be returned in solution.
        $captcha['solution'] = TRUE;
        $captcha['captcha_validate'] = 'google_captcha_captcha_validation';
        $captcha['form']['captcha_response'] = array(
          '#type' => 'hidden',
          '#value' => 'Google no captcha',
        );
        $captcha['form']['google_capture'] = array(
          '#markup' => '<div class="g-recaptcha" data-sitekey="' . $google_captcha_site_key . '"></div>',
        );
        drupal_add_js('https://www.google.com/recaptcha/api.js', array(
          'defer' => TRUE,
          'async' => TRUE,
        ));
      }
      return $captcha;
  }
}