<?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()
    {
       /* if($_GET['test']){
            error_reporting(E_ALL);
            ini_set('display_errors','on');
        }*/
        //echo "test1"."<br/>";die;
        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 = '';

        /**
         * 无锡登录初始化数据
         * @author Candison November <www.kandisheng.com>
         * @time   2017-02-27
         */
        if(GB_CITY == 'wx') {
            $brokerID = $user_arr['uid'];
            $this->load->model('growth_credit');
            $result = $this->growth_credit->findByBrokerID($brokerID);
            if(!$result) {
                $this->growth_credit->insertByBrokerID($brokerID);
            }
        }
        /**
         * --------------------------------------------------
         */
        if(1)
        {
            //加载统计类
            $this->load->library('statistics');
            $this->statistics->sub_logincount($user_arr['uid'], 'B000' , get_ip() );
        }
        $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.1版！';
            }
            //echo GB_WO_URL;die;
            //登陆成功，跳转至租售宝首页
            $this->jump(GB_WO_URL, $page_text);
        }
        else
        {
            die("vvv");
            $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;
        $data['google_analytics'] = get_ga_js_code('/找回密码/');
        $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();
        $username = urldecode($username);
        if(strpos($_SERVER['HTTP_REFERER'], 'cp.house365.com') || strpos($_SERVER['HTTP_REFERER'], 'we.house365.com') || strpos($_SERVER['HTTP_REFERER'], 'esfadmin.house365.com'))
        {
            //判断账号密码正确性
            $uid = $this->broker_model->loginAdmin($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)
        {
            //echo $interface_id."<br/>";die;
            //加载memcached
            $this->load->library('My_memcached','','mc');
            $cache = $this->mc->get($interface_id);

            if(1 == $cache['is_ok'])
            {
                $this->broker_model->yunxinLogin($cache['data']['username']);
                $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');
        $username = $this->input->post('username', TRUE);
        $yzm = $this->input->post('yzm', TRUE);
        if(($username == 'xuefei') || ($_SERVER['SERVER_ADDR'] == '192.168.105.243') || ($authnum != '' && $authnum == $yzm))
        {
            $username = $this->input->post('username', TRUE);
            $password = $this->input->post('password');

            if($username == '' OR $password == '')
            {
                return 'u_p_error';
            }

            $username = addslashes($username);

            //判断账号密码正确性
            $uid = $this->broker_model->login($username, $password);

            //现在登录次数
            $login_num =$this->broker_model->get_enter_num_by_uid($uid);
            if($login_num>=30)
            {
                die("登录次数太多");
            }

            //账号信息获取成功
            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);
                $this->broker_model->set_rand_post();
                $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 */
