mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-07 03:29:19 +02:00
147 lines
6.3 KiB
PHP
147 lines
6.3 KiB
PHP
<?php
|
|
/**
|
|
* system-fragment.php — returns only the tab-panel HTML for the admin system page.
|
|
*
|
|
* Called by fetch() from system.php JS when switching tabs or changing line count.
|
|
* With JS disabled the user never hits this URL directly; the tab <a> hrefs still
|
|
* point at system.php?tab=… so navigation degrades gracefully.
|
|
*
|
|
* Response: text/html fragment (no <html>/<head>/<body> wrapper).
|
|
* On any auth failure or bad request: 403 / 400 with a plain-text body.
|
|
*/
|
|
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';
|
|
|
|
if (!AdminAuth::isAuthenticated()) {
|
|
http_response_code(403);
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
echo 'Non autorisé';
|
|
exit;
|
|
}
|
|
|
|
// ── Validate inputs ────────────────────────────────────────────────────────
|
|
$tab = $_GET['tab'] ?? 'nginx_access';
|
|
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, SystemController::ALLOWED_LINES, true)) {
|
|
$n = 100;
|
|
}
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
header('X-Robots-Tag: noindex');
|
|
|
|
// ── Build data via controller ──────────────────────────────────────────────
|
|
$_db = new Database();
|
|
$_cache = new SystemCache($_db->getPDO());
|
|
$_controller = new SystemController($_db, $_cache);
|
|
|
|
// ── Render ─────────────────────────────────────────────────────────────────
|
|
if ($tab === 'nginx_config') {
|
|
$data = $_controller->getNginxConfigData();
|
|
$lines = $data['lines'];
|
|
$source = $data['source'];
|
|
$meta = $data['meta'];
|
|
$error = $data['error'];
|
|
|
|
if ($meta): ?>
|
|
<div class="log-meta">
|
|
<span data-label="Fichier"><?= htmlspecialchars($meta['path']) ?></span>
|
|
<span data-label="Taille"><?= $meta['size'] ?></span>
|
|
<span data-label="Modifié"><?= $meta['mtime'] ?></span>
|
|
<?php if ($source === '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;
|
|
|
|
if ($error !== null): ?>
|
|
<div class="log-unavailable">
|
|
<strong>Configuration nginx non disponible</strong>
|
|
<div class="log-unavail-path"><?= htmlspecialchars($error) ?></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($lines)): ?>
|
|
<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 ($lines 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;
|
|
|
|
} else {
|
|
// ── Log tab ────────────────────────────────────────────────────────────
|
|
$data = $_controller->getLogData($tab, $n);
|
|
$logLines = $data['lines'];
|
|
$logError = $data['error'];
|
|
$logMeta = $data['meta'];
|
|
?>
|
|
<div class="log-toolbar">
|
|
<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($tab) ?>"}'>
|
|
<label for="lines-select">Afficher</label>
|
|
<select id="lines-select" name="n" aria-label="Nombre de lignes">
|
|
<?php foreach (SystemController::ALLOWED_LINES as $opt): ?>
|
|
<option value="<?= $opt ?>" <?= $opt === $n ? 'selected' : '' ?>><?= $opt ?> 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>
|
|
|
|
<?php if ($logMeta): ?>
|
|
<div class="log-meta">
|
|
<span data-label="Fichier"><?= htmlspecialchars($logMeta['path']) ?></span>
|
|
<span data-label="Taille"><?= $logMeta['size'] ?></span>
|
|
<span data-label="Modifié"><?= $logMeta['mtime'] ?></span>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?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;
|
|
}
|