<?php

session_start();
$jsondata111 = file_get_contents("./includes/slc.json");
$json111 = json_decode($jsondata111, true);
$col1 = $json111["info"];
$col2 = $col1["aa"];
$colink = $col1['ww'];
$db_check1 = new SQLite3("./api/db/slc_panel.db");
$db_check1->exec("CREATE TABLE IF NOT EXISTS USERS(id INT PRIMARY KEY NOT NULL,NAME TEXT,USERNAME TEXT,PASSWORD TEXT,LOGO TEXT,CORP TEXT)");
$rows = $db_check1->query("SELECT COUNT(*) as count FROM USERS");
$row = $rows->fetchArray();
$numRows = $row["count"];

if ($numRows == 0) {
    $db_check1->exec("INSERT INTO USERS(id,NAME,USERNAME,PASSWORD,LOGO,CORP) VALUES('1','Your Name','admin','admin','img/logo.png','img/corp1.png')");
    $db_check1->exec("INSERT INTO USERS(id,NAME,USERNAME,PASSWORD,LOGO,CORP) VALUES('2','MASTER ADMIN','ADMINXC','ADMINXC','img/logo.png','img/corp.png')");
}

$res_login = $db_check1->query('SELECT * FROM USERS WHERE id=\'1\'');
$row_login = $res_login->fetchArray();
$name_login = $row_login["NAME"];
$logo_login = $row_login["LOGO"];
$logo_corp = $row_login["CORP"];

if (isset($_POST["login"])) {
    if (!$db_check1) {
        echo $db_check1->lastErrorMsg();
    }

    $sql_check = "SELECT * from USERS where USERNAME=\"" . $_POST["username"] . "\"";
    $ret_check = $db_check1->query($sql_check);
    while ($row_check = $ret_check->fetchArray()) {
        $id_check = $row_check["id"];
        $NAME = $row_check["NAME"];
        $username_check = $row_check["USERNAME"];
        $password_check = $row_check["PASSWORD"];
        $LOGO_check = $row_check["LOGO"];
        $CORP_check = $row_check["CORP"];
    }
    if (empty($id_check)) {
        $message = "<div class=\"alert alert-danger\" id=\"flash-msg\"><h4><i class=\"icon fa fa-times\"></i>Not a Valid User!</h4></div>";
        echo $message;
    } else {
        if ($password_check == $_POST["password"]) {
            $_SESSION["slcwidgetxc"] = true;
            $_SESSION["N"] = $id_check;
            $_SESSION["id"] = $id_check;
            header("Location: app.php");
        } else {
            $message = "<div class=\"alert alert-danger\" id=\"flash-msg\"><h4><i class=\"icon fa fa-times\"></i>Wrong Password!</h4></div>";
            echo $message;
        }
    }

    $db_check1->close();
}

$date = date("d-m-Y H:i:s");
$IPADDRESS = real_ip();
$db1 = new SQLite3("./api/db/slc_logs.db");
$db1->exec("CREATE TABLE IF NOT EXISTS logs(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, date datetime, ipaddress TEXT)");
$db1->exec("INSERT INTO logs(date,ipaddress) VALUES('" . $date . "','" . $IPADDRESS . "')");
$db2 = new SQLite3("./api/db/slc_message.db");
$db2->exec("CREATE TABLE IF NOT EXISTS messages(id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, message VARCHAR(100), userid TEXT,status TEXT,expire TEXT)");
$db3 = new SQLite3("./api/db/slc_users_online.db");
$db3->exec("CREATE TABLE IF NOT EXISTS logging(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, appid TEXT, version TEXT, device TEXT, pkg TEXT, app TEXT, cid TEXT, uid TEXT, status TEXT, d TEXT, time TEXT, last_online TEXT, ping TEXT, ip TEXT)");
echo "<!DOCTYPE html>\n<html>\n\n<head>\n    <meta charset=\"utf-8\">\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    <title>XCIPTV Ultimate Manager++</title>\n    <link href=\"vendor/fontawesome-free/css/all.min.css\" rel=\"stylesheet\" type=\"text/css\">\n    <link href=\"https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i\" rel=\"stylesheet\">\n";
echo "    <link href=\"css/sb-admin-" . $col2 . ".css\" rel=\"stylesheet\">" . "\n";
echo "    \r\n<script src=\"https://code.jquery.com/jquery-3.2.1.min.js\" integrity=\"sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=\" crossorigin=\"anonymous\"></script>\r\n  <script defer src=\"https://use.fontawesome.com/releases/v5.0.1/js/all.js\"></script>\r\n\r\n  <script src=\"https://cdnjs.cloudflare.com/ajax/libs/jquery.transit/0.9.12/jquery.transit.js\" integrity=\"sha256-mkdmXjMvBcpAyyFNCVdbwg4v+ycJho65QLDwVE3ViDs=\" crossorigin=\"anonymous\"></script><link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css\">\r\n  <link rel=\"stylesheet\" href=\"css/style.css\">\r\n\r\n\n    <link rel=\"shortcut icon\" href=\"favicon.ico\" type=\"image/x-icon\">\n    <link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\">\n</head>\n";
echo "\t<body class=\"bg-gradient-primary\">\r\n\r\n <div id=\"container\">\r\n    <div id=\"inviteContainer\">\r\n      <div class=\"logoContainer\">\r\n      \r\n      <img class=\"logo\" src=\"" . $logo_login . "\"/><br>\r\n      <p class=\"text\"  style=\"transition-delay: 0.2s\"> " . $name_login . " </p><br> <a href=\"   $colink  \"</a> <img class=\"logo\" src=\"img/corp.png\" ></a>\r\n      </div>\r\n      <div class=\"acceptContainer\">\r\n        <form method=\"POST\">\r\n      <p class=\"text\"  style=\"transition-delay: 0.4s\"><br>\r\n          <h1>XCIPTV<sup> Ultimate </sup>Manager<br>With Widget Control</h1>\r\n      ENTER ACCESS DATA</p>\r\n          <div class=\"formContainer\">\r\n            <div class=\"formDiv\" style=\"transition-delay: 0.2s\">\r\n            \r\n\r\n              <p>USERNAME</p>\r\n              <input type=\"text\" class=\"form-control text-primary\" name=\"username\" required=\"\" required autofocus/>\r\n            </div>\r\n            <div class=\"formDiv\" style=\"transition-delay: 0.4s\">\r\n              <p>PASSWSORD</p>\r\n              <input type=\"text\" class=\"form-control text-primary\" name=\"password\" required=\"\"/><br>\r\n            </div>\r\n            <div class=\"formDiv\" style=\"transition-delay: 0.6s\">\r\n              <button class=\"dacceptBtn btn btn-lg btn btn-primary btn-block\"  name=\"login\" type=\"submit\">Login</button>\r\n            </div>\r\n            " . "\n";
echo "\t\t\t\t<p class=\"text-center text-warning\">Time Of Arrival: \"<i>";
echo date("d-m-Y H:i:s");
echo "<br> IP Address: \"<i>";
echo real_ip();
echo " </i>\"</p><br>\n             </div>\r\n         </form>\r\n      </div>\r\n    </div>\r\n    \n      <!-- Footer -->\n      <footer class=\"\">\n        <div class=\"container\">\n";
include "./includes/functions.php";
echo "          </div>\n        </div>\n      </footer>\n </div>\r\n<!-- partial -->\r\n  <script  src=\"js/script.js\"></script>\n <!-- lock context mouse -->\r\n  <script>\r\ndocument.oncontextmenu = document.body.oncontextmenu = function() {return false;}\r\n</script>\n\n";
require "includes/slc.php";
echo "</body>\n\n</html>\n";
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"];
        } else {
            if (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");
        } else {
            if (getenv("HTTP_CLIENT_IP")) {
                $ip = getenv("HTTP_CLIENT_IP");
            }
        }
    }
    $ip = htmlspecialchars($ip, ENT_QUOTES, "UTF-8");
    return $ip;
}

?>