login.php 4.3 KB

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