<?php
declare(strict_types=1);
require __DIR__ . '/auth.php';
$user = requireLogin();
$assets = require __DIR__ . '/tool-data.php';
$html = base64_decode($assets['index.html'], true);
$account = '<div class="account-controls"><span>' . escape($user) . '</span><form method="post" action="' . escape(appUrl('logout.php')) . '"><input type="hidden" name="csrf" value="' . escape(csrfToken()) . '"><button class="secondary" type="submit">Keluar</button></form></div>';
$script = '<script>globalThis.TUNNEL_ACCOUNT=' . json_encode(strtolower($user), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) . ';window.addEventListener("tunnel-ready",function(){document.querySelector("header").insertAdjacentHTML("beforeend",' . json_encode($account, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) . ');});</script>';
header('Content-Type: text/html; charset=UTF-8');
echo str_replace('</head>', $script . '</head>', $html);
