<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class log_errorDAL extends dalBase {
		public $LogIP;
		public $LogCount;
		public $LogTime;

		public function stop_check(): bool {
			$intoAry = [
				parent::addArray(":logIP", $this->LogIP, "str", 50)
			];
			$sql = "SELECT `LogCount`,`LogTime` FROM `log_error` WHERE `LogIP`=:logIP";
			$data = $this->data->executeRecordset($sql, $intoAry, 0, 1);
			if (count($data) === 0) {
				return false;
			} else {
				if (time() - (int)$data[0]["LogTime"] > 3600) {    // 超過1小時，刪除紀錄
					$sql = "DELETE FROM `log_error` WHERE `LogIP`=:logIP";
					$this->data->executeNonQuery($sql, $intoAry, true);
					return false;
				} else if ((int)$data[0]["LogCount"] > 4) {
					return true;
				} else {
					return false;
				}
			}
		}

		public function login_success(): bool {
			$intoAry = [
				parent::addArray(":logIP", $this->LogIP, "str", 50)
			];
			$sql = "DELETE FROM `log_error` WHERE `LogIP`=:logIP";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error);
		}
		
		public function login_error(): bool {
			$intoAry = [
				parent::addArray(":logIP", $this->LogIP, "str", 50)
			];
			$sql = "INSERT INTO `log_error` (`LogIP`,`LogCount`,`LogTime`) VALUES (:logIP,1,UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE `LogCount`=`LogCount`+1";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error);
		}
	}
?>