<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Findpassword extends MY_Controller {

        function __construct()
        {
          parent::__construct();
          $this->load->model("user_model");
          $this->load->library("user_class");
          $this->load->helper("template_helper");		  
        }


        /**
         * 找回密码
         * @author cxf
        */
        public function index()
        {
			$_SESSION['token'] = md5($this->user_id);  
            $data['title'] = '用户找回密码'; 

            if($_POST)
			{
				if($_POST['token'] != $_SESSION['token'])
				{
                    //echo 2;exit();
					//echo $_POST['token']."<br/>";
					//echo $_SESSION['token']."<br/>";
				}
				$telno = $this->input->post('telno');
				$password = $this->input->post('setNewPassword');
				$againpassword = $this->input->post('repetPassword');
				$validcode = $this->input->post('validcode');
				$kind = $this->input->post('kind');
                
                
                //防止暴力破解，增加校验规则
                $keyphone  = "findpassword_".$telno;
                $checkCount = $this->memcached_class->get($keyphone);
                if(empty($checkCount)) $checkCount = 0;
                $checkCount = (int)$checkCount;
                if($checkCount >= 5){
                    //次数过多，等待
                    echo 10;
                    exit();
                }
                $this->memcached_class->delete($keyphone);
                $this->memcached_class->add($keyphone,$checkCount+1,60);
            
                
				if($password==$againpassword)
				{				
				
					//查找全站个人用户中心有无此号码 xue add
					$ucenterTel = $this->user_model->ucenterCheckTelno($telno);
					//验证二手房有无此号码
					$result2 = $this->user_model->yztelno($telno);
					//全站个人会员中心和二手房都有此用户
					if($ucenterTel&&$result2>0)
					{
						$ucenterReturn = $this->user_model->ucenterForgetpwd($telno,md5($password),$validcode);	     
						if($ucenterReturn['result']==1)
						{
							//修改二手房密码
							$result_pass = $this->user_model->updatepassowrd($telno,$password);
							if($result_pass>0)
							{
								//登录个人会员中心
								$ucenter_result = $this->user_model->ucenterLogin($telno,md5($password));
								//登录二手房
								$this->user_class->user_login($result2,md5($password),$telno);
								echo 1;exit();
							 }
						}else{
						 //修改密码失败
					     echo 3;
						 exit();
					 }
					//全站有此用户，二手房没有
					}elseif($ucenterTel&&$result2==0)
					{
						$ucenterReturn = $this->user_model->ucenterForgetpwd($telno,md5($password),$validcode);
						if($ucenterReturn['result']==1)
						{
							  //插入二手房数据
							  $passport_uid = $ucenterReturn['data']['passport_uid'];
							  $password = md5($password2);
							  $telno = $telno;
							  $city = $ucenterReturn['data']['city'];
							  $email = $ucenterReturn['data']['email'];
							  $username = $ucenterReturn['data']['username'];
							  $nickname = $ucenterReturn['data']['nickname'];
							  $avatar = $ucenterReturn['data']['avatar'];
							  $ip = $ucenterReturn['data']['ip'];
							  $insert_return = $this->user_model->addUserFromUcenter($passport_uid,md5($password),$telno,$city,$email,$username,$nickname,$avatar,$ip);
							  if($insert_return>0)
							  {
									 //登录个人会员中心
									$ucenter_result = $this->user_model->ucenterLogin($telno,md5($password2));
									//登录二手房
									$this->user_class->user_login($result2,md5($password),$telno);
									echo 1;exit(); 
							  }
						
						}else{
							 //修改密码失败
							 echo 3;
							 exit();					
						}				    
					
					}elseif(!$ucenterTel&&$result2>0){
						//modify xue 2015-03-11
						//验证手机验证码
						$result = $this->user_model->yzusersms($validcode,$telno,$kind);		  
						if($result>0)
						{
						  $result_pass = $this->user_model->updatepassowrd($telno,$password);					   
						  if($result_pass>0)
						  {
							 $this->user_class->user_login($result2,md5($password),$telno);
							 echo 1;exit;
						  }else{
							 //更新密码失败
							echo 3;exit;
						  }
						}else{
						  //短信验证不通过
						  echo 2;exit;
						}

						//（个人会员中心无此号码，二手房有这个号码）记录日志
						//$msg = date('Y-m-d H:i:s').":".$telno." \n";
						//@file_put_contents('./forgetpasswrod_err',$msg,FILE_APPEND);
						echo 4;exit();
					}elseif(!$ucenterTel&&$result2==0){
						echo 4;exit();
					}
              }
          }
         $this->load->view('findpassword',$data);
        }

}
?>
