Show
Ignore:
Timestamp:
06/02/11 09:20:45 (2 years ago)
Author:
Torben Brodt
Message:

recaptcha updates

Files:
1 moved

Legend:

Unmodified
Added
Removed
  • recaptcha/files/lib/system/event/listener/CaptchaFormReCaptchaListener.class.php

    r1438 r1452  
    22// wcf imports 
    33require_once(WCF_DIR.'lib/system/event/EventListener.class.php'); 
    4 require_once(WCF_DIR.'lib/util/RecaptchaUtil.class.php'); 
    5 require_once(WCF_DIR.'lib/util/UserUtil.class.php'); 
     4require_once(WCF_DIR.'lib/util/ReCaptchaUtil.class.php'); 
    65 
    76/** 
    8  * replaces the woltlab captcha through reCAPTCHA 
    9  *  
    10  * @author      Torben Brodt 
     7 * Replaces the original captcha with a reCaptcha captcha if enabled. 
     8 * 
     9 * @author      Markus Bartz <roul@codingcorner.info>, Torben Brodt <t.brodt@gmail.com> 
    1110 * @url         http://trac.easy-coding.de/trac/wcf/wiki/recaptcha 
    12  * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> 
     11 * @license     GNU Lesser General Public License <http://www.gnu.org/licenses/lgpl.html> 
    1312 */ 
    14 class RecaptchaListener implements EventListener { 
     13class CaptchaFormReCaptchaListener implements EventListener { 
     14 
     15        /** 
     16         * @var boolean 
     17         */ 
    1518        protected $useCaptcha = false; 
    16         protected $captchaHTML = ''; 
    1719 
    1820        /** 
     
    2022         */ 
    2123        public function execute($eventObj, $className, $eventName) { 
    22                 switch($eventName) { 
     24                if (MODULE_SYSTEM_RECAPTCHA) { 
     25                        switch ($eventName) { 
     26                                case 'readParameters': 
     27                                case 'validate': 
     28                                case 'save': 
     29                                case 'assignVariables': 
     30                                        $this->$eventName($eventObj, $className); 
     31                                        break; 
     32                        } 
     33                } 
     34        } 
    2335 
    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(); 
    3048                                } 
    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 { 
    6150                                        throw $e; 
    6251                                } 
    63                                  
    64                                 $this->useCaptcha = $eventObj->useCaptcha = false; 
    65                         break; 
     52                        } 
    6653                } 
     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'); 
    67120        } 
    68121}