1 | <?php namespace Arcanedev\NoCaptcha; |
||
13 | abstract class AbstractNoCaptcha implements Contracts\NoCaptcha |
||
14 | { |
||
15 | /* ----------------------------------------------------------------- |
||
16 | | Constants |
||
17 | | ----------------------------------------------------------------- |
||
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 | const CAPTCHA_NAME = 'g-recaptcha-response'; |
||
23 | |||
24 | /* ----------------------------------------------------------------- |
||
25 | | Properties |
||
26 | | ----------------------------------------------------------------- |
||
27 | */ |
||
28 | |||
29 | /** |
||
30 | * The shared key between your site and ReCAPTCHA |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $secret; |
||
35 | |||
36 | /** |
||
37 | * Your site key |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $siteKey; |
||
42 | |||
43 | /** |
||
44 | * Forces the widget to render in a specific language. |
||
45 | * Auto-detects the user's language if unspecified. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $lang; |
||
50 | |||
51 | /** |
||
52 | * HTTP Request Client |
||
53 | * |
||
54 | * @var \Arcanedev\NoCaptcha\Contracts\Utilities\Request |
||
55 | */ |
||
56 | protected $request; |
||
57 | |||
58 | /** |
||
59 | * ReCaptcha's response. |
||
60 | * |
||
61 | * @var \Arcanedev\NoCaptcha\Utilities\ResponseV3|null |
||
62 | */ |
||
63 | protected $response; |
||
64 | |||
65 | /* ----------------------------------------------------------------- |
||
66 | | Constructor |
||
67 | | ----------------------------------------------------------------- |
||
68 | */ |
||
69 | |||
70 | /** |
||
71 | * NoCaptcha constructor. |
||
72 | * |
||
73 | * @param string $secret |
||
74 | * @param string $siteKey |
||
75 | * @param string|null $lang |
||
76 | */ |
||
77 | 216 | public function __construct($secret, $siteKey, $lang = null) |
|
85 | |||
86 | /* ----------------------------------------------------------------- |
||
87 | | Getters & Setters |
||
88 | | ----------------------------------------------------------------- |
||
89 | */ |
||
90 | |||
91 | /** |
||
92 | * Set the secret key. |
||
93 | * |
||
94 | * @param string $secret |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | 216 | protected function setSecret($secret) |
|
106 | |||
107 | /** |
||
108 | * Get the site key. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 24 | public function getSiteKey() |
|
116 | |||
117 | /** |
||
118 | * Set Site key. |
||
119 | * |
||
120 | * @param string $siteKey |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | 216 | protected function setSiteKey($siteKey) |
|
132 | |||
133 | /** |
||
134 | * Set language code. |
||
135 | * |
||
136 | * @param string $lang |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | 216 | public function setLang($lang) |
|
146 | |||
147 | /** |
||
148 | * Set HTTP Request Client. |
||
149 | * |
||
150 | * @param \Arcanedev\NoCaptcha\Contracts\Utilities\Request $request |
||
151 | * |
||
152 | * @return $this |
||
153 | */ |
||
154 | 216 | public function setRequestClient(Contracts\Utilities\Request $request) |
|
160 | |||
161 | /** |
||
162 | * Get the last response. |
||
163 | * |
||
164 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|null |
||
165 | */ |
||
166 | 4 | public function getLastResponse() |
|
170 | |||
171 | /* ----------------------------------------------------------------- |
||
172 | | Main Methods |
||
173 | | ----------------------------------------------------------------- |
||
174 | */ |
||
175 | |||
176 | /** |
||
177 | * Verify Response. |
||
178 | * |
||
179 | * @param string $response |
||
180 | * @param string $clientIp |
||
181 | * |
||
182 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed |
||
183 | */ |
||
184 | 32 | public function verify($response, $clientIp = null) |
|
192 | |||
193 | /** |
||
194 | * Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA |
||
195 | * test using a PSR-7 ServerRequest object. |
||
196 | * |
||
197 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
198 | * |
||
199 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse |
||
200 | */ |
||
201 | 8 | public function verifyRequest(ServerRequestInterface $request) |
|
211 | |||
212 | /** |
||
213 | * Send verify request to API and get response. |
||
214 | * |
||
215 | * @param array $query |
||
216 | * |
||
217 | * @return \Arcanedev\NoCaptcha\Utilities\ResponseV3 |
||
218 | */ |
||
219 | 32 | protected function sendVerifyRequest(array $query = []) |
|
228 | |||
229 | /** |
||
230 | * Parse the response. |
||
231 | * |
||
232 | * @param string $json |
||
233 | * |
||
234 | * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed |
||
235 | */ |
||
236 | abstract protected function parseResponse($json); |
||
237 | |||
238 | /* ----------------------------------------------------------------- |
||
239 | | Check Methods |
||
240 | | ----------------------------------------------------------------- |
||
241 | */ |
||
242 | |||
243 | /** |
||
244 | * Check if has lang. |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 40 | protected function hasLang() |
|
252 | |||
253 | /** |
||
254 | * Check key. |
||
255 | * |
||
256 | * @param string $name |
||
257 | * @param string $value |
||
258 | */ |
||
259 | 216 | private static function checkKey($name, &$value) |
|
267 | |||
268 | /** |
||
269 | * Check if the value is a string value. |
||
270 | * |
||
271 | * @param string $name |
||
272 | * @param string $value |
||
273 | * |
||
274 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
275 | */ |
||
276 | 216 | private static function checkIsString($name, $value) |
|
284 | |||
285 | /** |
||
286 | * Check if the value is not empty. |
||
287 | * |
||
288 | * @param string $name |
||
289 | * @param string $value |
||
290 | * |
||
291 | * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException |
||
292 | */ |
||
293 | 216 | private static function checkIsNotEmpty($name, $value) |
|
299 | |||
300 | /* ----------------------------------------------------------------- |
||
301 | | Other Methods |
||
302 | | ----------------------------------------------------------------- |
||
303 | */ |
||
304 | |||
305 | /** |
||
306 | * Transform the string to an Html serializable object |
||
307 | * |
||
308 | * @param string $html |
||
309 | * |
||
310 | * @return \Illuminate\Support\HtmlString |
||
311 | */ |
||
312 | 124 | protected function toHtmlString($html) |
|
316 | } |
||
317 |