<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Admin
{
	function Admin()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	function infolist(  )
	{
		$pageSize = 20 ;	
		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		
		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间
		
		$sqlQuery = " SELECT admin_id, admin_name, admin_truename, admin_email, admin_logcount ,admin_addtime ,admin_lastlogtime  FROM  ".SQL_PREFIX."admin  WHERE 1 GROUP BY admin_id ORDER BY `admin_id` LIMIT  $LIMITnum , $pageSize ";
		
		$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."admin  WHERE 1  ORDER BY `admin_id`  ";
		
		$infocount = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//var_dump($info);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=admin&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ']->assign('ADMIN_BASEUSER',ADMIN_BASEUSER);
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('admin_tpl.html');
		
	}
	
	function add(  )
	{
		$GLOBALS['Templ'] -> caching = true;
		$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24

		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/admin/addinfo");
		$GLOBALS['Templ']->display('adminadd_tpl.html');
	}
	
	function addinfo()
	{
		
		$admin_nameval 		= $GLOBALS['Reque'] -> funpost("admin_name");
		//$admin_truenameval 	= $GLOBALS['Reque'] -> funpost("admin_truename");
		$admin_emailval 	= $GLOBALS['Reque'] -> funpost("admin_email");
		$admin_pwdval 		= $GLOBALS['Reque'] -> funpost("admin_pwd");
		$readmin_pwdval 	= $GLOBALS['Reque'] -> funpost("readmin_pwd");
		
		if($GLOBALS['Reque'] -> funpost("user_pwd")!=''){
			$user_pwdval 	= $GLOBALS['Reque'] -> funpost("user_pwd");
		}else{
			$user_pwdval 	= $GLOBALS['Reque'] -> funpost("admin_pwd");
		}
		
		
		$admin_addtimeval 	= time();
		
		if($admin_nameval !='' and $admin_pwdval !='' and $readmin_pwdval == $admin_pwdval)
		{
			//查询用户表是否存在数据库 ( 用户表是全站帐号统在的表 )	
			$sqlQuery = " SELECT count(1) as CNUM  FROM ".SQL_PREFIX."user  WHERE user_name = '".$admin_nameval."' ORDER BY `user_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			
			
			if( $info["CNUM"] < 1 )
			{
				//MD5加密码！！
				$md5user_pwdval = md5($user_pwdval);
				
				$user_regipval  = $GLOBALS['Helpe'] -> getip();
				
				$insertuser ="INSERT INTO `".SQL_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')";
				$GLOBALS['MySql'] -> querySql($insertuser);
				
				
				$md5admin_pwdval = md5($admin_pwdval);
				$insertadmin ="INSERT INTO `".SQL_PREFIX."admin` (`admin_name` ,  `admin_email` , `admin_pwd` , `admin_addtime` ) VALUES (  '$admin_nameval',  '$admin_emailval', '$md5admin_pwdval', '$admin_addtimeval')";
				$GLOBALS['MySql'] -> querySql($insertadmin);
				
				
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/admin/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/admin/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/admin/add/");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
		//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$indel = implode(',', $delArray);

		$deletesql ="DELETE FROM `".SQL_PREFIX."admin` WHERE `admin`.`admin_id` in ( $indel ) and admin_name != '".ADMIN_BASEUSER."' ";
		
		$GLOBALS['MySql'] -> querySql($deletesql);
		
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/admin/infolist/");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info(  )
	{
		$infoid = $infoArray[0];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."admin  WHERE admin_id = '".$infoid."' ORDER BY `admin_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		//echo $_SESSION["webAdmin"]["admin_name"];
		if(ADMIN_BASEUSER == $info["admin_name"] and ADMIN_BASEUSER != $_SESSION["webAdmin"]["admin_name"])
		{
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS["Templ"]->tpl_vars["LA_adminbaseuser"]->value);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/admin/infolist/");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			exit();
		}
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24 
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/admin/editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('adminedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$admin_idval 	= $GLOBALS['Reque'] -> funpost("admin_id");
		//$admin_nameval 	= $GLOBALS['Reque'] -> funpost("admin_name"); //不可修改
		$admin_truenameval 	= $GLOBALS['Reque'] -> funpost("admin_truename");
		$admin_emailval 	= $GLOBALS['Reque'] -> funpost("admin_email");
		$admin_pwdval 	= $GLOBALS['Reque'] -> funpost("admin_pwd");
		$readmin_pwdval = $GLOBALS['Reque'] -> 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{
					$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS["Templ"]->tpl_vars["LA_repwderror"]->value);
					$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
					$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/admin/info/".$admin_idval);
					$GLOBALS['Templ'] -> display('suggestion_tpl.html');
					exit();
				}
			}
			
			$updatelink = "UPDATE `".SQL_PREFIX."admin` SET  admin_truename = '".$admin_truenameval."' , admin_email = '".$admin_emailval."' ".$addsetUp."  WHERE  admin_id = '".$admin_idval."' ";
			
			$GLOBALS['MySql'] -> querySql( $updatelink );
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/admin/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/admin/info/".$admin_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
	
	function powerinfo(   )
	{
		$infoid = $infoArray[0];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."admin  WHERE admin_id = '".$infoid."' ORDER BY `admin_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		
		if(ADMIN_BASEUSER == $info["admin_name"] ) //and ADMIN_BASEUSER != $_SESSION["webAdmin"]["admin_name"]
		{
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS["Templ"]->tpl_vars["LA_adminbaseuser"]->value);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/admin/infolist/");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			exit();
		}
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/admin/editpowerinfo");
		$GLOBALS['Templ'] -> assign('info',$info);

		//一般情况是添加，修改，删除。
		$powerForm = array( 
			'companyweb' => array( 
					'product' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					'prosort' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					'artsort' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					'artlice' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
				   'helpsort' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					   'help' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
						 'ad' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
				 'adposition' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					 'invite' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					 'employ' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					'webmenu' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					   'vote' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false,'item'=>false),
					   'book' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					   'link' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
				   'download' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
					  'admin' => array('infolist'=>false, 'addinfo'=>false,'del'=>false,'editinfo'=>false),
						'msg' => array('infolist'=>false, 'del'=>false,'replyinfo'=>false), //查看回复
					 'config' => array('infolist'=>false, 'editinfo'=>false),//查看修改
					  'login' => array('actpwd'=>false) //登录，登出，修改密码
								)
						);
						
		$sqlQuery 	= " SELECT * FROM  ".SQL_PREFIX."userpower  WHERE admin_id = '".$infoid."' ORDER BY `admin_id` ";
					
		$infoclass 	= $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		//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);
				
			}
		}
	
		$GLOBALS['Templ'] -> assign('powerForm',$powerForm);
		$GLOBALS['Templ'] -> display('userpowerinfo_tpl.html');
		
	}
	
	//第一为组权限名称
	//第二为文件名称
	//第三为文件处理方法名称（数组）
	
	function editpowerinfo(  )
	{
		$admin_idval 		= $GLOBALS['Reque'] -> funpost("admin_id");
		$menuval 			= $GLOBALS['Reque'] -> funpost("menu");
		$menuitemval 		= $GLOBALS['Reque'] -> 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 .= " ); ";
			
	   }

		$sqlQuery 	= " SELECT count(1) as CNUM   FROM  ".SQL_PREFIX."userpower  WHERE admin_id = '".$admin_idval."' and userp_form = '".$userp_formval."' ORDER BY `admin_id` ";
				
		$infoclass 	= $GLOBALS['MySql'] -> selectOne( $sqlQuery );

		if( $infoclass["CNUM"] < 1 )
		{
			$insertsql ="INSERT INTO `".SQL_PREFIX."userpower` (`admin_id` , `userp_form` ,  `userp_action`  ) VALUES (  '$admin_idval', '$userp_formval', '".$addstr."')";
			$GLOBALS['MySql'] -> querySql( $insertsql );		
		}else{
					
			$updatesql = "UPDATE `".SQL_PREFIX."userpower` SET  userp_action = '".$addstr."'  WHERE  admin_id = '".$admin_idval."' and  userp_form = '".$userp_formval."' ";
			$GLOBALS['MySql'] -> querySql( $updatesql );
		}
			
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/admin/powerinfo/".$admin_idval);
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
	}
	
}

$Admin = new Admin();

?>