diff --git a/app/classes/agent.php b/app/classes/agent.php index a205596..dd60994 100644 --- a/app/classes/agent.php +++ b/app/classes/agent.php @@ -186,18 +186,23 @@ class Agent { try { $sql = 'UPDATE jilo_agents SET + agent_type_id = :agent_type_id, url = :url, secret_key = :secret_key, check_period = :check_period WHERE id = :agent_id'; + // Convert empty secret key to NULL + $secretKey = !empty($updatedAgent['secret_key']) ? $updatedAgent['secret_key'] : null; + $query = $this->db->prepare($sql); $query->execute([ - ':agent_id' => $agent_id, - ':url' => $updatedAgent['url'], - ':secret_key' => $updatedAgent['secret_key'], - ':check_period' => $updatedAgent['check_period'], + ':agent_id' => $agent_id, + ':agent_type_id' => $updatedAgent['agent_type_id'], + ':url' => $updatedAgent['url'], + ':secret_key' => $secretKey, + ':check_period' => $updatedAgent['check_period'], ]); return true; diff --git a/app/classes/host.php b/app/classes/host.php index 7f90997..f904e95 100644 --- a/app/classes/host.php +++ b/app/classes/host.php @@ -136,17 +136,28 @@ class Host { */ public function deleteHost($host_id) { try { - $sql = 'DELETE FROM hosts - WHERE - id = :host_id'; + // Start transaction + $this->db->beginTransaction(); + // First delete all agents associated with this host + $sql = 'DELETE FROM jilo_agents WHERE host_id = :host_id'; $query = $this->db->prepare($sql); $query->bindParam(':host_id', $host_id); - $query->execute(); + + // Then delete the host + $sql = 'DELETE FROM hosts WHERE id = :host_id'; + $query = $this->db->prepare($sql); + $query->bindParam(':host_id', $host_id); + $query->execute(); + + // Commit transaction + $this->db->commit(); return true; } catch (Exception $e) { + // Rollback transaction on error + $this->db->rollBack(); return $e->getMessage(); } } diff --git a/app/pages/config.php b/app/pages/config.php index d6c1c69..560370e 100644 --- a/app/pages/config.php +++ b/app/pages/config.php @@ -28,6 +28,13 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { * Handles form submissions from editing */ + // Get hash from URL if present + $hash = parse_url($_SERVER['REQUEST_URI'], PHP_URL_FRAGMENT) ?? ''; + $redirectUrl = htmlspecialchars($app_root) . '?page=config'; + if ($hash) { + $redirectUrl .= '#' . $hash; + } + // editing the config file if (isset($_POST['item']) && $_POST['item'] === 'config_file') { // check if file is writable @@ -41,6 +48,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $_SESSION['error'] = "Editing the config file failed. Error: $result"; } } + header('Location: ' . $redirectUrl); + exit; // host operations } elseif (isset($_POST['item']) && $_POST['item'] === 'host') { @@ -79,6 +88,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $_SESSION['error'] = "Editing the host failed. Error: $result"; } } + header('Location: ' . $redirectUrl); + exit; // agent operations } elseif (isset($_POST['item']) && $_POST['item'] === 'agent') { @@ -94,8 +105,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $newAgent = [ 'type_id' => $_POST['type'], 'url' => $_POST['url'], - 'secret_key' => $_POST['secret_key'], - 'check_period' => $_POST['check_period'], + 'secret_key' => empty($_POST['secret_key']) ? null : $_POST['secret_key'], + 'check_period' => empty($_POST['check_period']) ? 0 : $_POST['check_period'], ]; $result = $agentObject->addAgent($_POST['host'], $newAgent); if ($result === true) { @@ -106,9 +117,10 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } else { // This is an edit of existing agent $agent_id = $_POST['agent']; $updatedAgent = [ - 'url' => $_POST['url'], - 'secret_key' => $_POST['secret_key'], - 'check_period' => $_POST['check_period'], + 'agent_type_id' => $_POST['agent_type_id'], + 'url' => $_POST['url'], + 'secret_key' => empty($_POST['secret_key']) ? null : $_POST['secret_key'], + 'check_period' => empty($_POST['check_period']) ? 0 : $_POST['check_period'], ]; $result = $agentObject->editAgent($agent_id, $updatedAgent); if ($result === true) { @@ -117,6 +129,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $_SESSION['error'] = "Editing the agent failed. Error: $result"; } } + header('Location: ' . $redirectUrl); + exit; // platform operations } elseif (isset($_POST['item']) && $_POST['item'] === 'platform') { @@ -155,12 +169,10 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $_SESSION['error'] = "Editing the platform failed. Error: $result"; } } + header('Location: ' . $redirectUrl); + exit; } - // After any POST operation, redirect back to the main config page - header("Location: $app_root?page=config"); - exit(); - } else { /** * Handles GET requests to display templates. @@ -182,6 +194,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { default: if ($userObject->hasRight($user_id, 'view config file')) { + $jilo_agent_types = $agentObject->getAgentTypes(); include '../app/templates/config-jilo.php'; } else { include '../app/templates/error-unauthorized.php'; diff --git a/app/templates/config-jilo.php b/app/templates/config-jilo.php index 44aa67c..3538211 100644 --- a/app/templates/config-jilo.php +++ b/app/templates/config-jilo.php @@ -50,7 +50,7 @@ Edit platform - + Save @@ -146,15 +146,15 @@ Host description - + DNS name or IP - + @@ -165,7 +165,7 @@ Edit host - + Save @@ -201,6 +201,7 @@ Agent type Endpoint URL + Secret key Check period Actions @@ -214,6 +215,15 @@ = htmlspecialchars($agent['agent_description']) ?> + + + + > + = htmlspecialchars($type['description']) ?> + + + + @@ -221,19 +231,32 @@ = htmlspecialchars($agent['url']) ?> - + - - = htmlspecialchars($agent['check_period']) ?> = ($agent['check_period'] == 1 ? 'minute' : 'minutes') ?> - - Not monitored - + = isset($agent['secret_key']) ? '••••••' : '' ?> - + + + + + + = $agent['check_period'] > 0 ? + htmlspecialchars($agent['check_period']) . ' ' . + ($agent['check_period'] == 1 ? 'minute' : 'minutes') : + 'Not monitored' ?> + + + @@ -384,12 +407,12 @@ - Secret key * - + Secret key + - Check period (minutes) * - + Check period in minutes (0 to disable) + - + - - - + Are you sure you want to delete this agent? - Agent type - + Agent type + + + + Endpoint URL + @@ -518,6 +543,38 @@