@@ -32,31 +32,49 @@ public function checkCaptcha(ReCaptchaCheckEvent $event)
3232 $ event ->setHuman ($ this ->captchaVerified );
3333 return ;
3434 }
35- $ requestUrl = "https://www.google.com/recaptcha/api/siteverify " ;
35+
36+ if (!function_exists ('curl_init ' )) {
37+ $ this ->captchaVerified = false ;
38+ return ;
39+ }
3640
3741 $ secretKey = ReCaptcha::getConfigValue ('secret_key ' );
3842 $ minScore = ReCaptcha::getConfigValue ('min_score ' , 0.3 );
39- $ requestUrl .= "?secret= $ secretKey " ;
4043
4144 $ captchaResponse = $ event ->getCaptchaResponse ();
4245 if (null == $ captchaResponse && $ this ->request ) {
4346 $ captchaResponse = $ this ->request ->request ->get ('g-recaptcha-response ' );
4447 }
4548
46- $ requestUrl .= "&response= $ captchaResponse " ;
47-
4849 $ remoteIp = $ event ->getRemoteIp ();
4950 if (null == $ remoteIp && $ this ->request ) {
5051 $ remoteIp = $ this ->request ->server ->get ('REMOTE_ADDR ' );
5152 }
5253
53- $ requestUrl .= "&remoteip= $ remoteIp " ;
54-
55- $ result = json_decode (file_get_contents ($ requestUrl ), true );
56- if ($ result ['success ' ] == true && (!array_key_exists ('score ' , $ result ) || $ result ['score ' ] > $ minScore )) {
57- $ event ->setHuman (true );
58- $ this ->captchaVerified = true ;
59- return ;
54+ $ payload = http_build_query ([
55+ 'secret ' => (string ) $ secretKey ,
56+ 'response ' => (string ) $ captchaResponse ,
57+ 'remoteip ' => (string ) $ remoteIp ,
58+ ]);
59+
60+ $ curl = curl_init ('https://www.google.com/recaptcha/api/siteverify ' );
61+ curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
62+ curl_setopt ($ curl , CURLOPT_CONNECTTIMEOUT , 3 );
63+ curl_setopt ($ curl , CURLOPT_TIMEOUT , 5 );
64+ curl_setopt ($ curl , CURLOPT_POST , true );
65+ curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ payload );
66+ $ response = curl_exec ($ curl );
67+ curl_close ($ curl );
68+
69+ if (is_string ($ response )) {
70+ $ result = json_decode ($ response , true );
71+ if (is_array ($ result )
72+ && ($ result ['success ' ] ?? false ) === true
73+ && (!array_key_exists ('score ' , $ result ) || $ result ['score ' ] > $ minScore )) {
74+ $ event ->setHuman (true );
75+ $ this ->captchaVerified = true ;
76+ return ;
77+ }
6078 }
6179
6280 $ this ->captchaVerified = false ;
0 commit comments