Skip to content

Commit 3875d02

Browse files
authored
Merge pull request google#242 from google/v1.2
Version 1.2
2 parents fde48bf + 5264969 commit 3875d02

36 files changed

+1554
-353
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/.php_cs.cache
2+
/build
13
/composer.lock
4+
/examples/config.php
25
/nbproject/private/
36
/vendor/

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ php:
99
- '5.6'
1010
- '7.0'
1111
- '7.1'
12-
- nightly
1312

1413
before_script:
1514
- composer install
1615
- phpenv version-name | grep ^5.[34] && echo "extension=apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true
1716
- phpenv version-name | grep ^5.[34] && echo "apc.enable_cli=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true
1817

1918
script:
20-
- vendor/bin/phpunit
19+
- mkdir -p build/logs
20+
- composer run-script lint
21+
- composer run-script test
22+
23+
after_success:
24+
- travis_retry php vendor/bin/php-coveralls
2125

2226
cache:
2327
directories:

ARCHITECTURE.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Architecture
2+
3+
The general pattern of usage is to instantiate the `ReCaptcha` class with your
4+
secret key, specify any additional validation rules, and then call `verify()`
5+
with the reCAPTCHA response and user's IP address. For example:
6+
7+
```php
8+
<?php
9+
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
10+
$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com')
11+
->verify($gRecaptchaResponse, $remoteIp);
12+
if ($resp->isSuccess()) {
13+
// Verified!
14+
} else {
15+
$errors = $resp->getErrorCodes();
16+
}
17+
```
18+
19+
By default, this will use the
20+
[`stream_context_create()`](https://secure.php.net/stream_context_create) and
21+
[`file_get_contents()`](https://secure.php.net/file_get_contents) to make a POST
22+
request to the reCAPTCHA service. This is handled by the
23+
[`RequestMethod\Post`](./src/ReCaptcha/RequestMethod/Post.php) class.
24+
25+
## Alternate request methods
26+
27+
You may need to use other methods for making requests in your environment. The
28+
[`ReCaptcha`](./src/ReCaptcha/ReCaptcha.php) class allows an optional
29+
[`RequestMethod`](./src/ReCaptcha/RequestMethod.php) instance to configure this.
30+
For example, if you want to use [cURL](https://secure.php.net/curl) instead you
31+
can do this:
32+
33+
```php
34+
<?php
35+
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
36+
```
37+
38+
Alternatively, you can also use a [socket](https://secure.php.net/fsockopen):
39+
40+
```php
41+
<?php
42+
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost());
43+
```
44+
45+
## Adding new request methods
46+
47+
Create a class that implements the
48+
[`RequestMethod`](./src/ReCaptcha/RequestMethod.php) interface. The convention
49+
is to name this class `RequestMethod\`_MethodType_`Post` and create a separate
50+
`RequestMethod\`_MethodType_ class that wraps just the calls to the network
51+
calls themselves. This means that the `RequestMethod\`_MethodType_`Post` can be
52+
unit tested by passing in a mock. Take a look at
53+
[`RequestMethod\CurlPost`](./src/ReCaptcha/RequestMethod/CurlPost.php) and
54+
[`RequestMethod\Curl`](./src/ReCaptcha/RequestMethod/Curl.php) with the matching
55+
[`RequestMethod/CurlPostTest`](./tests/ReCaptcha/RequestMethod/CurlPostTest.php)
56+
to see this pattern in action.

CONTRIBUTING.md

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
1-
Want to contribute? Great! First, read this page (including the small print at the end).
1+
# Contributing
22

3-
### Before you contribute
4-
Before we can use your code, you must sign the
5-
[Google Individual Contributor License Agreement](https://developers.google.com/open-source/cla/individual?csw=1)
3+
Want to contribute? Great! First, read this page (including the small print at
4+
the end).
5+
6+
## Contributor License Agreement
7+
8+
Before we can use your code, you must sign the [Google Individual Contributor
9+
License
10+
Agreement](https://developers.google.com/open-source/cla/individual?csw=1)
611
(CLA), which you can do online. The CLA is necessary mainly because you own the
712
copyright to your changes, even after your contribution becomes part of our
813
codebase, so we need your permission to use and distribute your code. We also
914
need to be sure of various other things—for instance that you'll tell us if you
1015
know that your code infringes on other people's patents. You don't have to sign
11-
the CLA until after you've submitted your code for review and a member has
12-
approved it, but you must do it before we can put your code into our codebase.
13-
Before you start working on a larger contribution, you should get in touch with
14-
us first through the issue tracker with your idea so that we can help out and
15-
possibly guide you. Coordinating up front makes it much easier to avoid
16-
frustration later on.
16+
the CLA until after you've submitted your code for review (a link will be
17+
automatically added to your Pull Request) and a member has approved it, but you
18+
must do it before we can put your code into our codebase. Before you start
19+
working on a larger contribution, you should get in touch with us first through
20+
the issue tracker with your idea so that we can help out and possibly guide you.
21+
Coordinating up front makes it much easier to avoid frustration later on.
22+
23+
## Linting and testing
24+
25+
We use PHP Coding Standards Fixer to maintain coding standards and PHPUnit to
26+
run our tests. For convenience, there are Composer scripts to run each of these:
1727

18-
### Code reviews
19-
All submissions, including submissions by project members, require review. We
20-
use GitHub pull requests for this purpose.
28+
```sh
29+
composer run-script lint
30+
composer run-script test
31+
```
32+
33+
These are run automatically by [Travis
34+
CI](https://travis-ci.org/google/recaptcha) against your Pull Request, but it's
35+
a good idea to run them locally before submission to avoid getting things
36+
bounced back. That said, tests can be a little daunting so feel free to submit
37+
your PR and ask for help.
38+
39+
## Code reviews
40+
41+
All submissions, including submissions by project members, require review.
42+
Reviews are conducted on the Pull Requests. The reviews are there to ensure and
43+
improve code quality, so treat them like a discussion and opportunity to learn.
44+
Don't get disheartened if your Pull Request isn't just automatically approved.
2145

2246
### The small print
23-
Contributions made by corporations are covered by a different agreement than
24-
the one above, the Software Grant and Corporate Contributor License Agreement.
47+
48+
Contributions made by corporations are covered by a different agreement than the
49+
one above, the Software Grant and Corporate Contributor License Agreement.

README.md

Lines changed: 90 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,139 @@
11
# reCAPTCHA PHP client library
22

33
[![Build Status](https://travis-ci.org/google/recaptcha.svg)](https://travis-ci.org/google/recaptcha)
4+
[![Coverage Status](https://coveralls.io/repos/github/google/recaptcha/badge.svg)](https://coveralls.io/github/google/recaptcha)
45
[![Latest Stable Version](https://poser.pugx.org/google/recaptcha/v/stable.svg)](https://packagist.org/packages/google/recaptcha)
56
[![Total Downloads](https://poser.pugx.org/google/recaptcha/downloads.svg)](https://packagist.org/packages/google/recaptcha)
67

7-
* Project page: http://www.google.com/recaptcha/
8-
* Repository: https://github.com/google/recaptcha
9-
* Version: 1.1.3
10-
* License: BSD, see [LICENSE](LICENSE)
11-
12-
## Description
13-
148
reCAPTCHA is a free CAPTCHA service that protect websites from spam and abuse.
15-
This is Google authored code that provides plugins for third-party integration
16-
with reCAPTCHA.
9+
This is a PHP library that wraps up the server-side verification step required
10+
to process responses from the reCAPTCHA service. This client supports both v2
11+
and v3.
12+
13+
- reCAPTCHA: https://www.google.com/recaptcha
14+
- This repo: https://github.com/google/recaptcha
15+
- Version: 1.2
16+
- License: BSD, see [LICENSE](LICENSE)
1717

1818
## Installation
1919

20-
### Composer (Recommended)
20+
### Composer (recommended)
2121

22-
[Composer](https://getcomposer.org/) is a widely used dependency manager for PHP
23-
packages. This reCAPTCHA client is available on Packagist as
24-
[`google/recaptcha`](https://packagist.org/packages/google/recaptcha) and can be
25-
installed either by running the `composer require` command or adding the library
26-
to your `composer.json`. To enable Composer for you project, refer to the
27-
project's [Getting Started](https://getcomposer.org/doc/00-intro.md)
28-
documentation.
22+
Use [Composer](https://getcomposer.org) to install this library from Packagist:
23+
[`google/recaptcha`](https://packagist.org/packages/google/recaptcha)
2924

30-
To add this dependency using the command, run the following from within your
31-
project directory:
32-
```
33-
composer require google/recaptcha "~1.1"
25+
Run the following command from your project directory to add the dependency:
26+
27+
```sh
28+
composer require google/recaptcha "^1.2"
3429
```
3530

3631
Alternatively, add the dependency directly to your `composer.json` file:
32+
3733
```json
3834
"require": {
39-
"google/recaptcha": "~1.1"
35+
"google/recaptcha": "^1.2"
4036
}
4137
```
4238

43-
### Direct download (no Composer)
39+
### Direct download
4440

45-
If you wish to install the library manually (i.e. without Composer), then you
46-
can use the links on the main project page to either clone the repo or download
47-
the [ZIP file](https://github.com/google/recaptcha/archive/master.zip). For
48-
convenience, an autoloader script is provided in `src/autoload.php` which you
49-
can require into your script instead of Composer's `vendor/autoload.php`. For
50-
example:
41+
Download the [ZIP file](https://github.com/google/recaptcha/archive/master.zip)
42+
and extract into your project. An autoloader script is provided in
43+
`src/autoload.php` which you can require into your script. For example:
5144

5245
```php
53-
require('/path/to/recaptcha/src/autoload.php');
46+
require_once '/path/to/recaptcha/src/autoload.php';
5447
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
5548
```
5649

5750
The classes in the project are structured according to the
58-
[PSR-4](http://www.php-fig.org/psr/psr-4/) standard, so you may of course also
59-
use your own autoloader or require the needed files directly in your code.
51+
[PSR-4](http://www.php-fig.org/psr/psr-4/) standard, so you can also use your
52+
own autoloader or require the needed files directly in your code.
6053

61-
### Development install
54+
## Usage
6255

63-
If you would like to contribute to this project or run the unit tests on within
64-
your own environment you will need to install the development dependencies, in
65-
this case that means [PHPUnit](https://phpunit.de/). If you clone the repo and
66-
run `composer install` from within the repo, this will also grab PHPUnit and all
67-
its dependencies for you. If you only need the autoloader installed, then you
68-
can always specify to Composer not to run in development mode, e.g. `composer
69-
install --no-dev`.
56+
First obtain the appropriate keys for the type of reCAPTCHA you wish to
57+
integrate for v2 at https://www.google.com/recaptcha/admin or v3 at
58+
https://g.co/recaptcha/v3.
7059

71-
*Note:* These dependencies are only required for development, there's no
72-
requirement for them to be included in your production code.
60+
Then follow the [integration guide on the developer
61+
site](https://developers.google.com/recaptcha/intro) to add the reCAPTCHA
62+
functionality into your frontend.
7363

74-
## Usage
64+
This library comes in when you need to verify the user's response. On the PHP
65+
side you need the response from the reCAPTCHA service and secret key from your
66+
credentials. Instantiate the `ReCaptcha` class with your secret key, specify any
67+
additional validation rules, and then call `verify()` with the reCAPTCHA
68+
response and user's IP address. For example:
7569

76-
First, register keys for your site at https://www.google.com/recaptcha/admin
70+
```php
71+
<?php
72+
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
73+
$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com')
74+
->verify($gRecaptchaResponse, $remoteIp);
75+
if ($resp->isSuccess()) {
76+
// Verified!
77+
} else {
78+
$errors = $resp->getErrorCodes();
79+
}
80+
```
81+
82+
The following methods are available:
83+
84+
- `setExpectedHostname($hostname)`: ensures the hostname matches. You must do
85+
this if you have disabled "Domain/Package Name Validation" for your
86+
credentials.
87+
- `setExpectedApkPackageName($apkPackageName)`: if you're verifying a response
88+
from an Android app. Again, you must do this if you have disabled
89+
"Domain/Package Name Validation" for your credentials.
90+
- `setExpectedAction($action)`: ensures the action matches for the v3 API.
91+
- `setScoreThreshold($threshold)`: set a score theshold for responses from the
92+
v3 API
93+
- `setChallengeTimeout($timeoutSeconds)`: set a timeout between the user passing
94+
the reCAPTCHA and your server processing it.
95+
96+
Each of the `set`\*`()` methods return the `ReCaptcha` instance so you can chain
97+
them together. For example:
7798

78-
When your app receives a form submission containing the `g-recaptcha-response`
79-
field, you can verify it using:
8099
```php
81100
<?php
82101
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
83-
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
102+
$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com')
103+
->setExpectedAction('homepage')
104+
->setScoreThreshold(0.5)
105+
->verify($gRecaptchaResponse, $remoteIp);
106+
84107
if ($resp->isSuccess()) {
85-
// verified!
86-
// if Domain Name Validation turned off don't forget to check hostname field
87-
// if($resp->getHostName() === $_SERVER['SERVER_NAME']) { }
108+
// Verified!
88109
} else {
89110
$errors = $resp->getErrorCodes();
90111
}
91112
```
92113

93-
You can see an end-to-end working example in
94-
[examples/example-captcha.php](examples/example-captcha.php)
114+
You can find the constants for the libraries error codes in the `ReCaptcha`
115+
class constants, e.g. `ReCaptcha::E_HOSTNAME_MISMATCH`
116+
117+
For more details on usage and structure, see [ARCHITECTURE](ARCHITECTURE.md).
95118

96-
## Upgrading
119+
### Examples
97120

98-
### From 1.0.0
121+
You can see examples of each reCAPTCHA type in [examples/](examples/). You can
122+
run the examples locally by using the Composer script:
123+
124+
```sh
125+
composer run-script serve-examples
126+
```
99127

100-
The previous version of this client is still available on the `1.0.0` tag [in
101-
this repo](https://github.com/google/recaptcha/tree/1.0.0) but it is purely for
102-
reference and will not receive any updates.
128+
This makes use of the in-built PHP dev server to host the examples at
129+
http://localhost:8080/
103130

104-
The major changes in 1.1.0 are:
105-
* installation now via Composer;
106-
* class loading also via Composer;
107-
* classes now namespaced;
108-
* old method call was `$rc->verifyResponse($remoteIp, $response)`, new call is
109-
`$rc->verify($response, $remoteIp)`
131+
These are also hosted on Google AppEngine Flexible environment at
132+
https://recaptcha-demo.appspot.com/. This is configured by
133+
[`app.yaml`](./app.yaml) which you can also use to [deploy to your own AppEngine
134+
project](https://cloud.google.com/appengine/docs/flexible/php/download).
110135

111136
## Contributing
112137

113-
We accept contributions via GitHub Pull Requests, but all contributors need to
114-
be covered by the standard Google Contributor License Agreement. You can find
115-
instructions for this in [CONTRIBUTING](CONTRIBUTING.md)
138+
No one ever has enough engineers, so we're very happy to accept contributions
139+
via Pull Requests. For details, see [CONTRIBUTING](CONTRIBUTING.md)

app.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
runtime: php
2+
env: flex
3+
4+
skip_files:
5+
- tests
6+
7+
runtime_config:
8+
document_root: examples

0 commit comments

Comments
 (0)