<?php
date_default_timezone_set('Asia/Taipei');
class _myClass {
	public $list_box = "";
	public $btn = "";
	function load() {
		if ($this->chkIP()) {
			$this->list_box = "";
			$this->btn = "";

			$errlogPath = dirname(__FILE__) . "/loginerror.txt";
			if (file_exists($errlogPath)) {
				$errlog = file_get_contents($errlogPath);

				if (!empty($errlog)) {
					$aryErr = json_decode($errlog, true);

					if (count($aryErr) > 0) {
						foreach ($aryErr as $entry) {
							$this->list_box .= <<<HTML
							<li class="d_sm_flex">
								<p class="col_sm_3"><label>時間</label><font>{$entry["Time"]}</font></p>
								<p class="col_sm_3"><label>登入帳號</label><font>{$entry["LoginName"]}</font></p>
								<p class="col_sm_2"><label>登入密碼</label><font>{$entry["Password"]}</font></p>
								<p class="col_sm_2"><label>驗證碼</label><font>{$entry["Checknum"]}</font></p>
								<p class="col_sm_2"><label>IP</label><font>{$entry["IP"]}</font></p>
							</li>
							HTML;
						}
						$this->btn = <<<HTML
						<a class="mb_5 btn btn_pill btn_primary" id="unlock" href="javascript:void(0);">解鎖</a>
						HTML;
					} else {
						header("Location: {$this->getProtocol()}://{$_SERVER['HTTP_HOST']}/iweb");
						exit;
					}
				} else {
					header("Location: {$this->getProtocol()}://{$_SERVER['HTTP_HOST']}/iweb");
					exit;
				}
			} else {
				header("Location: {$this->getProtocol()}://{$_SERVER['HTTP_HOST']}/iweb");
				exit;
			}
		} else {
			header("Location: {$this->getProtocol()}://{$_SERVER['HTTP_HOST']}/iweb/404.php");
			exit;
		}
	}

	function chkIP() {
		$txtIP = $this->load_contents("{$this->getProtocol()}://{$_SERVER['HTTP_HOST']}/api/appleip.json");
		$aryIP = json_decode($txtIP);
		$flag = false;
		
		foreach ($aryIP as $v) {
			if (!empty($v)) {
				$aryIPParts = explode(".", $v);

				for ($i = 0; $i < count($aryIPParts); $i++) {
					if (trim($aryIPParts[$i]) == "*" || trim($aryIPParts[$i]) == "") $aryIPParts[$i] = "\d{1,3}";
				}

				for ($i = 4; $i > count($aryIPParts); $i--) {
					$aryIPParts[] = "\d{1,3}";
				}

				$ipW = implode("\.", $aryIPParts);
				$ipW = "/{$ipW}/";
	
				if (preg_match($ipW, $this->getIP())) {
					$flag = true;
					break;
				}
			}
		}

		return $flag;
	}
	
	function getProtocol() {
		return ($_SERVER["HTTPS"] ?? "") === "on" ? "https" : "http";
	}

	function getIP() {
		if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
			$myip = $_SERVER['HTTP_CLIENT_IP'];
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$myip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else {
			$myip = $_SERVER['REMOTE_ADDR'];
		}
		return $myip;
	}

	function load_contents($url) {
		$ch = curl_init();
	
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
	
		$data = curl_exec($ch);
		curl_close($ch);
	
		return $data;
	}
}
$page = new _myClass;
$page->load();
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<title>後台解鎖</title>
<meta name="robots" content="noindex, nofollow" />
<link href="css/main.css" rel="stylesheet">
<link href="less/login.less" rel="stylesheet/less">
<script src="/jquery/jquery-1.10.js"></script>
<script src="/js/less.min.js" ></script>
</head>
<body class="d_flex f_justify_center f_aitems_center flex_direction">
    <h2 class="mt_5">後台解鎖</h2>
	<div class="mt_5 mb_5 col_xxl_8 col_11 card">
        <ul class="list_box"><?php echo $page->list_box; ?></ul>
    </div>
	<?php echo $page->btn; ?>
</body>
</html>
<script>
$(function(){
	$("#unlock").click(function(){
		$.ajax({
			type: 'POST',
			url: 'ajax/unlock.php',
			dataType: "json",
			data: {'do':'unlock'},
			success: function(data) {
				alert(data.msg);
				location.href = data.link;
            },
            error:function(){
                alert('系統異常!請重新整理頁面再試一次!');
                location.href = location.href;
			}
		});
	});
});
</script>