<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (isset($_GET['name']) AND !eregi("[^0-9a-zA-Z_-]",$_GET['name'])) {
	$session_name = @$_GET['name'];
	
	header("Content-type: image/png");
	
	$width = 180;
	$height = 50;
	
	$im = @imagecreate($width,$height);
	$bg = imagecolorallocate($im,255,255,255); //Background
	$colorG = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
	$grey = imagecolorallocate($im,180,180,180);
	
	$rx = 0;
	
	//Common bg
	for ($a=0;$a<=5;$a++) {
		$rx += 35;
		$x = $rx+rand(-10,5);
		if ($x>145) { $x = 145; }
		$y = rand(30,60);
		$color = imagecolorallocate($im,rand(100,200),rand(100,200),rand(100,200));
		
		imageline($im,rand(0,179),rand(0,49),rand(0,179),rand(0,49),$color);
		imageline($im,rand(0,179),rand(0,49),rand(0,179),rand(0,49),$color);
		imageline($im,rand(0,179),rand(0,49),rand(0,179),rand(0,49),$color);
	}
	
	$gdinfo = @gd_info();
	if ($gdinfo['FreeType Support']) {
		$chars = array();
		for ($i=1;$i<=5;$i++) { $chars[] = randStr(1); }
		//Bg
		$rx = -20;
		foreach ($chars as $ch) {
			$rx += 35;
			$x = $rx+rand(-10,5);
			if ($x>145) { $x = 145; }
			$y = rand(30,60);
			$size = rand(20,70);
			$angle = rand(-20,20);
			$font = "inc/fonts/1.ttf";
			
			imagettftext($im,$size+rand(-20,20),$angle+rand(-20,20),$x+rand(-20,20),$y+rand(-20,20),$colorG,$font,$ch);
		}
		//Text
		$chars = array();
		for ($i=1;$i<=5;$i++) { $chars[] = randStr(1); }
		$text = "";
		$rx = -20;
		foreach ($chars as $ch) {
			$rx += 35;
			$x = $rx+rand(-10,5);
			if ($x>145) { $x = 145; }
			$y = 45;
			$size = rand(30,50);
			$angle = rand(-20,20);
			$text .= $ch;
			$font = "inc/fonts/1.ttf";
			$color = imagecolorallocate($im,rand(0,200),rand(0,200),rand(0,200));
			
			imagettftext($im,$size,$angle,$x,$y,$color,$font,$ch);
		}
	} else {				
		//Text
		$chars = array();
		for ($i=1;$i<=5;$i++) { $chars[] = randStr(1); }
		$text = "";
		$rx = -20;
		foreach ($chars as $ch) {
			$rx += 35;
			$x = $rx+rand(-10,5);
			if ($x>170) { $x = 170; }
			$y = rand(1,25);
			$text .= $ch;
			$color = imagecolorallocate($im,rand(0,200),rand(0,200),rand(0,200));
			
			imagestring($im,5,$x-1,$y-1,$ch,$white);
			imagestring($im,5,$x+1,$y+1,$ch,$white);
			imagestring($im,5,$x,$y,$ch,$color);
		}
		imagestring($im, 3, 66, 36,"FailSafe CAPTCHA", $bg);
		imagestring($im, 3, 67, 37,"FailSafe CAPTCHA", $grey);
	}
	imagerectangle($im,0,0,179,49,$grey);
	//---------------------------------------------
	@session_start();
	$_SESSION[$session_name] = $text;
	//---------------------------------------------
	imagepng($im);
	imagedestroy($im);
}

function randStr($length) {
	$key = "";
	$pattern = "23456789AbCdEFgHikLmNPqRsTuVwXyZ";
	for($i=0;$i<$length;$i++) {
		$key .= $pattern{rand(0,31)};
	}
	return $key;
}

?>