diff --git a/link.php b/link.php new file mode 100644 index 0000000..a648a56 --- /dev/null +++ b/link.php @@ -0,0 +1,411 @@ + 'gradio' +]; + +// Dateiname, in dem unsere Links gespeichert werden +$LINKS_FILE = __DIR__ . '/links.json'; + +// ---------------------- Funktionen ---------------------------- + +/** + * 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 -------------------------- +if (isset($_POST['action']) && $_POST['action'] === 'login') { + // Login-Formular wurde abgeschickt + $username = trim($_POST['username'] ?? ''); + $password = trim($_POST['password'] ?? ''); + + // Check Benutzername und Passwort + 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'] ?? ''); + + if ($newTitle !== '' && $newUrl !== '') { + $links[] = [ + 'title' => $newTitle, + 'url' => $newUrl + ]; + 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'] ?? ''); + + if ($editIndex >= 0 && isset($links[$editIndex])) { + $links[$editIndex]['title'] = $editTitle; + $links[$editIndex]['url'] = $editUrl; + 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; + } + } +} + +// ---------------------- HTML-Ausgabe -------------------------- +?> + + +
+ + +