api = $api; } public function getConfig(): array { return [ 'title' => 'Logs', 'viewable' => false, 'type' => 'system', ]; } 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, ], ]; } /** * Get this plugin's runtime config (defaults + overrides). */ 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); } 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 = 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(); ?>
| = htmlspecialchars($tr('col_time')) ?> | = htmlspecialchars($tr('col_level')) ?> | = htmlspecialchars($tr('col_ip')) ?> | = htmlspecialchars($tr('col_message')) ?> |
|---|---|---|---|
| = htmlspecialchars($tr('no_logs')) ?> | |||
| = htmlspecialchars($log['time']) ?> | = htmlspecialchars($log['level']) ?> | = htmlspecialchars($log['ip']) ?> |
= htmlspecialchars($log['message']) ?> |