Stable
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?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()]);
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* UrBackup plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
use GlpiPlugin\Urbackup\AssetTab;
|
||||
use GlpiPlugin\Urbackup\Config;
|
||||
use GlpiPlugin\Urbackup\Profile;
|
||||
use GlpiPlugin\Urbackup\ServerAsset;
|
||||
|
||||
if (!defined('GLPI_ROOT')) {
|
||||
define('GLPI_ROOT', dirname(__DIR__, 4));
|
||||
}
|
||||
|
||||
include_once GLPI_ROOT . '/inc/includes.php';
|
||||
|
||||
if (!Profile::canCurrentUser(READ)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
$itemtype = (string) ($_POST['itemtype'] ?? $_GET['itemtype'] ?? '');
|
||||
$items_id = (int) ($_POST['items_id'] ?? $_GET['items_id'] ?? 0);
|
||||
|
||||
if ($itemtype === '' || $items_id <= 0) {
|
||||
Html::displayValidationError(__('Invalid parameters'));
|
||||
}
|
||||
|
||||
if (!in_array($itemtype, Config::getEnabledItemtypes(), true)) {
|
||||
Html::displayValidationError(__('Item type not enabled for UrBackup'));
|
||||
}
|
||||
|
||||
$item = getItemForItemtype($itemtype);
|
||||
|
||||
if (!$item || !$item->getFromDB($items_id)) {
|
||||
Html::displayNotFoundError();
|
||||
}
|
||||
|
||||
if (isset($_POST['connect'])) {
|
||||
if (!Profile::canCurrentUser(UPDATE) && !Profile::canCurrentUser(CREATE)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
$server_id = (int) ($_POST['plugin_urbackup_servers_id'] ?? 0);
|
||||
|
||||
if ($server_id <= 0) {
|
||||
Html::displayValidationError(__('No server selected'));
|
||||
}
|
||||
|
||||
$link = ServerAsset::getLinkForAsset($itemtype, $items_id);
|
||||
|
||||
if ($link !== null) {
|
||||
Html::displayValidationError(__('Asset is already linked to a server'));
|
||||
}
|
||||
|
||||
$result = ServerAsset::createForAsset($itemtype, $items_id, $server_id);
|
||||
|
||||
if ($result) {
|
||||
$item->getFromDB($items_id);
|
||||
Html::redirect($item->getLinkURL());
|
||||
} else {
|
||||
Html::displayValidationError(__('Failed to link asset to server'));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['disconnect'])) {
|
||||
if (!Profile::canCurrentUser(UPDATE) && !Profile::canCurrentUser(DELETE)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
global $DB;
|
||||
|
||||
$link = ServerAsset::getLinkForAsset($itemtype, $items_id, false);
|
||||
|
||||
if ($link === null) {
|
||||
Html::displayValidationError(__('Asset is not linked to any server'));
|
||||
}
|
||||
|
||||
$result = $DB->delete('glpi_plugin_urbackup_serverassets', ['id' => (int) $link['id']]);
|
||||
|
||||
if ($result) {
|
||||
$item->getFromDB($items_id);
|
||||
Html::redirect($item->getLinkURL());
|
||||
} else {
|
||||
Html::displayValidationError(__('Failed to disconnect asset from server'));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['start_file_backup'])) {
|
||||
if (!Profile::canCurrentUser(UPDATE)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
AssetTab::startBackup($item, 'file');
|
||||
Html::redirect($item->getFormURL());
|
||||
}
|
||||
|
||||
if (isset($_POST['start_image_backup'])) {
|
||||
if (!Profile::canCurrentUser(UPDATE)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
AssetTab::startBackup($item, 'image');
|
||||
Html::redirect($item->getFormURL());
|
||||
}
|
||||
|
||||
if (isset($_POST['execute'])) {
|
||||
$action = $_POST['urbackup_action'] ?? '';
|
||||
|
||||
switch ($action) {
|
||||
case 'incremental_file_backup':
|
||||
if (Profile::canCurrentUser(UPDATE)) {
|
||||
AssetTab::startBackup($item, 'file');
|
||||
}
|
||||
break;
|
||||
case 'full_file_backup':
|
||||
if (Profile::canCurrentUser(UPDATE)) {
|
||||
$link = ServerAsset::getLinkForAsset($itemtype, $items_id, false);
|
||||
if ($link !== null) {
|
||||
$server = new \GlpiPlugin\Urbackup\Server();
|
||||
$server->getFromDB((int) $link['plugin_urbackup_servers_id']);
|
||||
$client_name = ServerAsset::getAssetName($itemtype, $items_id);
|
||||
try {
|
||||
$api = new \GlpiPlugin\Urbackup\UrbackupApiClient($server);
|
||||
$api->startFullFileBackup($client_name);
|
||||
} catch (\Throwable $e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'incremental_image_backup':
|
||||
if (Profile::canCurrentUser(UPDATE)) {
|
||||
AssetTab::startBackup($item, 'image');
|
||||
}
|
||||
break;
|
||||
case 'full_image_backup':
|
||||
if (Profile::canCurrentUser(UPDATE)) {
|
||||
$link = ServerAsset::getLinkForAsset($itemtype, $items_id, false);
|
||||
if ($link !== null) {
|
||||
$server = new \GlpiPlugin\Urbackup\Server();
|
||||
$server->getFromDB((int) $link['plugin_urbackup_servers_id']);
|
||||
$client_name = ServerAsset::getAssetName($itemtype, $items_id);
|
||||
try {
|
||||
$api = new \GlpiPlugin\Urbackup\UrbackupApiClient($server);
|
||||
$api->startFullImageBackup($client_name);
|
||||
} catch (\Throwable $e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'set_internet_mode':
|
||||
if (Profile::canCurrentUser(UPDATE)) {
|
||||
$enabled = (int) ($_POST['internet_mode'] ?? 0) === 1;
|
||||
if (!AssetTab::saveInternetMode($item, $enabled)) {
|
||||
Session::addMessageAfterRedirect(
|
||||
__('Failed to save internet mode', 'urbackup'),
|
||||
false,
|
||||
\ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'set_default_dirs':
|
||||
if (Profile::canCurrentUser(UPDATE)) {
|
||||
$dirs = (string) ($_POST['default_dirs'] ?? '');
|
||||
if (!AssetTab::saveDefaultDirs($item, $dirs)) {
|
||||
Session::addMessageAfterRedirect(
|
||||
__('Failed to save default directories', 'urbackup'),
|
||||
false,
|
||||
\ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$item->getFromDB($items_id);
|
||||
Html::redirect($item->getLinkURL());
|
||||
}
|
||||
|
||||
$item->getFromDB($items_id);
|
||||
Html::redirect($item->getLinkURL());
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use GlpiPlugin\Urbackup\Config;
|
||||
use Html;
|
||||
|
||||
global $CFG_GLPI;
|
||||
|
||||
// Check user has right to manage plugin configuration
|
||||
if (!Session::haveRight('config', UPDATE)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
// Handle form submission
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update'])) {
|
||||
Config::saveConfiguration($_POST);
|
||||
Html::redirect($_SERVER['REQUEST_URI']);
|
||||
}
|
||||
|
||||
// Display GLPI header
|
||||
Html::header(
|
||||
__('UrBackup configuration', 'urbackup'),
|
||||
'',
|
||||
'Config',
|
||||
'PluginUrbackupConfig'
|
||||
);
|
||||
|
||||
// Show configuration form
|
||||
$config = new Config();
|
||||
$config->showForm(1);
|
||||
|
||||
// Display GLPI footer
|
||||
Html::footer();
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* UrBackup plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
use GlpiPlugin\Urbackup\Profile;
|
||||
|
||||
include('../../../inc/includes.php');
|
||||
|
||||
Session::checkLoginUser();
|
||||
Session::checkRight('profile', UPDATE);
|
||||
Session::checkCSRF($_POST);
|
||||
|
||||
$profiles_id = (int) ($_POST['profiles_id'] ?? 0);
|
||||
|
||||
if ($profiles_id <= 0) {
|
||||
Session::addMessageAfterRedirect(
|
||||
__('Invalid profile.', 'urbackup'),
|
||||
true,
|
||||
ERROR
|
||||
);
|
||||
|
||||
Html::back();
|
||||
}
|
||||
|
||||
if (isset($_POST['update_urbackup_rights'])) {
|
||||
Profile::saveRights($_POST);
|
||||
|
||||
Session::addMessageAfterRedirect(
|
||||
__('UrBackup rights saved successfully.', 'urbackup'),
|
||||
true,
|
||||
INFO
|
||||
);
|
||||
}
|
||||
|
||||
global $CFG_GLPI;
|
||||
|
||||
Html::redirect($CFG_GLPI['root_doc'] . '/front/profile.form.php?id=' . $profiles_id);
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
use GlpiPlugin\Urbackup\Profile;
|
||||
use GlpiPlugin\Urbackup\Server;
|
||||
use Html;
|
||||
|
||||
if (!defined('GLPI_ROOT')) {
|
||||
define('GLPI_ROOT', dirname(__DIR__, 4));
|
||||
}
|
||||
|
||||
include_once GLPI_ROOT . "/inc/includes.php";
|
||||
|
||||
if (!Profile::canCurrentUser(UPDATE)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$id = $_POST['id'] ?? 0;
|
||||
|
||||
if ($id > 0) {
|
||||
$server->check($id, UPDATE);
|
||||
$server->update($_POST);
|
||||
} else {
|
||||
$server->check(-1, CREATE);
|
||||
$server->add($_POST);
|
||||
}
|
||||
|
||||
Html::redirect(PLUGIN_URBACKUP_WEB_DIR . "/front/server.php");
|
||||
}
|
||||
|
||||
$ID = $_GET['id'] ?? null;
|
||||
|
||||
Html::header(
|
||||
$ID ? __('Edit UrBackup server', 'urbackup') : __('Add UrBackup server', 'urbackup'),
|
||||
'',
|
||||
'Assets',
|
||||
'GlpiPlugin\Urbackup\Server'
|
||||
);
|
||||
|
||||
$server->showForm($ID);
|
||||
|
||||
if ($ID > 0 && $server->getFromDB($ID)) {
|
||||
echo '<div class="card mt-4">';
|
||||
echo '<div class="card-header">';
|
||||
echo '<h5 class="mb-0">' . htmlspecialchars(__('Linked clients', 'urbackup')) . '</h5>';
|
||||
echo '</div>';
|
||||
echo '<div class="card-body">';
|
||||
Server::showLinkedClientsTab($server);
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
|
||||
echo '<div class="card mt-4">';
|
||||
echo '<div class="card-header">';
|
||||
echo '<h5 class="mb-0">' . htmlspecialchars(__('Unlinked clients', 'urbackup')) . '</h5>';
|
||||
echo '</div>';
|
||||
echo '<div class="card-body">';
|
||||
Server::showUnlinkedClientsTab($server);
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
Html::footer();
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
use GlpiPlugin\Urbackup\Profile;
|
||||
use GlpiPlugin\Urbackup\Server;
|
||||
use Html;
|
||||
use Search;
|
||||
|
||||
if (!defined('GLPI_ROOT')) {
|
||||
define('GLPI_ROOT', dirname(__DIR__, 4));
|
||||
}
|
||||
|
||||
include_once GLPI_ROOT . "/inc/includes.php";
|
||||
|
||||
if (!Profile::canCurrentUser(READ)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
$can_read = Profile::canCurrentUser(READ);
|
||||
$can_create = Profile::canCurrentUser(CREATE);
|
||||
|
||||
Html::header(
|
||||
'UrBackup Servers',
|
||||
'',
|
||||
'Assets',
|
||||
''
|
||||
);
|
||||
|
||||
if ($can_create) {
|
||||
echo "<div class='center'>";
|
||||
echo Html::link(
|
||||
__('Add UrBackup server', 'urbackup'),
|
||||
'/plugins/urbackup/front/server.form.php',
|
||||
['class' => 'btn btn-primary']
|
||||
);
|
||||
echo "</div>";
|
||||
echo "<br>";
|
||||
}
|
||||
|
||||
Search::show('GlpiPlugin\Urbackup\Server', [
|
||||
'is_deleted' => 0,
|
||||
'massiveaction' => true,
|
||||
'start' => 0,
|
||||
'additional_actions' => [
|
||||
'view' => __('View', 'urbackup'),
|
||||
],
|
||||
]);
|
||||
|
||||
Html::footer();
|
||||
@@ -0,0 +1,64 @@
|
||||
<?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()]);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use GlpiPlugin\Urbackup\Profile;
|
||||
use GlpiPlugin\Urbackup\Server;
|
||||
use GlpiPlugin\Urbackup\ServerAsset;
|
||||
use Html;
|
||||
|
||||
if (!defined('GLPI_ROOT')) {
|
||||
define('GLPI_ROOT', dirname(__DIR__, 4));
|
||||
}
|
||||
|
||||
include_once GLPI_ROOT . "/inc/includes.php";
|
||||
|
||||
if (!Profile::canCurrentUser(READ)) {
|
||||
Html::displayRightError();
|
||||
}
|
||||
|
||||
$ID = $_GET['id'] ?? 0;
|
||||
|
||||
if ($ID <= 0) {
|
||||
Html::redirect(PLUGIN_URBACKUP_WEB_DIR . '/front/server.php');
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
if (!$server->getFromDB($ID)) {
|
||||
Html::redirect(PLUGIN_URBACKUP_WEB_DIR . '/front/server.php');
|
||||
}
|
||||
|
||||
Html::header(
|
||||
$server->fields['name'] . ' - UrBackup',
|
||||
'',
|
||||
'Assets',
|
||||
'GlpiPlugin\Urbackup\Server'
|
||||
);
|
||||
|
||||
$server->display([
|
||||
'id' => $ID,
|
||||
'show_nav' => true,
|
||||
'show_tabs' => true,
|
||||
]);
|
||||
|
||||
Html::footer();
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* AJAX endpoint for testing UrBackup API
|
||||
*/
|
||||
|
||||
$AJAX_INCLUDE = 1;
|
||||
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
Html::header_nocache();
|
||||
|
||||
Session::checkLoginUser();
|
||||
|
||||
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()]);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Test page - will work when accessed from within GLPI session
|
||||
*/
|
||||
$AJAX_INCLUDE = 1;
|
||||
|
||||
define('GLPI_ROOT', dirname(__DIR__, 4));
|
||||
require_once GLPI_ROOT . '/inc/includes.php';
|
||||
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
Html::header_nocache();
|
||||
|
||||
Session::checkLoginUser();
|
||||
|
||||
// Check rights
|
||||
if (!Session::haveRight('plugin_urbackup', READ)) {
|
||||
echo "<p style='color:red'>No READ permission on plugin_urbackup</p>";
|
||||
Html::footer();
|
||||
exit;
|
||||
}
|
||||
|
||||
echo "<p style='color:green'>READ permission OK</p>";
|
||||
|
||||
// Get server ID
|
||||
$server_id = (int) ($_GET['id'] ?? 0);
|
||||
|
||||
if ($server_id > 0) {
|
||||
$server = new GlpiPlugin\Urbackup\Server();
|
||||
if ($server->getFromDB($server_id)) {
|
||||
echo "<p>Server: " . $server->fields['name'] . "</p>";
|
||||
|
||||
$client = new GlpiPlugin\Urbackup\UrbackupApiClient($server);
|
||||
$result = $client->testConnection();
|
||||
|
||||
echo "<pre>" . print_r($result, true) . "</pre>";
|
||||
} else {
|
||||
echo "<p>Server not found</p>";
|
||||
}
|
||||
} else {
|
||||
echo "<p>No server ID provided</p>";
|
||||
}
|
||||
|
||||
Html::footer();
|
||||
Reference in New Issue
Block a user