1 | <?php namespace Arcanedev\NoCaptcha; |
||
14 | class NoCaptcha implements Contracts\NoCaptcha |
||
15 | { |
||
16 | /* ----------------------------------------------------------------- |
||
17 | | Constants |
||
18 | | ----------------------------------------------------------------- |
||
19 | */ |
||
20 | |||
21 | const CLIENT_URL = 'https://www.google.com/recaptcha/api.js'; |
||
22 | const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
||
23 | const CAPTCHA_NAME = 'g-recaptcha-response'; |
||
24 | |||
25 | /* ----------------------------------------------------------------- |
||
26 | | Properties |
||
27 | | ----------------------------------------------------------------- |
||
28 | */ |
||
29 | |||
30 | /** |
||
31 | * The shared key between your site and ReCAPTCHA |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $secret; |
||
36 | |||
37 | /** |
||
38 | * Your site key |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $siteKey; |
||
43 | |||
44 | /** |
||
45 | * Forces the widget to render in a specific language. |
||
46 | * Auto-detects the user's language if unspecified. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $lang; |
||
51 | |||
52 | /** |
||
53 | * Decides if we've already loaded the script file or not. |
||
54 | * |
||
55 | * @param bool |
||
56 | */ |
||
57 | protected $scriptLoaded = false; |
||
58 | |||
59 | /** |
||
60 | * HTTP Request Client |
||
61 | * |
||
62 | * @var \Arcanedev\NoCaptcha\Contracts\Utilities\RequestInterface |
||
63 | */ |
||
64 | protected $request; |
||
65 | |||
66 | /** |
||
67 | * noCaptcha Attributes |
||
68 | * |
||
69 | * @var \Arcanedev\NoCaptcha\Contracts\Utilities\AttributesInterface |
||
70 | */ |
||
71 | protected $attributes; |
||
72 | |||
73 | /* ----------------------------------------------------------------- |
||
74 | | Constructor |
||
75 | | ----------------------------------------------------------------- |
||
76 | */ |
||
77 | |||
78 | /** |
||
79 | * NoCaptcha constructor. |
||
80 | * |
||
81 | * @param string $secret |
||
82 | * @param string $siteKey |
||
83 | * @param string|null $lang |
||
84 | * @param array $attributes |
||
85 | */ |
||
86 | 108 | public function __construct($secret, $siteKey, $lang = null, array $attributes = []) |
|
95 | |||
96 | /* ----------------------------------------------------------------- |
||
97 | | Getters & Setters |
||
98 | | ----------------------------------------------------------------- |
||
99 | */ |
||
100 | |||
101 | /** |
||
102 | * Set the secret key. |
||
103 | * |
||
104 | * @param string $secret |
||
105 | * |
||
106 | * @return self |
||
107 | */ |
||
108 | 108 | protected function setSecret($secret) |
|
116 | |||
117 | /** |
||
118 | * Set Site key. |
||
119 | * |
||
120 | * @param string $siteKey |
||
121 | * |
||
122 | * @return self |
||
123 | */ |
||
124 | 108 | protected function setSiteKey($siteKey) |
|
132 | |||
133 | /** |
||
134 | * Set language code. |
||
135 | * |
||
136 | * @param string $lang |
||
137 | * |
||
138 | * @return self |
||
139 | */ |
||
140 | 108 | public function setLang($lang) |
|
146 | |||
147 | /** |
||
148 | * Get script source link. |
||
149 | * |
||
150 | * @param string|null $callbackName |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 18 | private function getScriptSrc($callbackName = null) |
|
168 | |||
169 | /** |
||
170 | * Set HTTP Request Client. |
||
171 | * |
||
172 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\RequestInterface $request |
||
173 | * |
||
174 | * @return self |
||
175 | */ |
||
176 | 108 | public function setRequestClient(Contracts\Utilities\RequestInterface $request) |
|
182 | |||
183 | /** |
||
184 | * Set noCaptcha Attributes. |
||
185 | * |
||
186 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\AttributesInterface $attributes |
||
187 | * |
||
188 | * @return self |
||
189 | */ |
||
190 | 108 | public function setAttributes(Contracts\Utilities\AttributesInterface $attributes) |
|
196 | |||
197 | /* ----------------------------------------------------------------- |
||
198 | | Main Methods |
||
199 | | ----------------------------------------------------------------- |
||
200 | */ |
||
201 | |||
202 | /** |
||
203 | * Display Captcha. |
||
204 | * |
||
205 | * @param string|null $name |
||
206 | * @param array $attributes |
||
207 | * |
||
208 | * @return \Illuminate\Support\HtmlString |
||
209 | */ |
||
210 | 60 | public function display($name = null, array $attributes = []) |
|
219 | |||
220 | /** |
||
221 | * Display image Captcha. |
||
222 | * |
||
223 | * @param string|null $name |
||
224 | * @param array $attributes |
||
225 | * |
||
226 | * @return \Illuminate\Support\HtmlString |
||
227 | */ |
||
228 | 9 | public function image($name = null, array $attributes = []) |
|
234 | |||
235 | /** |
||
236 | * Display audio Captcha. |
||
237 | * |
||
238 | * @param string|null $name |
||
239 | * @param array $attributes |
||
240 | * |
||
241 | * @return \Illuminate\Support\HtmlString |
||
242 | */ |
||
243 | 9 | public function audio($name = null, array $attributes = []) |
|
249 | |||
250 | /** |
||
251 | * Display an invisible Captcha (bind the challenge to a button). |
||
252 | * |
||
253 | * @param string $value |
||
254 | * @param array $attributes |
||
255 | * |
||
256 | * @return \Illuminate\Support\HtmlString |
||
257 | */ |
||
258 | 3 | public function button($value, array $attributes = []) |
|
268 | |||
269 | /** |
||
270 | * Verify Response. |
||
271 | * |
||
272 | * @param string $response |
||
273 | * @param string $clientIp |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 15 | public function verify($response, $clientIp = null) |
|
289 | |||
290 | /** |
||
291 | * Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA |
||
292 | * test using a PSR-7 ServerRequest object. |
||
293 | * |
||
294 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
295 | * |
||
296 | * @return bool |
||
297 | */ |
||
298 | 3 | public function verifyRequest(ServerRequestInterface $request) |
|
308 | |||
309 | /** |
||
310 | * Get script tag. |
||
311 | * |
||
312 | * @param string|null $callbackName |
||
313 | * |
||
314 | * @return \Illuminate\Support\HtmlString |
||
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 \Illuminate\Support\HtmlString |
||
335 | */ |
||
336 | 3 | public function scriptWithCallback(array $captchas, $callbackName = 'captchaRenderCallback') |
|
353 | |||
354 | /** |
||
355 | * Rendering captchas with callback function. |
||
356 | * |
||
357 | * @param array $captchas |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 3 | private function renderCaptchas(array $captchas) |
|
369 | |||
370 | /* ----------------------------------------------------------------- |
||
371 | | Check Methods |
||
372 | | ----------------------------------------------------------------- |
||
373 | */ |
||
374 | |||
375 | /** |
||
376 | * Check if has lang. |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | 18 | private function hasLang() |
|
384 | |||
385 | /** |
||
386 | * Check if callback is not empty. |
||
387 | * |
||
388 | * @param string|null $callbackName |
||
389 | * |
||
390 | * @return bool |
||
391 | */ |
||
392 | 18 | private function hasCallbackName($callbackName) |
|
396 | |||
397 | /** |
||
398 | * Check key. |
||
399 | * |
||
400 | * @param string $name |
||
401 | * @param string $value |
||
402 | */ |
||
403 | 108 | private function checkKey($name, &$value) |
|
411 | |||
412 | /** |
||
413 | * Check if the value is a string value. |
||
414 | * |
||
415 | * @param string $name |
||
416 | * @param string $value |
||
417 | * |
||
418 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
419 | */ |
||
420 | 108 | private function checkIsString($name, $value) |
|
428 | |||
429 | /** |
||
430 | * Check if the value is not empty. |
||
431 | * |
||
432 | * @param string $name |
||
433 | * @param string $value |
||
434 | * |
||
435 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
436 | */ |
||
437 | 108 | private function checkIsNotEmpty($name, $value) |
|
443 | |||
444 | /* ----------------------------------------------------------------- |
||
445 | | Other Methods |
||
446 | | ----------------------------------------------------------------- |
||
447 | */ |
||
448 | |||
449 | /** |
||
450 | * Send verify request to API and get response. |
||
451 | * |
||
452 | * @param array $query |
||
453 | * |
||
454 | * @return array |
||
455 | */ |
||
456 | 15 | private function sendVerifyRequest(array $query = []) |
|
462 | |||
463 | /** |
||
464 | * Get the NoCaptcha API Script. |
||
465 | * |
||
466 | * @return \Illuminate\Support\HtmlString |
||
467 | */ |
||
468 | 3 | protected function getApiScript() |
|
502 | |||
503 | /** |
||
504 | * Transform the string to an Html serializable object |
||
505 | * |
||
506 | * @param string $html |
||
507 | * |
||
508 | * @return \Illuminate\Support\HtmlString |
||
509 | */ |
||
510 | 72 | protected function toHtmlString($html) |
|
514 | } |
||
515 |