<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Open extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
        	$this->load->helper('form');
		$this->load->model('CsdjDB');
		$this->db->cache_off();
		$this->load->model('CsdjSkins');
                $this->load->library('session'); //加载session
                $this->load->model('CsdjEmail');
	}

        //第三方登入入口
	public function login()
	{
              if(CS_Appmode==2) exit($this->CsdjSkins->Msg_url('第三方登入已经关闭！',Web_Path));
              $urldata['log_fhurl']=$_SERVER['HTTP_REFERER'];
              $urldata['log_state']=md5(uniqid(rand(), TRUE)); //CSRF protection;
              $this->session->set_userdata($urldata);
              $ac = $this->security->xss_clean($this->uri->segment(3));   //方式
              $log_url="http://denglu.chshcms.com/denglu?ac=".$ac."&appid=".CS_Appid."&redirect_uri=".site_url("open/callback")."&state=".$urldata['log_state']."&getdate=".time();
              header("Location: $log_url"); 
	}

        //第三方登入返回
	public function callback()
	{
              if(CS_Appmode==2) exit($this->CsdjSkins->Msg_url('第三方登入已经关闭！',Web_Path));
              $ac=$this->input->get_post('ac', TRUE);
              $state=$this->input->get_post('state', TRUE);
              $openid=$this->input->get_post('uid', TRUE);
              $username=$this->input->get_post('username', TRUE);
              $userlogo=$this->input->get_post('userlogo', TRUE);

              if(!$this->session->userdata('log_state') || empty($state) || empty($ac)) exit($this->CsdjSkins->Msg_url('非法操作登入！',Web_Path));
              if($this->session->userdata('log_state')==$state){

                if($openid){
                      $qqlogin=$this->CsdjDB->get_select ('user','CS_Qqid','CS_Pass,CS_SID,CS_ID,CS_Vip,CS_Name,CS_LogNum,CS_Cion,CS_LogTime,CS_EndTime',$openid);
                      if($qqlogin){

                            $US["cs_id"] = $qqlogin[0]->CS_ID;
                            $US["cs_name"] = $qqlogin[0]->CS_Name;
                            $US["cs_pass"] = md5($qqlogin[0]->CS_Pass);
                            $this->session->set_userdata($US);

		               if(Cion_Log>0 && date("Y-m-d",strtotime($qqlogin[0]->CS_LogTime))!=date('Y-m-d')){
                                   $updata['CS_Cion']  = $qqlogin[0]->CS_Cion+Cion_Log;
                               }
                               //判断VIP到期
                               $viptime=$this->CsdjSkins->DateBj('n',$qqlogin[0]->CS_EndTime,date('Y-m-d H:i:s'));
                               IF($qqlogin[0]->CS_Vip>0 && $viptime>10){
	                                $updata['CS_Vip']  = 0;
	                                $updata['CS_VipTime']  = '00-00-00 00:00:00';
	                                $updata['CS_EndTime']  = '00-00-00 00:00:00';
                               }
	                       $updata['CS_Zx']  = 1;
	                       $updata['CS_LogNum']   = $qqlogin[0]->CS_LogNum+1;
	                       $updata['CS_LogTime'] = date('Y-m-d H:i:s');
	                       $updata['CS_LogIP'] = $this->CsdjSkins->GetIP();
	                       $updata['CS_Logms'] = time();
                               $this->CsdjDB->get_update ('user',$qqlogin[0]->CS_ID,$updata);

                               if(!$this->session->userdata('log_fhurl')){
                                    $fhurl=site_url('user/space/');
                               }else{
                                    $fhurl=$this->session->userdata('log_fhurl');
                               }

                               $urldata['log_fhurl']='';
                               $urldata['log_state']='';
                               $this->session->set_userdata($urldata);

                               $this->CsdjSkins->Msg_url('恭喜，登入成功!',$fhurl,'ok'); 
                      }else{

                               $this->load->get_user_templates();
		               $template=$this->load->view('user-denglu.html','',true);
		               $template=$this->CsdjSkins->TopandBottom($template,'user');
		               $Mark_Text=str_replace("{cscms:title}","会员第三方登入_".Web_Name,$template);
		               $Mark_Text=str_replace('{cscms:newlogin}',site_url('open/login/'.$ac),$Mark_Text);
		               $Mark_Text=str_replace('{cscms:banglogin}',site_url('open/bang'),$Mark_Text);
		               $Mark_Text=str_replace('{cscms:nichen}',$username,$Mark_Text);
		               $Mark_Text=str_replace('{cscms:logo}',$userlogo,$Mark_Text);
		               $Mark_Text=str_replace('{cscms:openid}',$openid,$Mark_Text);

                               $this->CsdjSkins->skins($Mark_Text);
                      }
                }else{
                       $this->CsdjSkins->Msg_url('没有获取到openid！',site_url('user/login/'));
                }

              }else{
                    exit($this->CsdjSkins->Msg_url('非法操作登入！',Web_Path));
              }
        }

        //绑定登入
	public function bang()
	{ 
            $user['CS_Name'] = $this->input->get_post('username', TRUE);   //username
            $user['CS_Pass'] = $this->input->get_post('userpass', TRUE);   //userpass
            $user['CS_Nichen'] = $this->input->get_post('usernichen', TRUE);   //usernichen
            $user['CS_Email'] = $this->input->get_post('useremail', TRUE);   //useremail
            $user['CS_Qqid'] = $this->input->get_post('openid', TRUE);   //openid
            $username=$user['CS_Name'];
            $userpass=$user['CS_Pass'];
            $useremail=$user['CS_Email'];

            if(empty($user['CS_Qqid'])) $this->CsdjSkins->Msg_url('没有获取到openid!',site_url('user/login/')); 

              //--------------------------------------可选字段-------------------------------------------------------------------
                $user['CS_City'] = $this->input->get_post('usercity', TRUE); 
                $user['CS_QQ']   = $this->input->get_post('userqq', TRUE);
                $user['CS_Sex']  = intval($this->input->get_post('usersex', TRUE));
                $user['CS_Logo'] = $this->input->get_post('userlogo', TRUE); 

            $denglu = $this->security->xss_clean($this->input->get_post('denglu', TRUE));   //qquser

            if($denglu=='login'){ //绑定登入
                 if(empty($user['CS_Name'])) $this->CsdjSkins->Msg_url('帐号不能为空!','javascript:history.back();');  
                 if(empty($user['CS_Pass'])) $this->CsdjSkins->Msg_url('密码不能为空!','javascript:history.back();'); 

                    //可以用ID、会员名、邮箱来进行登入
                    $sqlu="SELECT CS_Pass,CS_SID,CS_ID,CS_Name,CS_LogNum,CS_Cion,CS_LogTime FROM ".CS_SqlPrefix."user where cs_name='".$username."' or cs_email='".$username."'";
		    $row=$this->CsdjDB->get_all($sqlu);
		    if(!$row){
                            $this->CsdjSkins->Msg_url('抱歉，该帐号不存在!','javascript:history.back();');  
                    }else{
                           if($row[0]->CS_Pass!=md5($userpass)){
                                   $this->CsdjSkins->Msg_url('抱歉，密码错误!','javascript:history.back();');  
                           }elseif($row[0]->CS_SID==1){
                                   $this->CsdjSkins->Msg_url('抱歉，您的帐号已经被锁定或者没有验证审核，请联系站长!','javascript:history.back();');  
                           }else{

		               if(Cion_Log>0 && date("Y-m-d",strtotime($row[0]->CS_LogTime))!=date('Y-m-d')){
                                   $updata['CS_Cion']  = $row[0]->CS_Cion+Cion_Log;
                               }
	                       $updata['CS_Qqid']  = $user['CS_Qqid'];
	                       $updata['CS_Logo']  = $user['CS_Logo'];
	                       $updata['CS_Zx']  = 1;
	                       $updata['CS_LogNum']   = $row[0]->CS_LogNum+1;
	                       $updata['CS_LogTime'] = date('Y-m-d H:i:s');
	                       $updata['CS_LogIP'] = $this->CsdjSkins->GetIP();
	                       $updata['CS_Logms'] = time();
                               $this->CsdjDB->get_update ('user',$row[0]->CS_ID,$updata);

                               $QQ['openid']="";
                               $this->session->set_userdata($QQ);

                                  $ulogin['cs_id']=$row[0]->CS_ID;
                                  $ulogin['cs_name']=$row[0]->CS_Name;
                                  $ulogin['cs_pass']=md5($row[0]->CS_Pass);
                                  $this->session->set_userdata($ulogin);
                                   $this->CsdjSkins->Msg_url('恭喜，登入成功!',site_url('user/space/'),'ok'); 
                           }
                    }

            }else{   //注册
                 if(User_Reg==2) $this->CsdjSkins->Msg_url('抱歉，本站已经关闭注册通道!','javascript:history.back();');  
                 if(empty($user['CS_Nichen'])) $this->CsdjSkins->Msg_url('呢称不能为空!','javascript:history.back();');  
                 if(empty($user['CS_Email'])) $this->CsdjSkins->Msg_url('邮箱不能为空!','javascript:history.back();'); 


                $regip=$this->CsdjSkins->GetIP();
		if(empty($regip)){
                      $this->CsdjSkins->Msg_url('请不要使用代理注册!','javascript:history.back();'); 
                }


		if($this->CsdjSkins->Getqx($user['CS_Name'],User_RegJzName)=='ok'){ //禁止帐号
                      $this->CsdjSkins->Msg_url('您的帐号已被禁止注册!','javascript:history.back();'); 
                }

		if($this->CsdjSkins->Getqx($user['CS_Nichen'],User_RegJzName)=='ok'){ //禁止帐号
                      $this->CsdjSkins->Msg_url('您的呢称已被禁止注册!','javascript:history.back();'); 
                }

                $sqlu="SELECT cs_id,CS_AddTime FROM ".CS_SqlPrefix."user where CS_Regip='".$regip."'";
		$row=$this->CsdjDB->get_all($sqlu);
		if($row){
                      if($this->CsdjSkins->DateBj ('h', $row[0]->CS_AddTime,date('Y-m-d H:i:s'))<User_RegIP){
                             $this->CsdjSkins->Msg_url('本站限制同一IP '.User_RegIP.' 小时内不能重复注册!','javascript:history.back();'); 
                      }
                }

                   $this->load->helper('string');
                   $username=random_string('alnum',10);
                   $user['CS_Name']=$username;

                   $sqlu="SELECT cs_id FROM ".CS_SqlPrefix."user where cs_name='".$user['CS_Name']."'";
		   $row=$this->CsdjDB->get_all($sqlu);
		   if($row){
                      $this->CsdjSkins->Msg_url('该用户名已经存在!','javascript:history.back();'); 
                   }

                   $sqlu="SELECT cs_id FROM ".CS_SqlPrefix."user where cs_email='".$user['CS_Email']."'";
		   $row=$this->CsdjDB->get_all($sqlu);
		   if($row){
                      $this->CsdjSkins->Msg_url('该邮箱已经存在!','javascript:history.back();'); 
                   }

		   if(User_RegFun==1){
                       $user['CS_SID'] = 1;
                   }

		   if(User_RegEmailFun==1){
                       $user['CS_YID'] = 1;
                   }
		   if(Cion_Reg>0){
                       $user['CS_Cion'] = Cion_Reg;
                   }
                    $user['CS_RegIp'] = $regip;
                    $user['CS_AddTime'] = date('Y-m-d H:i:s');
                    $user['CS_Pass']=md5(uniqid(rand(), TRUE));
                    $this->CsdjDB->get_insert('user',$user);

		 if(strpos(User_RegMsgFun,'1') !== FALSE){ //发送站内欢迎信息
                      $Msg['username'] = $username;
                      $addmsg['CS_Usera']=$username;
                      $addmsg['CS_Userb']="管理员";
                      $addmsg['CS_Name'] =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegMsgTitle)),$Msg);
                      $addmsg['CS_Neir'] =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegMsgContent)),$Msg);
                      $addmsg['CS_AddTime'] =date('Y-m-d H:i:s');
                      $this->CsdjDB->get_insert('xiaoxi',$addmsg);
                 }
		 if(strpos(User_RegMsgFun,'2') !== FALSE){ //发送邮件欢迎信息
                      $Msg['username'] = $username;
                      $title   =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegMsgTitle)),$Msg);
                      $content =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegMsgContent)),$Msg);
                      $this->CsdjEmail->Email_Add($useremail,$title,$content);
                 }
		 if(User_RegEmailFun==1){
                      $this->load->helper('string');
                      $keyname=$user['CS_Name']."_passkey";
                      $key=random_string('alnum', 16);
                      $Pass[$keyname]=$key;
                      $Pass['username']=$username;
                      $this->session->set_userdata($Pass);

                      $Msg['username'] =$username;
                      $Msg['url']      = site_url('user/reg/addto/')."?key=".$key."&username=".$user['CS_Name'];
                      $title   =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegEmailTitle)),$Msg);
                      $content =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegEmailContent)),$Msg);
                      $this->CsdjEmail->Email_Add($useremail,$title,$content);
                      $oktitle='恭喜您，注册成功,请登入您的邮箱进行激活，正在转向登入页面!'; 
                 }else{
                      $oktitle='恭喜您，注册成功，正在转向登入页面!'; 
                 }

		 if(User_RegEmailFun==2){
                    $sqlu="SELECT CS_Pass,CS_SID,CS_ID,CS_Name,CS_LogNum,CS_Cion,CS_LogTime FROM ".CS_SqlPrefix."user where cs_email='".$user['CS_Email']."'";
		    $row=$this->CsdjDB->get_all($sqlu);
		    if($row){
		               if(Cion_Log>0 && date("Y-m-d",strtotime($row[0]->CS_LogTime))!=date('Y-m-d')){
                                   $updata['CS_Cion'] = $row[0]->CS_Cion+Cion_Log;
                               }
	                       $updata['CS_Zx']  = 1;
	                       $updata['CS_LogNum']  = $row[0]->CS_LogNum+1;
	                       $updata['CS_LogTime'] = date('Y-m-d H:i:s');
	                       $updata['CS_LogIP'] = $this->CsdjSkins->GetIP();
	                       $updata['CS_Logms'] = time();
                               $this->CsdjDB->get_update ('user',$row[0]->CS_ID,$updata);
                               $QQ['openid']="";
                               $this->session->set_userdata($QQ);

                                  $ulogin['cs_id']=$row[0]->CS_ID;
                                  $ulogin['cs_name']=$row[0]->CS_Name;
                                  $ulogin['cs_pass']=md5($row[0]->CS_Pass);
                                  $this->session->set_userdata($ulogin);
                                   $this->CsdjSkins->Msg_url('恭喜，登入成功!',site_url('user/space/'),'ok'); 
                    }else{
                          $this->CsdjSkins->Msg_url('抱歉，注册失败!',site_url('user/login/'));  
                    }
		 }else{
                          $this->CsdjSkins->Msg_url($oktitle,site_url('user/login/'),'ok'); 
                 }
            }

        }
}


