Commit | Line | Data |
---|---|---|
ec9e64f0 TD |
1 | <?php |
2 | namespace wcf\system\recaptcha; | |
ec9e64f0 TD |
3 | use wcf\system\exception\UserInputException; |
4 | use wcf\system\SingletonFactory; | |
5 | use wcf\system\WCF; | |
ec9e64f0 | 6 | use wcf\util\HTTPRequest; |
9c435fae | 7 | use wcf\util\JSON; |
ec9e64f0 TD |
8 | use wcf\util\UserUtil; |
9 | ||
10 | /** | |
11 | * Handles reCAPTCHA V2 support. | |
12 | * | |
13 | * @author Tim Duesterhus | |
7b7b9764 | 14 | * @copyright 2001-2019 WoltLab GmbH |
ec9e64f0 | 15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 16 | * @package WoltLabSuite\Core\System\Recaptcha |
b763de7a | 17 | * @deprecated 5.4 - This was an implementation detail of wcf\system\captcha\RecaptchaHandler. |
ec9e64f0 TD |
18 | */ |
19 | class RecaptchaHandlerV2 extends SingletonFactory { | |
20 | /** | |
21 | * Validates response. | |
22 | * | |
23 | * @param string $response | |
60205493 | 24 | * @param string $type |
2b770bdd | 25 | * @throws UserInputException |
ec9e64f0 | 26 | */ |
7cc7a716 | 27 | public function validate($response, $type = 'v2') { |
ec9e64f0 TD |
28 | // fail if response is empty to avoid sending api requests |
29 | if (empty($response)) { | |
30 | throw new UserInputException('recaptchaString', 'false'); | |
31 | } | |
32 | ||
7cc7a716 TD |
33 | if ($type === 'v2') { |
34 | $key = RECAPTCHA_PRIVATEKEY; | |
35 | } | |
36 | else if ($type === 'invisible') { | |
7c4b6e24 | 37 | $key = RECAPTCHA_PRIVATEKEY_INVISIBLE; |
7cc7a716 TD |
38 | } |
39 | else { | |
40 | throw new \InvalidArgumentException('$type must be either v2 or invisible.'); | |
41 | } | |
42 | ||
43 | $request = new HTTPRequest('https://www.google.com/recaptcha/api/siteverify?secret='.rawurlencode($key).'&response='.rawurlencode($response).'&remoteip='.rawurlencode(UserUtil::getIpAddress()), ['timeout' => 10]); | |
ec9e64f0 TD |
44 | |
45 | try { | |
46 | $request->execute(); | |
47 | $reply = $request->getReply(); | |
48 | $data = JSON::decode($reply['body']); | |
49 | ||
50 | if ($data['success']) { | |
51 | // yeah | |
52 | } | |
53 | else { | |
54 | throw new UserInputException('recaptchaString', 'false'); | |
55 | } | |
56 | } | |
280b49db | 57 | catch (\Exception $e) { |
6e8c1fae TD |
58 | if ($e instanceof UserInputException) throw $e; |
59 | ||
ec9e64f0 | 60 | // log error, but accept captcha |
280b49db | 61 | \wcf\functions\exception\logThrowable($e); |
ec9e64f0 TD |
62 | } |
63 | ||
64 | WCF::getSession()->register('recaptchaDone', true); | |
65 | } | |
66 | } |