/* addEventHandler( "main", "OnProlog", array( "CBitrixReCaptcha", "initJS" ) ); */ /* //Обработчик регистрации \Bitrix\Main\EventManager::getInstance()->addEventHandler( "main", "OnBeforeUserRegister", array( "CBitrixReCaptcha", "checkRegistration" ) ); */ class CBitrixReCaptcha { /* Секретные ключи */ private static $secret = "6LfY7tsaAAAAABXy3B8Q_OuOGkn0rBkHFgAC80qL"; //Секретный ключ private static $siteKey = "6LfY7tsaAAAAAGhpl8oUUlFzOaob0TQ5rz_ffifu"; //Ключ сайта private static $permissibleScore = 0.5; //допустимая оценка private static $hideBadge = "N"; const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; public static function verify($token) { $errorObject = [ 'success' => false, 'score' => false, 'isSuccess' => false, 'hostname' => null, 'action' => null, 'challenge_ts' => null, 'error_codes' => ['Empty token.'] ]; if (empty($token)) return $errorObject; $data = self::submit($token); if (!$data) return $errorObject; $success = isset($data['success']) ? $data['success'] : false; $score = isset($data['score']) ? $data['score'] : false; $isSuccess = $success && $score !== false ? self::$permissibleScore <= $score : false; $hostname = isset($data['hostname']) ? $data['hostname'] : null; $action = isset($data['action']) ? $data['action'] : null; $challenge_ts = isset($data['challenge_ts']) ? $data['challenge_ts'] : null; $error_codes = $data['error-codes']; return [ 'success' => $success, 'score' => $score, 'isSuccess' => $isSuccess, 'hostname' => $hostname, 'action' => $action, 'challenge_ts' => $challenge_ts, 'error_codes' => $error_codes ]; } public static function submit($token) { $params = ['secret' => self::$secret, 'response' => $token]; $params = http_build_query($params, '', '&'); $curl = curl_init(self::SITE_VERIFY_URL); curl_setopt_array($curl, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $params, CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'], CURLINFO_HEADER_OUT => false, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => true ]); $result = curl_exec($curl); curl_close($curl); return json_decode($result, true); } /** * Подключаем JS скрипты для reCaptcha v3 */ public function initJS() { $Asset = \Bitrix\Main\Page\Asset::getInstance(); $siteKey = self::$siteKey; $hideBadge = self::$hideBadge; if (empty($siteKey)) return true; $Asset->addString(''); $Asset->addString(''); // время жизни токена в секундах (2 минуты максимальное время жизни токена) $Asset->addString(''); if ($hideBadge == 'Y') $Asset->addString(''); } /** * Проверка при регистрации пользователя */ public function checkRegistration(&$arArgs) { return self::checkSpam(); } /** * Основной метод проверки капчи */ public function checkSpam() { global $APPLICATION; // из админки не проверяем // if (preg_match('/^\/bitrix\/admin\/.*$/i', $APPLICATION->GetCurPage())) return true; $isError = false; if( !$_REQUEST['recaptcha_token'] && $_POST['recaptcha_token'] ) { $_REQUEST['recaptcha_token'] = $_POST['recaptcha_token']; } $recaptcha_token = $_REQUEST['recaptcha_token']; if( isset($recaptcha_token) && !empty($recaptcha_token) ) { $secretKey = self::$siteKey; $permissibleScore = self::$permissibleScore; $response = self::verify($recaptcha_token); //echo "
"; print_r($response); echo "
"; if ( $response['isSuccess'] && ( floatval($response['score']) >= $permissibleScore ) ) { return true; } } return false; /* if ($isError) { $errorMessage = "Ошибка проверки"; $APPLICATION->ThrowException($errorMessage); return false; } */ } }//CBitrixReCaptcha
[RuntimeException] 
Could not start session because headers have already been sent. "/home/bitrix/www/bitrix/php_interface/recaptcha.php":1. (0)
/home/bitrix/www/bitrix/modules/main/lib/session/session.php:151
#0: Bitrix\Main\Session\Session->start
	/home/bitrix/www/bitrix/modules/main/lib/session/kernelsessionproxy.php:47
#1: Bitrix\Main\Session\KernelSessionProxy->start
	/home/bitrix/www/bitrix/modules/main/include.php:180
#2: require_once(string)
	/home/bitrix/www/bitrix/modules/main/include/prolog_before.php:19
#3: require_once(string)
	/home/bitrix/www/bitrix/modules/main/include/prolog.php:10
#4: require_once(string)
	/home/bitrix/www/bitrix/header.php:1
#5: require(string)
	/home/bitrix/www/courses/index.php:2
#6: include_once(string)
	/home/bitrix/www/bitrix/modules/main/include/urlrewrite.php:184
#7: include_once(string)
	/home/bitrix/www/bitrix/urlrewrite.php:2
----------