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

class Login extends MY_Controller {
     
    private $vaildcode_info;
    /**
    * 解析函数
    *
    * @access public
    * @return void
    */
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * Index Page for this controller.
     */
    public function index()
    {
        //加载broker模型类
        $this->load->model('broker_model');

        $is_online = $this->broker_model->check_online();
        
        $check_data = $username = $password = '';
        
        if($is_online)
        {
            if($this->isAjax())
            {
                echo 'check_ok';
            }
            else
            {
                $this->enter();
            }
        }
        else
        { 
            $action = $this->input->post('action', TRUE);
            if('check_login' == $action)
            {
                //提交验证  
                $check_data = $this->_post();
                
                if($check_data === TRUE)
                {
                    echo 'check_ok';
                }
                else
                {
                     echo $check_data;
                }
            }
            else
            {
                $data_arr['username'] = $username;
                $data_arr['password'] = $password;
				$data_arr['google_analytics'] = get_ga_js_code('/登陆页/');
                //加载模板
                $this->load->view('login', $data_arr);
            }
        }
    }

    /**
     * broker enter
     */
    public function enter()
    {
        //加载broker模型类
        $this->load->model('broker_model');

        $user_arr = $this->broker_model->get_user_session();
        $page_text = '';

        $nowtime = time();

        if($nowtime >= 1410224400)
        {
            if($user_arr["expiredate"] < date("Y-m-d", strtotime("+1 weeks")))
            {
              $page_text = "欢迎您使用租售宝！您的账户即将到期，请注意续费!";
            }
            else
            {
              $page_text = '尊敬的<span style="font-weight:bold;">'.$user_arr["truename"].'</span>，欢迎您使用租售宝V4.0版！';
            }

            //登陆成功，跳转至租售宝首页
            $this->jump(GB_WO_URL, $page_text);
        }
        else
        {
            $page_text = ' 尊敬的'.$user_arr["truename"].'您好，本次租售宝V4.0封闭测试已结束。<br />　　2014年9月9日(下周二)上午9点将重新开放公测。<br />　　感谢您对租售宝V4.0版的大力支持，祝您中秋快乐！';

            //清空数据
            $this->broker_model->logout();

            //登陆成功，跳转至租售宝首页
            $this->jump(GB_WO_URL.'/login/', $page_text, 6000);
        }
    }
    
    /**
     * broker logout
     */
    public function quit()
    {
        //加载broker模型类
        $this->load->model('broker_model');
        
        //清空数据
        $this->broker_model->logout();
        //跳转到登录页面
        $this->jump(GB_WO_URL.'/login/', '您已安全退出，期待您的再次使用！');
    }
    

    /**
     * broker find password
     */
    public function findpw(){      
        //加载broker模型类
        $this->load->model('broker_model');            
        $subusername = $this->input->post('subusername',TRUE);     
        $page_text ="";        
        if($subusername=='submit'){ 
            $this->jump(GB_WO_URL.'/login/findpw_code/');
            exit;
        }
        $data['page_text']= $page_text;     
        $this->load->view('findpw',$data);
    }
    
    
    /**
     * validate code
     */
    public function findpw_code()
    {        
        $telno = $this->session->userdata('vaildmoblie');
        $brokerid = $this->session->userdata('vaildbrokerid');
      
        if(!$telno||!$brokerid){     
            $this->jump(GB_WO_URL.'/login/findpw/');
        }
        $data['telno'] = $telno;
        $data['brokerid'] = $brokerid;       
        $this->load->view('findpw_code',$data); 
    }
    
    
    /**
     * confirm validcode
     */
    public function confirm_validcode()
    {
        $telno = $this->session->userdata('vaildmoblie');
        $brokerid = $this->session->userdata('vaildbrokerid');
        if($telno&&$brokerid){
            $session_data = array('vaildmoblie'=>'','vaildbrokerid'=>'');
            $this->session->unset_userdata($session_data); 
        }  else {
            $this->jump(GB_WO_URL.'/login/findpw/');
        }

        $this->load->view('confirm_validcode');
    }
    
    public function check()
    {
        //加载broker模型类
        $this->load->model('broker_model');
        $is_online = $this->broker_model->check_online();
        if($is_online) {
            
        } else {
            $this->redirectLogin();
        }
    }

    private function _enter($username, $password, $kind = 'cp')
    {
        //加载broker模型类
        $this->load->model('broker_model');

        //清空之前的登陆数据
        $this->broker_model->logout();

        if(strpos($_SERVER['HTTP_REFERER'], 'cp.house365.com') || strpos($_SERVER['HTTP_REFERER'], 'we.house365.com'))
        {
            //判断账号密码正确性
            $uid = $this->broker_model->login($username, $password, FALSE);

            if($uid > 0)
            {
                $this->broker_model->set_uid($uid);

                //获取经纪人信息
                $broker_info = $this->broker_model->get_broker_info();
                
                //验证无误，把用户基本信息存入SESSION，并记录登陆日志
                $this->broker_model->set_user_session($broker_info);

                $ip = get_ip(); //登陆IP
                $now = time(); //当前时间
                $log_data = array('kind'=>$kind, 'uid'=>$uid, 'logintime'=>$now, 'ip'=>$ip);
                $this->broker_model->log_broker_login($log_data);
                
                //登陆增加此COOKIE判断，防止串号
                $cookie = array(
                       'name'   => 'broker_id',
                       'value'  => $uid,
                       'expire' => 1800,    // 设置一天的有效期            
                );
                $this->input->set_cookie($cookie);

                $page_text = '正在进入'.$broker_info['truename'].'('.$broker_info['username'].')的租售宝...';
                //登陆成功，跳转至租售宝首页
                $this->jump(GB_WO_URL, $page_text);   
            }
            else
            {
                //跳转到登录页面
                $this->jump(GB_WO_URL.'/login/', '进入失败，请重试！');
            }
        }
        else
        {
            //跳转到登录页面
            $this->jump(GB_WO_URL.'/login/', '进入失败，请重试！');
        }
    }

    public function cp_enter_set($interface_id, $username, $password)
    {
        if('' != $interface_id && '' != $username && '' != $password)
        {
            $arr_data = array('username'=>$username, 'password'=>$password);

            //加载memcached
            $this->load->library('My_memcached','','mc');
            $this->mc->add($interface_id, array('is_ok'=>1, 'data'=>$arr_data),  30);

            echo 'ok';
        }
        else
        {
            echo 'error';
        }
        exit;
    }

    public function cp_enter($interface_id)
    {  
        //加载broker模型类
        $this->load->model('broker_model');

        //清空之前的登陆数据
        $this->broker_model->logout();

        if('' != $interface_id)
        {
            //加载memcached
            $this->load->library('My_memcached','','mc');
            $cache = $this->mc->get($interface_id);

            if(1 == $cache['is_ok'])
            {
                $this->_enter($cache['data']['username'], $cache['data']['password'], 'cp');
            }
            else
            {
                //跳转到登录页面
                $this->jump(GB_WO_URL.'/login/', '进入失败，请重试！');
            }
        }
        else
        {
            //跳转到登录页面
            $this->jump(GB_WO_URL.'/login/', '进入失败，请重试！');
        }
    }

    public function ag_enter_set($interface_id, $username, $password)
    {
        if('' != $interface_id && '' != $username && '' != $password)
        {
            $arr_data = array('username'=>$username, 'password'=>$password);

            //加载memcached
            $this->load->library('My_memcached','','mc');
            $this->mc->add($interface_id, array('is_ok'=>1, 'data'=>$arr_data),  30);

            echo 'ok';
        }
        else
        {
            echo 'error';
        }
        exit;
    }

    public function ag_enter($interface_id)
    {   
        //加载broker模型类
        $this->load->model('broker_model');

        //清空之前的登陆数据
        $this->broker_model->logout();

        if('' != $interface_id)
        {
            //加载memcached
            $this->load->library('My_memcached','','mc');
            $cache = $this->mc->get($interface_id);

            if(1 == $cache['is_ok'])
            {
                $this->_enter($cache['data']['username'], $cache['data']['password'], 'agentcp');
            }
            else
            {
                //跳转到登录页面
                $this->jump(GB_WO_URL.'/login/', '进入失败，请重试！');
            }
        }
        else
        {
            //跳转到登录页面
            $this->jump(GB_WO_URL.'/login/', '进入失败，请重试！');
        }
    }
    
    /**
    * broker post
    */
    private function _post()
    {
        //加载broker模型类
        $this->load->model('broker_model');

        $authnum = $this->session->userdata('authnum');
        //清空SESSION数据
        $this->session->unset_userdata('authnum');

        $yzm = $this->input->post('yzm', TRUE);
        if($authnum != '' && $authnum == $yzm)
        {
            $username = $this->input->post('username', TRUE);
            $password = $this->input->post('password');
            
            if($username == '' OR $password == '')
            {
                return 'u_p_error';
            }
            //判断账号密码正确性
            $uid = $this->broker_model->login($username, $password);
            
            //账号信息获取成功
            if($uid > 0)
            {
                //设置UID
                $this->broker_model->set_uid($uid);
                //获取经纪人信息
                $broker_info = $this->broker_model->get_broker_info();
                
                //当前时间
                $now = time();
                
                //判断账号是否到期
                $now_ymd = date('Y-m-d', $now);
                if($broker_info['expiredate'] < $now_ymd)
                {
                    return 'exp_error';
                }
                
                //判断账号是否禁用
                if($broker_info['valid_flag'] != '1')
                {
                    return 'vad_error';
                }

                //验证无误，把用户基本信息存入SESSION，并记录登陆日志
                $this->broker_model->set_user_session($broker_info);
                $ip = get_ip(); //登陆IP
                $log_data = array('kind'=>'brokercp', 'uid'=>$uid, 'logintime'=>$now, 'ip'=>$ip);
                $this->broker_model->log_broker_login($log_data);
                
                return TRUE;
            }
            else
            {
                return 'u_p_error';
            }
        }
        else
        {
            return 'yzm_error';
        }
    }
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
