admins.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. require_once __DIR__ . "/../config.php";
  3. require_once __DIR__ . "/../includes/functions.php";
  4. // Check admin login
  5. if (!isset($_SESSION["admin_logged_in"]) || !$_SESSION["admin_logged_in"]) {
  6. header("Location: login.php");
  7. exit();
  8. }
  9. $pageTitle = "Admins verwalten";
  10. $message = "";
  11. $messageType = "";
  12. function isValidAdminPasswordInput($password)
  13. {
  14. return is_string($password) && strlen($password) >= 8;
  15. }
  16. $adminAccounts = getAdminAccounts();
  17. function isValidAdminEmailInput($email)
  18. {
  19. return isValidAdminEmail($email);
  20. }
  21. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  22. // Validate CSRF token
  23. if (!validateCsrfToken($_POST["csrf_token"] ?? "")) {
  24. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  25. $messageType = "error";
  26. } else {
  27. if (isset($_POST["add_admin"])) {
  28. $username = normalizeAdminUsername($_POST["username"] ?? "");
  29. $description = normalizeAdminDescription(
  30. $_POST["description"] ?? "",
  31. );
  32. $email = normalizeAdminEmail($_POST["email"] ?? "");
  33. $password = $_POST["password"] ?? "";
  34. $passwordConfirm = $_POST["password_confirm"] ?? "";
  35. if (!isValidAdminUsername($username)) {
  36. $message =
  37. "Ungültiger Benutzername. Erlaubt: 3-50 Zeichen (Buchstaben, Zahlen, Punkt, Unterstrich, Bindestrich).";
  38. $messageType = "error";
  39. } elseif (isset($adminAccounts[$username])) {
  40. $message = "Dieser Benutzername existiert bereits.";
  41. $messageType = "error";
  42. } elseif (!isValidAdminDescription($description)) {
  43. $message = "Beschreibung ist erforderlich (max. 120 Zeichen).";
  44. $messageType = "error";
  45. } elseif (!isValidAdminEmailInput($email)) {
  46. $message = "Gültige E-Mail ist erforderlich.";
  47. $messageType = "error";
  48. } elseif (!isValidAdminPasswordInput($password)) {
  49. $message = "Passwort muss mindestens 8 Zeichen lang sein.";
  50. $messageType = "error";
  51. } elseif ($password !== $passwordConfirm) {
  52. $message = "Passwort und Bestätigung stimmen nicht überein.";
  53. $messageType = "error";
  54. } else {
  55. $adminAccounts[$username] = [
  56. "password_hash" => password_hash(
  57. $password,
  58. PASSWORD_BCRYPT,
  59. ),
  60. "description" => $description,
  61. "email" => $email,
  62. ];
  63. saveAdminAccounts($adminAccounts);
  64. logAccess("Admin added admin account", [
  65. "username" => $username,
  66. "description" => $description,
  67. ]);
  68. $message = "Admin wurde erfolgreich angelegt.";
  69. $messageType = "success";
  70. }
  71. }
  72. if (isset($_POST["update_description"])) {
  73. $targetUsername = normalizeAdminUsername(
  74. $_POST["target_username"] ?? "",
  75. );
  76. $description = normalizeAdminDescription(
  77. $_POST["description"] ?? "",
  78. );
  79. $email = normalizeAdminEmail($_POST["email"] ?? "");
  80. if (!isset($adminAccounts[$targetUsername])) {
  81. $message = "Admin nicht gefunden.";
  82. $messageType = "error";
  83. } elseif (!isValidAdminDescription($description)) {
  84. $message = "Beschreibung ist erforderlich (max. 120 Zeichen).";
  85. $messageType = "error";
  86. } elseif (!isValidAdminEmailInput($email)) {
  87. $message = "Gültige E-Mail ist erforderlich.";
  88. $messageType = "error";
  89. } else {
  90. $adminAccounts[$targetUsername]["description"] = $description;
  91. $adminAccounts[$targetUsername]["email"] = $email;
  92. saveAdminAccounts($adminAccounts);
  93. logAccess("Admin updated admin description", [
  94. "username" => $targetUsername,
  95. ]);
  96. $message = "Beschreibung und E-Mail wurden aktualisiert.";
  97. $messageType = "success";
  98. }
  99. }
  100. if (isset($_POST["change_password"])) {
  101. $targetUsername = normalizeAdminUsername(
  102. $_POST["target_username"] ?? "",
  103. );
  104. $newPassword = $_POST["new_password"] ?? "";
  105. $newPasswordConfirm = $_POST["new_password_confirm"] ?? "";
  106. if (!isset($adminAccounts[$targetUsername])) {
  107. $message = "Admin nicht gefunden.";
  108. $messageType = "error";
  109. } elseif (!isValidAdminPasswordInput($newPassword)) {
  110. $message = "Passwort muss mindestens 8 Zeichen lang sein.";
  111. $messageType = "error";
  112. } elseif ($newPassword !== $newPasswordConfirm) {
  113. $message = "Passwort und Bestätigung stimmen nicht überein.";
  114. $messageType = "error";
  115. } else {
  116. $adminAccounts[$targetUsername][
  117. "password_hash"
  118. ] = password_hash($newPassword, PASSWORD_BCRYPT);
  119. saveAdminAccounts($adminAccounts);
  120. logAccess("Admin changed admin password", [
  121. "username" => $targetUsername,
  122. ]);
  123. $message = "Passwort wurde aktualisiert.";
  124. $messageType = "success";
  125. }
  126. }
  127. if (isset($_POST["delete_admin"])) {
  128. $targetUsername = normalizeAdminUsername(
  129. $_POST["target_username"] ?? "",
  130. );
  131. if (!isset($adminAccounts[$targetUsername])) {
  132. $message = "Admin nicht gefunden.";
  133. $messageType = "error";
  134. } else {
  135. unset($adminAccounts[$targetUsername]);
  136. saveAdminAccounts($adminAccounts);
  137. logAccess("Admin deleted admin account", [
  138. "username" => $targetUsername,
  139. ]);
  140. if (
  141. isset($_SESSION["admin_username"]) &&
  142. $_SESSION["admin_username"] === $targetUsername
  143. ) {
  144. $_SESSION["admin_logged_in"] = false;
  145. unset($_SESSION["admin_username"]);
  146. session_destroy();
  147. header("Location: login.php");
  148. exit();
  149. }
  150. $message = "Admin wurde gelöscht.";
  151. $messageType = "success";
  152. }
  153. }
  154. $adminAccounts = getAdminAccounts();
  155. }
  156. }
  157. $currentAdmin = isset($_SESSION["admin_username"])
  158. ? normalizeAdminUsername($_SESSION["admin_username"])
  159. : "";
  160. $changeUsername = normalizeAdminUsername($_GET["change"] ?? "");
  161. $selectedChangeUser = null;
  162. $editDescriptionUsername = normalizeAdminUsername(
  163. $_GET["edit_description"] ?? "",
  164. );
  165. $selectedDescriptionUser = null;
  166. if ($changeUsername !== "") {
  167. if (!isset($adminAccounts[$changeUsername])) {
  168. if ($message === "") {
  169. $message = "Ausgewählter Admin wurde nicht gefunden.";
  170. $messageType = "error";
  171. }
  172. } else {
  173. $selectedChangeUser = $changeUsername;
  174. }
  175. }
  176. if ($editDescriptionUsername !== "") {
  177. if (!isset($adminAccounts[$editDescriptionUsername])) {
  178. if ($message === "") {
  179. $message = "Ausgewählter Admin wurde nicht gefunden.";
  180. $messageType = "error";
  181. }
  182. } else {
  183. $selectedDescriptionUser = $editDescriptionUsername;
  184. }
  185. }
  186. ksort($adminAccounts);
  187. $bodyClass = "admin-page";
  188. include __DIR__ . "/../includes/header.php";
  189. ?>
  190. <div class="admin-header">
  191. <h2>Admins verwalten</h2>
  192. <div>
  193. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  194. </div>
  195. </div>
  196. <?php if ($message !== ""): ?>
  197. <div class="alert alert-<?php echo $messageType; ?>">
  198. <?php echo htmlspecialchars($message); ?>
  199. </div>
  200. <?php endif; ?>
  201. <div class="panel">
  202. <p><strong>Eingeloggt als:</strong> <?php echo htmlspecialchars(
  203. $currentAdmin !== "" ? $currentAdmin : "Unbekannt",
  204. ); ?></p>
  205. </div>
  206. <div class="panel">
  207. <h3>Neuen Admin anlegen</h3>
  208. <form method="POST">
  209. <?php echo csrfField(); ?>
  210. <div class="form-group">
  211. <label for="username">Benutzername *</label>
  212. <input type="text" id="username" name="username" required maxlength="50" pattern="[A-Za-z0-9][A-Za-z0-9._-]{2,49}" placeholder="z.B. max.mustermann">
  213. </div>
  214. <div class="form-group">
  215. <label for="description">Beschreibung *</label>
  216. <input type="text" id="description" name="description" required maxlength="120" placeholder="z.B. Kassierer, Shop-Team">
  217. </div>
  218. <div class="form-group">
  219. <label for="email">E-Mail *</label>
  220. <input type="email" id="email" name="email" required maxlength="190" placeholder="z.B. max.mustermann@example.org">
  221. </div>
  222. <div class="form-group">
  223. <label for="password">Passwort (mind. 8 Zeichen) *</label>
  224. <input type="password" id="password" name="password" required minlength="8">
  225. </div>
  226. <div class="form-group">
  227. <label for="password_confirm">Passwort bestätigen *</label>
  228. <input type="password" id="password_confirm" name="password_confirm" required minlength="8">
  229. </div>
  230. <button type="submit" name="add_admin" class="btn">Admin anlegen</button>
  231. </form>
  232. </div>
  233. <div class="panel">
  234. <h3>Admin-Liste</h3>
  235. <div class="table-responsive">
  236. <table class="responsive-table">
  237. <thead>
  238. <tr>
  239. <th>Benutzername</th>
  240. <th>Beschreibung</th>
  241. <th>E-Mail</th>
  242. <th>Aktionen</th>
  243. </tr>
  244. </thead>
  245. <tbody>
  246. <?php foreach ($adminAccounts as $username => $account): ?>
  247. <tr>
  248. <td data-label="Benutzername">
  249. <strong><?php echo htmlspecialchars(
  250. $username,
  251. ); ?></strong>
  252. <?php if ($username === $currentAdmin): ?>
  253. <span class="status status-open status-self">Du</span>
  254. <?php endif; ?>
  255. </td>
  256. <td data-label="Beschreibung">
  257. <?php echo htmlspecialchars($account["description"]); ?>
  258. </td>
  259. <td data-label="E-Mail">
  260. <?php echo htmlspecialchars($account["email"]); ?>
  261. </td>
  262. <td data-label="Aktionen">
  263. <a href="admins.php?edit_description=<?php echo urlencode(
  264. $username,
  265. ); ?>" class="btn btn-small btn-secondary">Profil ändern</a>
  266. <a href="admins.php?change=<?php echo urlencode(
  267. $username,
  268. ); ?>" class="btn btn-small btn-secondary">Passwort ändern</a>
  269. <form method="POST" class="inline-form" onsubmit="return confirm('Admin wirklich löschen?');">
  270. <input type="hidden" name="target_username" value="<?php echo htmlspecialchars(
  271. $username,
  272. ); ?>">
  273. <button type="submit" name="delete_admin" class="btn btn-small">Löschen</button>
  274. </form>
  275. </td>
  276. </tr>
  277. <?php endforeach; ?>
  278. </tbody>
  279. </table>
  280. </div>
  281. </div>
  282. <?php if ($selectedDescriptionUser !== null): ?>
  283. <div class="panel">
  284. <h3>Profil ändern: <?php echo htmlspecialchars(
  285. $selectedDescriptionUser,
  286. ); ?></h3>
  287. <form method="POST">
  288. <?php echo csrfField(); ?>
  289. <input type="hidden" name="target_username" value="<?php echo htmlspecialchars(
  290. $selectedDescriptionUser,
  291. ); ?>">
  292. <div class="form-group">
  293. <label for="description_edit">Beschreibung *</label>
  294. <input type="text" id="description_edit" name="description" maxlength="120" required value="<?php echo htmlspecialchars(
  295. $adminAccounts[$selectedDescriptionUser]["description"],
  296. ); ?>">
  297. </div>
  298. <div class="form-group">
  299. <label for="email_edit">E-Mail *</label>
  300. <input type="email" id="email_edit" name="email" maxlength="190" required value="<?php echo htmlspecialchars(
  301. $adminAccounts[$selectedDescriptionUser]["email"],
  302. ); ?>">
  303. </div>
  304. <button type="submit" name="update_description" class="btn">Profil speichern</button>
  305. <a href="admins.php" class="btn btn-secondary">Abbrechen</a>
  306. </form>
  307. </div>
  308. <?php endif; ?>
  309. <?php if ($selectedChangeUser !== null): ?>
  310. <div class="panel">
  311. <h3>Passwort ändern: <?php echo htmlspecialchars(
  312. $selectedChangeUser,
  313. ); ?></h3>
  314. <form method="POST">
  315. <?php echo csrfField(); ?>
  316. <input type="hidden" name="target_username" value="<?php echo htmlspecialchars(
  317. $selectedChangeUser,
  318. ); ?>">
  319. <div class="form-group">
  320. <label for="new_password">Neues Passwort (mind. 8 Zeichen) *</label>
  321. <input type="password" id="new_password" name="new_password" required minlength="8">
  322. </div>
  323. <div class="form-group">
  324. <label for="new_password_confirm">Neues Passwort bestätigen *</label>
  325. <input type="password" id="new_password_confirm" name="new_password_confirm" required minlength="8">
  326. </div>
  327. <button type="submit" name="change_password" class="btn">Passwort speichern</button>
  328. <a href="admins.php" class="btn btn-secondary">Abbrechen</a>
  329. </form>
  330. </div>
  331. <?php endif; ?>
  332. <?php include __DIR__ . "/../includes/footer.php"; ?>