| 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 cURL request to the reCAPTCHA service. |
| 43 |
* Note: this requires the cURL extension to be enabled in PHP |
| 44 |
* @see http://php.net/manual/en/book.curl.php |
| 45 |
*/ |
| 46 |
class CurlPost implements RequestMethod |
| 47 |
{ |
| 48 |
/** |
| 49 |
* Curl connection to the reCAPTCHA service |
| 50 |
* @var Curl |
| 51 |
*/ |
| 52 |
private $curl; |
| 53 |
|
| 54 |
/** |
| 55 |
* URL for reCAPTCHA siteverify API |
| 56 |
* @var string |
| 57 |
*/ |
| 58 |
private $siteVerifyUrl; |
| 59 |
|
| 60 |
/** |
| 61 |
* Only needed if you want to override the defaults |
| 62 |
* |
| 63 |
* @param Curl $curl Curl resource |
| 64 |
* @param string $siteVerifyUrl URL for reCAPTCHA siteverify API |
| 65 |
*/ |
| 66 |
public function __construct(Curl $curl = null, $siteVerifyUrl = null) |
| 67 |
{ |
| 68 |
$this->curl = (is_null($curl)) ? new Curl() : $curl; |
| 69 |
$this->siteVerifyUrl = (is_null($siteVerifyUrl)) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Submit the cURL request with the specified parameters. |
| 74 |
* |
| 75 |
* @param RequestParameters $params Request parameters |
| 76 |
* @return string Body of the reCAPTCHA response |
| 77 |
*/ |
| 78 |
public function submit(RequestParameters $params) |
| 79 |
{ |
| 80 |
$handle = $this->curl->init($this->siteVerifyUrl); |
| 81 |
|
| 82 |
$options = array( |
| 83 |
CURLOPT_POST => true, |
| 84 |
CURLOPT_POSTFIELDS => $params->toQueryString(), |
| 85 |
CURLOPT_HTTPHEADER => array( |
| 86 |
'Content-Type: application/x-www-form-urlencoded' |
| 87 |
), |
| 88 |
CURLINFO_HEADER_OUT => false, |
| 89 |
CURLOPT_HEADER => false, |
| 90 |
CURLOPT_RETURNTRANSFER => true, |
| 91 |
CURLOPT_SSL_VERIFYPEER => true |
| 92 |
); |
| 93 |
$this->curl->setoptArray($handle, $options); |
| 94 |
|
| 95 |
$response = $this->curl->exec($handle); |
| 96 |
$this->curl->close($handle); |
| 97 |
|
| 98 |
if ($response !== false) { |
| 99 |
return $response; |
| 100 |
} |
| 101 |
|
| 102 |
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; |
| 103 |
} |
| 104 |
} |
| 105 |
|