<?php
include_once("../common/config.php");
include_once("../common/publicfun.php");
include_once("../classes/dbclass.php");
include_once("../conn.php");
include_once("../common/checkpost.php");

$username=flc_request(trim($_POST["txtName"]));
$password=flc_request(trim($_POST["txtPwd"]));
$password=md5($password);
$codestr=trim($_POST["codestr"]);
$codestr=strtolower($codestr);

if (CheckName($username)==false){
   $response_arr = array("status" => -1,"message"  => '用户名不合法!');
   echo json_encode($response_arr);
   exit;
}

/*
if ($codestr!=""){
  if (strtolower($_SESSION["code"])!=$codestr){
     if ($_POST["returnurl"]=="admin.php"){
        flc_msg("验证码错误!","../admin.php");
     }else{
        flc_msg("验证码错误!","../index.php");
     }
     exit;
  }
}
*/
//已审核的且为管理员才允许登录后台,登录后再按角色权限进行访问
$sql   ="select * from {$tablepre}scuser where UserName='".$username."' and AdminType=1 and isCheck=1";
$row   = $db->get_row($sql);
if (empty($row)){
   $response_arr = array("status" => -1,"message"  => '用户不存在或未审核');
   echo json_encode($response_arr);
   exit;
}else{
      if($row["isLock"]==1){
          $response_arr = array("status" => -1,"message"  => '登录失败多次已被锁了');
          echo json_encode($response_arr);
          exit;
	  }
	  
	  $a_uid=$row["ID"];
	  $admin_user=$row["UserName"];
	  $admin_pwd=$row["Password"];
	  $a_type=$row["AdminType"];
	  $ip=GetUserIP(); 
	  if ($admin_pwd==$password && $admin_user==$username){
	    $_SESSION["a_uid"]=$a_uid;
		$_SESSION["admin_user"]=$admin_user;
	    $_SESSION["admin_pwd"]=$admin_pwd;
		$_SESSION["a_type"]=$a_type;
		$sql="update {$tablepre}scuser set LastLoginTime='".$getnow."',LastLoginIP='".$ip."',LoginTimes=LoginTimes+1 where UserName='".$username."'";
        $db->exec($sql);

		$response_arr = array("status" => 1,"message"  => '登录成功');
        echo json_encode($response_arr);
        exit;

	  }else{
	    $_SESSION["errorcount"]=$_SESSION["errorcount"]+1;
		//登录失败超过5次时自动锁定用户
		if ($_SESSION["errorcount"]>6){
            $sql="update {$tablepre}scuser set isLock=1 where UserName='".$username."'";
            $db->exec($sql);
		}
		$response_arr = array("status" => -1,"message"  => '用户名不正确或已被禁用');
        echo json_encode($response_arr);
        exit;
	  }
}
?>