<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：cls.chk_code.php
 * 摘    要：验证码类
 * 适用范围：PHPWMS 1.0 需要GD库支持
 * 版    本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
	class CheckCode{
		private $img_w    = '70';
		private $img_h    = '20';
		private $rnd_len  = '4';
		private $rnd_type = 1; // 生成验证码的类型,1为数字字母混合,2为纯数字
		private $img_bgcolor     = '#ffffff';
		private $img_bordercolor = '#6699CC';
		private $img_border      = true;
		private $img_noisenum    = 30;
		private $img_noise       = true;
		public function __construct($img_w,$img_h,$len,$type,$noisenum = 30,$bgcolor = '#ffffff',$bordercolor = '#6699CC') {
			$this -> img_w           = $img_w;
			$this -> img_h           = $img_h;
			$this -> rnd_len         = $len;
			$this -> rnd_type        = $type;
			$this -> img_bgcolor     = $bgcolor;
			$this -> img_bordercolor = $bordercolor;
			$this -> img_noisenum    = $noisenum;
		}

		public function createCheckImg() {
			$image = imageCreate($this -> img_w,$this -> img_h);
			$back = $this -> getColor($this -> img_bgcolor,$image);
			imageFilledRectangle($image,0,0,$this -> img_w,$this -> img_h,$back);
			$size = $this -> img_w/$this -> rnd_len;
			if($size > $this -> img_h) $size = $this -> img_h;
			$left = ($this -> img_w - $this -> rnd_len * ($size + $size / 10)) / $size;
			$code = '';
			if($this -> rnd_type == 1) {
				for ($i=0; $i<$this -> rnd_len; $i++) {
					$randtext = rand(1,9);
					$crd = rand(1,9);
					$code1 = substr(md5($randtext),$crd,1);
					$code .= $code1;
					$textColor = imagecolorallocate($image,rand(0,150),rand(0,150),rand(0,150));
					$randsize = rand($size - $size / 10,$size + $size / 10);
					$location = $left + ($i * $size + $size / 10) + mt_rand(3,5);
					imagestring($image,5,$location,1,$code1,$textColor);
				}
			}
			if($this -> rnd_type == 2) {
				for ($i=0; $i<$this -> rnd_len; $i++) {
					$randtext = rand(1,9);
					$code .= $randtext;
					$textColor = imagecolorallocate($image,rand(0,150),rand(0,150), rand(0,150));
					$randsize = rand($size - $size / 10,$size + $size / 10);
					$location = $left + ($i * $size + $size / 10) + mt_rand(3,5);
					imagestring($image,5,$location,1,$randtext,$textColor);
				}
			}
			$this -> img_noise == true and $this -> setNoise($image);
			//$_SESSION['chk_code'] = $code;
			setcookie('chk_code',base64_encode(trim(strtolower($code))),time()+500,'/');
			$bordercolor = $this -> getColor($this -> img_bordercolor,$image); 
			if($this -> img_border == true) {
				imageRectangle($image,0,0,$this -> img_w - 1,$this -> img_h - 1,$bordercolor);
			}
			header("Content-type: image/png");
			imagepng($image);
			imagedestroy($image);
		}

		private function setNoise($image) {
				for ($i=0; $i<$this -> img_noisenum; $i++){
					$randColor = imageColorAllocate($image,rand(0,255),rand(0,255), rand(0,255));  
					imageSetPixel($image,rand(0,$this -> img_w),rand(0,$this -> img_h),$randColor);
				} 
		}

		private 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 check($string){
			if(empty($string))return FALSE;
			$o = trim($_COOKIE['chk_code']);
			$string = base64_encode(trim(strtolower($string)));
			return $o==$string?TRUE:false;
		}
	}
?>