<?php
if(!defined('IN_TM')) exit('Access Denied');
class admin{
	var $db;
	var $table;
	var $pages;
    var $TM;

    function __construct(){
    	global $db,$TM;
        $this->db = &$db;
        $this->table = DB_PRE.'admin';
        $this->TM = $TM;
    }

    function admin(){
        $this->__construct();
    }
    function msg(){
		return $this->msg;
    }

	
    /*
     * 修改用户资料
     */
    function del($userid){
       $result = $this->db->query("DELETE FROM $this->table WHERE id=$userid");
	if($result){
		return true;
	}
    return false;
    }
    /*
     * 修改用户密码
     */
    function updata_password($id,$pwd){
        $info =array ();
        $info['password'] = md5($pwd);
        $result = $this->db->update($this->table, $info, "id='$id'");
		if($result){
			return true;
		}
        return false;
    }
    /**
     * 根据用户ID,获得某个用户的信息
     *
     * @param CHAR $fields
     * @param INT $userid
     * @param BOOL $ismore
     * @return ARRAY
     */
	function get($id, $fields = '*'){
		$userid = intval($id);
		return $this->db->get_one("SELECT $fields FROM $this->table WHERE id='$userid'");
	}
/**
	 * 查询用户信息
	 *
	 * @param string $where
	 * @param char $order
	 * @param int $page
	 * @param int $pagesize
	 * @return 所有用户信息
	 */
	function listinfo($where = '', $order = '', $page = 1, $pagesize = 100){
		$limit = $result = '';
		if($where) $where = "$where";
		if($order) $order = " ORDER BY $order";
		$page = max(intval($page), 1);
        $offset = $pagesize*($page-1);
        $limit = " LIMIT $offset, $pagesize";
		$array = array();
		$sql = "SELECT * FROM $this->table $where $order $limit";
		$result = $this->db->query($sql);
		while($r = $this->db->fetch_array($result)){			
			$array[] = $r;
		}
      	$this->db->free_result($result);
		return $array;
	}

    
        /**
	 * 注册方法
	 *
	 * @param ARRAY $info
	 * @param ARRAY $moreinfo
	 * @return INT用户ID
	 */
	function register($info = ARRAY()){
        $adminfo = ARRAY();
        $adminfo['username'] = $info['username'];
        $adminfo['password'] = md5($info['password']);
        $this->db->insert($this->table, $adminfo);
        $userid = $this->db->insert_id();
		if(!$userid){
			unset($adminfo,$info);
			return false;
		}
        return $userid;
	}
	function is_username($username){		
		if($this->is_badword($username) || !preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $username)){					
			return false;		
		}		
		return true;
	}

	function username_exists($username){	
		$result = $this->db->get_one("SELECT id FROM $this->table WHERE username='$username'");		
		if($result){				
			return false;		
		}		
		return TRUE;
	}

	function is_password($password){
		$strlen = strlen($password);				
		if($strlen >= 4 && $strlen <= 20){ 			
			return true;		
		}				
		return false;		
	}

	function is_badword($string){
		$badwords = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n","#");
		foreach($badwords as $value){
			if(strpos($string, $value) !== FALSE){
				return TRUE;
			}
		}
		return FALSE;
    }
}
?>