public function validate($validator) {
        $recaptchaResponse = Controller::curr()->getRequest()->requestVar('g-recaptcha-response');
        if(!isset($recaptchaResponse)) {
            $validator->validationError($this->name, _t('UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.EMPTY', '_Please answer the captcha, if you do not see the captcha you must enable JavaScript'), 'validation');
            return false;
        }

        if(!function_exists('curl_init')) {
            user_error('You must enable php-curl to use this field', E_USER_ERROR);
            return false;
        }

        $url='https://www.google.com/recaptcha/api/siteverify?secret='.self::config()->secret_key.'&response='.rawurlencode($recaptchaResponse).'&remoteip='.rawurlencode($_SERVER['REMOTE_ADDR']);
        $ch=curl_init($url);
        $proxy_server=$this->_proxyServer ? $this->_proxyServer : self::config()->proxy_server;
        if(!empty($proxy_server)){
            curl_setopt($ch, CURLOPT_PROXY, $proxy_server);

            $proxy_auth=$this->_proxyAuth ? $this->_proxyAuth : self::config()->proxy_auth;
            if(!empty($proxy_auth)){
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_auth);
            }

            $proxy_port=$this->_proxyPort ? $this->_proxyPort : self::config()->proxy_port;
            if(!empty($proxy_port)){
                curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
            }
        }

        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::config()->verify_ssl);
        curl_setopt($ch, CURLOPT_USERAGENT, str_replace(',', '/', 'SilverStripe'));
        $response=json_decode(curl_exec($ch), true);

        if(is_array($response)) {
            if(array_key_exists('success', $response) && $response['success']==false) {
                $validator->validationError($this->name, _t('UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.EMPTY', '_Please answer the captcha, if you do not see the captcha you must enable JavaScript'), 'validation');
                return false;
            }
        }else {
            $validator->validationError($this->name, _t('UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.VALIDATE_ERROR', '_Captcha could not be validated'), 'validation');
            $logger = Injector::inst()->get(LoggerInterface::class);
            $logger->error(
                'Captcha validation failed as request was not successful.'
            );
            return false;
        }


        return true;
    }
public function validate($validator) {
        $recaptchaResponse = Controller::curr()->getRequest()->requestVar('g-recaptcha-response');
        if(!isset($recaptchaResponse)) {
            $validator->validationError($this->name, _t('UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.EMPTY', '_Please answer the captcha, if you do not see the captcha you must enable JavaScript'), 'validation');
            return false;
        }

        if(!function_exists('curl_init')) {
            user_error('You must enable php-curl to use this field', E_USER_ERROR);
            return false;
        }

        $url='https://www.google.com/recaptcha/api/siteverify?secret='.self::config()->secret_key.'&response='.rawurlencode($recaptchaResponse).'&remoteip='.rawurlencode($_SERVER['REMOTE_ADDR']);
        $ch=curl_init($url);
        $proxy_server=$this->_proxyServer ? $this->_proxyServer : self::config()->proxy_server;
        if(!empty($proxy_server)){
            curl_setopt($ch, CURLOPT_PROXY, $proxy_server);

            $proxy_auth=$this->_proxyAuth ? $this->_proxyAuth : self::config()->proxy_auth;
            if(!empty($proxy_auth)){
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_auth);
            }

            $proxy_port=$this->_proxyPort ? $this->_proxyPort : self::config()->proxy_port;
            if(!empty($proxy_port)){
                curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
            }
        }

        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::config()->verify_ssl);
        curl_setopt($ch, CURLOPT_USERAGENT, str_replace(',', '/', 'SilverStripe'));
        $response=json_decode(curl_exec($ch), true);

        if(is_array($response)) {
            if(array_key_exists('success', $response) && $response['success']==false) {
                $validator->validationError($this->name, _t('UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.EMPTY', '_Please answer the captcha, if you do not see the captcha you must enable JavaScript'), 'validation');
                return false;
            }
        }else {
            $validator->validationError($this->name, _t('UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.VALIDATE_ERROR', '_Captcha could not be validated'), 'validation');
            $logger = Injector::inst()->get(LoggerInterface::class);
            $logger->error(
                'Captcha validation failed as request was not successful.'
            );
            return false;
        }


        return true;
    }
public function validate ( $ validator ) { $ recaptchaResponse = Controller :: curr ( ) -> getRequest ( ) -> requestVar ( 'g-recaptcha-response' ) ; if ( ! isset ( $ recaptchaResponse ) ) { $ validator -> validationError ( $ this -> name , _t ( 'UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.EMPTY' , '_Please answer the captcha, if you do not see the captcha you must enable JavaScript' ) , 'validation' ) ; return false ; } if ( ! function_exists ( 'curl_init' ) ) { user_error ( 'You must enable php-curl to use this field' , E_USER_ERROR ) ; return false ; } $ url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . self :: config ( ) -> secret_key . '&response=' . rawurlencode ( $ recaptchaResponse ) . '&remoteip=' . rawurlencode ( $ _SERVER [ 'REMOTE_ADDR' ] ) ; $ ch = curl_init ( $ url ) ; $ proxy_server = $ this -> _proxyServer ? $ this -> _proxyServer : self :: config ( ) -> proxy_server ; if ( ! empty ( $ proxy_server ) ) { curl_setopt ( $ ch , CURLOPT_PROXY , $ proxy_server ) ; $ proxy_auth = $ this -> _proxyAuth ? $ this -> _proxyAuth : self :: config ( ) -> proxy_auth ; if ( ! empty ( $ proxy_auth ) ) { curl_setopt ( $ ch , CURLOPT_PROXYUSERPWD , $ proxy_auth ) ; } $ proxy_port = $ this -> _proxyPort ? $ this -> _proxyPort : self :: config ( ) -> proxy_port ; if ( ! empty ( $ proxy_port ) ) { curl_setopt ( $ ch , CURLOPT_PROXYPORT , $ proxy_port ) ; } } curl_setopt ( $ ch , CURLOPT_TIMEOUT , 10 ) ; curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true ) ; curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , self :: config ( ) -> verify_ssl ) ; curl_setopt ( $ ch , CURLOPT_USERAGENT , str_replace ( ',' , '/' , 'SilverStripe' ) ) ; $ response = json_decode ( curl_exec ( $ ch ) , true ) ; if ( is_array ( $ response ) ) { if ( array_key_exists ( 'success' , $ response ) && $ response [ 'success' ] == false ) { $ validator -> validationError ( $ this -> name , _t ( 'UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.EMPTY' , '_Please answer the captcha, if you do not see the captcha you must enable JavaScript' ) , 'validation' ) ; return false ; } } else { $ validator -> validationError ( $ this -> name , _t ( 'UndefinedOffset\\NoCaptcha\\Forms\\NocaptchaField.VALIDATE_ERROR' , '_Captcha could not be validated' ) , 'validation' ) ; $ logger = Injector :: inst ( ) -> get ( LoggerInterface :: class ) ; $ logger -> error ( 'Captcha validation failed as request was not successful.' ) ; return false ; } return true ; }
Validates the captcha against the Recaptcha2 API
@param Validator $validator Validator to send errors to
@return bool Returns boolean true if valid false if not
Validates the captcha against the Recaptcha2 API