<?php
/*
'#################################################################
'# 程序名称：中环CMSV2.0
'# 授权方式：免费软件
'# 作者：善
'# 联系QQ:9118847
'# 作者网站:http://www.ps173.com
'# 发布时间：2015-6-10
'# 声明：欢迎广大的PHP爱好者一起共同探讨
'# 声明：各种用途均可免费使用、自由传播，但必须保留程序代码中的版权信息。
'#################################################################
 */

//用户模型类
class UserModel extends Model{
		//成员初始化
	public function __construct(){
		parent::__construct();
	    $this->tabname = TAB_FIX."user";
		$this->tabkey = array("id", "gid", "user", "passwd", "email", "regtime", "disable");
	}

	public function logout(){
		$_SESSION = array();

		if(isset($_COOKIE[session_name()])){
			setCookie(session_name(), '', time()-3600, '/');
		}

		session_destroy();
	}
		
	//用户前台登陆
	public function userlogin($user, $passwd){
		$user = $this->field('id, gid, user, email, disable')->where(array("user"=>$user, "passwd"=>substr(md5($passwd), 5, 20)))->one();
		if(isset($user) && $user['disable'] === 0){
			$group = D("group");
			$guser = $group->manage($user);
			$_SESSION = $guser;
			$_SESSION['is_login'] = 1;
			$_SESSION['authid'] = $user['id'];
			unset($_SESSION['id']);
			return 2;
		}else if($user['disable'] === 1){
			return 1;
		}else{
			return 0;
		}

	}
	//注册用户
	public function adduser($post){
		if(!empty($post)){
			$data['gid'] = 2;
			$data['regtime'] = time();
			$data['user'] = $post['username'];
			$data['passwd'] = substr(md5($post['password']), 5, 20);
			$data['email'] = $post['email'];
		}

		return $this->add($data);
	}
	//检测用户是否存在
	public function check_total($where){
		return $this->total($where);
	}

		//通过传递进来的用户名和密码返回ID
	public function checkpwd($user){
		return $this->field("id")->where(array("user"=>$user['user'],  "passwd"=>substr(md5($user['passwd']), 5, 20)))->one();
		
	}
		//修改用户密码
	public function changuser($post){
		$post['passwd'] = substr(md5($post['passwd']), 5, 20);
		return $this->mod($post);
		
	}
		//删除用户
	public function delete($id){
		return $this->del($id);
	}
		//修改用户状态
	public function setuser($post){
		if($post['disable'] == 0){
			$post['disable'] = 1;
		}else if($post['disable'] == 1){
			$post['disable'] = 0;
		}else{
			return false;
		}
		
		$this->mod($post);
	}

	//获许用户总数
	public function user_total($gid){
		return  $this->total($this->checkid($gid));

	}
		//查找用户数量和用户所在分组，并输出分页
	public function user_all($limit, $gid){
		return $this->field("id, gid, user, email, regtime, disable")->where($this->checkid($gid))->order("id desc")->limit($limit)->multi(array("group", 'groname, info', 'id', 'gid'));
	
	}
		//根据提交过来的ID输出用户信息
	public function get_one($id){
		return $this->field("id, gid, user, email, disable")->where(array("id"=>$id))->one();
	}
		//添加用户或修改用户的密码
	public function getuser($post){
		if(isset($post) && !empty($post['passwd'])){
			unset($post['newpasswd']);
			$post['passwd'] = substr(md5($post['passwd']), 5, 20);
		}

		return $post;
	}
		//检测GID是否被设置
	public function checkid($id){
		if(isset($id)){
			$id = array("gid"=>$id);
		}else{
			$id = null;
		}

		return $id;
	}

		//修改用户
	public function update($post){
		$post = $this->getuser($post);
		if($post['id'] == 1){
			if($post['gid'] != 1 || $post['disable'] !=0){
				val::mess('不允许改变该用户所在分组，或者禁用该用户');
				return false;
			}
		}
		
		return $this->mod($post);
	}
		//添加用户
	public function insert($post){
		$post['regtime'] = time();
		$post = $this->getuser($post);
		return $this->add($post);
	}
		//检测用户是否存在
	public function checkuser($user){
		return $this->field("id")->where(array("user"=>$user))->one();
	}
		//用户所在分组
	public function select($name = "gid", $value = "0", $change =""){
		$group = D("group");
		$str = $group->field("id, groname")->all();
		$html ='<select '.$change.' id="sel" name="'.$name.'">';
		$html .='<option value="0">--选择用户组--</option>';
		foreach ($str as $val) {
			if($value == $val['id'])
				$html .='<option selected value="'.$val['id'].'">';
			else
				$html .='<option value="'.$val['id'].'">';

			$html.=$val['groname'];
			$html.='</option>';
		}
		$html .='</select>';
		return $html;
	}
		//后台登陆检测
	public function login($user, $passwd){
		if(strtoupper($_POST['code']) == $_SESSION['code']){
			$user = $this->field('id, gid, user, email, disable')->where(array("user"=>$user, "passwd"=>substr(md5($passwd), 5, 20)))->one();
			if(isset($user) && $user['disable'] === 0){
				$group = D("group");
				$guser = $group->manage($user);
				if($guser['login'] !== 1){ 
				 	val::mess('抱歉，你没有登陆后台的权限');
				 	return false;
				}else{
					$_SESSION = $guser;
					$_SESSION['is_login'] = 1;
					$_SESSION['authid'] = $user['id'];
					unset($_SESSION['id']);
					return 2;
				}
			}else if($user['disable'] === 1){
				return 1;
			}else{
				return 0;
			}
		}else{
			val::mess('验证码不正确');
		}
	}
}

?>