Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
captcha.class.php
Go to the documentation of this file.
00001 <?php
00021 class captcha {
00022 
00026         private $_name;
00027 
00031         private $_width;
00032         
00036         private $_height;
00037 
00041         private $_font_file;
00042         
00046         private $_letters;
00047 
00051         private $_numbers;
00052 
00056         private $_allow_numbers;
00057 
00067         function __construct($name, $opts=null) {
00068 
00069                 $this->_name = $name;
00070                 $this->_width = 200;
00071                 $this->_height = 40;
00072 
00073                 $this->_font_file = ABS_ROOT.DS.'font'.DS.'initial.ttf';
00074 
00075                 $this->_letters = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
00076                 $this->_numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
00077 
00078                 $this->_allow_numbers = gOpt($opts, 'allow_numbers', false);;
00079         
00080         }
00081 
00091         public function render($opts=null) {
00092         
00093                 $bkg_color = gOpt($opts, "bkg_color", "#00ff00");
00094                 $color = gOpt($opts, "color", "#000000");
00095 
00096                 $image = ImageCreatetruecolor($this->_width, $this->_height);
00097 
00098                 // background
00099                 $bcs = $this->hex2RGB($bkg_color);
00100                 $bkg_c = ImageColorAllocate($image, $bcs['red'], $bcs['green'], $bcs['blue']);
00101                 imagefill($image, 0, 0, $bkg_c);
00102 
00103                 // text
00104                 $tcs = $this->hex2RGB($color);
00105                 $text_c = ImageColorAllocate($image, $tcs['red'], $tcs['green'], $tcs['blue']);
00106                 list($s1, $s2) = $this->generateStrings();
00107                 imagettftext($image, 22, 2, 5, $this->_height-5, ImageColorAllocate($image, 0, 0, 0), $this->_font_file, $s1);
00108                 imagettftext($image, 22, -5, 110, 25, ImageColorAllocate($image, 0, 0, 0), $this->_font_file, $s2);
00109 
00110                 // ellipses
00111                 $i = 0;
00112                 while($i<$this->_width-5) {
00113                         $ell_color = ImageColorAllocate($image, rand(0,255), rand(0,255), rand(0,255));
00114                         imagefilledellipse($image , $i+5 , rand(5, $this->_height-5) , rand(0, 8) , rand(0, 8), $ell_color);
00115                         $i = $i+20;
00116                 }
00117 
00118                 // lines
00119                 $i = 0;
00120                 while($i<$this->_width-3) {
00121                         $line_color = ImageColorAllocate($image, rand(0,255), rand(0,255), rand(0,255));
00122                         imagesetthickness($image, rand(0,2));
00123                         imageline($image, $i , rand(0, $this->_height) , rand($i, $i+20) , rand(0, $this->_height) , $line_color);
00124                         $i = $i+10;
00125                 }
00126 
00127                 ob_start();
00128                 imagejpeg($image);
00129                 imagedestroy($image);
00130                 $img = ob_get_contents();
00131                 ob_end_clean();
00132 
00133                 $_SESSION['captcha_code'] = $s1.$s2;
00134 
00135                 $buffer = "<img src=\"data:image/jpeg;base64,".base64_encode($img)."\" /><br />";
00136                 $buffer .= "<input name=\"$this->_name\" type=\"text\" size=\"10\" maxlength=\"20\" required=\"required\" />";
00137 
00138                 return $buffer;
00139 
00140         }
00141 
00147         private function generateStrings() {
00148         
00149                 $s1 = $s2 = '';
00150 
00151                 $coin = rand(0,10) < 5 ? true : false;
00152                 
00153                 $ls1 = $coin ? 5 : 4;
00154                 $ls2 = $coin ? 4 : 5;
00155 
00156                 for($i=0; $i<$ls1; $i++) { 
00157                         $coin = $this->_allow_numbers ? rand(0, 10) : 0;
00158                         $s1 .= $coin < 5 ? $this->_letters[array_rand($this->_letters)] : $this->_numbers[array_rand($this->_numbers)];
00159                 }
00160 
00161                 for($i=0; $i<$ls2; $i++) { 
00162                         $coin = $this->_allow_numbers ? rand(0, 10) : 0;
00163                         $s2 .= $coin < 5 ? $this->_letters[array_rand($this->_letters)] : $this->_numbers[array_rand($this->_numbers)];
00164                 }
00165 
00166                 return array($s1, $s2);
00167 
00168         }
00169 
00176         private function hex2RGB($hexStr) {
00177     
00178                 $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
00179                 $rgbArray = array();
00180     
00181                 if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
00182                         $colorVal = hexdec($hexStr);
00183                         $rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
00184                         $rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
00185                         $rgbArray['blue'] = 0xFF & $colorVal;
00186                 } 
00187                 elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
00188                         $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
00189                         $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
00190                         $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
00191                 } 
00192                 else {
00193                         return false; //Invalid hex color code
00194     
00195                 }
00196     
00197                 return $rgbArray; // returns the rgb string or the associative array
00198         }
00199 
00205         public function check() {
00206         
00207                 $string = preg_replace("#[ \s]#", "", $_POST[$this->_name]);
00208 
00209                 if(!$string) return false;
00210 
00211                 $result = $string === $_SESSION['captcha_code'] ? true : false;
00212 
00213                 unset($_SESSION['captcha_code']);
00214 
00215                 return $result;
00216 
00217         } 
00218 
00219 }
00220 
00221 ?>