<?php
/*-------------------------------------
	版权所有：南京牛昆网络科技有限公司TEL-15896098989
	程序版本：1.0
	更新日期：//last modified on 2015-5-14 17:12 
-------------------------------------*/
class adminer
{
	private $db;
	private $role_table;
	private $admin_table;
	private $admin_log;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->role_table=LA_PRE.'adminrole';
		$this->admin_table=LA_PRE.'admin';
		$this->admin_log=LA_PRE.'admin_log';
	}

	function rolelist()
	{
		global $page;
		$where=1;
		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='id ASC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=10;
		$result=$datalist->getlist($this->role_table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function role_edit($info,$id)
	{
		global $_siteid,$_roleid;
		$info=array_map('trim',$info);
		$id=intval($id);
		if($id!=$_roleid && $_roleid!=1)
		{
			return false;
		}

		$ainfo=$this->getrole($id);
		if(!$ainfo)
		{
			return false;
		}

		return $this->db->update($this->role_table,$info,'id='.$id);
	}

	function getrole($id)
	{
		$id=intval($id);
		return $this->db->fetch_one("SELECT * FROM `$this->role_table` WHERE `$this->role_table`.`id`=$id");
	}

	function getrolename($id)
	{
		$id=intval($id);
		$r=$this->getrole($id);
		return $r['name'];
	}

	function admincount($roleid)
	{
		$sql="SELECT COUNT(*) AS count FROM `$this->admin_table` WHERE `$this->admin_table`.`roleid`=".intval($roleid);
		return get_cache_counts($sql);
	}

	function adminlist($roleid=-1)
	{
		global $page,$_siteid,$siteidobj;
		$roleid=intval($roleid);
		
		$where=$_siteid?'siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':1;
		$where.=$roleid==-1?' AND 1':' AND roleid='.$roleid;
		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='roleid ASC,userid DESC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=10;
		$result=$datalist->getlist($this->admin_table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function admin_edit($info,$id)
	{
		global $_siteid,$siteidobj,$_userid,$_roleid,$admin_founders;
		$info=array_map('trim',$info);
		$id=intval($id);
		if($id!=$_userid && $_roleid!=1)
		{
			return false;
		}

		$ainfo=$this->get($id);
		if(!$ainfo || ($_siteid && !in_array($ainfo['siteid'],$siteidobj->getchildid($_siteid))))
		{
			return false;
		}

		if($this->getbyusername($info['username']) && $info['username']!=$ainfo['username'])
		{
			return false;
		}
		
		$info['roleid']=($info['roleid']==$_roleid || $_roleid==1)?intval($info['roleid']):$_roleid;
		if(in_array($_userid,$admin_founders))
		{
			$info['roleid']=$_roleid;
		}

		$info['password']=trim($info['password'])?md5(trim($info['password'])):$ainfo['password'];
		$info['siteid']==($_siteid || $siteidobj->in_parentid($info['siteid'],$_siteid)) ?intval($info['siteid']):$_siteid;
		return $this->db->update($this->admin_table,$info,'userid='.$id);
	}

	function admin_add($info)
	{
		global $_siteid,$siteidobj,$_userid,$_roleid,$admin_founders;
		$info=array_map('trim',$info);
		if($_roleid!=1)
		{
			return false;
		}

		if($this->getbyusername($info['username']))
		{
			return false;
		}
		
		$info['roleid']=($info['roleid']==$_roleid || $_roleid==1)?intval($info['roleid']):$_roleid;
		if(!in_array($_userid,$admin_founders))
		{
			$info['roleid']=$_roleid;
		}

		$info['password']=trim($info['password'])?md5(trim($info['password'])):$ainfo['password'];
		$info['siteid']==($_siteid || $siteidobj->in_parentid($info['siteid'],$_siteid)) ?intval($info['siteid']):$_siteid;
		return $this->db->insert($this->admin_table,$info,true);
	}

	function admin_delete($id)
	{
		global $_siteid,$siteidobj,$_userid,$_roleid,$admin_founders;
		$id=intval($id);

		if(in_array($id,$admin_founders))
		{
			return false;
		}

		if($id!=$_userid && $_roleid!=1)
		{
			return false;
		}

		$ainfo=$this->get($id);
		if(!$ainfo || ($_siteid && !in_array($ainfo['siteid'],$siteidobj->getchildid($_siteid))))
		{
			return false;
		}

		return $this->db->mysql_delete($this->admin_table,$id,'userid');
	}

	function admin_set($id,$status)
	{
		global $_siteid,$siteidobj,$_userid,$_roleid,$admin_founders;
		$id=intval($id);

		if(in_array($id,$admin_founders))
		{
			return false;
		}

		if($id!=$_userid && $_roleid!=1)
		{
			return false;
		}

		$ainfo=$this->get($id);
		if(!$ainfo || ($_siteid && !in_array($ainfo['siteid'],$siteidobj->getchildid($_siteid))))
		{
			return false;
		}

		return $this->db->update($this->admin_table,array('status'=>intval($status)),'userid='.intval($id));
	}

	function get($id)
	{
		$id=intval($id);
		return $this->db->fetch_one("SELECT * FROM `$this->admin_table` WHERE `$this->admin_table`.`userid`=$id");
	}

	function getbyusername($username)
	{
		$username=trim($username);
		return $this->db->fetch_one("SELECT * FROM `$this->admin_table` WHERE `$this->admin_table`.`username`='$username'");
	}
	
	function loglist()
	{
		$roleid=intval($roleid);
		global $page,$_siteid,$siteidobj;
		$where=$_siteid?'siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':1;

		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='id DESC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=10;
		$result=$datalist->getlist($this->admin_log,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function setlog($username,$password,$desc,$status)
	{
		$log['inputpwd']=$status?'******':$password;
		$log['inputuser']=$username;
		$log['status']=$status;
		$log['desc']=$desc;
		$log['ip']=IP;
		$log['logtime']=TIME;
		$info['siteid']=intval($_SESSION['siteid']);
		return $this->db->insert($this->admin_log,$log);
	}

	function getlog($id)
	{
		$id=intval($id);
		return $this->db->fetch_one("SELECT * FROM `$this->admin_log` WHERE `$this->admin_log`.`id`=$id");
	}

	function log_delete($id)
	{
		global $_siteid,$siteidobj;
		$id=intval($id);

		$linfo=$this->getlog($id);
		if(!$linfo || ($_siteid && !in_array($linfo['siteid'],$siteidobj->getchildid($_siteid))))
		{
			return false;
		}
		return $this->db->mysql_delete($this->admin_log,$id);
	}
}
