@@ -16,25 +16,25 @@ |
||
16 | 16 | */ |
17 | 17 | class ReCaptchaBuilderV2 extends ReCaptchaBuilder { |
18 | 18 | |
19 | - /** |
|
20 | - * ReCaptchaBuilderV2 constructor. |
|
21 | - * |
|
22 | - * @param string $api_site_key |
|
23 | - * @param string $api_secret_key |
|
24 | - */ |
|
25 | - public function __construct(string $api_site_key, string $api_secret_key) { |
|
19 | + /** |
|
20 | + * ReCaptchaBuilderV2 constructor. |
|
21 | + * |
|
22 | + * @param string $api_site_key |
|
23 | + * @param string $api_secret_key |
|
24 | + */ |
|
25 | + public function __construct(string $api_site_key, string $api_secret_key) { |
|
26 | 26 | |
27 | - parent::__construct($api_site_key, $api_secret_key, 'v2',config('recaptcha.language')); |
|
28 | - } |
|
27 | + parent::__construct($api_site_key, $api_secret_key, 'v2',config('recaptcha.language')); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Write ReCAPTCHA HTML tag in your FORM |
|
32 | - * Insert before </form> tag |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function htmlFormSnippet(): string { |
|
30 | + /** |
|
31 | + * Write ReCAPTCHA HTML tag in your FORM |
|
32 | + * Insert before </form> tag |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function htmlFormSnippet(): string { |
|
36 | 36 | |
37 | - return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="' . $this->api_site_key . '"></div>' : ''; |
|
38 | - } |
|
37 | + return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="' . $this->api_site_key . '"></div>' : ''; |
|
38 | + } |
|
39 | 39 | |
40 | 40 | } |
41 | 41 | \ No newline at end of file |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | */ |
25 | 25 | public function __construct(string $api_site_key, string $api_secret_key) { |
26 | 26 | |
27 | - parent::__construct($api_site_key, $api_secret_key, 'v2',config('recaptcha.language')); |
|
27 | + parent::__construct($api_site_key, $api_secret_key, 'v2', config('recaptcha.language')); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function htmlFormSnippet(): string { |
36 | 36 | |
37 | - return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="' . $this->api_site_key . '"></div>' : ''; |
|
37 | + return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="'.$this->api_site_key.'"></div>' : ''; |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | } |
41 | 41 | \ No newline at end of file |
@@ -14,207 +14,207 @@ discard block |
||
14 | 14 | |
15 | 15 | class ReCaptchaBuilder { |
16 | 16 | |
17 | - /** |
|
18 | - * The Site key |
|
19 | - * please visit https://developers.google.com/recaptcha/docs/start |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $api_site_key; |
|
23 | - |
|
24 | - /** |
|
25 | - * The Secret key |
|
26 | - * please visit https://developers.google.com/recaptcha/docs/start |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $api_secret_key; |
|
30 | - |
|
31 | - /** |
|
32 | - * The chosen ReCAPTCHA version |
|
33 | - * please visit https://developers.google.com/recaptcha/docs/start |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $version; |
|
37 | - |
|
38 | - /** |
|
39 | - * The chosen ReCAPTCHA language |
|
40 | - * please visit https://developers.google.com/recaptcha/docs/language |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $language; |
|
44 | - |
|
45 | - /** |
|
46 | - * Whether is true the ReCAPTCHA is inactive |
|
47 | - * @var boolean |
|
48 | - */ |
|
49 | - protected $skip_by_ip = false; |
|
50 | - |
|
51 | - /** |
|
52 | - * The API request URI |
|
53 | - */ |
|
54 | - protected $api_url = 'https://www.google.com/recaptcha/api/siteverify'; |
|
55 | - |
|
56 | - public function __construct($api_site_key, $api_secret_key, $version = 'v2', $language = 'en') { |
|
57 | - |
|
58 | - $this->setApiSiteKey($api_site_key); |
|
59 | - $this->setApiSecretKey($api_secret_key); |
|
60 | - $this->setVersion($version); |
|
61 | - $this->setLanguage($language); |
|
62 | - $this->setSkipByIp($this->skipByIp()); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param string $api_site_key |
|
67 | - * |
|
68 | - * @return ReCaptchaBuilder |
|
69 | - */ |
|
70 | - public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder { |
|
71 | - |
|
72 | - $this->api_site_key = $api_site_key; |
|
73 | - |
|
74 | - return $this; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @param string $api_secret_key |
|
79 | - * |
|
80 | - * @return ReCaptchaBuilder |
|
81 | - */ |
|
82 | - public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder { |
|
83 | - |
|
84 | - $this->api_secret_key = $api_secret_key; |
|
85 | - |
|
86 | - return $this; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param string $version |
|
91 | - * |
|
92 | - * @return ReCaptchaBuilder |
|
93 | - */ |
|
94 | - public function setVersion(string $version): ReCaptchaBuilder { |
|
95 | - |
|
96 | - $this->version = $version; |
|
97 | - |
|
98 | - return $this; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param string $language |
|
103 | - * |
|
104 | - * @return ReCaptchaBuilder |
|
105 | - */ |
|
106 | - public function setLanguage(string $language): ReCaptchaBuilder { |
|
107 | - |
|
108 | - $this->language = $language; |
|
109 | - |
|
110 | - return $this; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getVersion(): string { |
|
117 | - |
|
118 | - return $this->version; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @return string |
|
123 | - */ |
|
124 | - public function getLanguage(): string { |
|
125 | - |
|
126 | - return $this->language; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param bool $skip_by_ip |
|
131 | - * |
|
132 | - * @return ReCaptchaBuilder |
|
133 | - */ |
|
134 | - public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder { |
|
135 | - |
|
136 | - $this->skip_by_ip = $skip_by_ip; |
|
137 | - |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @return array|mixed |
|
143 | - */ |
|
144 | - public function getIpWhitelist() { |
|
145 | - $whitelist = config('recaptcha.skip_ip', []); |
|
146 | - |
|
147 | - if(!is_array($whitelist)) { |
|
148 | - $whitelist = explode(',', $whitelist); |
|
149 | - } |
|
150 | - |
|
151 | - return $whitelist; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Checks whether the user IP address is among IPs "to be skipped" |
|
156 | - * |
|
157 | - * @return boolean |
|
158 | - */ |
|
159 | - public function skipByIp(): bool { |
|
160 | - |
|
161 | - return (in_array(request()->ip(), $this->getIpWhitelist())); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Write script HTML tag in you HTML code |
|
166 | - * Insert before </head> tag |
|
167 | - * |
|
168 | - * @param string|null $formId |
|
169 | - * @param array|null $configuration |
|
170 | - * |
|
171 | - * @return string |
|
172 | - * @throws Exception |
|
173 | - */ |
|
174 | - public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string { |
|
175 | - |
|
176 | - if ($this->skip_by_ip) { |
|
177 | - return ''; |
|
178 | - } |
|
179 | - |
|
180 | - switch ($this->version) { |
|
181 | - case 'v3': |
|
182 | - $html = "<script src=\"https://www.google.com/recaptcha/api.js?hl={$this->language}&render={$this->api_site_key}\"></script>"; |
|
183 | - break; |
|
184 | - default: |
|
185 | - $html = "<script src=\"https://www.google.com/recaptcha/api.js?hl={$this->language}\" async defer></script>"; |
|
186 | - } |
|
187 | - |
|
188 | - if ($this->version == 'invisible') { |
|
189 | - if (!$formId) { |
|
190 | - throw new Exception("formId required", 1); |
|
191 | - } |
|
192 | - $html .= '<script> |
|
17 | + /** |
|
18 | + * The Site key |
|
19 | + * please visit https://developers.google.com/recaptcha/docs/start |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $api_site_key; |
|
23 | + |
|
24 | + /** |
|
25 | + * The Secret key |
|
26 | + * please visit https://developers.google.com/recaptcha/docs/start |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $api_secret_key; |
|
30 | + |
|
31 | + /** |
|
32 | + * The chosen ReCAPTCHA version |
|
33 | + * please visit https://developers.google.com/recaptcha/docs/start |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $version; |
|
37 | + |
|
38 | + /** |
|
39 | + * The chosen ReCAPTCHA language |
|
40 | + * please visit https://developers.google.com/recaptcha/docs/language |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $language; |
|
44 | + |
|
45 | + /** |
|
46 | + * Whether is true the ReCAPTCHA is inactive |
|
47 | + * @var boolean |
|
48 | + */ |
|
49 | + protected $skip_by_ip = false; |
|
50 | + |
|
51 | + /** |
|
52 | + * The API request URI |
|
53 | + */ |
|
54 | + protected $api_url = 'https://www.google.com/recaptcha/api/siteverify'; |
|
55 | + |
|
56 | + public function __construct($api_site_key, $api_secret_key, $version = 'v2', $language = 'en') { |
|
57 | + |
|
58 | + $this->setApiSiteKey($api_site_key); |
|
59 | + $this->setApiSecretKey($api_secret_key); |
|
60 | + $this->setVersion($version); |
|
61 | + $this->setLanguage($language); |
|
62 | + $this->setSkipByIp($this->skipByIp()); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param string $api_site_key |
|
67 | + * |
|
68 | + * @return ReCaptchaBuilder |
|
69 | + */ |
|
70 | + public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder { |
|
71 | + |
|
72 | + $this->api_site_key = $api_site_key; |
|
73 | + |
|
74 | + return $this; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @param string $api_secret_key |
|
79 | + * |
|
80 | + * @return ReCaptchaBuilder |
|
81 | + */ |
|
82 | + public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder { |
|
83 | + |
|
84 | + $this->api_secret_key = $api_secret_key; |
|
85 | + |
|
86 | + return $this; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param string $version |
|
91 | + * |
|
92 | + * @return ReCaptchaBuilder |
|
93 | + */ |
|
94 | + public function setVersion(string $version): ReCaptchaBuilder { |
|
95 | + |
|
96 | + $this->version = $version; |
|
97 | + |
|
98 | + return $this; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param string $language |
|
103 | + * |
|
104 | + * @return ReCaptchaBuilder |
|
105 | + */ |
|
106 | + public function setLanguage(string $language): ReCaptchaBuilder { |
|
107 | + |
|
108 | + $this->language = $language; |
|
109 | + |
|
110 | + return $this; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getVersion(): string { |
|
117 | + |
|
118 | + return $this->version; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @return string |
|
123 | + */ |
|
124 | + public function getLanguage(): string { |
|
125 | + |
|
126 | + return $this->language; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param bool $skip_by_ip |
|
131 | + * |
|
132 | + * @return ReCaptchaBuilder |
|
133 | + */ |
|
134 | + public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder { |
|
135 | + |
|
136 | + $this->skip_by_ip = $skip_by_ip; |
|
137 | + |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @return array|mixed |
|
143 | + */ |
|
144 | + public function getIpWhitelist() { |
|
145 | + $whitelist = config('recaptcha.skip_ip', []); |
|
146 | + |
|
147 | + if(!is_array($whitelist)) { |
|
148 | + $whitelist = explode(',', $whitelist); |
|
149 | + } |
|
150 | + |
|
151 | + return $whitelist; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Checks whether the user IP address is among IPs "to be skipped" |
|
156 | + * |
|
157 | + * @return boolean |
|
158 | + */ |
|
159 | + public function skipByIp(): bool { |
|
160 | + |
|
161 | + return (in_array(request()->ip(), $this->getIpWhitelist())); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Write script HTML tag in you HTML code |
|
166 | + * Insert before </head> tag |
|
167 | + * |
|
168 | + * @param string|null $formId |
|
169 | + * @param array|null $configuration |
|
170 | + * |
|
171 | + * @return string |
|
172 | + * @throws Exception |
|
173 | + */ |
|
174 | + public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string { |
|
175 | + |
|
176 | + if ($this->skip_by_ip) { |
|
177 | + return ''; |
|
178 | + } |
|
179 | + |
|
180 | + switch ($this->version) { |
|
181 | + case 'v3': |
|
182 | + $html = "<script src=\"https://www.google.com/recaptcha/api.js?hl={$this->language}&render={$this->api_site_key}\"></script>"; |
|
183 | + break; |
|
184 | + default: |
|
185 | + $html = "<script src=\"https://www.google.com/recaptcha/api.js?hl={$this->language}\" async defer></script>"; |
|
186 | + } |
|
187 | + |
|
188 | + if ($this->version == 'invisible') { |
|
189 | + if (!$formId) { |
|
190 | + throw new Exception("formId required", 1); |
|
191 | + } |
|
192 | + $html .= '<script> |
|
193 | 193 | function biscolabLaravelReCaptcha(token) { |
194 | 194 | document.getElementById("' . $formId . '").submit(); |
195 | 195 | } |
196 | 196 | </script>'; |
197 | - } |
|
198 | - elseif ($this->version == 'v3') { |
|
197 | + } |
|
198 | + elseif ($this->version == 'v3') { |
|
199 | 199 | |
200 | - $action = array_get($configuration, 'action', 'homepage'); |
|
200 | + $action = array_get($configuration, 'action', 'homepage'); |
|
201 | 201 | |
202 | - $js_custom_validation = array_get($configuration, 'custom_validation', ''); |
|
202 | + $js_custom_validation = array_get($configuration, 'custom_validation', ''); |
|
203 | 203 | |
204 | - // Check if set custom_validation. That function will override default fetch validation function |
|
205 | - if ($js_custom_validation) { |
|
204 | + // Check if set custom_validation. That function will override default fetch validation function |
|
205 | + if ($js_custom_validation) { |
|
206 | 206 | |
207 | - $validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : ''; |
|
208 | - } |
|
209 | - else { |
|
207 | + $validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : ''; |
|
208 | + } |
|
209 | + else { |
|
210 | 210 | |
211 | - $js_then_callback = array_get($configuration, 'callback_then', ''); |
|
212 | - $js_callback_catch = array_get($configuration, 'callback_catch', ''); |
|
211 | + $js_then_callback = array_get($configuration, 'callback_then', ''); |
|
212 | + $js_callback_catch = array_get($configuration, 'callback_catch', ''); |
|
213 | 213 | |
214 | - $js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : ''; |
|
215 | - $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
|
214 | + $js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : ''; |
|
215 | + $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
|
216 | 216 | |
217 | - $validate_function = " |
|
217 | + $validate_function = " |
|
218 | 218 | fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, { |
219 | 219 | headers: { |
220 | 220 | \"X-Requested-With\": \"XMLHttpRequest\", |
@@ -227,9 +227,9 @@ discard block |
||
227 | 227 | .catch(function(err) { |
228 | 228 | {$js_callback_catch} |
229 | 229 | });"; |
230 | - } |
|
230 | + } |
|
231 | 231 | |
232 | - $html .= "<script> |
|
232 | + $html .= "<script> |
|
233 | 233 | var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]'); |
234 | 234 | grecaptcha.ready(function() { |
235 | 235 | grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) { |
@@ -237,90 +237,90 @@ discard block |
||
237 | 237 | }); |
238 | 238 | }); |
239 | 239 | </script>"; |
240 | - } |
|
241 | - |
|
242 | - return $html; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * @param array|null $configuration |
|
247 | - * |
|
248 | - * @return string |
|
249 | - */ |
|
250 | - public function htmlScriptTagJsApiV3(?array $configuration = []): string { |
|
251 | - |
|
252 | - return $this->htmlScriptTagJsApi('', $configuration); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Call out to reCAPTCHA and process the response |
|
257 | - * |
|
258 | - * @param string $response |
|
259 | - * |
|
260 | - * @return boolean|array |
|
261 | - */ |
|
262 | - public function validate($response) { |
|
263 | - |
|
264 | - if ($this->skip_by_ip) { |
|
265 | - if ($this->returnArray()) { |
|
266 | - // Add 'skip_by_ip' field to response |
|
267 | - return [ |
|
268 | - 'skip_by_ip' => true, |
|
269 | - 'score' => 0.9, |
|
270 | - 'success' => true |
|
271 | - ]; |
|
272 | - } |
|
273 | - |
|
274 | - return true; |
|
275 | - } |
|
276 | - |
|
277 | - $params = http_build_query([ |
|
278 | - 'secret' => $this->api_secret_key, |
|
279 | - 'remoteip' => request()->getClientIp(), |
|
280 | - 'response' => $response, |
|
281 | - ]); |
|
282 | - |
|
283 | - $url = $this->api_url . '?' . $params; |
|
284 | - |
|
285 | - if (function_exists('curl_version')) { |
|
286 | - $curl = curl_init($url); |
|
287 | - curl_setopt($curl, CURLOPT_HEADER, false); |
|
288 | - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
289 | - curl_setopt($curl, CURLOPT_TIMEOUT, 1); |
|
290 | - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
|
291 | - $curl_response = curl_exec($curl); |
|
292 | - } |
|
293 | - else { |
|
294 | - $curl_response = file_get_contents($url); |
|
295 | - } |
|
296 | - |
|
297 | - if (is_null($curl_response) || empty($curl_response)) { |
|
298 | - if ($this->returnArray()) { |
|
299 | - // Add 'error' field to response |
|
300 | - return [ |
|
301 | - 'error' => 'cURL response empty', |
|
302 | - 'score' => 0.1, |
|
303 | - 'success' => false |
|
304 | - ]; |
|
305 | - } |
|
306 | - |
|
307 | - return false; |
|
308 | - } |
|
309 | - $response = json_decode(trim($curl_response), true); |
|
310 | - |
|
311 | - if ($this->returnArray()) { |
|
312 | - return $response; |
|
313 | - } |
|
314 | - |
|
315 | - return $response['success']; |
|
316 | - |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * @return bool |
|
321 | - */ |
|
322 | - protected function returnArray(): bool { |
|
323 | - |
|
324 | - return ($this->version == 'v3'); |
|
325 | - } |
|
240 | + } |
|
241 | + |
|
242 | + return $html; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * @param array|null $configuration |
|
247 | + * |
|
248 | + * @return string |
|
249 | + */ |
|
250 | + public function htmlScriptTagJsApiV3(?array $configuration = []): string { |
|
251 | + |
|
252 | + return $this->htmlScriptTagJsApi('', $configuration); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Call out to reCAPTCHA and process the response |
|
257 | + * |
|
258 | + * @param string $response |
|
259 | + * |
|
260 | + * @return boolean|array |
|
261 | + */ |
|
262 | + public function validate($response) { |
|
263 | + |
|
264 | + if ($this->skip_by_ip) { |
|
265 | + if ($this->returnArray()) { |
|
266 | + // Add 'skip_by_ip' field to response |
|
267 | + return [ |
|
268 | + 'skip_by_ip' => true, |
|
269 | + 'score' => 0.9, |
|
270 | + 'success' => true |
|
271 | + ]; |
|
272 | + } |
|
273 | + |
|
274 | + return true; |
|
275 | + } |
|
276 | + |
|
277 | + $params = http_build_query([ |
|
278 | + 'secret' => $this->api_secret_key, |
|
279 | + 'remoteip' => request()->getClientIp(), |
|
280 | + 'response' => $response, |
|
281 | + ]); |
|
282 | + |
|
283 | + $url = $this->api_url . '?' . $params; |
|
284 | + |
|
285 | + if (function_exists('curl_version')) { |
|
286 | + $curl = curl_init($url); |
|
287 | + curl_setopt($curl, CURLOPT_HEADER, false); |
|
288 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
289 | + curl_setopt($curl, CURLOPT_TIMEOUT, 1); |
|
290 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
|
291 | + $curl_response = curl_exec($curl); |
|
292 | + } |
|
293 | + else { |
|
294 | + $curl_response = file_get_contents($url); |
|
295 | + } |
|
296 | + |
|
297 | + if (is_null($curl_response) || empty($curl_response)) { |
|
298 | + if ($this->returnArray()) { |
|
299 | + // Add 'error' field to response |
|
300 | + return [ |
|
301 | + 'error' => 'cURL response empty', |
|
302 | + 'score' => 0.1, |
|
303 | + 'success' => false |
|
304 | + ]; |
|
305 | + } |
|
306 | + |
|
307 | + return false; |
|
308 | + } |
|
309 | + $response = json_decode(trim($curl_response), true); |
|
310 | + |
|
311 | + if ($this->returnArray()) { |
|
312 | + return $response; |
|
313 | + } |
|
314 | + |
|
315 | + return $response['success']; |
|
316 | + |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * @return bool |
|
321 | + */ |
|
322 | + protected function returnArray(): bool { |
|
323 | + |
|
324 | + return ($this->version == 'v3'); |
|
325 | + } |
|
326 | 326 | } |
327 | 327 | \ No newline at end of file |
@@ -16,15 +16,15 @@ |
||
16 | 16 | */ |
17 | 17 | class ReCaptchaBuilderV3 extends ReCaptchaBuilder { |
18 | 18 | |
19 | - /** |
|
20 | - * ReCaptchaBuilderV3 constructor. |
|
21 | - * |
|
22 | - * @param string $api_site_key |
|
23 | - * @param string $api_secret_key |
|
24 | - */ |
|
25 | - public function __construct(string $api_site_key, string $api_secret_key) { |
|
19 | + /** |
|
20 | + * ReCaptchaBuilderV3 constructor. |
|
21 | + * |
|
22 | + * @param string $api_site_key |
|
23 | + * @param string $api_secret_key |
|
24 | + */ |
|
25 | + public function __construct(string $api_site_key, string $api_secret_key) { |
|
26 | 26 | |
27 | - parent::__construct($api_site_key, $api_secret_key, 'v3',config('recaptcha.language')); |
|
28 | - } |
|
27 | + parent::__construct($api_site_key, $api_secret_key, 'v3',config('recaptcha.language')); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -24,7 +24,7 @@ |
||
24 | 24 | */ |
25 | 25 | public function __construct(string $api_site_key, string $api_secret_key) { |
26 | 26 | |
27 | - parent::__construct($api_site_key, $api_secret_key, 'v3',config('recaptcha.language')); |
|
27 | + parent::__construct($api_site_key, $api_secret_key, 'v3', config('recaptcha.language')); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -16,28 +16,28 @@ |
||
16 | 16 | */ |
17 | 17 | class ReCaptchaBuilderInvisible extends ReCaptchaBuilder { |
18 | 18 | |
19 | - /** |
|
20 | - * ReCaptchaBuilderInvisible constructor. |
|
21 | - * |
|
22 | - * @param string $api_site_key |
|
23 | - * @param string $api_secret_key |
|
24 | - */ |
|
25 | - public function __construct(string $api_site_key, string $api_secret_key) { |
|
19 | + /** |
|
20 | + * ReCaptchaBuilderInvisible constructor. |
|
21 | + * |
|
22 | + * @param string $api_site_key |
|
23 | + * @param string $api_secret_key |
|
24 | + */ |
|
25 | + public function __construct(string $api_site_key, string $api_secret_key) { |
|
26 | 26 | |
27 | - parent::__construct($api_site_key, $api_secret_key, 'invisible',config('recaptcha.language')); |
|
28 | - } |
|
27 | + parent::__construct($api_site_key, $api_secret_key, 'invisible',config('recaptcha.language')); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Write HTML <button> tag in your HTML code |
|
32 | - * Insert before </form> tag |
|
33 | - * |
|
34 | - * @param string $buttonInnerHTML |
|
35 | - * |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function htmlFormButton($buttonInnerHTML = 'Submit'): string { |
|
30 | + /** |
|
31 | + * Write HTML <button> tag in your HTML code |
|
32 | + * Insert before </form> tag |
|
33 | + * |
|
34 | + * @param string $buttonInnerHTML |
|
35 | + * |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function htmlFormButton($buttonInnerHTML = 'Submit'): string { |
|
39 | 39 | |
40 | - return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="' . $this->api_site_key . '" data-callback="biscolabLaravelReCaptcha">' . $buttonInnerHTML . '</button>' : ''; |
|
41 | - } |
|
40 | + return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="' . $this->api_site_key . '" data-callback="biscolabLaravelReCaptcha">' . $buttonInnerHTML . '</button>' : ''; |
|
41 | + } |
|
42 | 42 | |
43 | 43 | } |
44 | 44 | \ No newline at end of file |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | */ |
25 | 25 | public function __construct(string $api_site_key, string $api_secret_key) { |
26 | 26 | |
27 | - parent::__construct($api_site_key, $api_secret_key, 'invisible',config('recaptcha.language')); |
|
27 | + parent::__construct($api_site_key, $api_secret_key, 'invisible', config('recaptcha.language')); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | */ |
38 | 38 | public function htmlFormButton($buttonInnerHTML = 'Submit'): string { |
39 | 39 | |
40 | - return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="' . $this->api_site_key . '" data-callback="biscolabLaravelReCaptcha">' . $buttonInnerHTML . '</button>' : ''; |
|
40 | + return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="'.$this->api_site_key.'" data-callback="biscolabLaravelReCaptcha">'.$buttonInnerHTML.'</button>' : ''; |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | } |
44 | 44 | \ No newline at end of file |
@@ -13,61 +13,61 @@ |
||
13 | 13 | */ |
14 | 14 | return [ |
15 | 15 | |
16 | - /** |
|
17 | - * |
|
18 | - * The site key |
|
19 | - * get site key @ www.google.com/recaptcha/admin |
|
20 | - * |
|
21 | - */ |
|
22 | - 'api_site_key' => env('RECAPTCHA_SITE_KEY', ''), |
|
16 | + /** |
|
17 | + * |
|
18 | + * The site key |
|
19 | + * get site key @ www.google.com/recaptcha/admin |
|
20 | + * |
|
21 | + */ |
|
22 | + 'api_site_key' => env('RECAPTCHA_SITE_KEY', ''), |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * The secret key |
|
27 | - * get secret key @ www.google.com/recaptcha/admin |
|
28 | - * |
|
29 | - */ |
|
30 | - 'api_secret_key' => env('RECAPTCHA_SECRET_KEY', ''), |
|
24 | + /** |
|
25 | + * |
|
26 | + * The secret key |
|
27 | + * get secret key @ www.google.com/recaptcha/admin |
|
28 | + * |
|
29 | + */ |
|
30 | + 'api_secret_key' => env('RECAPTCHA_SECRET_KEY', ''), |
|
31 | 31 | |
32 | - /** |
|
33 | - * |
|
34 | - * ReCATCHA version |
|
35 | - * Supported: "v2", "invisible", "v3", |
|
36 | - * |
|
37 | - * get more info @ https://developers.google.com/recaptcha/docs/versions |
|
38 | - * |
|
39 | - */ |
|
40 | - 'version' => env('RECAPTCHA_DEFAULT_VERSION', 'v2'), |
|
32 | + /** |
|
33 | + * |
|
34 | + * ReCATCHA version |
|
35 | + * Supported: "v2", "invisible", "v3", |
|
36 | + * |
|
37 | + * get more info @ https://developers.google.com/recaptcha/docs/versions |
|
38 | + * |
|
39 | + */ |
|
40 | + 'version' => env('RECAPTCHA_DEFAULT_VERSION', 'v2'), |
|
41 | 41 | |
42 | - /** |
|
43 | - * |
|
44 | - * ReCATCHA language |
|
45 | - * |
|
46 | - * get more info @ https://developers.google.com/recaptcha/docs/language |
|
47 | - * |
|
48 | - */ |
|
49 | - 'language' => env('RECAPTCHA_DEFAULT_LANGUAGE', 'en'), |
|
42 | + /** |
|
43 | + * |
|
44 | + * ReCATCHA language |
|
45 | + * |
|
46 | + * get more info @ https://developers.google.com/recaptcha/docs/language |
|
47 | + * |
|
48 | + */ |
|
49 | + 'language' => env('RECAPTCHA_DEFAULT_LANGUAGE', 'en'), |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * IP addresses for which validation will be skipped |
|
54 | - * |
|
55 | - */ |
|
56 | - 'skip_ip' => [], |
|
51 | + /** |
|
52 | + * |
|
53 | + * IP addresses for which validation will be skipped |
|
54 | + * |
|
55 | + */ |
|
56 | + 'skip_ip' => [], |
|
57 | 57 | |
58 | - /** |
|
59 | - * |
|
60 | - * Default route called to check the Google reCAPTCHA token |
|
61 | - * @since v3.2.0 |
|
62 | - * |
|
63 | - */ |
|
64 | - 'default_validation_route' => env('RECAPTCHA_DEFAULT_VALIDATION_ROUTE', 'biscolab-recaptcha/validate'), |
|
58 | + /** |
|
59 | + * |
|
60 | + * Default route called to check the Google reCAPTCHA token |
|
61 | + * @since v3.2.0 |
|
62 | + * |
|
63 | + */ |
|
64 | + 'default_validation_route' => env('RECAPTCHA_DEFAULT_VALIDATION_ROUTE', 'biscolab-recaptcha/validate'), |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * The name of the parameter used to send Google reCAPTCHA token to verify route |
|
69 | - * @since v3.2.0 |
|
70 | - * |
|
71 | - */ |
|
72 | - 'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token') |
|
66 | + /** |
|
67 | + * |
|
68 | + * The name of the parameter used to send Google reCAPTCHA token to verify route |
|
69 | + * @since v3.2.0 |
|
70 | + * |
|
71 | + */ |
|
72 | + 'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token') |
|
73 | 73 | ]; |
74 | 74 | \ No newline at end of file |