<?php
	//user defined information
	$udir = "UserDB/";
	$username = $_POST['userID'];
	$enteredpw = $_POST['userPWD'];
	
	$exist = file_exists($udir . $username . ".txt");
	
	if ($exist == true) {
		$filename = $udir . $username . ".txt";
		$file = fopen($filename,"r");
			$readed = fread($file,filesize($filename));
			fclose($file);
		$rearray = explode("\r\n",$readed);

		$hashf = $rearray[0];
	}
	else {
		header("Location: wrong-pass.html");
	}
	
	//verify user not trying to fuck this shitty web
	$userexploit = isset($username);

	//Verify it and tell what to do if
		$pstat = password_verify($enteredpw, $hashf);
		
		if($userexploit) {
			if($pstat == false) {
				header("Location: wrong-pass.html");
			}
		}
		else {
			header("Location: anti-exploit.html");
		}
?>

<!DOCTYPE HTML>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>MyBank</title>
	</head>
	<body>
		<style>
			html, body {
			margin: 0;
			padding: 0;
		</style>
		
		<style>
			.header {
			  padding: 20px;
			  text-align: center;
			  background: #1abc9c;
			  color: white;
			  font-size: 30px;
			}
			
			.footer {
				padding: 12px;
				text-align: left;
				background: #4d4c5e;
				color: white;
				font-size: 11px;
			}
			
			.main {
				padding-top: 1px;
				padding-bottom: 1px;
				padding-left: 15px;
				background: #e6e6eb;
			}
			
			.btn-danger {
				background: #ff4a4a;
				color: white;
			}
		</style>
		
		<div class="header">
			<p style=color:white>Bank Main Page</p>
		</div>
		
		<div class="main">
			<p>
				Selamat datang <b><?php echo $username; ?></b> Di bank!<br>
				Sisa Saldo Anda: MBC. <?php echo $rearray[1]; ?><br>
				<?php if((int)$rearray[1] < 0){echo "WARNING! YOUR BALANCE IS LESS THAN MBC. 0";} ?>
			</p>
			
			
			<!-- <p><b>DEBUG:</b><br>
			pass="" <br>
			with hash ="" <br>
			With status="<?php if($pstat){echo "Verified";} else{echo "not at all";} ?>"</p> -->
			
			<p><b>Mau Meng Transfer Uang?</b></p>
				<form method="POST" action="transfer.php">
					<!-- <label for="username">Nama: </label> -->
					<input type="hidden" name="username" id="username" readonly value="<?php echo $username; ?>">
				
					<label for="userTarget">Nama Target: </label>
					<input type="text" name="userTarget" id="userTarget"><br>
					
					<label for="userPWT">Password: </label>
					<input type="password" name="userPWT" id="userPWT"><br>
					
					<label for="moneyval">How Many? </label>
					<input type="number" name="moneyval" id="moneyval">
					
					<input type="submit" value="Transfer!" id="tid">
				</form>
				
			<p><b>Mau TopUp CShop?</b></p>
				<form method="POST" action="CShopTPU.php" id="tpuf">
					<!-- <label for="username">Nama: </label> -->
					<input type="hidden" name="username" id="username" readonly value="<?php echo $username; ?>">
				
					<label for="userAccount">Akun CShop Anda: </label>
					<input type="text" name="userAccount" id="userAccount"><br>
					
					<label for="PW">Password: </label>
					<input type="password" name="PW" id="PW"><br>
					
					<label for="manyvalue">How Many? </label>
					<input type="number" name="manyvalue" id="manyvalue">
					
					<input type="submit" value="TopUp">
				</form>
			
			<p><b>Mau Ganti Password?</b></p>
				<form method="POST" action="changePW.php">
					<!-- <label for="username">Nama: </label> -->
					<input type="hidden" name="username" id="username" readonly value="<?php echo $username; ?>">
				
					<label for="oldPW">Old Password: </label>
					<input type="password" name="oldPW" id="oldPW"><br>
					
					<label for="newPW">New Password: </label>
					<input type="password" name="newPW" id="newPW"><br>
					
					<label for="newPWC">Confirm New Password: </label>
					<input type="password" name="newPWC" id="newPWC"><br>
					
					<input type="submit" value="Change password">
				</form>
			<br>
			
			<button onclick="ReplaceLinkButton()" class="btn-danger">Log Out</button>
			
			<script>
				function ReplaceLinkButton() {
				  location.replace("./")
				}
			</script>
			
			<script>
				var tbtn = document.getElementById("tid");
				var tpuf = document.getElementById("tpuf");
				var outs = "<?php if((int)$rearray[1] < 1){echo "out";} else{echo "ok";} ?>";
				
				if (outs == "out") {
					tbtn.parentElement.style.display = "none";
					tpuf.style.display = "none";
				}
			</script>
		</div>
		
		<div class="footer">
			<p>Made with love using HTML generator!</p>
		</div>
	</body>

</html>