admin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 ---
  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?text=Kein+Bild";
  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([
  80. $_POST['new_titel'],
  81. ($_POST['new_typ_id'] == '0' ? null : (int)$_POST['new_typ_id']),
  82. $ean,
  83. $_POST['new_level'],
  84. $bild_url
  85. ]);
  86. $msg = "Spiel erfolgreich angelegt!";
  87. }
  88. // SPIEL AKTUALISIEREN
  89. if (isset($_POST['update_game'])) {
  90. $spiel_id = (int)$_POST['spiel_id'];
  91. $ean = $_POST['ean'];
  92. if (!empty($_POST['update_bild_quelle'])) {
  93. $new_path = downloadGameImage($ean, $_POST['update_bild_quelle']);
  94. if ($new_path) $pdo->prepare("UPDATE spiele SET bild_url = ? WHERE id = ?")->execute([$new_path, $spiel_id]);
  95. }
  96. $stmt = $pdo->prepare("UPDATE spiele SET titel = ?, typ_id = ?, ean = ?, level = ? WHERE id = ?");
  97. $stmt->execute([
  98. $_POST['titel'],
  99. ($_POST['typ_id'] == '0' ? null : (int)$_POST['typ_id']),
  100. $ean,
  101. $_POST['level'],
  102. $spiel_id
  103. ]);
  104. $msg = "Änderungen gespeichert!";
  105. }
  106. // SPIEL LÖSCHEN
  107. if (isset($_POST['delete_game'])) {
  108. $pdo->prepare("DELETE FROM spiele WHERE id = ?")->execute([(int)$_POST['spiel_id']]);
  109. $msg = "Spiel wurde gelöscht!";
  110. }
  111. // GRUPPEN & TYPEN LOGIK
  112. if (isset($_POST['add_player'])) { $pdo->prepare("INSERT INTO spieler (name) VALUES (?)")->execute([$_POST['player_name']]); }
  113. if (isset($_POST['delete_player'])) { $pdo->prepare("DELETE FROM spieler WHERE id = ?")->execute([(int)$_POST['player_id']]); }
  114. if (isset($_POST['add_type'])) { $pdo->prepare("INSERT INTO game_typen (bezeichnung) VALUES (?)")->execute([$_POST['type_name']]); }
  115. if (isset($_POST['delete_type'])) { $pdo->prepare("DELETE FROM game_typen WHERE id = ?")->execute([(int)$_POST['type_id']]); }
  116. // DATEN LADEN
  117. $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();
  118. $spieler = $pdo->query("SELECT * FROM spieler ORDER BY id ASC")->fetchAll();
  119. $typen = $pdo->query("SELECT * FROM game_typen ORDER BY bezeichnung ASC")->fetchAll();
  120. ?>
  121. <!DOCTYPE html>
  122. <html lang="de">
  123. <head>
  124. <meta charset="UTF-8">
  125. <title>EXIT Admin - Verwaltung</title>
  126. <style>
  127. body { font-family: 'Segoe UI', sans-serif; background: #f4f7f6; padding: 20px; color: #333; }
  128. .container { max-width: 1200px; margin: 0 auto; }
  129. .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
  130. .alert { background: #27ae60; color: white; padding: 15px; border-radius: 8px; margin-bottom: 20px; text-align: center; font-weight: bold; }
  131. .tabs { display: flex; gap: 5px; }
  132. .tab-button { padding: 12px 25px; cursor: pointer; background: #ddd; border: none; border-radius: 8px 8px 0 0; font-weight: bold; }
  133. .tab-button.active { background: #fff; border-top: 4px solid #e67e22; color: #e67e22; }
  134. .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; }
  135. .tab-content.active { display: block; }
  136. table { width: 100%; border-collapse: collapse; margin-top: 15px; }
  137. th, td { padding: 12px; border-bottom: 1px solid #eee; text-align: left; }
  138. input, select { padding: 8px; border: 1px solid #ddd; border-radius: 5px; width: 100%; box-sizing: border-box; }
  139. .btn { padding: 8px 15px; border: none; border-radius: 5px; color: white; cursor: pointer; font-weight: bold; text-decoration: none; display: inline-block; }
  140. .btn-add { background: #27ae60; } .btn-save { background: #2980b9; } .btn-del { background: #e74c3c; } .logout { background: #c0392b; font-size: 0.8em; }
  141. </style>
  142. </head>
  143. <body>
  144. <div class="container">
  145. <div class="header">
  146. <h1>🛠 Stammdaten</h1>
  147. <div>
  148. <a href="index.php" class="btn btn-save">Dashboard</a>
  149. <a href="admin.php?logout=1" class="btn logout">Abmelden 🚪</a>
  150. </div>
  151. </div>
  152. <?php if ($msg): ?><div class="alert" id="msg-box"><?= $msg ?></div><?php endif; ?>
  153. <div class="tabs">
  154. <button class="tab-button active" onclick="openTab(event, 'tab-spiele')">🎮 Spiele</button>
  155. <button class="tab-button" onclick="openTab(event, 'tab-gruppen')">👥 Gruppen</button>
  156. <button class="tab-button" onclick="openTab(event, 'tab-typen')">🏷 Typen</button>
  157. </div>
  158. <div id="tab-spiele" class="tab-content active">
  159. <div style="background:#f9f9f9; padding:20px; border-radius:8px; border:1px solid #eee; margin-bottom:30px;">
  160. <h3>🆕 Neues Spiel erfassen</h3>
  161. <form method="POST">
  162. <div style="display:grid; grid-template-columns: 2fr 1fr 1fr 1fr 2fr; gap:10px;">
  163. <input type="text" name="new_titel" placeholder="Spieltitel" required>
  164. <select name="new_typ_id">
  165. <option value="0">-- Typ: Unknown --</option>
  166. <?php foreach($typen as $t): ?><option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['bezeichnung']) ?></option><?php endforeach; ?>
  167. </select>
  168. <input type="text" name="new_ean" placeholder="EAN" required>
  169. <select name="new_level">
  170. <option value="Unknown" selected>-- Level: Unknown --</option>
  171. <option value="Einsteiger">Einsteiger</option>
  172. <option value="Fortgeschrittene">Fortgeschrittene</option>
  173. <option value="Profi">Profi</option>
  174. </select>
  175. <input type="text" name="new_bild_quelle" placeholder="Bild-URL (Download)">
  176. </div>
  177. <button type="submit" name="add_new_game" class="btn btn-add" style="margin-top:15px; width:100%;">Spiel in Datenbank speichern</button>
  178. </form>
  179. </div>
  180. <table>
  181. <thead><tr><th>Bild</th><th>Titel & Typ</th><th>EAN</th><th>Level</th><th>Aktion</th></tr></thead>
  182. <tbody>
  183. <?php foreach ($spiele as $sp): ?>
  184. <tr>
  185. <form method="POST">
  186. <input type="hidden" name="spiel_id" value="<?= $sp['id'] ?>">
  187. <td width="60"><img src="<?= $sp['bild_url'] ?>" width="50" style="border-radius:4px;" onerror="this.src='https://via.placeholder.com/50?text=?'"></td>
  188. <td>
  189. <input type="text" name="titel" value="<?= htmlspecialchars($sp['titel']) ?>" style="margin-bottom:5px;">
  190. <select name="typ_id">
  191. <option value="0" <?= $sp['typ_id'] == null ? 'selected' : '' ?>>-- Unknown --</option>
  192. <?php foreach($typen as $t): ?>
  193. <option value="<?= $t['id'] ?>" <?= $sp['typ_id']==$t['id']?'selected':'' ?>><?= htmlspecialchars($t['bezeichnung']) ?></option>
  194. <?php endforeach; ?>
  195. </select>
  196. </td>
  197. <td><input type="text" name="ean" value="<?= htmlspecialchars($sp['ean']) ?>"></td>
  198. <td>
  199. <select name="level">
  200. <option value="Unknown" <?= ($sp['level'] == 'Unknown' || empty($sp['level'])) ? 'selected' : '' ?>>Unknown</option>
  201. <option value="Einsteiger" <?= $sp['level']=='Einsteiger'?'selected':'' ?>>Einsteiger</option>
  202. <option value="Fortgeschrittene" <?= $sp['level']=='Fortgeschrittene'?'selected':'' ?>>Fortgeschrittene</option>
  203. <option value="Profi" <?= $sp['level']=='Profi'?'selected':'' ?>>Profi</option>
  204. </select>
  205. </td>
  206. <td style="white-space:nowrap;">
  207. <button type="submit" name="update_game" class="btn btn-save">💾</button>
  208. <button type="submit" name="delete_game" class="btn btn-del" onclick="return confirm('Löschen?')">🗑</button>
  209. </td>
  210. </form>
  211. </tr>
  212. <?php endforeach; ?>
  213. </tbody>
  214. </table>
  215. </div>
  216. <div id="tab-gruppen" class="tab-content">
  217. <h3>👥 Teams / Spieler</h3>
  218. <form method="POST" style="display:flex; gap:10px; margin-bottom:20px;">
  219. <input type="text" name="player_name" placeholder="Name der Gruppe" required>
  220. <button type="submit" name="add_player" class="btn btn-add">Anlegen</button>
  221. </form>
  222. <table>
  223. <?php foreach ($spieler as $s): ?>
  224. <tr><td><?= htmlspecialchars($s['name']) ?></td><td><form method="POST"><input type="hidden" name="player_id" value="<?= $s['id'] ?>"><button type="submit" name="delete_player" class="btn btn-del">🗑</button></form></td></tr>
  225. <?php endforeach; ?>
  226. </table>
  227. </div>
  228. <div id="tab-typen" class="tab-content">
  229. <h3>🏷 Spiel-Typen</h3>
  230. <form method="POST" style="display:flex; gap:10px; margin-bottom:20px;">
  231. <input type="text" name="type_name" placeholder="Typ-Bezeichnung" required>
  232. <button type="submit" name="add_type" class="btn btn-add">Anlegen</button>
  233. </form>
  234. <table>
  235. <?php foreach ($typen as $t): ?>
  236. <tr><td><?= htmlspecialchars($t['bezeichnung']) ?></td><td><form method="POST"><input type="hidden" name="type_id" value="<?= $t['id'] ?>"><button type="submit" name="delete_type" class="btn btn-del">🗑</button></form></td></tr>
  237. <?php endforeach; ?>
  238. </table>
  239. </div>
  240. </div>
  241. <script>
  242. function openTab(evt, tabName) {
  243. var i, tabcontent, tablinks;
  244. tabcontent = document.getElementsByClassName("tab-content");
  245. for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; }
  246. tablinks = document.getElementsByClassName("tab-button");
  247. for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); }
  248. document.getElementById(tabName).style.display = "block";
  249. evt.currentTarget.className += " active";
  250. }
  251. setTimeout(function() { var msg = document.getElementById('msg-box'); if(msg) msg.style.display = 'none'; }, 3000);
  252. </script>
  253. </body>
  254. </html>