<?php
/**
 * Copy Right IJH.CC
 * Each engineer has a duty to keep the code elegant
 * $Id$
 */
if(!defined('__CORE_DIR')){
    exit("Access Denied");
}
class Ctl_Xiche_Passport extends Ctl_Xiche
{
    

    public function index()
    {
        $this->login();
    }
    
    public function login()
    {
        if (!empty($_SERVER['HTTP_REFERER']) && strstr($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) && !strstr($_SERVER['HTTP_REFERER'], 'passport')) {
            $backurl = $_SERVER['HTTP_REFERER'];
        } else {
            $backurl = '/xiche/index';
        }
        

        $yzm_config = $this->system->config->get('access');

        if($yzm_config['verifycode']['reg'] == 'on'){
            $this->pagedata['reg_yzm'] = $yzm_config['verifycode']['reg']; 
        }

        
        $this->pagedata['backurl'] = $backurl;        
        if($this->uid){
            $this->tmpl = 'xiche/index.html';
        }else{
            $this->tmpl = 'xiche/passport/login.html';
        }        
    }
    
    
    public function sendsms()
	{
         if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
             $phone = $this->GP('mobile');
             
  
              if(!$a = K::M('verify/check')->mobile($phone)){
                    $this->msgbox->add('电话号码有误', 212);
                }else{
                    $code = rand(100000,999999);
                    $session =K::M('system/session')->start();
                                $session->set('code_'.$phone, $code,900); //15分钟缓存
                    $smsdata =  array('code'=>$code);
                    if(K::M('sms/sms')->send($phone, 'login', $smsdata)){
                        $this->msgbox->add('信息发送成功');
                    }
               } 
         }else{
             $this->error(404);
         }
	}


    public function handle($phone=null,$yzm=null,$yzm_val=null)
    {
        $session =K::M('system/session')->start();
        
        $yzm_config = $this->system->config->get('access');

        if($yzm_config['verifycode']['reg'] == 'on'){
            if(!$yzm_val){
                $this->msgbox->add('网页安全验证码没有填写', 212);
                return false;
            }else if(!K::M('magic/verify')->check($yzm_val)){
                $this->msgbox->add('网页安全验证码不正确', 212);
                return false;
            }
        }
        
        
        if(!$a = K::M('verify/check')->mobile($phone)){
            $this->msgbox->add('电话号码有误', 212);
        }else if(!$yzm){
            $this->msgbox->add('手机验证码错误',212);
        }else if(!($_SESSION['code_'.$phone]) || $yzm != $_SESSION['code_'.$phone]){
            $this->msgbox->add('手机验证码错误或已过期',212);
        }else if($member = K::M('member/member')->member($phone, 'mobile')){
            if($member = $this->auth->manager($member['uid'])){                
                $this->msgbox->add("欢迎您回来!".$a);
            }
        }else{
            $data['mobile'] = $phone;
            $data['passwd'] = uniqid();
            $data['paypasswd'] = uniqid();
            $data['nickname'] = '';
            if(defined('WX_OPENID')){
                $data['wx_openid'] = WX_OPENID;
                if($wx_info = $this->wechat_client()->getUserInfoById(WX_OPENID)){
                    $data['nickname'] = $wx_info['nickname'];
                }
            }
            if($member = K::M('member/member')->member($phone, 'mobile')){
                $this->msgbox->add('该手机号码已经被注册', 213);
            }else if($uid = K::M('member/account')->create($data)){
                K::M('system/logs')->log('passport', $this->system->db->SQLLOG());
                $this->msgbox->add('恭喜您，注册会员成功，请修改您的密码方便下次登录!');
                $this->msgbox->set_data('forward', 'ucenter/member/set/modifypwd.html');
            }
        }
	}
    
    public function handle2($phone=null,$password=null)
	{

        if(!$a = K::M('verify/check')->mobile($phone)){
            $this->msgbox->add('电话号码有误', 212);
        }else if(!$password){
            $this->msgbox->add('密码没有填写',213);
        }else if($member = K::M('member/member')->find(array('mobile'=>$phone))){
            if($member['passwd'] == md5($password)){
                if($member = $this->auth->manager($member['uid'])){
                    $this->msgbox->add("欢迎您回来!");
                }
            }else{
                $this->msgbox->add("密码错误!",215);
            }
            
        }else{
          $this->msgbox->add("不存在!",214);
        }
	}
    

    public function loginout()
    {
        $this->auth->loginout();
        header('Location:login');
    }

}