Skip to content

Commit

Permalink
dev/core#2150 Update checking of response function to work with v2 api
Browse files Browse the repository at this point in the history
Use Guzzle instead of fsockopen
  • Loading branch information
seamuslee001 committed Oct 29, 2020
1 parent 994f174 commit 724ba4a
Showing 1 changed file with 23 additions and 44 deletions.
67 changes: 23 additions & 44 deletions recaptcha/recaptchalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
*/
define("RECAPTCHA_API_SERVER", "http://www.google.com/recaptcha/api.js");
define("RECAPTCHA_API_SECURE_SERVER", "https://www.google.com/recaptcha/api.js");
define("RECAPTCHA_VERIFY_SERVER", "www.google.com");
define("RECAPTCHA_VERIFY_SERVER", "https://www.google.com");

use GuzzleHttp\Client;

/**
* Encodes the given data into a query string format
Expand Down Expand Up @@ -65,30 +67,14 @@ function _recaptcha_qsencode ($data) {
* @return array response
*/
function _recaptcha_http_post($host, $path, $data, $port = 80) {

$req = _recaptcha_qsencode ($data);

$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;

$response = '';
if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
die ('Could not open socket');
}

fwrite($fs, $http_request);

while ( !feof($fs) )
$response .= fgets($fs, 1160); // One TCP-IP packet
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);

return $response;
$client = new Client();
try {
$response = $client->request('POST', $host . '/' . $path, $data);
}
catch (Exception $e) {
return NULL;
}
return $response->getBody();
}


Expand Down Expand Up @@ -144,12 +130,11 @@ class ReCaptchaResponse {
* Calls an HTTP POST function to verify if the user's guess was correct
* @param string $privkey
* @param string $remoteip
* @param string $challenge
* @param string $response
* @param array $extra_params an array of extra variables to post to the server
* @return ReCaptchaResponse
*/
function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array())
function recaptcha_check_answer ($privkey, $remoteip, $response, $extra_params = array())
{
if ($privkey == null || $privkey == '') {
die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
Expand All @@ -162,32 +147,26 @@ function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $ex


//discard spam submissions
if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
if ($response == null || strlen($response) == 0) {
$recaptcha_response = new ReCaptchaResponse();
$recaptcha_response->is_valid = false;
$recaptcha_response->error = 'incorrect-captcha-sol';
return $recaptcha_response;
}

$response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/verify",
array (
'privatekey' => $privkey,
'remoteip' => $remoteip,
'challenge' => $challenge,
'response' => $response
) + $extra_params
);
$response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/siteverify",
[
'secret' => $privkey,
'remoteip' => $remoteip,
'response' => $response
] + $extra_params
);

$answers = explode ("\n", $response [1]);
$answers = json_decode($response, TRUE);
$recaptcha_response = new ReCaptchaResponse();

if (trim ($answers [0]) == 'true') {
$recaptcha_response->is_valid = true;
}
else {
$recaptcha_response->is_valid = false;
$recaptcha_response->error = $answers [1];
}
$recaptcha_response->is_valid = $answers['success'];
$recaptcha_response->error = $answers['error-codes'];
return $recaptcha_response;

}
Expand Down

0 comments on commit 724ba4a

Please sign in to comment.