login.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. require_once __DIR__ . "/../config.php";
  3. require_once __DIR__ . "/../includes/functions.php";
  4. // Handle logout
  5. if (isset($_GET["logout"])) {
  6. $_SESSION["admin_logged_in"] = false;
  7. unset($_SESSION["admin_username"]);
  8. session_destroy();
  9. header("Location: login.php");
  10. exit();
  11. }
  12. $error = "";
  13. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  14. // Validate CSRF token
  15. if (!validateCsrfToken($_POST["csrf_token"] ?? "")) {
  16. $error = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  17. } else {
  18. $username = normalizeAdminUsername($_POST["username"] ?? "");
  19. $password = $_POST["password"] ?? "";
  20. $users = getAdminUsers();
  21. if (
  22. isset($users[$username]) &&
  23. password_verify($password, $users[$username])
  24. ) {
  25. $_SESSION["admin_logged_in"] = true;
  26. $_SESSION["admin_username"] = $username;
  27. logAccess("Admin login successful", ["username" => $username]);
  28. header("Location: index.php");
  29. exit();
  30. } else {
  31. logAccess("Admin login failed", ["username" => $username]);
  32. $error = "Benutzername oder Passwort falsch.";
  33. }
  34. }
  35. }
  36. // Redirect if already logged in
  37. if (isset($_SESSION["admin_logged_in"]) && $_SESSION["admin_logged_in"]) {
  38. header("Location: index.php");
  39. exit();
  40. }
  41. ?>
  42. <!DOCTYPE html>
  43. <html lang="de">
  44. <head>
  45. <meta charset="UTF-8">
  46. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  47. <title>Administration - <?php echo escape(SITE_FULL_NAME); ?></title>
  48. <link rel="stylesheet" href="<?php echo escape(
  49. SITE_URL,
  50. ); ?>/assets/css/style.css">
  51. </head>
  52. <body class="admin-page">
  53. <header class="site-header">
  54. <div class="container header-inner">
  55. <a class="brand" href="<?php echo escape(SITE_URL); ?>/index.php">
  56. <img class="brand-logo" src="<?php echo escape(
  57. SITE_URL,
  58. ); ?>/assets/branding/stadt-freising-logo.png" alt="Wappen der Stadt Freising">
  59. <div class="brand-text">
  60. <span class="brand-title"><?php echo escape(
  61. SITE_NAME,
  62. ); ?></span>
  63. <span class="brand-subtitle"><?php echo escape(
  64. SITE_SERVICE_HEADER,
  65. ); ?></span>
  66. </div>
  67. </a>
  68. <a href="<?php echo escape(
  69. SITE_URL,
  70. ); ?>/index.php" class="btn btn-secondary">Zurück zu <?php echo escape(
  71. SITE_SERVICE_NAME,
  72. ); ?></a>
  73. </div>
  74. </header>
  75. <main>
  76. <div class="container container-narrow page-top-gap">
  77. <h2>Administration</h2>
  78. <?php if ($error): ?>
  79. <div class="alert alert-error">
  80. <?php echo htmlspecialchars($error); ?>
  81. </div>
  82. <?php endif; ?>
  83. <form method="POST" class="panel panel-lg">
  84. <?php echo csrfField(); ?>
  85. <div class="form-group">
  86. <label for="username">Benutzername:</label>
  87. <input type="text" id="username" name="username" required autofocus>
  88. </div>
  89. <div class="form-group">
  90. <label for="password">Passwort:</label>
  91. <input type="password" id="password" name="password" required>
  92. </div>
  93. <button type="submit" class="btn btn-block">Anmelden</button>
  94. </form>
  95. <div class="text-center mt-2">
  96. <a href="<?php echo escape(
  97. SITE_URL,
  98. ); ?>/index.php">Zurück zu <?php echo escape(
  99. SITE_SERVICE_NAME,
  100. ); ?></a>
  101. </div>
  102. </div>
  103. </main>
  104. </body>
  105. </html>