<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class User
{
	function User()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	function infolist(  )
	{
		
		$pageSize = 20 ;		
		$keywordval 	= $GLOBALS['Reque'] -> funpost("keyword");
		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		$searchtypeval 	= $GLOBALS['Reque'] -> reqval["searchtype"];
		
		//var_dump( $searchtypeval );
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}

		
		if($GLOBALS['Reque'] -> getval["keyword"] !='')
		{   
			$keywordval 	= $GLOBALS['Reque'] -> getval["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			
			//$whereadd .= " AND (  use_name LIKE  '%".$keywordval."%' )";
			
			if( $searchtypeval =='' )
			{
				$whereadd .= " AND (  use_name LIKE  '%".$keywordval."%' )"; 
				$searchtypeval['0'] = 'use_name';
				$endArray["searchtype[0]"] = 'use_name';
			}else if($searchtypeval[0] !='' and $searchtypeval[1] !='')
			{
				$whereadd .= " AND  (  ".$searchtypeval[0]." LIKE  '%".$keywordval."%' OR ".$searchtypeval[1]." LIKE  '%".$keywordval."%' )";
				$endArray["searchtype[0]"] =$searchtypeval[0];
				$endArray["searchtype[1]"] =$searchtypeval[1];
			}else if($searchtypeval[0] !='' and $searchtypeval[1] =='')
			{
				$whereadd .= " AND  (  ".$searchtypeval[0]." LIKE  '%".$keywordval."%' )";
				$endArray["searchtype[0]"] =$searchtypeval[0];
			}else if( $searchtypeval[0] =='' and $searchtypeval[1] !='')
			{
				$whereadd .= " AND  (  ".$searchtypeval[1]." LIKE  '%".$keywordval."%' )";
				$endArray["searchtype[1]"] =$searchtypeval[1];
			}

			
		}
		
		
		//echo $whereadd;
		//exit();

		$listval  = $GLOBALS['Reque'] -> getval["listval"];
		$listtype = $GLOBALS['Reque'] -> getval["listtype"];

		$orderbyval = "";
		if($listval!='' and $listtype =='DESC' )
		{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = $listval;
			$orderbyval = $listval ."  DESC ";
			$listhtml = '';	//反向排序参数
			
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';
			
			$endArray["listtype"] = '';
			$endArray["listval"]  = $listval;

		}else{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = 'use_id';
			$orderbyval = " `use_id` DESC , `use_regtime` DESC ";	
		}
		

		//var_dump( $searchtypeval );

		 //["searchtype"]=> array(5) { [0]=> string(9) "user_name" [3]=> string(10) "userd_nick" [4]=> string(8) "userd_qq" [5]=> string(12) "userd_mobile" [6]=> string(5) "phone" }
		
		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间
		
		$sqlQuery = " SELECT use_id, use_name,  use_email, use_locked, use_logcount ,use_regtime ,use_logintime  FROM  ".SQL_PREFIX."user  WHERE 1  $whereadd  GROUP BY use_id ORDER BY  $orderbyval LIMIT  $LIMITnum , $pageSize ";
		
		$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."user  WHERE 1 $whereadd  ORDER BY `use_id`  ";
		
		$infocount = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=user&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"], $endArray );
		
		
		$GLOBALS['Templ']->assign('searchtype',$searchtypeval);
		$GLOBALS['Templ']->assign('listhtml',$listhtml);
		$GLOBALS['Templ']->assign('listval',$listval);
		$GLOBALS['Templ']->assign('listtype',$listtype);
 
		$GLOBALS['Templ']->assign('keywordval',$keywordval);
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('user_tpl.html');
		
		
	}
	
	function add(  )
	{
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24

		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=user&act=addinfo");
		$GLOBALS['Templ']->display('useradd_tpl.html');
	}
	
	function addinfo()
	{
		$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");

		$use_regtimeval 	= time();
		
		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 )
			{
				//MD5加密码！！
				$md5use_pwdval = md5($use_pwdval);
				
				$insertuser ="INSERT INTO `".SQL_PREFIX."user` (`use_name` , `use_email` , `use_pwd` , `use_regtime` ) VALUES (  '$use_nameval',  '$use_emailval', '$md5use_pwdval', '$use_regtimeval')";
				$userid = $GLOBALS['MySql'] -> insertSql($insertuser);
				
				$usd_truenameval 	= $GLOBALS['Reque'] -> funpost("usd_truename");
				$usd_sexval 		= $GLOBALS['Reque'] -> funpost("usd_sex");
				$usd_nickval 		= $GLOBALS['Reque'] -> funpost("usd_nick");
				$usd_birthdayval 	= $GLOBALS['Reque'] -> funpost("usd_birthday");
				$usd_saltval 		= $GLOBALS['Reque'] -> funpost("usd_salt");
				$usd_msnval 		= $GLOBALS['Reque'] -> funpost("usd_msn");
				$usd_qqval 			= $GLOBALS['Reque'] -> funpost("usd_qq");
				$usd_officephoneval = $GLOBALS['Reque'] -> funpost("usd_officephone");
				$usd_homephoneval 	= $GLOBALS['Reque'] -> funpost("usd_homephone");
				$usd_mobileval 		= $GLOBALS['Reque'] -> funpost("usd_mobile");
				
				$usd_provinceval 	= $GLOBALS['Reque'] -> funpost("usd_province");
				
				$usd_provinceval 	= $GLOBALS['Reque'] -> funpost("idprovince");
				$usd_cityval 		= $GLOBALS['Reque'] -> funpost("idcity");
				$usd_districtval 	= $GLOBALS['Reque'] -> funpost("iddistrict");
				$usd_pcdallval		= $GLOBALS['Reque'] -> funpost("hiprovince").$GLOBALS['Reque'] -> funpost("hicity").$GLOBALS['Reque'] -> funpost("hidistrict");
				
				$usd_addressval 	= $GLOBALS['Reque'] -> funpost("usd_address");				

				$insertuserdetail ="INSERT INTO `".SQL_PREFIX."userdetail` (`use_id` , `usd_truename` , `usd_sex` , `usd_nick`,`usd_birthday`,`usd_salt`,`usd_msn` ,`usd_qq`,`usd_officephone`,`usd_homephone`,`usd_mobile`,`usd_province`,`usd_city`,`usd_district`,`usd_address`) VALUES (  '$userid',  '$usd_truenameval', '$usd_sexval', '$usd_nickval', '$usd_birthdayval', '$usd_saltval', '$usd_msnval', '$usd_qqval', '$usd_officephoneval', '$usd_homephoneval', '$usd_mobileval', '$usd_provinceval', '$usd_cityval', '$usd_districtval', '$usd_addressval')";
				$GLOBALS['MySql'] -> insertSql($insertuserdetail);
				
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}else{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_isrealyerror']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=add");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}
			
			
		}else{
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
			//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		//查询一下是否超级帐号
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."user  WHERE `use_id` in ( $infoid ) and use_name = '".EMPLOYEE_BASEUSER."'   ";
			
		$infocnum = $GLOBALS['MySql'] -> selectOne( $sqlQuery );

		
		if($infocnum["CNUM"]> 0 )
		{
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_deladminerror']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			
		}else{
			$deletesql ="DELETE FROM `".SQL_PREFIX."user` WHERE `use_id` in ( $infoid )  ";
			$GLOBALS['MySql'] -> querySql($deletesql);
			
			$deletesqldetail ="DELETE FROM `".SQL_PREFIX."userdetail` WHERE `use_id` in ( $infoid )  ";
			$GLOBALS['MySql'] -> querySql( $deletesqldetail );
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		

		
	}
	
	function info(  )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT u.use_id ,u.use_name, u.use_email ,d.usd_truename,d.usd_sex,d.usd_nick,d.usd_birthday,d.usd_salt ,d.usd_msn,d.usd_qq,d.usd_officephone,d.usd_homephone ,d.usd_mobile,d.usd_province,d.usd_city,d.usd_district,d.usd_address  FROM  ".SQL_PREFIX."user as u  left join  ".SQL_PREFIX."userdetail as d  on d.use_id = u.use_id  WHERE u.use_id = '".$infoid."' ORDER BY u.`use_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//var_dump( $info );
		
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24 
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=user&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('useredit_tpl.html');
	
	}
	
	function infopwd(  )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT use_id ,use_name,use_type  FROM  ".SQL_PREFIX."user   WHERE use_id = '".$infoid."' ORDER BY `use_id` ";
		
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		if(EMPLOYEE_BASEUSER == $info["use_name"])
		{
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['db_adminbaseuser']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');

		}else{
		
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24 
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=user&act=editinfopwd");
		$GLOBALS['Templ'] -> assign('info',$info);
		
		$GLOBALS['Templ'] -> display('usereditpwd_tpl.html');
		}
	}
	
	function editinfo( )
	{	
	
		$use_idval 	= $GLOBALS['Reque'] -> funpost("use_id");
				
		//$use_nameval 	= $GLOBALS['Reque'] -> funpost("use_name"); //不可修改
		
		$use_emailval 	 = $GLOBALS['Reque'] -> funpost("use_email");


		$addsetUp = '';
		if($use_idval !='' and $use_emailval !='' )
		{
		
			$updateuser = "UPDATE `".SQL_PREFIX."user` SET    use_email = '".$use_emailval."'   WHERE  use_id = '".$use_idval."' ";
			
			$GLOBALS['MySql'] -> querySql( $updateuser );
			
			$usd_truenameval 	= $GLOBALS['Reque'] -> funpost("usd_truename");
			$usd_sexval 	 	= $GLOBALS['Reque'] -> funpost("usd_sex");
			
			$usd_nickval 	 	= $GLOBALS['Reque'] -> funpost("usd_nick");
			$usd_birthdayval 	= $GLOBALS['Reque'] -> funpost("usd_birthday");
			
			$usd_saltval 	 	= $GLOBALS['Reque'] -> funpost("usd_salt");
			$usd_msnval 	 	= $GLOBALS['Reque'] -> funpost("usd_msn");

			$usd_qqval 			= $GLOBALS['Reque'] -> funpost("usd_qq");
			$usd_officephoneval = $GLOBALS['Reque'] -> funpost("usd_officephone");
			
			$usd_homephoneval 	= $GLOBALS['Reque'] -> funpost("usd_homephone");
			$usd_mobileval 		= $GLOBALS['Reque'] -> funpost("usd_mobile");
			
			$usd_provinceval 	= $GLOBALS['Reque'] -> funpost("usd_province");
			$usd_addressval  	= $GLOBALS['Reque'] -> funpost("usd_address");
			
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."userdetail  WHERE use_id = '".$use_idval."' ";
			
			$infoNUM = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $infoNUM["CNUM"] < 1 )
			{
				$insertsql ="INSERT INTO `".SQL_PREFIX."userdetail` (`use_id` , `usd_truename` , `usd_sex` , `usd_nick` , `usd_birthday` , `usd_salt` , `usd_msn` , `usd_qq` , `usd_officephone` , `usd_homephone` , `usd_mobile` , `usd_province` , `usd_address`  ) VALUES (  '$use_idval', '$usd_truenameval', '$usd_sexval', '$usd_nickval', '$usd_birthdayval', '$usd_saltval', '$usd_msnval', '$usd_qqval', '$usd_officephoneval', '$usd_homephoneval', '$usd_mobileval','$usd_provinceval','$usd_addressval')";
				$GLOBALS['MySql'] -> querySql( $insertsql );	
			}else{
				$updateuserdetail = "UPDATE `".SQL_PREFIX."userdetail` SET    usd_truename = '".$usd_truenameval."',usd_sex = '".$usd_sexval."',usd_nick = '".$usd_nickval."',usd_birthday = '".$usd_birthdayval."',usd_salt = '".$usd_saltval."',usd_msn = '".$usd_msnval."',usd_qq = '".$usd_qqval."',usd_officephone = '".$usd_officephoneval."',usd_homephone = '".$usd_homephoneval."',usd_mobile = '".$usd_mobileval."',usd_province = '".$usd_provinceval."',usd_address = '".$usd_addressval."'   WHERE  use_id = '".$use_idval."' ";
				$GLOBALS['MySql'] -> querySql( $updateuserdetail );
			}
			
			

			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=info&id=".$use_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}

	function editinfopwd( )
	{	
	
		$use_idval 		 = $GLOBALS['Reque'] -> funpost("use_id");		
		$use_pwdval 	 = $GLOBALS['Reque'] -> funpost("use_pwd");
		$use_repwdval 	 = $GLOBALS['Reque'] -> funpost("use_repwd");

		$addsetUp = '';
		
		if($use_idval !='' and $use_pwdval !='' and  $use_repwdval !='' )
		{
			
			if($use_pwdval == $use_repwdval)
			{
				$updateuser = "UPDATE  `".SQL_PREFIX."user` SET    use_pwd = '".md5($use_pwdval)."'   WHERE  use_id = '".$use_idval."' ";
			
				$GLOBALS['MySql'] -> querySql( $updateuser );
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');		
					
			}else{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['js_repwd']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infopwd&id=".$use_idval);
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	

			}

		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=user&act=infopwd&id=".$use_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}

	
	function actlogout()
	{
		$GLOBALS['WebSe'] -> dropSession();
		header("location:".EMPLOYEE_WEBURL."/index.php?load=user&act=act");
		exit();
	}
	
	function pwd()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		$GLOBALS['Templ'] -> caching = true;
		$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24	
		$GLOBALS['Templ'] -> assign('use_name',$_SESSION["emplyeeUser"]["use_name"]);
		$GLOBALS['Templ'] -> assign('actUrl',EMPLOYEE_WEBURL."/?load=user&act=actpwd");
		$GLOBALS['Templ'] -> display('pwd_tpl.html');
		
		//exit();
	}
	
	function actpwd()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		
		$oldpwdval  	= $GLOBALS['Reque'] -> funpost("oldpwd");
		$use_pwdval		= $GLOBALS['Reque'] -> funpost("use_pwd");
		$reuse_pwdval 	= $GLOBALS['Reque'] -> funpost("reuse_pwd");
		$use_idval 		= $_SESSION["emplyeeUser"]["use_id"];
		
		$sql = " SELECT use_id  FROM ".SQL_PREFIX."user WHERE use_id= '".$use_idval."' AND  use_pwd = '".md5($oldpwdval)."' LIMIT 1 ";
		
		$User = $GLOBALS['MySql'] -> selectOne($sql);
		
		if($User["use_id"]!='')
		{
			//修改成功！
			$sqlup = " UPDATE ".SQL_PREFIX."user  set  use_pwd = '".md5($use_pwdval)."'  WHERE  use_id= ".$use_idval;
			$GLOBALS['MySql'] -> querySql($sqlup);
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['LA_pwdsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/?load=user&act=pwd");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			//exit();
		}else{
			//修改有误!
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['LA_pwderror']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/?load=user&act=pwd");	
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			
		}
		
		//exit();
	}
	
}

$User = new User();

?>