Files
E.Noorlander d9ea2eee47 v2.6.5 (Lyra): Dynamische pad-resolutie, WordPress-stijl docblocks, security-fix wachtwoord, git-historie schoon
- Bug: dashboard toonde 0 content (AdminPluginAPI::getContentDir() gaf relatief pad terug zonder normalisatie)
- Dynamische pad-resolutie: PluginAPIInterface uitgebreid met getProjectRoot/getContentDir/getPluginsDir/getVersionInfo; CMSAPI en AdminPluginAPI implementeren deze universeel
- public/index.php media-serving gebruikt $config['content_dir'] i.p.v. hardcoded /content
- Navigation en Logs plugins halen paden via de API i.p.v. hardcoded dirname(__DIR__)
- WordPress-stijl docblocks toegevoegd voor alle classes, methods, properties en functies (~450 docblocks, @since 2.6.5)
- Security: hardcoded plaintext-wachtwoord 'admin' verwijderd uit AdminAuth.php; bij eerste installatie wordt een cryptografisch veilig wachtwoord gegenereerd (random_bytes, 16 tekens) en eenmalig op het inlogscherm getoond
- Security: git-geschiedenis schoongemaakt (admin.json, admin.json.example, admin-console/config/admin.json verwijderd uit alle commits; filter-branch over alle branches + tags, gc --prune --aggressive)
- README.md, README.en.md, AGENTS.md bijgewerkt
- Test-scripts bijgewerkt naar clean-URL structuur + actuele ARIA-waarden
- Versie verhoogd naar 2.6.5
- Tests: pentest 29/29, WCAG 25/25, functioneel 16/16, enhanced 25/25
2026-08-27 09:05:51 +00:00

199 lines
7.0 KiB
PHP

<?php
/**
* Logs plugin: toont admin- en request-logs in het beheer.
*
* Systeem-plugin die logbestanden uitleest en in een tabel toont,
* met tabbladen voor admin-logs en request-logs.
*
* @since 2.6.5
*/
class Logs
{
/**
* Plugin-API instantie voor communicatie met de CMS-core.
*
* @since 2.6.5
* @var PluginAPIInterface|null
*/
private ?PluginAPIInterface $api = null;
/**
* Stelt de Plugin-API instantie in.
*
* @since 2.6.5
* @param PluginAPIInterface $api De Plugin-API instantie.
* @return void
*/
public function setAPI(PluginAPIInterface $api): void
{
$this->api = $api;
}
/**
* Geeft de plugin-configuratie terug.
*
* @since 2.6.5
* @return array<string, mixed> Plugin-configuratie met title, viewable en type.
*/
public function getConfig(): array
{
return [
'title' => 'Logs',
'viewable' => false,
'type' => 'system',
];
}
/**
* Geeft de admin-menu-items voor deze plugin terug.
*
* @since 2.6.5
* @return array<int, array<string, mixed>> Lijst met admin-menu-item configuraties.
*/
public function getAdminMenu(): array
{
$config = $this->getPluginConfig();
$requiredRoles = $config['required_roles'] ?? ['bi-manager', 'site-admin', 'admin'];
return [
[
'plugin' => 'Logs',
'route' => 'logs',
'label' => 'Logs',
'label_key' => 'menu_label',
'icon' => 'bi-journal-text',
'section' => 'general',
'permission' => 'logs',
'required_roles' => $requiredRoles,
],
];
}
/**
* Haalt de runtime-configuratie van deze plugin op (standaardwaarden plus overrides).
*
* Leest standaardwaarden uit plugin.json en overschrijft deze met config.json.
*
* @since 2.6.5
* @return array<string, mixed> De samengevoegde plugin-configuratie.
*/
private function getPluginConfig(): array
{
$pluginDir = dirname(__DIR__);
$pluginJsonFile = $pluginDir . '/plugin.json';
$configJsonFile = $pluginDir . '/config.json';
$defaults = [];
if (file_exists($pluginJsonFile)) {
$pluginJson = json_decode(file_get_contents($pluginJsonFile), true) ?? [];
foreach ($pluginJson['settings'] ?? [] as $setting) {
if (isset($setting['key'])) {
$defaults[$setting['key']] = $setting['default'] ?? null;
}
}
}
$overrides = [];
if (file_exists($configJsonFile)) {
$overrides = json_decode(file_get_contents($configJsonFile), true) ?? [];
}
return array_merge($defaults, $overrides);
}
/**
* Verwerkt een admin-route en toont de logs-pagina.
*
* Leest de geselecteerde log, parset de regels en toont deze in een tabel
* met tabbladen voor admin- en request-logs.
*
* @since 2.6.5
* @param string $action De uit te voeren actie.
* @return string|null De HTML-uitvoer van de logs-pagina, of null.
*/
public function handleAdminRoute(string $action): ?string
{
$config = $this->getPluginConfig();
$maxLines = (int)($config['max_lines'] ?? 100);
$showAdminTab = $config['show_admin_tab'] ?? true;
$showRequestsTab = $config['show_requests_tab'] ?? true;
// System plugin: translations resolve against the admin language.
$t = $this->api ? $this->api->getPluginTranslations('Logs') : [];
$tr = function (string $key) use ($t): string {
return $t[$key] ?? $key;
};
$tab = $_GET['tab'] ?? 'admin';
$logDir = ($this->api ? $this->api->getProjectRoot() : dirname(__DIR__, 2)) . '/admin/storage/logs';
$logFile = $tab === 'requests' ? $logDir . '/requests.log' : $logDir . '/admin.log';
$logs = [];
if (file_exists($logFile)) {
$lines = file($logFile) ?: [];
$lines = array_slice($lines, -$maxLines);
foreach ($lines as $line) {
if (preg_match('/^\[([^\]]+)\] \[([^\]]+)\] \[([^\]]+)\] (.+)$/', trim($line), $m)) {
$logs[] = [
'time' => $m[1],
'level' => strtolower($m[2]),
'ip' => $m[3],
'message' => $m[4],
];
}
}
$logs = array_reverse($logs);
}
ob_start();
?>
<h2 class="mb-4"><i class="bi bi-journal-text"></i> <?= htmlspecialchars($tr('page_title')) ?></h2>
<?php if ($showAdminTab || $showRequestsTab): ?>
<ul class="nav nav-tabs mb-3">
<?php if ($showAdminTab): ?>
<li class="nav-item">
<a class="nav-link <?= $tab === 'admin' ? 'active' : '' ?>" href="/admin/logs?tab=admin"><?= htmlspecialchars($tr('tab_admin')) ?></a>
</li>
<?php endif; ?>
<?php if ($showRequestsTab): ?>
<li class="nav-item">
<a class="nav-link <?= $tab === 'requests' ? 'active' : '' ?>" href="/admin/logs?tab=requests"><?= htmlspecialchars($tr('tab_requests')) ?></a>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
<div class="card shadow-sm">
<div class="card-body p-0">
<table class="table table-hover mb-0">
<thead>
<tr>
<th><?= htmlspecialchars($tr('col_time')) ?></th>
<th><?= htmlspecialchars($tr('col_level')) ?></th>
<th><?= htmlspecialchars($tr('col_ip')) ?></th>
<th><?= htmlspecialchars($tr('col_message')) ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($logs)): ?>
<tr><td colspan="4" class="text-muted text-center py-4"><?= htmlspecialchars($tr('no_logs')) ?></td></tr>
<?php else: ?>
<?php foreach ($logs as $log): ?>
<tr>
<td class="text-muted small"><?= htmlspecialchars($log['time']) ?></td>
<td><span class="badge bg-<?= $log['level'] === 'warning' ? 'warning text-dark' : ($log['level'] === 'error' ? 'danger' : 'info') ?>"><?= htmlspecialchars($log['level']) ?></span></td>
<td><code class="text-muted"><?= htmlspecialchars($log['ip']) ?></code></td>
<td><?= htmlspecialchars($log['message']) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<?php
return ob_get_clean();
}
}