Source of file HCaptchaField.php
Size: 5,856 Bytes - Last Modified: 2023-03-17T10:00:03+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/HCaptchaField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 | <?php namespace X3dgoo\HCaptcha\Forms; use Psr\Log\LoggerInterface; use SilverStripe\Control\Controller; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\FormField; use SilverStripe\Forms\Validator; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\View\Requirements; class HCaptchaField extends FormField { /** * HCaptcha Site Key * @config HCaptchaField.site_key */ private static $site_key; /** * HCaptcha Secret Key * @config HCaptchaField.secret_key */ private static $secret_key; /** * HCaptcha Site Key * Configurable via Injector config */ protected $_siteKey; /** * HCaptcha Site Key * Configurable via Injector config */ protected $_secretKey; /** * Captcha theme, currently options are light and dark * @var string * @default light */ private static $default_theme = 'light'; /** * Captcha theme, currently options are light and dark * @var string */ private $_captchaTheme; /** * Creates a new HCaptcha field. * @param string $name The internal field name, passed to forms. * @param string $title The human-readable field label. * @param mixed $value The value of the field (unused) */ public function __construct($name, $title = null, $value = null) { parent::__construct($name, $title, $value); $this->title = $title; $this->_captchaTheme = self::config()->default_theme; } /** * Adds in the requirements for the field * @param array $properties Array of properties for the form element (not used) * @return DBHTMLText Rendered field template */ public function Field($properties = []) { $siteKey = $this->getSiteKey(); $secretKey = $this->getSecretKey(); if (empty($siteKey) || empty($secretKey)) { user_error( 'You must configure HCaptchaField.site_key and HCaptchaField.secret_key. ' . 'You can retrieve these at https://hcaptcha.com', E_USER_ERROR ); } Requirements::javascript('https://hcaptcha.com/1/api.js'); return parent::Field($properties); } /** * Validates the captcha against the hCaptcha API * @param Validator $validator Validator to send errors to * @return bool Returns boolean true if valid false if not */ public function validate($validator) { $valid = $this->processCaptcha(); if (!$valid) { $validator->validationError( $this->name, _t( 'X3dgoo\\HCaptcha\\Forms\\HCaptchaField.EMPTY', 'Please answer the captcha. If you do not see the captcha please enable Javascript' ), 'validation' ); } return $valid; } /** * Validates the captcha against the hCaptcha API * @return bool Returns boolean true if valid false if not */ private function processCaptcha() { $hCaptchaResponse = Controller::curr()->getRequest()->requestVar('h-captcha-response'); if (!isset($hCaptchaResponse) || !$hCaptchaResponse) { return false; } $secretKey = $this->getSecretKey(); $client = new \GuzzleHttp\Client([ 'base_uri' => 'https://hcaptcha.com/', ]); $response = $client->request( 'GET', 'siteverify', [ 'query' => [ 'secret' => $secretKey, 'response' => rawurlencode($hCaptchaResponse), 'remoteip' => rawurlencode($_SERVER['REMOTE_ADDR']), ], ] ); $response = json_decode($response->getBody(), true); if (!is_array($response)) { $logger = Injector::inst()->get(LoggerInterface::class); $logger->error( 'Captcha validation failed as request was not successful.' ); return false; } if (array_key_exists('success', $response) && $response['success'] === false) { return false; } return true; } /** * Gets the site key configured via HCaptchaField.site_key this is used in the template * @return string */ public function getSiteKey() { return $this->_siteKey ? $this->_siteKey : self::config()->site_key; } /** * Gets the secret key configured via HCaptchaField.secret_key * @return string */ private function getSecretKey() { return $this->_secretKey ? $this->_secretKey : self::config()->secret_key; } /** * Setter for _siteKey to allow injector config to override the value * @param string $key */ public function setSiteKey($key) { $this->_siteKey = $key; } /** * Setter for _secretKey to allow injector config to override the value * @param string $key */ public function setSecretKey($key) { $this->_secretKey = $key; } /** * Gets the form's id * @return string */ public function getFormID() { return ($this->form ? $this->getTemplateHelper()->generateFormID($this->form) : null); } /** * Sets the theme for this captcha * @param string $value Theme to set it to, currently the api supports light and dark * @return NocaptchaField */ public function setTheme($value) { $this->_captchaTheme = $value; return $this; } /** * Gets the theme for this captcha * @return string */ public function getCaptchaTheme() { return $this->_captchaTheme; } } |