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

class Reg extends CI_Controller {

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

	public function index()
	{
                $this->load->get_user_templates();
		$template=$this->load->view('user-reg.html','',true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');
		$Mark_Text=str_replace("{cscms:title}","新会员注册_".Web_Name,$template);
		$Mark_Text=str_replace('{cscms:regadd}',site_url('user/reg/add/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:passlink}',site_url('user/pass/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:loginlink}',site_url('user/login/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:qqlink}',site_url('user/login/qq/'),$Mark_Text);
	        $Mark_Text=str_replace('{cscms:weibolink}',site_url("open/login/weibo"),$Mark_Text);
	        $Mark_Text=str_replace('{cscms:kaixinlink}',site_url("open/login/kaixin"),$Mark_Text);
	        $Mark_Text=str_replace('{cscms:baidulink}',site_url("open/login/baidu"),$Mark_Text);
	        $Mark_Text=str_replace('{cscms:doubanlink}',site_url("open/login/douban"),$Mark_Text);
	        $Mark_Text=str_replace('{cscms:sohulink}',site_url("open/login/sohu"),$Mark_Text);
	        $Mark_Text=str_replace('{cscms:wangyilink}',site_url("open/login/netease"),$Mark_Text);
	        $Mark_Text=str_replace('{cscms:renrenlink}',site_url("open/login/renren"),$Mark_Text);
		$Mark_Text=str_replace('{cscms:codes}',site_url('user/codes/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:regxy}',stripslashes(html_entity_decode(User_Regxy)),$Mark_Text);

                $this->CsdjSkins->skins($Mark_Text);
        }

	public function check()
	{
                $username = $this->input->get_post('username', TRUE);   //username

                $sqlu="SELECT cs_id FROM ".CS_SqlPrefix."user where cs_name='".$username."'";
		$row=$this->CsdjDB->get_all($sqlu);
		if(!$row){
                      echo 'no';
                }else{
                      echo 'ok';
                }
        }

	public function addto()
	{
                $key = $this->input->get_post('key', TRUE);   //key
                $username = $this->input->get_post('username', TRUE);   //username
                $keyname=$username."_passkey";
                if($this->session->userdata($keyname)==$key && $this->session->userdata('username')==$username){
                     $this->db->query("update ".CS_SqlPrefix."user set CS_YID=0 where CS_Name='".$username."'");

                      $Pass[$keyname]='';
                      $Pass['username']='';
                      $this->session->set_userdata($Pass);

                     $this->CsdjSkins->Msg_url('恭喜您，验证成功，正在转入登入页面!',site_url('user/login'));  
                }else{
                     $this->CsdjSkins->Msg_url('抱歉，验证失败，KEY不正确,请联系管理员!',site_url('user/login'));  
                }
        }

	public function add()
	{

                if(User_Reg==2) $this->CsdjSkins->Msg_url('抱歉，本站已经关闭注册通道!','javascript:history.back();');  

                $user['CS_Name'] = $this->CsdjSkins->str_checkhtml($this->input->get_post('username', TRUE));   //username
                $user['CS_Pass'] = $this->CsdjSkins->str_checkhtml($this->input->get_post('userpass', TRUE));   //userpass
                $user['CS_Email'] = $this->CsdjSkins->str_checkhtml($this->input->get_post('useremail', TRUE));   //useremail
                $codes = $this->input->get_post('codes', TRUE);   //code
                $useremail=$user['CS_Email'];
                //-----------------------------------------以上是必须字段-------------------------------------

                $user['CS_City'] = $this->CsdjSkins->str_checkhtml($this->input->get_post('usercity', TRUE));   //usercity
                $user['CS_QQ'] = $this->CsdjSkins->str_checkhtml($this->input->get_post('userqq', TRUE));   //userqq
                $user['CS_Nichen'] = $this->CsdjSkins->str_checkhtml($this->input->get_post('usernichen', TRUE));   //usernichen
                $user['CS_Sex'] = intval($this->input->get_post('usersex', TRUE));   //usersex

                if(empty($user['CS_Name'])) $this->CsdjSkins->Msg_url('用户名不能为空!','javascript:history.back();');  
                if(empty($user['CS_Pass'])) $this->CsdjSkins->Msg_url('密码不能为空!','javascript:history.back();');  
                if(empty($user['CS_Email'])) $this->CsdjSkins->Msg_url('邮箱不能为空!','javascript:history.back();');  
                if(empty($codes)) $this->CsdjSkins->Msg_url('验证码不能为空!','javascript:history.back();'); 
                if(strtolower($codes)!=strtolower($this->session->userdata('cs_codes'))) $this->CsdjSkins->Msg_url('验证码不正确!','javascript:history.back();'); 

		if(strlen($user['CS_Name'])<6 && strlen($user['CS_Name'])>30){
			 $this->CsdjSkins->Msg_url('用户名非法!','javascript:history.back();'); 
		}


                $_march = '/^[0-9a-zA-Z\_]*$/';  
                if(!preg_match($_march, $user['CS_Name'])) {  
                   $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."' order by CS_ID desc";
		        $row=$this->db->query($sqlu)->row();
		        if($row){
                      if($this->CsdjSkins->DateBj('h',$row->cs_addtime,date('Y-m-d H:i:s'))<User_RegIP){
                             $this->CsdjSkins->Msg_url('本站限制同一IP '.User_RegIP.' 小时内不能重复注册!','javascript:history.back();'); 
                      }
                }

                $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_Qianm'] = '';
                 $user['CS_RegIp'] = $regip;
                 $user['CS_AddTime'] = date('Y-m-d H:i:s');
                 $user['CS_Pass']=md5($user['CS_Pass']);
                 $this->CsdjDB->get_insert('user',$user);

		 if(strpos(User_RegMsgFun,'1') !== FALSE){ //发送消息欢迎信息
                      $Msg['username'] = $user['CS_Name'];
                      $addmsg['CS_Usera']=$user['CS_Name'];
                      $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'] = $user['CS_Name'];
                      $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']=$user['CS_Name'];
                      $this->session->set_userdata($Pass);

                      $Msg['username'] = $user['CS_Name'];
                      $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);
                      $this->CsdjSkins->Msg_url('恭喜您，注册成功,请登入您的邮箱进行激活，正在转向登入页面!',site_url('user/login/'),'ok'); 
                 }else{
                      $this->CsdjSkins->Msg_url('恭喜您，注册成功，正在转向登入页面!',site_url('user/login/'),'ok'); 
                 }
        }
}

