<?php
session_start();
//require ('antiflood.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')");
	}

$log_check = $db->query("SELECT * FROM users WHERE id='1'");
$roe = $log_check->fetchArray();
if ($_COOKIE['auth'] == $roe['username']){header("location:"."dns1manager.php");}

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 ($password==$_POST["password"]){
			setcookie("auth",$username);
			header('Location: user.php');    
		}else{
		header('Location: ./api/index.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;
}

$curr = basename($_SERVER['PHP_SELF']);
$page = substr($curr, 0, 3);

?>
<!doctype html>
<html lang="en">
  <head>
    <title>LiveWireApps Legazy Panel</title>
  <meta charset="utf-8" />
  <link rel="apple-touch-icon" sizes="76x76" href="assets/img/apple-icon.png">
  <link rel="icon" type="image/png" href="assets/img/fav.png">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

    <!--     Fonts and icons     -->
    <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" />
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <!-- Material Dashboard CSS -->
    <link rel="stylesheet" href="assets/css/material-dashboard.css?v=2.1.0" rel="stylesheet" />
<style>
    select * {
        background-color:#202940;
        color: #000;
    }
	@-webkit-keyframes autofill {
	to {
		color: white;
		background: transparent;
	}
	}
	
	input:-webkit-autofill {
	-webkit-animation-name: autofill;
	-webkit-animation-fill-mode: both;
	}
</style>
<body class="dark-edition">
  <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-warning">LiveWireApps Legazy Panel</h1>
				<h2 class="text-center text-warning">Sign in</h2>
				<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-warning btn-block" name="login" type="submit">Sign in</button>
					</form>
				</div>
			<div class="card-body">
				<div class="panel-body">
				<p class="text-warning">Time Of Arrival: "<i><?php echo  date('Y-m-d H:i:s')?></i>"</p>
				<p class="text-warning">IP Address: "<i><?php echo  real_ip()?></i>"</p>
			</div>
			</div>
		</div>
	</div>

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

</html>
