summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhass2015-09-03 00:10:53 +0200
committerhass2015-09-03 00:10:53 +0200
commit7d599fb6a7364de69a95d266d460ca6d925011a8 (patch)
tree5bdd499f6600db67c7e0f3ae545f7933b3bfd281
parent3717208f2d7de65e629a8e8226f3e71e0de649cd (diff)
Issue #2562117 by hass: Update reCAPTCHA lib to 1.1.2
-rw-r--r--recaptcha-php/README.md77
-rw-r--r--recaptcha-php/examples/example-captcha.php6
-rw-r--r--recaptcha-php/phpunit.xml.dist5
-rw-r--r--recaptcha-php/src/ReCaptcha/ReCaptcha.php2
-rw-r--r--recaptcha-php/src/ReCaptcha/RequestMethod/Curl.php74
-rw-r--r--recaptcha-php/src/ReCaptcha/RequestMethod/CurlPost.php88
-rw-r--r--recaptcha-php/src/ReCaptcha/RequestMethod/Socket.php1
-rw-r--r--recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php47
-rw-r--r--recaptcha-php/src/ReCaptcha/RequestParameters.php2
-rw-r--r--recaptcha-php/src/autoload.php38
-rw-r--r--recaptcha-php/tests/ReCaptcha/RequestMethod/CurlPostTest.php63
11 files changed, 359 insertions, 44 deletions
diff --git a/recaptcha-php/README.md b/recaptcha-php/README.md
index fac73b8..6e81a8e 100644
--- a/recaptcha-php/README.md
+++ b/recaptcha-php/README.md
@@ -6,7 +6,7 @@
* Project page: http://www.google.com/recaptcha/
* Repository: https://github.com/google/recaptcha
-* Version: 1.1.0
+* Version: 1.1.2
* License: BSD, see [LICENSE](LICENSE)
## Description
@@ -17,15 +17,60 @@ with reCAPTCHA.
## Installation
-Use [Composer](https://getcomposer.org/) to install the library. Either use
-`composer require google/recaptcha "~1.1"` or add the following to your
-`composer.json`:
+### Composer (Recommended)
+
+[Composer](https://getcomposer.org/) is a widely used dependency manager for PHP
+packages. This reCAPTCHA client is available on Packagist as
+[`google/recaptcha`](https://packagist.org/packages/google/recaptcha) and can be
+installed either by running the `composer require` command or adding the library
+to your `composer.json`. To enable Composer for you project, refer to the
+project's [Getting Started](https://getcomposer.org/doc/00-intro.md)
+documentation.
+
+To add this dependency using the command, run the following from within your
+project directory:
+```
+composer require google/recaptcha "~1.1"
+```
+
+Alternatively, add the dependency directly to your `composer.json` file:
```json
- "require": {
- "google/recaptcha": "~1.1"
- }
+"require": {
+ "google/recaptcha": "~1.1"
+}
+```
+
+### Direct download (no Composer)
+
+If you wish to install the library manually (i.e. without Composer), then you
+can use the links on the main project page to either clone the repo or download
+the [ZIP file](https://github.com/google/recaptcha/archive/master.zip). For
+convenience, an autoloader script is provided in `src/autoload.php` which you
+can require into your script instead of Composer's `vendor/autoload.php`. For
+example:
+
+```php
+require('/path/to/recaptcha/src/autoload.php');
+$recaptcha = new \ReCaptcha\ReCaptcha($secret);
```
+The classes in the project are structured according to the
+[PSR-4](http://www.php-fig.org/psr/psr-4/) standard, so you may of course also
+use your own autoloader or require the needed files directly in your code.
+
+### Development install
+
+If you would like to contribute to this project or run the unit tests on within
+your own environment you will need to install the development dependencies, in
+this case that means [PHPUnit](https://phpunit.de/). If you clone the repo and
+run `composer install` from within the repo, this will also grab PHPUnit and all
+its dependencies for you. If you only need the autoloader installed, then you
+can always specify to Composer not to run in development mode, e.g. `composer
+install --no-dev`.
+
+*Note:* These dependencies are only required for development, there's no
+requirement for them to be included in your production code.
+
## Usage
First, register keys for your site at https://www.google.com/recaptcha/admin
@@ -43,21 +88,23 @@ if ($resp->isSuccess()) {
}
```
-You can see an end-to-end working example in [examples/example-captcha.php](examples/example-captcha.php)
+You can see an end-to-end working example in
+[examples/example-captcha.php](examples/example-captcha.php)
## Upgrading
### From 1.0.0
-The previous version of this client is still available on the `1.0.0` tag
-[in this repo](https://github.com/google/recaptcha/tree/1.0.0) but it is purely
-for reference and will not receive any updates.
+The previous version of this client is still available on the `1.0.0` tag [in
+this repo](https://github.com/google/recaptcha/tree/1.0.0) but it is purely for
+reference and will not receive any updates.
The major changes in 1.1.0 are:
- * installation now via Composer;
- * class loading also via Composer;
- * classes now namespaced;
- * old method call was `$rc->verifyResponse($remoteIp, $response)`, new call is `$rc->verify($response, $remoteIp)`
+* installation now via Composer;
+* class loading also via Composer;
+* classes now namespaced;
+* old method call was `$rc->verifyResponse($remoteIp, $response)`, new call is
+ `$rc->verify($response, $remoteIp)`
## Contributing
diff --git a/recaptcha-php/examples/example-captcha.php b/recaptcha-php/examples/example-captcha.php
index e767239..8875171 100644
--- a/recaptcha-php/examples/example-captcha.php
+++ b/recaptcha-php/examples/example-captcha.php
@@ -30,7 +30,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
-// Initiate the autoloader.
+// Initiate the autoloader. The file should be generated by Composer.
+// You will provide your own autoloader or require the files directly if you did
+// not install via Composer.
require_once __DIR__ . '/../vendor/autoload.php';
// Register API keys at https://www.google.com/recaptcha/admin
@@ -98,7 +100,7 @@ elseif (isset($_POST['g-recaptcha-response'])):
<h2>Something went wrong</h2>
<p>The following error was returned: <?php
foreach ($resp->getErrorCodes() as $code) {
- echo '<tt>' . $code . '</tt> ';
+ echo '<tt>' , $code , '</tt> ';
}
?></p>
<p>Check the error code reference at <tt><a href="https://developers.google.com/recaptcha/docs/verify#error-code-reference">https://developers.google.com/recaptcha/docs/verify#error-code-reference</a></tt>.
diff --git a/recaptcha-php/phpunit.xml.dist b/recaptcha-php/phpunit.xml.dist
index c4a8896..4376678 100644
--- a/recaptcha-php/phpunit.xml.dist
+++ b/recaptcha-php/phpunit.xml.dist
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd"
+ xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd"
colors="true"
- verbose="true">
+ verbose="true"
+ bootstrap="src/autoload.php">
<testsuites>
<testsuite name="reCAPTCHA Test Suite">
<directory>tests/ReCaptcha/</directory>
diff --git a/recaptcha-php/src/ReCaptcha/ReCaptcha.php b/recaptcha-php/src/ReCaptcha/ReCaptcha.php
index 414c521..e2f7c34 100644
--- a/recaptcha-php/src/ReCaptcha/ReCaptcha.php
+++ b/recaptcha-php/src/ReCaptcha/ReCaptcha.php
@@ -35,7 +35,7 @@ class ReCaptcha
* Version of this client library.
* @const string
*/
- const VERSION = 'php_1.1.0';
+ const VERSION = 'php_1.1.2';
/**
* Shared secret for the site.
diff --git a/recaptcha-php/src/ReCaptcha/RequestMethod/Curl.php b/recaptcha-php/src/ReCaptcha/RequestMethod/Curl.php
new file mode 100644
index 0000000..20ee530
--- /dev/null
+++ b/recaptcha-php/src/ReCaptcha/RequestMethod/Curl.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * This is a PHP library that handles calling reCAPTCHA.
+ *
+ * @copyright Copyright (c) 2015, Google Inc.
+ * @link http://www.google.com/recaptcha
+ *
+ * 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:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * 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.
+ */
+
+namespace ReCaptcha\RequestMethod;
+
+/**
+ * Convenience wrapper around the cURL functions to allow mocking.
+ */
+class Curl
+{
+
+ /**
+ * @see http://php.net/curl_init
+ * @param string $url
+ * @return resource cURL handle
+ */
+ public function init($url = null)
+ {
+ return curl_init($url);
+ }
+
+ /**
+ * @see http://php.net/curl_setopt_array
+ * @param resource $ch
+ * @param array $options
+ * @return bool
+ */
+ public function setoptArray($ch, array $options)
+ {
+ return curl_setopt_array($ch, $options);
+ }
+
+ /**
+ * @see http://php.net/curl_exec
+ * @param resource $ch
+ * @return mixed
+ */
+ public function exec($ch)
+ {
+ return curl_exec($ch);
+ }
+
+ /**
+ * @see http://php.net/curl_close
+ * @param resource $ch
+ */
+ public function close($ch)
+ {
+ curl_close($ch);
+ }
+}
diff --git a/recaptcha-php/src/ReCaptcha/RequestMethod/CurlPost.php b/recaptcha-php/src/ReCaptcha/RequestMethod/CurlPost.php
new file mode 100644
index 0000000..e06d1cc
--- /dev/null
+++ b/recaptcha-php/src/ReCaptcha/RequestMethod/CurlPost.php
@@ -0,0 +1,88 @@
+<?php
+/**
+ * This is a PHP library that handles calling reCAPTCHA.
+ *
+ * @copyright Copyright (c) 2015, Google Inc.
+ * @link http://www.google.com/recaptcha
+ *
+ * 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:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * 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.
+ */
+
+namespace ReCaptcha\RequestMethod;
+
+use ReCaptcha\RequestMethod;
+use ReCaptcha\RequestParameters;
+
+/**
+ * Sends cURL request to the reCAPTCHA service.
+ * Note: this requires the cURL extension to be enabled in PHP
+ * @see http://php.net/manual/en/book.curl.php
+ */
+class CurlPost implements RequestMethod
+{
+ /**
+ * URL to which requests are sent via cURL.
+ * @const string
+ */
+ const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
+
+ /**
+ * Curl connection to the reCAPTCHA service
+ * @var Curl
+ */
+ private $curl;
+
+ public function __construct(Curl $curl = null)
+ {
+ if (!is_null($curl)) {
+ $this->curl = $curl;
+ } else {
+ $this->curl = new Curl();
+ }
+ }
+
+ /**
+ * Submit the cURL request with the specified parameters.
+ *
+ * @param RequestParameters $params Request parameters
+ * @return string Body of the reCAPTCHA response
+ */
+ public function submit(RequestParameters $params)
+ {
+ $handle = $this->curl->init(self::SITE_VERIFY_URL);
+
+ $options = array(
+ CURLOPT_POST => true,
+ CURLOPT_POSTFIELDS => $params->toQueryString(),
+ CURLOPT_HTTPHEADER => array(
+ 'Content-Type: application/x-www-form-urlencoded'
+ ),
+ CURLINFO_HEADER_OUT => false,
+ CURLOPT_HEADER => false,
+ CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_SSL_VERIFYPEER => true
+ );
+ $this->curl->setoptArray($handle, $options);
+
+ $response = $this->curl->exec($handle);
+ $this->curl->close($handle);
+
+ return $response;
+ }
+}
diff --git a/recaptcha-php/src/ReCaptcha/RequestMethod/Socket.php b/recaptcha-php/src/ReCaptcha/RequestMethod/Socket.php
index e74fc49..d3c8792 100644
--- a/recaptcha-php/src/ReCaptcha/RequestMethod/Socket.php
+++ b/recaptcha-php/src/ReCaptcha/RequestMethod/Socket.php
@@ -74,6 +74,7 @@ class Socket
*
* @see http://php.net/fgets
* @param int $length
+ * @return string
*/
public function fgets($length = null)
{
diff --git a/recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php b/recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php
index 4f0c61a..4754121 100644
--- a/recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php
+++ b/recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php
@@ -30,15 +30,15 @@ use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
/**
- * Sends a POST request to the reCAPTCHA service, but makes use of fsockopen()
- * instead of get_file_contents(). This is to account for people who may be on
+ * Sends a POST request to the reCAPTCHA service, but makes use of fsockopen()
+ * instead of get_file_contents(). This is to account for people who may be on
* servers where allow_furl_open is disabled.
*/
class SocketPost implements RequestMethod
{
/**
* reCAPTCHA service host.
- * @const string
+ * @const string
*/
const RECAPTCHA_HOST = 'www.google.com';
@@ -65,7 +65,7 @@ class SocketPost implements RequestMethod
/**
* Constructor
- *
+ *
* @param \ReCaptcha\RequestMethod\Socket $socket optional socket, injectable for testing
*/
public function __construct(Socket $socket = null)
@@ -88,33 +88,34 @@ class SocketPost implements RequestMethod
$errno = 0;
$errstr = '';
- if ($this->socket->fsockopen('ssl://' . self::RECAPTCHA_HOST, 443, $errno, $errstr, 30) !== false) {
- $content = $params->toQueryString();
+ if (false === $this->socket->fsockopen('ssl://' . self::RECAPTCHA_HOST, 443, $errno, $errstr, 30)) {
+ return self::BAD_REQUEST;
+ }
- $request = "POST " . self::SITE_VERIFY_PATH . " HTTP/1.1\r\n";
- $request .= "Host: " . self::RECAPTCHA_HOST . "\r\n";
- $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
- $request .= "Content-length: " . strlen($content) . "\r\n";
- $request .= "Connection: close\r\n\r\n";
- $request .= $content . "\r\n\r\n";
+ $content = $params->toQueryString();
- $this->socket->fwrite($request);
- $response = '';
+ $request = "POST " . self::SITE_VERIFY_PATH . " HTTP/1.1\r\n";
+ $request .= "Host: " . self::RECAPTCHA_HOST . "\r\n";
+ $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
+ $request .= "Content-length: " . strlen($content) . "\r\n";
+ $request .= "Connection: close\r\n\r\n";
+ $request .= $content . "\r\n\r\n";
- while (!$this->socket->feof()) {
- $response .= $this->socket->fgets(4096);
- }
+ $this->socket->fwrite($request);
+ $response = '';
- $this->socket->fclose();
+ while (!$this->socket->feof()) {
+ $response .= $this->socket->fgets(4096);
+ }
- if (0 === strpos($response, 'HTTP/1.1 200 OK')) {
- $parts = preg_split("#\n\s*\n#Uis", $response);
- return $parts[1];
- }
+ $this->socket->fclose();
+ if (0 !== strpos($response, 'HTTP/1.1 200 OK')) {
return self::BAD_RESPONSE;
}
- return self::BAD_REQUEST;
+ $parts = preg_split("#\n\s*\n#Uis", $response);
+
+ return $parts[1];
}
}
diff --git a/recaptcha-php/src/ReCaptcha/RequestParameters.php b/recaptcha-php/src/ReCaptcha/RequestParameters.php
index f446842..cb66f26 100644
--- a/recaptcha-php/src/ReCaptcha/RequestParameters.php
+++ b/recaptcha-php/src/ReCaptcha/RequestParameters.php
@@ -98,6 +98,6 @@ class RequestParameters
*/
public function toQueryString()
{
- return http_build_query($this->toArray());
+ return http_build_query($this->toArray(), '', '&');
}
}
diff --git a/recaptcha-php/src/autoload.php b/recaptcha-php/src/autoload.php
new file mode 100644
index 0000000..a53cbd7
--- /dev/null
+++ b/recaptcha-php/src/autoload.php
@@ -0,0 +1,38 @@
+<?php
+
+/* An autoloader for ReCaptcha\Foo classes. This should be require()d
+ * by the user before attempting to instantiate any of the ReCaptcha
+ * classes.
+ */
+
+spl_autoload_register(function ($class) {
+ if (substr($class, 0, 10) !== 'ReCaptcha\\') {
+ /* If the class does not lie under the "ReCaptcha" namespace,
+ * then we can exit immediately.
+ */
+ return;
+ }
+
+ /* All of the classes have names like "ReCaptcha\Foo", so we need
+ * to replace the backslashes with frontslashes if we want the
+ * name to map directly to a location in the filesystem.
+ */
+ $class = str_replace('\\', '/', $class);
+
+ /* First, check under the current directory. It is important that
+ * we look here first, so that we don't waste time searching for
+ * test classes in the common case.
+ */
+ $path = dirname(__FILE__).'/'.$class.'.php';
+ if (is_readable($path)) {
+ require_once $path;
+ }
+
+ /* If we didn't find what we're looking for already, maybe it's
+ * a test class?
+ */
+ $path = dirname(__FILE__).'/../tests/'.$class.'.php';
+ if (is_readable($path)) {
+ require_once $path;
+ }
+});
diff --git a/recaptcha-php/tests/ReCaptcha/RequestMethod/CurlPostTest.php b/recaptcha-php/tests/ReCaptcha/RequestMethod/CurlPostTest.php
new file mode 100644
index 0000000..892e207
--- /dev/null
+++ b/recaptcha-php/tests/ReCaptcha/RequestMethod/CurlPostTest.php
@@ -0,0 +1,63 @@
+<?php
+/**
+ * This is a PHP library that handles calling reCAPTCHA.
+ *
+ * @copyright Copyright (c) 2015, Google Inc.
+ * @link http://www.google.com/recaptcha
+ *
+ * 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:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * 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.
+ */
+
+namespace ReCaptcha\RequestMethod;
+
+use \ReCaptcha\RequestParameters;
+
+class CurlPostTest extends \PHPUnit_Framework_TestCase
+{
+
+ protected function setUp()
+ {
+ if (!extension_loaded('curl')) {
+ $this->markTestSkipped(
+ 'The cURL extension is not available.'
+ );
+ }
+ }
+
+ public function testSubmit()
+ {
+ $curl = $this->getMock('\\ReCaptcha\\RequestMethod\\Curl',
+ array('init', 'setoptArray', 'exec', 'close'));
+ $curl->expects($this->once())
+ ->method('init')
+ ->willReturn(new \stdClass);
+ $curl->expects($this->once())
+ ->method('setoptArray')
+ ->willReturn(true);
+ $curl->expects($this->once())
+ ->method('exec')
+ ->willReturn('RESPONSEBODY');
+ $curl->expects($this->once())
+ ->method('close');
+
+ $pc = new CurlPost($curl);
+ $response = $pc->submit(new RequestParameters("secret", "response"));
+ $this->assertEquals('RESPONSEBODY', $response);
+ }
+}