<?php
session_start();

$config = require '/etc/foxio-tv.php';

if (!isset($_SESSION['tv_csrf'])) {
    $_SESSION['tv_csrf'] = bin2hex(random_bytes(32));
}

$csrf = $_SESSION['tv_csrf'];

$logged = !empty($_SESSION['tv_auth']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    header('Content-Type: application/json; charset=utf-8');

    $action = $_POST['action'] ?? '';
    $token  = $_POST['csrf'] ?? '';

    if (!hash_equals($csrf, $token)) {
        http_response_code(403);
        echo json_encode([
            'ok' => false,
            'msg' => 'Nieprawidłowy token bezpieczeństwa.'
        ]);
        exit;
    }

    if ($action === 'login') {

        $password = $_POST['password'] ?? '';

        if (
            empty($password) ||
            empty($config['password_hash']) ||
            !password_verify($password, $config['password_hash'])
        ) {
            http_response_code(401);
            echo json_encode([
                'ok' => false,
                'msg' => 'Nieprawidłowe hasło.'
            ]);
            exit;
        }

        $_SESSION['tv_auth'] = true;

        echo json_encode([
            'ok' => true,
            'msg' => 'Dostęp przyznany.'
        ]);
        exit;
    }

    if ($action === 'logout') {
        unset($_SESSION['tv_auth']);

        echo json_encode([
            'ok' => true
        ]);
        exit;
    }

    http_response_code(400);

    echo json_encode([
        'ok' => false,
        'msg' => 'Nieprawidłowe żądanie.'
    ]);

    exit;
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>FOXIO.PL — Streaming</title>

<style>

*{
    box-sizing:border-box;
}

html,
body{
    margin:0;
    padding:0;
    min-height:100%;
}

body{
    background:
        radial-gradient(circle at 50% -10%, rgba(0,210,255,.13), transparent 38%),
        linear-gradient(180deg,#050b12 0%,#020509 100%);
    color:#fff;
    font-family:Arial,Helvetica,sans-serif;
    display:flex;
    justify-content:center;
    align-items:center;
    min-height:100vh;
    padding:25px;
}

.container{
    width:100%;
    max-width:760px;
}

.panel{
    background:rgba(7,14,22,.92);
    border:1px solid rgba(0,210,255,.18);
    border-radius:24px;
    padding:32px;
    box-shadow:
        0 0 60px rgba(0,180,255,.08),
        0 20px 60px rgba(0,0,0,.55);
}

.logo{
    text-align:center;
    margin-bottom:25px;
}

.logo img{
    max-width:180px;
    height:auto;
    display:inline-block;
}

.logo a{
    display:inline-block;
}

.title{
    text-align:center;
    font-size:26px;
    font-weight:800;
    letter-spacing:2px;
    margin-bottom:8px;
}

.subtitle{
    text-align:center;
    color:#7f9aaa;
    font-size:13px;
    margin-bottom:28px;
}

.login-box{
    max-width:420px;
    margin:0 auto;
}

input[type=password]{
    width:100%;
    padding:15px 17px;
    border-radius:13px;
    border:1px solid rgba(0,210,255,.2);
    background:#071019;
    color:#fff;
    outline:none;
    font-size:15px;
    margin-bottom:12px;
}

input[type=password]:focus{
    border-color:rgba(0,210,255,.65);
    box-shadow:0 0 20px rgba(0,210,255,.08);
}

.button{
    width:100%;
    padding:14px 16px;
    border:0;
    border-radius:13px;
    background:linear-gradient(135deg,#00bfff,#007cff);
    color:#fff;
    font-size:14px;
    font-weight:800;
    cursor:pointer;
    transition:.2s ease;
}

.button:hover{
    transform:translateY(-1px);
    box-shadow:0 0 22px rgba(0,180,255,.25);
}

.button:disabled{
    opacity:.6;
    cursor:not-allowed;
}

.admin-button{
    display:block;
    width:100%;
    margin-top:12px;
    padding:13px 15px;
    border-radius:13px;
    border:1px solid rgba(0,220,255,.28);
    background:rgba(0,180,255,.07);
    color:#67ddff;
    text-decoration:none;
    text-align:center;
    font-size:13px;
    font-weight:800;
    transition:.2s ease;
}

.admin-button:hover{
    background:rgba(0,180,255,.14);
    border-color:rgba(0,220,255,.55);
    box-shadow:0 0 18px rgba(0,210,255,.12);
    color:#fff;
}

.message{
    min-height:22px;
    text-align:center;
    margin-top:15px;
    font-size:13px;
}

.message.ok{
    color:#52f5a4;
}

.message.err{
    color:#ff657c;
}

.player{
    width:100%;
    aspect-ratio:16/9;
    background:
        radial-gradient(circle at center,rgba(0,180,255,.08),transparent 45%),
        #02070b;
    border:1px solid rgba(0,210,255,.16);
    border-radius:18px;
    display:flex;
    align-items:center;
    justify-content:center;
    text-align:center;
    overflow:hidden;
    margin-bottom:20px;
}

.player-content{
    padding:30px;
}

.player-icon{
    font-size:42px;
    margin-bottom:15px;
}

.player-title{
    font-size:19px;
    font-weight:800;
    margin-bottom:8px;
}

.player-desc{
    color:#728999;
    font-size:13px;
}

.info{
    display:grid;
    grid-template-columns:repeat(3,1fr);
    gap:12px;
    margin-top:18px;
}

.info-box{
    background:rgba(0,180,255,.045);
    border:1px solid rgba(0,210,255,.11);
    border-radius:13px;
    padding:15px;
    text-align:center;
}

.info-title{
    color:#5fdcff;
    font-size:11px;
    font-weight:800;
    margin-bottom:6px;
    text-transform:uppercase;
}

.info-text{
    color:#8397a4;
    font-size:12px;
}

.logout{
    display:block;
    width:100%;
    margin-top:12px;
    padding:11px;
    border:1px solid rgba(255,255,255,.08);
    border-radius:12px;
    background:rgba(255,255,255,.025);
    color:#7d909c;
    cursor:pointer;
    font-size:12px;
}

.logout:hover{
    color:#fff;
    background:rgba(255,255,255,.05);
}

.footer{
    text-align:center;
    margin-top:25px;
    color:#52636f;
    font-size:11px;
}

.footer a{
    color:#67ddff;
    text-decoration:none;
}

@media(max-width:600px){

    body{
        padding:12px;
    }

    .panel{
        padding:22px 16px;
        border-radius:19px;
    }

    .title{
        font-size:22px;
    }

    .info{
        grid-template-columns:1fr;
    }

}

</style>

</head>

<body>

<div class="container">

<div class="panel">

<div class="logo">

<a href="https://www.foxio.pl/">

<img src="/foxio-logo.png" alt="FOXIO.PL">

</a>

</div>

<div class="title">
FOXIO TV
</div>

<div class="subtitle">
PRYWATNA TRANSMISJA
</div>


<?php if (!$logged): ?>

<div class="login-box">

<input
    type="password"
    id="password"
    placeholder="Hasło dostępu"
    autocomplete="current-password"
>

<button
    type="button"
    class="button"
    id="loginButton"
>
🔓 ODBLOKUJ TRANSMISJĘ
</button>

<div id="message" class="message"></div>

<a
    class="admin-button"
    href="http://foxio.pl:5585/"
>
⚙ ADMINISTRACJA
</a>

</div>


<?php else: ?>


<div class="player">

<div class="player-content">

<div class="player-icon">
📺
</div>

<div class="player-title">
Oczekiwanie na źródło transmisji
</div>

<div class="player-desc">
Transmisja zostanie uruchomiona po podłączeniu źródła.
</div>

</div>

</div>


<a
    class="admin-button"
    href="http://foxio.pl:5585/"
>
⚙ ADMINISTRACJA
</a>


<div class="info">

<div class="info-box">

<div class="info-title">
Status
</div>

<div class="info-text">
Połączono
</div>

</div>


<div class="info-box">

<div class="info-title">
Źródło
</div>

<div class="info-text">
FOXIO TV
</div>

</div>


<div class="info-box">

<div class="info-title">
Transmisja
</div>

<div class="info-text">
Oczekiwanie
</div>

</div>

</div>


<button
    type="button"
    class="logout"
    id="logoutButton"
>
Wyloguj
</button>


<?php endif; ?>


<div class="footer">

Część Ekosystemu |
<a href="https://www.foxx.pl/">
FOXX.PL
</a>

</div>

</div>

</div>


<script>

const csrf = <?= json_encode($csrf) ?>;

<?php if (!$logged): ?>

const password = document.getElementById('password');
const button = document.getElementById('loginButton');
const message = document.getElementById('message');

async function login(){

    const value = password.value.trim();

    if(!value){

        message.textContent='Wpisz hasło.';
        message.className='message err';

        password.focus();

        return;
    }

    button.disabled=true;
    button.textContent='⏳ SPRAWDZANIE...';

    message.textContent='';
    message.className='message';

    try{

        const form = new FormData();

        form.append('action','login');
        form.append('password',value);
        form.append('csrf',csrf);

        const response = await fetch(location.href,{
            method:'POST',
            body:form
        });

        const data = await response.json();

        if(!response.ok || !data.ok){

            throw new Error(
                data.msg || 'Nieprawidłowe hasło.'
            );

        }

        message.textContent=
            '✓ Dostęp przyznany. Uruchamiam transmisję...';

        message.className='message ok';

        setTimeout(()=>{

            location.reload();

        },500);

    }catch(error){

        message.textContent=error.message;
        message.className='message err';

        password.value='';
        password.focus();

    }finally{

        button.disabled=false;
        button.textContent='🔓 ODBLOKUJ TRANSMISJĘ';

    }

}

button.addEventListener('click',login);

password.addEventListener('keydown',(event)=>{

    if(event.key==='Enter'){

        login();

    }

});

<?php else: ?>

const logoutButton =
    document.getElementById('logoutButton');

logoutButton.addEventListener('click',async()=>{

    const form = new FormData();

    form.append('action','logout');
    form.append('csrf',csrf);

    try{

        await fetch(location.href,{
            method:'POST',
            body:form
        });

    }finally{

        location.reload();

    }

});

<?php endif; ?>

</script>

</body>
</html>
