diff --git a/app/pages/config.php b/app/pages/config.php index 777bdff..ca3303b 100644 --- a/app/pages/config.php +++ b/app/pages/config.php @@ -25,7 +25,7 @@ $agentObject = new Agent($dbWeb); if ($_SERVER['REQUEST_METHOD'] == 'POST') { /** - * Handles form submissions from editing page + * Handles form submissions from editing */ // editing the config file @@ -42,200 +42,126 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } } - // new host adding - } elseif (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'host') { - $newHost = [ - 'address' => $address, - 'port' => $port, - 'platform_id' => $platform_id, - 'name' => $name, - ]; - $result = $hostObject->addHost($newHost); - if ($result === true) { - $_SESSION['notice'] = "New Jilo host added."; - } else { - $_SESSION['error'] = "Adding the host failed. Error: $result"; - } - - // new agent adding - } elseif (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'agent') { - $newAgent = [ - 'type_id' => $type, - 'url' => $url, - 'secret_key' => $secret_key, - 'check_period' => $check_period, - ]; - $result = $agentObject->addAgent($platform_id, $newAgent); - if ($result === true) { - $_SESSION['notice'] = "New Jilo Agent added."; - } else { - $_SESSION['error'] = "Adding the agent failed. Error: $result"; - } - - // new platform adding - } elseif (isset($_POST['new']) && $_POST['new'] === 'true') { - $newPlatform = [ - 'name' => $name, - 'jitsi_url' => $_POST['jitsi_url'], - 'jilo_database' => $_POST['jilo_database'], - ]; - $result = $platformObject->addPlatform($newPlatform); - if ($result === true) { - $_SESSION['notice'] = "New Jitsi platform added."; - } else { - $_SESSION['error'] = "Adding the platform failed. Error: $result"; - } - - // deleting a host - } elseif (isset($_POST['delete']) && isset($_POST['host']) && $_POST['delete'] === 'true') { - $result = $hostObject->deleteHost($host); - if ($result === true) { - $_SESSION['notice'] = "Host id \"{$_REQUEST['host']}\" deleted."; - } else { - $_SESSION['error'] = "Deleting the host failed. Error: $result"; - } - - // deleting an agent - } elseif (isset($_POST['delete']) && isset($_POST['agent']) && $_POST['delete'] === 'true') { - $result = $agentObject->deleteAgent($agent); - if ($result === true) { - $_SESSION['notice'] = "Agent id \"{$_REQUEST['agent']}\" deleted."; - } else { - $_SESSION['error'] = "Deleting the agent failed. Error: $result"; - } - - // deleting a platform - } elseif (isset($_POST['delete']) && $_POST['delete'] === 'true') { - $platform = $_POST['platform']; - $result = $platformObject->deletePlatform($platform); - if ($result === true) { - $_SESSION['notice'] = "Platform \"{$platformObject['name']}\" added."; - } else { - $_SESSION['error'] = "Adding the platform failed. Error: $result"; - } - - // an update to an existing host + // host operations } elseif (isset($_POST['item']) && $_POST['item'] === 'host') { - $host_id = $_POST['host']; - $platform_id = $_POST['platform']; - $updatedHost = [ - 'id' => $host_id, - 'address' => $_POST['address'], - 'name' => $_POST['name'] - ]; - $result = $hostObject->editHost($platform_id, $updatedHost); - - // Check if it's an AJAX request - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && - strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { - - // Clear any output buffers to ensure clean JSON - while (ob_get_level()) ob_end_clean(); - - header('Content-Type: application/json'); + if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is a host delete + $host_id = $_POST['host']; + $result = $hostObject->deleteHost($host_id); if ($result === true) { - echo json_encode(['success' => true]); + $_SESSION['notice'] = "Host deleted successfully."; } else { - echo json_encode([ - 'success' => false, - 'message' => "Editing the host failed. Error: $result" - ]); + $_SESSION['error'] = "Deleting the host failed. Error: $result"; } - exit(); - } else { - // Regular form submission + } else if (!isset($_POST['host'])) { // This is a new host + $newHost = [ + 'address' => $_POST['address'], + 'platform_id' => $_POST['platform'], + 'name' => $_POST['name'], + ]; + $result = $hostObject->addHost($newHost); + if ($result === true) { + $_SESSION['notice'] = "New Jilo host added."; + } else { + $_SESSION['error'] = "Adding the host failed. Error: $result"; + } + } else { // This is an edit of existing host + $host_id = $_POST['host']; + $platform_id = $_POST['platform']; + $updatedHost = [ + 'id' => $host_id, + 'address' => $_POST['address'], + 'name' => $_POST['name'], + ]; + $result = $hostObject->editHost($platform_id, $updatedHost); if ($result === true) { $_SESSION['notice'] = "Host edited."; } else { $_SESSION['error'] = "Editing the host failed. Error: $result"; } - header("Location: $app_root?page=config&item=$item"); - exit(); } - // an update to an existing agent + // agent operations } elseif (isset($_POST['item']) && $_POST['item'] === 'agent') { - $agent_id = $_POST['agent']; - $platform_id = $_POST['platform']; - $updatedAgent = [ - 'id' => $agent_id, - 'agent_type_id' => $_POST['agent_type_id'], - 'url' => $_POST['url'], - 'secret_key' => $_POST['secret_key'], - 'check_period' => $_POST['check_period'] - ]; - $result = $agentObject->editAgent($platform_id, $updatedAgent); - - // Check if it's an AJAX request - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && - strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { - - // Clear any output buffers to ensure clean JSON - while (ob_get_level()) ob_end_clean(); - - header('Content-Type: application/json'); + if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is an agent delete + $agent_id = $_POST['agent']; + $result = $agentObject->deleteAgent($agent_id); if ($result === true) { - echo json_encode(['success' => true]); + $_SESSION['notice'] = "Agent deleted successfully."; } else { - echo json_encode([ - 'success' => false, - 'message' => "Editing the agent failed. Error: $result" - ]); + $_SESSION['error'] = "Deleting the agent failed. Error: $result"; } - exit(); - } else { - // Regular form submission + } else if (isset($_POST['new']) && $_POST['new'] === 'true') { // This is a new agent + $newAgent = [ + 'type_id' => $_POST['type'], + 'url' => $_POST['url'], + 'secret_key' => $_POST['secret_key'], + 'check_period' => $_POST['check_period'], + ]; + $result = $agentObject->addAgent($_POST['platform'], $newAgent); + if ($result === true) { + $_SESSION['notice'] = "New Jilo Agent added."; + } else { + $_SESSION['error'] = "Adding the agent failed. Error: $result"; + } + } else { // This is an edit of existing agent + $agent_id = $_POST['agent']; + $platform_id = $_POST['platform']; + $updatedAgent = [ + 'id' => $agent_id, + 'agent_type_id' => $_POST['agent_type_id'], + 'url' => $_POST['url'], + 'secret_key' => $_POST['secret_key'], + 'check_period' => $_POST['check_period'] + ]; + $result = $agentObject->editAgent($platform_id, $updatedAgent); if ($result === true) { $_SESSION['notice'] = "Agent edited."; } else { $_SESSION['error'] = "Editing the agent failed. Error: $result"; } - header("Location: $app_root?page=config&item=$item"); - exit(); } - // an update to an existing platform - } else { - $platform = $_POST['platform_id']; - $updatedPlatform = [ - 'name' => $_POST['name'], - 'jitsi_url' => $_POST['jitsi_url'], - 'jilo_database' => $_POST['jilo_database'] - ]; - $result = $platformObject->editPlatform($platform, $updatedPlatform); - - // Check if it's an AJAX request - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && - strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { - - // Clear any output buffers to ensure clean JSON - while (ob_get_level()) ob_end_clean(); - - header('Content-Type: application/json'); + // platform operations + } elseif (isset($_POST['item']) && $_POST['item'] === 'platform') { + if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is a platform delete + $platform_id = $_POST['platform']; + $result = $platformObject->deletePlatform($platform_id); if ($result === true) { - echo json_encode(['success' => true]); + $_SESSION['notice'] = "Platform deleted successfully."; } else { - echo json_encode([ - 'success' => false, - 'message' => "Editing the platform failed. Error: $result" - ]); + $_SESSION['error'] = "Deleting the platform failed. Error: $result"; } - exit(); - } else { - // Regular form submission + } else if (!isset($_POST['platform'])) { // This is a new platform + $newPlatform = [ + 'name' => $_POST['name'], + 'jitsi_url' => $_POST['jitsi_url'], + 'jilo_database' => $_POST['jilo_database'], + ]; + $result = $platformObject->addPlatform($newPlatform); + if ($result === true) { + $_SESSION['notice'] = "New Jitsi platform added."; + } else { + $_SESSION['error'] = "Adding the platform failed. Error: $result"; + } + } else { // This is an edit of existing platform + $platform_id = $_POST['platform']; + $updatedPlatform = [ + 'id' => $platform_id, + 'name' => $_POST['name'], + 'jitsi_url' => $_POST['jitsi_url'], + 'jilo_database' => $_POST['jilo_database'], + ]; + $result = $platformObject->editPlatform($updatedPlatform); if ($result === true) { $_SESSION['notice'] = "Platform edited."; } else { $_SESSION['error'] = "Editing the platform failed. Error: $result"; } - header("Location: $app_root?page=config&item=$item"); - exit(); } } -// FIXME the new file is not loaded on first page load - unset($config); - header("Location: $app_root?page=config&item=$item"); + // After any POST operation, redirect back to the main config page + header("Location: $app_root?page=config"); exit(); } else { @@ -245,76 +171,6 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { switch ($item) { - case 'platform': - if (isset($action) && $action === 'add') { - include '../app/templates/config-platform-add.php'; - } elseif (isset($action) && $action === 'edit') { - include '../app/templates/config-platform-edit.php'; - } elseif (isset($action) && $action === 'delete') { - include '../app/templates/config-platform-delete.php'; - } else { - if ($userObject->hasRight($user_id, 'view config file')) { - include '../app/templates/config-platform.php'; - } else { - include '../app/templates/error-unauthorized.php'; - } - } - break; - - case 'host': - if (isset($action) && $action === 'add') { - include '../app/templates/config-host-add.php'; - } elseif (isset($action) && $action === 'edit') { - $hostDetails = $hostObject->getHostDetails($platform_id, $agent); - include '../app/templates/config-host-edit.php'; - } elseif (isset($action) && $action === 'delete') { - $hostDetails = $hostObject->getHostDetails($platform_id, $agent); - include '../app/templates/config-host-delete.php'; - } else { - if ($userObject->hasRight($user_id, 'view config file')) { - $hostDetails = $hostObject->getHostDetails(); - include '../app/templates/config-host.php'; - } else { - include '../app/templates/error-unauthorized.php'; - } - } - break; - - case 'agent': - if (isset($action) && $action === 'add') { - $jilo_agent_types = $agentObject->getAgentTypes(); - $platform_id = $_REQUEST['platform'] ?? ''; - if (!empty($platform_id)) { - $jilo_agents_in_platform = $agentObject->getPlatformAgentTypes($platform_id); - $jilo_agent_types_in_platform = array_column($jilo_agents_in_platform, 'agent_type_id'); - include '../app/templates/config-agent-add.php'; - } else { - $_SESSION['error'] = "Platform ID is required to add an agent."; - header("Location: $app_root?page=config&item=agent"); - exit(); - } - } elseif (isset($action) && $action === 'edit') { - if (isset($_REQUEST['agent'])) { - $platform_id = $_REQUEST['platform'] ?? ''; - $agentDetails = $agentObject->getAgentDetails($platform_id, $agent)['0']; - $jilo_agent_types = $agentObject->getAgentTypes(); - include '../app/templates/config-agent-edit.php'; - } - } elseif (isset($action) && $action === 'delete') { - if (isset($_REQUEST['agent'])) { - $platform_id = $_REQUEST['platform'] ?? ''; - $agentDetails = $agentObject->getAgentDetails($platform_id, $agent)['0']; - include '../app/templates/config-agent-delete.php'; - } - } else { - if ($userObject->hasRight($user_id, 'view config file')) { - include '../app/templates/config-agent.php'; - } else { - include '../app/templates/error-unauthorized.php'; - } - } - break; - case 'config_file': if (isset($action) && $action === 'edit') { include '../app/templates/config-configfile-edit.php'; @@ -328,9 +184,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { break; default: - // the default config page is the platforms page - header("Location: $app_root?page=config&item=platform"); - exit(); + if ($userObject->hasRight($user_id, 'view config file')) { + include '../app/templates/config-jilo.php'; + } else { + include '../app/templates/error-unauthorized.php'; + } } } diff --git a/app/templates/config-add-agent.php b/app/templates/config-add-agent.php deleted file mode 100644 index 43578b7..0000000 --- a/app/templates/config-add-agent.php +++ /dev/null @@ -1,72 +0,0 @@ - - -
Add new Jilo Agent to Jitsi platform "= htmlspecialchars($platformDetails[0]['name']) ?>"
-Add new Jilo agent
-Delete Jilo agent
-Are you sure you want to delete this agent?
- -Edit Jilo agent
-Jilo Agent configuration for Jitsi platform "= htmlspecialchars($platformDetails[0]['name']) ?>"
-delete an agent:
- -Jilo Agent configuration for Jitsi platform "= htmlspecialchars($platformDetails[0]['name']) ?>"
-edit the agent details:
- -Add new host in Jitsi platform = htmlspecialchars($platformDetails[0]['name']) ?>
-Jilo configuration for Jitsi platform "= htmlspecialchars($platformDetails[0]['name']) ?>"
-delete a host:
- -Agent Type | +Agent type | Endpoint URL | Check period (minutes) | Actions | @@ -215,7 +208,7 @@
---|---|---|---|---|
@@ -244,7 +237,7 @@
-
+
@@ -277,10 +270,9 @@
+
- |