1 | <?php namespace Arcanedev\NoCaptcha; |
||
15 | class NoCaptchaV2 extends AbstractNoCaptcha |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Properties |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** |
||
23 | * Decides if we've already loaded the script file or not. |
||
24 | * |
||
25 | * @param bool |
||
26 | */ |
||
27 | protected $scriptLoaded = false; |
||
28 | |||
29 | /** |
||
30 | * noCaptcha Attributes |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $attributes; |
||
35 | |||
36 | /* ----------------------------------------------------------------- |
||
37 | | Constructor |
||
38 | | ----------------------------------------------------------------- |
||
39 | */ |
||
40 | |||
41 | /** |
||
42 | * NoCaptcha constructor. |
||
43 | * |
||
44 | * @param string $secret |
||
45 | * @param string $siteKey |
||
46 | * @param string|null $lang |
||
47 | * @param array $attributes |
||
48 | */ |
||
49 | 128 | public function __construct($secret, $siteKey, $lang = null, array $attributes = []) |
|
50 | { |
||
51 | 128 | parent::__construct($secret, $siteKey, $lang); |
|
52 | |||
53 | 128 | $this->setAttributes($attributes); |
|
54 | 128 | } |
|
55 | |||
56 | /* ----------------------------------------------------------------- |
||
57 | | Getters & Setters |
||
58 | | ----------------------------------------------------------------- |
||
59 | */ |
||
60 | |||
61 | /** |
||
62 | * Get script source link. |
||
63 | * |
||
64 | * @param string|null $callbackName |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 20 | private function getScriptSrc($callbackName = null) |
|
69 | { |
||
70 | 20 | $queries = []; |
|
71 | |||
72 | 20 | if ($this->hasLang()) |
|
73 | 8 | Arr::set($queries, 'hl', $this->lang); |
|
74 | |||
75 | 20 | if ($this->hasCallbackName($callbackName)) { |
|
76 | 4 | Arr::set($queries, 'onload', $callbackName); |
|
77 | 4 | Arr::set($queries, 'render', 'explicit'); |
|
78 | } |
||
79 | |||
80 | 20 | return static::CLIENT_URL . (count($queries) ? '?' . http_build_query($queries) : ''); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Set noCaptcha Attributes. |
||
85 | * |
||
86 | * @param array $attributes |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | 128 | public function setAttributes(array $attributes) |
|
91 | { |
||
92 | 128 | $this->attributes = $attributes; |
|
93 | |||
94 | 128 | return $this; |
|
95 | } |
||
96 | |||
97 | /* ----------------------------------------------------------------- |
||
98 | | Main Methods |
||
99 | | ----------------------------------------------------------------- |
||
100 | */ |
||
101 | |||
102 | /** |
||
103 | * Display Captcha. |
||
104 | * |
||
105 | * @param string|null $name |
||
106 | * @param array $attributes |
||
107 | * |
||
108 | * @return \Arcanedev\Html\Elements\Div |
||
109 | */ |
||
110 | 76 | public function display($name = null, array $attributes = []) |
|
111 | { |
||
112 | 76 | return Div::make()->attributes(array_merge( |
|
113 | 76 | static::prepareNameAttribute($name), |
|
114 | 64 | $this->prepareAttributes($attributes) |
|
115 | )); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Display image Captcha. |
||
120 | * |
||
121 | * @param string|null $name |
||
122 | * @param array $attributes |
||
123 | * |
||
124 | * @return \Arcanedev\Html\Elements\Div |
||
125 | */ |
||
126 | 12 | public function image($name = null, array $attributes = []) |
|
127 | { |
||
128 | 12 | return $this->display($name, array_merge( |
|
129 | 9 | $attributes, |
|
130 | 12 | ['data-type' => 'image'] |
|
131 | )); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Display audio Captcha. |
||
136 | * |
||
137 | * @param string|null $name |
||
138 | * @param array $attributes |
||
139 | * |
||
140 | * @return \Arcanedev\Html\Elements\Div |
||
141 | */ |
||
142 | 12 | public function audio($name = null, array $attributes = []) |
|
143 | { |
||
144 | 12 | return $this->display($name, array_merge( |
|
145 | 9 | $attributes, |
|
146 | 12 | ['data-type' => 'audio'] |
|
147 | )); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Display an invisible Captcha (bind the challenge to a button). |
||
152 | * |
||
153 | * @param string $value |
||
154 | * @param array $attributes |
||
155 | * |
||
156 | * @return \Arcanedev\Html\Elements\Button |
||
157 | */ |
||
158 | 4 | public function button($value, array $attributes = []) |
|
159 | { |
||
160 | 4 | return Button::make()->text($value)->attributes(array_merge( |
|
161 | 4 | ['data-callback' => 'onSubmit'], |
|
162 | 4 | $this->prepareAttributes($attributes) |
|
163 | )); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get script tag. |
||
168 | * |
||
169 | * @param string|null $callbackName |
||
170 | * |
||
171 | * @return \Illuminate\Support\HtmlString |
||
172 | */ |
||
173 | 20 | public function script($callbackName = null) |
|
184 | |||
185 | /** |
||
186 | * Get the NoCaptcha API Script. |
||
187 | * |
||
188 | * @return \Illuminate\Support\HtmlString |
||
189 | */ |
||
190 | 4 | public function getApiScript() |
|
232 | |||
233 | /** |
||
234 | * Get script tag with a callback function. |
||
235 | * |
||
236 | * @param array $captchas |
||
237 | * @param string $callbackName |
||
238 | * |
||
239 | * @return \Illuminate\Support\HtmlString |
||
240 | */ |
||
241 | 4 | public function scriptWithCallback(array $captchas, $callbackName = 'captchaRenderCallback') |
|
259 | |||
260 | /** |
||
261 | * Rendering captchas with callback function. |
||
262 | * |
||
263 | * @param array $captchas |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | 4 | private function renderCaptchas(array $captchas) |
|
273 | |||
274 | /* ----------------------------------------------------------------- |
||
275 | | Check Methods |
||
276 | | ----------------------------------------------------------------- |
||
277 | */ |
||
278 | |||
279 | /** |
||
280 | * Check if callback is not empty. |
||
281 | * |
||
282 | * @param string|null $callbackName |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 20 | private function hasCallbackName($callbackName) |
|
290 | |||
291 | /* ----------------------------------------------------------------- |
||
292 | | Other Methods |
||
293 | | ----------------------------------------------------------------- |
||
294 | */ |
||
295 | |||
296 | /** |
||
297 | * Parse the response. |
||
298 | * |
||
299 | * @param string $json |
||
300 | * |
||
301 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed |
||
302 | */ |
||
303 | 12 | protected function parseResponse($json) |
|
307 | |||
308 | /** |
||
309 | * Prepare the attributes. |
||
310 | * |
||
311 | * @param array $attributes |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | 68 | private function prepareAttributes(array $attributes) |
|
330 | |||
331 | /** |
||
332 | * Check the `data-*` attribute. |
||
333 | * |
||
334 | * @param array $attributes |
||
335 | * @param string $name |
||
336 | * @param array $supported |
||
337 | * @param string $default |
||
338 | */ |
||
339 | 68 | private static function checkDataAttribute(array &$attributes, $name, array $supported, $default) |
|
351 | |||
352 | /** |
||
353 | * Prepare the name and id attributes. |
||
354 | * |
||
355 | * @param string|null $name |
||
356 | * |
||
357 | * @return array |
||
358 | * |
||
359 | * @throws \Arcanedev\NoCaptcha\Exceptions\InvalidArgumentException |
||
360 | */ |
||
361 | 76 | protected static function prepareNameAttribute($name) |
|
374 | } |
||
375 |