requireAdmin(); $db = Database::getInstance(); $appTitle = $db->getSetting('app_title', 'UniFi Voucher System'); I18n::init(); $error = ''; $success = ''; // Edit site if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['edit_site'])) { if (!$auth->validateCsrfToken($_POST['csrf_token']??'')) { $error = __('error_csrf'); } else { try { $siteId = (int)$_POST['site_id']; $name = trim($_POST['name']); $siteIdStr = trim($_POST['site_id_str']); $controllerUrl = trim($_POST['controller_url']); $username = trim($_POST['username']); $password = $_POST['password']; $publicAccess = isset($_POST['public_access']) ? 1 : 0; if (empty($name)||empty($siteIdStr)||empty($controllerUrl)||empty($username)) throw new Exception(__('error_fill_all')); if (!empty($password)) { $test = UniFiController::testConnection($controllerUrl,$username,$password,$siteIdStr); if ($test !== true) throw new Exception('Verbindung fehlgeschlagen: '.$test); $db->execute("UPDATE sites SET name=?,site_id=?,unifi_controller_url=?,unifi_username=?,unifi_password=?,public_access=? WHERE id=?", [$name,$siteIdStr,$controllerUrl,$username,Crypto::encrypt($password),$publicAccess,$siteId]); } else { $db->execute("UPDATE sites SET name=?,site_id=?,unifi_controller_url=?,unifi_username=?,public_access=? WHERE id=?", [$name,$siteIdStr,$controllerUrl,$username,$publicAccess,$siteId]); } $auth->writeAuditLog($_SESSION['user_id'],'site_edit','site',$siteId,"Site {$name} aktualisiert"); $success = __('sites_updated'); } catch (Exception $e) { $error = $e->getMessage(); } } } // Add site if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['add_site'])) { if (!$auth->validateCsrfToken($_POST['csrf_token']??'')) { $error = __('error_csrf'); } else { try { $name = trim($_POST['name']); $siteId = trim($_POST['site_id']); $controllerUrl = trim($_POST['controller_url']); $username = trim($_POST['username']); $password = $_POST['password']; $publicAccess = isset($_POST['public_access']) ? 1 : 0; if (empty($name)||empty($siteId)||empty($controllerUrl)||empty($username)) throw new Exception(__('error_fill_all')); $test = UniFiController::testConnection($controllerUrl,$username,$password,$siteId); if ($test !== true) throw new Exception('Verbindung fehlgeschlagen: '.$test); $newId = $db->execute("INSERT INTO sites (name,site_id,unifi_controller_url,unifi_username,unifi_password,public_access) VALUES (?,?,?,?,?,?)", [$name,$siteId,$controllerUrl,$username,Crypto::encrypt($password),$publicAccess]); $auth->writeAuditLog($_SESSION['user_id'],'site_create','site',$newId,"Site {$name} erstellt"); $success = __('sites_added'); } catch (Exception $e) { $error = $e->getMessage(); } } } // Delete site if (isset($_GET['delete']) && isset($_GET['token'])) { if ($auth->validateCsrfToken($_GET['token'])) { $delId = (int)$_GET['delete']; $db->query("DELETE FROM sites WHERE id=?", [$delId]); $auth->writeAuditLog($_SESSION['user_id'],'site_delete','site',$delId,'Site gelöscht'); $success = __('sites_deleted'); } else { $error = __('error_csrf'); } } // Toggle site if (isset($_GET['toggle']) && isset($_GET['token'])) { if ($auth->validateCsrfToken($_GET['token'])) { $site = $db->fetchOne("SELECT is_active FROM sites WHERE id=?", [(int)$_GET['toggle']]); if ($site) { $db->query("UPDATE sites SET is_active=? WHERE id=?", [$site['is_active']?0:1,(int)$_GET['toggle']]); $success = 'Site-Status aktualisiert!'; } } else { $error = __('error_csrf'); } } $sites = $db->fetchAll("SELECT * FROM sites ORDER BY name"); $currentPage = 'sites'; ?> <?= __('sites_title') ?> – <?= htmlspecialchars($appTitle) ?>

ID: