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

class Sft_Index_module extends CI_Module {

	function __construct(){
		    parent::__construct();
        	$this->load->helper('url');
	        $this->load->database();
            $this->load->library('session'); //加载session
	        $this->load->model('CsdjDB'); //加载数据操作模型
            $this->load->model('CsdjSkins'); //加载全局标签模型 
			require_once PLUGINDIR.'sft/conxiu.php'; 
	        $dir = $this->uri->segment(2);
            $sql="SELECT CS_SID FROM ".CS_SqlPrefix."plugins where cs_dir='".$dir."'";
	        $row=$this->CsdjDB->get_all($sql);
	        if($row){
                      if($row[0]->CS_SID==1){
                             $this->CsdjAdmin->Admin_Url('<font color=red>该插件尚未开启！</font>',"javascript:history.back();");
                      }
            }else{
                      $this->CsdjAdmin->Admin_Url('<font color=red>该插件尚未安装！</font>',"javascript:history.back();");
            }
	}


	public function index()
	{
	     $share =$this->input->get('share', TRUE);
		 $md5 =$this->input->get('md5', TRUE);
		 $name=md5($share);
		 $row=$this->db->query("SELECT CS_Logo,CS_Qianm,CS_Nichen,CS_Name,CS_Cion,CS_Vip,CS_Hits,CS_Qdts,CS_Qdtime,CS_Qda,CS_Qdb,CS_Qdc,CS_Qdd,CS_Qde,CS_Qdf,CS_Leiji,CS_Qdsjian,CS_Qdtian FROM ".CS_SqlPrefix."user where cs_name='".$share."'")->row();
		 if($name==$md5){
	      echo'<!DOCTYPE html>
<html>
<head>
<meta charset="gbk" />
<title>推广注册-'.Web_Name.'</title>
<meta name="Keywords" content="{cscms:keywords}" />
<meta name="Description" content="{cscms:description}" />
<link rel="shortcut icon" href="{cscms:path}favicon.ico" />
<link rel="stylesheet" type="text/css" href="/plugins/sft/statics/css/style.css"/>
<script type="text/javascript">var web_url="{cscms:path}";</script>
<script type="text/javascript" src="/plugins/sft/statics/js/jquery.js"></script>
<script type="text/javascript" src="/plugins/sft/statics/js/common.js"></script>
</head>
<body>


<div id="cscms_log">
    <div class="title">会员注册</div>
    <form class="iForm" action="/index.php/m/sft/index/reg_save" method="post">
      <div><font color=red></font></div>
      <p>
        <label>用户名：</label><input id="name" name="name" size="50" value="" class="i" /> <span id="name_msg">(帐号应该由6~15位之内的数字、下划线、英文字母组成)</span>
       </p>
      <p>
        <label>密码：</label><input type="password" id="pass" name="pass" size="50" class="i" /><span id="pass_msg">(6位以上数字、字母或者符号的组合)</span>
      </p>
      <p>
        <label>昵称：</label><input id="nichen" name="nichen" size="50" value="" class="i" /> <span id="nichen_msg">(请输入您的昵称。)</span>
      </p>
      <p>
        <label>联系邮箱：</label><input id="email" name="email" size="50" value="" class="i" /><span id="email_msg">(重要！找回密码用，推荐使用QQ邮箱。)</span>
      </p>
      <p>
        <label>推荐人：</label><input id="share" name="share" size="50" value="'.$share.'" class="i" /><span id="email_msg">(注册推荐人:<font color="#FF0000">'.$row->CS_Nichen.'</font>。)</span>
      </p>
      <p>
        <label>验证码：</label><input maxlength=4 id="codes" name="codes" size=5 class="v" > <img src="'.Web_Path.'index.php/user/codes" align="absmiddle"  onclick="this.src=\''.Web_Path.'index.php/user/codes?temp=\'+ (new Date().getTime().toString(36)); return false"  style="cursor:pointer" title="看不清点击更新" alt="看不清点击更新" ><span id="code_msg">(输入4位验证码。)</span>
      </p>
      <p>
        <input class="btn" id="regsubmit" name="submit" type="submit" value="完成注册" />
      </p>
    </form>
    </div>
    <div class="clear"></div>
</div>


</body>
</html>
';
}else{
$this->CsdjSkins->Msg_url('你获得的会员推广链接有误，请重新获取。',site_url(''),'ok');
}		 
        }


	public function reg_save()
	{
            $username=$this->input->get_post('name', TRUE);
            $userpass=$this->input->get_post('pass', TRUE);
            $useremail=$this->input->get_post('email', TRUE);
            $codes=$this->input->get_post('codes', TRUE);
			$nichen=$this->input->get_post('nichen', TRUE);
			$share=$this->input->get_post('share', TRUE);

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

            if(empty($username) || empty($userpass) || empty($useremail)){
				exit('<script type="text/javascript">alert("资料不完整!");location.href="javascript:history.back();";</script>');
			}

            if(strtolower($codes)!=strtolower($this->session->userdata('cs_codes'))){
				exit('<script type="text/javascript">alert("验证码错误!");location.href="javascript:history.back();";</script>');
			}

            $_march = '/^[0-9a-zA-Z\_]*$/';  
            if(!preg_match($_march, $username) || strlen($username)<6 || strlen($username)>20) {  
			    exit('<script type="text/javascript">alert("用户名只能是6-20位数字、字母和下划线!");location.href="javascript:history.back();";</script>');
            }  
            if(strlen($userpass)<6 || strlen($userpass)>20) {  
			    exit('<script type="text/javascript">alert("密码只能是6-20位!");location.href="javascript:history.back();";</script>');
            }  

            $regip=$this->CsdjSkins->GetIP();
			$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();'); 
                      }
                }
             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 FROM ".CS_SqlPrefix."user where cs_name='".$username."'";
		    $row=$this->CsdjDB->get_all($sqlu);
		    if($row){
				exit('<script type="text/javascript">alert("该用户名已经存在!");location.href="javascript:history.back();";</script>');
            }

            $sqlu="SELECT cs_id FROM ".CS_SqlPrefix."user where cs_email='".$useremail."'";
		    $row=$this->CsdjDB->get_all($sqlu);
		    if($row){
				exit('<script type="text/javascript">alert("该邮箱已经存在!");location.href="javascript:history.back();";</script>');
            }
			
            $sqlu="SELECT cs_id FROM ".CS_SqlPrefix."user where cs_nichen='".$nichen."'";
		    $row=$this->CsdjDB->get_all($sqlu);
		    if($row){
				exit('<script type="text/javascript">alert("该昵称已经存在!");location.href="javascript:history.back();";</script>');
            }
             
			$user['CS_Nichen'] = $nichen;
            $user['CS_Name'] = $username;
            $user['CS_Pass'] = md5($userpass);
            $user['CS_Email'] = $useremail;
            $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']);
            $ids=$this->CsdjDB->get_insert('user',$user);
            //会员推广开始
			$row=$this->db->query("SELECT CS_ID,CS_Cion FROM ".CS_SqlPrefix."user where cs_name='".$share."'")->row();
			$updata['CS_Cion']=$row->CS_Cion+jinbi;
            $this->CsdjDB->get_update ('user',$row->CS_ID,$updata);
			//会员推广结束
			
			
            if($ids>0){
                   $ulogin['cs_id']=$ids;
                   $ulogin['cs_name']=$username;
                   $ulogin['cs_pass']=md5($user['CS_Pass']);
                   $this->session->set_userdata($ulogin);
                   $this->CsdjSkins->Msg_url('注册成功，正在登陆。',site_url('user/space/'),'ok'); //成功
			}else{
                   $this->CsdjSkins->Msg_url('注册失败!',site_url('m/sft'),'ok');
			}
	}




	public function admin()
	{
              header("Location: ".site_url('m/sft/admin').""); 
        }
		
		public function hqu()
	{
             $this->User_Login();
			 $row=$this->db->query("SELECT CS_Logo,CS_Qianm,CS_Nichen,CS_Name,CS_Cion,CS_Hits FROM ".CS_SqlPrefix."user where cs_id='".$this->session->userdata('cs_id')."'")->row();
			 $name=md5($row->CS_Name);
			echo'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获取推广链接-'.Web_Name.'</title>
</head>

<body><div class="quanbu" style="width:690px; margin:0 auto;border:1px #00CC00 solid;">
<div class="zhong" style=" width:690px; height:28px; line-height:28px; padding-left:10px; color:#FF0000; float:left; margin-top:10px;">会员推广中心</div>
<div class="xia" style="width:690px;padding-left:10px;margin-top:10px;">
如果您的朋友通过下面的连接访问网站并注册新会员，您将获得'.jinbi.'个金币的奖励！<br>下面是你的推广连接，您可以通过复制下面的推广连接在QQ、MSN等IM已经其它的网站论坛分享给任何人。
</div><br><br>
  点击复制您的推广连接：
			<textarea id="codeContent" onClick="oCopy(this)" rows="2" cols="51">
'.site_url('m/sft/index/?share='.$row->CS_Name.'&md5='.$name.'').'
</textarea><input type="button" style=" margin-bottom:30px;" onClick="copyCode()" value="复制推广" />

</div></body>
</html>';
			echo"
<script language='javascript'>
var obj=document.getElementById('codeContent');
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand('Copy')
alert('复制成功!');
}
var obj=document.getElementById('codeContent');
function runCode(){
	var code=obj.value;
	var newWindow=window.open('','','');
	newWindow.opener=null;
	newWindow.document.write(code);
	newWindow.document.close();
}
function saveCode(){
	var code=obj.value;
	var winname=window.open('','_blank','top=10000');
	winname.document.open('text/html','replace');
	winname.document.writeln(code);
	winname.document.execCommand('saveas','','phpernote.txt');
	winname.close();
}
function copyCode(){
	if(isIE()){
		alert('代码已复制到剪贴板！');
	}else{
		alert('本页面复制功能仅支持IE浏览器！');
		return false;
	}
	var rng=document.body.createTextRange();
	rng.moveToElementText(obj);
	rng.scrollIntoView();
	rng.select();
	rng.execCommand('Copy');
	rng.collapse(false);
}
function isIE(){
	var Sys={};
	var ua=navigator.userAgent.toLowerCase();
	var s;
	(s=ua.match(/msie ([\d.]+)/))?Sys.ie=s[1]:false;
	return Sys.ie;
}

</script>";
        }
		//
    function User_Login($cid=0) {
          $id   = intval($this->session->userdata('cs_id'));
          $name = $this->session->userdata('cs_name');
          $pass = $this->session->userdata('cs_pass');
          $login= TRUE;

          if($id==0||empty($name)||empty($pass)){
              if($cid==0){
                    die("<script language='javascript'>window.parent.location='".site_url('user/login')."';</script>");
              }else{
                    $login=FALSE; 
              }  
          }else{
		        $row=$this->db->query("SELECT CS_Pass FROM ".CS_SqlPrefix."user where cs_id='".$id."'")->row();
		        if(!$row){
                    if($cid==0){
                           die("<script language='javascript'>window.parent.location='".site_url('user/login')."';</script>");
                    }else{
                           $login=FALSE; 
                    }
                }else{
		        if(md5($row->CS_Pass)!=$pass){
                          if($cid==0){
                                 die("<script language='javascript'>window.parent.location='".site_url('user/login')."';</script>");
                          }else{
                                 $login=FALSE; 
                          }
                    }
                }
          }
          return $login;
    }
		
		//
}
