<?php
require dirname(__FILE__).'/include/common.inc.php';
$session = new hs_session();

$enablegd = 1;
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
	if(!function_exists($func)) 
	{
		$enablegd = 0;
		break;
	}
}

if($enablegd)
{

	$consts = 'ABCDEFGHJKLMNPQRTUVWXY';
	$vowels = '379KLMNPQR';
	for ($x = 0; $x < 4; $x++)
	{
		$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
		$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
	}
	$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
	$_SESSION['checkcode'] = $string = substr($radomstring,0,4); 
  $imageX = 80;	
	$imageY = 30;	
	$im = imagecreatetruecolor($imageX,$imageY);

	$background = imagecolorallocate($im, 255, 255, 255);
  $font = "CanGoods.ttf";
	$foregroundArr1 =imagecolorallocate($im, rand(0, 255), rand(0, 200), rand(0, 220));
	$foregroundArr2 =imagecolorallocate($im, rand(0, 255), rand(0, 200), rand(0, 220));
	$foregroundArr3 =imagecolorallocate($im, rand(0, 255), rand(0, 200), rand(0, 220));
	$foregroundArr4 =imagecolorallocate($im, rand(0, 255), rand(0, 200), rand(0, 220));
	



	imagefill($im, 0, 0, imagecolorallocate($im, 255, 255, 255));

	imagettftext($im,16,rand(-30,30),7,23,$foregroundArr1,$font,$string[0]);
	imagettftext($im,16,rand(-30,30),22,23,$foregroundArr2,$font,$string[1]);
	imagettftext($im,16,rand(-30,30),41,23,$foregroundArr3,$font,$string[2]);
	imagettftext($im,16,rand(-30,30),56,23,$foregroundArr4,$font,$string[3]);
/*
	$border = imagecolorallocate($im, 255, 255, 255);
	imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
	for($i=0;$i<10;$i++)
	{
	$color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
	imageline($im,rand(0,200),rand(0,200),rand(0,120),rand(0,120),$color);
  }
*/
  header("content-type:image/png\r\n");
	imagepng($im);
	imagedestroy($im);
}
else
{
	$files = glob(HS_ROOT.'/images/checkcode/*.jpg');
	if(!is_array($files)) exit($LANG['please_check_dir_images_checkcode']);

	$checkcodefile = $files[rand(0, count($files)-1)];
	$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4);

	header("content-type:image/jpeg\r\n");
	include $checkcodefile;
}
?>