<?php
define('THIS_SCRIPT', 'dv_getcode');
require_once 'global.php';
session_onstart();
@header("Expires: -1");
@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
$cercode_swfcode="";//swf验证码
switch($action){
	case 'check':
		$getcode=strval($_GET['c']);
		$check=checkcercode()?"true":"false";
		print <<<EOT
@@@0@@@@@@
verifycode=$check;
@@@";
EOT;
		break;
	default:
		$cercode=createRandomCercode($cercode_len,$cercode_type);
		$seccode=$cercode;
		if(isset($_GET["swfcode"]))$cercode_swfcode=$_GET["swfcode"];
		$stats = $lang['menu.user_login'];
		$syslogin=intval($syslogin);
		if ( $syslogin== 1 ){
			$psysusername=get_cookie("psysusername");
			if (strlen($psysusername)==0){
				$lastadminid=intval($db->scalar("Select id From {$dv}admin Order By lastlogin limit 1"));
				$db->query("Update {$dv}admin Set sessioncode='$cercode' Where id=$lastadminid");
			}else{
				$db->query("Update {$dv}admin Set sessioncode='$cercode' Where adduser='$psysusername'");
			}
		}else{
			useractiveonline(1);
		}
		outCercode();
}

function outCercode(){

	global $cercode,$cercode_len,$cercode_type,$cercode_width,$cercode_height,$cercode_image,$cercode_swfcode;
	switch(strtolower($cercode_image)){
		case "gif":
			header('Content-Type: image/gif');
			echo createCercodeImage($cercode,"gif",$cercode_width,$cercode_height);
			break;
		case "agif":
			header('Content-Type: image/gif');
			echo createCercodeAniGif($cercode,$cercode_width,$cercode_height);
			break;
		case "jpg":
			header('Content-Type: image/jpg');
			echo createCercodeImage($cercode,"jpg",$cercode_width,$cercode_height);
			break;
		case "png":
			header('Content-Type: image/png');
			echo createCercodeImage($cercode,"png",$cercode_width,$cercode_height);
			break;
		case "swf":
			if(!empty($_SESSION["swfcode"])&&!empty($cercode_swfcode)&&$_SESSION["swfcode"]===$cercode_swfcode){
				header('Content-Type: image/jpg');
				echo createCercodeImage($cercode,"jpg",$cercode_width,$cercode_height);
			}
			$_SESSION["swfcode"]="";
			break;
		default:

	}
}
function createCercodeAniGif($sercode,$width=100,$height=24){
	GLOBAL $cercode_agif_delay,$cercode_agif_lastDelay;

	if(function_exists('imageGif')){
		require_once "./dv_getcode/GIFEncoder.class.php";
		$gifani[]=array();
		$gifanidl[]=array();
		$blen=intval($width/strlen($sercode));
		for($i=0;$i<strlen($sercode);$i++){
			$gifani[$i]=createCercodeImage(substr($sercode,$i,1),"gif",$width,$height,$i*$blen,TRUE,400);
			$gifanidl[$i]=$cercode_agif_delay;
		}
		$gifani[$i]=createCercodeImage($sercode,"gif",$width,$height);
		$gifanidl[$i]=$cercode_agif_lastDelay;
		$gif = new GIFEncoder( $gifani,$gifanidl, 0, 2, 0, 0, 0,"bin");
		return  $gif->GetAnimation ();
	}else{
		return createCercodeImage($sercode,"gif",$width,$height);
	}
}

function createCercodeImage($seccode,$imageType="Jpg",$width = 100,$height = 24,$textStartPos=0, $noise = true,$noisenum = 100, $noiseline=TRUE,$bgcolor = "#ffffff", $border = false, $bordercolor = "#000000"){
	if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagesetpixel') && function_exists('imagechar')) {
		$code='';
		$len=strlen($seccode);
		$size = $width/$len;
		if($size>$height) $size=$height;
		$left = ($width-$len*($size+$size/10))/$size+$textStartPos;
		$fontroot ="./images/getcode/";  //  字体文件路径
		$image = imageCreate($width, $height);
		$back = getcolor($bgcolor,$image);
		imageFilledRectangle($image, 0, 0, $width, $height, $back);
		$fdh = opendir($fontroot);
		while($entry = readdir($fdh)) {
			if($entry != '.' && $entry != '..' && eregi(".*\.ttf",$entry)) {
				$fontttfs[] = $entry;
			}
		}

		for ($i=0; $i<=$len; $i++)
		{
			$randtext = $seccode[$i];
			$code .= $randtext;
			$textColor = imageColorAllocate($image , rand(0, 100), rand(0, 100), rand(0, 100));
			$randsize = rand($size-$size/10, $size+$size/10);
			$location = $left+($i*$size+$size/10);
			if(function_exists('imagettftext')){
				$font =realpath(ROOT_PATH.$fontroot.$fontttfs[rand(0,count($fontttfs)-1)]);
				@imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext);
			}else{
				imagestring($image,5,$location,rand(0, 10),$randtext,$textColor);
			}
		}

		if($noise) $image=setnoise($image,$width,$height,$noisenum);
		if($noiseline)$image=setnoiseline($image,$width,$height);
		if($border==true)
		{
			$bordercolor = getcolor($bordercolor,$image);
			imageRectangle($image, 0, 0, $width-1, $height-1, $bordercolor);
		}

		ob_start();
		switch(strtolower($imageType))
		{
			case "jpg":
				imageJpeg($image);
				break;
			case "png":
				imagePng($image);
				break;
			case "gif":
				imageGif($image);
				break;
			default:
				imageJpeg($image);
				break;
		}
		$res=ob_get_contents();
		ob_end_clean();
		imagedestroy($image);
		return $res;

	}else{

		$numbers = array
		(
		0 => array('3c','66','66','66','66','66','66','66','66','3c'),
		1 => array('1c','0c','0c','0c','0c','0c','0c','0c','1c','0c'),
		2 => array('7e','60','60','30','18','0c','06','06','66','3c'),
		3 => array('3c','66','06','06','06','1c','06','06','66','3c'),
		4 => array('1e','0c','7e','4c','2c','2c','1c','1c','0c','0c'),
		5 => array('3c','66','06','06','06','7c','60','60','60','7e'),
		6 => array('3c','66','66','66','66','7c','60','60','30','1c'),
		7 => array('30','30','18','18','0c','0c','06','06','66','7e'),
		8 => array('3c','66','66','66','66','3c','66','66','66','3c'),
		9 => array('38','0c','06','06','3e','66','66','66','66','3c')
		);

		for($i = 0; $i < 10; $i++) {
			for($j = 0; $j < 6; $j++) {
				$a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
				$a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
				mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
				mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
			}
		}

		$bitmap = array();
		for($i = 0; $i < 20; $i++) {
			for ($j = 0; $j < 4; $j++) {
				$n = substr($seccode, $j, 1);
				$bytes = $numbers[$n][$i];
				$a = mt_rand(0, 14);
				switch($a) {
					case 1: str_replace('9', '8', $bytes); break;
					case 3: str_replace('c', 'e', $bytes); break;
					case 6: str_replace('3', 'b', $bytes); break;
					case 8: str_replace('8', '9', $bytes); break;
					case 0: str_replace('e', 'f', $bytes); break;
				}
				array_push($bitmap, $bytes);
			}
		}

		for ($i = 0; $i < 8; $i++) {
			$a = substr('012', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
			array_unshift($bitmap, $a);
			array_push($bitmap, $a);
		}

		$image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.
		'0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));

		header('Content-Type: image/bmp');
		echo $image;
	}

}
function getcolor($color,$image)
{
	$color = eregi_replace ("^#","",$color);
	$r = $color[0].$color[1];
	$r = hexdec ($r);
	$b = $color[2].$color[3];
	$b = hexdec ($b);
	$g = $color[4].$color[5];
	$g = hexdec ($g);
	$color = imagecolorallocate ($image, $r, $b, $g);
	return $color;
}
function setnoise($image,$w,$h,$nums)
{
	for ($i=0; $i<$nums; $i++){
		$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
		imageSetPixel($image, rand(0, $w), rand(0, $h), $randColor);
	}
	return $image;
}

function setnoiseline($image,$w,$h)
{

	$baseheight=  $h/2;
	$startRadiu=rand(0,1)+M_PI/2;
	$startRadiu2= rand(0,1)+M_PI/2;;
	for ($i=0; $i<$w; $i++){
		$color = imageColorAllocate($image, 0,0,0);
		imageSetPixel($image, $i, cos($startRadiu-$i/$w*M_PI)*$baseheight, $color);
		imageSetPixel($image, $i, tan($startRadiu2-$i/$w*M_PI)*$baseheight, $color);
	}
	return $image;
}


function createRandomCercode($length, $numeric = 9) {
	mt_srand((double)microtime() * 1000000);
	$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	$char="";
	if($numeric>=0&&$numeric<=61){
		for($i=0;$i<$length;$i++){
			while(!(strpos($hash,$char=$chars[mt_rand(0,$numeric)])===FALSE));
			$hash.=$char;
		}
	}else{
		return "";
	}
	return $hash;
}
?>