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

Commit 3d5d630

Browse files
committed
Merge branch 'feature/curl_timeout' into develop
2 parents 87b8419 + e55c2ae commit 3d5d630

10 files changed

+97
-25
lines changed

.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

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
}

tests/ReCaptchaConfigurationTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public function testSkipIpWhiteListIsArray() {
3737
$this->assertCount(2, $ip_whitelist);
3838
}
3939

40+
/**
41+
* @test
42+
*/
43+
public function testCurlTimeoutIsSet() {
44+
$this->assertEquals($this->recaptcha->getCurlTimeout(), 3);
45+
}
46+
4047
/**
4148
* Define environment setup.
4249
*
@@ -56,6 +63,6 @@ protected function setUp(): void {
5663

5764
parent::setUp(); // TODO: Change the autogenerated stub
5865

59-
$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
66+
$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key', 3);
6067
}
6168
}

tests/ReCaptchaTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ protected function setUp(): void {
8585

8686
parent::setUp(); // TODO: Change the autogenerated stub
8787

88-
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key');
89-
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
88+
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key', 3);
89+
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key', 3);
9090

9191
}
9292
}

tests/ReCaptchaV3Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function setUp(): void {
106106

107107
parent::setUp(); // TODO: Change the autogenerated stub
108108

109-
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
109+
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key', 3);
110110

111111
}
112112
}

0 commit comments

Comments
 (0)