login.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Admin\Auth;
  5. use App\Security\Csrf;
  6. require dirname(__DIR__) . '/src/autoload.php';
  7. Bootstrap::init();
  8. $auth = new Auth();
  9. if (isset($_GET['logout']) && $_GET['logout'] === '1') {
  10. $auth->logout();
  11. header('Location: /admin/login.php');
  12. exit;
  13. }
  14. if ($auth->isLoggedIn()) {
  15. header('Location: /admin/index.php');
  16. exit;
  17. }
  18. $error = '';
  19. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  20. if (!Csrf::validate((string) ($_POST['csrf'] ?? ''))) {
  21. $error = 'Ungültiges CSRF-Token.';
  22. } else {
  23. $password = (string) ($_POST['password'] ?? '');
  24. if ($auth->login($password)) {
  25. header('Location: /admin/index.php');
  26. exit;
  27. }
  28. $error = 'Login fehlgeschlagen.';
  29. }
  30. }
  31. $csrf = Csrf::token();
  32. ?><!doctype html>
  33. <html lang="de">
  34. <head>
  35. <meta charset="utf-8">
  36. <meta name="viewport" content="width=device-width, initial-scale=1">
  37. <title>Admin Login</title>
  38. <link rel="stylesheet" href="/assets/css/tokens.css">
  39. <link rel="stylesheet" href="/assets/css/base.css">
  40. </head>
  41. <body>
  42. <main class="container">
  43. <section class="card">
  44. <h1>Admin Login</h1>
  45. <?php if ($error !== ''): ?>
  46. <p class="error"><?= htmlspecialchars($error) ?></p>
  47. <?php endif; ?>
  48. <form method="post">
  49. <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf) ?>">
  50. <div class="field">
  51. <label for="password">Passwort</label>
  52. <input id="password" name="password" type="password" required>
  53. </div>
  54. <button type="submit">Anmelden</button>
  55. </form>
  56. </section>
  57. </main>
  58. </body>
  59. </html>