You are here

class HCaptcha in hCaptcha 7

Hierarchy

Expanded class hierarchy of HCaptcha

1 file declares its use of HCaptcha
hcaptcha.module in ./hcaptcha.module

File

src/HCaptcha/HCaptcha.php, line 7

Namespace

HCaptcha
View source
class HCaptcha {
  const SITE_VERIFY_URL = 'https://hcaptcha.com/siteverify';
  protected $attributes = array(
    'class' => 'h-captcha',
    'data-sitekey' => '',
    'data-theme' => '',
    'data-size' => '',
    'data-tabindex' => 0,
  );
  protected $siteKey = '';
  protected $secretKey = '';
  protected $errors = array();
  private $success = false;
  private $validated;
  private $requestMethod;
  public function __construct($site_key, $secret_key, $attributes = array(), RequestMethod $requestMethod = null) {
    $this->siteKey = $site_key;
    $this->secretKey = $secret_key;
    $this->requestMethod = $requestMethod;
    if (!empty($attributes) && is_array($attributes)) {
      foreach ($attributes as $name => $attribute) {
        if (isset($this->attributes[$name])) {
          $this->attributes[$name] = $attribute;
        }
      }
    }
  }

  /**
   * Build the hCaptcha captcha form.
   * @return mixed
   */
  public function getWidget($validation_function) {

    // Captcha requires TRUE to be returned in solution.
    $widget['solution'] = true;
    $widget['captcha_validate'] = $validation_function;
    $widget['form']['captcha_response'] = array(
      '#type' => 'hidden',
      '#value' => 'hCaptcha no captcha',
    );

    // As the validate callback does not depend on sid or solution, this
    // captcha type can be displayed on cached pages.
    $widget['cacheable'] = true;
    $widget['form']['hcaptcha_widget'] = array(
      '#markup' => '<div' . $this
        ->getAttributesString() . '></div>',
    );
    return $widget;
  }
  public function validate($response_token, $remote_ip = '') {
    $query = array(
      'secret' => $this->secretKey,
      'response' => $response_token,
      'remoteip' => $remote_ip,
    );
    $this->validated = $this->requestMethod
      ->submit(self::SITE_VERIFY_URL, array_filter($query));
    if (isset($this->validated->success) && $this->validated->success === true) {

      // Verified!
      $this->success = true;
    }
    else {
      $this->errors = $this
        ->getResponseErrors();
    }
  }
  public function isSuccess() {
    return $this->success;
  }
  public function getErrors() {
    return $this->errors;
  }
  public function getResponseErrors() {

    // Error code reference, https://hcaptcha.com/docs#server
    $errors = array();
    if (isset($this->validated->{'error-codes'}) && is_array($this->validated->{'error-codes'})) {
      $error_codes = $this
        ->getErrorCodes();
      foreach ($this->validated->{'error-codes'} as $code) {
        if (!isset($error_codes[$code])) {
          $code = 'unknown-error';
        }
        $errors[] = $error_codes[$code];
      }
    }
    return $errors;
  }
  public function getErrorCodes() {
    $error_codes = array(
      'missing-input-secret' => t('Your secret key is missing.'),
      'invalid-input-secret' => t('Your secret key is invalid or malformed.'),
      'missing-input-response' => t('The response parameter (verification token) is missing.'),
      'invalid-input-response' => t('The response parameter (verification token) is invalid or malformed.'),
      'bad-request' => t('The request is invalid or malformed.'),
      'bad-response' => t('Did not receive a 200 from the service.'),
      'connection-failed' => t('Could not connect to service.'),
      'unknown-error' => t('Not a success, but no error codes received.'),
    );
    return $error_codes;
  }
  public function getAttributesString() {
    $attributes = array_filter($this->attributes);
    foreach ($attributes as $attribute => &$data) {
      $data = implode(' ', (array) $data);
      $data = $attribute . '="' . htmlspecialchars($data, ENT_QUOTES, 'UTF-8') . '"';
    }
    return $attributes ? ' ' . implode(' ', $attributes) : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HCaptcha::$attributes protected property
HCaptcha::$errors protected property
HCaptcha::$requestMethod private property
HCaptcha::$secretKey protected property
HCaptcha::$siteKey protected property
HCaptcha::$success private property
HCaptcha::$validated private property
HCaptcha::getAttributesString public function
HCaptcha::getErrorCodes public function
HCaptcha::getErrors public function
HCaptcha::getResponseErrors public function
HCaptcha::getWidget public function Build the hCaptcha captcha form.
HCaptcha::isSuccess public function
HCaptcha::SITE_VERIFY_URL constant
HCaptcha::validate public function
HCaptcha::__construct public function