<?php

$db = new SQLite3("./api/.acces.db");
$db->exec("CREATE TABLE IF NOT EXISTS USERS(ID INT PRIMARY KEY,NAME TEXT,USERNAME TEXT,PASSWORD TEXT,LOGO 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 ,NAME,USERNAME,PASSWORD,LOGO) VALUES('1','Your Name','admin','admin','img/logo.png')");
}

if( isset($_POST["login"]) ) 
{
    $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( empty($id) ) 
    {
        header("Location: ./api/index.php");
    }

    if( $id != "" ) 
    {
        if( $password == $_POST["password"] ) 
        {
            session_start();
            $_SESSION["loggedin"] = true;
            $_SESSION["N"] = $_POST["username"];
            header("Location: dns.php");
        }
        else
        {
            $message = "<div class=\"alert alert-danger\" id=\"flash-msg\"><h4><i class=\"icon fa fa-times\"></i>Wrong Password!</h4></div>";
            header("Location: login.php?m=" . $message);
        }

    }
    else
    {
        echo "<div class=\"alert alert-danger\" id=\"flash-msg\"><h4><i class=\"icon fa fa-times\"></i>User Not Exist!</h4></div>";
    }

    $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>
<!-- Required meta tags -->
<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        margin-left: auto ;
  margin-right: auto ;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
          margin-left: auto ;
  margin-right: auto ;
        }
      }
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        margin-left: auto ;
  margin-right: auto ;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
          margin-left: auto ;
  margin-right: auto ;
        }
        
  body {
        background: black }
    .container {
        background-image: radial-gradient(circle, #d16ba5, #c777b9, #ba83ca, #aa8fd8, #9a9ae1, #8aa7ec, #79b3f4, #69bff8, #52cffe, #41dfff, #46eefa, #5ffbf1);
        color: white;
        border-radius: 1em;
        padding: 1em;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-right: -50%;
        transform: translate(-50%, -50%) 
		}
		.logo {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}
      
      
    </style>
  </head>

  <body class="text-center">

<body>
    
	<div class="container" style="margin-top:30px">
	    <div style="width:400px; margin:0 auto;">
		<div class="row">
			<div class="center">
			<img src="img/logo.png" width="200" height="100 "class="logo">
			    <h3 class="text-center">AppsNScripts OTT Panel</h3>
				<h2 class="text-center">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-primary btn-block" name="login" type="submit">
						Sign in</button>
					</form>
				</div>
			<div class="card-body">
				<div class="panel-body">
				<p>Time Of Arrival: "<i><?php echo  date('Y-m-d H:i:s')?></i>"</p>
				<p>IP Address: "<i><?php echo  real_ip()?></i>"</p>
			</div>
			</div>
				</div>
			</div>
		</div>
	</div>
	<center><a href="https://api.whatsapp.com/send/?phone=5543984778034/">TR4app</a></center>
	</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->	
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
