admin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. session_start();
  3. require_once 'config.php'; // Enthält ADMIN_PASSWORD und DB-Konstanten
  4. // --- 1. LOGOUT LOGIK ---
  5. if (isset($_GET['logout'])) {
  6. session_destroy();
  7. header("Location: admin.php");
  8. exit;
  9. }
  10. // --- 2. LOGIN SCHUTZ ---
  11. $show_login = true;
  12. if (isset($_SESSION['admin_auth']) && $_SESSION['admin_auth'] === true) {
  13. $show_login = false;
  14. }
  15. if (isset($_POST['login_auth'])) {
  16. if ($_POST['pw'] === ADMIN_PASSWORD) {
  17. $_SESSION['admin_auth'] = true;
  18. $show_login = false;
  19. } else {
  20. $login_error = "Falsches Passwort!";
  21. }
  22. }
  23. if ($show_login):
  24. ?>
  25. <!DOCTYPE html>
  26. <html lang="de">
  27. <head>
  28. <meta charset="UTF-8">
  29. <title>Admin Login</title>
  30. <style>
  31. body { font-family: 'Segoe UI', sans-serif; background: #121212; color: white; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
  32. .login-box { background: #1e1e1e; padding: 40px; border-radius: 12px; border: 1px solid #e67e22; text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.5); width: 300px; }
  33. input { width: 100%; padding: 12px; margin: 15px 0; border-radius: 5px; border: 1px solid #444; background: #2a2a2a; color: white; box-sizing: border-box; }
  34. button { background: #e67e22; color: white; border: none; padding: 12px; border-radius: 5px; cursor: pointer; width: 100%; font-weight: bold; }
  35. .error { color: #e74c3c; margin-bottom: 10px; font-size: 0.9em; }
  36. a { color: #888; text-decoration: none; font-size: 0.8em; margin-top: 15px; display: inline-block; }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="login-box">
  41. <h2>🔐 Admin Login</h2>
  42. <?php if(isset($login_error)) echo "<div class='error'>$login_error</div>"; ?>
  43. <form method="POST">
  44. <input type="password" name="pw" placeholder="Passwort" autofocus required>
  45. <button type="submit" name="login_auth">Einloggen</button>
  46. </form>
  47. <a href="index.php"> Zurück zum Dashboard</a>
  48. </div>
  49. </body>
  50. </html>
  51. <?php
  52. exit;
  53. endif;
  54. // --- 3. HAUPT-LOGIK (NUR ERREICHBAR WENN EINGELOGGT) ---
  55. require_once 'db_config.php';
  56. $msg = "";
  57. // Bild-Download Hilfsfunktion
  58. function downloadGameImage($ean, $url) {
  59. if (!empty($ean) && filter_var($url, FILTER_VALIDATE_URL)) {
  60. $content = @file_get_contents($url);
  61. if ($content) {
  62. if (!is_dir('img')) mkdir('img', 0777, true);
  63. $path = "img/" . $ean . ".jpg";
  64. file_put_contents($path, $content);
  65. return $path;
  66. }
  67. }
  68. return null;
  69. }
  70. // SPIEL HINZUFÜGEN
  71. if (isset($_POST['add_new_game'])) {
  72. $ean = $_POST['new_ean'];
  73. $bild_url = "https://via.placeholder.com/60";
  74. if (!empty($_POST['new_bild_quelle'])) {
  75. $downloaded = downloadGameImage($ean, $_POST['new_bild_quelle']);
  76. if ($downloaded) $bild_url = $downloaded;
  77. }
  78. $stmt = $pdo->prepare("INSERT INTO spiele (titel, typ_id, ean, level, bild_url) VALUES (?, ?, ?, ?, ?)");
  79. $stmt->execute([$_POST['new_titel'], (int)$_POST['new_typ_id'], $ean, $_POST['new_level'], $bild_url]);
  80. $msg = "Spiel erfolgreich angelegt!";
  81. }
  82. // SPIEL AKTUALISIEREN
  83. if (isset($_POST['update_game'])) {
  84. $spiel_id = (int)$_POST['spiel_id'];
  85. $ean = $_POST['ean'];
  86. if (!empty($_POST['update_bild_quelle'])) {
  87. $new_path = downloadGameImage($ean, $_POST['update_bild_quelle']);
  88. if ($new_path) $pdo->prepare("UPDATE spiele SET bild_url = ? WHERE id = ?")->execute([$new_path, $spiel_id]);
  89. }
  90. $stmt = $pdo->prepare("UPDATE spiele SET titel = ?, typ_id = ?, ean = ?, level = ? WHERE id = ?");
  91. $stmt->execute([$_POST['titel'], (int)$_POST['typ_id'], $ean, $_POST['level'], $spiel_id]);
  92. $msg = "Änderungen gespeichert!";
  93. }
  94. // SPIEL LÖSCHEN
  95. if (isset($_POST['delete_game'])) {
  96. $pdo->prepare("DELETE FROM spiele WHERE id = ?")->execute([(int)$_POST['spiel_id']]);
  97. $msg = "Spiel wurde gelöscht!";
  98. }
  99. // GRUPPEN & TYPEN LOGIK
  100. if (isset($_POST['add_player'])) { $pdo->prepare("INSERT INTO spieler (name) VALUES (?)")->execute([$_POST['player_name']]); }
  101. if (isset($_POST['delete_player'])) { $pdo->prepare("DELETE FROM spieler WHERE id = ?")->execute([(int)$_POST['player_id']]); }
  102. if (isset($_POST['add_type'])) { $pdo->prepare("INSERT INTO game_typen (bezeichnung) VALUES (?)")->execute([$_POST['type_name']]); }
  103. if (isset($_POST['delete_type'])) { $pdo->prepare("DELETE FROM game_typen WHERE id = ?")->execute([(int)$_POST['type_id']]); }
  104. // DATEN LADEN
  105. $spiele = $pdo->query("SELECT s.*, t.bezeichnung as typ_name FROM spiele s LEFT JOIN game_typen t ON s.typ_id = t.id ORDER BY s.id DESC")->fetchAll();
  106. $spieler = $pdo->query("SELECT * FROM spieler ORDER BY id ASC")->fetchAll();
  107. $typen = $pdo->query("SELECT * FROM game_typen ORDER BY bezeichnung ASC")->fetchAll();
  108. ?>
  109. <!DOCTYPE html>
  110. <html lang="de">
  111. <head>
  112. <meta charset="UTF-8">
  113. <title>EXIT Admin - Verwaltung</title>
  114. <style>
  115. body { font-family: 'Segoe UI', sans-serif; background: #f4f7f6; padding: 20px; color: #333; }
  116. .container { max-width: 1200px; margin: 0 auto; }
  117. .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
  118. .alert { background: #27ae60; color: white; padding: 15px; border-radius: 8px; margin-bottom: 20px; text-align: center; font-weight: bold; }
  119. .tabs { display: flex; gap: 5px; }
  120. .tab-button { padding: 12px 25px; cursor: pointer; background: #ddd; border: none; border-radius: 8px 8px 0 0; font-weight: bold; transition: 0.3s; }
  121. .tab-button.active { background: #fff; border-top: 4px solid #e67e22; color: #e67e22; }
  122. .tab-content { background: #fff; padding: 25px; border-radius: 0 8px 8px 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); display: none; }
  123. .tab-content.active { display: block; }
  124. table { width: 100%; border-collapse: collapse; margin-top: 15px; }
  125. th, td { padding: 12px; border-bottom: 1px solid #eee; text-align: left; }
  126. input, select { padding: 8px; border: 1px solid #ddd; border-radius: 5px; width: 100%; box-sizing: border-box; }
  127. .btn { padding: 8px 15px; border: none; border-radius: 5px; color: white; cursor: pointer; font-weight: bold; text-decoration: none; display: inline-block; }
  128. .btn-add { background: #27ae60; }
  129. .btn-save { background: #2980b9; }
  130. .btn-del { background: #e74c3c; }
  131. .logout { background: #c0392b; font-size: 0.8em; }
  132. </style>
  133. </head>
  134. <body>
  135. <div class="container">
  136. <div class="header">
  137. <h1>🛠 Stammdaten</h1>
  138. <div>
  139. <a href="index.php" class="btn btn-save">Dashboard</a>
  140. <a href="admin.php?logout=1" class="btn logout">Abmelden 🚪</a>
  141. </div>
  142. </div>
  143. <?php if ($msg): ?><div class="alert" id="msg-box"><?= $msg ?></div><?php endif; ?>
  144. <div class="tabs">
  145. <button class="tab-button active" onclick="openTab(event, 'tab-spiele')">🎮 Spiele</button>
  146. <button class="tab-button" onclick="openTab(event, 'tab-gruppen')">👥 Gruppen</button>
  147. <button class="tab-button" onclick="openTab(event, 'tab-typen')">🏷 Typen</button>
  148. </div>
  149. <div id="tab-spiele" class="tab-content active">
  150. <div style="background:#f9f9f9; padding:20px; border-radius:8px; border:1px solid #eee; margin-bottom:30px;">
  151. <h3>🆕 Neues Spiel erfassen</h3>
  152. <form method="POST">
  153. <div style="display:grid; grid-template-columns: 2fr 1fr 1fr 1fr 2fr; gap:10px;">
  154. <input type="text" name="new_titel" placeholder="Spieltitel" required>
  155. <select name="new_typ_id">
  156. <?php foreach($typen as $t): ?><option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['bezeichnung']) ?></option><?php endforeach; ?>
  157. </select>
  158. <input type="text" name="new_ean" placeholder="EAN" required>
  159. <select name="new_level">
  160. <option value="Einsteiger">Einsteiger</option>
  161. <option value="Fortgeschrittene">Fortgeschrittene</option>
  162. <option value="Profi">Profi</option>
  163. </select>
  164. <input type="text" name="new_bild_quelle" placeholder="Bild-URL (Download)">
  165. </div>
  166. <button type="submit" name="add_new_game" class="btn btn-add" style="margin-top:15px; width:100%;">Spiel in Datenbank speichern</button>
  167. </form>
  168. </div>
  169. <table>
  170. <thead><tr><th>Bild</th><th>Titel & Typ</th><th>EAN</th><th>Level</th><th>Aktion</th></tr></thead>
  171. <tbody>
  172. <?php foreach ($spiele as $sp): ?>
  173. <tr>
  174. <form method="POST">
  175. <input type="hidden" name="spiel_id" value="<?= $sp['id'] ?>">
  176. <td width="60"><img src="<?= $sp['bild_url'] ?>" width="50" style="border-radius:4px;"></td>
  177. <td>
  178. <input type="text" name="titel" value="<?= htmlspecialchars($sp['titel']) ?>" style="margin-bottom:5px;">
  179. <select name="typ_id">
  180. <?php foreach($typen as $t): ?>
  181. <option value="<?= $t['id'] ?>" <?= $sp['typ_id']==$t['id']?'selected':'' ?>><?= htmlspecialchars($t['bezeichnung']) ?></option>
  182. <?php endforeach; ?>
  183. </select>
  184. </td>
  185. <td><input type="text" name="ean" value="<?= htmlspecialchars($sp['ean']) ?>"></td>
  186. <td>
  187. <select name="level">
  188. <option value="Einsteiger" <?= $sp['level']=='Einsteiger'?'selected':'' ?>>Einsteiger</option>
  189. <option value="Fortgeschrittene" <?= $sp['level']=='Fortgeschrittene'?'selected':'' ?>>Fortgeschrittene</option>
  190. <option value="Profi" <?= $sp['level']=='Profi'?'selected':'' ?>>Profi</option>
  191. </select>
  192. </td>
  193. <td style="white-space:nowrap;">
  194. <button type="submit" name="update_game" class="btn btn-save" title="Speichern">💾</button>
  195. <button type="submit" name="delete_game" class="btn btn-del" onclick="return confirm('Wirklich löschen?')" title="Löschen">🗑</button>
  196. </td>
  197. </form>
  198. </tr>
  199. <?php endforeach; ?>
  200. </tbody>
  201. </table>
  202. </div>
  203. <div id="tab-gruppen" class="tab-content">
  204. <h3>👥 Teams / Spieler</h3>
  205. <form method="POST" style="display:flex; gap:10px; margin-bottom:20px;">
  206. <input type="text" name="player_name" placeholder="Name der Gruppe" required>
  207. <button type="submit" name="add_player" class="btn btn-add">Anlegen</button>
  208. </form>
  209. <table>
  210. <?php foreach ($spieler as $s): ?>
  211. <tr>
  212. <td><?= htmlspecialchars($s['name']) ?></td>
  213. <td width="50">
  214. <form method="POST">
  215. <input type="hidden" name="player_id" value="<?= $s['id'] ?>">
  216. <button type="submit" name="delete_player" class="btn btn-del">🗑</button>
  217. </form>
  218. </td>
  219. </tr>
  220. <?php endforeach; ?>
  221. </table>
  222. </div>
  223. <div id="tab-typen" class="tab-content">
  224. <h3>🏷 Spiel-Typen (z.B. EXIT, Adventure Games)</h3>
  225. <form method="POST" style="display:flex; gap:10px; margin-bottom:20px;">
  226. <input type="text" name="type_name" placeholder="Typ-Bezeichnung" required>
  227. <button type="submit" name="add_type" class="btn btn-add">Anlegen</button>
  228. </form>
  229. <table>
  230. <?php foreach ($typen as $t): ?>
  231. <tr>
  232. <td><?= htmlspecialchars($t['bezeichnung']) ?></td>
  233. <td width="50">
  234. <form method="POST">
  235. <input type="hidden" name="type_id" value="<?= $t['id'] ?>">
  236. <button type="submit" name="delete_type" class="btn btn-del">🗑</button>
  237. </form>
  238. </td>
  239. </tr>
  240. <?php endforeach; ?>
  241. </table>
  242. </div>
  243. </div>
  244. <script>
  245. function openTab(evt, tabName) {
  246. var i, tabcontent, tablinks;
  247. tabcontent = document.getElementsByClassName("tab-content");
  248. for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; }
  249. tablinks = document.getElementsByClassName("tab-button");
  250. for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); }
  251. document.getElementById(tabName).style.display = "block";
  252. evt.currentTarget.className += " active";
  253. }
  254. setTimeout(function() {
  255. var msg = document.getElementById('msg-box');
  256. if(msg) msg.style.display = 'none';
  257. }, 3000);
  258. </script>
  259. </body>
  260. </html>