'gradio' ]; // Dateiname, in dem unsere Links gespeichert werden $LINKS_FILE = __DIR__ . '/links.json'; /** * Lädt die Linkliste aus dem JSON-File * * @return array */ function loadLinks($file) { if (!file_exists($file)) { return []; } $jsonData = file_get_contents($file); return json_decode($jsonData, true) ?? []; } /** * Schreibt die Linkliste in das JSON-File * * @param array $linksArray * @param string $file */ function saveLinks($linksArray, $file) { file_put_contents($file, json_encode($linksArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); } /** * Prüft, ob ein Nutzer eingeloggt ist. * * @return bool */ function isLoggedIn() { return isset($_SESSION['username']); } // ---------------------- Login/Logout-Prozess ------------------ if (isset($_POST['action']) && $_POST['action'] === 'login') { // Login-Formular wurde abgeschickt $username = trim($_POST['username'] ?? ''); $password = trim($_POST['password'] ?? ''); global $USER_CREDENTIALS; if (array_key_exists($username, $USER_CREDENTIALS) && $USER_CREDENTIALS[$username] === $password ) { $_SESSION['username'] = $username; } else { $loginError = 'Falscher Benutzername oder Passwort!'; } } elseif (isset($_POST['action']) && $_POST['action'] === 'logout') { // Logout session_destroy(); header("Location: {$_SERVER['PHP_SELF']}"); exit; } // ---------------------- Link-Management ----------------------- $links = loadLinks($LINKS_FILE); // Link hinzufügen/bearbeiten/löschen nur, wenn eingeloggt if (isLoggedIn()) { // Link hinzufügen if (isset($_POST['action']) && $_POST['action'] === 'add_link') { $newTitle = trim($_POST['title'] ?? ''); $newUrl = trim($_POST['url'] ?? ''); $newImgUrl = trim($_POST['image'] ?? ''); // Bild-URL (optional) if ($newTitle !== '' && $newUrl !== '') { $links[] = [ 'title' => $newTitle, 'url' => $newUrl, 'image' => $newImgUrl ]; saveLinks($links, $LINKS_FILE); header("Location: {$_SERVER['PHP_SELF']}"); exit; } } // Link bearbeiten if (isset($_POST['action']) && $_POST['action'] === 'edit_link') { $editIndex = intval($_POST['index'] ?? -1); $editTitle = trim($_POST['title'] ?? ''); $editUrl = trim($_POST['url'] ?? ''); $editImg = trim($_POST['image'] ?? ''); if ($editIndex >= 0 && isset($links[$editIndex])) { $links[$editIndex]['title'] = $editTitle; $links[$editIndex]['url'] = $editUrl; $links[$editIndex]['image'] = $editImg; saveLinks($links, $LINKS_FILE); header("Location: {$_SERVER['PHP_SELF']}"); exit; } } // Link löschen if (isset($_POST['action']) && $_POST['action'] === 'delete_link') { $deleteIndex = intval($_POST['index'] ?? -1); if ($deleteIndex >= 0 && isset($links[$deleteIndex])) { array_splice($links, $deleteIndex, 1); saveLinks($links, $LINKS_FILE); header("Location: {$_SERVER['PHP_SELF']}"); exit; } } } ?>