1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2017 - present |
4
|
|
|
* LaravelGoogleRecaptcha - ReCaptchaBuilderInvisible.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 12/9/2018 |
8
|
|
|
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\ReCaptcha; |
12
|
|
|
|
13
|
|
|
use Biscolab\ReCaptcha\Exceptions\InvalidConfigurationException; |
14
|
|
|
use Illuminate\Support\Arr; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ReCaptchaBuilderInvisible |
18
|
|
|
* @package Biscolab\ReCaptcha |
19
|
|
|
*/ |
20
|
|
|
class ReCaptchaBuilderInvisible extends ReCaptchaBuilder |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var null|string |
25
|
|
|
*/ |
26
|
|
|
protected $form_id = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ReCaptchaBuilderInvisible constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $api_site_key |
32
|
|
|
* @param string $api_secret_key |
33
|
|
|
*/ |
34
|
18 |
|
public function __construct(string $api_site_key, string $api_secret_key) |
35
|
|
|
{ |
36
|
|
|
|
37
|
18 |
|
parent::__construct($api_site_key, $api_secret_key, 'invisible'); |
38
|
18 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Write HTML <button> tag in your HTML code |
42
|
|
|
* Insert before </form> tag |
43
|
|
|
* |
44
|
|
|
* @param string $button_label |
45
|
|
|
* @param array|null $properties |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
3 |
|
public function htmlFormButton($button_label = 'Submit', ?array $properties = []): string |
50
|
|
|
{ |
51
|
|
|
|
52
|
3 |
|
$tag_properties = ''; |
53
|
|
|
|
54
|
3 |
|
$properties = array_merge($properties, [ |
55
|
3 |
|
'data-sitekey' => $this->api_site_key, |
56
|
3 |
|
'data-callback' => 'biscolabLaravelReCaptcha', |
57
|
|
|
]); |
58
|
|
|
|
59
|
3 |
|
if (empty($properties['class'])) { |
60
|
2 |
|
$properties['class'] = 'g-recaptcha'; |
61
|
|
|
} else { |
62
|
1 |
|
$properties['class'] .= ' g-recaptcha'; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
ksort($properties); |
66
|
|
|
|
67
|
3 |
|
if ($properties) { |
68
|
|
|
// $tag_properties = str_replace("=", '="', |
69
|
|
|
// http_build_query($properties, null, '" ', PHP_QUERY_RFC3986)) . '"'; |
70
|
3 |
|
$temp_properties = []; |
71
|
3 |
|
foreach ($properties as $k => $v) { |
72
|
3 |
|
$temp_properties[] = $k . '="' . $v . '"'; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
$tag_properties = implode(" ", $temp_properties); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return ($this->version == 'invisible') ? '<button ' . $tag_properties . '>' . $button_label . '</button>' : ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Write script HTML tag in you HTML code |
83
|
|
|
* Insert before </head> tag |
84
|
|
|
* |
85
|
|
|
* @param array|null $configuration |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
public function htmlScriptTagJsApi(?array $configuration = []): string |
91
|
|
|
{ |
92
|
|
|
|
93
|
|
|
$html = parent::htmlScriptTagJsApi(); |
94
|
|
|
|
95
|
|
|
$form_id = Arr::get($configuration, 'form_id'); |
96
|
|
|
if (!$form_id) { |
97
|
|
|
$form_id = $this->getFormId(); |
98
|
|
|
} else { |
99
|
|
|
$this->form_id = $form_id; |
100
|
|
|
} |
101
|
|
|
$html .= '<script> |
102
|
|
|
function biscolabLaravelReCaptcha(token) { |
103
|
|
|
document.getElementById("' . $form_id . '").submit(); |
104
|
|
|
} |
105
|
|
|
</script>'; |
106
|
|
|
|
107
|
|
|
return $html; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
* @throws \Exception |
113
|
|
|
*/ |
114
|
1 |
|
public function getFormId(): string |
115
|
|
|
{ |
116
|
|
|
|
117
|
1 |
|
if (!$this->form_id) { |
118
|
1 |
|
$form_id = config('recaptcha.default_form_id'); |
119
|
|
|
} else { |
120
|
|
|
$form_id = $this->form_id; |
121
|
|
|
} |
122
|
1 |
|
if (!$form_id) { |
123
|
|
|
throw new InvalidConfigurationException("formId required"); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $form_id; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|