├── LICENSE ├── composer.json └── src ├── Recaptcha.php └── RecaptchaException.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Vincent Klaiber 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vinkla/recaptcha", 3 | "description": "An easy-to-use reCAPTCHA package", 4 | "keywords": [ 5 | "recaptcha", 6 | "google", 7 | "security" 8 | ], 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Vincent Klaiber", 13 | "homepage": "https://github.com/vinkla" 14 | } 15 | ], 16 | "require": { 17 | "php": "^7.2", 18 | "php-http/client-implementation": "^1.0", 19 | "php-http/discovery": "^1.4", 20 | "php-http/httplug": "^1.1 || ^2.0", 21 | "php-http/message-factory": "^1.0" 22 | }, 23 | "require-dev": { 24 | "php-http/guzzle6-adapter": "^1.1", 25 | "php-http/message": "^1.7", 26 | "php-http/mock-client": "^1.1", 27 | "phpunit/phpunit": "^8.0" 28 | }, 29 | "config": { 30 | "preferred-install": "dist" 31 | }, 32 | "extra": { 33 | "branch-alias": { 34 | "dev-master": "4.2-dev" 35 | } 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "Vinkla\\Recaptcha\\": "src/" 40 | } 41 | }, 42 | "autoload-dev": { 43 | "psr-4": { 44 | "Vinkla\\Tests\\Recaptcha\\": "tests/" 45 | } 46 | }, 47 | "minimum-stability": "dev", 48 | "prefer-stable": true 49 | } 50 | -------------------------------------------------------------------------------- /src/Recaptcha.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | declare(strict_types=1); 13 | 14 | namespace Vinkla\Recaptcha; 15 | 16 | use Http\Client\HttpClient; 17 | use Http\Discovery\HttpClientDiscovery; 18 | use Http\Discovery\MessageFactoryDiscovery; 19 | use Http\Message\RequestFactory; 20 | 21 | /** 22 | * This is the recaptcha class. 23 | * 24 | * @author Vincent Klaiber 25 | */ 26 | class Recaptcha 27 | { 28 | /** 29 | * The secret key. 30 | * 31 | * @var string 32 | */ 33 | protected $secret; 34 | 35 | /** 36 | * The http client. 37 | * 38 | * @var \Http\Client\HttpClient 39 | */ 40 | protected $httpClient; 41 | 42 | /** 43 | * The http request factory. 44 | * 45 | * @var \Http\Message\RequestFactory 46 | */ 47 | protected $requestFactory; 48 | 49 | /** 50 | * Create a new recaptcha instance. 51 | * 52 | * @param string $secret 53 | * @param \Http\Client\HttpClient|null $httpClient 54 | * @param \Http\Message\RequestFactory|null $requestFactory 55 | * 56 | * @return void 57 | */ 58 | public function __construct(string $secret, HttpClient $httpClient = null, RequestFactory $requestFactory = null) 59 | { 60 | $this->secret = $secret; 61 | $this->httpClient = $httpClient ?: HttpClientDiscovery::find(); 62 | $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); 63 | } 64 | 65 | /** 66 | * Verify the response string. 67 | * 68 | * @param string $response 69 | * @param string $ip 70 | * 71 | * @throws \Vinkla\Recaptcha\RecaptchaException 72 | * 73 | * @return object 74 | */ 75 | public function verify(string $response, string $ip = null): object 76 | { 77 | $uri = 'https://www.google.com/recaptcha/api/siteverify'; 78 | 79 | $headers = [ 80 | 'Content-Type' => 'application/x-www-form-urlencoded', 81 | ]; 82 | 83 | $body = http_build_query(array_filter([ 84 | 'secret' => $this->secret, 85 | 'response' => $response, 86 | 'remoteip' => $ip, 87 | ])); 88 | 89 | $request = $this->requestFactory->createRequest('POST', $uri, $headers, $body); 90 | 91 | $response = $this->httpClient->sendRequest($request); 92 | 93 | $data = json_decode((string) $response->getBody()); 94 | 95 | if (!isset($data->success) || !$data->success) { 96 | if (property_exists($data, 'error-codes')) { 97 | $message = $this->getErrorMessage($data->{'error-codes'}[0]); 98 | 99 | throw new RecaptchaException($message); 100 | } 101 | 102 | throw new RecaptchaException('Invalid reCAPTCHA response.'); 103 | } 104 | 105 | return $data; 106 | } 107 | 108 | /** 109 | * Get the error message by code. 110 | * 111 | * @param string $code 112 | * 113 | * @see https://developers.google.com/recaptcha/docs/verify#error-code-reference 114 | * 115 | * @return string 116 | */ 117 | protected function getErrorMessage(string $code): string 118 | { 119 | $messages = [ 120 | 'bad-request' => 'The request is invalid or malformed.', 121 | 'invalid-input-response' => 'The response parameter is invalid or malformed.', 122 | 'invalid-input-secret' => 'The secret parameter is invalid or malformed.', 123 | 'missing-input-response' => 'The response parameter is missing.', 124 | 'missing-input-secret' => 'The secret parameter is missing.', 125 | ]; 126 | 127 | return $messages[$code] ?? 'Invalid reCAPTCHA response.'; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/RecaptchaException.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | declare(strict_types=1); 13 | 14 | namespace Vinkla\Recaptcha; 15 | 16 | use RuntimeException; 17 | 18 | /** 19 | * This is the recaptcha exception class. 20 | * 21 | * @author Vincent Klaiber 22 | */ 23 | class RecaptchaException extends RuntimeException 24 | { 25 | // 26 | } 27 | --------------------------------------------------------------------------------