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

Commit bd4f9bd

Browse files
committed
'api_url' and 'api_js_url' attributes supported in ReCaptchaBuilder ad ReCaptchaBuilderV3 for any ReCaptcha version
1 parent f613b36 commit bd4f9bd

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

src/ReCaptchaBuilder.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaBuilder.php
@@ -44,6 +45,11 @@ class ReCaptchaBuilder
4445
*/
4546
const DEFAULT_RECAPTCHA_FIELD_NAME = 'g-recaptcha-response';
4647

48+
/**
49+
* @var string
50+
*/
51+
const DEFAULT_RECAPTCHA_API_DOMAIN = 'www.google.com';
52+
4753
/**
4854
* The Site key
4955
* please visit https://developers.google.com/recaptcha/docs/start
@@ -71,10 +77,23 @@ class ReCaptchaBuilder
7177
*/
7278
protected $skip_by_ip = false;
7379

80+
/**
81+
* The API domain (default: retrieved from config file)
82+
* @var string
83+
*/
84+
protected $api_domain = '';
85+
7486
/**
7587
* The API request URI
88+
* @var string
7689
*/
77-
protected $api_url = 'https://www.google.com/recaptcha/api/siteverify';
90+
protected $api_url = '';
91+
92+
/**
93+
* The URI of the API Javascript file to embed in you pages
94+
* @var string
95+
*/
96+
protected $api_js_url = '';
7897

7998
/**
8099
* ReCaptchaBuilder constructor.
@@ -93,6 +112,8 @@ public function __construct(
93112
$this->setApiSecretKey($api_secret_key);
94113
$this->setVersion($version);
95114
$this->setSkipByIp($this->skipByIp());
115+
$this->setApiDomain();
116+
$this->setApiUrls();
96117
}
97118

98119
/**
@@ -165,6 +186,40 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder
165186
return $this;
166187
}
167188

189+
/**
190+
* @param null|string $api_domain
191+
*
192+
* @return ReCaptchaBuilder
193+
*/
194+
public function setApiDomain(?string $api_domain = null): ReCaptchaBuilder
195+
{
196+
197+
$this->api_domain = $api_domain ?? config('recaptcha.api_domain', self::DEFAULT_RECAPTCHA_API_DOMAIN);
198+
199+
return $this;
200+
}
201+
202+
/**
203+
* @return string
204+
*/
205+
public function getApiDomain(): string
206+
{
207+
208+
return $this->api_domain;
209+
}
210+
211+
/**
212+
* @return ReCaptchaBuilder
213+
*/
214+
public function setApiUrls(): ReCaptchaBuilder
215+
{
216+
217+
$this->api_url = 'https://' . $this->api_domain . '/recaptcha/api/siteverify';
218+
$this->api_js_url = 'https://' . $this->api_domain . '/recaptcha/api.js';
219+
220+
return $this;
221+
}
222+
168223
/**
169224
* @return array|mixed
170225
*/
@@ -233,7 +288,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
233288

234289
// Create query string
235290
$query = ($query) ? '?' . http_build_query($query) : "";
236-
$html .= "<script src=\"https://www.google.com/recaptcha/api.js" . $query . "\" async defer></script>";
291+
$html .= "<script src=\"" . $this->api_js_url . $query . "\" async defer></script>";
237292

238293
return $html;
239294
}
@@ -300,7 +355,6 @@ public function validate($response)
300355
}
301356

302357
return $response['success'];
303-
304358
}
305359

306360
/**
@@ -329,4 +383,4 @@ protected function returnArray(): bool
329383

330384
return ($this->version == 'v3');
331385
}
332-
}
386+
}

src/ReCaptchaBuilderV3.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaBuilderV3.php
@@ -46,7 +47,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
4647
return '';
4748
}
4849

49-
$html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>";
50+
$html = "<script src=\"" . $this->api_js_url . "?render={$this->api_site_key}\"></script>";
5051

5152
$action = Arr::get($configuration, 'action', 'homepage');
5253

@@ -65,9 +66,13 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
6566
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';
6667

6768
$validate_function = "
68-
fetch('/" . config('recaptcha.default_validation_route',
69-
'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name',
70-
'token') . "=' + token, {
69+
fetch('/" . config(
70+
'recaptcha.default_validation_route',
71+
'biscolab-recaptcha/validate'
72+
) . "?" . config(
73+
'recaptcha.default_token_parameter_name',
74+
'token'
75+
) . "=' + token, {
7176
headers: {
7277
\"X-Requested-With\": \"XMLHttpRequest\",
7378
\"X-CSRF-TOKEN\": csrfToken.content
@@ -92,5 +97,4 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
9297

9398
return $html;
9499
}
95-
96-
}
100+
}

0 commit comments

Comments
 (0)