mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 19:19:19 +02:00
359 lines
17 KiB
PHP
359 lines
17 KiB
PHP
<?php
|
|
require_once __DIR__ . "/../../bootstrap.php";
|
|
require_once __DIR__ . '/../../src/AdminAuth.php';
|
|
require_once APP_ROOT . '/src/Database.php';
|
|
require_once APP_ROOT . '/src/SystemCache.php';
|
|
require_once APP_ROOT . '/src/Controllers/SystemController.php';
|
|
AdminAuth::requireLogin();
|
|
|
|
$pageTitle = "Système";
|
|
|
|
$_db = new Database();
|
|
$_cache = new SystemCache($_db->getPDO());
|
|
$_controller = new SystemController($_db, $_cache);
|
|
|
|
// ?refresh=1 force-busts all cached sections
|
|
if (isset($_GET['refresh']) && $_GET['refresh'] === '1') {
|
|
$_controller->invalidateAll();
|
|
}
|
|
|
|
// ── Status / PHP / Disk data ──────────────────────────────────────────────────
|
|
$statusData = $_controller->getStatusData();
|
|
$checks = $statusData['checks'];
|
|
$statusCached = $statusData['cached'];
|
|
$statusCacheAge = $statusData['cacheAge'];
|
|
|
|
$phpInfo = $_controller->getPhpInfo();
|
|
$diskInfo = $_controller->getDiskInfo();
|
|
|
|
$diskTotal = $diskInfo['total'];
|
|
$diskFree = $diskInfo['free'];
|
|
$diskUsed = $diskInfo['used'];
|
|
$diskPct = $diskInfo['pct'];
|
|
$diskColor = SystemController::diskColor($diskPct);
|
|
|
|
// ── Active tab + line count ───────────────────────────────────────────────────
|
|
$activeTab = $_GET['tab'] ?? 'nginx_access';
|
|
if ($activeTab === 'status') {
|
|
$activeTab = 'nginx_access'; // legacy redirect
|
|
} elseif ($activeTab !== 'nginx_config' && !array_key_exists($activeTab, SystemController::LOG_FILES)) {
|
|
$activeTab = 'nginx_access';
|
|
}
|
|
|
|
$selectedN = isset($_GET['n']) ? (int) $_GET['n'] : 100;
|
|
if (!in_array($selectedN, SystemController::ALLOWED_LINES, true)) {
|
|
$selectedN = 100;
|
|
}
|
|
|
|
// ── Tab content data ──────────────────────────────────────────────────────────
|
|
$logLines = null;
|
|
$logError = null;
|
|
$logFileMeta = null;
|
|
|
|
$nginxConfigLines = null;
|
|
$nginxConfigSource = null;
|
|
$nginxConfigError = null;
|
|
$nginxConfigMeta = null;
|
|
|
|
if ($activeTab === 'nginx_config') {
|
|
$nginxData = $_controller->getNginxConfigData();
|
|
$nginxConfigLines = $nginxData['lines'];
|
|
$nginxConfigSource = $nginxData['source'];
|
|
$nginxConfigMeta = $nginxData['meta'];
|
|
$nginxConfigError = $nginxData['error'];
|
|
} else {
|
|
$logData = $_controller->getLogData($activeTab, $selectedN);
|
|
$logLines = $logData['lines'];
|
|
$logError = $logData['error'];
|
|
$logFileMeta = $logData['meta'];
|
|
}
|
|
|
|
$isAdmin = true; $bodyClass = 'admin-body';
|
|
$extraCss = ['/assets/css/system.css'];
|
|
// HTMX loaded once in footer; status collapse + copy via inline JS
|
|
require_once APP_ROOT . '/templates/head.php';
|
|
|
|
// Restore collapsed state from cookie
|
|
$collapsed = $_COOKIE['sys_collapsed'] ?? null;
|
|
$statusInitiallyCollapsed = $collapsed === '1';
|
|
?>
|
|
<?php include APP_ROOT . '/templates/header.php'; ?>
|
|
|
|
|
|
|
|
<main id="main-content">
|
|
<h1>Système</h1>
|
|
|
|
<p class="sys-refresh-note">
|
|
Affiché le <?= date('d/m/Y à H:i:s') ?> —
|
|
<a href="?tab=<?= htmlspecialchars($activeTab) ?>&n=<?= $selectedN ?>">Rafraîchir</a> —
|
|
<a href="?tab=<?= htmlspecialchars($activeTab) ?>&n=<?= $selectedN ?>&refresh=1">Forcer actualisation</a>
|
|
</p>
|
|
|
|
<!-- ════════════════════════════════════════════════════════════════════
|
|
STATUS SECTION — always visible above tabs
|
|
════════════════════════════════════════════════════════════════════ -->
|
|
<section class="sys-status-section" aria-label="Statut du système">
|
|
<div class="sys-status-header">
|
|
<h2 class="srv-section-title srv-section-title--compact">Statut
|
|
<?php if ($statusCached && $statusCacheAge !== null): ?>
|
|
<span class="sys-cache-badge sys-cache-badge--hit" title="Données en cache">
|
|
⚡ Cache — il y a <?= $statusCacheAge ?>s
|
|
</span>
|
|
<?php else: ?>
|
|
<span class="sys-cache-badge sys-cache-badge--miss" title="Données fraîches">
|
|
⟳ Actualisé
|
|
</span>
|
|
<?php endif; ?>
|
|
</h2>
|
|
<button id="sys-status-toggle" class="sys-status-toggle"
|
|
aria-expanded="<?= $statusInitiallyCollapsed ? 'false' : 'true' ?>" aria-controls="sys-status-body"
|
|
type="button"
|
|
onclick="var b=document.getElementById('sys-status-body');var c=b.hidden;b.hidden=!c;this.setAttribute('aria-expanded',c);this.textContent=c?'▲ Réduire':'▼ Développer';document.cookie='sys_collapsed='+(!c)+';path=/;max-age=31536000';return false">
|
|
<?= $statusInitiallyCollapsed ? '▼ Développer' : '▲ Réduire' ?>
|
|
</button>
|
|
</div>
|
|
|
|
<div id="sys-status-body"<?= $statusInitiallyCollapsed ? ' hidden' : '' ?>>
|
|
<div class="srv-grid">
|
|
<?php foreach ($checks as $check): ?>
|
|
<?php $st = $check['status'] ?? 'unknown'; ?>
|
|
<div class="srv-card">
|
|
<div class="srv-card__header">
|
|
<span class="srv-card__name"><?= htmlspecialchars($check['label']) ?></span>
|
|
<span class="<?= SystemController::statusClass($st) ?>"><?= SystemController::statusLabel($st) ?></span>
|
|
</div>
|
|
<?php if (!empty($check['detail'])): ?>
|
|
<div class="srv-card__detail"><?= htmlspecialchars($check['detail']) ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<div class="sys-status-meta">
|
|
<div>
|
|
<h3 class="srv-section-title srv-section-title--sub">Environnement PHP</h3>
|
|
<div class="php-grid php-grid--flush">
|
|
<?php foreach ($phpInfo as $key => $val): ?>
|
|
<div class="php-item">
|
|
<div class="php-item__key"><?= htmlspecialchars($key) ?></div>
|
|
<div class="php-item__val"><?= htmlspecialchars($val) ?></div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<h3 class="srv-section-title srv-section-title--sub">Espace disque</h3>
|
|
<div class="disk-bar-wrap">
|
|
<div class="disk-bar" style="--disk-pct:<?= $diskPct ?>%;--disk-color:<?= $diskColor ?>"></div>
|
|
</div>
|
|
<div class="disk-stats">
|
|
<span><?= SystemController::humanBytes($diskUsed) ?> utilisé (<?= $diskPct ?>%)</span>
|
|
<span><?= SystemController::humanBytes($diskFree) ?> libre / <?= SystemController::humanBytes($diskTotal) ?></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- ── Tab bar ─────────────────────────────────────────────────────── -->
|
|
<nav class="sys-tabs" aria-label="Journaux et configuration">
|
|
<?php foreach (SystemController::LOG_FILES as $key => $def): ?>
|
|
<a href="?tab=<?= htmlspecialchars($key) ?>&n=<?= $selectedN ?>"
|
|
class="sys-tab <?= $activeTab === $key ? 'active' : '' ?>"
|
|
hx-get="/admin/system-fragment.php?tab=<?= htmlspecialchars($key) ?>&n=<?= $selectedN ?>"
|
|
hx-target="#sys-tab-panel"
|
|
hx-push-url="?tab=<?= htmlspecialchars($key) ?>&n=<?= $selectedN ?>"
|
|
hx-swap="innerHTML"
|
|
hx-indicator="#sys-tab-panel"
|
|
data-tab="<?= htmlspecialchars($key) ?>"
|
|
<?= $activeTab === $key ? 'aria-current="page"' : '' ?>>
|
|
<?= htmlspecialchars($def['label']) ?>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
<a href="?tab=nginx_config"
|
|
class="sys-tab <?= $activeTab === 'nginx_config' ? 'active' : '' ?>"
|
|
hx-get="/admin/system-fragment.php?tab=nginx_config"
|
|
hx-target="#sys-tab-panel"
|
|
hx-push-url="?tab=nginx_config"
|
|
hx-swap="innerHTML"
|
|
hx-indicator="#sys-tab-panel"
|
|
data-tab="nginx_config"
|
|
<?= $activeTab === 'nginx_config' ? 'aria-current="page"' : '' ?>>nginx — config</a>
|
|
</nav>
|
|
|
|
<!-- Tab panel — content swapped by HTMX -->
|
|
<div id="sys-tab-panel">
|
|
|
|
<?php if ($activeTab === 'nginx_config'): ?>
|
|
<!-- ════════════════════════════════════════════════════════════════════
|
|
NGINX CONFIG PANEL
|
|
════════════════════════════════════════════════════════════════════ -->
|
|
|
|
<?php if ($nginxConfigMeta): ?>
|
|
<div class="log-meta">
|
|
<span data-label="Fichier"><?= htmlspecialchars($nginxConfigMeta['path']) ?></span>
|
|
<span data-label="Taille"><?= $nginxConfigMeta['size'] ?></span>
|
|
<span data-label="Modifié"><?= $nginxConfigMeta['mtime'] ?></span>
|
|
<?php if ($nginxConfigSource === 'live'): ?>
|
|
<span class="nginx-source-badge nginx-source-badge--live">● Config déployée</span>
|
|
<?php else: ?>
|
|
<span class="nginx-source-badge nginx-source-badge--local">⚠ Référence locale (config live inaccessible)</span>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($nginxConfigError !== null): ?>
|
|
<div class="log-unavailable">
|
|
<strong>Configuration nginx non disponible</strong>
|
|
<div class="log-unavail-path"><?= htmlspecialchars($nginxConfigError) ?></div>
|
|
<?php if (php_sapi_name() === 'cli-server'): ?>
|
|
<div class="log-unavail-dev">
|
|
En développement, <code>/etc/nginx/sites-available/posterg</code> n'existe pas.
|
|
La config de référence se trouve dans <code>nginx/posterg.conf</code>.
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php elseif (empty($nginxConfigLines)): ?>
|
|
<div class="log-empty">Le fichier de configuration est vide.</div>
|
|
|
|
<?php else: ?>
|
|
<div class="log-output" id="log-output" role="region" aria-label="Configuration nginx">
|
|
<button class="log-copy-btn" id="log-copy-btn" type="button" title="Copier la configuration"
|
|
onclick="copyLogContent(this);return false">
|
|
Copier
|
|
</button>
|
|
<?php foreach ($nginxConfigLines as $i => $line): ?>
|
|
<span class="log-line <?= SystemController::nginxLineClass($line) ?>"
|
|
data-n="<?= $i + 1 ?>"><?= htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE) ?></span>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php else: ?>
|
|
<!-- ════════════════════════════════════════════════════════════════════
|
|
LOG PANEL
|
|
════════════════════════════════════════════════════════════════════ -->
|
|
|
|
<!-- Lines selector (submits via JS on change; no button needed) -->
|
|
<div class="log-toolbar">
|
|
<label for="lines-select">Afficher</label>
|
|
<form id="lines-form" hx-get="/admin/system-fragment.php"
|
|
hx-target="#sys-tab-panel"
|
|
hx-swap="innerHTML"
|
|
hx-indicator="#sys-tab-panel"
|
|
hx-trigger="change"
|
|
hx-vals='{"tab":"<?= htmlspecialchars($activeTab) ?>"}'>
|
|
<label for="lines-select">Afficher</label>
|
|
<select id="lines-select" name="n" aria-label="Nombre de lignes">
|
|
<?php foreach (SystemController::ALLOWED_LINES as $n): ?>
|
|
<option value="<?= $n ?>" <?= $n === $selectedN ? 'selected' : '' ?>>
|
|
<?= $n ?> dernières lignes
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</form>
|
|
<?php if ($logLines !== null && count($logLines) > 0): ?>
|
|
<span class="log-count-badge"><?= count($logLines) ?> ligne(s)</span>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- File metadata -->
|
|
<?php if ($logFileMeta): ?>
|
|
<div class="log-meta">
|
|
<span data-label="Fichier"><?= htmlspecialchars(SystemController::LOG_FILES[$activeTab]['path']) ?></span>
|
|
<span data-label="Taille"><?= $logFileMeta['size'] ?></span>
|
|
<span data-label="Modifié"><?= $logFileMeta['mtime'] ?></span>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Log output -->
|
|
<?php if ($logError !== null): ?>
|
|
<div class="log-unavailable">
|
|
<strong>Journaux non disponibles</strong>
|
|
<div class="log-unavail-path"><?= $logError ?></div>
|
|
<?php if (php_sapi_name() === 'cli-server'): ?>
|
|
<div class="log-unavail-dev">
|
|
En environnement de développement, les logs nginx ne sont pas disponibles.
|
|
Cette page est pleinement fonctionnelle sur le serveur de production.
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php elseif (empty($logLines)): ?>
|
|
<div class="log-empty">Le fichier journal est vide.</div>
|
|
|
|
<?php else: ?>
|
|
<div class="log-output" id="log-output" role="log" aria-live="off" aria-label="Contenu du journal">
|
|
<button class="log-copy-btn" id="log-copy-btn" type="button" title="Copier le contenu"
|
|
onclick="copyLogContent(this);return false">
|
|
Copier
|
|
</button>
|
|
<?php foreach ($logLines as $i => $line): ?>
|
|
<span class="log-line <?= SystemController::logLineClass($line) ?>"
|
|
data-n="<?= count($logLines) - $i ?>"><?= htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE) ?></span>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php endif; ?>
|
|
|
|
</div><!-- #sys-tab-panel -->
|
|
|
|
</main>
|
|
|
|
<script>
|
|
function copyLogContent(btn) {
|
|
var logOut = document.querySelector('#log-output');
|
|
if (!logOut) return;
|
|
var text = Array.from(logOut.querySelectorAll('.log-line'))
|
|
.map(function(el){ return el.textContent; }).join('\n');
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
navigator.clipboard.writeText(text).then(function(){
|
|
btn.textContent = '\u2713 Copi\u00e9';
|
|
btn.classList.add('copied');
|
|
setTimeout(function(){ btn.textContent = 'Copier'; btn.classList.remove('copied'); }, 2000);
|
|
});
|
|
} else {
|
|
fallbackCopy(text, btn);
|
|
}
|
|
}
|
|
function fallbackCopy(text, btn) {
|
|
var ta = document.createElement('textarea');
|
|
ta.value = text;
|
|
ta.style.cssText = 'position:fixed;opacity:0';
|
|
document.body.appendChild(ta); ta.select();
|
|
try { document.execCommand('copy'); btn.textContent = '\u2713 Copi\u00e9'; btn.classList.add('copied');
|
|
setTimeout(function(){ btn.textContent = 'Copier'; btn.classList.remove('copied'); }, 2000);
|
|
} catch(e) {}
|
|
document.body.removeChild(ta);
|
|
}
|
|
// Update active tab class after each HTMX swap on #sys-tab-panel
|
|
document.body.addEventListener('htmx:afterSwap', function(evt) {
|
|
if (evt.detail.target && evt.detail.target.id === 'sys-tab-panel') {
|
|
var rc = evt.detail.requestConfig;
|
|
var tab = null;
|
|
// Tab clicks carry ?tab=… in the path
|
|
var qIdx = rc.path.indexOf('?');
|
|
if (qIdx !== -1) {
|
|
tab = new URLSearchParams(rc.path.substring(qIdx + 1)).get('tab');
|
|
}
|
|
// Line-count form sends tab via hx-vals in parameters
|
|
if (!tab && rc.parameters && rc.parameters.tab) {
|
|
tab = rc.parameters.tab;
|
|
}
|
|
if (tab) {
|
|
document.querySelectorAll('.sys-tabs .sys-tab').forEach(function(a) {
|
|
var isActive = a.getAttribute('data-tab') === tab;
|
|
a.classList.toggle('active', isActive);
|
|
if (isActive) a.setAttribute('aria-current', 'page');
|
|
else a.removeAttribute('aria-current');
|
|
});
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<?php require_once APP_ROOT . '/templates/admin/footer.php'; ?>
|