1
1
<?php
2
+
2
3
/**
3
4
* Copyright (c) 2017 - present
4
5
* LaravelGoogleRecaptcha - ReCaptchaBuilder.php
@@ -44,6 +45,11 @@ class ReCaptchaBuilder
44
45
*/
45
46
const DEFAULT_RECAPTCHA_FIELD_NAME = 'g-recaptcha-response ' ;
46
47
48
+ /**
49
+ * @var string
50
+ */
51
+ const DEFAULT_RECAPTCHA_API_DOMAIN = 'www.google.com ' ;
52
+
47
53
/**
48
54
* The Site key
49
55
* please visit https://developers.google.com/recaptcha/docs/start
@@ -71,10 +77,23 @@ class ReCaptchaBuilder
71
77
*/
72
78
protected $ skip_by_ip = false ;
73
79
80
+ /**
81
+ * The API domain (default: retrieved from config file)
82
+ * @var string
83
+ */
84
+ protected $ api_domain = '' ;
85
+
74
86
/**
75
87
* The API request URI
88
+ * @var string
76
89
*/
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 = '' ;
78
97
79
98
/**
80
99
* ReCaptchaBuilder constructor.
@@ -93,6 +112,8 @@ public function __construct(
93
112
$ this ->setApiSecretKey ($ api_secret_key );
94
113
$ this ->setVersion ($ version );
95
114
$ this ->setSkipByIp ($ this ->skipByIp ());
115
+ $ this ->setApiDomain ();
116
+ $ this ->setApiUrls ();
96
117
}
97
118
98
119
/**
@@ -165,6 +186,40 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder
165
186
return $ this ;
166
187
}
167
188
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
+
168
223
/**
169
224
* @return array|mixed
170
225
*/
@@ -233,7 +288,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
233
288
234
289
// Create query string
235
290
$ 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> " ;
237
292
238
293
return $ html ;
239
294
}
@@ -300,7 +355,6 @@ public function validate($response)
300
355
}
301
356
302
357
return $ response ['success ' ];
303
-
304
358
}
305
359
306
360
/**
@@ -329,4 +383,4 @@ protected function returnArray(): bool
329
383
330
384
return ($ this ->version == 'v3 ' );
331
385
}
332
- }
386
+ }
0 commit comments