1 | <?php namespace Arcanedev\NoCaptcha; |
||
13 | class NoCaptcha implements Contracts\NoCaptcha |
||
14 | { |
||
15 | /* ------------------------------------------------------------------------------------------------ |
||
16 | | Constants |
||
17 | | ------------------------------------------------------------------------------------------------ |
||
18 | */ |
||
19 | const CLIENT_URL = 'https://www.google.com/recaptcha/api.js'; |
||
20 | const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
||
21 | const CAPTCHA_NAME = 'g-recaptcha-response'; |
||
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 | 84 | 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 | 84 | protected function setSecret($secret) |
|
111 | |||
112 | /** |
||
113 | * Set Site key. |
||
114 | * |
||
115 | * @param string $siteKey |
||
116 | * |
||
117 | * @return self |
||
118 | */ |
||
119 | 84 | protected function setSiteKey($siteKey) |
|
127 | |||
128 | /** |
||
129 | * Set language code. |
||
130 | * |
||
131 | * @param string $lang |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | 84 | public function setLang($lang) |
|
141 | |||
142 | /** |
||
143 | * Get script source link. |
||
144 | * |
||
145 | * @param string|null $callbackName |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 18 | private function getScriptSrc($callbackName = null) |
|
164 | |||
165 | /** |
||
166 | * Set HTTP Request Client. |
||
167 | * |
||
168 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\RequestInterface $request |
||
169 | * |
||
170 | * @return self |
||
171 | */ |
||
172 | 84 | public function setRequestClient( |
|
179 | |||
180 | /** |
||
181 | * Set noCaptcha Attributes. |
||
182 | * |
||
183 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\AttributesInterface $attributes |
||
184 | * |
||
185 | * @return self |
||
186 | */ |
||
187 | 84 | public function setAttributes( |
|
194 | |||
195 | /* ------------------------------------------------------------------------------------------------ |
||
196 | | Main Functions |
||
197 | | ------------------------------------------------------------------------------------------------ |
||
198 | */ |
||
199 | /** |
||
200 | * Display Captcha. |
||
201 | * |
||
202 | * @param string|null $name |
||
203 | * @param array $attributes |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 33 | public function display($name = null, array $attributes = []) |
|
216 | |||
217 | /** |
||
218 | * Display image Captcha. |
||
219 | * |
||
220 | * @param string|null $name |
||
221 | * @param array $attributes |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | 9 | public function image($name = null, array $attributes = []) |
|
231 | |||
232 | /** |
||
233 | * Display audio Captcha. |
||
234 | * |
||
235 | * @param string|null $name |
||
236 | * @param array $attributes |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 9 | public function audio($name = null, array $attributes = []) |
|
246 | |||
247 | /** |
||
248 | * Display an invisible Captcha (bind the challenge to a button). |
||
249 | * |
||
250 | * @param string $value |
||
251 | * @param array $attributes |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 3 | public function button($value, array $attributes = []) |
|
263 | |||
264 | /** |
||
265 | * Verify Response. |
||
266 | * |
||
267 | * @param string $response |
||
268 | * @param string $clientIp |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | 18 | public function verify($response, $clientIp = null) |
|
284 | |||
285 | /** |
||
286 | * Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA |
||
287 | * test using a PSR-7 ServerRequest object. |
||
288 | * |
||
289 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | 3 | public function verifyRequest(ServerRequestInterface $request) |
|
308 | |||
309 | /** |
||
310 | * Get script tag. |
||
311 | * |
||
312 | * @param string|null $callbackName |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | 18 | public function script($callbackName = null) |
|
327 | |||
328 | /** |
||
329 | * Get script tag with a callback function. |
||
330 | * |
||
331 | * @param array $captchas |
||
332 | * @param string $callbackName |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | 3 | public function scriptWithCallback(array $captchas, $callbackName = 'captchaRenderCallback') |
|
352 | |||
353 | /** |
||
354 | * Rendering captchas with callback function. |
||
355 | * |
||
356 | * @param array $captchas |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | private function renderCaptchas(array $captchas) |
||
366 | |||
367 | /* ------------------------------------------------------------------------------------------------ |
||
368 | | Check Functions |
||
369 | | ------------------------------------------------------------------------------------------------ |
||
370 | */ |
||
371 | /** |
||
372 | * Check if has lang. |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | 18 | private function hasLang() |
|
380 | |||
381 | /** |
||
382 | * Check if callback is not empty. |
||
383 | * |
||
384 | * @param string|null $callbackName |
||
385 | * |
||
386 | * @return bool |
||
387 | */ |
||
388 | 18 | private function hasCallbackName($callbackName) |
|
392 | |||
393 | /** |
||
394 | * Check key. |
||
395 | * |
||
396 | * @param string $name |
||
397 | * @param string $value |
||
398 | * |
||
399 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
400 | */ |
||
401 | 84 | private function checkKey($name, &$value) |
|
409 | |||
410 | /** |
||
411 | * Check if the value is a string value. |
||
412 | * |
||
413 | * @param string $name |
||
414 | * @param string $value |
||
415 | * |
||
416 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
417 | */ |
||
418 | 84 | private function checkIsString($name, $value) |
|
426 | |||
427 | /** |
||
428 | * Check if the value is not empty. |
||
429 | * |
||
430 | * @param string $name |
||
431 | * @param string $value |
||
432 | * |
||
433 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
434 | */ |
||
435 | 84 | private function checkIsNotEmpty($name, $value) |
|
441 | |||
442 | /* ------------------------------------------------------------------------------------------------ |
||
443 | | Other functions |
||
444 | | ------------------------------------------------------------------------------------------------ |
||
445 | */ |
||
446 | /** |
||
447 | * Send verify request to API and get response. |
||
448 | * |
||
449 | * @param array $query |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | 18 | private function sendVerifyRequest(array $query = []) |
|
461 | } |
||
462 |