<?php
if(!defined('IN_TM')) exit('Access Denied');
class member{
	var $db;
	var $table;
    var $table_cache;
	var $table_info;
	var $table_admin;
    var $_userid;
    var $_username;
	var $pages;
    var $TM;

    function __construct(){
    	global $db,$TM;
        $this->db = &$db;
        $this->table_admin = DB_PRE.'admin';
        $this->table = DB_PRE.'member';
        $this->table_cache = DB_PRE.'member_cache';
        $this->table_info = DB_PRE.'member_info';
        $this->TM = $TM;
        cache_member();
    }

    function member(){
        $this->__construct();
    }
    function msg(){
		global $LANG;
		return $LANG[$this->msg];
    }
    /*
     * 修改用户资料
     */
    function updata_info($userid,$info){
        $result = $this->db->update($this->table_info, $info, "userid='$userid'");
	if($result){
		return true;
	}
        return false;
    }
    /*
     * 修改用户密码
     */
    function updata_password($userid,$pwd,$newpwd){
	$r = $this->db->get_one("SELECT * FROM `".DB_PRE."member` WHERE `userid`=$userid");
        if(!$r){
            $this->msg = 'username_not_exist';
            return FALSE;
        }
        $md5_password = md5($pwd);
        if($r['password'] != $md5_password){
            $this->msg = 'old password_not_right';
             return FALSE;
        }
        $info =array ();
        $info['password'] = md5($newpwd);
        $result = $this->db->update($this->table, $info, "userid='$userid'");
	if($result){
		return true;
	}
        return false;
    }
    /**
     * 根据用户名，获得用户ID
     *
     * @param CHAR $username
     *
     * @return $user
     */
	function get_userid($username){
		if(!$this->is_username($username)) return false;
		$sql = "SELECT userid FROM $this->table_cache WHERE username='$username' LIMIT 1";
		$user = $this->db->get_one($sql);
		return $user['userid'];
	}

    /**
     * 根据用户ID,获得某个用户的信息
     *
     * @param CHAR $fields
     * @param INT $userid
     * @param BOOL $ismore
     * @return ARRAY
     */
	function get($userid, $fields = '*', $ismore = 0){
		$userid = intval($userid);
		$sql = $ismore ? "SELECT $fields FROM $this->table_cache m LEFT JOIN $this->table_info i ON m.userid=i.userid WHERE m.userid='$userid'" : "SELECT $fields FROM $this->table_cache WHERE userid='$userid'";
		return $this->db->get_one($sql);
	}
	/**
	 * 检查用户名是否存在
	 * @param STRING $username
	 * @return $username
	 */
	function username_exists($username, $userid=''){
		if(!isset($username) && empty($username)){
			return false;
		}
		$result = $this->db->get_one("SELECT userid FROM $this->table_cache WHERE username='$username' AND userid!='$userid'");
		if($result){
			$this->msg = 'have_registered';
			return false;
		}
		return $username;
	}
	/**
	 * 检查用户名是否符合规定
	 * @param STRING $username
	 * @return 	TRUE or FALSE
	 */
	function is_username($username){
		$strlen = strlen($username);
		if($this->is_badword($username) || !preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $username)){
			$this->msg = 'username_not_accord_with_critizen';
			return false;
		}elseif ( 20 <= $strlen || $strlen < 2 ){
			$this->msg = 'username_not_less_than_3_longer_than_20';
			return false;
		}
		return true;
	}
        /**
	 * 检测输入中是否含有错误字符
	 *
	 * @param char $string
	 * @return TRUE or FALSE
	 */
	function is_badword($string){
		$badwords = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n","#");
		foreach($badwords as $value){
			if(strpos($string, $value) !== FALSE){
				return TRUE;
			}
		}
		return FALSE;
    }
    /*
     * 修改用户姓名
     */
    function updata_truename($userid,$truename){
        $result = $this->db->query("UPDATE $this->table_info SET `truename` = '$truename' WHERE userid =$userid ;");
	if($result){
		return true;
	}
        return false;
    }
	/**
	 * 检查邮件是否存在
	 *
	 * @param STRING $email
	 *
	 * @return $email
	 */
	function email_exists($email, $userid = ''){
		$result = $this->db->get_one("SELECT userid FROM $this->table_cache WHERE email='$email' AND userid!='$userid'");
		if($result){
			$this->msg = 'have_used_change_one_email';
			return true;
		}
		return false;
	}
        /**
	 * 检查密码长度是否符合规定
	 *
	 * @param STRING $password
	 * @return 	TRUE or FALSE
	 */
	function is_password($password){
		$strlen = strlen($password);
		if($strlen >= 4 && $strlen <= 20) return true;
		return false;
	}
        /**
	 * 用户注册检测函数
	 *
	 * @param ARRAY $info
	 * @return unknown
	 */
    function register_check($info){
		if(!($this->TM['member_allowregister'])){
			$this->msg = 'sorry_new_register_not_allowed';
			return false;
		}
		if(!is_array($info)){
			$this->msg = 'username_not_accord_with_critizen';
			return false;
		}
		if(!$this->username_exists($info['username'])){
			return false;
		}
		if(!$this->is_username($info['username'])){
			return false;
		}
		if(!$this->is_password($info['password'])){
			$this->msg = 'password_not_less_than_3_longer_than_20';
			return false;
		}
                if($info['password'] != $info['pwdconfirm']){
			$this->msg = 'Two password the importation is inconsistent';
			return false;
		}
		if(!is_email($info['email'])){
			$this->msg = 'input_valid_email';
			return false;
		}
		if($this->email_exists($info['email'])){
			return false;
		}
            return true;
        }
        /**
	 * 注册方法
	 *
	 * @param ARRAY $info
	 * @param ARRAY $moreinfo
	 * @return INT用户ID
	 */
	function register($info = ARRAY()){
        global $TM;
        if(!$this->register_check($info)) return false;
        $member_info = ARRAY();
        $member_info['username'] = $info['username'];
        $member_info['password'] = md5($info['password']);
        $member_info['groupid'] = 3;
        $member_info['email'] = $info['email'];
        $member_info['amount'] = $TM['member_defualtamount'];
        $member_info['disabled'] = 1;
        $this->db->insert($this->table, $member_info);
        $userid = $this->db->insert_id();
        $member_info['userid'] = $userid;
        $this->db->insert($this->table_cache, $member_info);
        $moreinfo = array ();
        $moreinfo['userid'] = $userid ;
        $moreinfo['regip'] = $moreinfo['lastloginip'] = IP;
        $moreinfo['regtime'] = $moreinfo['logintime'] = TIME;
        $this->db->insert($this->table_info, $moreinfo);
        unset($moreinfo,$member_info,$info);
        return $userid;
	}

	/**
	 * 用户退出方法
	 *
	 * @return TRUE
	 */
	function logout(){
		set_cookie('xt_auth', '');
		set_cookie('cookietime', '');
		unset($_SESSION);
		return true;
	}
	   /*
     * 用户登陆
     * -1 用户不存在
     * 0  密码错误
     * > 0 登陆成功返回用户uid
     */
    function login($username, $password, $cookietime = 0){
        $userid = $this->get_userid($username);
        	$r = $this->get($userid, '*', 1);
        	if(!$r){
        		$this->msg = 'username_not_exist';
        		return FALSE;
        	}
                $md5_password = md5($password);
        	if($r['password'] != $md5_password){
                    $this->msg = 'password_not_right';
                    return FALSE;
                }
                $this->_userid = $r['userid'];
                $this->_username = $r['username'];
        	if(!$cookietime) $get_cookietime = get_cookie('cookietime');
        	$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
        	$cookietime = $_cookietime ? TIME + $_cookietime : 0;
        	$tm_auth = xt_auth($this->_userid."\t".$this->_username."\t".$md5_password, 'ENCODE');
                set_cookie('xt_auth', $tm_auth, $cookietime);
            	set_cookie('cookietime', $_cookietime, $cookietime);
        	return $this->_userid;
        }
    /**
     * 验证是否是管理员
     *
     * @param CHAR $username
     *
     * @return $user
     */
    function get_adminid($adminid){
	$sql = "SELECT userid FROM $this->table_admin WHERE userid='$adminid' LIMIT 1";
	$user = $this->db->get_one($sql);
        if($user){
            return $user['userid'];
        }else{
            return false;
        }
    }
    /*
     * 后台登陆
     * -1 用户不存在
     * 0  密码错误
     * > 0 登陆成功返回用户uid
     */
    function admin_login($username, $password, $cookietime = 0){
        $userid = $this->get_userid($username);
        $r = $this->get($userid, '*', 1);
        if(!$r){
            $this->msg = 'username_not_exist';
            return FALSE;
        }
        $md5_password = md5($password);
        if($r['password'] != $md5_password){
            $this->msg = 'password_not_right';
                return FALSE;
            }
        if($this->get_adminid($r['userid'])){
            $_SESSION['adminid'] = $r['userid'];
            return True;
        }else{
            $this->msg = 'You are not an administrator bar';
            return FALSE;
        }
    }
}
?>