<?php
include("fun.php");
start_session(14400);

date_default_timezone_set('Asia/Taipei');

?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<SCRIPT language="JavaScript">
function isMobileDevice() {
  let mobileDevices = ['Android', 'webOS', 'iPhone', 'iPad', 'iPod', 'BlackBerry', 'Windows Phone'];
  for (var i = 0; i < mobileDevices.length; i++) {
      if (navigator.userAgent.match(mobileDevices[i])) {
        //console.log("isMobileDevice: match " + mobileDevices[i]);
        return true;
      }
  }
  return false
}

if (isMobileDevice()) {
	alert("本系統不適用手機!!");
	top.location.href = "Login.php";
}
</SCRIPT>
<?php
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");
require_once("class/MacOperation.php");

/*正式環境
require_once("./class/DBConfig.php");
require_once("./class/DBOperation.php");
require_once("./class/MacOperation.php");
require_once("./class/CheckFun.php");
*/


$ac = $_POST['ac'];
$pw = $_POST['pw'];
$verifyCode = $_POST['verifyCode'];
$macAddress = $_POST['MACAddress'];

//檢查狀態
$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
if ($ac != "admin") {
	$db->query("SELECT SValue FROM SysConfig where SName = 'WEBFLAG'");
	$row = $db->fetch_row();
	//$db->close_db();
	if ($row != null) {
		$webStatus = $row[0];
	} else {
		$webStatus = "0";
	}
} else {
	$webStatus = "1";
}
if ($webStatus == "0") {
	echo '<script type="text/javascript">';
	echo 'alert("網站無法使用，請洽系統管理員!!!");';
	echo 'top.location.href="Login.php"';
	echo '</script>';
	exit(0);
}
if ($ac != "admin") {
	$db->query("SELECT SValue FROM SysConfig where SName = 'ISENABLELOCK'");
	$row = $db->fetch_row();
	// $db->close_db();
	if ($row != null) {
		$isEnableLock = $row[0];
	} else {
		$isEnableLock = "0";
	}
} else {
	$isEnableLock = "0";
}

$isPassErrTimes = 0;
if ($isEnableLock == "1") {
	$db->query("SELECT SValue FROM SysConfig where SName = 'PASSERRTIME'");
	$row = $db->fetch_row();
	$db->close_db();
	if ($row != null) {
		$isPassErrTimes = $row[0];
	} else {
		$isPassErrTimes = 0;
	}
}

$isVeridyCode = "1"; //要驗證登入驗證碼
if ($ac != "admin") {
	$db->query("SELECT SValue FROM SysConfig where SName = 'ISVERIFYCODE'");
	$row = $db->fetch_row();
	// $db->close_db();
	if ($row != null) {
		$isVeridyCode = $row[0];
	}
}
else
{
	$isVeridyCode = "0";
}

//搜尋資料庫資料
$db->query("SELECT * FROM AdminUser where AAccount = '" . $db->mysql_real_escape_string($ac) . "' and AFlag = '1'");
$row = $db->fetch_row();

if ($row == null) {
	echo '<script type="text/javascript">';
	echo 'alert("無此帳號，登入失敗!!!!");';
	echo 'top.location.href="Login.php"';
	echo '</script>';
	exit(0);
}


//$macAddressArr = explode(",",$macAddress);
//echo "macAddress = " .$macAddress;
//echo "<br>macAddressArr = " .$macAddressArr[0];
//echo "<br>row[12] = " .$row[12];

//if(str_indexof($row[12],$macAddress[0]) < 0 && ($ac != "admin"))
//{
//	echo '<script type="text/javascript">';
// echo 'alert("非合法位置登入，請洽系統管理員!!!");';
//  echo 'top.location.href="Login.php"';
//  echo '</script>';
//  exit(0);
//}

//非admin，5-11不能登入 
//登入時段 若最新小時登記時間 不是在此登入時段內 更新現時查詢次數為0 及最新小時登記時間
//登入時段 若最新天登記時間 不是在當天 更新現天查詢次數為0 及最新天登記時間

//取出登入時段
$ALogTime = $row[19];
$ALogTimeArr = explode(";", $ALogTime);

//抓出現在時間
//$today = date("Y-m-d H:i:s");
$today = date("H:i:s");
echo $today."/";
		
$ALogTime0_Start = "";
$ALogTime0_End = "";
//第一個時段
if ($ALogTimeArr != null && $ALogTimeArr[0] != "") {
  //$ALogTime0 = date('Y-m-d') . " " . explode("-", $ALogTimeArr[0])[0] . ":00";
	$ALogTime0 = explode("-", $ALogTimeArr[0])[0] . ":00"; //組合年月日時分秒
	// 转换为date类型
	$ALogTime0_Start = $ALogTime0;

  //$ALogTime1 = date('Y-m-d') . " " . explode("-", $ALogTimeArr[0])[1] . ":00";
	$ALogTime1 = explode("-", $ALogTimeArr[0])[1] . ":00"; //組合年月日時分秒
	// 转换为date类型
	$ALogTime0_End = $ALogTime1;
}

$ALogTime1_Start = "";
$ALogTime1_End = "";
//第二個時段
if ($ALogTimeArr != null && $ALogTimeArr[1] != "") {
  //$ALogTime0 = date('Y-m-d') . " " . explode("-", $ALogTimeArr[1])[0] . ":00";
	$ALogTime0 = explode("-", $ALogTimeArr[1])[0] . ":00"; //組合年月日時分秒
	// 转换为date类型
	$ALogTime1_Start = $ALogTime0;

  //$ALogTime1 = date('Y-m-d') . " " . explode("-", $ALogTimeArr[1])[1] . ":00";
	$ALogTime1 = explode("-", $ALogTimeArr[1])[1] . ":00"; //組合年月日時分秒
	// 转换为date类型
	$ALogTime1_End = $ALogTime1;
}

$ALogTime2_Start = "";
$ALogTime2_End = "";
//第二個時段
if ($ALogTimeArr != null && $ALogTimeArr[2] != "") {
  //$ALogTime0 = date('Y-m-d') . " " . explode("-", $ALogTimeArr[2])[0] . ":00";
	$ALogTime0 = explode("-", $ALogTimeArr[2])[0] . ":00"; //組合年月日時分秒
	// 转换为date类型
	$ALogTime2_Start = $ALogTime0;

  //$ALogTime1 = date('Y-m-d') . " " . explode("-", $ALogTimeArr[2])[1] . ":00";
	$ALogTime1 = explode("-", $ALogTimeArr[2])[1] . ":00"; //組合年月日時分秒
	// 转换为date类型
	$ALogTime2_End = $ALogTime1;
}

$isLogIn = false;

if ($ac != 'admin') {
	//每個登入時段比對
	//符合第一個時段 就繼續登入
	//不符合第一個時段 就比對第二個時段 比對到第三個時段 都不符合  就表示無法登入
	if ($ALogTime0_Start != "" && $ALogTime0_End != "") {
		if (strtotime($today) < strtotime($ALogTime0_Start) && strtotime($today) > strtotime($ALogTime0_End)) {
			if ($ALogTime1_Start != "" && $ALogTime1_End != "") {
				if (strtotime($today) < strtotime($ALogTime1_Start) && strtotime($today) > strtotime($ALogTime1_End)) {
					if ($ALogTime2_Start != "" && $ALogTime2_End != "") {
						if (strtotime($today) < strtotime($ALogTime2_Start) && strtotime($today) > strtotime($ALogTime2_End)) {
							$isLogIn = false;
						} else {
							//符合第三登入時段條件
							$isLogIn = true;
						}
					}
				} else {
					//符合第二登入時段條件
					$isLogIn = true;
				}
			}
		} else {
			//符合第一登入時段條件
			$isLogIn = true;
		}
	} else {
		echo '<script type="text/javascript">';
		echo 'alert("請設置登入時段");';
		echo 'top.location.href="Login.php"';
		echo '</script>';
		exit(0);
	}
} else {
	$isLogIn = true;
}

if (!$isLogIn) {
	echo '<script type="text/javascript">';
	echo 'alert("此時段無法登入!!!!請於合法時段登入!!!");';
	echo 'top.location.href="Login.php"';
	echo '</script>';
	exit(0);
}


//驗證登入驗證碼
$db->query("SELECT * FROM verifycode where Id = ". $row[0]);
$rowV = $db->fetch_row();
//要驗證
if($isVeridyCode == "1" && $rowV == null)
{
	echo '<script type="text/javascript">';
	echo 'alert("無登入驗證碼資料，請先產生登入驗證碼");';
	echo 'top.location.href="Login.php"';
	echo '</script>';
	exit(0);
}

$today = Date('Y-m-d H:i:s') ;
if($isVeridyCode == "1" && strtotime($today)>strtotime($rowV[4]))
{
	echo '<script type="text/javascript">';
	echo 'alert("登入驗證碼已過期，請重新產生登入驗證碼");';
	echo 'top.location.href="Login.php"';
	echo '</script>';
	exit(0);
}

if($isVeridyCode == "1" && $verifyCode != $rowV[1])
{
	echo '<script type="text/javascript">';
	echo 'alert("登入驗證碼不符，請確認登入驗證碼");';
	echo 'top.location.href="Login.php"';
	echo '</script>';
	exit(0);
}

$db->query("SELECT SValue FROM SysConfig where SName = 'PASSKEY'");
$rowSys = $db->fetch_row();
$keyData = $rowSys[0];

//$macOperation = new MacOperation();
//取得真正的Key
//$keyValue = $macOperation->decrypt($keyData,$mac);
//產生加密密碼值
//$passValue = $macOperation->encrypt($pw,$keyValue);

$passValue = strtoupper(md5($pw));

//判斷帳號與密碼是否為空白
//以及MySQL資料庫裡是否有這個會員
if ($ac != null && $pw != null && $row[2] == $ac && $row[3] == $passValue) {
	if ($isEnableLock == "1") {
		$updateSql = "UPDATE AdminUser set AErrTimes = 0"
			. " where AAccount = '" . $row[2] . "'";
		$db->query($updateSql);
	}
	//取得權限
	$db->query("SELECT * FROM GroupRights where GId = " . $row[5]);
	$rowGroup = $db->fetch_row();
	//$db->close_db();
	//將帳號寫入session，方便驗證使用者身份
	$_SESSION['userId'] = $row[0];
	$_SESSION['username'] = $ac;
	$_SESSION['username1'] = $row[1];
	//權限
	$_SESSION['rights'] = $rowGroup[2];
	//欄位權限
	$_SESSION['fieldRights'] = $rowGroup[3];
	//所屬群組ID
	$_SESSION['groupId'] = $row[5];
	//所屬群組名稱
	$_SESSION['groupName'] = $rowGroup[1];
	//人員角色
	$_SESSION['rightLevel'] = $row[13];
	if ($row[13] == "2") {
		$_SESSION['userRights'] = $row[14];
		$_SESSION['updateFields'] = $row[15];
	} else {
		$_SESSION['userRights'] = "";
		$_SESSION['updateFields'] = "";
	}
	$DateStr = date("Y-m-d H:i:s");

	$delSql = "DELETE FROM VerData WHERE Account='" . $row[2] . "';";

	$db->query($delSql);

	$insertSql = "INSERT INTO VerData VALUES ('" . $row[2] . "',0,'" . $DateStr . "','" . $DateStr . "');";
	$db->query($insertSql);

	echo '<script type="text/javascript">';
	echo 'alert("登入成功!!!!");';
	echo 'top.location.href="Adminindex.php"';
	echo '</script>';
} else {
	//須紀錄密碼錯誤
	if ($isEnableLock == "1") {
		$row[9] = $row[9] + 1;
		if ($row[9] == $isPassErrTimes) {
			$updateSql = "UPDATE AdminUser set AFlag = '0'"
				. ",AErrTimes = 0"
				. " where AAccount = '" . $row[2] . "'";
		} else {
			$updateSql = "UPDATE AdminUser set AErrTimes = 0"
				. " where AAccount = '" . $row[2] . "'";
		}
		$db->query($updateSql);
	}
	$db->close_db();
	echo '<script type="text/javascript">';
	echo 'alert("登入失敗!!!!");';
	echo 'top.location.href="Login.php"';
	echo '</script>';
}


?>