<?php
require_once "init.php";
require_once DIR_ROOT.'/lib/function.login.php';
$act= isset($_GET['act']) ? addslashes($_GET['act']) : (isset($_POST['act']) ? addslashes($_POST['act']) : '');
$db=MySql::getInstance();
if ($act=='is_registered')
{  

  $username = isset($_GET['username']) ? addslashes(trim($_GET['username'])) : '';
  if (!$db->IsExistsRecord("select 1 from teuser where yhm='$username'"))
  {
  die("true") ;
  }else{
  die("false");
  }
}
if ($act=='check_email')
{  

  $email = isset($_GET['email']) ? addslashes(trim($_GET['email'])) : '';
  if (!$db->IsExistsRecord("select 1 from teuser where email='$email'"))
  {
    die("ok") ;
  }else{
    die("false");
  }
}
if ($act == 'logout')
{
	
setAuthCookie('');
loginPage();
}
define('islogin', isLogin());
if (islogin)
emDirect("user.php?act=index");


if ($act == 'login')
{   
	$username = isset($_POST['username']) ? addslashes(trim($_POST['username'])) : '';
	$password = isset($_POST['password']) ? addslashes(trim($_POST['password'])) : '';
	
	$img_code = isset($_POST['yzm']) ? addslashes(trim(strtoupper($_POST['yzm']))) : '';

if (checkUser($username ,$password,$img_code))
{  

	setAuthCookie($_POST['username']);
	emDirect("user.php?act=index");
} else
{
  loginPage();
}
}
if ($act=='')
loginPage();
if ($act == 'reg')
{
$operation=isset($_POST['operation']) ? addslashes(trim($_POST['operation'])) : '';
if ($operation=='add')
{
  $password = isset($_POST['password']) ? addslashes(trim($_POST['password'])) : '';
  $username = isset($_POST['username']) ? addslashes(trim($_POST['username'])) : '';
  $email = isset($_POST['email']) ? addslashes(trim($_POST['email'])) : '';
  $qq = isset($_POST['qq']) ? addslashes(trim($_POST['qq'])) : '';
  $office_phone = isset($_POST['office_phone']) ? addslashes(trim($_POST['office_phone'])) : '';
  $mobile_phone = isset($_POST['mobile_phone']) ? addslashes(trim($_POST['mobile_phone'])) : '';
  testzd($qq.$office_phone.$mobile_phone.$email);
  
  if ($db->IsExistsRecord("select 1 from teuser where yhm='$username'"))
  {
  emMsg("用户名：$username 已被使用！");
  }
  if ($email!="")  
  if ($db->IsExistsRecord("select 1 from teuser where email='$email'"))
  {
  emMsg("邮箱地址：$email 已被使用！");
  }
  if ($db->IsExistsRecord("select 1 from teuser where yhm='$username'"))
  {
  emMsg("用户名：$username 已被使用！");
  }
  
  
  $sql="insert into teuser (yhm,mm,email,qq,dh,sj,dlrq) value('$username','$password','$email','$qq','$office_phone','$mobile_phone',sysdate() )";
  $db->query($sql);
  setAuthCookie($username);
  emDirect("user.php?act=index");  
} else	
include "inc/zc.htm";
}
