requireAdmin(); $db = Database::getInstance(); $appTitle = $db->getSetting('app_title', 'UniFi Voucher System'); I18n::init(); $error = ''; $success = ''; // AJAX: Verbindungstest mit gespeicherten Zugangsdaten (Health-Check pro Site) if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['ajax_test_site'])) { header('Content-Type: application/json'); if (!$auth->validateCsrfToken($_POST['csrf_token'] ?? '')) { echo json_encode(['success' => false, 'message' => __('error_csrf')]); exit; } $site = $db->fetchOne("SELECT * FROM sites WHERE id=?", [(int)$_POST['ajax_test_site']]); if (!$site) { echo json_encode(['success' => false, 'message' => __('error_site_not_found')]); exit; } $test = UniFiController::testConnection( $site['unifi_controller_url'], $site['unifi_username'], Crypto::decrypt($site['unifi_password']), $site['site_id'], $site['ssl_verify'] ?? 0 ); echo json_encode([ 'success' => $test === true, 'message' => $test === true ? __('site_test_ok') : __('site_test_fail') . ': ' . $test, ]); exit; } // 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; $sslVerify = isset($_POST['ssl_verify']) ? 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,$sslVerify); if ($test !== true) throw new Exception(__('site_test_fail').': '.$test); $db->execute("UPDATE sites SET name=?,site_id=?,unifi_controller_url=?,unifi_username=?,unifi_password=?,public_access=?,ssl_verify=? WHERE id=?", [$name,$siteIdStr,$controllerUrl,$username,Crypto::encrypt($password),$publicAccess,$sslVerify,$siteId]); } else { // Auch ohne Passwortaenderung testen (mit gespeichertem Passwort) – // sonst fallen Tippfehler in URL/Username erst beim naechsten Voucher auf. $stored = $db->fetchOne("SELECT unifi_password FROM sites WHERE id=?", [$siteId]); if (!$stored) throw new Exception(__('error_site_not_found')); $test = UniFiController::testConnection($controllerUrl,$username,Crypto::decrypt($stored['unifi_password']),$siteIdStr,$sslVerify); if ($test !== true) throw new Exception(__('site_test_fail').': '.$test); $db->execute("UPDATE sites SET name=?,site_id=?,unifi_controller_url=?,unifi_username=?,public_access=?,ssl_verify=? WHERE id=?", [$name,$siteIdStr,$controllerUrl,$username,$publicAccess,$sslVerify,$siteId]); } $auth->writeAuditLog($_SESSION['user_id'],'site_edit','site',$siteId,"Site {$name} aktualisiert"); flashSet(__('sites_updated')); header('Location: sites.php'); exit; } 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; $sslVerify = isset($_POST['ssl_verify']) ? 1 : 0; if (empty($name)||empty($siteId)||empty($controllerUrl)||empty($username)) throw new Exception(__('error_fill_all')); $test = UniFiController::testConnection($controllerUrl,$username,$password,$siteId,$sslVerify); if ($test !== true) throw new Exception(__('site_test_fail').': '.$test); $newId = $db->execute("INSERT INTO sites (name,site_id,unifi_controller_url,unifi_username,unifi_password,public_access,ssl_verify) VALUES (?,?,?,?,?,?,?)", [$name,$siteId,$controllerUrl,$username,Crypto::encrypt($password),$publicAccess,$sslVerify]); $auth->writeAuditLog($_SESSION['user_id'],'site_create','site',$newId,"Site {$name} erstellt"); flashSet(__('sites_added')); header('Location: sites.php'); exit; } catch (Exception $e) { $error = $e->getMessage(); } } } // Delete site (POST + PRG) if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['delete_site'])) { if ($auth->validateCsrfToken($_POST['csrf_token'] ?? '')) { $delId = (int)$_POST['delete_site']; $db->query("DELETE FROM sites WHERE id=?", [$delId]); $auth->writeAuditLog($_SESSION['user_id'],'site_delete','site',$delId,'Site gelöscht'); flashSet(__('sites_deleted')); header('Location: sites.php'); exit; } else { $error = __('error_csrf'); } } // Toggle site (POST + PRG) if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['toggle_site'])) { if ($auth->validateCsrfToken($_POST['csrf_token'] ?? '')) { $site = $db->fetchOne("SELECT is_active FROM sites WHERE id=?", [(int)$_POST['toggle_site']]); if ($site) { $db->query("UPDATE sites SET is_active=? WHERE id=?", [$site['is_active']?0:1,(int)$_POST['toggle_site']]); flashSet(__('sites_status_updated')); header('Location: sites.php'); exit; } } else { $error = __('error_csrf'); } } if (empty($success) && empty($error) && ($flash = flashGet())) { $success = $flash['message']; } $sites = $db->fetchAll("SELECT * FROM sites ORDER BY name"); $currentPage = 'sites'; ?>
= __('sites_none') ?>