Установка reCAPTCHA v3 на сайт - amel-post/bitrix.help GitHub Wiki

<?php
/**
 * A ReCaptchaResponse is returned from checkAnswer().
 */
class ReCaptchaResponse
{
    public $success;
    public $errorCodes;
    public $action;
}

class ReCaptcha
{
    private static $_signupUrl = "https://www.google.com/recaptcha/admin";
    private static $_siteVerifyUrl =
        "https://www.google.com/recaptcha/api/siteverify";
    private $_secret;
    private static $_version = "php_1.0";

    /**
     * Constructor.
     *
     * @param string $secret shared secret between site and ReCAPTCHA server.
     */
    function ReCaptcha($secret)
    {
        if ($secret == null || $secret == "") {
            die("To use reCAPTCHA you must get an API key from <a href='"
                . self::$_signupUrl . "'>" . self::$_signupUrl . "</a>");
        }
        $this->_secret=$secret;
    }


    /**
     * Calls the reCAPTCHA siteverify API to verify whether the user passes
     * CAPTCHA test.
     *
     * @param string $remoteIp IP address of end user.
     * @param $token
     * @param $action
     * @return ReCaptchaResponse
     */
    public function verifyResponse($remoteIp, $token, $action)
    {
        // Discard empty solution submissions
        if ($token == null || strlen($token) == 0) {
            $recaptchaResponse = new ReCaptchaResponse();
            $recaptchaResponse->success = false;
            $recaptchaResponse->errorCodes = 'missing-input';
            return $recaptchaResponse;
        }

        $recaptchaResponse = new ReCaptchaResponse();
        $recaptchaResponse->success = false;

        $params = [
            'secret' => $this->_secret,
            'response' => $token,
            'remoteip' => $remoteIp
        ];

        $ch = curl_init(self::$_siteVerifyUrl);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $response = curl_exec($ch);
        if(!empty($response))
        {
            $decoded_response = json_decode($response);
            $array = json_decode(json_encode($decoded_response), true);

            if ($decoded_response && $decoded_response->success && $decoded_response->action == $action && $decoded_response->score > 0.1) {
                $recaptchaResponse->success = true;
                $recaptchaResponse->action = $decoded_response->action;
            } else {

                $recaptchaResponse->success = false;
                $recaptchaResponse->errorCodes = $array['error-codes'];
            }
        }

        return $recaptchaResponse;
    }
}

class ProjectReCaptcha extends ReCaptcha
{
    private static $publicKey = '6Lflsp0UAAAAAEzvdn93q0ai0XPPE_0fXB1bYqPs';
    private static $secretKey = '6Lflsp0UAAAAANYxxjqWrwU9lUNm_kdG2x8Pypf3';

    /**
     * @return string
     */
    public static function getPublicKey()
    {
        return self::$publicKey;
    }

    /**
     * @return string
     */
    public static function getSecretKey()
    {
        return self::$secretKey;
    }

    function __construct()
    {
        parent::ReCaptcha(self::$secretKey);
    }

    public function check()
    {
        $request = \Bitrix\Main\Application::getInstance()->getContext()->getRequest();

        $token = $_REQUEST['token'];
        $action = $_REQUEST['action'];
        $remoteIp = $request->getRemoteAddress();
        return self::verifyResponse($remoteIp, $token, $action);
    }

    public static function scripts($action)
    {
        ?>
        <script src="https://www.google.com/recaptcha/api.js?render=<?=self::$publicKey?>"></script>
        <script>
            let captcha_action = '<?=$action?>';

            grecaptcha.ready(function() {
                grecaptcha.execute('<?=self::$publicKey?>', {action: captcha_action})
                    .then(function(token) {
                        if (token) {
                            document.getElementById('token').value = token;
                            document.getElementById('action').value = captcha_action;
                        }
                    });
            });
        </script>
        <?
    }
}

?>
⚠️ **GitHub.com Fallback** ⚠️