From 7d7ebf88c6d5285401ab0f9545f14dedb2f2940e Mon Sep 17 00:00:00 2001 From: flop Date: Sat, 23 May 2026 11:27:58 +0200 Subject: [PATCH] feat(php): update live --- ts/live.php | 129 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/ts/live.php b/ts/live.php index d6ea848..b8ca88c 100644 --- a/ts/live.php +++ b/ts/live.php @@ -5,25 +5,23 @@ define('PASSWORD', 'cttue_avj2305_44'); define('FILES_DIR', __DIR__ . '/avj2305'); define('ACTIVE_FILE', __DIR__ . '/active.txt'); -// -- helpers ------------------------------------------------------------------ +// ============================================================================= +// HELPERS +// ============================================================================= -function is_authed(): bool { - return !empty($_SESSION['auth']); +function redirect(string $url): never { + header('Location: ' . $url); + exit; } function require_auth(): void { - if (!is_authed()) { - header('Location: ?login'); - exit; - } + if (empty($_SESSION['auth'])) redirect('?login'); } function get_active(): string { - if (file_exists(ACTIVE_FILE)) { - $v = trim(file_get_contents(ACTIVE_FILE)); - if ($v !== '') return $v; - } - return ''; + if (!file_exists(ACTIVE_FILE)) return ''; + $v = trim(file_get_contents(ACTIVE_FILE)); + return $v !== '' ? $v : ''; } function set_active(string $filename): void { @@ -32,45 +30,42 @@ function set_active(string $filename): void { function get_bin_files(): array { $files = glob(FILES_DIR . '/*.bin'); - if (!$files) return []; - return array_map('basename', $files); + return $files ? array_map('basename', $files) : []; } function safe_filename(string $name): bool { - // basename only, no path traversal, must end in .bin return $name === basename($name) && str_ends_with($name, '.bin') && !str_contains($name, "\0"); } -// -- routing ------------------------------------------------------------------ +// ============================================================================= +// ACTIONS (POST handlers - redirect, never render) +// ============================================================================= -$q = array_key_first($_GET) ?? ''; // first query param key - -// POST: login -if ($q === 'login' && $_SERVER['REQUEST_METHOD'] === 'POST') { +function action_login(): never { if (hash_equals(PASSWORD, $_POST['password'] ?? '')) { $_SESSION['auth'] = true; - header('Location: ?edit'); - } else { - header('Location: ?login&err=1'); + redirect('?edit'); } - exit; + redirect('?login&err=1'); } -// POST: set active file -if ($q === 'edit' && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['file'])) { +function action_set_file(): never { require_auth(); - $f = $_POST['file']; + $f = $_POST['file'] ?? ''; if (safe_filename($f) && file_exists(FILES_DIR . '/' . $f)) { set_active($f); } - header('Location: ?edit'); - exit; + redirect('?edit'); } -// GET: login page -if ($q === 'login') { ?> +// ============================================================================= +// RENDERERS (GET handlers - output HTML, never redirect) +// ============================================================================= + +function render_login(): never { + $err = !empty($_GET['err']); ?> Login @@ -88,17 +83,15 @@ if ($q === 'login') { ?> - Wrong password. + Wrong password. + $active = get_active(); ?> Select File @@ -122,7 +115,7 @@ if ($q === 'edit') {
  • - +
  • @@ -133,27 +126,53 @@ if ($q === 'edit') { serve active .bin file +// GET /live.php?login -> login page +// POST /live.php?login -> process login +// GET /live.php?edit -> file picker [auth required] +// POST /live.php?edit -> set active file [auth required] +// +// ============================================================================= + +$method = $_SERVER['REQUEST_METHOD']; +$route = array_key_first($_GET) ?? ''; + +match (true) { + $method === 'POST' && $route === 'login' => action_login(), + $method === 'POST' && $route === 'edit' => action_set_file(), + $method === 'GET' && $route === 'login' => render_login(), + $method === 'GET' && $route === 'edit' => render_edit(), + default => serve_active_file(), +};