edit_database.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. session_start();
  3. // Config lade
  4. $config = include(__DIR__ . '/../../config.php');
  5. // WebsiteID us der URL oder session hole
  6. $websiteId = isset($_GET['websiteId']) ? (int)$_GET['websiteId'] : (isset($_SESSION['websiteId']) ? (int)$_SESSION['websiteId'] : 1);
  7. $_SESSION['websiteId'] = $websiteId;
  8. // Website config ider config finde
  9. $websiteConfig = array_filter($config, function($site) use ($websiteId) {
  10. return isset($site['id']) && $site['id'] === $websiteId;
  11. });
  12. $websiteConfig = reset($websiteConfig);
  13. // Sqlite datenbank ahfigge
  14. $db = new SQLite3($websiteConfig['database']);
  15. // Backup erstelle oder ganzi datehbank lôsche *evil laugh* 😈 jk mer propbieret sehr schlecht IDs wieder sekuentiel zmache
  16. if (isset($_POST['backup']) || isset($_POST['delete_row']) || isset($_POST['reassign_ids'])) {
  17. $source = $websiteConfig['database'];
  18. $backupDir = __DIR__ . '/../../' . $websiteConfig['backup_folder'];
  19. $timestamp = date('Y-m-d-H-i-s');
  20. $destination = $backupDir . '/' . $timestamp . '.db';
  21. // machen das backup directory wenns nid git
  22. if (!is_dir($backupDir)) {
  23. mkdir($backupDir, 0755, true);
  24. }
  25. // omg file deht ineh kopiere 😱
  26. if (copy($source, $destination)) {
  27. $backupMessage = "Database backup created successfully: " . htmlspecialchars($destination);
  28. } else {
  29. $backupMessage = "Failed to create database backup.";
  30. }
  31. }
  32. $selectedTable = $_POST['table'] ?? $_SESSION['selectedTable'] ?? '';
  33. $columns = [];
  34. $rows = [];
  35. $sortOrder = $_GET['sortOrder'] ?? 'ASC';
  36. $tablesResult = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name != 'sqlite_sequence'");
  37. $tables = [];
  38. while ($row = $tablesResult->fetchArray(SQLITE3_ASSOC)) {
  39. $tables[] = $row['name'];
  40. }
  41. if ($selectedTable && in_array($selectedTable, $tables)) {
  42. $_SESSION['selectedTable'] = $selectedTable;
  43. $columnsResult = $db->query("PRAGMA table_info($selectedTable)");
  44. while ($row = $columnsResult->fetchArray(SQLITE3_ASSOC)) {
  45. $columns[] = $row['name'];
  46. }
  47. if ($selectedTable == 'content' && in_array('date', $columns)) {
  48. $orderBy = "ORDER BY date DESC"; // das hier machen default sorting nach datum und ziiht de neuschti row zerscht will susch isch das unmanagebar
  49. } else {
  50. $orderBy = in_array('date', $columns) ? "ORDER BY date $sortOrder" : "ORDER BY id ASC";
  51. }
  52. $rowsResult = $db->query("SELECT * FROM $selectedTable $orderBy");
  53. while ($row = $rowsResult->fetchArray(SQLITE3_ASSOC)) {
  54. $rows[] = $row;
  55. }
  56. } else {
  57. $selectedTable = '';
  58. $_SESSION['selectedTable'] = '';
  59. }
  60. if (isset($_POST['edit_row'])) {
  61. header("Location: edit_row.php?table=$selectedTable&id=" . $_POST['id']);
  62. exit();
  63. }
  64. if (isset($_POST['delete_row'])) {
  65. $idsToDelete = $_POST['ids'] ?? [];
  66. foreach ($idsToDelete as $id) {
  67. $stmt = $db->prepare("DELETE FROM $selectedTable WHERE id = :id");
  68. $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
  69. $stmt->execute();
  70. }
  71. $message = "Selected rows deleted successfully (most likely).";
  72. }
  73. if (isset($_POST['insert_row'])) {
  74. $values = $_POST['values'];
  75. $highestIdResult = $db->querySingle("SELECT MAX(id) as max_id FROM $selectedTable");
  76. $highestId = $highestIdResult ? $highestIdResult : 0;
  77. $values['id'] = $highestId + 1;
  78. if ($selectedTable == 'content') {
  79. if (empty($values['date'])) {
  80. $values['date'] = date('Y-m-d H:i:s');
  81. }
  82. }
  83. $columnsString = implode(", ", array_keys($values));
  84. $placeholders = implode(", ", array_fill(0, count($values), "?"));
  85. $stmt = $db->prepare("INSERT INTO $selectedTable ($columnsString) VALUES ($placeholders)");
  86. $index = 1;
  87. foreach ($values as $value) {
  88. $stmt->bindValue($index, $value ?: null, SQLITE3_TEXT);
  89. $index++;
  90. }
  91. $stmt->execute();
  92. $message = "Row inserted successfully. (maybe)";
  93. }
  94. if (isset($_POST['swap_row'])) {
  95. $id = $_POST['id'];
  96. $targetId = $_POST['target_id'];
  97. $db->exec("BEGIN TRANSACTION");
  98. $db->exec("UPDATE $selectedTable SET id = -1 WHERE id = $id");
  99. $db->exec("UPDATE $selectedTable SET id = $id WHERE id = $targetId");
  100. $db->exec("UPDATE $selectedTable SET id = $targetId WHERE id = -1");
  101. $db->exec("COMMIT");
  102. $message = "Row swapped successfully for sure.";
  103. }
  104. if (isset($_POST['push_row'])) {
  105. $id = $_POST['id'];
  106. $targetId = $_POST['target_id'];
  107. $tempTable = $selectedTable . '_temp';
  108. $db->exec("BEGIN TRANSACTION");
  109. $db->exec("CREATE TEMPORARY TABLE $tempTable AS SELECT * FROM $selectedTable");
  110. $db->exec("UPDATE $tempTable SET id = -1 WHERE id = $id");
  111. if ($id < $targetId) {
  112. $db->exec("UPDATE $tempTable SET id = id - 1 WHERE id > $id AND id <= $targetId");
  113. } else {
  114. $db->exec("UPDATE $tempTable SET id = id + 1 WHERE id < $id AND id >= $targetId");
  115. }
  116. $db->exec("UPDATE $tempTable SET id = $targetId WHERE id = -1");
  117. $db->exec("DELETE FROM $selectedTable");
  118. $db->exec("INSERT INTO $selectedTable SELECT * FROM $tempTable");
  119. $db->exec("DROP TABLE $tempTable");
  120. $db->exec("COMMIT");
  121. $message = "Row pushed successfully (perhaps).";
  122. }
  123. if (isset($_POST['reassign_ids'])) {
  124. $tempBackup = $backupDir . '/' . $timestamp . '_temp.db';
  125. copy($source, $tempBackup);
  126. try {
  127. $db->exec("BEGIN TRANSACTION");
  128. $tempTable = $selectedTable . '_temp';
  129. $db->exec("CREATE TEMPORARY TABLE $tempTable AS SELECT * FROM $selectedTable");
  130. $db->exec("DELETE FROM $selectedTable");
  131. if (in_array('date', $columns)) {
  132. $orderBy = "ORDER BY date ASC";
  133. } else {
  134. $orderBy = "ORDER BY id ASC";
  135. }
  136. $rowsResult = $db->query("SELECT * FROM $tempTable $orderBy");
  137. $newId = 1;
  138. while ($row = $rowsResult->fetchArray(SQLITE3_ASSOC)) {
  139. $row['id'] = $newId++;
  140. $columnsString = implode(", ", array_keys($row));
  141. $placeholders = implode(", ", array_fill(0, count($row), "?"));
  142. $stmt = $db->prepare("INSERT INTO $selectedTable ($columnsString) VALUES ($placeholders)");
  143. $index = 1;
  144. foreach ($row as $value) {
  145. $stmt->bindValue($index, $value ?: null, SQLITE3_TEXT);
  146. $index++;
  147. }
  148. $stmt->execute();
  149. }
  150. $db->exec("DROP TABLE $tempTable");
  151. $db->exec("COMMIT");
  152. $message = "IDs reassigned successfully.";
  153. } catch (Exception $e) {
  154. copy($tempBackup, $source);
  155. $message = "Failed to reassign IDs. Database restored from backup. (probably)";
  156. } finally {
  157. unlink($tempBackup);
  158. }
  159. }
  160. ?>
  161. <!DOCTYPE html>
  162. <html lang="en">
  163. <head>
  164. <meta charset="UTF-8">
  165. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  166. <title>Edit Database</title>
  167. </head>
  168. <body>
  169. <form method="post" action="../index.php">
  170. <button type="submit">Back to Admin Panel</button>
  171. </form>
  172. <h1>Edit Database</h1>
  173. <form method="get" action="edit_database.php">
  174. <label for="websiteId">Select Website:</label>
  175. <select name="websiteId" id="websiteId" onchange="this.form.submit()">
  176. <?php foreach ($config as $site): ?>
  177. <?php if (isset($site['id'])): ?>
  178. <option value="<?php echo $site['id']; ?>" <?php echo ($websiteId == $site['id']) ? 'selected' : ''; ?>>
  179. <?php echo htmlspecialchars($site['name']); ?>
  180. </option>
  181. <?php endif; ?>
  182. <?php endforeach; ?>
  183. </select>
  184. </form>
  185. <form method="post">
  186. <button type="submit" name="backup">Backup das Database</button>
  187. </form>
  188. <?php if (isset($backupMessage)): ?>
  189. <p><?php echo $backupMessage; ?></p>
  190. <?php endif; ?>
  191. <form method="post">
  192. <label for="table">Select das Table:</label>
  193. <select id="table" name="table" onchange="this.form.submit()">
  194. <option value="">-- Select a table --</option>
  195. <?php foreach ($tables as $table): ?>
  196. <option value="<?php echo htmlspecialchars($table); ?>" <?php echo ($selectedTable == $table) ? 'selected' : ''; ?>><?php echo htmlspecialchars($table); ?></option>
  197. <?php endforeach; ?>
  198. </select>
  199. </form>
  200. <form method="post">
  201. <button type="submit" name="refresh">Refresh</button>
  202. </form>
  203. <?php if ($selectedTable): ?>
  204. <h2>Editing Table: <?php echo htmlspecialchars($selectedTable); ?></h2>
  205. <?php if ($selectedTable == 'content'): ?>
  206. <form method="post">
  207. <input type="hidden" name="table" value="<?php echo htmlspecialchars($selectedTable); ?>">
  208. <table border="1">
  209. <thead>
  210. <tr>
  211. <th>Select</th>
  212. <?php foreach ($columns as $column): ?>
  213. <th>
  214. <?php echo htmlspecialchars($column); ?>
  215. <?php if ($column == 'date'): ?>
  216. <a href="?table=<?php echo htmlspecialchars($selectedTable); ?>&sortOrder=<?php echo $sortOrder == 'ASC' ? 'DESC' : 'ASC'; ?>">Sort by Date</a>
  217. <?php endif; ?>
  218. </th>
  219. <?php endforeach; ?>
  220. <th>Actions</th>
  221. </tr>
  222. </thead>
  223. <tbody>
  224. <tr>
  225. <td></td>
  226. <?php foreach ($columns as $column): ?>
  227. <td>
  228. <?php if ($column == 'id'): ?>
  229. <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" disabled>
  230. <?php elseif ($column == 'date'): ?>
  231. <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="<?php echo date('Y-m-d H:i:s'); ?>" disabled>
  232. <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]">
  233. <?php endif; ?>
  234. </td>
  235. <?php endforeach; ?>
  236. <td>
  237. <button type="submit" name="insert_row">Add Row</button>
  238. </td>
  239. </tr>
  240. <?php foreach ($rows as $row): ?>
  241. <tr>
  242. <td><input type="checkbox" name="ids[]" value="<?php echo $row['id']; ?>"></td>
  243. <?php foreach ($columns as $column): ?>
  244. <td><?php echo htmlspecialchars($row[$column] ?? ''); ?></td>
  245. <?php endforeach; ?>
  246. <td>
  247. <form method="post" style="display:inline;">
  248. <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
  249. <button type="submit" name="edit_row">Edit</button>
  250. </form>
  251. <form method="post" style="display:inline;">
  252. <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
  253. <button type="submit" name="delete_row">Delete</button>
  254. </form>
  255. <form method="post" style="display:inline;">
  256. <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
  257. <input type="text" name="target_id" placeholder="Target ID">
  258. <button type="submit" name="swap_row">Swap</button>
  259. <button type="submit" name="push_row">Push</button>
  260. </form>
  261. </td>
  262. </tr>
  263. <?php endforeach; ?>
  264. </tbody>
  265. </table>
  266. <button type="submit" name="delete_row">Maybe Delete Selected Rows</button>
  267. <button type="submit" name="reassign_ids">Maybe Reassign IDs</button>
  268. </form>
  269. <?php else: ?>
  270. <form method="post">
  271. <input type="hidden" name="table" value="<?php echo htmlspecialchars($selectedTable); ?>">
  272. <table border="1">
  273. <thead>
  274. <tr>
  275. <th>Select</th>
  276. <?php foreach ($columns as $column): ?>
  277. <th>
  278. <?php echo htmlspecialchars($column); ?>
  279. <?php if ($column == 'date'): ?>
  280. <a href="?table=<?php echo htmlspecialchars($selectedTable); ?>&sortOrder=<?php echo $sortOrder == 'ASC' ? 'DESC' : 'ASC'; ?>">Sort by Datum</a>
  281. <?php endif; ?>
  282. </th>
  283. <?php endforeach; ?>
  284. <th>Actions</th>
  285. </tr>
  286. </thead>
  287. <tbody>
  288. <?php foreach ($rows as $row): ?>
  289. <tr>
  290. <td><input type="checkbox" name="ids[]" value="<?php echo $row['id']; ?>"></td>
  291. <?php foreach ($columns as $column): ?>
  292. <td><?php echo htmlspecialchars($row[$column] ?? ''); ?></td>
  293. <?php endforeach; ?>
  294. <td>
  295. <form method="post" style="display:inline;">
  296. <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
  297. <button type="submit" name="edit_row">Edit</button>
  298. </form>
  299. <form method="post" style="display:inline;">
  300. <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
  301. <button type="submit" name="delete_row">Bye</button>
  302. </form>
  303. <form method="post" style="display:inline;">
  304. <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
  305. <input type="text" name="target_id" placeholder="Target ID">
  306. <button type="submit" name="swap_row">Swap</button>
  307. <button type="submit" name="push_row">Push</button>
  308. </form>
  309. </td>
  310. </tr>
  311. <?php endforeach; ?>
  312. <tr>
  313. <td></td>
  314. <?php foreach ($columns as $column): ?>
  315. <td>
  316. <?php if ($column == 'id'): ?>
  317. <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" disabled>
  318. <?php elseif ($column == 'date'): ?>
  319. <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="<?php echo date('Y-m-d H:i:s'); ?>" disabled>
  320. <?php elseif ($selectedTable == 'content' && $column == 'custom_html'): ?>
  321. <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="0">
  322. <?php else: ?>
  323. <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]">
  324. <?php endif; ?>
  325. </td>
  326. <?php endforeach; ?>
  327. <td>
  328. <button type="submit" name="insert_row">Add Row</button>
  329. </td>
  330. </tr>
  331. </tbody>
  332. </table>
  333. <button type="submit" name="delete_row">Delete Selected Rows</button>
  334. <button type="submit" name="reassign_ids">Reassign IDs</button>
  335. </form>
  336. <?php endif; ?>
  337. <?php else: ?>
  338. <p>No das table selected. Please select das table to editieren.</p>
  339. <?php endif; ?>
  340. </body>
  341. </html>