Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ReCaptcha/RequestMethod/CurlPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
use ReCaptcha\Response;

/**
* Sends cURL request to the reCAPTCHA service.
Expand Down Expand Up @@ -98,7 +99,7 @@ public function submit(RequestParameters $params): string
return $response;
}

return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED);
} finally {
curl_close($handle);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ReCaptcha/RequestMethod/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
use ReCaptcha\Response;

/**
* Sends POST requests to the reCAPTCHA service.
Expand Down Expand Up @@ -87,6 +88,6 @@ public function submit(RequestParameters $params): string
return $response;
}

return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED);
}
}
11 changes: 6 additions & 5 deletions src/ReCaptcha/RequestMethod/SocketPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
use ReCaptcha\Response;

/**
* Sends a POST request to the reCAPTCHA service, but makes use of fsockopen()
Expand Down Expand Up @@ -76,17 +77,17 @@ public function submit(RequestParameters $params): string
$urlParsed = parse_url($this->siteVerifyUrl);

if (false === $urlParsed || !isset($urlParsed['host']) || !isset($urlParsed['path'])) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED);
}

$handle = fsockopen('ssl://'.$urlParsed['host'], 443, $errno, $errstr, 30);

if (false === $handle || 0 !== $errno || '' !== $errstr) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED);
}

if (false === stream_set_timeout($handle, 60)) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED);
}

$content = $params->toQueryString();
Expand All @@ -108,13 +109,13 @@ public function submit(RequestParameters $params): string
}

if (1 !== preg_match('#^HTTP/1\.[01] 200 OK#', $response)) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}';
return Response::errorJson(ReCaptcha::E_BAD_RESPONSE);
}

$parts = preg_split("#\n\\s*\n#Uis", $response);

if (!is_array($parts) || !isset($parts[1])) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}';
return Response::errorJson(ReCaptcha::E_BAD_RESPONSE);
}

return $parts[1];
Expand Down
12 changes: 12 additions & 0 deletions src/ReCaptcha/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
/**
* Score assigned to the request.
*/
private ?float $score;

Check failure on line 77 in src/ReCaptcha/Response.php

View workflow job for this annotation

GitHub Actions / build (8.4)

Method ReCaptcha\Response::errorJson() should return string but returns string|false.

/**
* Action as specified by the page.
Expand All @@ -97,6 +97,18 @@
$this->errorCodes = $errorCodes;
}

/**
* Build an error response JSON string with the specified error code.
*
* @param string $errorCode The error code to return
*
* @return string The JSON string
*/
public static function errorJson(string $errorCode): string
{
return json_encode(['success' => false, 'error-codes' => [$errorCode]]);
}

/**
* Build the response from the expected JSON returned by the service.
*/
Expand Down
Loading