Files
GLPI-Plugin-CVE-Prototype/ajax/sync_now.php
2025-05-31 10:03:48 +02:00

66 lines
1.9 KiB
PHP

<?php
/**
* GLPI CVE Plugin - Immediate Sync Handler
* This file handles immediate synchronization requests
*/
include ("../../../inc/includes.php");
// Check CSRF token
Session::checkLoginUser();
Session::checkRight("config", UPDATE);
// Force no time limit for long-running operations
set_time_limit(0);
// Start synchronization of all active sources
$source = new PluginCveCveSource();
$sources = $source->find(['is_active' => 1]);
$success = true;
$count = 0;
foreach ($sources as $data) {
$source->getFromDB($data['id']);
// Log the start of sync
Toolbox::logInFile('cve_plugin', sprintf('Starting synchronization of source: %s (ID: %d)',
$data['name'], $data['id']));
// Update source status to in progress
$source->update([
'id' => $data['id'],
'sync_status' => 'IN_PROGRESS',
]);
try {
$result = $source->syncNow($data['id']);
$count += $result ? 1 : 0;
$success = $success && $result;
// Log the result
Toolbox::logInFile('cve_plugin', sprintf('Sync of source %s %s',
$data['name'], ($result ? 'succeeded' : 'failed')));
} catch (Exception $e) {
$success = false;
// Log the error
Toolbox::logInFile('cve_plugin', sprintf('Error during sync of source %s: %s',
$data['name'], $e->getMessage()), true);
// Update source status to failed
$source->update([
'id' => $data['id'],
'sync_status' => 'FAILED',
'last_sync' => $_SESSION['glpi_currenttime']
]);
}
}
// Log the overall result
if ($count > 0) {
Toolbox::logInFile('cve_plugin', sprintf('CVE synchronization completed: %d sources processed with %s',
$count, ($success ? 'success' : 'some failures')));
}
// No HTML output needed for background task