From 950193edee8b30840392402c647aaddb0e2f7314 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 14 Mar 2019 16:17:59 +0100 Subject: [PATCH] Restructure index.php --- public/index.php | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/public/index.php b/public/index.php index 08219cf..df9d99c 100644 --- a/public/index.php +++ b/public/index.php @@ -1,23 +1,49 @@ isAuthorized()) { http_response_code(403); } -} elseif (isset($_POST['toggle']) && $config->isAuthorized()) { - $file = $config->stateFile(); - if ($config->isOpen()) { - unlink($file); + + + echo 'Some text otherwise PHP crashes. I wish this was a joke.'; +} + +function toggle(Configuration $config) +{ + if ($config->isAuthorized()) { + $file = $config->stateFile(); + if ($config->isOpen()) { + unlink($file); + } else { + touch($file); + } + } + + header('location: ' . $_SERVER['REQUEST_URI']); +} + +function display_state(bool $open) +{ + header('X-Foobar-Open: ' . var_export($open, true)); + if ($open) { + require '../templates/open.html'; } else { - touch($file); + require '../templates/closed.html'; } } -if ($config->isOpen()) { - require '../templates/open.html'; +if (isset($_POST['auth_check'])) { + auth_check($config); +} elseif (isset($_POST['toggle'])) { + toggle($config); } else { - require '../templates/closed.html'; + display_state($config->isOpen()); }