<?php

class captcha extends engine{

	function TuringGenerateNumber() {
		/*//generate word
			$length = 5;
			$conso=array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z"); 
		$vocal=array("a","e","i","o","u"); 
		$numbers=array(2,3,4,5,6,7,8,9); 
		
		  $max = $length/2; 
		  for($i=1; $i<=$length; $i++) { 
			$password.=$conso[rand(0,19)];
			$i++; 
			if ($i<=$length) $password.=$vocal[rand(0,4)];
		  }
		
		  $_SESSION['TuringNumber'] = $password; 
			*/
		//generate number
		for($i=0; $i < 4; $i++) {
			$number .= rand(0,9);
		}
		$_SESSION['TuringNumber'] = $number;
		
		return true;
		
	}

	function TuringNumberValidate($TuringNumber) {
		
		if ($_SESSION['TuringNumber'] == $TuringNumber) return true;
		else return false;
		
	}

	function TuringImageGenerate($colorProfile) {
			
		TuringGenerateNumber();	
	
		$display_number = "".$_SESSION['TuringNumber']."";	
		
		ob_clean();

		// Image should expire immediately 
		header('Content-type:image/png'); 
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
		header('Cache-Control: no-store, no-cache, must-revalidate'); 
		header('Cache-Control: post-check=0, pre-check=0', false); 

		// Variables 
		////////////////////////////////////////////////////////////////////////// 
		$x = 100;                                     // Image width 
		$y = 22;                                     // Image height 
		$freq=1;                                    // Number of noise dots 
		$noise_method="new";                        // line | dots | rectangle 
		$font_selection = "fixed";                    // random | fixed 
		$font_folder = "";        // Path to fonts folder 
		$fonts = array("gothicb.ttf"); 
		$default_font = 0;        // Array index of default font in $fonts array. 
		$angle_selection = "random";                // random | fixed 
		$max_angle = 8;                            // Max angle 
		$default_angle = 8;                            // Default angle. 
		$font_size = rand(12, 14);                            // Font size in points 
		////////////////////////////////////////////////////////////////////////// 
		
		for($i=0; $i < strlen($display_number); $i++) {
			$number .= $display_number[$i]." ";
		}
		
		// Set font 
		if ($font_selection=="random") { 
			$font = rand(0, count($fonts)); 
			$font = $font_folder.$fonts[$font]; 
		} else { 
			$font = $font_folder.$fonts[$default_font]; 
		} 
		
		// Set Text Angle 
		if ($angle_selection=="random") { 
			$angle = rand((-1)*($max_angle/2), ($max_angle/2)); 
		} else { 
			$angle = $default_angle; 
		} 
		
		// Create image with specified size. 
		$img = @ImageCreate($x, $y) or die("Couldn't create image"); 
		
		// Allocate colors 
		if ($colorProfile==1) {
			$black = ImageColorAllocate($img, 110, 110, 110); 
			$white = ImageColorAllocate($img, 244, 244, 244); 
			$lines = ImageColorAllocate($img, 80, 80, 80); 
		}
		else {
			$black = ImageColorAllocate($img, 255, 255, 255); 
			$white = ImageColorAllocate($img, 60, 60, 60); 
			$lines = ImageColorAllocate($img, 205, 205, 205); 
		}
		// Stop execution if any error occured before 
		
		if (isset($error_msg)) { 
			//Fill image background with white 
			ImageFill ($img, 100, 50, $white); 
			//Display error 
			ImageString($img, 2, 5, 5, $error_msg, $black); 
		} else { 
			//Fill image with background color 
			ImageFill ($img, 70, 20, $white) OR die("Couldn't create image"); 
			// Add centered text 
			$arr=ImageTtfbBox($font_size, $angle, $font, $number); 
		
			$text_x= round(($x-(abs($arr[2]-$arr[0]))) / 2, 0); 
			$text_y= round(($y-(abs($arr[5]-$arr[3]))) / 2, 0); 
			ImageTTFText($img, $font_size, $angle, $text_x, $text_y - $arr[5], $black, $font, $number); 
			
		
			$i=0;        //<---------Noise Counter 
		
			// Add Noise Points 
			while ($i < $freq) { 
				$dotX = rand(0, $x); $dotY = rand(0, $y);
				$rndplus = rand(0, 8); 
				switch ($noise_method) { 
				case "line": 
					$line_width = rand(8,60); 
					if (rand(0,10)>=5) { 
						// Draw horizontal line 
						ImageLine($img, $dotX+$rndplus, $dotY+$rndplus, $dotX+$line_width, $dotY, $black); 
						//ImageLine($img, $dotX, $dotY, $dotX+$line_width, $dotY, $dots_color); 
					} else { 
						// Draw vertical line 
						ImageLine($img, $dotX+$rndplus, $dotY+$rndplus, $dotX, $dotY+$line_width, $black); 
					} 
					break; 
				case "dots": 
					ImageSetPixel($img, $dotX, $dotY, $dots_color); 
					break; 
				case "rectangle": 
					ImageRectangle($img, $dotX-1, $dotY-1, $dotX+1, $dotY+1, $black); 
					break;
			   case "new": 
					for($xs=1; $xs < 150; $xs=$xs+25) {   
						ImageEllipse($img, 0, 0, $dotX+$xs, $dotY+$xs, $lines);
					}
					for($xs=1; $xs < 150; $xs=$xs+25) {   
						ImageEllipse($img, 30, 0, $dotX+$xs, $dotY+$xs, $lines);
					}
					
							break;
				} 
				$i++; 
			}  
		} 
		
		//Finalize the image. Free memory 
		
		ImagePNG($img);
		ImageDestroy($img);
	}
}

?>