<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace temmoku\lib;
use temmoku\db;
use temmoku\lib\cookie;
class user{
	public function Login($username,$password){
	  	$User=self::GET_User_Name($username);
		if('0'==$User['code']){
			$password=md5(md5($password).$User['data']['rand']);
			if($User['data']['password']==$password){
				db::update(jab."user")->cols(['lasttime'=>NOWTIME,'lastip'=>onlineip])->where('uid='.$User['data']['uid'])->query();
				$User['data']['lasttime']=NOWTIME;
				return array("code"=>'0','text'=>"登录成功",'UserDB'=>$User['data']);
			}else{
				return array("code"=>'21','text'=>"密码错误");
			}
		}else{
			return $User;
		}
	}
	//通过用户名获取用户信息
	public static function GET_User_Name($username){
		$username=htmlspecialchars($username,ENT_QUOTES);
		$User=db::select('*')->from(jab."user")->where("username LIKE '$username'")->row();
		if($User){
			return array('code'=>'0','text'=>"存在的用户",'data'=>$User);
		}else{
			return array('code'=>'20','text'=>"不存在的用户");
		}
	}
	
	//通过用户UID获取用户信息
	public function get_user_uid($uid){
		$uid=intval($uid);
		$User=db::select('*')->from(jab."user")->where("uid = '$uid'")->row();
		if($User){
			return array('code'=>'0','text'=>"存在的用户",'data'=>$User);
		}else{
			return array('code'=>'20','text'=>"不存在的用户");
		}
	}
	
	//通过cookie获取用户信息
	public function Cookie_Login($uid,$password){
		$user=$this->get_user_uid($uid);
		if($user['code']==0 && $user['data']['password']==$password){
			return array("code"=>'0','text'=>"登录成功",'UserDB'=>$user['data']);
		}else{
			return array("code"=>'21','text'=>"密码错误");
		}
	}
	/* 
		判断用户是否存在
	*/
	public function User_Check(){
    	$ORDINARY_MEMBER=cookie::get('ORDINARY_MEMBER');
    	if($ORDINARY_MEMBER){
			if(json_encode($ORDINARY_MEMBER)){
				$ORDINARY_MEMBER=decode($ORDINARY_MEMBER);
				list($uid,$password,$onlineip)=$r =explode(' ',$ORDINARY_MEMBER);
				if(!intval($uid) || !$password || C('ONLINEIP') != $onlineip || !$onlineip){
					cookie::del('ORDINARY_MEMBER');
					return;
				}
				$data = $this->Cookie_Login($uid,$password);
				if('0'===$data['code']){
					 $data['UserDB']['thumb']=$data['UserDB']['thumb'] ? get_img_url($data['UserDB']['thumb']) :'';
					C('MYDB',$data['UserDB']);
					$group=C('USER_GROUP.'.$data['UserDB']['groupid']);
					$group['setting']=unserialize($group['setting']);
					C('MYGROUP',$group);
					return $data['UserDB'];
				}else{
					cookie::del('ORDINARY_MEMBER');
					return;
				}
			}else{
				cookie::del('ORDINARY_MEMBER');
				return;
			}
		}
    }
	

}
?>