Restructure index.php

This commit is contained in:
2019-03-14 16:17:59 +01:00
parent 2ea06db745
commit 950193edee

View File

@@ -1,13 +1,24 @@
<?php <?php
use FooBar\Configuration;
require_once '../vendor/autoload.php'; require_once '../vendor/autoload.php';
$config = \FooBar\Configuration::loadConfig(); $config = Configuration::loadConfig();
if (isset($_POST['auth_check'])) { function auth_check(Configuration $config)
{
if (!$config->isAuthorized()) { if (!$config->isAuthorized()) {
http_response_code(403); http_response_code(403);
} }
} elseif (isset($_POST['toggle']) && $config->isAuthorized()) {
echo 'Some text otherwise PHP crashes. I wish this was a joke.';
}
function toggle(Configuration $config)
{
if ($config->isAuthorized()) {
$file = $config->stateFile(); $file = $config->stateFile();
if ($config->isOpen()) { if ($config->isOpen()) {
unlink($file); unlink($file);
@@ -16,8 +27,23 @@ if (isset($_POST['auth_check'])) {
} }
} }
if ($config->isOpen()) { header('location: ' . $_SERVER['REQUEST_URI']);
}
function display_state(bool $open)
{
header('X-Foobar-Open: ' . var_export($open, true));
if ($open) {
require '../templates/open.html'; require '../templates/open.html';
} else { } else {
require '../templates/closed.html'; require '../templates/closed.html';
} }
}
if (isset($_POST['auth_check'])) {
auth_check($config);
} elseif (isset($_POST['toggle'])) {
toggle($config);
} else {
display_state($config->isOpen());
}