<?php
require_once('header.php');
require_once("phpmailer/class.phpmailer.php");
require_once("TnCode.class.php");

$username = flc_request(trim($_POST["acount"]));
$username = strtolower($username); //转成小写
$password = md5(trim($_POST["pwd"]));


$ip=GetUserIP(); 



//$username = str_replace(' ', '',$username);
//$username = preg_replace('/\<(.+?)\>/is', '',$username);

//滑动验证
if ($_SESSION['tncode_check']!='ok'){
   $response_arr = array("status" => 0,"message"  => '验证未通过,请重新验证!');
   echo json_encode($response_arr);
   exit;
}

unset($_SESSION['tncode_check']);

$sql="select * from {$tablepre}scuser where UserName='".$username."'";
$row   = $db->get_row($sql);

if (!empty($row)){
   $response_arr = array("status" => 0,"message"  => '该帐号已经有人使用，请重新注册!');
   echo json_encode($response_arr);
   exit;
}else{
  
   if ($smtpoff==1){

      $JFM=randomchar(12);

	  $sql="insert into {$tablepre}scuser (UserName,Password,LastLoginIP,LastLoginTime,LoginTimes,JFM) values('".$username."','".$password."','".$ip."','".$getnow."',0,'".$JFM."')";
      $db->exec($sql);

      //error_log($openssl."\r\n", 3, dirname(__FILE__).'/cht_log.txt');
	  
      $mail = new PHPMailer(true);         //建立邮件发送类
      $mail->CharSet = "UTF-8";            //设置信息的编码类型
      $mail->IsSMTP();                     //使用SMTP方式发送
      $mail->Host = $smtpserver;           //使用邮箱服务器
      $mail->SMTPAuth = true;              //启用SMTP验证功能
      $mail->Username = $smtpusermail;     //你的服务器邮箱账号
      $mail->Password = $smtppass;         //邮箱密码
	  if ($openssl==1){
      $mail->SMTPSecure = 'ssl';           //设置使用ssl加密方式登录鉴权 php.ini开启openssl
	  }
      $mail->Port = $smtpport;             //邮箱服务器端口号
      $mail->From = $smtpusermail;         //邮件发送者email地址
      $mail->FromName = $site_name;         //发件人名称
      $mail->AddAddress($username);        //发送多人就写多行，显示为陈惠添(chenhuitian@septwolves.com)
      $mail->IsHTML(true);                 //是否使用HTML格式

      $mail->Subject = $site_name."新用户确认通知";; //邮件标题

	  $mailbody = "亲爱的".$username.",离成功注册".$site_name."就差一步了！<br><br>请点击以下链接完成注册：<br>";
      $mailbody = $mailbody.'<a href="'.$site_url.'/activeuser.php?username='.$username.'&pwd='.$password.'&JFM='.$JFM.'"  target="_blank">'.$site_url.'/activeuser.php?username='.$username.'&pwd='.$password.'</a>';

      $mail->Body = $mailbody; //邮件内容，上面设置HTML，则可以是HTML

      if($mail->Send()){
		 $response_arr = array("status" => 1,"message"  => '注册资料已提交成功,请登录邮箱激活你的帐户!');
         echo json_encode($response_arr);
         exit;
	  }else{
		 $response_arr = array("status" => 0,"message"  => '邮件服务器异常,帐号可能无法激活,请联系网站管理员!');
         echo json_encode($response_arr);
         exit;
	  }

   }else{
      $sql="insert into {$tablepre}scuser (UserName,Password,LastLoginIP,LastLoginTime,LoginTimes,AdminType,isCheck) values('".$username."','".$password."','".$ip."','".$getnow."',0,0,1)";
      $db->exec($sql);
      $uid=$db->lastInsertId();

      $_SESSION["uid"]=$uid;
	  $_SESSION["UserName"]=$username;
	  $_SESSION["pwd"]=$password;
	  $response_arr = array("status" => 2,"message"  => '注册资料已提交成功,点击确定登录你后台!');
      echo json_encode($response_arr);
      exit;
   }

}
?>