Fix newest homepage detection, file creation dates, real IP, and request log visitor type

- Fix detectNewestPage() to search subdirectories recursively and handle language prefixes
- Fix getFileInfo() to preserve frontmatter created date or ctime instead of overwriting with mtime
- Automatically store created date in frontmatter when creating/editing files
- Add RequestLogger::getClientIp() with proxy and Cloudflare header support
- Replace domain column in request log with visitor/bot type badges (Human, AI, Search, Scraper)
- Update 'Activiteitenlog' to 'Activiteiten log' in UI and guide
This commit is contained in:
2026-07-28 16:56:51 +02:00
parent b8d4a4e6d5
commit f81be9e047
9 changed files with 256 additions and 44 deletions
+5 -21
View File
@@ -21,6 +21,7 @@ if (file_exists($autoloader)) {
// Load admin components
$appConfig = require __DIR__ . '/../admin/config/app.php';
require_once __DIR__ . '/../admin/src/AdminAuth.php';
require_once __DIR__ . '/../cms/core/class/RequestLogger.php';
$auth = new AdminAuth($appConfig);
@@ -220,22 +221,8 @@ function handleDashboard(AdminAuth $auth, array $config): void
// Load recent request log
$requestLogFile = $config['request_log'];
$recentRequests = [];
if (file_exists($requestLogFile)) {
$lines = file($requestLogFile);
$lines = array_slice($lines, -20);
foreach ($lines as $line) {
if (preg_match('/^\[([^\]]+)\] \[([^\]]+)\] \[([^\]]+)\] \[([^\]]*)\] \[([^\]]+)\] \[([^\]]*)\] \[([^\]]*)\]$/', trim($line), $m)) {
$recentRequests[] = [
'time' => $m[1],
'ip' => $m[2],
'page' => $m[5],
'ua' => $m[6],
];
}
}
$recentRequests = array_reverse($recentRequests);
}
$requestLogger = new RequestLogger($requestLogFile);
$recentRequests = $requestLogger->getLogs(20);
require __DIR__ . '/../admin/templates/layout.php';
}
@@ -318,10 +305,7 @@ function adminLog(array $config, string $level, string $message): void
@mkdir($dir, 0755, true);
}
$timestamp = date('Y-m-d H:i:s');
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? 'cli';
if (($commaPos = strpos($ip, ',')) !== false) {
$ip = trim(substr($ip, 0, $commaPos));
}
$ip = RequestLogger::getClientIp();
@file_put_contents($logFile, "[{$timestamp}] [{$level}] [{$ip}] {$message}\n", FILE_APPEND);
}
@@ -1489,7 +1473,7 @@ function handleLogs(AdminAuth $auth, array $config): void
if (isset($_GET['clear'])) {
$target = $activeTab === 'admin' ? $adminLogFile : $requestLogFile;
@file_put_contents($target, '');
$message = $activeTab === 'admin' ? 'Activiteitenlog gewist.' : 'Requestlog gewist.';
$message = $activeTab === 'admin' ? 'Activiteiten log gewist.' : 'Requestlog gewist.';
header('Location: /admin/logs?tab=' . $activeTab);
exit;
}
+7 -2
View File
@@ -103,14 +103,19 @@ $cms = new CodePressCMS($config);
// Log page view (not for media/assets)
if (!str_starts_with($path, '/-media/') && !str_starts_with($path, '/-assets/')) {
if (session_status() === PHP_SESSION_NONE) {
@session_start();
}
$loggedInUser = $_SESSION['admin_user'] ?? 'Gast';
$requestLogFile = dirname(__DIR__) . '/admin/storage/logs/requests.log';
$logger = new RequestLogger($requestLogFile);
$logger->log(
$_GET['page'] ?? $config['default_page'] ?? 'index',
$_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? 'cli',
RequestLogger::getClientIp(),
$_SERVER['HTTP_USER_AGENT'] ?? '',
$_SERVER['HTTP_REFERER'] ?? '',
$_SERVER['HTTP_HOST'] ?? '',
$loggedInUser,
$_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? ''
);
}