1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2017 - present |
5
|
|
|
* LaravelGoogleRecaptcha - ReCaptchaBuilderV2.php |
6
|
|
|
* author: Roberto Belotti - [email protected] |
7
|
|
|
* web : robertobelotti.com, github.com/biscolab |
8
|
|
|
* Initial version created on: 12/9/2018 |
9
|
|
|
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Biscolab\ReCaptcha; |
13
|
|
|
|
14
|
|
|
use Biscolab\ReCaptcha\Exceptions\InvalidConfigurationException; |
15
|
|
|
use Illuminate\Support\Arr; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ReCaptchaBuilderV2 |
19
|
|
|
* @package Biscolab\ReCaptcha |
20
|
|
|
*/ |
21
|
|
|
class ReCaptchaBuilderV2 extends ReCaptchaBuilder |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
protected static $allowed_data_attribute = [ |
25
|
|
|
"theme", |
26
|
|
|
"size", |
27
|
|
|
"tabindex", |
28
|
|
|
"callback", |
29
|
|
|
"expired-callback", |
30
|
|
|
"error-callback", |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ReCaptchaBuilderV2 constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $api_site_key |
37
|
|
|
* @param string $api_secret_key |
38
|
|
|
*/ |
39
|
34 |
|
public function __construct(string $api_site_key, string $api_secret_key) |
40
|
|
|
{ |
41
|
|
|
|
42
|
34 |
|
parent::__construct($api_site_key, $api_secret_key, 'v2'); |
43
|
34 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Write ReCAPTCHA HTML tag in your FORM |
47
|
|
|
* Insert before </form> tag |
48
|
|
|
* |
49
|
|
|
* @param null|array $attributes |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
5 |
|
public function htmlFormSnippet(?array $attributes = []): string |
53
|
|
|
{ |
54
|
|
|
|
55
|
5 |
|
$data_attributes = []; |
56
|
5 |
|
$config_data_attributes = array_merge($this->getTagAttributes(), self::cleanAttributes($attributes)); |
57
|
5 |
|
ksort($config_data_attributes); |
58
|
5 |
|
foreach ($config_data_attributes as $k => $v) { |
59
|
5 |
|
if ($v) { |
60
|
5 |
|
$data_attributes[] = 'data-' . $k . '="' . $v . '"'; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
$html = '<div class="g-recaptcha" ' . implode(" ", $data_attributes) . ' id="recaptcha-element"></div>'; |
65
|
|
|
|
66
|
5 |
|
return $html; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
* @throws InvalidConfigurationException |
72
|
|
|
*/ |
73
|
10 |
|
public function getTagAttributes(): array |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
$tag_attributes = [ |
77
|
10 |
|
'sitekey' => $this->api_site_key |
78
|
|
|
]; |
79
|
|
|
|
80
|
10 |
|
$tag_attributes = array_merge($tag_attributes, config('recaptcha.tag_attributes', [])); |
81
|
|
|
|
82
|
10 |
|
if (Arr::get($tag_attributes, 'callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) { |
83
|
1 |
|
throw new InvalidConfigurationException('Property "callback" ("data-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"'); |
84
|
|
|
} |
85
|
|
|
|
86
|
9 |
|
if (Arr::get($tag_attributes, 'expired-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) { |
87
|
|
|
throw new InvalidConfigurationException('Property "expired-callback" ("data-expired-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"'); |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
if (Arr::get($tag_attributes, 'error-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) { |
91
|
|
|
throw new InvalidConfigurationException('Property "error-callback" ("data-error-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"'); |
92
|
|
|
} |
93
|
|
|
|
94
|
9 |
|
return $tag_attributes; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
3 |
|
public function getOnLoadCallback(): string |
101
|
|
|
{ |
102
|
|
|
|
103
|
3 |
|
$attributes = $this->getTagAttributes(); |
104
|
|
|
|
105
|
2 |
|
return "<script>var biscolabOnloadCallback = function() {grecaptcha.render('recaptcha-element', " . json_encode($attributes) . ");};</script>"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Compare given attributes with allowed attributes |
110
|
|
|
* |
111
|
|
|
* @param array|null $attributes |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
6 |
|
public static function cleanAttributes(?array $attributes = []): array |
115
|
|
|
{ |
116
|
|
|
return array_filter($attributes, function ($k) { |
117
|
3 |
|
return in_array($k, self::$allowed_data_attribute); |
118
|
6 |
|
}, ARRAY_FILTER_USE_KEY); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|