Changeset 3031
- Timestamp:
- 07/10/2008 11:06:23 AM (3 months ago)
- Location:
- trunk
- Files:
-
- 5 modified
-
application/controllers/examples.php (modified) (2 diffs)
-
system/config/captcha.php (modified) (2 diffs)
-
system/libraries/Captcha.php (modified) (3 diffs)
-
system/libraries/drivers/Captcha/Alpha.php (modified) (4 diffs)
-
system/libraries/drivers/Captcha/Basic.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/application/controllers/examples.php
r3017 r3031 176 176 { 177 177 // Form submitted 178 if (isset($_POST['captcha_ answer']))178 if (isset($_POST['captcha_response'])) 179 179 { 180 180 // 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!'; 182 182 } 183 183 … … 187 187 echo Captcha::factory()->render(); // <-- shows the Captcha challenge (image/riddle/etc) 188 188 echo '</p>'; 189 echo form::input('captcha_ answer');189 echo form::input('captcha_response'); 190 190 echo form::submit(array('value' => 'Check')); 191 191 echo form::close(); -
trunk/system/config/captcha.php
r3028 r3031 8 8 * 9 9 * Group Options: 10 * style - Captcha type, e.g. basic, word, math, riddle10 * style - Captcha type, e.g. basic, alpha, word, math, riddle 11 11 * width - Width of the Captcha image 12 12 * height - Height of the Captcha image … … 19 19 $config['default'] = array 20 20 ( 21 'style' => ' basic',21 'style' => 'alpha', 22 22 'width' => 150, 23 23 'height' => 50, -
trunk/system/libraries/Captcha.php
r3027 r3031 27 27 28 28 // The Captcha challenge answer, the text the user is supposed to enter 29 public static $ answer;29 public static $response; 30 30 31 31 /** … … 121 121 122 122 // Generate a new Captcha challenge 123 self::$ answer= (string) $this->driver->generate_challenge();123 self::$response = (string) $this->driver->generate_challenge(); 124 124 125 125 // Store the answer in a session 126 Session::instance()->set('captcha_ answer', self::$answer);126 Session::instance()->set('captcha_response', self::$response); 127 127 128 128 Log::add('debug', 'Captcha Library initialized'); … … 130 130 131 131 /** 132 * Validates a Captcha answer.132 * Validates a Captcha response. 133 133 * 134 * @param string captcha answer134 * @param string captcha response 135 135 * @return boolean 136 136 */ 137 public static function valid($ answer)137 public static function valid($response) 138 138 { 139 return (strtoupper($ answer) === strtoupper(Session::instance()->get('captcha_answer')));139 return (strtoupper($response) === strtoupper(Session::instance()->get('captcha_response'))); 140 140 } 141 141 -
trunk/system/libraries/drivers/Captcha/Alpha.php
r3030 r3031 1 1 <?php defined('SYSPATH') or die('No direct script access.'); 2 2 /** 3 * Captcha driver for " basic" style.3 * Captcha driver for "alpha" style. 4 4 * 5 5 * $Id$ … … 39 39 $this->image_gradient($color1, $color2); 40 40 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 41 49 // 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)); 44 52 45 53 // Background alphabetic character attributes … … 48 56 49 57 // 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++) 51 59 { 52 60 $angle = mt_rand(-40, 20); 53 61 // Scale the character size on image height 54 62 $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]); 56 64 57 65 // Calculate character starting coordinates … … 64 72 65 73 // 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]); 67 75 68 76 // 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); 73 80 } 74 81 -
trunk/system/libraries/drivers/Captcha/Basic.php
r3028 r3031 40 40 41 41 // 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++) 43 43 { 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)); 45 45 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); 46 46 } 47 47 48 48 // 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)); 51 51 52 52 // 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++) 54 54 { 55 55 // Allocate random color, size and rotation attributes to text … … 59 59 // Scale the character size on image height 60 60 $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]); 62 62 63 63 // Calculate character starting coordinates … … 66 66 67 67 // 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]); 69 69 } 70 70
