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

Commit c06c5e0

Browse files
committed
Merge branch 'feature/language' into develop
2 parents 3b033d0 + 975e572 commit c06c5e0

17 files changed

+503
-117
lines changed

.scrutinizer.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ build:
77
coverage:
88
file: clover.xml
99
format: clover
10+
tools:
11+
php_code_sniffer:
12+
config:
13+
standard: PSR2

config/recaptcha.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,15 @@
6868
* @since v3.2.0
6969
*
7070
*/
71-
'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token')
71+
'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token'),
72+
73+
/**
74+
*
75+
* The default Google reCAPTCHA language code
76+
* It has no effect with v3
77+
* @see https://developers.google.com/recaptcha/docs/language
78+
* @since v3.6.0
79+
*
80+
*/
81+
'default_language' => env('RECAPTCHA_DEFAULT_LANGUAGE', null)
7282
];

src/Controllers/ReCaptchaController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
* Class ReCaptchaController
1717
* @package Biscolab\ReCaptcha\Controllers
1818
*/
19-
class ReCaptchaController extends Controller {
19+
class ReCaptchaController extends Controller
20+
{
2021

2122
/**
2223
* @return array
2324
*/
24-
public function validateV3(): array {
25+
public function validateV3(): array
26+
{
2527

2628
$token = request()->input(config('recaptcha.default_token_parameter_name', 'token'), '');
29+
2730
return recaptcha()->validate($token);
2831
}
2932
}

src/Facades/ReCaptcha.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
* Class ReCaptcha
1717
* @package Biscolab\ReCaptcha\Facades
1818
*/
19-
class ReCaptcha extends Facade {
19+
class ReCaptcha extends Facade
20+
{
2021

2122
/**
2223
* Get the registered name of the component.
2324
*
2425
* @return string
2526
*/
26-
protected static function getFacadeAccessor() {
27+
protected static function getFacadeAccessor()
28+
{
2729

2830
return 'recaptcha';
2931
}

src/ReCaptchaBuilder.php

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* Class ReCaptchaBuilder
1818
* @package Biscolab\ReCaptcha
1919
*/
20-
class ReCaptchaBuilder {
20+
class ReCaptchaBuilder
21+
{
2122

2223
/**
2324
* @var string
@@ -50,13 +51,6 @@ class ReCaptchaBuilder {
5051
*/
5152
protected $version;
5253

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-
6054
/**
6155
* Whether is true the ReCAPTCHA is inactive
6256
* @var boolean
@@ -74,19 +68,16 @@ class ReCaptchaBuilder {
7468
* @param string $api_site_key
7569
* @param string $api_secret_key
7670
* @param null|string $version
77-
* @param int|null $curl_timeout
7871
*/
7972
public function __construct(
8073
string $api_site_key,
8174
string $api_secret_key,
82-
?string $version = self::DEFAULT_API_VERSION,
83-
?int $curl_timeout = self::DEFAULT_CURL_TIMEOUT
75+
?string $version = self::DEFAULT_API_VERSION
8476
) {
8577

8678
$this->setApiSiteKey($api_site_key);
8779
$this->setApiSecretKey($api_secret_key);
8880
$this->setVersion($version);
89-
$this->setCurlTimeout($curl_timeout);
9081
$this->setSkipByIp($this->skipByIp());
9182
}
9283

@@ -95,7 +86,8 @@ public function __construct(
9586
*
9687
* @return ReCaptchaBuilder
9788
*/
98-
public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder {
89+
public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder
90+
{
9991

10092
$this->api_site_key = $api_site_key;
10193

@@ -107,42 +99,30 @@ public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder {
10799
*
108100
* @return ReCaptchaBuilder
109101
*/
110-
public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder {
102+
public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder
103+
{
111104

112105
$this->api_secret_key = $api_secret_key;
113106

114107
return $this;
115108
}
116109

117-
/**
118-
* @param int|null $curl_timeout
119-
*
120-
* @return ReCaptchaBuilder
121-
*/
122-
public function setCurlTimeout(?int $curl_timeout = null): ReCaptchaBuilder {
123-
124-
if($curl_timeout === null) {
125-
$curl_timeout = config('recaptcha.curl_timeout', ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT);
126-
}
127-
$this->curl_timeout = $curl_timeout;
128-
129-
return $this;
130-
}
131-
132110
/**
133111
* @return int
134112
*/
135-
public function getCurlTimeout(): int {
113+
public function getCurlTimeout(): int
114+
{
136115

137-
return $this->curl_timeout;
116+
return config('recaptcha.curl_timeout', self::DEFAULT_CURL_TIMEOUT);
138117
}
139118

140119
/**
141120
* @param string $version
142121
*
143122
* @return ReCaptchaBuilder
144123
*/
145-
public function setVersion(string $version): ReCaptchaBuilder {
124+
public function setVersion(string $version): ReCaptchaBuilder
125+
{
146126

147127
$this->version = $version;
148128

@@ -152,7 +132,8 @@ public function setVersion(string $version): ReCaptchaBuilder {
152132
/**
153133
* @return string
154134
*/
155-
public function getVersion(): string {
135+
public function getVersion(): string
136+
{
156137

157138
return $this->version;
158139
}
@@ -162,7 +143,8 @@ public function getVersion(): string {
162143
*
163144
* @return ReCaptchaBuilder
164145
*/
165-
public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder {
146+
public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder
147+
{
166148

167149
$this->skip_by_ip = $skip_by_ip;
168150

@@ -172,7 +154,8 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder {
172154
/**
173155
* @return array|mixed
174156
*/
175-
public function getIpWhitelist() {
157+
public function getIpWhitelist()
158+
{
176159

177160
$whitelist = config('recaptcha.skip_ip', []);
178161

@@ -188,7 +171,8 @@ public function getIpWhitelist() {
188171
*
189172
* @return boolean
190173
*/
191-
public function skipByIp(): bool {
174+
public function skipByIp(): bool
175+
{
192176

193177
return (in_array(request()->ip(), $this->getIpWhitelist()));
194178
}
@@ -203,7 +187,8 @@ public function skipByIp(): bool {
203187
* @return string
204188
* @throws Exception
205189
*/
206-
public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string {
190+
public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string
191+
{
207192

208193
if ($this->skip_by_ip) {
209194
return '';
@@ -214,7 +199,9 @@ public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration =
214199
$html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>";
215200
break;
216201
default:
217-
$html = "<script src=\"https://www.google.com/recaptcha/api.js\" async defer></script>";
202+
$language = config('recaptcha.default_language', null);
203+
$query = ($language) ? "?hl=" . $language : "";
204+
$html = "<script src=\"https://www.google.com/recaptcha/api.js" . $query . "\" async defer></script>";
218205
}
219206

220207
if ($this->version == 'invisible') {
@@ -226,8 +213,7 @@ function biscolabLaravelReCaptcha(token) {
226213
document.getElementById("' . $formId . '").submit();
227214
}
228215
</script>';
229-
}
230-
elseif ($this->version == 'v3') {
216+
} elseif ($this->version == 'v3') {
231217

232218
$action = Arr::get($configuration, 'action', 'homepage');
233219

@@ -237,8 +223,7 @@ function biscolabLaravelReCaptcha(token) {
237223
if ($js_custom_validation) {
238224

239225
$validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : '';
240-
}
241-
else {
226+
} else {
242227

243228
$js_then_callback = Arr::get($configuration, 'callback_then', '');
244229
$js_callback_catch = Arr::get($configuration, 'callback_catch', '');
@@ -281,7 +266,8 @@ function biscolabLaravelReCaptcha(token) {
281266
*
282267
* @return string
283268
*/
284-
public function htmlScriptTagJsApiV3(?array $configuration = []): string {
269+
public function htmlScriptTagJsApiV3(?array $configuration = []): string
270+
{
285271

286272
return $this->htmlScriptTagJsApi('', $configuration);
287273
}
@@ -293,7 +279,8 @@ public function htmlScriptTagJsApiV3(?array $configuration = []): string {
293279
*
294280
* @return boolean|array
295281
*/
296-
public function validate($response) {
282+
public function validate($response)
283+
{
297284

298285
if ($this->skip_by_ip) {
299286
if ($this->returnArray()) {
@@ -317,14 +304,14 @@ public function validate($response) {
317304
$url = $this->api_url . '?' . $params;
318305

319306
if (function_exists('curl_version')) {
307+
320308
$curl = curl_init($url);
321309
curl_setopt($curl, CURLOPT_HEADER, false);
322310
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
323-
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curl_timeout);
311+
curl_setopt($curl, CURLOPT_TIMEOUT, $this->getCurlTimeout());
324312
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
325313
$curl_response = curl_exec($curl);
326-
}
327-
else {
314+
} else {
328315
$curl_response = file_get_contents($url);
329316
}
330317

@@ -350,10 +337,29 @@ public function validate($response) {
350337

351338
}
352339

340+
/**
341+
* @return string
342+
*/
343+
public function getApiSiteKey(): string
344+
{
345+
346+
return $this->api_site_key;
347+
}
348+
349+
/**
350+
* @return string
351+
*/
352+
public function getApiSecretKey(): string
353+
{
354+
355+
return $this->api_secret_key;
356+
}
357+
353358
/**
354359
* @return bool
355360
*/
356-
protected function returnArray(): bool {
361+
protected function returnArray(): bool
362+
{
357363

358364
return ($this->version == 'v3');
359365
}

src/ReCaptchaBuilderInvisible.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
* Class ReCaptchaBuilderInvisible
1515
* @package Biscolab\ReCaptcha
1616
*/
17-
class ReCaptchaBuilderInvisible extends ReCaptchaBuilder {
17+
class ReCaptchaBuilderInvisible extends ReCaptchaBuilder
18+
{
1819

1920
/**
2021
* ReCaptchaBuilderInvisible constructor.
2122
*
22-
* @param string $api_site_key
23-
* @param string $api_secret_key
24-
* @param int|null $curl_timeout
23+
* @param string $api_site_key
24+
* @param string $api_secret_key
2525
*/
26-
public function __construct(string $api_site_key, string $api_secret_key, ?int $curl_timeout = null) {
26+
public function __construct(string $api_site_key, string $api_secret_key)
27+
{
2728

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

3132
/**
@@ -36,7 +37,8 @@ public function __construct(string $api_site_key, string $api_secret_key, ?int $
3637
*
3738
* @return string
3839
*/
39-
public function htmlFormButton($buttonInnerHTML = 'Submit'): string {
40+
public function htmlFormButton($buttonInnerHTML = 'Submit'): string
41+
{
4042

4143
return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="' . $this->api_site_key . '" data-callback="biscolabLaravelReCaptcha">' . $buttonInnerHTML . '</button>' : '';
4244
}

src/ReCaptchaBuilderV2.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,28 @@
1414
* Class ReCaptchaBuilderV2
1515
* @package Biscolab\ReCaptcha
1616
*/
17-
class ReCaptchaBuilderV2 extends ReCaptchaBuilder {
17+
class ReCaptchaBuilderV2 extends ReCaptchaBuilder
18+
{
1819

1920
/**
2021
* ReCaptchaBuilderV2 constructor.
2122
*
22-
* @param string $api_site_key
23-
* @param string $api_secret_key
24-
* @param int|null $curl_timeout
23+
* @param string $api_site_key
24+
* @param string $api_secret_key
2525
*/
26-
public function __construct(string $api_site_key, string $api_secret_key, ?int $curl_timeout = null) {
26+
public function __construct(string $api_site_key, string $api_secret_key)
27+
{
2728

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

3132
/**
3233
* Write ReCAPTCHA HTML tag in your FORM
3334
* Insert before </form> tag
3435
* @return string
3536
*/
36-
public function htmlFormSnippet(): string {
37+
public function htmlFormSnippet(): string
38+
{
3739

3840
return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="' . $this->api_site_key . '"></div>' : '';
3941
}

0 commit comments

Comments
 (0)