<?php
class Login
{
	function Login()
	{
		$getmenu = $GLOBALS['Helpe']->get_menu();
		$GLOBALS['Templ']->assign('getmenu',$getmenu);
	}
	
	function act()
	{

		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24 缓存时间 一天
		
		$webtitle = '免费注册';
		
		//处理你现在的位置
		$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/user/login.php?load=login&act=act">登录</a>';


		$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
		$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);
		
		$GLOBALS['Templ'] -> assign('copyright',COPYRIGHT);	 //版权
		
		$GLOBALS['Templ'] -> assign('actUrl',BASE_WEBURL."/user/login.php?load=login&act=actlogin");
		
		$GLOBALS['Templ'] -> display('login_tpl.html');
		
	}
	
	function loginuser()
	{
		$use_nameval 		= $GLOBALS['Reque'] -> funpost("use_name");
		$use_pwdval 		= $GLOBALS['Reque'] -> funpost("use_pwd");
		$use_codeval 		= $GLOBALS['Reque'] -> funpost("use_code");
		
		
		if($use_codeval!=$_SESSION["Img"])
		{
				echo $GLOBALS['_LANG']["codeerror"];
				
		}else{
		
			$sqlQuery = " SELECT use_id,use_name,use_email  FROM ".SQL_PREFIX."user WHERE use_name= '".$use_nameval."' AND  use_pwd = '".md5($use_pwdval)."' and use_locked = 0 LIMIT 1 ";
			
			$userInfo["webUser"] = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if($userInfo["webUser"]["use_id"])
			{
				$GLOBALS['WebSe'] -> SetSession( $userInfo );
				
				$nowtime = time();
				
				$userip = $GLOBALS['Helpe'] -> getip();
				//登录成功更新用户信息
				$sqlup = " UPDATE ".SQL_PREFIX."user  set  use_logcount = use_logcount +1 , use_loginip = '".$adminip."', use_logintime = ".$nowtime."  WHERE  use_name= '".$use_nameval."'  LIMIT 1 " ;
				$GLOBALS['MySql'] -> querySql($sqlup);

				echo 'success';
			}else{
				echo $GLOBALS['_LANG']["userorpwderror"];
				
			}
		}

	}
	
	
	function actlogin(  )
	{

		$use_nameval 	= $GLOBALS['Reque'] -> funpost("use_name");
		$use_pwdval  	= $GLOBALS['Reque'] -> funpost("use_pwd");
		$use_codeval  	= $GLOBALS['Reque'] -> funpost("use_code");
		
		$this -> logincount();
		
		if($use_codeval!=$_SESSION["Img"])
		{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["codeerror"]);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=act");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}else{
		
			$sqlQuery = " SELECT use_id,use_name,use_email  FROM ".SQL_PREFIX."user WHERE use_name= '".$use_nameval."' AND  use_pwd = '".md5($use_pwdval)."' and use_enabled = 1  LIMIT 1 ";
			//exit();
			$adminInfo["webUser"] = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if($adminInfo["webUser"]["use_id"])
			{
				$GLOBALS['WebSe'] -> SetSession( $adminInfo );

				$nowtime = time();
				
				$adminip = $GLOBALS['Helpe'] -> getip();
				//登录成功更新用户信息
				$sqlup = " UPDATE ".SQL_PREFIX."user  set  use_logcount = use_logcount +1 , use_loginip = '".$adminip."', use_logintime = ".$nowtime."  WHERE  use_name= '".$use_nameval."'  and use_id = ".$adminInfo["webUser"]["use_id"]." LIMIT 1 " ;
				$GLOBALS['MySql'] -> querySql($sqlup);
				
				//登录成功！重置IP错误信息清0！
				$updateip = "UPDATE `".SQL_PREFIX."loginerror` SET errorcount =  0  WHERE  ip_address = '".$adminip."' AND logtype = 'login' ";
				$GLOBALS['MySql'] -> querySql( $updateip );
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["loginuser"]);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL.'/user');
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				//var_dump($_SESSION);
			}else{
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["userorpwderror"]);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=act");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');

			}
		}
	}
	
	function logincount()
	{
		$adminip = $GLOBALS['Helpe'] -> getip();
		
		$now = time();
		if( $adminip!='Unknown' and $adminip!='' )
		{
			//查询记录是否存在
			$sqlip = " SELECT error_id , session_id , errorcount , start_time   FROM  ".SQL_PREFIX."loginerror WHERE ip_address = '".$adminip."' AND logtype = 'login' LIMIT 1 ";
			$useripInfo = $GLOBALS['MySql'] -> selectOne($sqlip);
			
			if($useripInfo["error_id"])
			{
				//超过一天。重置时间及数量
				if( $useripInfo["start_time"] > ( $now + 86400 ) )
				{
					$updateip = "UPDATE `".SQL_PREFIX."loginerror` SET `session_id` = '".$_SESSION["session_id"]."' , start_time ='".$now."' ,errorcount = '1'  WHERE  ip_address = '".$adminip."' AND logtype = 'login'   ";
					$GLOBALS['MySql'] -> querySql($updateip);	
				}elseif( $useripInfo["errorcount"] >= 20 )
				{
						$updateip = "UPDATE `loginerror` SET errorcount = errorcount + 1  WHERE  ip_address = '".$adminip."' AND logtype = 'login' ";
						$GLOBALS['MySql'] -> querySql( $updateip );
						$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_tomorrowerror']);
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=act");
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');
						exit();
				}else{
					if( $useripInfo["errorcount"] < 5 )
					{
						$updateip = "UPDATE `".SQL_PREFIX."loginerror` SET errorcount = errorcount + 1  WHERE  ip_address = '".$adminip."' AND logtype = 'login' ";
						$GLOBALS['MySql'] -> querySql($updateip);
						
					}elseif( $useripInfo["start_time"] > ( $now - 3600) )
					{
						$updateip = "UPDATE `".SQL_PREFIX."loginerror` SET errorcount = errorcount + 1  WHERE  ip_address = '".$adminip."' AND logtype = 'login' ";
						$GLOBALS['MySql'] -> querySql($updateip);

						$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_counterror']);
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=act");
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');
						exit();	
					}else{
						$updateip = "UPDATE `".SQL_PREFIX."loginerror` SET  errorcount = errorcount + 1 ,session_id = '".$_SESSION["session_id"]."'   WHERE  ip_address = '".$adminip."' AND logtype = 'login' ";
						$GLOBALS['MySql'] -> querySql($updateip);
					}

				}

				//时间未超过1小时，状态有效，数量是否超过5次
				//未超过5次更新+1

			}else{
				$insertip ="INSERT INTO `".SQL_PREFIX."loginerror` (`session_id` , `errorcount` , `ip_address` , `start_time` , `logtype` ) VALUES (  '".$_SESSION["session_id"]."', '1', '$adminip', '$now', 'login')";
				$GLOBALS['MySql'] -> querySql($insertip);	
			}
		}else{
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_syserror']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=act");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			exit();
		}
	
	}
	function reg()
	{
	
		$sqlQuery 	= " SELECT cfs_codestr,cfs_value FROM  ".SQL_PREFIX."configsys  WHERE cfs_type = 'reg' ORDER BY `cfs_id` ";
					
		$needReginfo 	= $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		if( $needReginfo )
		{
			foreach($needReginfo as $key => $val)
			{
				$needReg[$val["cfs_codestr"]] = explode('|',$val["cfs_value"]);	
			}
		}
		$getuserlink = $GLOBALS['WebSe'] -> getuserlink();
		
		$GLOBALS['Templ']->assign('getuserlink',$getuserlink);
		
		
		$webtitle = '免费注册';
		//处理你现在的位置
		$nowaddress = '您现在所在的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/user/login.php?load=login&act=reg">免费注册</a>';


		$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
		$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);
		//var_dump($needReg);
		
		$GLOBALS['Templ'] -> assign('needReg',$needReg);

		$GLOBALS['Templ'] -> display('reg_tpl.html');
		
		exit();
	}
	
	function reginfo()
	{

		$use_nameval 		= $GLOBALS['Reque'] -> funpost("use_name");
		$use_emailval 		= $GLOBALS['Reque'] -> funpost("use_email");
		$use_pwdval 		= $GLOBALS['Reque'] -> funpost("use_pwd");
		$reuse_pwdval 		= $GLOBALS['Reque'] -> funpost("reuse_pwd");
		
		$usd_truenameval 	= $GLOBALS['Reque'] -> funpost("usd_truename");
		$usd_provinceval	= $GLOBALS['Reque'] -> funpost("usd_province");
		$usd_cityval		= $GLOBALS['Reque'] -> funpost("usd_city");
		$usd_districtval 	= $GLOBALS['Reque'] -> funpost("usd_district");

		$usd_msnval 		= $GLOBALS['Reque'] -> funpost("regmsn");
		$usd_qqval			= $GLOBALS['Reque'] -> funpost("regqq");
		$usd_officephoneval	= $GLOBALS['Reque'] -> funpost("regphote");
		$usd_homephoneval 	= $GLOBALS['Reque'] -> funpost("reghomephote");
		$usd_mobileval 		= $GLOBALS['Reque'] -> funpost("regtel");

  
		
		if($use_nameval !='' and $use_emailval !='' and $use_pwdval !='' and $reuse_pwdval == $use_pwdval)
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."user  WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);

			
			if( $info["CNUM"] < 1 )
			{
				
				$sqlemailQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."user  WHERE use_email = '".$use_emailval."' ORDER BY `use_id` ";
				$infoEmail = $GLOBALS['MySql'] -> selectOne($sqlemailQuery);
				
				if( $infoEmail["CNUM"] < 1 )
				{
					if($usd_mobileval!=''){
						$sqlmobileQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."userdetail  WHERE usd_mobile = '".$usd_mobileval."' ORDER BY `use_id` ";
						$infoMobile = $GLOBALS['MySql'] -> selectOne( $sqlmobileQuery );
					}

					if( $infoMobile["CNUM"] < 1 )
					{
						//MD5加密码！！
						$md5use_pwdval = md5($use_pwdval);
						
						//最后收集信息
						$use_loginipval = $GLOBALS['Helpe'] -> getip();
						$use_regtimeval 	= time();
						
						$insertuser ="INSERT INTO `".SQL_PREFIX."user` (`use_name` ,  `use_email` , `use_pwd` ,`use_regtime`,`use_loginip` ) VALUES (  '$use_nameval',  '$use_emailval', '$md5use_pwdval', '$use_regtimeval','$use_loginipval')";
						
						$insertId = $GLOBALS['MySql'] -> insertSql($insertuser);
						
						$usd_truenameval 	= $GLOBALS['Reque'] -> funpost("usd_truename");
						$usd_provinceval	= $GLOBALS['Reque'] -> funpost("usd_province");
						$usd_cityval		= $GLOBALS['Reque'] -> funpost("usd_city");
						$usd_districtval 	= $GLOBALS['Reque'] -> funpost("usd_district");
				
						$usd_msnval 		= $GLOBALS['Reque'] -> funpost("regmsn");
						$usd_qqval			= $GLOBALS['Reque'] -> funpost("regqq");
						$usd_officephoneval	= $GLOBALS['Reque'] -> funpost("regphote");
						$usd_homephoneval 	= $GLOBALS['Reque'] -> funpost("reghomephote");
						$usd_mobileval 		= $GLOBALS['Reque'] -> funpost("regtel");
		
						
						$insertdetail ="INSERT INTO `".SQL_PREFIX."userdetail` (`use_id` , `usd_truename`,`usd_province`,`usd_city`,`usd_district`,`usd_msn` ,`usd_qq`,`usd_officephone`,`usd_homephone`,`usd_mobile`) VALUES (  '$insertId',  '$usd_truename','$usd_provinceval','$usd_cityval','$usd_districtval','$usd_msnval','$usd_qqval','$usd_officephoneval','$usd_homephoneval','$usd_mobileval')";
						$insertdetailId = $GLOBALS['MySql'] -> insertSql($insertdetail);
						
						
						//登录设置
						$userInfo["webUser"] = array('use_id' => $insertId ,'use_name' => $use_nameval, 'use_email' => $use_emailval);
						
						$GLOBALS['WebSe'] -> SetSession( $userInfo );

						
						$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["regsusse"]);
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/");
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');
						
					}else{
					
						$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["ismobileerror"]);
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=reg");
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
					}
				
				}else{
				
					$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["isemailerror"]);
					$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
					$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=reg");
					$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
				
				}
			}else{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["isrealyerror"]);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=reg");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}
			
			
			
		}else{
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["losserror"]);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/user/login.php?load=login&act=reg");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		
		}
	}


	function logout()
	{
		unset( $_SESSION["webUser"] );
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['us_logoutsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL."/");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}

}

$Login = new Login();

?>