<?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(32767);
session_start();

$jsondata111 = file_get_contents("./includes/ansibo.json");
$json111 = json_decode($jsondata111, true);
$col1 = $json111["info"];
$col2 = $col1["aa"];

$db  = new SQLite3("api/.ansdb.db");
$db2 = new SQLite3("api/.anspanel.db");

$res_login  = $db2->query("SELECT * FROM USERS WHERE id='1'");
$row_login  = $res_login->fetchArray();
$name_login = "MY FULL";
$logo_login = $row_login["LOGO"];
$corp_login = $row_login["CORP"];
$db2->close();

if (isset($_POST["login"])) {
    $store_id = '';
    if (isset($_POST["store"])) {
        $store_id = $_POST["store"];
    }

    $mac      = strtoupper($_POST['mac']);
    $macRegex = '/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}.[0-9a-fA-F]{4}.[0-9a-fA-F]{4})$/';
    $validMac = preg_match($macRegex, $mac);

    if ($validMac) {
        $sql_check = "SELECT COUNT(*) as count from ibo where mac_address='$mac' AND store_type = 2";
        $ret_check = $db->query($sql_check);
        $count     = $ret_check->fetchArray()['count'];

        $expire = date('Y-m-d', strtotime('+7 days'));

        $limited = false;
        if ($count == 0) {
            if ($store_id) {
                $dbUsers  = new SQLite3("./api/.anspanel.db");
                $res      = $dbUsers->query("SELECT mac_amount FROM USERS WHERE id = '$store_id' ");
                $macCount = $res->fetchArray()['mac_amount'];
                $dbUsers->close();

                $res   = $db->query("SELECT COUNT(*) as count FROM ibo WHERE id_user = '$store_id' AND active = 1 AND expire_date > date('now')");
                $count = $res->fetchArray()['count'];

                $limited = $count >= $macCount;
            }

            if (!$limited) {
                $db->exec("INSERT INTO ibo (title, mac_address, expire_date, id_user, store_type) VALUES ('Playlist $mac', '$mac', '$expire', '$store_id' ,2)");
            }
        }

        $sql_check = "SELECT * from ibo where mac_address='$mac' AND store_type = 2";
        $ret_check = $db->query($sql_check);

        while ($row_check = $ret_check->fetchArray()) {
            $id_check    = $row_check["id"];
            $mac_address = $row_check['mac_address'];
            $key         = $row_check['key'];
        }

        if (empty($id_check)) {
            if ($limited) {
                echo '<div class="alert alert-danger">MAC limit exceeded for this store</div>';
            } else {
                echo '<div class="alert alert-danger">Error. Please try again.</div>';
            }
        } else {
            $_SESSION["admin"]       = false;
            $_SESSION["id"]          = $id_check;
            $_SESSION["mac_address"] = $mac_address;

            header("Location: playlists_mac.php");
        }
        $db->close();
    } else {
        echo '<div class="alert alert-danger">Invalid MAC</div>';
    }
}

$date      = date("d-m-Y H:i:s");
$IPADDRESS = real_ip();
?>

<!DOCTYPE html>
<html lang="en">
<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>User Panel</title>

    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <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">
    <link href="css/sb-admin-<?php echo $col2; ?>.css" rel="stylesheet">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="icon" href="favicon.ico" type="image/x-icon">

    <!-- Custom ESso4K neon login theme -->
    <style>
        :root{
            --esso-bg: #020617;
            --esso-bg-soft: #020617;
            --esso-card: #020617;
            --esso-accent1: #00e0ff;
            --esso-accent2: #8a2eff;
            --esso-success: #22c55e;
            --esso-text-main: #e5e7eb;
            --esso-text-soft: #9ca3af;
        }

        *{
            box-sizing:border-box;
        }

        body.mac-body{
            min-height:100vh;
            margin:0;
            font-family:'Nunito', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
            background:
                radial-gradient(circle at top left, rgba(45,212,191,0.16), transparent 55%),
                radial-gradient(circle at bottom right, rgba(129,140,248,0.22), transparent 60%),
                radial-gradient(circle at top right, rgba(56,189,248,0.16), transparent 55%),
                #020617;
            color:var(--esso-text-main);
            display:flex;
            align-items:center;
            justify-content:center;
            padding:20px;
        }

        .mac-shell{
            width:100%;
            max-width:480px;
        }

        .mac-card{
            position:relative;
            background:radial-gradient(circle at top, #020617 0, #020617 45%, #020617 100%);
            border-radius:28px;
            padding:34px 32px 30px;
            box-shadow:
                0 26px 70px rgba(0,0,0,0.95),
                0 0 0 1px rgba(148,163,184,0.40);
            overflow:hidden;
        }

        .mac-card::before{
            content:"";
            position:absolute;
            inset:-1px;
            border-radius:inherit;
            background:
                conic-gradient(from 220deg,
                    rgba(56,189,248,0.00) 0deg,
                    rgba(56,189,248,0.75) 80deg,
                    rgba(168,85,247,0.75) 160deg,
                    rgba(34,197,94,0.75) 240deg,
                    rgba(56,189,248,0.00) 330deg);
            filter:blur(20px);
            opacity:0.85;
            z-index:-1;
            animation:spinHalo 18s linear infinite;
        }

        @keyframes spinHalo{
            to{ transform:rotate(360deg); }
        }

        .mac-logo-wrap{
            width:110px;
            height:110px;
            margin:0 auto 14px auto;
            position:relative;
            display:flex;
            align-items:center;
            justify-content:center;
        }

        .mac-logo-halo{
            position:absolute;
            inset:-16px;
            border-radius:999px;
            background:
                conic-gradient(
                    from 0deg,
                    rgba(34,211,238,0.0),
                    rgba(34,211,238,0.9),
                    rgba(168,85,247,0.9),
                    rgba(52,211,153,0.9),
                    rgba(34,211,238,0.0)
                );
            filter:blur(18px);
            opacity:0.95;
            animation:spinHaloInner 10s linear infinite;
        }

        @keyframes spinHaloInner{
            to{ transform:rotate(-360deg); }
        }

        .mac-logo-img{
            position:relative;
            width:100%;
            height:100%;
            object-fit:contain;
            border-radius:24px;
            background:#020617;
            box-shadow:
                0 12px 30px rgba(0,0,0,0.9),
                0 0 22px rgba(34,211,238,0.65);
        }

        .mac-title{
            text-align:center;
            font-size:1.4rem;
            letter-spacing:.24em;
            text-transform:uppercase;
            margin:4px 0 6px;
            color:#f9fafb;
        }

        .mac-subtitle{
            text-align:center;
            font-size:.9rem;
            color:var(--esso-text-soft);
            margin-bottom:22px;
        }

        .mac-form{
            margin-top:4px;
        }

        .mac-label{
            display:block;
            font-size:.78rem;
            text-transform:uppercase;
            letter-spacing:.16em;
            color:#9ca3af;
            margin-bottom:6px;
        }

        .mac-input-wrap{
            position:relative;
        }

        .mac-input{
            width:100%;
            border-radius:999px;
            border:1px solid rgba(148,163,184,0.6);
            background:rgba(15,23,42,0.98);
            padding:12px 16px;
            padding-left:18px;
            font-size:.93rem;
            color:var(--esso-text-main);
            outline:none;
            transition:border-color .18s ease, box-shadow .18s ease, background .18s ease, transform .08s ease;
        }

        .mac-input::placeholder{
            color:#6b7280;
        }

        .mac-input:focus{
            border-color:var(--esso-accent1);
            background:#020617;
            box-shadow:0 0 0 1px rgba(56,189,248,0.55);
            transform:translateY(-1px);
        }

        .mac-btn{
            width:100%;
            margin-top:18px;
            border:none;
            border-radius:999px;
            padding:12px 20px;
            font-weight:700;
            font-size:.95rem;
            letter-spacing:.18em;
            text-transform:uppercase;
            background:linear-gradient(90deg,#22c55e,#16a34a);
            color:#022c22;
            box-shadow:0 18px 40px rgba(22,163,74,0.6);
            cursor:pointer;
            transition:transform .12s ease, box-shadow .12s ease, filter .15s ease;
        }

        .mac-btn:hover{
            filter:brightness(1.05);
            transform:translateY(-1px);
            box-shadow:0 22px 48px rgba(22,163,74,0.75);
        }

        .mac-meta{
            margin-top:18px;
            font-size:.78rem;
            text-align:center;
            color:#9ca3af;
        }

        .mac-meta i{
            color:#e5e7eb;
        }

        @media (max-width:480px){
            .mac-card{
                padding:26px 20px 24px;
            }
            .mac-title{
                font-size:1.25rem;
                letter-spacing:.18em;
            }
        }
    </style>
</head>

<body class="mac-body">
    <div class="mac-shell">
        <div class="mac-card">
            <div class="mac-logo-wrap">
                <div class="mac-logo-halo"></div>
                <img src="<?php echo htmlspecialchars($logo_login, ENT_QUOTES, 'UTF-8'); ?>" alt="Logo" class="mac-logo-img">
            </div>

            <h1 class="mac-title">MAC PANEL</h1>
            <p class="mac-subtitle">Please enter your MAC address to access your playlists.</p>

            <form method="POST" class="mac-form">
                <label for="mac" class="mac-label">MAC Address</label>
                <div class="mac-input-wrap">
                    <input
                        type="text"
                        class="mac-input"
                        id="mac"
                        name="mac"
                        maxlength="17"
                        placeholder="AA:BB:CC:DD:EE:FF"
                        required
                        autofocus
                    >
                </div>

                <button class="mac-btn" name="login" type="submit">Login</button>

                <p class="mac-meta">
                    CURRENT DATE &amp; TIME:
                    "<i><?php echo htmlspecialchars($date, ENT_QUOTES, 'UTF-8'); ?></i>"
                    <br>
                    IP ADDRESS:
                    "<i><?php echo real_ip(); ?></i>"
                </p>
            </form>
        </div>
    </div>

    <?php require "includes/ans.php"; ?>

    <script type="text/javascript">
        (function () {
            var element = document.getElementById("mac");
            if (!element) return;

            element.addEventListener('keydown', function () {
                var mac = element.value;
                var macs = mac.split(':').join('');
                macs = chunk(macs, 2).join(':');
                element.value = macs.toString();
            });

            function chunk(str, n) {
                var ret = [];
                var i, len;
                for (i = 0, len = str.length; i < len; i += n) {
                    ret.push(str.substr(i, n));
                }
                return ret;
            }
        })();
    </script>
</body>
</html>

<?php
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;
}
?>
