<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

class Captcha {
	
	var $hash;
	var $length = 5;
	var $type;
	var $noise = 100;

	function captcha() {
		if(function_exists('imagecreate')) $this->type='g';
		else $this->type='t';
		$this->type='g';
	}
	
	function create_captcha() {

		$this->hash = md5(time().rand(0, 10000));

		$captchastring='';
		
		if($this->type=='g') {

			$imgziel = imagecreatetruecolor(($this->length*15), 15);
			$bgcolor = ImageColorAllocate($imgziel, 255, 255, 255);
			$fontcolor = imagecolorallocate($imgziel, 0, 0, 0);
			$xziel = imagesx($imgziel); //Ermittlung der Bildbreite
			$yziel = imagesy($imgziel); //Ermittlung der Bildhöhe
			ImageFilledRectangle($imgziel, 0, 0, $xziel, $yziel, $bgcolor);

			for($i=0;$i<$this->length;$i++) {
				$int=rand(0,9);
				$captchastring.=$int;
				imagestring($imgziel, rand(2,5), $i*15, 0, $int, $fontcolor);
			}		
			
			for($i=0;$i<$this->noise;$i++) {
				imagesetpixel($imgziel, rand(0,$xziel), rand(0,$yziel), $fontcolor);
			}
			imageJPEG($imgziel, 'tmp/'.$this->hash.'.jpg');
			$captcha = '<img src="tmp/'.$this->hash.'.jpg" border="0" alt="Security Code">';
			
		} elseif($this->type=='t') {
			
			for($i=0;$i<$this->length;$i++) {
				$captcha .= rand(0,9);			
			}	
			$captchastring=$captcha;
		}
		safe_query("INSERT INTO `".PREFIX."captcha` (`hash`, `captcha`, `deltime`) VALUES ('".$this->hash."', '".$captchastring."', '".(time()+(5*60))."');");
		return $captcha;		
	}
	
	function get_hash() {
		
		return $this->hash;
		
	}
	
	function check_captcha($input,$hash) {
		if(mysql_num_rows(safe_query("SELECT hash FROM `".PREFIX."captcha` WHERE captcha='".$input."' AND hash='".$hash."'"))) {
			safe_query("DELETE FROM `".PREFIX."captcha` WHERE captcha='".$input."' AND hash='".$hash."'");
			$file='tmp/'.$hash.'.jpg';
			if(file_exists($file)) @unlink($file);
			return true;
		}
		else return false;
	}
	
	function clear_oldcaptcha() {
		$ergebnis=safe_query("SELECT hash FROM `".PREFIX."captcha` WHERE deltime<".time());
		while($ds=mysql_fetch_array($ergebnis)) {
			$file='tmp/'.$ds['hash'].'.jpg';
			if(file_exists($file)) @unlink($file);
		}	
		safe_query("DELETE FROM `".PREFIX."captcha` WHERE deltime<".time());
	}
}
?>