jilo-web/app/pages/agents.php

65 lines
2.0 KiB
PHP
Raw Normal View History

2024-09-04 22:06:38 +00:00
<?php
2024-12-04 10:17:30 +00:00
/**
* Agent cache management
*
* This page ("agents") handles caching for agents. It allows storing, clearing, and retrieving
* agent-related data in the session using AJAX requests. The cache is stored with a timestamp
* to allow time-based invalidation if needed.
*/
2024-09-04 22:06:38 +00:00
$action = $_REQUEST['action'] ?? '';
$agent = $_REQUEST['agent'] ?? '';
require '../app/classes/agent.php';
$agentObject = new Agent($dbWeb);
2024-10-02 13:35:10 +00:00
// if it's a POST request, it's saving to cache
2024-09-04 22:06:38 +00:00
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
2024-10-02 13:35:10 +00:00
// read the JSON sent from javascript
$data = file_get_contents("php://input");
$result = json_decode($data, true);
2024-09-04 22:06:38 +00:00
2024-10-02 13:35:10 +00:00
// store the data in the session
if ($result) {
$_SESSION["agent{$agent}_cache"] = $result;
$_SESSION["agent{$agent}_cache_time"] = time(); // store the cache time
2024-10-03 07:59:32 +00:00
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."
]);
2024-09-29 07:07:04 +00:00
} else {
2024-10-03 07:59:32 +00:00
echo json_encode([
'status' => 'error',
'message' => 'Invalid data'
]);
2024-09-29 07:07:04 +00:00
}
2024-10-02 13:35:10 +00:00
//// 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']);
// }
2024-09-04 22:06:38 +00:00
// no form submitted, show the templates
} else {
2024-09-23 18:39:32 +00:00
$agentDetails = $agentObject->getAgentDetails($platform_id);
include '../app/templates/agent-list.php';
2024-09-04 22:06:38 +00:00
}
?>