mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 19:19:19 +02:00
Extract SystemController: centralise system page data logic, eliminate frag_ helper duplication
- Add src/SystemController.php (452 lines) encapsulating:
- runStatusChecks(): nginx, php-fpm, HTTP ping, SQLite DB, storage, maintenance flag
- getStatusData() / getPhpInfo() / getDiskInfo() with SystemCache TTL delegation
- getLogData(tab, n): log file tail reading + file metadata
- getNginxConfigData(): live-then-local nginx config reading
- Static helpers: logLineClass(), nginxLineClass(), statusLabel(), statusClass(),
humanBytes(), diskColor() — shared by both entry points
- invalidateAll() for ?refresh=1 cache busting
- Rewrite admin/system.php: 582 → 282 lines
- All free functions (safeExec, systemdStatus, localHttpCheck, humanBytes,
statusLabel, statusClass, logLineClass, nginxLineClass, readLogTail) removed
- Data sections replaced by controller method calls
- View template unchanged; now calls SystemController::statusClass() etc. directly
- Rewrite admin/system-fragment.php: 213 → 137 lines
- All duplicated frag_readLogTail(), frag_logLineClass(), frag_nginxLineClass()
helpers removed
- Now instantiates SystemController and delegates getLogData()/getNginxConfigData()
- Identical rendering logic preserved; constant references updated to
SystemController::LOG_FILES and SystemController::ALLOWED_LINES
No behaviour change; no CSS/JS changes.
This commit is contained in:
@@ -11,6 +11,9 @@
|
||||
*/
|
||||
require_once __DIR__ . "/../../config/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/SystemController.php';
|
||||
|
||||
if (!AdminAuth::isAuthenticated()) {
|
||||
http_response_code(403);
|
||||
@@ -20,105 +23,31 @@ if (!AdminAuth::isAuthenticated()) {
|
||||
}
|
||||
|
||||
// ── Validate inputs ────────────────────────────────────────────────────────
|
||||
const LOG_FILES_FRAG = [
|
||||
'nginx_access' => ['label' => 'nginx — accès', 'path' => '/var/log/nginx/posterg_access.log'],
|
||||
'nginx_error' => ['label' => 'nginx — erreurs', 'path' => '/var/log/nginx/posterg_error.log'],
|
||||
'php_error' => ['label' => 'PHP-FPM — erreurs', 'path' => '/var/log/php8.4-fpm.log'],
|
||||
];
|
||||
const ALLOWED_LINES_FRAG = [50, 100, 200, 500];
|
||||
|
||||
$tab = $_GET['tab'] ?? 'nginx_access';
|
||||
if ($tab !== 'nginx_config' && !array_key_exists($tab, LOG_FILES_FRAG)) {
|
||||
if ($tab !== 'nginx_config' && !array_key_exists($tab, SystemController::LOG_FILES)) {
|
||||
$tab = 'nginx_access';
|
||||
}
|
||||
|
||||
$n = isset($_GET['n']) ? (int)$_GET['n'] : 100;
|
||||
if (!in_array($n, ALLOWED_LINES_FRAG, true)) {
|
||||
$n = isset($_GET['n']) ? (int) $_GET['n'] : 100;
|
||||
if (!in_array($n, SystemController::ALLOWED_LINES, true)) {
|
||||
$n = 100;
|
||||
}
|
||||
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
header('X-Robots-Tag: noindex');
|
||||
|
||||
// ── Helpers (duplicated from system.php — small enough to inline) ──────────
|
||||
function frag_readLogTail(string $logPath, int $lines, ?string &$errorMsg): ?array
|
||||
{
|
||||
$errorMsg = null;
|
||||
if (!function_exists('exec')) {
|
||||
$errorMsg = "exec() est désactivé sur ce serveur.";
|
||||
return null;
|
||||
}
|
||||
if (!file_exists($logPath)) {
|
||||
$errorMsg = "Fichier introuvable : " . htmlspecialchars($logPath);
|
||||
return null;
|
||||
}
|
||||
if (!is_readable($logPath)) {
|
||||
$errorMsg = "Fichier non lisible (permissions insuffisantes) : " . htmlspecialchars($logPath);
|
||||
return null;
|
||||
}
|
||||
$output = [];
|
||||
$rc = 0;
|
||||
exec('tail -n ' . intval($lines) . ' ' . escapeshellarg($logPath) . ' 2>/dev/null', $output, $rc);
|
||||
if ($rc !== 0) {
|
||||
$errorMsg = "Erreur lors de la lecture du fichier journal.";
|
||||
return null;
|
||||
}
|
||||
return array_reverse($output);
|
||||
}
|
||||
|
||||
function frag_logLineClass(string $line): string
|
||||
{
|
||||
if (preg_match('/\[(crit|emerg|alert)\]/', $line)) return 'log-crit';
|
||||
if (preg_match('/\[error\]/', $line)) return 'log-error';
|
||||
if (preg_match('/\[warn\]/', $line)) return 'log-warn';
|
||||
if (preg_match('/\[notice\]/', $line)) return 'log-notice';
|
||||
if (preg_match('/" [45]\d\d /', $line)) return 'log-error';
|
||||
if (preg_match('/" 3\d\d /', $line)) return 'log-notice';
|
||||
return '';
|
||||
}
|
||||
|
||||
function frag_nginxLineClass(string $line): string
|
||||
{
|
||||
$trimmed = ltrim($line);
|
||||
if ($trimmed === '' || str_starts_with($trimmed, '#')) return 'nginx-comment';
|
||||
if (preg_match('/^\s*(location|server|upstream|events|http|geo|map|types)\b/', $line)) return 'nginx-block';
|
||||
return 'nginx-directive';
|
||||
}
|
||||
// ── Build data via controller ──────────────────────────────────────────────
|
||||
$_db = new Database();
|
||||
$_cache = new SystemCache($_db->getPDO());
|
||||
$_controller = new SystemController($_db, $_cache);
|
||||
|
||||
// ── Render ─────────────────────────────────────────────────────────────────
|
||||
if ($tab === 'nginx_config') {
|
||||
$livePath = '/etc/nginx/sites-available/posterg';
|
||||
$localPath = APP_ROOT . '/nginx/posterg.conf';
|
||||
|
||||
$lines = null;
|
||||
$source = null;
|
||||
$meta = null;
|
||||
$error = null;
|
||||
|
||||
foreach ([[$livePath, 'live'], [$localPath, 'local']] as [$path, $src]) {
|
||||
if (file_exists($path) && is_readable($path)) {
|
||||
$raw = file($path, FILE_IGNORE_NEW_LINES);
|
||||
if ($raw !== false) {
|
||||
$lines = $raw;
|
||||
$source = $src;
|
||||
$sz = filesize($path);
|
||||
$meta = [
|
||||
'path' => $path,
|
||||
'size' => $sz > 1048576
|
||||
? number_format($sz / 1048576, 2) . ' MB'
|
||||
: number_format($sz / 1024, 1) . ' KB',
|
||||
'mtime' => date('d/m/Y H:i:s', filemtime($path)),
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($lines === null) {
|
||||
$error = file_exists($livePath)
|
||||
? "Fichier non lisible (permissions insuffisantes) : " . htmlspecialchars($livePath)
|
||||
: "Config live introuvable (" . htmlspecialchars($livePath) . ") et config locale introuvable (" . htmlspecialchars($localPath) . ").";
|
||||
}
|
||||
$data = $_controller->getNginxConfigData();
|
||||
$lines = $data['lines'];
|
||||
$source = $data['source'];
|
||||
$meta = $data['meta'];
|
||||
$error = $data['error'];
|
||||
|
||||
if ($meta): ?>
|
||||
<div class="log-meta">
|
||||
@@ -150,32 +79,23 @@ if ($tab === 'nginx_config') {
|
||||
<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">Copier</button>
|
||||
<?php foreach ($lines as $i => $line): ?>
|
||||
<span class="log-line <?= frag_nginxLineClass($line) ?>"
|
||||
<span class="log-line <?= SystemController::nginxLineClass($line) ?>"
|
||||
data-n="<?= $i + 1 ?>"><?= htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE) ?></span>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif;
|
||||
|
||||
} else {
|
||||
// ── Log tab ────────────────────────────────────────────────────────
|
||||
$logPath = LOG_FILES_FRAG[$tab]['path'];
|
||||
$logError = null;
|
||||
$logLines = frag_readLogTail($logPath, $n, $logError);
|
||||
$logMeta = null;
|
||||
if (file_exists($logPath)) {
|
||||
$sz = filesize($logPath);
|
||||
$logMeta = [
|
||||
'size' => $sz > 1048576
|
||||
? number_format($sz / 1048576, 2) . ' MB'
|
||||
: number_format($sz / 1024, 1) . ' KB',
|
||||
'mtime' => date('d/m/Y H:i:s', filemtime($logPath)),
|
||||
];
|
||||
}
|
||||
// ── Log tab ────────────────────────────────────────────────────────────
|
||||
$data = $_controller->getLogData($tab, $n);
|
||||
$logLines = $data['lines'];
|
||||
$logError = $data['error'];
|
||||
$logMeta = $data['meta'];
|
||||
?>
|
||||
<div class="log-toolbar">
|
||||
<label for="lines-select" style="font-size:.84rem;color:var(--text-secondary);">Afficher</label>
|
||||
<label for="lines-select">Afficher</label>
|
||||
<select id="lines-select" aria-label="Nombre de lignes">
|
||||
<?php foreach (ALLOWED_LINES_FRAG as $opt): ?>
|
||||
<?php foreach (SystemController::ALLOWED_LINES as $opt): ?>
|
||||
<option value="<?= $opt ?>" <?= $opt === $n ? 'selected' : '' ?>><?= $opt ?> dernières lignes</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
@@ -186,7 +106,7 @@ if ($tab === 'nginx_config') {
|
||||
|
||||
<?php if ($logMeta): ?>
|
||||
<div class="log-meta">
|
||||
<span data-label="Fichier"><?= htmlspecialchars($logPath) ?></span>
|
||||
<span data-label="Fichier"><?= htmlspecialchars($logMeta['path']) ?></span>
|
||||
<span data-label="Taille"><?= $logMeta['size'] ?></span>
|
||||
<span data-label="Modifié"><?= $logMeta['mtime'] ?></span>
|
||||
</div>
|
||||
@@ -209,7 +129,7 @@ if ($tab === 'nginx_config') {
|
||||
<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">Copier</button>
|
||||
<?php foreach ($logLines as $i => $line): ?>
|
||||
<span class="log-line <?= frag_logLineClass($line) ?>"
|
||||
<span class="log-line <?= SystemController::logLineClass($line) ?>"
|
||||
data-n="<?= count($logLines) - $i ?>"><?= htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE) ?></span>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user