<?php
if (!defined('HUCART_ORG')) exit('HuCart.COM No direct script access allowed');
class Admin
{
	function Admin()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	function info_list(  )
	{
		$page_size = 20 ;	
		$page_num_val 	= $this -> input ->  req["page_num"];
		
		if( $page_num_val > 1 )
		{
			$page_num  = $page_num_val;
			$limit_num = ceil( ( $page_num_val -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}
		
		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间
		
		$sql_query = " SELECT admin_id, admin_name, admin_truename, admin_email, admin_logcount ,admin_addtime ,admin_lastlogtime  FROM  ".$this -> db -> prefix."admin  WHERE 1 GROUP BY admin_id ORDER BY `admin_id` LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."admin  WHERE 1  ORDER BY `admin_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		
		//var_dump($info);
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=admin&act=info_list', $page_num  , $page_size  , $result_count = $infocount["CNUM"] );
		
		$this -> view -> assign('ADMIN_BASEUSER',ADMIN_BASEUSER);
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('admin.tpl');
		
	}
	
	function add(  )
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/admin/add_info");
		$this -> view -> display('adminadd.tpl');
	}
	
	function add_info()
	{
		
		$admin_nameval 		= $this -> input ->  funpost("admin_name");
		//$admin_truenameval 	= $this -> input ->  funpost("admin_truename");
		$admin_emailval 	= $this -> input ->  funpost("admin_email");
		$admin_pwdval 		= $this -> input ->  funpost("admin_pwd");
		$readmin_pwdval 	= $this -> input ->  funpost("readmin_pwd");
		
		if($this -> input ->  funpost("user_pwd")!=''){
			$user_pwdval 	= $this -> input ->  funpost("user_pwd");
		}else{
			$user_pwdval 	= $this -> input ->  funpost("admin_pwd");
		}
		
		
		$admin_addtimeval 	= time();
		
		if($admin_nameval !='' and $admin_pwdval !='' and $readmin_pwdval == $admin_pwdval)
		{
			//查询用户表是否存在数据库 ( 用户表是全站帐号统在的表 )	
			$sql_query = " SELECT count(1) as CNUM  FROM ".$this -> db -> prefix."user  WHERE user_name = '".$admin_nameval."' ORDER BY `user_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			
			
			if( $info["CNUM"] < 1 )
			{
				//MD5加密码！！
				$md5user_pwdval = md5($user_pwdval);
				
				$user_regipval  = $this -> helper -> getip();
				
				$insertuser ="INSERT INTO `".$this -> db -> prefix."user` (`user_name` ,`user_type`, `user_email`, `user_pwd` , `user_regtime`,`user_regip` ) VALUES (  '$admin_nameval', 'admin', '$user_emailval',  '$md5user_pwdval', '$admin_addtimeval','$user_regipval')";
				$this -> db -> query_sql($insertuser);
				
				
				$md5admin_pwdval = md5($admin_pwdval);
				$insertadmin ="INSERT INTO `".$this -> db -> prefix."admin` (`admin_name` ,  `admin_email` , `admin_pwd` , `admin_addtime` ) VALUES (  '$admin_nameval',  '$admin_emailval', '$md5admin_pwdval', '$admin_addtimeval')";
				$this -> db -> query_sql($insertadmin);
				
				
				
				$this -> view ->  assign('info_title',$this->lang['add_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/info_list/");
				$this -> view ->  display('suggestion.tpl');	
			}else{
				
				$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/add/");
				$this -> view ->  display('suggestion.tpl');	
			}
			
		}else{
			
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/add/");
			$this -> view ->  display('suggestion.tpl');		}
		//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$indel = implode(',', $delArray);

		$deletesql ="DELETE FROM `".$this -> db -> prefix."admin` WHERE `admin`.`admin_id` in ( $indel ) and admin_name != '".ADMIN_BASEUSER."' ";
		
		$this -> db -> query_sql($deletesql);
		
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/info_list/");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info(  )
	{
		$infoid = $infoArray[0];
		
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."admin  WHERE admin_id = '".$infoid."' ORDER BY `admin_id` ";
		$info = $this -> db -> select_one($sql_query);
		//echo $_SESSION["webAdmin"]["admin_name"];
		if(ADMIN_BASEUSER == $info["admin_name"] and ADMIN_BASEUSER != $_SESSION["webAdmin"]["admin_name"])
		{
			$this -> view ->  assign('info_title',$GLOBALS["Templ"]->tpl_vars["LA_adminbaseuser"]->value);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/info_list/");
			$this -> view ->  display('suggestion.tpl');
			exit();
		}
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24 
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/admin/edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('adminedit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$admin_idval 	= $this -> input ->  funpost("admin_id");
		//$admin_nameval 	= $this -> input ->  funpost("admin_name"); //不可修改
		$admin_truenameval 	= $this -> input ->  funpost("admin_truename");
		$admin_emailval 	= $this -> input ->  funpost("admin_email");
		$admin_pwdval 	= $this -> input ->  funpost("admin_pwd");
		$readmin_pwdval = $this -> input ->  funpost("readmin_pwd");
		$addsetUp = '';
		if($admin_idval !='' and $admin_truenameval !='' )
		{
			if($admin_pwdval!='')
			{
				
				if( $admin_pwdval == $readmin_pwdval )
				{
					
					$addsetUp = " , admin_pwd = '".md5($admin_pwdval)."' ";
					
				}else{
					$this -> view ->  assign('info_title',$GLOBALS["Templ"]->tpl_vars["LA_repwderror"]->value);
					$this -> view ->  assign('URL_TIME',URL_TIME);
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/info/".$admin_idval);
					$this -> view ->  display('suggestion.tpl');
					exit();
				}
			}
			
			$updatelink = "UPDATE `".$this -> db -> prefix."admin` SET  admin_truename = '".$admin_truenameval."' , admin_email = '".$admin_emailval."' ".$addsetUp."  WHERE  admin_id = '".$admin_idval."' ";
			
			$this -> db -> query_sql( $updatelink );
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/info_list/");
			$this -> view ->  display('suggestion.tpl');
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/info/".$admin_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
	
	function powerinfo(   )
	{
		$infoid = $infoArray[0];
		
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."admin  WHERE admin_id = '".$infoid."' ORDER BY `admin_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		
		if(ADMIN_BASEUSER == $info["admin_name"] ) //and ADMIN_BASEUSER != $_SESSION["webAdmin"]["admin_name"]
		{
			$this -> view ->  assign('info_title',$GLOBALS["Templ"]->tpl_vars["LA_adminbaseuser"]->value);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/info_list/");
			$this -> view ->  display('suggestion.tpl');
			exit();
		}
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/admin/editpowerinfo");
		$this -> view ->  assign('info',$info);

		//一般情况是添加，修改，删除。
		$powerForm = array( 
			'companyweb' => array( 
					'product' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					'prosort' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					'artsort' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					'artlice' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
				   'helpsort' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					   'help' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
						 'ad' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
				 'adposition' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					 'invite' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					 'employ' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					'webmenu' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					   'vote' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false,'item'=>false),
					   'book' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					   'link' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
				   'download' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
					  'admin' => array('info_list'=>false, 'add_info'=>false,'del'=>false,'edit_info'=>false),
						'msg' => array('info_list'=>false, 'del'=>false,'replyinfo'=>false), //查看回复
					 'config' => array('info_list'=>false, 'edit_info'=>false),//查看修改
					  'login' => array('actpwd'=>false) //登录，登出，修改密码
								)
						);
						
		$sql_query 	= " SELECT * FROM  ".$this -> db -> prefix."userpower  WHERE admin_id = '".$infoid."' ORDER BY `admin_id` ";
					
		$infoclass 	= $this -> db -> select_info( $sql_query );
		
		//var_dump($infoclass);					
		eval( $infoclass[0]["userp_action"] );
		
		
		foreach($powerForm["companyweb"] as $keykey => $valval)
		{
			if($companywebArray[$keykey])
			{
				
				$fillArray = array_fill_keys( $companywebArray[$keykey], true);
				$powerForm["companyweb"][$keykey] = array_merge( $valval ,$fillArray);
				
			}
		}
	
		$this -> view ->  assign('powerForm',$powerForm);
		$this -> view ->  display('userpowerinfo.tpl');
		
	}
	
	//第一为组权限名称
	//第二为文件名称
	//第三为文件处理方法名称（数组）
	
	function editpowerinfo(  )
	{
		$admin_idval 		= $this -> input ->  funpost("admin_id");
		$menuval 			= $this -> input ->  funpost("menu");
		$menuitemval 		= $this -> input ->  funpost("menuitem");
		
		//echo $admin_idval ;
		$userp_formval = 'companyweb';
		
		if(is_array($menuitemval))
		{

			$addstr = " \$companywebArray =  array( ";
			
			$ff = 0; 
			foreach( $menuitemval as $key => $val )
			{
				if($ff==0){
					$addstr .= "\'".$key."\'=> array( ";  // implode(',',$val);
				}else{
					$addstr .= ",\'".$key."\'=> array( ";  // implode(',',$val);
				}
				$vv = 0;
				foreach($val as $vkey =>$vval)
				{
					
					if($vv==0)
					{
						$addstr = $addstr."\'".$vval."\'";
					}else{
						$addstr = $addstr.",\'".$vval."\'";
					}
					$vv++;
				}
				$addstr .= " ) ";
				
				$ff++;
				
			}
			$addstr .= " ); ";
			
	   }

		$sql_query 	= " SELECT count(1) as CNUM   FROM  ".$this -> db -> prefix."userpower  WHERE admin_id = '".$admin_idval."' and userp_form = '".$userp_formval."' ORDER BY `admin_id` ";
				
		$infoclass 	= $this -> db -> select_one( $sql_query );

		if( $infoclass["CNUM"] < 1 )
		{
			$insertsql ="INSERT INTO `".$this -> db -> prefix."userpower` (`admin_id` , `userp_form` ,  `userp_action`  ) VALUES (  '$admin_idval', '$userp_formval', '".$addstr."')";
			$this -> db -> query_sql( $insertsql );		
		}else{
					
			$updatesql = "UPDATE `".$this -> db -> prefix."userpower` SET  userp_action = '".$addstr."'  WHERE  admin_id = '".$admin_idval."' and  userp_form = '".$userp_formval."' ";
			$this -> db -> query_sql( $updatesql );
		}
			
		$this -> view ->  assign('info_title',$this->lang['edit_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/admin/powerinfo/".$admin_idval);
		$this -> view ->  display('suggestion.tpl');
				
	}
	
}

$Admin = new Admin();

?>