Changeset 2535

Show
Ignore:
Timestamp:
04/20/2008 04:04:33 AM (7 months ago)
Author:
Geert
Message:

Cleaning up Captcha library

Location:
trunk/system
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/config/captcha.php

    r2206 r2535  
    66 * Custom styles can be added by extending the Captcha class 
    77 */ 
     8 
    89/** 
    9  * Width of captcha image, ignored if using a background 
     10 * Width and height of the Captcha image. 
     11 * These settings are ignored if using a background image. 
    1012 */ 
    11 $config['width'] = 150; 
    12 /** 
    13  * Height of captcha image, ignored if using a background 
    14  */ 
     13$config['width']  = 150; 
    1514$config['height'] = 40; 
     15 
    1616/** 
    1717 * Captcha style to use. Default is 'basic' and is only for testing as it 
    18  * does not require any TrueType fonts installed. 
     18 * does not require any truetype fonts installed. 
    1919 * 'standard' is the recommended style. A font must be supplied. A background 
    2020 * image is optional. 
    2121 * 'math' is a 'solve the question' style. 
    2222 * A font must be supplied. A background image is optional. 
    23  * Custom styles can be added easily by extending the library 
     23 * Custom styles can be added easily by extending the library. 
    2424 */ 
    2525$config['style'] = 'basic'; 
     26 
    2627/** 
    27  * Number of characters to use for the captcha, ignored if using style 'math' 
    28  * Four or five seems optimal. 
     28 * Number of characters to use for the Captcha (4 or 5 recommended). 
     29 * This setting is ignored if using style 'math'. 
    2930 */ 
    3031$config['num_chars'] = 4; 
     32 
    3133/** 
    32  * Path to font files. Default is none, ''. Example 'application/fonts/' 
     34 * Path to font files. Example: 'application/fonts/'. 
    3335 * If using 'standard' style, you must supply a valid truetype font file. 
    3436 */ 
    3537$config['font_path'] = ''; 
     38 
    3639/** 
    37  * Name of the font, Case sensitive, with the file extension, default is '' 
     40 * Name of the font, with the file extension. Case sensitive. 
    3841 */ 
    3942$config['font_name'] = ''; 
     43 
    4044/** 
    41  * Background image. Default ''. Example 'application/images/pattern.jpg' 
     45 * Background image. Example: 'application/images/pattern.jpg'. 
    4246 * The dimensions of this image will be used. 
    4347 */ 
  • trunk/system/libraries/Captcha.php

    r2534 r2535  
    1111 */ 
    1212class Captcha_Core { 
    13         //config 
    14         protected $font_path = ''; 
    15         protected $font_name = ''; 
    16         protected $width = 150; 
    17         protected $height = 50; 
     13 
     14        // Config 
     15        protected $font_path        = ''; 
     16        protected $font_name        = ''; 
     17        protected $width            = 150; 
     18        protected $height           = 50; 
    1819        protected $background_image = ''; 
    19         protected $style = 'basic'; 
    20         protected $num_chars = 4; 
    21  
    22         // class internal variables 
     20        protected $style            = 'basic'; 
     21        protected $num_chars        = 4; 
     22 
     23        // Class internal variables 
    2324        protected $image; 
    2425        protected $color_black; 
     
    2829        protected $numerals = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'); 
    2930 
    30  
    3131        /** 
    3232         * Creates a new Captcha instance. 
     33         * 
    3334         * @throws  Kohana_Exception 
    3435         * @param   array  configuration 
     
    5051                $this->initialize($config); 
    5152 
    52                 // If using a background image, check it exists. 
     53                // If using a background image, check if it exists. 
    5354                if ($this->background_image) 
    5455                { 
     
    5657                                throw new Kohana_Exception('captcha.file_not_found', $this->background_image); 
    5758                } 
    58                 // If using a font, check it exists. 
     59 
     60                // If using a font, check if it exists. 
    5961                if ($this->font_name) 
    6062                { 
     
    8587 
    8688        /** 
    87          * Sets the captcha code to use 
    88          * 
    89          * @param   string captcha code generated in captcha controller 
     89         * Sets the Captcha code to use. 
     90         * 
     91         * @param   string  captcha code generated in captcha controller 
    9092         * @return  void 
    9193         */ 
    9294        public function set_code($str) 
    9395        { 
    94                 $this->captcha_code = $str; 
    95         } 
    96  
    97         /** 
    98          * Generates the Captcha Image 
    99          * 
    100          * @param   none 
     96                $this->captcha_code = (string) $str; 
     97        } 
     98 
     99        /** 
     100         * Generates the Captcha image. 
     101         * 
    101102         * @return  void 
    102103         */ 
    103104        public function render() 
    104105        { 
    105                 // if extending the class with a custom captcha function, name it 'xyz_captcha' 
    106                 // style 'xyz' must be added to config 
    107                 // Call the method that implements the captcha 
     106                // If extending the class with a custom Captcha function, name it 'xyz_captcha'. 
     107                // Style 'xyz' must be added to config. Now call the method that implements the Captcha. 
    108108                $this->{$this->style.'_captcha'}(); 
    109109 
    110                 //Tell browser what to expect 
    111                 // todo make this automatic 
    112                 //header("Content-Type: image/jpeg"); 
    113                 header("Content-Type: image/png"); 
    114  
    115                 //Output the captcha image 
    116                 //imagejpeg($this->image); 
     110                // Tell browser what to expect 
     111                // TODO: make this automatic 
     112                // header('Content-Type: image/jpeg'); 
     113                header('Content-Type: image/png'); 
     114 
     115                // Output the captcha image 
     116                // imagejpeg($this->image); 
    117117                imagepng($this->image); 
    118118 
    119                 //Free up resources 
     119                // Free up resources 
    120120                imagedestroy($this->image); 
    121121        } 
    122         /** 
    123          * Validates the captcha code against session captcha code 
    124          * 
    125          * @param   string captcha code text 
     122 
     123        /** 
     124         * Validates the Captcha code against session Captcha code 
     125         * 
     126         * @param   string   captcha code text 
    126127         * @return  boolean 
    127128         */ 
    128129        public static function valid_captcha($str) 
    129130        { 
    130                 return ($str == $_SESSION['captcha_code']) ? TRUE : FALSE; 
    131         } 
    132  
    133         /** 
    134          * Creates image resource and allocates some basic colors 
     131                return ((string) $str === Session::instance()->get('captcha_code')); 
     132        } 
     133 
     134        /** 
     135         * Creates image resource and allocates some basic colors. 
    135136         * If a background image is supplied, the image dimensions are used. 
    136          * @param   none 
     137         * 
    137138         * @return  void 
    138139         */ 
     
    141142                if ($this->background_image) 
    142143                { 
    143                         // todo create from any valid image 
     144                        // TODO: create from any valid image 
    144145                        $this->image = imagecreatefromjpeg($this->background_image); 
    145146                        $this->color_white = imagecolorallocate($this->image, 255, 255, 255); 
    146                         // get the background image dimensions 
    147                         $this->width = imagesx($this->image); 
     147 
     148                        // Get the background image dimensions 
     149                        $this->width  = imagesx($this->image); 
    148150                        $this->height = imagesy($this->image); 
    149151                } 
     
    153155                        $this->color_white = imagecolorallocate($this->image, 255, 255, 255); 
    154156 
    155                         // Fill the image with a colored gradient 
    156                         // Use random colors, but try not to obscure the text 
    157                         $left_color = array(mt_rand(100,255), 0, 255); 
     157                        // Fill the image with a colored gradient (use random colors, but try not to obscure text) 
     158                        $left_color  = array(mt_rand(100,255), 0, 255); 
    158159                        $right_color = array(100, 100, mt_rand(100,0)); 
    159160                        $this->img_color_gradient($this->image, 0, 0, $this->height, $this->width, $left_color, $right_color); 
     
    162163 
    163164        /** 
    164          * Allocates a background color to image 
    165          * 
    166          * @param   array GD image color identifier 
     165         * Allocates a background color to image. 
     166         * 
     167         * @param   array  GD image color identifier 
    167168         * @return  void 
    168169         */ 
     
    173174 
    174175        /** 
    175          * Draws a very basic captcha image: 
     176         * Draws a very basic Captcha image. 
    176177         * Requires only GD. Useful for testing or if you can't use truetype fonts. 
    177178         * 
    178          * @param   none 
    179179         * @return  void 
    180180         */ 
    181181        protected function basic_captcha() 
    182182        { 
    183                 $this->image = imagecreate($this->width, $this->height); 
     183                $this->image       = imagecreate($this->width, $this->height); 
    184184                $this->color_white = imagecolorallocate($this->image, 255, 255, 255); 
    185185                $this->color_black = imagecolorallocate($this->image, 0, 0, 0); 
     
    189189 
    190190        /** 
    191          * Draws the standard captcha image: 
    192          * Requires GD with freetype and available true type compatible font files. 
     191         * Draws the standard Captcha image: 
     192         * Requires GD with freetype and available truetype compatible font files. 
    193193         * 
    194194         * @param   none 
     
    202202                $this->calculate_spacing(); 
    203203 
    204                 // draw each captcha character with varying attributes 
    205                 for ($i = 0; $i < strlen($this->captcha_code); $i++) 
    206                 { 
    207                         // allocate random color, size, rotation attributes to text. 
     204                // Draw each Captcha character with varying attributes 
     205                for ($i = 0, $strlen = strlen($this->captcha_code); $i < $strlen; $i++) 
     206                { 
     207                        // Allocate random color, size and rotation attributes to text 
    208208                        $text_color = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); 
    209209                        $angle = mt_rand(-40, 40); 
    210                         // make first char angle inward 
    211                         if ($i == 0) 
     210 
     211                        // Make first char angle inward 
     212                        if ($i === 0) 
    212213                        { 
    213214                                $angle = -abs($angle); 
    214215                        } 
    215                         // make last char angle inward 
    216                         if ($i == ($this->num_chars -1)) 
     216                        // Make last char angle inward 
     217                        if ($i === ($this->num_chars - 1)) 
    217218                        { 
    218219                                $angle = abs($angle); 
    219220                        } 
    220                         // Scale the charcter size on image height 
    221                         $font_size = mt_rand(($this->height -20), ($this->height -12)); 
    222  
     221 
     222                        // Scale the character size on image height 
     223                        $font_size = mt_rand($this->height - 20, $this->height - 12); 
    223224                        $char_details = imageftbbox($font_size, $angle, $font, $this->captcha_code[$i], array()); 
    224225 
    225             // calculate character starting coordinates 
    226             $iX = $this->spacing / 4 + $i * $this->spacing; 
    227             $char_height = $char_details[2] - $char_details[5]; 
    228             $iY = $this->height / 2 + $char_height / 4; 
    229  
    230             // write text character to image 
    231             imagefttext($this->image, $font_size, $angle, $iX, $iY, $text_color, $font, $this->captcha_code[$i], array()); 
    232                 } 
    233         } 
    234  
    235         /** 
    236          * Draws the math riddle captcha image: 
    237          * Requires GD with freetype and available true type compatible font files. 
    238          * 
    239          * @param   none 
     226                        // Calculate character starting coordinates 
     227                        $iX = $this->spacing / 4 + $i * $this->spacing; 
     228                        $char_height = $char_details[2] - $char_details[5]; 
     229                        $iY = $this->height / 2 + $char_height / 4; 
     230 
     231                        // Write text character to image 
     232                        imagefttext($this->image, $font_size, $angle, $iX, $iY, $text_color, $font, $this->captcha_code[$i], array()); 
     233                } 
     234        } 
     235 
     236        /** 
     237         * Draws the math riddle Captcha image. 
     238         * Requires GD with freetype and available truetype compatible font files. 
     239         * 
    240240         * @return  void 
    241241         */ 
    242242        protected function math_captcha() 
    243243        { 
    244                 $answer = $_SESSION['captcha_code']; 
    245                 //get the last digit 
    246                 $last_digit = substr($answer, -1); 
    247                 // convert to numeral 
    248                 $numeral = $this->numerals[$last_digit]; 
    249                 // subtract last digit from answer 
     244                $answer = Session::instance()->get('captcha_code'); 
     245 
     246                // Convert to numeral 
     247                $numeral = $this->numerals[substr($answer, -1)]; 
     248 
     249                // Subtract last digit from answer 
    250250                $number = substr($answer, 0, 2).'0'; 
     251 
    251252                // $number plus $numeral equals $answer 
    252253                $text = $number.' + '.$numeral.' = '; 
    253254                $this->img_create(); 
    254255                $font = $this->font_path.$this->font_name; 
    255                 // scale the font size to image height 
     256 
     257                // Scale the font size to image height 
    256258                $font_size = $this->height / 3; 
    257259                $text_details = imageftbbox($font_size, 0, $font, $text, array()); 
    258260                $iX = 5; 
    259261                $iY = ($this->height / 2) + 5; 
     262 
    260263                imagefttext($this->image, $font_size, 0, $iX, $iY, $this->color_white, $font, $text, array()); 
    261264        } 
    262         /** 
    263          * Calculates letter spacing for true type font characters 
    264          * 
    265          * @param   none 
     265 
     266        /** 
     267         * Calculates letter spacing for truetype font characters. 
     268         * 
    266269         * @return  integer 
    267270         */ 
    268271        protected function calculate_spacing() 
    269272        { 
    270                 $this->spacing = (int)($this->width / $this->num_chars); 
     273                return $this->spacing = (int) $this->width / $this->num_chars; 
    271274        } 
    272275 
     
    274277         * Fills the image with a colored gradient. 
    275278         * 
    276          * @param   resource gd image resource identifier 
    277          * @param   integer  start X position 
    278          * @param   integer  start Y position 
    279          * @param   integer  height of fill in pixels 
    280          * @param   integer  width of fill in pixels 
    281          * @param   resource gd image color identifier for left of image 
    282          * @param   resource gd image color identifier for right of image 
     279         * @param   resource  gd image resource identifier 
     280         * @param   integer   start X position 
     281         * @param   integer   start Y position 
     282         * @param   integer   height of fill in pixels 
     283         * @param   integer   width of fill in pixels 
     284         * @param   resource  gd image color identifier for left of image 
     285         * @param   resource  gd image color identifier for right of image 
    283286         * @return  void 
    284287         */ 
     
    288291                $color1 = ($left_color[1] - $right_color[1]) / $width; 
    289292                $color2 = ($left_color[2] - $right_color[2]) / $width; 
    290                 for ($i=0; $i <= $width; $i++) 
    291                 { 
    292                         $red = $left_color[0] -floor($i*$color0); 
     293 
     294                for ($i = 0; $i <= $width; $i++) 
     295                { 
     296                        $red   = $left_color[0] -floor($i*$color0); 
    293297                        $green = $left_color[1] -floor($i*$color1); 
    294                         $blue = $left_color[2] -floor($i*$color2); 
    295                         $col = imagecolorallocate($this->image, $red, $green, $blue); 
     298                        $blue  = $left_color[2] -floor($i*$color2); 
     299                        $col   = imagecolorallocate($this->image, $red, $green, $blue); 
     300 
    296301                        imageline($this->image, $x1 + $i, $y1, $x1 + $i, $y1 + $height, $col); 
    297302                }