Changeset 3031

Show
Ignore:
Timestamp:
07/10/2008 11:06:23 AM (3 months ago)
Author:
Geert
Message:

A few minor tweaks to Basic and Alpha Captcha style in order to preserve enough readability and make both styles differ a bit more.

Also changed internal terminology to the "official" convention: http://en.wikipedia.org/wiki/Challenge-response_authentication

Location:
trunk
Files:
5 modified

Legend:

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

    r3017 r3031  
    176176        { 
    177177                // Form submitted 
    178                 if (isset($_POST['captcha_answer'])) 
     178                if (isset($_POST['captcha_response'])) 
    179179                { 
    180180                        // Note: Captcha::valid() can be used in conjunction with the Validation library too 
    181                         echo (Captcha::valid($_POST['captcha_answer'])) ? 'Good answer!' : 'Wrong answer!'; 
     181                        echo (Captcha::valid($_POST['captcha_response'])) ? 'Good answer!' : 'Wrong answer!'; 
    182182                } 
    183183 
     
    187187                echo Captcha::factory()->render(); // <-- shows the Captcha challenge (image/riddle/etc) 
    188188                echo '</p>'; 
    189                 echo form::input('captcha_answer'); 
     189                echo form::input('captcha_response'); 
    190190                echo form::submit(array('value' => 'Check')); 
    191191                echo form::close(); 
  • trunk/system/config/captcha.php

    r3028 r3031  
    88 * 
    99 * Group Options: 
    10  *  style           - Captcha type, e.g. basic, word, math, riddle 
     10 *  style           - Captcha type, e.g. basic, alpha, word, math, riddle 
    1111 *  width           - Width of the Captcha image 
    1212 *  height          - Height of the Captcha image 
     
    1919$config['default'] = array 
    2020( 
    21         'style'           => 'basic', 
     21        'style'           => 'alpha', 
    2222        'width'           => 150, 
    2323        'height'          => 50, 
  • trunk/system/libraries/Captcha.php

    r3027 r3031  
    2727 
    2828        // The Captcha challenge answer, the text the user is supposed to enter 
    29         public static $answer; 
     29        public static $response; 
    3030 
    3131        /** 
     
    121121 
    122122                // Generate a new Captcha challenge 
    123                 self::$answer = (string) $this->driver->generate_challenge(); 
     123                self::$response = (string) $this->driver->generate_challenge(); 
    124124 
    125125                // Store the answer in a session 
    126                 Session::instance()->set('captcha_answer', self::$answer); 
     126                Session::instance()->set('captcha_response', self::$response); 
    127127 
    128128                Log::add('debug', 'Captcha Library initialized'); 
     
    130130 
    131131        /** 
    132          * Validates a Captcha answer. 
     132         * Validates a Captcha response. 
    133133         * 
    134          * @param   string   captcha answer 
     134         * @param   string   captcha response 
    135135         * @return  boolean 
    136136         */ 
    137         public static function valid($answer) 
     137        public static function valid($response) 
    138138        { 
    139                 return (strtoupper($answer) === strtoupper(Session::instance()->get('captcha_answer'))); 
     139                return (strtoupper($response) === strtoupper(Session::instance()->get('captcha_response'))); 
    140140        } 
    141141 
  • trunk/system/libraries/drivers/Captcha/Alpha.php

    r3030 r3031  
    11<?php defined('SYSPATH') or die('No direct script access.'); 
    22/** 
    3  * Captcha driver for "basic" style. 
     3 * Captcha driver for "alpha" style. 
    44 * 
    55 * $Id$ 
     
    3939                $this->image_gradient($color1, $color2); 
    4040 
     41                // Add a few random circles 
     42                for ($i = 0, $count = mt_rand(10, Captcha::$config['complexity'] * 3); $i < $count; $i++) 
     43                { 
     44                        $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), mt_rand(80, 120)); 
     45                        $size = mt_rand(5, Captcha::$config['height'] / 3); 
     46                        imagefilledellipse($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $size, $size, $color); 
     47                } 
     48 
    4149                // Calculate character font-size and spacing 
    42                 $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen(Captcha::$answer); 
    43                 $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen(Captcha::$answer)); 
     50                $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen(Captcha::$response); 
     51                $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen(Captcha::$response)); 
    4452 
    4553                // Background alphabetic character attributes 
     
    4856 
    4957                // Draw each Captcha character with varying attributes 
    50                 for ($i = 0, $strlen = strlen(Captcha::$answer); $i < $strlen; $i++) 
     58                for ($i = 0, $strlen = strlen(Captcha::$response); $i < $strlen; $i++) 
    5159                { 
    5260                        $angle = mt_rand(-40, 20); 
    5361                        // Scale the character size on image height 
    5462                        $size = $default_size / 10 * mt_rand(8, 12); 
    55                         $box = imageftbbox($size, $angle, Captcha::$config['font'], Captcha::$answer[$i]); 
     63                        $box = imageftbbox($size, $angle, Captcha::$config['font'], Captcha::$response[$i]); 
    5664 
    5765                        // Calculate character starting coordinates 
     
    6472 
    6573                        // Write text character to image 
    66                         imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], Captcha::$answer[$i]); 
     74                        imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], Captcha::$response[$i]); 
    6775 
    6876                        // Draw "ghost" alphabetic character 
    69                         $text_color = imagecolorallocatealpha($this->image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(60, 120)); 
    70                         $char = substr($chars, mt_rand(0,14), 1); 
    71                         imagettftext($this->image, $size, ($angle + (mt_rand(5,10))), ($x - (mt_rand(5,10))), ($y + (mt_rand(5,10))), $text_color, Captcha::$config['font'], $char); 
    72  
     77                        $text_color = imagecolorallocatealpha($this->image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(70, 120)); 
     78                        $char = substr($chars, mt_rand(0, 14), 1); 
     79                        imagettftext($this->image, $size * 2, mt_rand(-45, 45), ($x - (mt_rand(5, 10))), ($y + (mt_rand(5, 10))), $text_color, Captcha::$config['font'], $char); 
    7380                } 
    7481 
  • trunk/system/libraries/drivers/Captcha/Basic.php

    r3028 r3031  
    4040 
    4141                // Add a few random lines 
    42                 for ($i = 0, $count = mt_rand(5, 10); $i < $count; $i++) 
     42                for ($i = 0, $count = mt_rand(5, Captcha::$config['complexity'] * 3); $i < $count; $i++) 
    4343                { 
    44                         $color = imagecolorallocatealpha($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255), mt_rand(60, 120)); 
     44                        $color = imagecolorallocatealpha($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255), mt_rand(50, 120)); 
    4545                        imageline($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $color); 
    4646                } 
    4747 
    4848                // Calculate character font-size and spacing 
    49                 $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (strlen(Captcha::$answer) + 1); 
    50                 $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen(Captcha::$answer)); 
     49                $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (strlen(Captcha::$response) + 1); 
     50                $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen(Captcha::$response)); 
    5151 
    5252                // Draw each Captcha character with varying attributes 
    53                 for ($i = 0, $strlen = strlen(Captcha::$answer); $i < $strlen; $i++) 
     53                for ($i = 0, $strlen = strlen(Captcha::$response); $i < $strlen; $i++) 
    5454                { 
    5555                        // Allocate random color, size and rotation attributes to text 
     
    5959                        // Scale the character size on image height 
    6060                        $size = $default_size / 10 * mt_rand(8, 12); 
    61                         $box = imageftbbox($size, $angle, Captcha::$config['font'], Captcha::$answer[$i]); 
     61                        $box = imageftbbox($size, $angle, Captcha::$config['font'], Captcha::$response[$i]); 
    6262 
    6363                        // Calculate character starting coordinates 
     
    6666 
    6767                        // Write text character to image 
    68                         imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], Captcha::$answer[$i]); 
     68                        imagefttext($this->image, $size, $angle, $x, $y, $color, Captcha::$config['font'], Captcha::$response[$i]); 
    6969                } 
    7070