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\Request |
||
63 | */ |
||
64 | protected $request; |
||
65 | |||
66 | /** |
||
67 | * ReCaptcha's response. |
||
68 | * |
||
69 | * @var \Arcanedev\NoCaptcha\Utilities\Response|null |
||
70 | */ |
||
71 | protected $response; |
||
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 | */ |
||
85 | 51 | public function __construct($secret, $siteKey, $lang = null) |
|
93 | |||
94 | /* ----------------------------------------------------------------- |
||
95 | | Getters & Setters |
||
96 | | ----------------------------------------------------------------- |
||
97 | */ |
||
98 | |||
99 | /** |
||
100 | * Set the secret key. |
||
101 | * |
||
102 | * @param string $secret |
||
103 | * |
||
104 | * @return self |
||
105 | */ |
||
106 | 51 | protected function setSecret($secret) |
|
114 | |||
115 | /** |
||
116 | * Get the site key. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 15 | public function getSiteKey() |
|
124 | |||
125 | /** |
||
126 | * Set Site key. |
||
127 | * |
||
128 | * @param string $siteKey |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | 51 | protected function setSiteKey($siteKey) |
|
140 | |||
141 | /** |
||
142 | * Set language code. |
||
143 | * |
||
144 | * @param string $lang |
||
145 | * |
||
146 | * @return self |
||
147 | */ |
||
148 | 51 | public function setLang($lang) |
|
154 | |||
155 | /** |
||
156 | * Set HTTP Request Client. |
||
157 | * |
||
158 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\Request $request |
||
159 | * |
||
160 | * @return self |
||
161 | */ |
||
162 | 51 | public function setRequestClient(Contracts\Utilities\Request $request) |
|
168 | |||
169 | /** |
||
170 | * Get the last response. |
||
171 | * |
||
172 | * @return \Arcanedev\NoCaptcha\Utilities\Response|null |
||
173 | */ |
||
174 | 3 | public function getLastResponse() |
|
178 | |||
179 | /* ----------------------------------------------------------------- |
||
180 | | Main Methods |
||
181 | | ----------------------------------------------------------------- |
||
182 | */ |
||
183 | |||
184 | /** |
||
185 | * @param string $name |
||
186 | * |
||
187 | * @return \Illuminate\Support\HtmlString |
||
188 | */ |
||
189 | 9 | public function input($name = 'g-recaptcha-response') |
|
195 | |||
196 | /** |
||
197 | * Verify Response. |
||
198 | * |
||
199 | * @param string $response |
||
200 | * @param string $clientIp |
||
201 | * |
||
202 | * @return \Arcanedev\NoCaptcha\Utilities\Response |
||
203 | */ |
||
204 | 15 | public function verify($response, $clientIp = null) |
|
212 | |||
213 | /** |
||
214 | * Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA |
||
215 | * test using a PSR-7 ServerRequest object. |
||
216 | * |
||
217 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
218 | * |
||
219 | * @return \Arcanedev\NoCaptcha\Utilities\Response |
||
220 | */ |
||
221 | 3 | public function verifyRequest(ServerRequestInterface $request) |
|
231 | |||
232 | /** |
||
233 | * Get script tag. |
||
234 | * |
||
235 | * @param string|null $callbackName |
||
236 | * |
||
237 | * @return \Illuminate\Support\HtmlString |
||
238 | */ |
||
239 | 15 | public function script($callbackName = null) |
|
250 | |||
251 | /** |
||
252 | * Get the NoCaptcha API Script. |
||
253 | * |
||
254 | * @return \Illuminate\Support\HtmlString |
||
255 | */ |
||
256 | public function getApiScript() |
||
269 | |||
270 | /* ----------------------------------------------------------------- |
||
271 | | Check Methods |
||
272 | | ----------------------------------------------------------------- |
||
273 | */ |
||
274 | |||
275 | /** |
||
276 | * Check if has lang. |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 15 | private function hasLang() |
|
284 | |||
285 | /** |
||
286 | * Check if callback is not empty. |
||
287 | * |
||
288 | * @param string|null $callbackName |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | 15 | private function hasCallbackName($callbackName) |
|
296 | |||
297 | /** |
||
298 | * Check key. |
||
299 | * |
||
300 | * @param string $name |
||
301 | * @param string $value |
||
302 | */ |
||
303 | 51 | private static function checkKey($name, &$value) |
|
311 | |||
312 | /** |
||
313 | * Check if the value is a string value. |
||
314 | * |
||
315 | * @param string $name |
||
316 | * @param string $value |
||
317 | * |
||
318 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
319 | */ |
||
320 | 51 | private static function checkIsString($name, $value) |
|
328 | |||
329 | /** |
||
330 | * Check if the value is not empty. |
||
331 | * |
||
332 | * @param string $name |
||
333 | * @param string $value |
||
334 | * |
||
335 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
336 | */ |
||
337 | 51 | private static function checkIsNotEmpty($name, $value) |
|
343 | |||
344 | /* ----------------------------------------------------------------- |
||
345 | | Other Methods |
||
346 | | ----------------------------------------------------------------- |
||
347 | */ |
||
348 | |||
349 | /** |
||
350 | * Send verify request to API and get response. |
||
351 | * |
||
352 | * @param array $query |
||
353 | * |
||
354 | * @return \Arcanedev\NoCaptcha\Utilities\Response |
||
355 | */ |
||
356 | 15 | private function sendVerifyRequest(array $query = []) |
|
364 | |||
365 | /** |
||
366 | * Get script source link. |
||
367 | * |
||
368 | * @param string|null $callbackName |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | 15 | private function getScriptSrc($callbackName = null) |
|
387 | |||
388 | /** |
||
389 | * Transform the string to an Html serializable object |
||
390 | * |
||
391 | * @param string $html |
||
392 | * |
||
393 | * @return \Illuminate\Support\HtmlString |
||
394 | */ |
||
395 | 24 | protected function toHtmlString($html) |
|
399 | } |
||
400 |