Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 457939f

Browse files
committed
Merge branch 'develop' into release/v3
* develop: HHVM removed from travis yml Version renamed deprecated array_get helper replaced with Arr::get new ReCaptchaBuilder class constants: DEFAULT_API_VERSION and DEFAULT_CURL_TIMEOUT Update recaptcha.php make curl timeout of server token validation call configurable Update bug_report.md Update README.md Update README.md Update README.md coverage badge added to README.md Update README.md Update README.md Update README.md Update README.md removed "before" from scrutinizer yml Update .scrutinizer.yml
2 parents 15052ad + 3d5d630 commit 457939f

13 files changed

+109
-35
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ A clear and concise description of what you expected to happen.
2424
If applicable, add screenshots to help explain your problem.
2525

2626
**Environment:**
27-
- OS: [e.g. Ubuntu 16.04]
27+
- OS and server: [e.g. Ubuntu 16.04 / Nginx] (If you are using a VM on Windows DON'T "say" Windows)
2828
- PHP version [7.1.x]
29+
- Laravel version [e.g. 5.5]
30+
- Package version [e.g. 3.4.1]
2931

3032
**Additional context**
31-
Add any other context about the problem here.
33+
Add any other context about the problem here, like code snippets.

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ build:
66
- command: vendor/bin/phpunit --coverage-clover=clover.xml
77
coverage:
88
file: clover.xml
9-
format: clover
9+
format: clover

.travis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ php:
44
- 7.1
55
- 7.2
66
- 7.3
7-
- hhvm
87

98
before_script:
109
- travis_retry composer self-update
1110
- travis_retry composer install --prefer-source --no-interaction
12-
- curl -sSfL -o ~/.phpenv/versions/hhvm/bin/phpunit https://phar.phpunit.de/phpunit-5.7.phar
1311

14-
matrix:
15-
allow_failures:
16-
- php: hhvm
1712
fast_finish: true

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ Available reCAPTCHA versions:
66
* v2 Checkbox
77
* v3
88

9-
[![Build Status](https://travis-ci.org/biscolab/laravel-recaptcha.svg?branch=master)](https://travis-ci.org/biscolab/laravel-recaptcha) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/biscolab/laravel-recaptcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/biscolab/laravel-recaptcha/?branch=master) [![Packagist version](https://img.shields.io/packagist/v/biscolab/laravel-recaptcha.svg#img-thumbnail)](https://packagist.org/packages/biscolab/laravel-recaptcha)
9+
[![Build Status](https://travis-ci.org/biscolab/laravel-recaptcha.svg?branch=master)](https://travis-ci.org/biscolab/laravel-recaptcha) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/biscolab/laravel-recaptcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/biscolab/laravel-recaptcha/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/biscolab/laravel-recaptcha/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/biscolab/laravel-recaptcha/?branch=master)
10+
[![Downloads](https://img.shields.io/packagist/dt/biscolab/laravel-recaptcha.svg#img-thumbnail)](https://packagist.org/packages/biscolab/laravel-recaptcha)
11+
[![Packagist version](https://img.shields.io/packagist/v/biscolab/laravel-recaptcha.svg#img-thumbnail)](https://packagist.org/packages/biscolab/laravel-recaptcha)
1012

1113
## System requirements
12-
| Package version | PHP version | Laravel version |
13-
|-----------------|-------------|-----------------|
14-
| 3.x | 7.1 or greater | 5.5 or greater |
15-
| 2.x | 5.5.9, 7.0 or greater | 5.0 or greater |
16-
17-
Are you still using PHP 5.x or 7.0? Please go to [V2](https://github.com/biscolab/laravel-recaptcha/tree/v2.0.4)
14+
| Package | reCAPTCHA | PHP | Laravel | Docs |
15+
|-----------------|-------------|-------------|-----------------|-------------------|
16+
| 3.x |v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater | [latest](https://laravel-recaptcha-docs.biscolab.com) |
17+
| 2.x | v2 Invisible, v2 Checkbox | 5.5.9, 7.0 or greater | 5.0 or greater | [v2.0.4](https://laravel-recaptcha-docs.biscolab.com/docs/2.0.4/intro) |
1818

1919
## !!! Documentation !!!
2020

config/recaptcha.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
*/
4040
'version' => env('RECAPTCHA_DEFAULT_VERSION', 'v2'),
4141

42+
/**
43+
*
44+
* The curl timout in seconds to validate a recaptcha token
45+
* @since v3.5.0
46+
*
47+
*/
48+
'curl_timeout' => env('RECAPTCHA_CURL_TIMEOUT', 10),
49+
4250
/**
4351
*
4452
* IP addresses for which validation will be skipped
@@ -61,4 +69,4 @@
6169
*
6270
*/
6371
'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token')
64-
];
72+
];

src/ReCaptchaBuilder.php

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@
1111
namespace Biscolab\ReCaptcha;
1212

1313
use Exception;
14+
use Illuminate\Support\Arr;
1415

16+
/**
17+
* Class ReCaptchaBuilder
18+
* @package Biscolab\ReCaptcha
19+
*/
1520
class ReCaptchaBuilder {
1621

22+
/**
23+
* @var string
24+
*/
25+
const DEFAULT_API_VERSION = 'v2';
26+
27+
/**
28+
* @var int
29+
*/
30+
const DEFAULT_CURL_TIMEOUT = 10;
31+
1732
/**
1833
* The Site key
1934
* please visit https://developers.google.com/recaptcha/docs/start
@@ -35,6 +50,13 @@ class ReCaptchaBuilder {
3550
*/
3651
protected $version;
3752

53+
/**
54+
* The curl timeout
55+
* please visit https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html
56+
* @var int
57+
*/
58+
protected $curl_timeout;
59+
3860
/**
3961
* Whether is true the ReCAPTCHA is inactive
4062
* @var boolean
@@ -46,11 +68,25 @@ class ReCaptchaBuilder {
4668
*/
4769
protected $api_url = 'https://www.google.com/recaptcha/api/siteverify';
4870

49-
public function __construct($api_site_key, $api_secret_key, $version = 'v2') {
71+
/**
72+
* ReCaptchaBuilder constructor.
73+
*
74+
* @param string $api_site_key
75+
* @param string $api_secret_key
76+
* @param null|string $version
77+
* @param int|null $curl_timeout
78+
*/
79+
public function __construct(
80+
string $api_site_key,
81+
string $api_secret_key,
82+
?string $version = self::DEFAULT_API_VERSION,
83+
?int $curl_timeout = self::DEFAULT_CURL_TIMEOUT
84+
) {
5085

5186
$this->setApiSiteKey($api_site_key);
5287
$this->setApiSecretKey($api_secret_key);
5388
$this->setVersion($version);
89+
$this->setCurlTimeout($curl_timeout);
5490
$this->setSkipByIp($this->skipByIp());
5591
}
5692

@@ -78,6 +114,26 @@ public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder {
78114
return $this;
79115
}
80116

117+
/**
118+
* @param int $curl_timeout
119+
*
120+
* @return ReCaptchaBuilder
121+
*/
122+
public function setCurlTimeout(int $curl_timeout): ReCaptchaBuilder {
123+
124+
$this->curl_timeout = $curl_timeout;
125+
126+
return $this;
127+
}
128+
129+
/**
130+
* @return int
131+
*/
132+
public function getCurlTimeout(): int {
133+
134+
return $this->curl_timeout;
135+
}
136+
81137
/**
82138
* @param string $version
83139
*
@@ -114,9 +170,10 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder {
114170
* @return array|mixed
115171
*/
116172
public function getIpWhitelist() {
173+
117174
$whitelist = config('recaptcha.skip_ip', []);
118175

119-
if(!is_array($whitelist)) {
176+
if (!is_array($whitelist)) {
120177
$whitelist = explode(',', $whitelist);
121178
}
122179

@@ -169,9 +226,9 @@ function biscolabLaravelReCaptcha(token) {
169226
}
170227
elseif ($this->version == 'v3') {
171228

172-
$action = array_get($configuration, 'action', 'homepage');
229+
$action = Arr::get($configuration, 'action', 'homepage');
173230

174-
$js_custom_validation = array_get($configuration, 'custom_validation', '');
231+
$js_custom_validation = Arr::get($configuration, 'custom_validation', '');
175232

176233
// Check if set custom_validation. That function will override default fetch validation function
177234
if ($js_custom_validation) {
@@ -180,14 +237,16 @@ function biscolabLaravelReCaptcha(token) {
180237
}
181238
else {
182239

183-
$js_then_callback = array_get($configuration, 'callback_then', '');
184-
$js_callback_catch = array_get($configuration, 'callback_catch', '');
240+
$js_then_callback = Arr::get($configuration, 'callback_then', '');
241+
$js_callback_catch = Arr::get($configuration, 'callback_catch', '');
185242

186243
$js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : '';
187244
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';
188245

189246
$validate_function = "
190-
fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, {
247+
fetch('/" . config('recaptcha.default_validation_route',
248+
'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name',
249+
'token') . "=' + token, {
191250
headers: {
192251
\"X-Requested-With\": \"XMLHttpRequest\",
193252
\"X-CSRF-TOKEN\": csrfToken.content
@@ -258,7 +317,7 @@ public function validate($response) {
258317
$curl = curl_init($url);
259318
curl_setopt($curl, CURLOPT_HEADER, false);
260319
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
261-
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
320+
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curl_timeout);
262321
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
263322
$curl_response = curl_exec($curl);
264323
}

src/ReCaptchaBuilderInvisible.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class ReCaptchaBuilderInvisible extends ReCaptchaBuilder {
2121
*
2222
* @param string $api_site_key
2323
* @param string $api_secret_key
24+
* @param int $curl_timeout
2425
*/
25-
public function __construct(string $api_site_key, string $api_secret_key) {
26+
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {
2627

27-
parent::__construct($api_site_key, $api_secret_key, 'invisible');
28+
parent::__construct($api_site_key, $api_secret_key, 'invisible', $curl_timeout);
2829
}
2930

3031
/**

src/ReCaptchaBuilderV2.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class ReCaptchaBuilderV2 extends ReCaptchaBuilder {
2121
*
2222
* @param string $api_site_key
2323
* @param string $api_secret_key
24+
* @param int $curl_timeout
2425
*/
25-
public function __construct(string $api_site_key, string $api_secret_key) {
26+
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {
2627

27-
parent::__construct($api_site_key, $api_secret_key, 'v2');
28+
parent::__construct($api_site_key, $api_secret_key, 'v2', $curl_timeout);
2829
}
2930

3031
/**

src/ReCaptchaBuilderV3.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class ReCaptchaBuilderV3 extends ReCaptchaBuilder {
2121
*
2222
* @param string $api_site_key
2323
* @param string $api_secret_key
24+
* @param int $curl_timeout
2425
*/
25-
public function __construct(string $api_site_key, string $api_secret_key) {
26+
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {
2627

27-
parent::__construct($api_site_key, $api_secret_key, 'v3');
28+
parent::__construct($api_site_key, $api_secret_key, 'v3', $curl_timeout);
2829
}
2930

3031
}

src/ReCaptchaServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function registerReCaptchaBuilder() {
114114
break;
115115
}
116116

117-
return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'));
117+
return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'), config('recaptcha.curl_timeout'));
118118

119119
});
120120
}

0 commit comments

Comments
 (0)