65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Test API endpoint
|
||
|
|
* Works without GLPI session redirect
|
||
|
|
*/
|
||
|
|
|
||
|
|
define('PLUGIN_URBACKUP_DIR', __DIR__ . '/..');
|
||
|
|
define('GLPI_ROOT', dirname(__DIR__, 4));
|
||
|
|
|
||
|
|
// Load minimal GLPI
|
||
|
|
require_once GLPI_ROOT . '/inc/includes.php';
|
||
|
|
|
||
|
|
// Check session exists
|
||
|
|
if (!isset($_SESSION['glpiID']) || (int) $_SESSION['glpiID'] <= 0) {
|
||
|
|
http_response_code(401);
|
||
|
|
echo json_encode(['success' => false, 'message' => 'Unauthorized']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check rights
|
||
|
|
if (!Session::haveRight('plugin_urbackup', READ)) {
|
||
|
|
http_response_code(403);
|
||
|
|
echo json_encode(['success' => false, 'message' => 'Forbidden - No right plugin_urbackup READ']);
|
||
|
|
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()]);
|
||
|
|
}
|