Commit | Line | Data |
---|---|---|
ec9e64f0 TD |
1 | <?php |
2 | namespace wcf\system\recaptcha; | |
3 | use wcf\system\exception\SystemException; | |
4 | use wcf\system\exception\UserInputException; | |
5 | use wcf\system\SingletonFactory; | |
6 | use wcf\system\WCF; | |
7 | use wcf\util\JSON; | |
8 | use wcf\util\HTTPRequest; | |
9 | use wcf\util\UserUtil; | |
10 | ||
11 | /** | |
12 | * Handles reCAPTCHA V2 support. | |
13 | * | |
14 | * @author Tim Duesterhus | |
15 | * @copyright 2001-2014 WoltLab GmbH | |
16 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
17 | * @package com.woltlab.wcf | |
18 | * @subpackage system.recaptcha | |
19 | * @category Community Framework | |
20 | */ | |
21 | class RecaptchaHandlerV2 extends SingletonFactory { | |
22 | /** | |
23 | * Validates response. | |
24 | * | |
25 | * @param string $response | |
26 | */ | |
27 | public function validate($response) { | |
28 | // fail if response is empty to avoid sending api requests | |
29 | if (empty($response)) { | |
30 | throw new UserInputException('recaptchaString', 'false'); | |
31 | } | |
32 | ||
33 | $request = new HTTPRequest('https://www.google.com/recaptcha/api/siteverify?secret='.rawurlencode(RECAPTCHA_PRIVATEKEY).'&response='.rawurlencode($response).'&remoteip='.rawurlencode(UserUtil::getIpAddress()), array('timeout' => 10)); | |
34 | ||
35 | try { | |
36 | $request->execute(); | |
37 | $reply = $request->getReply(); | |
38 | $data = JSON::decode($reply['body']); | |
39 | ||
40 | if ($data['success']) { | |
41 | // yeah | |
42 | } | |
43 | else { | |
44 | throw new UserInputException('recaptchaString', 'false'); | |
45 | } | |
46 | } | |
47 | catch (SystemException $e) { | |
48 | // log error, but accept captcha | |
49 | $e->getExceptionID(); | |
50 | } | |
51 | ||
52 | WCF::getSession()->register('recaptchaDone', true); | |
53 | } | |
54 | } |