<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Login extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	function __construct()
	{
		parent::__construct();
		$this->load_lang('adminsys/login');
	}
	
	public function act()
	{
		//var_dump($this);
		//缓存一天 //60 *  60 * 24 缓存时间 一天
		//$this -> view -> caching = true;
		//$this -> view -> cache_lifetime = 86400 ; 
		
		//$this -> view -> assign('copyright',COPYRIGHT);	//版权
		
		//var_dump($_SESSION['session_id']);
		//exit();
		
		$this -> view -> assign('act_url',"./index.php?load=login&act=act_login");
		$this -> view -> display('login.tpl');

	}
	
	public function get_img_code()
	{
		$this -> load_lib('imgcode'); //加载辅助类
		$this -> imgcode_lib -> build_img_code();
	}
	
	private function check_img_code_time()
	{
		if (isset($_SESSION["img_code"]['time']) && $_SESSION["img_code"]['time'] + 30 < time()) {
			unset($_SESSION['img_code']);
			return false;
		}elseif(!isset($_SESSION["img_code"]['time'])){
			unset($_SESSION['img_code']);
			return false;
		}
		return true;
	}

 
	
	public function act_login(  )
	{
		$this -> load_db();
		$this -> load('helper'); //加载辅助类
		
		

		$adm_user 		= $this -> input -> post["adm_user"]; 
		$adm_pwd 		= $this -> input -> post["adm_pwd"]; 
		$adm_captcha 	= $this -> input -> post["adm_captcha"];
		
		//信息不能为空
		if($adm_user !='' && $adm_pwd !='' && $adm_captcha!=''){
			
			//验证码不能过期及是否正确
			if( $adm_captcha == $_SESSION["img_code"]['num'] && $this->check_img_code_time() )
			{
				//用户名不应该存在非法字符
				if(!$this -> input -> check_illegal_str("adm_user")){
						
						$admin_ip = $this -> helper -> get_ip();
						
						$this -> login_error_count( $admin_ip );
					
						$sql_query = " SELECT adm_id,adm_user,adm_email  FROM ".$this -> db -> prefix."admins WHERE adm_user= '".$adm_user."' AND  adm_pwd = '".md5($adm_pwd)."' and adm_enabled = 1  LIMIT 1 ";
						
						$admin_info["admin"] = $this -> db -> select_one($sql_query);
			
						if($admin_info["admin"]["adm_id"])
						{
							$this -> session -> set_session_arr( $admin_info );
			
							$now 	   =  time();
							
							//登录成功更新用户信息
							$sql_update = " UPDATE ".$this -> db -> prefix."admins  set  adm_login_count = adm_login_count +1 , adm_login_ip = '".$admin_ip."', adm_login_time = '".$now."'  WHERE  adm_user= '".$adm_user."'  and adm_id = ".$admin_info["admin"]["adm_id"]." " ;
							$this -> db -> query_sql($sql_update);
							
							//登录成功！重置IP错误信息清0！
							$updateip = "UPDATE `".$this -> db -> prefix."adm_login_error` SET adl_count =  0  WHERE  adl_ip = '".$admin_ip."' AND adl_type = 'login' ";
							
							$this -> db -> query_sql( $updateip );
							
							//写入IP记录
							$sql_insert = "INSERT INTO  `".$this -> db -> prefix."adm_login_log` (`adm_id` ,`adl_ip` ,`adl_time` ,`adl_type`)VALUES ( '".$admin_info["admin"]["adm_id"]."','".$admin_ip."','".$now."', 1)";
							$this -> db -> query_sql( $sql_insert );
							
							$this -> view -> assign('info_title',$this->lang['login_success']);
							$this -> view -> assign('URL_TIME',URL_TIME);
							$this -> view -> assign('get_url',ADMIN_WEBURL);
							$this -> view -> display('suggestion.tpl');
							
							//var_dump($_SESSION);
							
						}else{
						
							$this -> view -> assign('info_title',$this->lang['err_user_pwd']);
							$this -> view -> assign('URL_TIME',URL_TIME);
							$this -> view -> assign('get_url',ADMIN_WEBURL."/index.php?load=login&act=act");
							$this -> view -> display('suggestion.tpl');	
						}

				}else{
					$this -> view -> assign('info_title',$this->lang['err_user_str']);
					$this -> view -> assign('URL_TIME',URL_TIME);
					$this -> view -> assign('get_url',ADMIN_WEBURL."/index.php?load=login&act=act");
					$this -> view -> display('suggestion.tpl');
				}				
			}else{
				$this -> view -> assign('info_title',$this->lang['err_captcha']);
				$this -> view -> assign('URL_TIME',URL_TIME);
				$this -> view -> assign('get_url',ADMIN_WEBURL."/index.php?load=login&act=act");
				$this -> view -> display('suggestion.tpl');
				
			}
		
		}else{
				$this -> view -> assign('info_title',$this->lang['err_empty']);
				$this -> view -> assign('URL_TIME',URL_TIME);
				$this -> view -> assign('get_url',ADMIN_WEBURL."/index.php?load=login&act=act");
				$this -> view -> display('suggestion.tpl');
		}
 
	}
	
	private function login_error_count( $admin_ip = '')
	{
		
		
		//0000-00-00 00:00:00
		$now 	   =  time();
		$yesterday =  strtotime("-1 day");
		
		
		if( $admin_ip!='Unknown' and $admin_ip!='' )
		{
			//24小时内，查询记录是否存在 adm_session_id
			$sqlip = " SELECT adl_id , adl_session_id , adl_count , adl_start_time   FROM  ".$this -> db -> prefix."adm_login_error WHERE adl_ip = '".$admin_ip."' AND adl_type = 'login'  LIMIT 1 ";
			$admin_login_info = $this -> db -> select_one($sqlip);
			

			if($admin_login_info["adl_id"])
			{
				//登录超过一天。重置时间及数量 更新用户登录当天1次
				if( $admin_login_info["adl_start_time"] < $yesterday )
				{
					$update_login = "UPDATE `".$this -> db -> prefix."adm_login_error` SET `adl_session_id` = '".$_SESSION["session_id"]."' , adl_start_time ='".$now."' ,adl_count = '1'  WHERE  adl_ip = '".$admin_ip."' AND adl_type = 'login'   ";
					$this -> db -> query_sql( $update_login );

				}elseif( $admin_login_info["adl_count"] < 20 )
				{
						
					if( $admin_login_info["adl_start_time"] > ( $now - 3600)  &&  $admin_login_info["adl_count"] >= 5)
					{
						$updateip = "UPDATE `".$this -> db -> prefix."adm_login_error` SET adl_count = adl_count + 1  WHERE  adl_ip = '".$admin_ip."' AND adl_type = 'login' ";
						$this -> db -> query_sql($updateip);

						$this -> view -> assign('info_title',$this->lang['err_count']);
						$this -> view -> assign('URL_TIME',URL_TIME);
						$this -> view -> assign('get_url',ADMIN_WEBURL."/index.php?load=login&act=act");
						$this -> view -> display('suggestion.tpl');
						
					}else{
						
						$updateip = "UPDATE `".$this -> db -> prefix."adm_login_error` SET  adl_count = adl_count + 1 ,adl_session_id = '".$_SESSION["session_id"]."'   WHERE  adl_ip = '".$admin_ip."' AND adl_type = 'login' ";
						$this -> db -> query_sql($updateip);
					}
 	

				}else{

						$updateip = "UPDATE `".$this -> db -> prefix."adm_login_error` SET adl_count = adl_count + 1  WHERE  adl_ip = '".$admin_ip."' AND adl_type = 'login' ";
						$this -> db -> query_sql( $updateip );
						$this -> view -> assign('info_title',$this->lang['err_need_tomorrow']);
						$this -> view -> assign('URL_TIME',URL_TIME);
						$this -> view -> assign('get_url',ADMIN_WEBURL."/index.php?load=login&act=act");
						$this -> view -> display('suggestion.tpl');
					
				}
				
				//时间未超过1小时，状态有效，数量是否超过5次
				//未超过5次更新+1

			}else{
				$insertip ="INSERT INTO `".$this -> db -> prefix."adm_login_error` (`adl_session_id` , `adl_count` , `adl_ip` , `adl_start_time` , `adl_type` ) VALUES (  '".$_SESSION["session_id"]."', '1', '$admin_ip', '$now', 'login')";
				$this -> db -> query_sql($insertip);	
			}
			
		}else{
		
			$this -> view -> assign('info_title',$this->lang['err_no_ip']);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',ADMIN_WEBURL."/index.php?load=login&act=act");
			$this -> view -> display('suggestion.tpl');
			
		}
	
	}
	
	function find_pwd()
	{
		$this -> view -> assign('act_url',"./index.php?load=login&act=act_find_pwd");
		$this -> view -> display('find_pwd.tpl');	
	}
	
	
	function act_logout()
	{
		$this -> load('admin'); 
		$this -> admin -> drop_admin_session();
		header("location:".ADMIN_WEBURL."/index.php?load=login&act=act");
		exit();
	}
	
	function act_find_pwd()
	{
		exit('need_config_systeim!');
	}

}
