58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* AJAX endpoint for testing UrBackup API
|
||
|
|
*/
|
||
|
|
|
||
|
|
$AJAX_INCLUDE = 1;
|
||
|
|
|
||
|
|
if (!defined('GLPI_ROOT')) {
|
||
|
|
define('GLPI_ROOT', dirname(__DIR__, 3));
|
||
|
|
}
|
||
|
|
|
||
|
|
require_once GLPI_ROOT . "/inc/includes.php";
|
||
|
|
|
||
|
|
header("Content-Type: application/json; charset=UTF-8");
|
||
|
|
Html::header_nocache();
|
||
|
|
|
||
|
|
Session::checkLoginUser();
|
||
|
|
|
||
|
|
// Allow AJAX requests without CSRF token (internal plugin calls)
|
||
|
|
if (!Session::getCSRFToken()) {
|
||
|
|
// Allow for AJAX calls
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!Session::haveRight('plugin_urbackup', READ)) {
|
||
|
|
echo json_encode(['success' => false, 'message' => 'No permission']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
$server_id = (int) ($_POST['id'] ?? $_GET['id'] ?? 0);
|
||
|
|
|
||
|
|
if ($server_id <= 0) {
|
||
|
|
echo json_encode(['success' => false, 'message' => 'Invalid server ID']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
$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()]);
|
||
|
|
}
|