init($code, $this->width, $this->height, $phpbb_root_path . 'images/captcha/smilies'); $img = $capt->paint(); // Send image header('Content-Type: image/png'); header('Cache-control: no-cache, no-store'); imagepng($img); imagedestroy($img); } } /** * @package VC */ class yes_no_captcha { var $letter_img = array(); var $path; var $img; var $width = 0; var $height = 0; var $x_per_letter; /** * Init method * @param string $code The correct answer * @param int $width The number of pixels in the x direction * @param int $height The number of pixels in the y direction * @param string $path The path to load the images one. Needs to contain directories "correct" and "incorrect" with right respectively wrong pictures */ function init($code, $width, $height, $path) { global $phpbb_root_path, $phpEx; $this->width = $width; $this->height = $height; $this->path = $path; $this->img = imagecreatetruecolor($this->width, $this->height); if (!function_exists('filelist')) { include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); } $images = filelist($phpbb_root_path . $path); $code_len = strlen($code); $anticode_len = mt_rand($code_len - 1, $code_len + 1); $rand_str = md5(mt_rand(0, 2000000000)); $rand_str = str_replace('0', 'Z', strtoupper(base_convert($rand_str, 16, 35))); $anticode = substr($rand_str, 0, $anticode_len); $codes = array( 'correct' => $code, 'incorrect' => $anticode, ); $letters_correct = 0; $letters_incorrect = 0; while($letters_correct < strlen($codes['correct']) || $letters_incorrect < strlen($codes['incorrect'])) { $which = ($letters_correct < strlen($codes['correct']) && $letters_incorrect < strlen($codes['incorrect']) ? mt_rand(0,1) : ($letters_correct < strlen($codes['correct'])) ? 1 : 0); if ($which) { $name = 'correct'; $i = $letters_correct++; } else { $name = 'incorrect'; $i = $letters_incorrect++; } $this->letter_img[] = array( 'letter' => $codes[$name][$i], 'image' => $name . '/'. ($images[$name . '/'][mt_rand(0, count($images[$name . '/']) -1)]), ); } $this->x_per_letter = floor($this->width / count($this->letter_img)); } function paint() { $count = count($this->letter_img); for ($index = 0; $index < $count; $index ++) { $this->write_letter($index); } return $this->img; } function write_letter($index) { $imgfile = $this->path . '/' . $this->letter_img[$index]['image']; $imagedata = getimagesize($imgfile); switch ($imagedata[2]) { case 1 : $img = imagecreatefromgif($imgfile); break; case 2 : $img = imagecreatefromjpeg($imgfile); break; case 3 : $img = imagecreatefrompng($imgfile); break; default : return false; } $black = imagecolorallocate($this->img, 0, 0, 0); $img_rot = imagerotate($img, mt_rand(0, 30) - 15, $black); $position_x = $this->x_per_letter * $index; $color = imagecolorallocate($img_rot, 0, 10, 245); imagecopyresized($this->img, $img_rot, $position_x, 0, 0, 0, $this->x_per_letter, $this->height / 2, imagesx($img_rot), imagesy($img_rot)); imagestring($this->img, 5, $position_x + mt_rand(5,10), $this->height / 2 + mt_rand(5,10), $this->letter_img[$index]['letter'], $color); imagedestroy($img); imagedestroy($img_rot); } } ?>