Implement Recaptcha V2
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / captcha / RecaptchaHandler.class.php
CommitLineData
96714cab
MS
1<?php
2namespace wcf\system\captcha;
3use wcf\system\WCF;
4use wcf\util\StringUtil;
5
6/**
7 * Captcha handler for reCAPTCHA.
8 *
9 * @author Matthias Schmidt
10 * @copyright 2001-2014 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @package com.woltlab.wcf
13 * @subpackage system.captcha
14 * @category Community Framework
15 */
16class RecaptchaHandler implements ICaptchaHandler {
17 /**
18 * recaptcha challenge
19 * @var string
20 */
21 public $challenge = '';
22
23 /**
24 * response to the challenge
25 * @var string
26 */
27 public $response = '';
28
29 /**
30 * @see \wcf\system\captcha\ICaptchaHandler::getFormElement()
31 */
32 public function getFormElement() {
ec9e64f0
TD
33 if (WCF::getSession()->getVar('recaptchaDone')) return '';
34
35 if (!RECAPTCHA_PUBLICKEY || !RECAPTCHA_PRIVATEKEY) {
36 // V1
37 \wcf\system\recaptcha\RecaptchaHandler::getInstance()->assignVariables();
38 }
39 else {
40 // V2
41 WCF::getTPL()->assign(array(
42 'recaptchaLegacyMode' => true
43 ));
44 }
96714cab
MS
45
46 return WCF::getTPL()->fetch('recaptcha');
47 }
48
49 /**
50 * @see \wcf\system\captcha\ICaptchaHandler::isAvailable()
51 */
52 public function isAvailable() {
ec9e64f0 53 return true;
96714cab
MS
54 }
55
56 /**
57 * @see \wcf\system\captcha\ICaptchaHandler::readFormParameters()
58 */
59 public function readFormParameters() {
ec9e64f0
TD
60 if (!RECAPTCHA_PUBLICKEY || !RECAPTCHA_PRIVATEKEY) {
61 // V1
62 if (isset($_POST['recaptcha_challenge_field'])) $this->challenge = StringUtil::trim($_POST['recaptcha_challenge_field']);
63 if (isset($_POST['recaptcha_response_field'])) $this->response = StringUtil::trim($_POST['recaptcha_response_field']);
64 }
65 else {
66 // V2
67 if (isset($_POST['g-recaptcha-response'])) $this->response = $_POST['g-recaptcha-response'];
68 }
96714cab
MS
69 }
70
71 /**
72 * @see \wcf\system\captcha\ICaptchaHandler::reset()
73 */
74 public function reset() {
75 WCF::getSession()->unregister('recaptchaDone');
76 }
77
78 /**
79 * @see \wcf\system\captcha\ICaptchaHandler::validate()
80 */
81 public function validate() {
ec9e64f0
TD
82 if (WCF::getSession()->getVar('recaptchaDone')) return;
83
84 if (!RECAPTCHA_PUBLICKEY || !RECAPTCHA_PRIVATEKEY) {
85 // V1
86 \wcf\system\recaptcha\RecaptchaHandler::getInstance()->validate($this->challenge, $this->response);
87 }
88 else {
89 // V2
90 \wcf\system\recaptcha\RecaptchaHandlerV2::getInstance()->validate($this->response);
91 }
96714cab
MS
92 }
93}