1 | <?php namespace Arcanedev\NoCaptcha; |
||
14 | class NoCaptcha implements Contracts\NoCaptcha |
||
15 | { |
||
16 | /* ------------------------------------------------------------------------------------------------ |
||
17 | | Constants |
||
18 | | ------------------------------------------------------------------------------------------------ |
||
19 | */ |
||
20 | const CLIENT_URL = 'https://www.google.com/recaptcha/api.js'; |
||
21 | const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
||
22 | |||
23 | /* ------------------------------------------------------------------------------------------------ |
||
24 | | Properties |
||
25 | | ------------------------------------------------------------------------------------------------ |
||
26 | */ |
||
27 | /** |
||
28 | * The shared key between your site and ReCAPTCHA |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $secret; |
||
33 | |||
34 | /** |
||
35 | * Your site key |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $siteKey; |
||
40 | |||
41 | /** |
||
42 | * Forces the widget to render in a specific language. |
||
43 | * Auto-detects the user's language if unspecified. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $lang; |
||
48 | |||
49 | /** |
||
50 | * Decides if we've already loaded the script file or not. |
||
51 | * |
||
52 | * @param bool |
||
53 | */ |
||
54 | protected $scriptLoaded = false; |
||
55 | |||
56 | /** |
||
57 | * HTTP Request Client |
||
58 | * |
||
59 | * @var \Arcanedev\NoCaptcha\Contracts\Utilities\RequestInterface |
||
60 | */ |
||
61 | protected $request; |
||
62 | |||
63 | /** |
||
64 | * noCaptcha Attributes |
||
65 | * |
||
66 | * @var \Arcanedev\NoCaptcha\Contracts\Utilities\AttributesInterface |
||
67 | */ |
||
68 | protected $attributes; |
||
69 | |||
70 | /* ------------------------------------------------------------------------------------------------ |
||
71 | | Constructor |
||
72 | | ------------------------------------------------------------------------------------------------ |
||
73 | */ |
||
74 | /** |
||
75 | * NoCaptcha constructor. |
||
76 | * |
||
77 | * @param string $secret |
||
78 | * @param string $siteKey |
||
79 | * @param string|null $lang |
||
80 | * @param array $attributes |
||
81 | */ |
||
82 | 276 | public function __construct($secret, $siteKey, $lang = null, array $attributes = []) |
|
91 | |||
92 | /* ------------------------------------------------------------------------------------------------ |
||
93 | | Getters & Setters |
||
94 | | ------------------------------------------------------------------------------------------------ |
||
95 | */ |
||
96 | /** |
||
97 | * Set the secret key. |
||
98 | * |
||
99 | * @param string $secret |
||
100 | * |
||
101 | * @return self |
||
102 | */ |
||
103 | 276 | protected function setSecret($secret) |
|
111 | |||
112 | /** |
||
113 | * Set Site key. |
||
114 | * |
||
115 | * @param string $siteKey |
||
116 | * |
||
117 | * @return self |
||
118 | */ |
||
119 | 276 | protected function setSiteKey($siteKey) |
|
127 | |||
128 | /** |
||
129 | * Set language code. |
||
130 | * |
||
131 | * @param string $lang |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | 276 | public function setLang($lang) |
|
141 | |||
142 | /** |
||
143 | * Get script source link. |
||
144 | * |
||
145 | * @param string|null $callbackName |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 72 | private function getScriptSrc($callbackName = null) |
|
166 | |||
167 | /** |
||
168 | * Set HTTP Request Client. |
||
169 | * |
||
170 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\RequestInterface $request |
||
171 | * |
||
172 | * @return self |
||
173 | */ |
||
174 | 276 | public function setRequestClient( |
|
181 | |||
182 | /** |
||
183 | * Set noCaptcha Attributes. |
||
184 | * |
||
185 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\AttributesInterface $attributes |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | 276 | public function setAttributes( |
|
196 | |||
197 | /** |
||
198 | * Get attribute name + id. |
||
199 | * |
||
200 | * @param string $name |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | 84 | protected function getNameAttribute($name) |
|
208 | |||
209 | /* ------------------------------------------------------------------------------------------------ |
||
210 | | Main Functions |
||
211 | | ------------------------------------------------------------------------------------------------ |
||
212 | */ |
||
213 | /** |
||
214 | * Display Captcha. |
||
215 | * |
||
216 | * @param string $name |
||
217 | * @param array $attributes |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | 84 | public function display($name, array $attributes = []) |
|
229 | |||
230 | /** |
||
231 | * Display image Captcha. |
||
232 | * |
||
233 | * @param string $name |
||
234 | * @param array $attributes |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 12 | public function image($name, array $attributes = []) |
|
244 | |||
245 | /** |
||
246 | * Display audio Captcha. |
||
247 | * |
||
248 | * @param string $name |
||
249 | * @param array $attributes |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | 12 | public function audio($name, array $attributes = []) |
|
259 | |||
260 | /** |
||
261 | * Verify Response. |
||
262 | * |
||
263 | * @param string $response |
||
264 | * @param string $clientIp |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | 72 | public function verify($response, $clientIp = null) |
|
280 | |||
281 | /** |
||
282 | * Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA |
||
283 | * test using a PSR-7 ServerRequest object. |
||
284 | * |
||
285 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 12 | public function verifyRequest(ServerRequestInterface $request) |
|
304 | |||
305 | /** |
||
306 | * Get script tag. |
||
307 | * |
||
308 | * @param string|null $callbackName |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | 72 | public function script($callbackName = null) |
|
323 | |||
324 | /** |
||
325 | * Get script tag with a callback function. |
||
326 | * |
||
327 | * @param array $captchas |
||
328 | * @param string $callbackName |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | 12 | public function scriptWithCallback(array $captchas, $callbackName = 'captchaRenderCallback') |
|
348 | |||
349 | /** |
||
350 | * Rendering captchas with callback function. |
||
351 | * |
||
352 | * @param array $captchas |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | private function renderCaptchas(array $captchas) |
||
362 | |||
363 | /* ------------------------------------------------------------------------------------------------ |
||
364 | | Check Functions |
||
365 | | ------------------------------------------------------------------------------------------------ |
||
366 | */ |
||
367 | /** |
||
368 | * Check if has lang. |
||
369 | * |
||
370 | * @return bool |
||
371 | */ |
||
372 | 72 | private function hasLang() |
|
376 | |||
377 | /** |
||
378 | * Check if callback is not empty. |
||
379 | * |
||
380 | * @param string|null $callbackName |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | 72 | private function hasCallbackName($callbackName) |
|
388 | |||
389 | /** |
||
390 | * Check key. |
||
391 | * |
||
392 | * @param string $name |
||
393 | * @param string $value |
||
394 | * |
||
395 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
396 | */ |
||
397 | 276 | private function checkKey($name, &$value) |
|
405 | |||
406 | /** |
||
407 | * Check if the value is a string value. |
||
408 | * |
||
409 | * @param string $name |
||
410 | * @param string $value |
||
411 | * |
||
412 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
413 | */ |
||
414 | 276 | private function checkIsString($name, $value) |
|
422 | |||
423 | /** |
||
424 | * Check if the value is not empty. |
||
425 | * |
||
426 | * @param string $name |
||
427 | * @param string $value |
||
428 | * |
||
429 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
430 | */ |
||
431 | 276 | private function checkIsNotEmpty($name, $value) |
|
437 | |||
438 | /* ------------------------------------------------------------------------------------------------ |
||
439 | | Other functions |
||
440 | | ------------------------------------------------------------------------------------------------ |
||
441 | */ |
||
442 | /** |
||
443 | * Send verify request to API and get response. |
||
444 | * |
||
445 | * @param array $query |
||
446 | * |
||
447 | * @return array |
||
448 | */ |
||
449 | 72 | private function sendVerifyRequest(array $query = []) |
|
457 | } |
||
458 |