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

start_session(600);

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
if($_SESSION['username'] == null || $_SESSION['username'] == "")
{
	echo '<script type="text/javascript">';
  echo 'alert("請重新登入使用!!!!");';
  echo 'top.location.href="Login.php";';
  echo '</script>';
  exit;
}

//檢查是否是此帳號登入時段 如果不是 則強制登出
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");
require_once("class/MacOperation.php");
//檢查狀態
$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
//搜尋資料庫資料
$db->query("SELECT * FROM AdminUser where AId = ".$_SESSION['userId']);
$row = $db->fetch_row();

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

//抓出現在時間
//$today = date("Y-m-d H:i:s");
$today = date("H:i:s");

$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 ($_SESSION['username'] != '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);
}

?>
