_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', '