1 | <?php namespace Arcanedev\NoCaptcha; |
||
13 | abstract class AbstractNoCaptcha implements Contracts\NoCaptcha |
||
14 | { |
||
15 | /* ----------------------------------------------------------------- |
||
16 | | Constants |
||
17 | | ----------------------------------------------------------------- |
||
18 | */ |
||
19 | |||
20 | const CAPTCHA_NAME = 'g-recaptcha-response'; |
||
21 | |||
22 | /* ----------------------------------------------------------------- |
||
23 | | Properties |
||
24 | | ----------------------------------------------------------------- |
||
25 | */ |
||
26 | |||
27 | /** |
||
28 | * The shared key between your site and ReCAPTCHA |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $secret; |
||
33 | |||
34 | /** |
||
35 | * Your site key |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $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 | * HTTP Request Client |
||
51 | * |
||
52 | * @var \Arcanedev\NoCaptcha\Contracts\Utilities\Request |
||
53 | */ |
||
54 | protected $request; |
||
55 | |||
56 | /** |
||
57 | * ReCaptcha's response. |
||
58 | * |
||
59 | * @var \Arcanedev\NoCaptcha\Utilities\ResponseV3|null |
||
60 | */ |
||
61 | protected $response; |
||
62 | |||
63 | /** |
||
64 | * Use the global domain. |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | public static $useGlobalDomain = false; |
||
69 | |||
70 | /* ----------------------------------------------------------------- |
||
71 | | Constructor |
||
72 | | ----------------------------------------------------------------- |
||
73 | */ |
||
74 | |||
75 | /** |
||
76 | * NoCaptcha constructor. |
||
77 | * |
||
78 | * @param string $secret |
||
79 | * @param string $siteKey |
||
80 | * @param string|null $lang |
||
81 | */ |
||
82 | 236 | public function __construct($secret, $siteKey, $lang = null) |
|
90 | |||
91 | /* ----------------------------------------------------------------- |
||
92 | | Getters & Setters |
||
93 | | ----------------------------------------------------------------- |
||
94 | */ |
||
95 | |||
96 | /** |
||
97 | * Set the secret key. |
||
98 | * |
||
99 | * @param string $secret |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | 236 | protected function setSecret($secret) |
|
111 | |||
112 | /** |
||
113 | * Get the site key. |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 24 | public function getSiteKey() |
|
121 | |||
122 | /** |
||
123 | * Set Site key. |
||
124 | * |
||
125 | * @param string $siteKey |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | 236 | protected function setSiteKey($siteKey) |
|
137 | |||
138 | /** |
||
139 | * Set language code. |
||
140 | * |
||
141 | * @param string $lang |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | 236 | public function setLang($lang) |
|
151 | |||
152 | /** |
||
153 | * Set HTTP Request Client. |
||
154 | * |
||
155 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\Request $request |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | 236 | public function setRequestClient(Contracts\Utilities\Request $request) |
|
165 | |||
166 | /** |
||
167 | * Get the last response. |
||
168 | * |
||
169 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|null |
||
170 | */ |
||
171 | 4 | public function getLastResponse() |
|
175 | |||
176 | /** |
||
177 | * Get the client url. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 48 | public static function getClientUrl() |
|
187 | |||
188 | /** |
||
189 | * Get the verification url. |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | 40 | public static function getVerificationUrl() |
|
199 | |||
200 | /* ----------------------------------------------------------------- |
||
201 | | Main Methods |
||
202 | | ----------------------------------------------------------------- |
||
203 | */ |
||
204 | |||
205 | /** |
||
206 | * Verify Response. |
||
207 | * |
||
208 | * @param string $response |
||
209 | * @param string $clientIp |
||
210 | * |
||
211 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed |
||
212 | */ |
||
213 | 32 | public function verify($response, $clientIp = null) |
|
221 | |||
222 | /** |
||
223 | * Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA |
||
224 | * test using a PSR-7 ServerRequest object. |
||
225 | * |
||
226 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
227 | * |
||
228 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse |
||
229 | */ |
||
230 | 8 | public function verifyRequest(ServerRequestInterface $request) |
|
240 | |||
241 | /** |
||
242 | * Send verify request to API and get response. |
||
243 | * |
||
244 | * @param array $query |
||
245 | * |
||
246 | * @return \Arcanedev\NoCaptcha\Utilities\ResponseV3 |
||
247 | */ |
||
248 | 32 | protected function sendVerifyRequest(array $query = []) |
|
257 | |||
258 | /** |
||
259 | * Parse the response. |
||
260 | * |
||
261 | * @param string $json |
||
262 | * |
||
263 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed |
||
264 | */ |
||
265 | abstract protected function parseResponse($json); |
||
266 | |||
267 | /* ----------------------------------------------------------------- |
||
268 | | Check Methods |
||
269 | | ----------------------------------------------------------------- |
||
270 | */ |
||
271 | |||
272 | /** |
||
273 | * Check if has lang. |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 40 | protected function hasLang() |
|
281 | |||
282 | /** |
||
283 | * Check key. |
||
284 | * |
||
285 | * @param string $name |
||
286 | * @param string $value |
||
287 | */ |
||
288 | 236 | private static function checkKey($name, &$value) |
|
296 | |||
297 | /** |
||
298 | * Check if the value is a string value. |
||
299 | * |
||
300 | * @param string $name |
||
301 | * @param string $value |
||
302 | * |
||
303 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
304 | */ |
||
305 | 236 | private static function checkIsString($name, $value) |
|
313 | |||
314 | /** |
||
315 | * Check if the value is not empty. |
||
316 | * |
||
317 | * @param string $name |
||
318 | * @param string $value |
||
319 | * |
||
320 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
321 | */ |
||
322 | 236 | private static function checkIsNotEmpty($name, $value) |
|
328 | |||
329 | /* ----------------------------------------------------------------- |
||
330 | | Other Methods |
||
331 | | ----------------------------------------------------------------- |
||
332 | */ |
||
333 | |||
334 | /** |
||
335 | * Transform the string to an Html serializable object |
||
336 | * |
||
337 | * @param string $html |
||
338 | * |
||
339 | * @return \Illuminate\Support\HtmlString |
||
340 | */ |
||
341 | 44 | protected function toHtmlString($html) |
|
345 | } |
||
346 |