diff --git a/backend/helper/RendererHelper.php b/backend/helper/RendererHelper.php index 5a06c0e60f12ba288505d36ed7c43e6eeb6e0c57..3529434a437266765148c7cd49a321de2f838005 100644 --- a/backend/helper/RendererHelper.php +++ b/backend/helper/RendererHelper.php @@ -8,10 +8,12 @@ require_once 'TranslationHelper.php'; class RendererHelper { private $config; private $translator; + private $sessionHelper; - public function __construct(Config $config) { + public function __construct(Config $config, SessionHelper $sessionHelper = null) { $this->config = $config; $this->translator = new TranslationHelper($config); + $this->sessionHelper = $sessionHelper; } public function renderError($e) { @@ -33,13 +35,15 @@ class RendererHelper { echo json_encode($json) . "\n"; } - public function renderPage(string $template, array $vars = null) { + public function renderPage(string $template, array $vars = []) { $translation = $this->translator->loadTranslation($this->translator->findMatchingLanguage($_SERVER['HTTP_ACCEPT_LANGUAGE'])); - $viewHelper = new ViewHelper($translation, $vars); + $viewHelper = new ViewHelper($translation, array_merge($this->sessionHelper->vars, $vars)); $viewHelper->render($template); } - public function redirect(string $page, string $flash = null) { + public function redirect(string $page, array $vars = []) { header('Location: ' . $this->config->path_prefix . $page); + $this->sessionHelper->startSession(); + $this->sessionHelper->vars = $vars; } } \ No newline at end of file diff --git a/backend/helper/ViewHelper.php b/backend/helper/ViewHelper.php index ff7e78adcf431c560083d36ade96e64a204dc619..1d22ba96d6ee6eb94f0cff1b5970a5a06c1ce826 100644 --- a/backend/helper/ViewHelper.php +++ b/backend/helper/ViewHelper.php @@ -28,6 +28,7 @@ class ViewHelper { $var = $var[$key]; echo $var; }; + $vars = $this->vars; $path = $this->template_dir . '/' . $template_file . '.phtml'; if (file_exists($path)) { diff --git a/index.php b/index.php index 63fa89198591dc7379c7547194e88dd89e54b380..8e667477528f21061cbdaea98ae4d758312804d6 100644 --- a/index.php +++ b/index.php @@ -9,12 +9,12 @@ require_once 'backend/helper/SessionHelper.php'; $session = SessionHelper::getInstance(); $config = Config::createFromGlobals(); -$renderer = new RendererHelper($config); +$renderer = new RendererHelper($config, $session); $backend = Backend::createFromConfig($config); if (!$backend->authenticate($session->username ?: '', $session->password ?: '')) { $session->destroy(); - $renderer->redirect('/login.php'); + $renderer->redirect('/login.php', ['message' => 'login.message.error_unauthed', 'type' => 'error']); } else { $renderer->renderPage('search', ['username' => $session->username]); } \ No newline at end of file diff --git a/login.php b/login.php index 1aa44fd59b5557e8c208fcb14b99979dc971d29e..d1bd68f50674fd6e9a162a31df279820160e37d9 100644 --- a/login.php +++ b/login.php @@ -9,7 +9,7 @@ require_once 'backend/helper/SessionHelper.php'; $session = SessionHelper::getInstance(); $config = Config::createFromGlobals(); -$renderer = new RendererHelper($config); +$renderer = new RendererHelper($config, $session); $backend = Backend::createFromConfig($config); if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['action'] === 'login') { @@ -21,10 +21,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['ac $renderer->redirect('/'); } else { syslog(LOG_ERR, "Could not authenticate user " . $username); + $renderer->redirect('/login.php', ['message' => 'login.message.error_invalid', 'type' => 'error']); } } elseif (isset($_GET['action']) && $_GET['action'] === 'logout') { $session->destroy(); - $renderer->redirect('/login.php'); + $renderer->redirect('/login.php', ['message' => 'login.message.success_logout', 'type' => 'info']); } else if ($backend->authenticate($session->username ?: '', $session->password ?: '')) { $renderer->redirect('/'); } else { diff --git a/res/login.css b/res/login.css index 29f00a5b99e85cf796e2773bf05b3d363f3cb313..7e0bde0fdbb805dba06889edc427f14bfb6adfb2 100644 --- a/res/login.css +++ b/res/login.css @@ -1,8 +1,13 @@ -body :first-child { +body { + font-family: 'Open Sans', Helvetica Neue, Helvetica, Arial, arial, sans-serif; + -webkit-font-smoothing: antialiased; +} + +body > :first-child { margin-top: 8rem; } -body:last-child { +body > :last-child { margin-bottom: 2rem; } @@ -11,13 +16,11 @@ form { margin: 0 auto 25px; border-radius: 2px; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3); - width: 274px; - padding: 40px 40px; + width: 16rem; + padding: 1.5rem 2rem; } h1 { - font-family: 'Open Sans', arial; - -webkit-font-smoothing: antialiased; color: #555; font-size: 42px; font-weight: 300; @@ -26,7 +29,6 @@ h1 { } h2 { - font-family: 'Open Sans', arial; color: #555; font-size: 18px; font-weight: 400; @@ -76,18 +78,26 @@ input[type=submit] { background-image: linear-gradient(top, #4d90fe, #4787ed); width: 100%; display: block; - margin-bottom: 10px; z-index: 1; position: relative; box-sizing: border-box; } +form .message { + margin: 0 0 1rem 0; + font-size: 14px; +} + +form .message.error { + color: #b71c1c; +} + @media (max-width: 800px) { - body :first-child { + body > :first-child { margin-top: 1rem; } - body:last-child { + body > :last-child { margin-bottom: 1rem; } } \ No newline at end of file diff --git a/templates/login.phtml b/templates/login.phtml index ab93067bedfc96a15bc313532a25a89187d41ab5..cfca65c020fecb9a80a33a8cf70ba6062b2df8bf 100644 --- a/templates/login.phtml +++ b/templates/login.phtml @@ -21,6 +21,9 @@ <h2><?php $t('login.description'); ?></h2> <form method="post" action="login.php?action=login"> + <?php if ($vars['message']): ?> + <p class="message <?php echo $vars['type']; ?>"><?php echo $t($vars['message']); ?></p> + <?php endif; ?> <input name="username" type="text" placeholder="<?php $t('login.username'); ?>"> <input name="password" type="password" placeholder="<?php $t('login.password'); ?>"> <input type="submit" value="<?php $t('login.submit'); ?>"> diff --git a/translations/de.json b/translations/de.json index 2ba1b895c86ec815e7b92618b632a216780755e0..190ad83611eb9fe42715cc39b66620150af5f176 100644 --- a/translations/de.json +++ b/translations/de.json @@ -14,7 +14,12 @@ "description": "You have to login to access this page", "username": "Benutzername", "password": "Passwort", - "submit": "Anmelden" + "submit": "Anmelden", + "message": { + "success_logout": "You have successfully logged out.", + "error_invalid": "Invalid username/password combination.", + "error_unauthed": "You need to be logged in to access this page." + } }, "search": "Suchen", "logout": "Abmelden", diff --git a/translations/en.json b/translations/en.json index be43c3c60303f620e45f500a629f2af2d62ecab4..1d63a0582820befd95cd3a77a5bb99bec6928a72 100644 --- a/translations/en.json +++ b/translations/en.json @@ -14,7 +14,12 @@ "description": "You have to login to access this page", "username": "Username", "password": "Password", - "submit": "Login" + "submit": "Login", + "message": { + "success_logout": "You have successfully logged out.", + "error_invalid": "Invalid username/password combination.", + "error_unauthed": "You need to be logged in to access this page." + } }, "search": "Search", "logout": "Logout",