View source
<?php
namespace HCaptcha;
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;
}
}
}
}
public function getWidget($validation_function) {
$widget['solution'] = true;
$widget['captcha_validate'] = $validation_function;
$widget['form']['captcha_response'] = array(
'#type' => 'hidden',
'#value' => 'hCaptcha no captcha',
);
$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) {
$this->success = true;
}
else {
$this->errors = $this
->getResponseErrors();
}
}
public function isSuccess() {
return $this->success;
}
public function getErrors() {
return $this->errors;
}
public function getResponseErrors() {
$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) : '';
}
}