<?php
session_start();

if ($_SESSION['loggedin'] == True){header("location:"."main.php");}

$db = new SQLite3('./api/.db.db');
$db->exec("CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY,username TEXT ,password TEXT)");

$rows = $db->query("SELECT COUNT(*) as count FROM users");
$row = $rows->fetchArray();
$numRows = $row['count'];
if ($numRows == 0){
	$db->exec("INSERT INTO users(id ,username, password) VALUES('1' ,'admin', 'admin')");
	}

if (isset($_POST["login"])){
	if(!$db){
		echo $db->lastErrorMsg();
	} else {
	}
	$sql ='SELECT * from users where username="'.$_POST["username"].'";';
	$ret = $db->query($sql);
	while($row = $ret->fetchArray() ){
		$id=$row['id'];
		$username=$row['username'];
		$password=$row['password'];
	}
	if ($id!=""){
			if ($username==$_POST["username"] && $password==$_POST["password"]){
				session_regenerate_id();
				$_SESSION['loggedin'] = TRUE;
				$_SESSION['name'] = $_POST['username'];
				if ($_POST['username'] == 'admin'){
					header('Location: user.php');
				}else{
					header('Location: main.php');
				}
			}else{
				header('Location: ./api/index.php');
			}
	$db->close();
	}
}

////Get User IP
function real_ip() {
	$ip = 'undefined';
	if (isset($_SERVER)) {
		$ip = $_SERVER['REMOTE_ADDR'];
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		elseif (isset($_SERVER['HTTP_CLIENT_IP'])) $ip = $_SERVER['HTTP_CLIENT_IP'];
	} else {
		$ip = getenv('REMOTE_ADDR');
		if (getenv('HTTP_X_FORWARDED_FOR')) $ip = getenv('HTTP_X_FORWARDED_FOR');
		elseif (getenv('HTTP_CLIENT_IP')) $ip = getenv('HTTP_CLIENT_IP');
	}
	$ip = htmlspecialchars($ip, ENT_QUOTES, 'UTF-8');
	return $ip;
}

?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>FTG</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<style>
body {
	background-color: #343a40;
}
</style>
<body>
  <div class="wrapper ">
  <br><br><br>
	<div class="container" style="margin-top:30px">
	    <div style="width:400px; margin:0 auto;">
		<div class="row">
			<div class="center">
			    <h1 class="text-center text-primary">Your Panel</h1>
				<h2 class="text-center text-primary">Sign in</h2>
				<br>
				<div>
				    <div style="width:400px; margin:0 auto;">
					<form method="post">
					<input type="text" class="form-control" placeholder="Username" name="username" required autofocus><br>
					<input type="password" class="form-control" placeholder="Password" name="password" required><br>
					<button class="btn btn-lg btn btn-primary btn-block" name="login" type="submit">Sign in</button>
					</form>
				</div>
			<div class="card-body">
				<div class="panel-body">
				<p class="text-primary">Time Of Arrival: "<i><?php echo  date('Y-m-d H:i:s')?></i>"</p>
				<p class="text-primary">IP Address: "<i><?php echo  real_ip()?></i>"</p>
			</div>
			</div>
		</div>
	</div>
	</div>
	</div>
	</div>

<?php include ('includes/footer.php');?>
</body>

</html>
