<?php
/**
 * 会员注册
 */
namespace Phonegame\Controller;
use Common\Controller\PhonegameController;
class LoginController extends PhonegameController {
	
	function index(){
	    if(sp_is_user_login()){ //已经登录时直接跳到首页
	        redirect(__ROOT__."/");
	    }else{
            $_SESSION['login_http_referer']=$_SERVER["HTTP_REFERER"];
	        $this->display($this->template.'/template1'.'/User'.'/login');
	    }
	}
	
	function active(){
		$this->check_login();
		$this->display($this->template.'/template1'.'/User'.'/active');
	}
	
	function doactive(){
		$this->check_login();
		$this->_send_to_active();
		$this->success('激活邮件发送成功，激活请重新登录！',U("user/logout"));
	}
    /**
     * 手机找回密码
     * @author 顽皮蛋 <shf_l@163.com>
     */
	public function forgetPhone(){
        $this->display($this->template.'/template1'.'/User'.'/forgetPhone');
    }

    /**
     * 修改密码
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function doforgotPhone(){
       if(IS_POST){
           $users_model=M("Users");
           $rules = array(
                //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
                array('password', 'require', '密码不能为空！', 1 ),
                array('repassword', 'require', '重复密码不能为空！', 1 ),
                array('repassword','password','确认密码不正确',0,'confirm'),
            );
            if($users_model->validate($rules)->create()===false){
                $this->error($users_model->getError());
            }else{
                $password=sp_password(I("post.password"));
                $code=I("post.code");//验证码
                $user_login=I("post.user_login");//验证码
                $tel=I("post.tel");//验证码
                if($code){
                    $time = time()-2*60;
                    $flag = M('smslog')->where('tel ='.$tel.' and time >='.$time)->find();
                    if($code != $flag['code']){
                        $this->error('短信验证码错误或者已经过期！');      
                    }else{
                        $map['user_login'] = $user_login;
                        $map['tel'] = $tel;
                        $result=$users_model->where($map)->save(array("user_pass"=>$password));
                        if($result){
                            $this->success("密码重置成功，请登录！",U("user/login/index"));
                        }else {
                            $this->error("密码重置失败，重置码无效！");
                        }
                    }  
                }else{
                    $this->error('短信验证码不能为空');    
            } 
                    
          }
        }

    }

	function forgot_password(){
		$this->display($this->template.'/template1'.'/User'.'/forgot_password');

	}

	/*检查邮箱是否正确*/
	public function chekEmail(){
        $users_model=M("Users");
        $rules = array(
            //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
            array('email', 'require', '邮箱不能为空！', 1 ),
            array('email','email','邮箱格式不正确！',1), // 验证email字段格式是否正确
        );
        if($users_model->validate($rules)->create()===false){
            $sdata['state'] = '-1';
            $sdata['msg'] = $users_model->getError();
            exit(json_encode($sdata));
        }else{
            $sdata['state'] = '1';
            exit(json_encode($sdata));
        }
    }
	function doforgot_password(){
		if(IS_POST){
				$users_model=M("Users");
				$rules = array(
						//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
						array('email', 'require', '邮箱不能为空！', 1 ),
						array('email','email','邮箱格式不正确！',1), // 验证email字段格式是否正确
						
				);
				if($users_model->validate($rules)->create()===false){
					$this->error($users_model->getError());
				}else{
					$email=I("post.email");
					$find_user=$users_model->where(array("user_email"=>$email))->find();
					if($find_user){
						$this->_send_to_resetpass($find_user);
						$this->success("密码重置邮件发送成功！",__ROOT__."/");
					}else {
						$this->error("账号不存在！");
					}
					
				}
			
		}
	}
	
	protected  function _send_to_resetpass($user){
		$options=get_site_options();
		//邮件标题
		$title = $options['site_name']."密码重置";
		$uid=$user['id'];
		$username=$user['user_login'];
		$activekey=md5($uid.time().uniqid());
		$users_model=M("Users");
	
		$result=$users_model->where(array("id"=>$uid))->save(array("user_activation_key"=>$activekey));
		if(!$result){
			$this->error('密码重置激活码生成失败！');
		}
		//生成激活链接
        $webhost = parseHost($_SERVER["HTTP_HOST"]);
        $url = 'http://'.$webhost['2'].U('/login/password_reset',array("hash"=>$activekey));
        //邮件内容
		$template =<<<hello
		#username#，你好！<br>
		请点击或复制下面链接进行密码重置：<br>
		<a href="http://#link#">http://#link#</a>
hello;
		$content = str_replace(array('http://#link#','#username#'), array($url,$username),$template);
	
		$send_result=sp_send_email($user['user_email'], $title, $content);
		if($send_result['error']){
			$this->error('密码重置邮件发送失败！');
		}
	}
	
	
	function password_reset(){
        $this->display($this->template.'/template1'.'/User'.'/password_reset');
	}
	
	function dopassword_reset(){
		if(IS_POST){
				$users_model=M("Users");
				$rules = array(
						//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
						array('password', 'require', '密码不能为空！', 1 ),
						array('repassword', 'require', '重复密码不能为空！', 1 ),
						array('repassword','password','确认密码不正确',0,'confirm'),
						array('hash', 'require', '重复密码激活码不能空！', 1 ),
				);
				if($users_model->validate($rules)->create()===false){
					$this->error($users_model->getError());
				}else{
					$password=sp_password(I("post.password"));
					$hash=I("post.hash");
					$result=$users_model->where(array("user_activation_key"=>$hash))->save(array("user_pass"=>$password,"user_activation_key"=>""));
					if($result){
						$this->success("密码重置成功，请登录！",U("/login/index"));
					}else {
						$this->error("密码重置失败，重置码无效！");
					}
					
				}
		}
	}
	
	
    //登录验证
    function dologin(){
    	$users_model=M("Users");
    	$rules = array(
    			//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
    			array('username', 'require', '用户名或者邮箱不能为空！', 1 ),
    			array('password','require','密码不能为空！',1),
    	
    	);
    	if($users_model->validate($rules)->create()===false){
    		$this->error($users_model->getError());
    	}
    	extract($_POST);
    	if(strpos($username,"@")>0){//邮箱登陆
    		$where['user_email']=$username;
    	}else{
    		$where['user_login']=$username;
    	}
    	$users_model=M('Users');
    	$result = $users_model->where($where)->find();
    	/*判断该用户是否被拉黑*/
    	if($result['user_status']=='0'){
			$this->error("该用户已经管理员禁用！");
    	}
    	$ucenter_syn=C("UCENTER_ENABLED");
    	$ucenter_old_user_login=false;
    	$ucenter_login_ok=false;
    	if($ucenter_syn){
    		setcookie("thinkcmf_auth","");
    		include UC_CLIENT_ROOT."client.php";
    		list($uc_uid, $username, $password, $email)=uc_user_login($username, $password);
    		if($uc_uid>0){
    			if(!$result){
    				$data=array(
    						'user_login' => $username,
    						'user_email' => $email,
                            'user_nicename'=> $username,
    						'user_pass' => sp_password($password),
    						'last_login_ip' => get_client_ip(0,true),
    						'create_time' => time(),
    						'last_login_time' => time(),
    						'user_status' => '1',
                            'user_type' =>'2'
    				);
    				$id= $users_model->add($data);
                    M('users_other')->add(array('uid'=>$id));
    				$data['id']=$id;
    				$result=$data;
    			}
    		}else{
    			switch ($uc_uid){
    				case "-1"://用户不存在，或者被删除
    					if($result){//本应用已经有这个用户
    						if(sp_compare_password($password, $result['user_pass'])){//本应用已经有这个用户,且密码正确，同步用户
    							$uc_uid2=uc_user_register($username, $password, $result['user_email']);
    							if($uc_uid2<0){
    								$uc_register_errors=array(
    										"-1"=>"用户名不合法",
    										"-2"=>"包含不允许注册的词语",
    										"-3"=>"用户名已经存在",
    										"-4"=>"Email格式有误",
    										"-5"=>"Email不允许注册",
    										"-6"=>"该Email已经被注册",
    								);
    								$this->error("同步用户失败--".$uc_register_errors[$uc_uid2]);
    							}
    							$uc_uid=$uc_uid2;
    						}else{
    							$this->error("密码错误！");
    						}
    					}
    					break;
    				case -2://密码错
    					if($result){//本应用已经有这个用户
    						if(sp_compare_password($password, $result['user_pass'])){//本应用已经有这个用户,且密码正确，同步用户
    							$uc_user_edit_status=uc_user_edit($username,"",$password,"",1);
    							if($uc_user_edit_status<=0){
    								$this->error("登陆错误！");
    							}
    							list($uc_uid2)=uc_get_user($username);
    							$uc_uid=$uc_uid2;
    							$ucenter_old_user_login=true;
    						}else{
    							$this->error("密码错误！");
    						}
    					}else{
    						$this->error("密码错误！");
    					}
    					break;
    			}
    		}
    		$ucenter_login_ok=true;
    		echo uc_user_synlogin($uc_uid);
    	}
    	//exit();
    	if($result != null)
    	{
    		if(sp_compare_password($password, $result['user_pass'])|| $ucenter_login_ok){

                $uresut['id'] = $result["id"];
                $uresut['user_login'] = $result["user_login"];
                $uresut['user_email'] = $result["user_email"];
                $uresut['score'] = $result["score"];
                $uresut['money'] = $result["money"];
                $uresut['last_login_ip'] = $result["last_login_ip"];
                $uresut['last_login_time'] = $result["last_login_time"];
                $uresut['avatar'] = $result["avatar"]?$result["avatar"]:'headicon_128.png';
    			$_SESSION["user"]=$uresut;
    			//写入此次登录信息
    			$data = array(
    					'last_login_time' => date("Y-m-d H:i:s"),
    					'last_login_ip' => get_client_ip(0,true),
    			);
    			$users_model->where("id=".$result["id"])->save($data);
    			$redirect=empty($_SESSION['login_http_referer'])?__ROOT__."/":$_SESSION['login_http_referer'];
    			$_SESSION['login_http_referer']="";
    			$ucenter_old_user_login_msg="";
    			if($ucenter_old_user_login){
    				//$ucenter_old_user_login_msg="老用户请在跳转后，再次登陆";
    			}
                if(empty($ucenter_syn)){
                   
                   $authckey = C('AUTHCODE');
                   setcookie('thinkcmf_auth', sp_authcode($result["id"]."\t".$result["user_login"], 'ENCODE',$authckey), time()+3600*24*7, C ( 'COOKIE_PATH' ), C ( 'COOKIE_DOMAIN' ),false); 
                }
    			$this->success("登录验证成功！", $redirect);
    		}else{
    			$this->error("密码错误！");
    		}
    	}else{
    		$this->error("用户名不存在！");
    	}
    	 
    }
   //登录验证
   public function ajaxLogin(){
        $users_model=M("Users");
        $rules = array(
            //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
            array('username', 'require', '用户名或者邮箱不能为空！', 1 ),
            array('password','require','密码不能为空！',1),
        );
        if($users_model->validate($rules)->create()===false){
            $rearr['msg'] = $users_model->getError();
            $rearr['status'] = '2';
            $this->ajaxReturn($rearr);
        }
        extract($_POST);
        if(strpos($username,"@")>0){//邮箱登陆
            $where['user_email']=$username;
        }else{
            $where['user_login']=$username;
        }
        $users_model=M('Users');
        $result = $users_model->where($where)->find();
        /*判断该用户是否被拉黑*/
    	if($result['user_status']=='0'){
			$rearr['msg'] = "该用户已经管理员禁用！";
			$rearr['status'] = '2';
            $this->ajaxReturn($rearr);
    	}
        $ucenter_syn=C("UCENTER_ENABLED");
        $ucenter_old_user_login=false;
        $ucenter_login_ok=false;
        if($ucenter_syn){
            setcookie("thinkcmf_auth","");
            include UC_CLIENT_ROOT."client.php";
            list($uc_uid, $username, $password, $email)=uc_user_login($username, $password);
            if($uc_uid>0){
                if(!$result){
                    $data=array(
                            'user_login' => $username,
                            'user_email' => $email,
                            'user_nicename'=> $username,
                            'user_pass' => sp_password($password),
                            'last_login_ip' => get_client_ip(0,true),
                            'create_time' => time(),
                            'last_login_time' => time(),
                            'user_status' => '1',
                            'user_type' =>'2'
                    );
                    $id= $users_model->add($data);
                    M('users_other')->add(array('uid'=>$id));
                    $data['id']=$id;
                    $result=$data;
                }
            }else{
                switch ($uc_uid){
                    case "-1"://用户不存在，或者被删除
                        if($result){//本应用已经有这个用户
                            if(sp_compare_password($password, $result['user_pass'])){//本应用已经有这个用户,且密码正确，同步用户
                                $uc_uid2=uc_user_register($username, $password, $result['user_email']);
                                if($uc_uid2<0){
                                    $uc_register_errors=array(
                                            "-1"=>"用户名不合法",
                                            "-2"=>"包含不允许注册的词语",
                                            "-3"=>"用户名已经存在",
                                            "-4"=>"Email格式有误",
                                            "-5"=>"Email不允许注册",
                                            "-6"=>"该Email已经被注册",
                                    );
                                    $rearr['msg'] = "同步用户失败--".$uc_register_errors[$uc_uid2];
                                    $rearr['status'] = '2';
                                    $this->ajaxReturn($rearr);
                                }
                                $uc_uid=$uc_uid2;
                            }else{
                                $rearr['msg'] = "密码错误！";
                                $rearr['status'] = '2';
                                $this->ajaxReturn($rearr);
                            }
                        }
                        break;
                    case -2://密码错
                        if($result){//本应用已经有这个用户
                            if(sp_compare_password($password, $result['user_pass'])){//本应用已经有这个用户,且密码正确，同步用户
                                $uc_user_edit_status=uc_user_edit($username,"",$password,"",1);
                                if($uc_user_edit_status<=0){
                                    $rearr['msg'] = "登陆错误！";
                                    $rearr['status'] = '2';
                                    $this->ajaxReturn($rearr);
                                }
                                list($uc_uid2)=uc_get_user($username);
                                $uc_uid=$uc_uid2;
                                $ucenter_old_user_login=true;
                            }else{
                                $rearr['msg'] = "密码错误！";
                                $rearr['status'] = '2';
                                $this->ajaxReturn($rearr);
                            }
                        }else{
                            $rearr['msg'] = "密码错误！";
                            $rearr['status'] = '2';
                            $this->ajaxReturn($rearr);
                        }
                        break;
                }
            }
            $ucenter_login_ok=true;
           $ucsynlogin = uc_user_synlogin($uc_uid);
           //echo  $ucsynlogin;
        }
        //exit();
        if($result != null)
        {
            if(sp_compare_password($password, $result['user_pass'])|| $ucenter_login_ok){
                $uresut['id'] = $result["id"];
                $uresut['user_login'] = $result["user_login"];
                $uresut['user_email'] = $result["user_email"];
                $uresut['score'] = $result["score"];
                $uresut['money'] = $result["money"];
                $uresut['last_login_ip'] = $result["last_login_ip"];
                $uresut['last_login_time'] = $result["last_login_time"];
                $uresut['avatar'] = $result["avatar"];
                $_SESSION["user"]=$uresut;
                //写入此次登录信息
                $data = array(
                        'last_login_time' => date("Y-m-d H:i:s"),
                        'last_login_ip' => get_client_ip(0,true),
                );
                $users_model->where("id=".$result["id"])->save($data);
                $redirect=empty($_SESSION['login_http_referer'])?__ROOT__."/":$_SESSION['login_http_referer'];
                $_SESSION['login_http_referer']="";
                $ucenter_old_user_login_msg="";
                if($ucenter_old_user_login){
                    //$ucenter_old_user_login_msg="老用户请在跳转后，再次登陆";
                }
                $rearr['msg'] = "登录验证成功！";
                $rearr['status'] = '1';
                $rearr['script'] = $ucsynlogin;
                /*获取用户登录记录*/
                $id=$result["id"];//I("get.id");
                $users_model=M("Users");
                $user=$users_model->where(array("id"=>$id))->find();               
                
                  $rearr['url']='/themes/main_template/Public/images/vip_icon_un.png';
                
                $rearr['username'] = $result["user_login"];
                $rearr['avatar'] = $result["avatar"]?$result["avatar"]:'headicon_128.png';
                if(empty($ucenter_syn)){
                   
                   $authckey = C('AUTHCODE');
                   setcookie('thinkcmf_auth', sp_authcode($result["id"]."\t".$result["user_login"], 'ENCODE',$authckey), time()+3600*24*7, C ( 'COOKIE_PATH' ), C ( 'COOKIE_DOMAIN' ),false); 
                }
                $this->ajaxReturn($rearr);
            }else{
                $rearr['msg'] = "密码错误！";
                $rearr['status'] = '2';
                $this->ajaxReturn($rearr);
            }
        }else{
            $rearr['msg'] = "用户名不存在！";
            $rearr['status'] = '0';
            $this->ajaxReturn($rearr);
        }
         
    }


}