<?php
	require_once "pageBase.php";
	require_once "../dal/userDAL.php";
	require_once "../bin/system/utility.php";
	require_once "../bin/web/tables/sends.php";

	class _quick_login extends pageBase {
		//覆蓋父類load()方法
		public function load() {
			/* 錯誤鎖定 */
			$aryErr = [];
			if (file_exists("loginerror.txt")) {
				$errlog = file_get_contents("loginerror.txt");
				if (!empty($errlog)) $aryErr = json_decode($errlog, true);
			}
			/* 錯誤鎖定 */
			
			/* 參數檢查 */
			$checkCode = $_GET["cc"];
			if (!preg_match("/^[A-Z]{10}$/", $checkCode)) {
				$this->handleError('參數異常!請重新整理頁面再試一次!');
				exit;
			}
			/* 檢索快速登入資料 */
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, "https://tq33.org/k_quick_login2-{$checkCode}.html");
			curl_setopt($ch, CURLOPT_HEADER, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$txtJ = curl_exec($ch);
			curl_close($ch);
			$objJ = json_decode($txtJ);
			/* 檢查快速登入網域 */
			if ($objJ->error == "0") {
				$loginName = $objJ->login;
				$password = $objJ->pass;
				$websiteUrl = $objJ->url;
				$http_host = preg_replace("/^https?:/", "", $websiteUrl);
				$http_host = preg_replace("/^\/\//", "", $http_host);
				$http_host = preg_replace("/^www\./", "", $http_host);
				$http_host = preg_replace("/\/.*$/", "", $http_host);
				/* 檢查快速登入網域 */
				if (preg_replace("/^www\./", "", $_SERVER["HTTP_HOST"]) != $http_host) {
					$this->handleError('快速登入網域與本網域不吻合!請勿非法操作!');
					exit;
				}

				/* 快速登入 */
				$dalU = new userDAL();
				$dalU->LoginName = $loginName;
				$dalU->loadRecord();
				if (!isset($dalU->UserID) || $dalU->UserID === "" || $dalU->UserID === 0 || ($dalU->Flags & 1) > 0 || (($dalU->UserType & userType::ADMIN) === 0 && ($dalU->UserType & userType::ADMINISTRATOR) === 0)) {
					$this->loginError($loginName, $password, $checknum);
					$this->handleError('用戶不存在,請聯繫管理員!');
				} else if ($password !== $dalU->Password) {
					$this->loginError($loginName, $password, $checknum);
					$this->handleError('密碼錯誤,請聯繫管理員!');
				} else if ($dalU->Visible != "1") {
					$this->loginError($loginName, $password, $checknum);
					$this->handleError('管理帳號已被停權!');
				} else {
					/* 錯誤鎖定 */
					if (file_exists("loginerror.txt")) unlink('loginerror.txt');
	
					$info = new userInfo();
					$info->userID = $dalU->UserID;
					$info->loginName = $dalU->LoginName;
					$info->userName = $dalU->UserName;
					$info->userType = $dalU->UserType;
	
					$_SESSION[sessionKeys::ADMIN_USER_ID] = $dalU->UserID;
					$_SESSION[sessionKeys::USER_INFO] = $info;
	
					$dalU->LoginTimes = time();
					$dalU->LastLoginIp = $this->getIP();
					$dalU->LastLoginTime = date("Y-m-d H:i:s");
					$dalU->updateRecord();

					header("Location: /iweb/");
				}
			} else {
				$this->handleError($objJ->msg);
			}

			parent::load();
		}
		
		// 錯誤鎖定
		private function loginError($loginName, $password, $checknum) {
			$aryErr[] = [
				"Time" => date("Y-m-d H:i:s"),
				"IP" => $this->getIP(),
				"LoginName" => $loginName !== null ? urlencode($loginName) : null,
				"Password" => $password !== null ? urlencode($password) : null,
				"Checknum" => $checknum !== null ? urlencode($checknum) : null,
			];
			$newlog = json_encode($aryErr);
			file_put_contents("loginerror.txt", $newlog);
		}

		// 錯誤訊息
		private function handleError(string $errorMessage): void {
			echo "<script>" . PHP_EOL;
			echo "alert('" . $errorMessage . "');" . PHP_EOL;
			echo "window.close();" . PHP_EOL;
			echo "</script>";
		}
	}

	$page = new _quick_login();
	$page->load();
	$page->render();
?>