diff --git a/app/pages/agents.php b/app/pages/agents.php index a8d6262..be45988 100644 --- a/app/pages/agents.php +++ b/app/pages/agents.php @@ -8,61 +8,163 @@ * to allow time-based invalidation if needed. */ -// Get any new messages -include '../app/includes/messages.php'; -include '../app/includes/messages-show.php'; +// Constants for session keys and cache settings +define('SESSION_CACHE_SUFFIX', '_cache'); +define('SESSION_CACHE_TIME_SUFFIX', '_cache_time'); +define('CACHE_EXPIRY_TIME', 3600); // 1 hour in seconds + +// Input validation +$action = isset($_GET['action']) ? htmlspecialchars(trim($_GET['action']), ENT_QUOTES, 'UTF-8') : ''; +$agentId = filter_input(INPUT_GET, 'agent', FILTER_VALIDATE_INT); -$action = $_REQUEST['action'] ?? ''; -$agent = $_REQUEST['agent'] ?? ''; require '../app/classes/agent.php'; - +require '../app/classes/host.php'; $agentObject = new Agent($dbWeb); +$hostObject = new Host($dbWeb); -// if it's a POST request, it's saving to cache -if ($_SERVER['REQUEST_METHOD'] == 'POST') { - - // read the JSON sent from javascript - $data = file_get_contents("php://input"); - $result = json_decode($data, true); - - // store the data in the session - if ($result) { - $_SESSION["agent{$agent}_cache"] = $result; - $_SESSION["agent{$agent}_cache_time"] = time(); // store the cache time - echo json_encode([ - 'status' => 'success', - 'message' => "Cache for agent {$agent} is stored." - ]); - } elseif ($result === null && !empty($agent)) { - unset($_SESSION["agent{$agent}_cache"]); - unset($_SESSION["agent{$agent}_cache_time"]); - echo json_encode([ - 'status' => 'success', - 'message' => "Cache for agent {$agent} is cleared." - ]); - } else { - echo json_encode([ - 'status' => 'error', - 'message' => 'Invalid data' - ]); - } - -//// if it's a GET request, it's read/load from cache -//} elseif ($loadcache === true) { -// -// // check if cached data exists in session -// if (isset($_SESSION["agent{$agent}_cache"])) { -// // return the cached data in JSON format -// echo json_encode(['status' => 'success', 'data' => $_SESSION["agent{$agent}_cache"]]); -// } else { -// // if no cached data exists -// echo json_encode(['status' => 'error', 'message' => 'No cached data found']); -// } - -// no form submitted, show the templates -} else { - $agentDetails = $agentObject->getAgentDetails($platform_id); - include '../app/templates/agent-list.php'; +/** + * Get the cache key for an agent + * @param int $agentId The agent ID + * @param string $suffix The suffix to append (_cache or _cache_time) + * @return string The cache key + */ +function getAgentCacheKey($agentId, $suffix) { + return "agent{$agentId}{$suffix}"; } -?> +/** + * Check if cache is expired + * @param int $agentId The agent ID + * @return bool True if cache is expired or doesn't exist + */ +function isCacheExpired($agentId) { + $timeKey = getAgentCacheKey($agentId, SESSION_CACHE_TIME_SUFFIX); + if (!isset($_SESSION[$timeKey])) { + return true; + } + return (time() - $_SESSION[$timeKey]) > CACHE_EXPIRY_TIME; +} + +// Handle POST request (saving to cache) +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + // Validate agent ID for POST operations + if ($agentId === false || $agentId === null) { + Messages::flash('ERROR', 'DEFAULT', 'Invalid agent ID format'); + echo json_encode(['status' => 'error', 'message' => 'Invalid agent ID format']); + exit; + } + + // Read and validate JSON data + $jsonData = file_get_contents("php://input"); + if ($jsonData === false) { + Messages::flash('ERROR', 'DEFAULT', 'Failed to read input data'); + echo json_encode(['status' => 'error', 'message' => 'Failed to read input data']); + exit; + } + + $data = json_decode($jsonData, true); + + // Handle cache clearing + if ($data === null && !empty($agentId)) { + $cacheKey = getAgentCacheKey($agentId, SESSION_CACHE_SUFFIX); + $timeKey = getAgentCacheKey($agentId, SESSION_CACHE_TIME_SUFFIX); + + unset($_SESSION[$cacheKey]); + unset($_SESSION[$timeKey]); + + Messages::flash('SUCCESS', 'DEFAULT', "Cache for agent {$agentId} is cleared."); + echo json_encode([ + 'status' => 'success', + 'message' => "Cache for agent {$agentId} is cleared." + ]); + } + // Handle cache storing + elseif ($data) { + $cacheKey = getAgentCacheKey($agentId, SESSION_CACHE_SUFFIX); + $timeKey = getAgentCacheKey($agentId, SESSION_CACHE_TIME_SUFFIX); + + $_SESSION[$cacheKey] = $data; + $_SESSION[$timeKey] = time(); + + Messages::flash('SUCCESS', 'DEFAULT', "Cache for agent {$agentId} is stored."); + echo json_encode([ + 'status' => 'success', + 'message' => "Cache for agent {$agentId} is stored." + ]); + } + else { + Messages::flash('ERROR', 'DEFAULT', 'Invalid data format'); + echo json_encode(['status' => 'error', 'message' => 'Invalid data format']); + } + +// Handle AJAX requests +} elseif (isset($_GET['action'])) { + $action = $_GET['action']; + $agentId = filter_input(INPUT_GET, 'agent', FILTER_VALIDATE_INT); + + if ($action === 'fetch') { + $response = ['status' => 'success', 'data' => $data]; + echo json_encode($response); + exit; + } + + if ($action === 'status') { + $response = ['status' => 'success', 'data' => $statusData]; + echo json_encode($response); + exit; + } + +// Handle template display +} else { + + // Validate platform_id is set + if (!isset($platform_id)) { + Messages::flash('ERROR', 'DEFAULT', 'Platform ID is not set'); + } + + // Get host details for this platform + $hostDetails = $hostObject->getHostDetails($platform_id); + + // Group agents by host + $agentsByHost = []; + foreach ($hostDetails as $host) { + $hostId = $host['id']; + $agentsByHost[$hostId] = [ + 'host_name' => $host['name'], + 'agents' => [] + ]; + + // Get agents for this host + $hostAgents = $agentObject->getAgentDetails($hostId); + if ($hostAgents) { + $agentsByHost[$hostId]['agents'] = $hostAgents; + } + + // Generate JWT tokens for each agent beforehand + $agentTokens = []; + foreach ($agentsByHost[$hostId]['agents'] as $agent) { + $payload = [ + 'iss' => 'Jilo Web', + 'aud' => $config['domain'], + 'iat' => time(), + 'exp' => time() + 3600, + 'agent_id' => $agent['id'] + ]; + $agentTokens[$agent['id']] = $agentObject->generateAgentToken($payload, $agent['secret_key']); + } + + /** + * Now we have: + * $hostDetails - hosts in this platform + * $agentsByHost[$hostId]['agents'] - agents details by hostId + * $agentTokens[$agent['id']] - tokens for the agentsIds + */ + } + + // Get any new messages + include '../app/includes/messages.php'; + include '../app/includes/messages-show.php'; + + // Load the template + include '../app/templates/agents.php'; +} diff --git a/app/templates/agent-list.php b/app/templates/agent-list.php deleted file mode 100644 index 0b6be35..0000000 --- a/app/templates/agent-list.php +++ /dev/null @@ -1,39 +0,0 @@ - - -
-

Jilo Agents on platform ()

-
- -

- agent id: - agent type: () -
- endpoint: -
- 'Jilo Web', - 'aud' => $config['domain'], - 'iat' => time(), - 'exp' => time() + 3600, - 'agent_id' => $agent['id'] - ]; - $jwt = $agentObject->generateAgentToken($payload, $agent['secret_key']); -// print_r($_SESSION); -?> - - - - - - - - - - - - - -

-
click a button to display data from the agent.
- diff --git a/app/templates/agents.php b/app/templates/agents.php new file mode 100644 index 0000000..cb83728 --- /dev/null +++ b/app/templates/agents.php @@ -0,0 +1,94 @@ + + +
+
+
+

Jilo Agents on platform ()

+ Manage and monitor agents on this platform. +
+
+ + +
+ $hostData): ?> +
+
+
+
+ + Host: + + + +
+
+
+ +

No agents on this host.

+ + +
+
+
+ + Agent ID: | + Type: () | + Endpoint: + + + +
+
+ +
+ + + + +
+ "> +
Click a button to display data from the agent.
+
+ + +
+
+
+ +
+
+ diff --git a/app/templates/page-sidebar.php b/app/templates/page-sidebar.php index 92f7ce9..207052e 100644 --- a/app/templates/page-sidebar.php +++ b/app/templates/page-sidebar.php @@ -61,7 +61,7 @@ $timeNow = new DateTime('now', new DateTimeZone($userTimezone));
  • - jilo agents + jilo agents
  • diff --git a/app/templates/status-agent.php b/app/templates/status-agent.php index eac4a91..a515ab8 100644 --- a/app/templates/status-agent.php +++ b/app/templates/status-agent.php @@ -2,6 +2,7 @@