mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
🔄Sync hub 2.0
This commit is contained in:
@@ -3,8 +3,11 @@
|
||||
// External files
|
||||
require '/app/front/php/server/init.php';
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// ----------------------------------------------
|
||||
// Method to check authorization
|
||||
function checkAuthorization($method) {
|
||||
// Retrieve the authorization header
|
||||
$headers = apache_request_headers();
|
||||
$auth_header = $headers['Authorization'] ?? '';
|
||||
@@ -14,16 +17,56 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if ($auth_header !== $expected_token) {
|
||||
http_response_code(403);
|
||||
echo 'Forbidden';
|
||||
write_notification("[Plugin: SYNC] Incoming data: Incorrect API Token", "alert");
|
||||
write_notification("[Plugin: SYNC] Incoming data: Incorrect API Token (".$method.")", "alert");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------
|
||||
// Function to return JSON response
|
||||
function jsonResponse($status, $data = [], $message = '') {
|
||||
http_response_code($status);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode([
|
||||
'node_name' => getSettingValue('SYNC_node_name'),
|
||||
'status' => $status,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
'timestamp' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
}
|
||||
|
||||
// ----------------------------------------------
|
||||
// MAIN
|
||||
// ----------------------------------------------
|
||||
|
||||
|
||||
// requesting data (this is a NODE)
|
||||
if ($method === 'GET') {
|
||||
checkAuthorization($method);
|
||||
|
||||
$file_path = "/app/front/api/table_devices.json";
|
||||
|
||||
$data = file_get_contents($file_path);
|
||||
|
||||
// Prepare the data to return as a JSON response
|
||||
$response_data = [
|
||||
'data_base64' => base64_encode($data),
|
||||
];
|
||||
|
||||
// Return JSON response
|
||||
jsonResponse(200, $response_data, 'OK');
|
||||
|
||||
}
|
||||
// receiving data (this is a HUB)
|
||||
else if ($method === 'POST') {
|
||||
checkAuthorization($method);
|
||||
|
||||
// Retrieve and decode the data from the POST request
|
||||
$data = $_POST['data'] ?? '';
|
||||
$plugin_folder = $_POST['plugin_folder'] ?? '';
|
||||
$node_name = $_POST['node_name'] ?? '';
|
||||
|
||||
|
||||
$storage_path = "/app/front/plugins/{$plugin_folder}";
|
||||
|
||||
// Create the storage directory if it doesn't exist
|
||||
@@ -43,12 +86,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
$file_path = "{$storage_path}/last_result.encoded.{$node_name}.{$file_count}.log";
|
||||
|
||||
|
||||
// Save the decoded data to the file
|
||||
file_put_contents($file_path, $data);
|
||||
http_response_code(200);
|
||||
echo 'Data received and stored successfully';
|
||||
write_notification("[Plugin: SYNC] Data received ({$plugin_folder})", "info");
|
||||
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo 'Method Not Allowed';
|
||||
|
||||
Reference in New Issue
Block a user