1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2017 - present |
5
|
|
|
* LaravelGoogleRecaptcha - ReCaptchaBuilderV3.php |
6
|
|
|
* author: Roberto Belotti - [email protected] |
7
|
|
|
* web : robertobelotti.com, github.com/biscolab |
8
|
|
|
* Initial version created on: 22/1/2019 |
9
|
|
|
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Biscolab\ReCaptcha; |
13
|
|
|
|
14
|
|
|
use Illuminate\Support\Arr; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ReCaptchaBuilderV3 |
18
|
|
|
* @package Biscolab\ReCaptcha |
19
|
|
|
*/ |
20
|
|
|
class ReCaptchaBuilderV3 extends ReCaptchaBuilder |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ReCaptchaBuilderV3 constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $api_site_key |
27
|
|
|
* @param string $api_secret_key |
28
|
|
|
*/ |
29
|
22 |
|
public function __construct(string $api_site_key, string $api_secret_key) |
30
|
|
|
{ |
31
|
|
|
|
32
|
22 |
|
parent::__construct($api_site_key, $api_secret_key, 'v3'); |
33
|
22 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Write script HTML tag in you HTML code |
37
|
|
|
* Insert before </head> tag |
38
|
|
|
* |
39
|
|
|
* @param array|null $configuration |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
5 |
|
public function htmlScriptTagJsApi(?array $configuration = []): string |
44
|
|
|
{ |
45
|
|
|
|
46
|
5 |
|
if ($this->skip_by_ip) { |
47
|
|
|
return ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
$html = "<script src=\"" . $this->api_js_url . "?render={$this->api_site_key}\"></script>"; |
51
|
|
|
|
52
|
5 |
|
$action = Arr::get($configuration, 'action', 'homepage'); |
53
|
|
|
|
54
|
5 |
|
$js_custom_validation = Arr::get($configuration, 'custom_validation', ''); |
55
|
|
|
|
56
|
|
|
// Check if set custom_validation. That function will override default fetch validation function |
57
|
5 |
|
if ($js_custom_validation) { |
58
|
|
|
|
59
|
1 |
|
$validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : ''; |
60
|
|
|
} else { |
61
|
|
|
|
62
|
4 |
|
$js_then_callback = Arr::get($configuration, 'callback_then', ''); |
63
|
4 |
|
$js_callback_catch = Arr::get($configuration, 'callback_catch', ''); |
64
|
|
|
|
65
|
4 |
|
$js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : ''; |
66
|
4 |
|
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
67
|
|
|
|
68
|
|
|
$validate_function = " |
69
|
4 |
|
fetch('/" . config( |
70
|
4 |
|
'recaptcha.default_validation_route', |
71
|
4 |
|
'biscolab-recaptcha/validate' |
72
|
4 |
|
) . "?" . config( |
73
|
4 |
|
'recaptcha.default_token_parameter_name', |
74
|
4 |
|
'token' |
75
|
|
|
) . "=' + token, { |
76
|
|
|
headers: { |
77
|
|
|
\"X-Requested-With\": \"XMLHttpRequest\", |
78
|
|
|
\"X-CSRF-TOKEN\": csrfToken.content |
79
|
|
|
} |
80
|
|
|
}) |
81
|
|
|
.then(function(response) { |
82
|
4 |
|
{$js_then_callback} |
83
|
|
|
}) |
84
|
|
|
.catch(function(err) { |
85
|
4 |
|
{$js_callback_catch} |
86
|
|
|
});"; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$html .= "<script> |
90
|
|
|
var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]'); |
91
|
|
|
grecaptcha.ready(function() { |
92
|
5 |
|
grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) { |
93
|
5 |
|
{$validate_function} |
94
|
|
|
}); |
95
|
|
|
}); |
96
|
|
|
</script>"; |
97
|
|
|
|
98
|
5 |
|
return $html; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|