| 1 |
<?php |
| 2 |
/** |
| 3 |
* This is a PHP library that handles calling reCAPTCHA. |
| 4 |
* |
| 5 |
* BSD 3-Clause License |
| 6 |
* @copyright (c) 2019, Google Inc. |
| 7 |
* @link https://www.google.com/recaptcha |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or without |
| 11 |
* modification, are permitted provided that the following conditions are met: |
| 12 |
* 1. Redistributions of source code must retain the above copyright notice, this |
| 13 |
* list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
* this list of conditions and the following disclaimer in the documentation |
| 17 |
* and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. Neither the name of the copyright holder nor the names of its |
| 20 |
* contributors may be used to endorse or promote products derived from |
| 21 |
* this software without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 24 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 29 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 31 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
*/ |
| 34 |
|
| 35 |
namespace ReCaptcha\RequestMethod; |
| 36 |
|
| 37 |
use ReCaptcha\ReCaptcha; |
| 38 |
use ReCaptcha\RequestMethod; |
| 39 |
use ReCaptcha\RequestParameters; |
| 40 |
|
| 41 |
/** |
| 42 |
* Sends a POST request to the reCAPTCHA service, but makes use of fsockopen() |
| 43 |
* instead of get_file_contents(). This is to account for people who may be on |
| 44 |
* servers where allow_url_open is disabled. |
| 45 |
*/ |
| 46 |
class SocketPost implements RequestMethod |
| 47 |
{ |
| 48 |
/** |
| 49 |
* Socket to the reCAPTCHA service |
| 50 |
* @var Socket |
| 51 |
*/ |
| 52 |
private $socket; |
| 53 |
|
| 54 |
/** |
| 55 |
* Only needed if you want to override the defaults |
| 56 |
* |
| 57 |
* @param \ReCaptcha\RequestMethod\Socket $socket optional socket, injectable for testing |
| 58 |
* @param string $siteVerifyUrl URL for reCAPTCHA siteverify API |
| 59 |
*/ |
| 60 |
public function __construct(Socket $socket = null, $siteVerifyUrl = null) |
| 61 |
{ |
| 62 |
$this->socket = (is_null($socket)) ? new Socket() : $socket; |
| 63 |
$this->siteVerifyUrl = (is_null($siteVerifyUrl)) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Submit the POST request with the specified parameters. |
| 68 |
* |
| 69 |
* @param RequestParameters $params Request parameters |
| 70 |
* @return string Body of the reCAPTCHA response |
| 71 |
*/ |
| 72 |
public function submit(RequestParameters $params) |
| 73 |
{ |
| 74 |
$errno = 0; |
| 75 |
$errstr = ''; |
| 76 |
$urlParsed = parse_url($this->siteVerifyUrl); |
| 77 |
|
| 78 |
if (false === $this->socket->fsockopen('ssl://' . $urlParsed['host'], 443, $errno, $errstr, 30)) { |
| 79 |
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; |
| 80 |
} |
| 81 |
|
| 82 |
$content = $params->toQueryString(); |
| 83 |
|
| 84 |
$request = "POST " . $urlParsed['path'] . " HTTP/1.1\r\n"; |
| 85 |
$request .= "Host: " . $urlParsed['host'] . "\r\n"; |
| 86 |
$request .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
| 87 |
$request .= "Content-length: " . strlen($content) . "\r\n"; |
| 88 |
$request .= "Connection: close\r\n\r\n"; |
| 89 |
$request .= $content . "\r\n\r\n"; |
| 90 |
|
| 91 |
$this->socket->fwrite($request); |
| 92 |
$response = ''; |
| 93 |
|
| 94 |
while (!$this->socket->feof()) { |
| 95 |
$response .= $this->socket->fgets(4096); |
| 96 |
} |
| 97 |
|
| 98 |
$this->socket->fclose(); |
| 99 |
|
| 100 |
if (0 !== strpos($response, 'HTTP/1.1 200 OK')) { |
| 101 |
return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; |
| 102 |
} |
| 103 |
|
| 104 |
$parts = preg_split("#\n\s*\n#Uis", $response); |
| 105 |
|
| 106 |
return $parts[1]; |
| 107 |
} |
| 108 |
} |
| 109 |
|