Changeset 3080

Show
Ignore:
Timestamp:
07/11/2008 01:30:46 PM (5 months ago)
Author:
Geert
Message:

Added a promote config setting for Captcha which basically means after how many valid responses you want to promote a user to human. As soon as a user has been promoted Captcha responses will automatically validate. The only thing you should add in your controller or view is a check that hides the Captcha field if the user has been promoted. I updated the examples controller as well.

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/application/controllers/examples.php

    r3047 r3080  
    175175        public function captcha() 
    176176        { 
    177                 // Look at the counters in the Session Profiler 
     177                // Look at the counters for valid and invalid 
     178                // responses in the Session Profiler. 
    178179                new Profiler; 
    179180 
    180                 // Load Captcha library 
     181                // Load Captcha library, you can supply the name 
     182                // of the config group you would like to use. 
    181183                $captcha = new Captcha; 
    182184 
    183                 // Ban bots (that accept session cookies) after 50 invalid responses 
     185                // Ban bots (that accept session cookies) after 50 invalid responses. 
    184186                // Be careful not to ban real people though! Set the threshold high enough. 
    185187                if ($captcha->invalid_count() > 49) 
     
    189191                if ($_POST) 
    190192                { 
    191                         // User has not given three valid responses yet 
    192                         if ($captcha->valid_count() < 3) 
     193                        // Captcha::valid() is a static method that can be used as a Validation rule also. 
     194                        if (Captcha::valid($this->input->post('captcha_response'))) 
    193195                        { 
    194                                 // Captcha::valid() is a static method that can be used as a Validation rule also 
    195                                 if (Captcha::valid($this->input->post('captcha_response'))) 
    196                                 { 
    197                                         echo '<p style="color:green">Good answer!</p>'; 
    198                                 } 
    199                                 else 
    200                                 { 
    201                                         echo '<p style="color:red">Wrong answer!</p>'; 
    202                                 } 
     196                                echo '<p style="color:green">Good answer!</p>'; 
    203197                        } 
     198                        else 
     199                        { 
     200                                echo '<p style="color:red">Wrong answer!</p>'; 
     201                        } 
    204202 
    205203                        // Validate other fields here 
    206204                } 
    207205 
    208                 // Open form 
     206                // Show form 
    209207                echo form::open(); 
    210208                echo '<p>Other form fields here...</p>'; 
    211209 
    212                 // Don't show Captcha anymore after three or more valid responses 
    213                 if ($captcha->valid_count() < 3) 
     210                // Don't show Captcha anymore after the user has given enough valid 
     211                // responses. The "enough" count is set in the captcha config. 
     212                if ( ! $captcha->promoted()) 
    214213                { 
    215214                        echo '<p>'; 
     
    217216                        echo '</p>'; 
    218217                        echo form::input('captcha_response'); 
     218                } 
     219                else 
     220                { 
     221                        echo '<p>You have been promoted to human.</p>'; 
    219222                } 
    220223 
  • trunk/system/config/captcha.php

    r3046 r3080  
    1414 *  background - Path to background image file 
    1515 *  font       - Path to font file 
     16 *  promote    - Valid response count threshold to promote user (FALSE to disable) 
    1617 */ 
    1718$config['default'] = array 
     
    2324        'background' => '', 
    2425        'font'       => SYSPATH.'fonts/DejaVuSerif.ttf', 
     26        'promote'    => FALSE, 
    2527); 
  • trunk/system/libraries/Captcha.php

    r3065 r3080  
    2727                'background' => '', 
    2828                'font'       => '', 
     29                'promote'    => FALSE, 
    2930        ); 
    3031 
     
    171172                static $counted; 
    172173 
     174                // User has been promoted, always TRUE and don't count anymore 
     175                if (self::instance()->promoted()) 
     176                        return TRUE; 
     177 
    173178                // Challenge result 
    174179                $result = (sha1(strtoupper($response)) === Session::instance()->get('captcha_response')); 
     
    253258 
    254259        /** 
     260         * Checks whether user has been promoted after having given enough valid responses. 
     261         * 
     262         * @return  boolean 
     263         */ 
     264        public function promoted() 
     265        { 
     266                // Promotion has been disabled 
     267                if (self::$config['promote'] === FALSE) 
     268                        return FALSE; 
     269 
     270                // Compare the valid response count to the threshold 
     271                return ($this->valid_count() >= self::$config['promote']); 
     272        } 
     273 
     274        /** 
    255275         * Output the Captcha challenge. 
    256276         *