| 24 | | // do not create a woltlab captcha |
| 25 | | // do not use woltlab validation |
| 26 | | case 'readData': |
| 27 | | $this->useCaptcha = $eventObj->useCaptcha; |
| 28 | | if($this->useCaptcha) { |
| 29 | | $this->captchaHTML = RecaptchaUtil::get_html(RECAPTCHA_PUBLICKEY); |
| | 36 | /** |
| | 37 | * Validates the captcha. |
| | 38 | */ |
| | 39 | protected function validate($eventObj, $className) { |
| | 40 | if ($this->useCaptcha) { |
| | 41 | try { |
| | 42 | ReCaptchaUtil::validateAnswer(); |
| | 43 | $this->useCaptcha = false; |
| | 44 | } |
| | 45 | catch (UserInputException $e) { |
| | 46 | if ($eventObj instanceof RegisterForm) { |
| | 47 | $eventObj->errorType[$e->getField()] = $e->getType(); |
| 31 | | $eventObj->useCaptcha = false; |
| 32 | | break; |
| 33 | | |
| 34 | | // reset state of use Captcha |
| 35 | | // push html code |
| 36 | | case 'assignVariables': |
| 37 | | $eventObj->useCaptcha = $this->useCaptcha; |
| 38 | | if($this->useCaptcha) { |
| 39 | | WCF::getTPL()->assign('recaptchaString', $this->captchaHTML); |
| 40 | | } |
| 41 | | break; |
| 42 | | |
| 43 | | // use recaptcha validation |
| 44 | | case 'validate': |
| 45 | | $resp = RecaptchaUtil::check_answer( |
| 46 | | RECAPTCHA_PRIVATEKEY, |
| 47 | | UserUtil::getIpAddress(), |
| 48 | | isset($_POST["recaptcha_challenge_field"]) ? $_POST["recaptcha_challenge_field"] : '', |
| 49 | | isset($_POST["recaptcha_response_field"]) ? $_POST["recaptcha_response_field"] : '' |
| 50 | | ); |
| 51 | | |
| 52 | | try { |
| 53 | | if(!$resp->is_valid) { |
| 54 | | throw new UserInputException('captchaString'); |
| 55 | | } |
| 56 | | |
| 57 | | // captcha ok |
| 58 | | WCF::getSession()->register('captchaDone', true); |
| 59 | | } |
| 60 | | catch (Exception $e) { |
| | 49 | else { |
| | 54 | } |
| | 55 | |
| | 56 | /** |
| | 57 | * @see Page::assignVariables() |
| | 58 | */ |
| | 59 | protected function assignVariables($eventObj) { |
| | 60 | // reset state of use Captcha |
| | 61 | $eventObj->useCaptcha = $this->useCaptcha; |
| | 62 | |
| | 63 | if ($this->useCaptcha) { |
| | 64 | |
| | 65 | // we need a positive (true) captchaID for showing the captcha fields. |
| | 66 | $eventObj->captchaID = true; |
| | 67 | |
| | 68 | WCF::getTPL()->assign(array( |
| | 69 | 'reCaptchaPublicKey' => ReCaptchaUtil::getPublicKey(), |
| | 70 | 'reCaptchaLanguage' => ReCaptchaUtil::getLanguageCode(), |
| | 71 | )); |
| | 72 | |
| | 73 | } |
| | 74 | } |
| | 75 | |
| | 76 | /** |
| | 77 | * Checks if we need to use a captcha and deactivates the original captcha. |
| | 78 | */ |
| | 79 | protected function readParameters($eventObj, $className) { |
| | 80 | |
| | 81 | // disable original captcha to avoid database entries for woltlab captchas |
| | 82 | WCF::getSession()->register('captchaDone', true); |
| | 83 | |
| | 84 | if ($eventObj instanceof UserLoginForm) { |
| | 85 | |
| | 86 | if (defined('LOGIN_USE_CAPTCHA') && LOGIN_USE_CAPTCHA) { |
| | 87 | $this->useCaptcha = true; |
| | 88 | } |
| | 89 | |
| | 90 | // Workaround for WCFRCS-2 <http://codingcorner.info/bugtracker/browse/WCFRCS-2> |
| | 91 | // We deactivate reCAPTCHA in this case, because we can't control the eventlistener. |
| | 92 | if (defined('FAILED_LOGIN_IP_CAPTCHA') && FAILED_LOGIN_IP_CAPTCHA > 0) { |
| | 93 | require_once(WCF_DIR.'lib/data/user/login/FailedLogin.class.php'); |
| | 94 | if (FailedLogin::countFailedLogins() > FAILED_LOGIN_IP_CAPTCHA) { |
| | 95 | if (!($eventObj instanceof UserLoginForm) || !defined('LOGIN_USE_CAPTCHA') |
| | 96 | || !LOGIN_USE_CAPTCHA || WCF::getSession()->getVar('captchaDone')) { |
| | 97 | $this->useCaptcha = false; |
| | 98 | } |
| | 99 | } |
| | 100 | } |
| | 101 | } |
| | 102 | else if (defined('REGISTER_USE_CAPTCHA') && REGISTER_USE_CAPTCHA && $eventObj instanceof RegisterForm) { |
| | 103 | $this->useCaptcha = true; |
| | 104 | } |
| | 105 | else if (!($eventObj instanceof UserLoginForm) && !($eventObj instanceof RegisterForm)) { |
| | 106 | $this->useCaptcha = $eventObj->useCaptcha; |
| | 107 | } |
| | 108 | |
| | 109 | if (WCF::getUser()->userID || WCF::getSession()->getVar('reCaptchaDone')) { |
| | 110 | $this->useCaptcha = false; |
| | 111 | } |
| | 112 | } |
| | 113 | |
| | 114 | /** |
| | 115 | * Reactivates captchas. |
| | 116 | */ |
| | 117 | protected function save() { |
| | 118 | WCF::getSession()->unregister('captchaDone'); |
| | 119 | WCF::getSession()->unregister('reCaptchaDone'); |