60 lines
1.4 KiB
PHP
60 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* AJAX endpoint for API test
|
||
|
|
* Uses standard GLPI front page pattern
|
||
|
|
*/
|
||
|
|
|
||
|
|
require_once(__DIR__ . '/_check_webserver_config.php');
|
||
|
|
|
||
|
|
global $CFG_GLPI;
|
||
|
|
|
||
|
|
header("Content-Type: application/json; charset=UTF-8");
|
||
|
|
|
||
|
|
// Check login
|
||
|
|
Session::checkLoginUser();
|
||
|
|
|
||
|
|
// Check rights
|
||
|
|
if (!Session::haveRight('plugin_urbackup', READ)) {
|
||
|
|
echo json_encode(['success' => false, 'message' => 'No permission']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
$server_id = (int) (($_GET['id'] ?? $_POST['id'] ?? 0));
|
||
|
|
|
||
|
|
if ($server_id <= 0) {
|
||
|
|
echo json_encode(['success' => false, 'message' => 'Invalid server ID']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Load plugin classes
|
||
|
|
$classes = ['Server', 'UrbackupApiClient'];
|
||
|
|
foreach ($classes as $class) {
|
||
|
|
$file = PLUGIN_URBACKUP_DIR . '/src/' . $class . '.php';
|
||
|
|
if (file_exists($file)) {
|
||
|
|
require_once $file;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$server = new \GlpiPlugin\Urbackup\Server();
|
||
|
|
|
||
|
|
if (!$server->getFromDB($server_id)) {
|
||
|
|
echo json_encode(['success' => false, 'message' => 'Server not found']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$client = new \GlpiPlugin\Urbackup\UrbackupApiClient($server);
|
||
|
|
$result = $client->testConnection();
|
||
|
|
|
||
|
|
$server->update([
|
||
|
|
'id' => $server_id,
|
||
|
|
'last_api_status' => $result['success'] ? 1 : 0,
|
||
|
|
'last_api_message' => $result['message'],
|
||
|
|
'last_api_check' => date('Y-m-d H:i:s'),
|
||
|
|
]);
|
||
|
|
|
||
|
|
echo json_encode($result);
|
||
|
|
} catch (Throwable $e) {
|
||
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
||
|
|
}
|