login.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $app = Bootstrap::config('app');
  9. $auth = new Auth();
  10. if (isset($_GET['logout']) && $_GET['logout'] === '1') {
  11. $auth->logout();
  12. header('Location: ' . Bootstrap::url('admin/login.php'));
  13. exit;
  14. }
  15. if ($auth->isLoggedIn()) {
  16. header('Location: ' . Bootstrap::url('admin/index.php'));
  17. exit;
  18. }
  19. $error = '';
  20. $username = '';
  21. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  22. if (!Csrf::validate((string) ($_POST['csrf'] ?? ''))) {
  23. $error = 'Ungültiges CSRF-Token.';
  24. } else {
  25. $username = trim((string) ($_POST['username'] ?? ''));
  26. $password = (string) ($_POST['password'] ?? '');
  27. if ($auth->login($username, $password)) {
  28. header('Location: ' . Bootstrap::url('admin/index.php'));
  29. exit;
  30. }
  31. $error = 'Login fehlgeschlagen.';
  32. }
  33. }
  34. $csrf = Csrf::token();
  35. ?><!doctype html>
  36. <html lang="de">
  37. <head>
  38. <meta charset="utf-8">
  39. <meta name="viewport" content="width=device-width, initial-scale=1">
  40. <title>Admin Login</title>
  41. <link rel="stylesheet" href="<?= htmlspecialchars(Bootstrap::url('assets/css/tokens.css')) ?>">
  42. <link rel="stylesheet" href="<?= htmlspecialchars(Bootstrap::url('assets/css/base.css')) ?>">
  43. </head>
  44. <body class="admin-page">
  45. <header class="site-header">
  46. <div class="container header-inner">
  47. <a class="brand" href="<?= htmlspecialchars(Bootstrap::url('admin/login.php')) ?>">
  48. <img class="brand-logo" src="<?= htmlspecialchars(Bootstrap::url('assets/images/feuerwehr-logo-invers.webp')) ?>" alt="Feuerwehr Logo">
  49. <div class="brand-title"><?= htmlspecialchars((string) ($app['project_name'] ?? 'Admin')) ?></div>
  50. </a>
  51. </div>
  52. </header>
  53. <main class="container">
  54. <section class="card auth-container">
  55. <h1>Admin Login</h1>
  56. <?php if ($error !== ''): ?>
  57. <p class="alert alert-error"><?= htmlspecialchars($error) ?></p>
  58. <?php endif; ?>
  59. <form method="post">
  60. <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf) ?>">
  61. <div class="field">
  62. <label for="username">Benutzername</label>
  63. <input
  64. id="username"
  65. name="username"
  66. type="text"
  67. required
  68. autocomplete="username"
  69. value="<?= htmlspecialchars($username) ?>"
  70. >
  71. </div>
  72. <div class="field">
  73. <label for="password">Passwort</label>
  74. <input id="password" name="password" type="password" required autocomplete="current-password">
  75. </div>
  76. <button type="submit" class="btn">Anmelden</button>
  77. </form>
  78. </section>
  79. </main>
  80. </body>
  81. </html>