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'; }