Source of file RecaptchaField.php
Size: 5,610 Bytes - Last Modified: 2021-12-23T10:02:00+00:00
/var/www/docs.ssmods.com/process/src/code/RecaptchaField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 | <?php /** * The reCAPTCHA form field with validation logic. * @see https://github.com/google/recaptcha * @see https://developers.google.com/recaptcha/docs/verify#error-code-reference * Class RecaptchaField */ class RecaptchaField extends FormField { /** * @var Locale to init the field with. Auto-detects the user's language if unspecified. */ private $locale = null; /** * JavaScript options array in the following form: * array( * 'theme' => 'dark' * ); * * @see https://developers.google.com/recaptcha/docs/display#render_param * @var array */ private $jsOptions = array(); /** * CSS classes to apply to recaptcha base class * @var array */ private $css = array(); public function Field($properties = array()) { // Check if keys were given if( empty(SiteConfig::current_site_config()->RecaptchaSecret) || empty(SiteConfig::current_site_config()->RecaptchaWebkey)) SS_Log::log('Please specify valid reCAPTCHA API keys.', SS_Log::ERR); // Include Google's JS $scriptURL = 'https://www.google.com/recaptcha/api.js'; if($this->locale !== null) $scriptURL .= '?hl=' . $this->locale; else if(isset($properties['hl'])) $scriptURL .= '?hl' . $properties['hl']; Requirements::javascript($scriptURL); // Build css class string $css = ' ' . implode(' ', $this->css); // Iterate over JavaScript options and build config markup for tag $jsOpt = ''; foreach($this->jsOptions as $key=>$val) { $jsOpt .= "data-$key='$val' "; } // Return non-rendered field was div return "<div class='g-recaptcha $css' data-sitekey='" . SiteConfig::current_site_config()->RecaptchaWebkey . "' $jsOpt></div>"; } public function validate($validator) { // Check if recaptcha response field was set if( !isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) { // Set error message and exit $validator->validationError( $this->name, _t('RecaptchaField.EMPTY', 'Please verify that you are not a robot.'), 'validation' ); return false; } else $captchaResponse = $_POST['g-recaptcha-response']; // Create reCAPTCHA instance $recaptcha = new \ReCaptcha\ReCaptcha(SiteConfig::current_site_config()->RecaptchaSecret); // Verify identity $validationResponse = $recaptcha->verify($captchaResponse, $this->getClientIp()); // Handle validation errors if(!$validationResponse->isSuccess()) { $errorCodes = $validationResponse->getErrorCodes(); if ( in_array('invalid-input-secret', $errorCodes) && in_array('invalid-input-response', $errorCodes) ) { $errorString = _t('RecaptchaField.ERROR_INVALID_RESPONSE', 'Your secret key and your response seem to be wrong, please check your settings and try again'); } else if ( in_array('invalid-input-secret', $errorCodes) ) { $errorString = _t('RecaptchaField.ERROR_INVALID_RESPONSE', 'Your secret key seems to be wrong.'); } else if ( in_array('invalid-input-response', $errorCodes) ){ $errorString = _t('RecaptchaField.ERROR_INVALID_RESPONSE', 'Your response was wrong, please try again.'); } else { $errorString = _t('RecaptchaField.ERROR', 'There was an error validating your identity.'); } $validator->validationError( $this->name, $errorString, 'validation' ); return false; } // If you made it to this point you are validated as human - yeah! return true; } /** * Determins the client's IP. * * @return null|string The determined IP address of the client. */ private function getClientIp() { if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] != '') { $ip = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']; } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '') { $addresses = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $ip = trim($addresses[0]); } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != '') $ip = $_SERVER['REMOTE_ADDR']; else $ip = null; return $ip; } /** * Injection method for JavaScript options * @param $key * @param $val * * @return RecaptchaField */ public function settings($key, $val) { $this->jsOptions[$key] = $val; return $this; } /** * Add one (String) or more (Array) CSS classes to apply to the recaptcha base tag * @param $classes * * @return RecaptchaField */ public function setCSS($classes) { if(is_array($classes)) { foreach($classes as $class) $this->css[] = $class; } else $this->css[] = $classes; return $this; } /** * Sets a locale for the field. * * @param $locale * * @see https://developers.google.com/recaptcha/docs/language * @return RecaptchaField */ public function setLocale($locale) { $this->locale = $locale; return $this; } } |