291 lines
7.6 KiB
PHP
291 lines
7.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* -------------------------------------------------------------------------
|
|
* UrBackup plugin for GLPI
|
|
* -------------------------------------------------------------------------
|
|
*/
|
|
|
|
namespace GlpiPlugin\Urbackup;
|
|
|
|
use CommonDBTM;
|
|
use CommonGLPI;
|
|
use Html;
|
|
use Session;
|
|
|
|
class ServerAsset extends CommonDBTM
|
|
{
|
|
public static $rightname = 'plugin_urbackup';
|
|
|
|
/**
|
|
* Get table name.
|
|
*
|
|
* @param string|null $classname Class name
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function getTable($classname = null): string
|
|
{
|
|
return 'glpi_plugin_urbackup_serverassets';
|
|
}
|
|
|
|
/**
|
|
* Get type name.
|
|
*
|
|
* @param int $nb Number
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function getTypeName($nb = 0): string
|
|
{
|
|
return _n('UrBackup linked asset', 'UrBackup linked assets', $nb, 'urbackup');
|
|
}
|
|
|
|
/**
|
|
* Connect asset to server.
|
|
*
|
|
* @param string $itemtype Itemtype
|
|
* @param int $items_id Item ID
|
|
* @param int $server_id Server ID
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function connectAssetToServer(string $itemtype, int $items_id, int $server_id): bool
|
|
{
|
|
global $DB;
|
|
|
|
if (!Profile::canCurrentUser(UPDATE) && !Profile::canCurrentUser(CREATE)) {
|
|
return false;
|
|
}
|
|
|
|
if (!Config::isItemtypeEnabled($itemtype)) {
|
|
return false;
|
|
}
|
|
|
|
if ($items_id <= 0 || $server_id <= 0) {
|
|
return false;
|
|
}
|
|
|
|
if (!class_exists($itemtype)) {
|
|
return false;
|
|
}
|
|
|
|
$item = new $itemtype();
|
|
|
|
if (!$item instanceof CommonDBTM || !$item->getFromDB($items_id)) {
|
|
return false;
|
|
}
|
|
|
|
$table = self::getTable();
|
|
$date = $_SESSION['glpi_currenttime'] ?? date('Y-m-d H:i:s');
|
|
|
|
$existing = self::getLinkForAsset($itemtype, $items_id, false);
|
|
|
|
if ($existing !== null) {
|
|
return $DB->update(
|
|
$table,
|
|
[
|
|
'plugin_urbackup_servers_id' => $server_id,
|
|
'client_name' => (string) ($item->fields['name'] ?? ''),
|
|
'client_ip' => self::extractAssetIp($item),
|
|
'is_active' => 1,
|
|
'date_mod' => $date,
|
|
],
|
|
[
|
|
'id' => (int) $existing['id'],
|
|
]
|
|
);
|
|
}
|
|
|
|
return $DB->insert(
|
|
$table,
|
|
[
|
|
'plugin_urbackup_servers_id' => $server_id,
|
|
'itemtype' => $itemtype,
|
|
'items_id' => $items_id,
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Disconnect asset from server.
|
|
*
|
|
* @param string $itemtype Itemtype
|
|
* @param int $items_id Item ID
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function disconnectAsset(string $itemtype, int $items_id): bool
|
|
{
|
|
global $DB;
|
|
|
|
if (!Profile::canCurrentUser(UPDATE)) {
|
|
return false;
|
|
}
|
|
|
|
$link = self::getLinkForAsset($itemtype, $items_id, true);
|
|
|
|
if ($link === null) {
|
|
return true;
|
|
}
|
|
|
|
return $DB->delete(
|
|
self::getTable(),
|
|
[
|
|
'id' => (int) $link['id'],
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get active link for asset.
|
|
*
|
|
* @param string $itemtype Itemtype
|
|
* @param int $items_id Item ID
|
|
* @param bool $active_only Active only
|
|
*
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public static function createForAsset(
|
|
string $itemtype,
|
|
int $items_id,
|
|
int $servers_id
|
|
): bool {
|
|
global $DB;
|
|
|
|
$item = getItemForItemtype($itemtype);
|
|
if (!$item || !$item->getFromDB($items_id)) {
|
|
return false;
|
|
}
|
|
|
|
$result = $DB->insert(self::getTable(), [
|
|
'plugin_urbackup_servers_id' => $servers_id,
|
|
'itemtype' => $itemtype,
|
|
'items_id' => $items_id,
|
|
]);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function getAssetName(string $itemtype, int $items_id): string
|
|
{
|
|
$item = getItemForItemtype($itemtype);
|
|
if (!$item || !$item->getFromDB($items_id)) {
|
|
return '';
|
|
}
|
|
|
|
return (string) ($item->fields['name'] ?? '');
|
|
}
|
|
|
|
public static function getLinkForAsset(
|
|
string $itemtype,
|
|
int $items_id,
|
|
bool $active_only = true
|
|
): ?array {
|
|
global $DB;
|
|
|
|
$iterator = $DB->request([
|
|
'FROM' => self::getTable(),
|
|
'WHERE' => [
|
|
'itemtype' => $itemtype,
|
|
'items_id' => $items_id,
|
|
],
|
|
'LIMIT' => 1,
|
|
]);
|
|
|
|
if (count($iterator) === 0) {
|
|
return null;
|
|
}
|
|
|
|
return $iterator->current();
|
|
}
|
|
|
|
/**
|
|
* Extract asset IP address.
|
|
*
|
|
* @param CommonDBTM $item Item
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function extractAssetIp(CommonDBTM $item): string
|
|
{
|
|
if (!isset($item->fields['ip_address']) || $item->fields['ip_address'] === '') {
|
|
return '';
|
|
}
|
|
|
|
$ip = $item->fields['ip_address'];
|
|
|
|
if (is_array($ip)) {
|
|
return $ip[0] ?? '';
|
|
}
|
|
|
|
return (string) $ip;
|
|
}
|
|
|
|
/**
|
|
* Display tab content for item.
|
|
*
|
|
* @param CommonGLPI $item Server item
|
|
* @param int $tabnum Tab number
|
|
* @param int $withtemplate Template
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0): bool
|
|
{
|
|
global $DB;
|
|
|
|
if (!$item instanceof Server) {
|
|
return false;
|
|
}
|
|
|
|
echo "<div class='center'>";
|
|
echo "<table class='tab_cadre_fixehov'>";
|
|
echo "<tr><th colspan='5'>" . htmlspecialchars(__('Linked assets', 'urbackup')) . "</th></tr>";
|
|
echo "<tr>";
|
|
echo "<th>" . htmlspecialchars(__('Asset', 'urbackup')) . "</th>";
|
|
echo "<th>" . htmlspecialchars(__('Type')) . "</th>";
|
|
echo "<th>" . htmlspecialchars(__('IP address', 'urbackup')) . "</th>";
|
|
echo "<th>" . htmlspecialchars(__('Last file backup', 'urbackup')) . "</th>";
|
|
echo "<th>" . htmlspecialchars(__('Last image backup', 'urbackup')) . "</th>";
|
|
echo "</tr>";
|
|
|
|
$iterator = $DB->request([
|
|
'FROM' => self::getTable(),
|
|
'WHERE' => [
|
|
'plugin_urbackup_servers_id' => (int) $item->fields['id'],
|
|
'is_active' => 1,
|
|
],
|
|
'ORDER' => 'client_name',
|
|
]);
|
|
|
|
foreach ($iterator as $row) {
|
|
$asset_label = (string) $row['client_name'];
|
|
$itemtype = (string) $row['itemtype'];
|
|
$items_id = (int) $row['items_id'];
|
|
|
|
if (class_exists($itemtype)) {
|
|
$asset = new $itemtype();
|
|
|
|
if ($asset instanceof CommonDBTM && $asset->getFromDB($items_id)) {
|
|
$asset_label = $asset->getLink();
|
|
}
|
|
}
|
|
|
|
echo "<tr class='tab_bg_1'>";
|
|
echo "<td>" . $asset_label . "</td>";
|
|
echo "<td>" . htmlspecialchars($itemtype) . "</td>";
|
|
echo "<td>" . htmlspecialchars((string) $row['client_ip']) . "</td>";
|
|
echo "<td>" . htmlspecialchars((string) $row['last_file_backup']) . "</td>";
|
|
echo "<td>" . htmlspecialchars((string) $row['last_image_backup']) . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
|
|
echo "</table>";
|
|
echo "</div>";
|
|
|
|
return true;
|
|
}
|
|
} |