_reg_id = md5(uniqid("")); $this->_use_captcha = function_exists('imagecreate'); } //-------------------------------------------- // Create the captcha image itself. It is a JPEG. //-------------------------------------------- function createCaptcha(&$reg, &$req) { //-------------------------------------------- // Make sure that something is actually using this // captcha. //-------------------------------------------- if(!$this->captchaIsRegistered($reg, $req) || !$this->_use_captcha) { header("HTTP/1.0 404 Not Found"); exit(); } //-------------------------------------------- // Unregister this captcha. //-------------------------------------------- $this->unregisterCaptcha($reg); //-------------------------------------------- // Deal with the font path. //-------------------------------------------- putenv('GDFONTPATH='. realpath(K4_BASE_DIR .'/fonts/')); //-------------------------------------------- // Get the session. //-------------------------------------------- $session = &$reg->get('session'); //-------------------------------------------- // Do all of the headers. //-------------------------------------------- header("Content-type: image/jpeg"); header(base64_decode("WC1DYXB0Y2hhOiBPbmVMb2JieSBodHRwOi8vd3d3Lm9uZWxvYmJ5LmNvbQ==")); header("Expires: Mon, 23 Jul 1993 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); //-------------------------------------------- // Create the base image 130x40 px. //-------------------------------------------- $create_fn = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate'; $final_image = $create_fn(95, 40); //-------------------------------------------- // Add some nice background junk. //-------------------------------------------- $background = imagecolorallocate($final_image, 255, 255, 255); imagefill($final_image, 0, 0, $background); imagecolordeallocate($final_image, $background); unset($background); //-------------------------------------------- // Figure out the code. //-------------------------------------------- $code = strtoupper(substr(md5(uniqid("")), 0, $this->_captcha_length)); //-------------------------------------------- // Add the code the session. //-------------------------------------------- $session->set('captcha_code', k4_hash($code)); //-------------------------------------------- // Add the code in. //-------------------------------------------- // stuff for the letter itself $letter_width = 14; $letter_height = 20; for($i = 0; $i < $this->_captcha_length; $i++) { // stuff for the letter itself $letter_text = $code{$i}; // positioning $layer_x = ($i * $letter_width); $layer_y = 32 + rand(-7, 7); $font_color = imagecolorallocate($final_image, rand(0,150), rand(0,150), rand(0,150)); imagettftext($final_image, 20, rand(-5, 5), $layer_x, $layer_y, $font_color, $this->_font, $letter_text); imagecolordeallocate($final_image, $font_color); } unset($font_color); //-------------------------------------------- // Interlace the image for better output in the // browser. //-------------------------------------------- imageinterlace($final_image, 1); //-------------------------------------------- // Output the final image in very low quality. //-------------------------------------------- imagejpeg($final_image, FALSE, 15); //-------------------------------------------- // Clean up the memory //-------------------------------------------- imagedestroy($final_image); unset($final_image); } //-------------------------------------------- // Create all the HTML needed to use this captcha // so that we can just plunk in {$captcha_img}, // etc. where it's stylish and everything // will work. //-------------------------------------------- function drawCaptcha(&$reg, &$req, &$resp) { //-------------------------------------------- // If a captcha has already been passed, we don't // need to display any others. //-------------------------------------------- if($this->passedAlready($reg)) { return; } //-------------------------------------------- // Register the captcha. //-------------------------------------------- $this->registerCaptcha($reg); //-------------------------------------------- // Path to the image. //-------------------------------------------- $url = k4_url('/captcha/'. $this->_reg_id .'/'); //-------------------------------------------- // Set the html to the templates. //-------------------------------------------- $resp->assign('captcha_img', '
 
'); $resp->assign('captcha_length', $this->_captcha_length); $resp->assign('captcha_name', 'captcha_input'); $resp->assign('use_captcha', intval($this->_use_captcha)); } //-------------------------------------------- // Validate a the captcha image with the session. //-------------------------------------------- function isValid(&$reg, &$req) { $session = &$reg->get('session'); //-------------------------------------------- // If we can't use captcha's then set the valid to // default to TRUE. //-------------------------------------------- $valid = !$this->_use_captcha ? TRUE : FALSE; //-------------------------------------------- // Validate the request variable against the // session variable. //-------------------------------------------- if($session->get('captcha_code')) { if(k4_hash($req->get('captcha_input')) == $session->get('captcha_code')) { $valid = TRUE; $session->set('captcha_passed', $valid); } } //-------------------------------------------- // Reset the value to nothing, which means that this // function can and should only be used once. //-------------------------------------------- $session->set('captcha_code', $null = NULL); //-------------------------------------------- // So... is it valid? //-------------------------------------------- return $valid; } //-------------------------------------------- // Register a captcha. //-------------------------------------------- function registerCaptcha(&$reg) { $session = &$reg->get('session'); $session->set('captcha_registered', $this->_reg_id); } //-------------------------------------------- // Unregister a captcha. //-------------------------------------------- function unregisterCaptcha(&$reg) { $session = &$reg->get('session'); $session->set('captcha_registered', $false = FALSE); } //-------------------------------------------- // Check to see if the captcha is in use, otherwise // we don't want to be able to see the image. //-------------------------------------------- function captchaIsRegistered(&$reg, &$req) { $session = &$reg->get('session'); $sess_reg_id = $session->get('captcha_registered') ? $session->get('captcha_registered') : FALSE; $req_reg_id = $req->get('reg_id') ? $req->get('reg_id') : FALSE; return ($sess_reg_id && $req_reg_id && $sess_reg_id == $req_reg_id); } //-------------------------------------------- // Check to see if a captcha has been passed already. //-------------------------------------------- function passedAlready(&$reg) { $session = &$reg->get('session'); $passed = FALSE; if($session->get('captcha_passed')) { $passed = TRUE; } return $passed; } } ?>