src/Charcoal/Admin/AdminAction.php
protected function validateCaptcha($token)
    {
        if (empty($token)) {
            throw new RuntimeException('Google reCAPTCHA response parameter is invalid or malformed.');
        }

        $secret = $this->recaptchaSecretKey();
        if (empty($secret)) {
            throw new RuntimeException('Google reCAPTCHA [apis.google.recaptcha.private_key] is not configured.');
        }

        $data = [
            'secret'   => $secret,
            'response' => $token,
        ];

        if (isset($_SERVER['REMOTE_ADDR'])) {
            $data['remoteip'] = $_SERVER['REMOTE_ADDR'];
        }

        $query = http_build_query($data);
        $url   = static::GOOGLE_RECAPTCHA_SERVER_URL.'?'.$query;

        $this->logger->debug(sprintf('Verifying reCAPTCHA user response: %s', $url));

        /**
         * @todo Use Guzzle
         */
        $result = file_get_contents($url);
        $result = (array)json_decode($result, true);

        $this->recaptchaLastToken  = $token;
        $this->recaptchaLastResult = $result;

        return isset($result['success']) && (bool)$result['success'];
    }
protected function validateCaptcha($token)
    {
        if (empty($token)) {
            throw new RuntimeException('Google reCAPTCHA response parameter is invalid or malformed.');
        }

        $secret = $this->recaptchaSecretKey();
        if (empty($secret)) {
            throw new RuntimeException('Google reCAPTCHA [apis.google.recaptcha.private_key] is not configured.');
        }

        $data = [
            'secret'   => $secret,
            'response' => $token,
        ];

        if (isset($_SERVER['REMOTE_ADDR'])) {
            $data['remoteip'] = $_SERVER['REMOTE_ADDR'];
        }

        $query = http_build_query($data);
        $url   = static::GOOGLE_RECAPTCHA_SERVER_URL.'?'.$query;

        $this->logger->debug(sprintf('Verifying reCAPTCHA user response: %s', $url));

        /**
         * @todo Use Guzzle
         */
        $result = file_get_contents($url);
        $result = (array)json_decode($result, true);

        $this->recaptchaLastToken  = $token;
        $this->recaptchaLastResult = $result;

        return isset($result['success']) && (bool)$result['success'];
    }
protected function validateCaptcha ( $ token ) { if ( empty ( $ token ) ) { throw new RuntimeException ( 'Google reCAPTCHA response parameter is invalid or malformed.' ) ; } $ secret = $ this -> recaptchaSecretKey ( ) ; if ( empty ( $ secret ) ) { throw new RuntimeException ( 'Google reCAPTCHA [apis.google.recaptcha.private_key] is not configured.' ) ; } $ data = [ 'secret' => $ secret , 'response' => $ token , ] ; if ( isset ( $ _SERVER [ 'REMOTE_ADDR' ] ) ) { $ data [ 'remoteip' ] = $ _SERVER [ 'REMOTE_ADDR' ] ; } $ query = http_build_query ( $ data ) ; $ url = static :: GOOGLE_RECAPTCHA_SERVER_URL . '?' . $ query ; $ this -> logger -> debug ( sprintf ( 'Verifying reCAPTCHA user response: %s' , $ url ) ) ; /**
         * @todo Use Guzzle
         */ $ result = file_get_contents ( $ url ) ; $ result = ( array ) json_decode ( $ result , true ) ; $ this -> recaptchaLastToken = $ token ; $ this -> recaptchaLastResult = $ result ; return isset ( $ result [ 'success' ] ) && ( bool ) $ result [ 'success' ] ; }
Validate a Google reCAPTCHA user response.

@todo   {@link https://github.com/mcaskill/charcoal-recaptcha Implement CAPTCHA validation as a service}.
@link   https://developers.google.com/recaptcha/docs/verify
@param  string $token A user response token provided by reCAPTCHA.
@throws RuntimeException If Google reCAPTCHA is not configured.
@return boolean Returns TRUE if the user response is valid, FALSE if it is invalid.
Validate a Google reCAPTCHA user response .