CodePress CMS v1.8.0: BotGuard security engine, HAProxy docs & ARIA fix

- Implement BotGuard security engine (Bot, AI, Scraper & Empty UA blocking)
- Add Admin Security page (/admin/security) with toggles, rate limiter & block/allowlists
- Add per-IP RateLimiter handoff in index.php with HTTP 429 response
- Add dynamic /robots.txt generation and noai/noimageai meta tags
- Add RequestLogger status column and blocked badges in admin request logs
- Fix ARIAComponents.php syntax errors on lines 67, 137, 262
- Add HAProxy / PFSense bot blocking & IP forwarding guide (docs/haproxy-bot-blocking.md)
- Update version to 1.8.0 with release notes in version.php and guides
This commit is contained in:
2026-07-29 14:26:48 +02:00
parent 62dd7ddb9c
commit 239762fd3a
18 changed files with 618 additions and 117 deletions
+36 -98
View File
@@ -9,7 +9,7 @@ class RequestLogger
$this->logFile = $logFile;
}
public function log(string $page, string $ip, string $userAgent, string $referrer, string $host, string $acceptLanguage): void
public function log(string $page, string $ip, string $userAgent, string $referrer, string $host, string $acceptLanguage, string $status = 'ok'): void
{
$dir = dirname($this->logFile);
if (!is_dir($dir)) {
@@ -19,7 +19,7 @@ class RequestLogger
$timestamp = date('Y-m-d H:i:s');
$ua = substr(preg_replace('/[[:cntrl:]]/', '', $userAgent), 0, 500);
$ref = substr(preg_replace('/[[:cntrl:]]/', '', $referrer), 0, 500);
$line = "[{$timestamp}] [{$ip}] [{$host}] [{$acceptLanguage}] [{$page}] [{$ua}] [{$ref}]\n";
$line = "[{$timestamp}] [{$ip}] [{$host}] [{$acceptLanguage}] [{$page}] [{$ua}] [{$ref}] [{$status}]\n";
@file_put_contents($this->logFile, $line, FILE_APPEND | LOCK_EX);
}
@@ -73,116 +73,50 @@ class RequestLogger
public static function detectVisitorInfo(string $ua, string $user = ''): array
{
if (empty($ua)) {
$userLabel = ($user && $user !== 'Gast') ? $user : 'Onbekend';
return ['type' => 'unknown', 'label' => $userLabel, 'badge' => 'secondary', 'icon' => 'bi-question-circle'];
}
if (class_exists('BotGuard')) {
$id = BotGuard::identify($ua);
$cat = $id['category'];
$label = $id['label'];
$bots = [
'AI' => [
'GPTBot' => 'AI (GPTBot)',
'ChatGPT-User' => 'AI (ChatGPT)',
'Claude-Web' => 'AI (Claude)',
'ClaudeBot' => 'AI (ClaudeBot)',
'anthropic-ai' => 'AI (Anthropic)',
'Google-Extended' => 'AI (Gemini/Google)',
'CCBot' => 'AI (CommonCrawl)',
'PerplexityBot' => 'AI (Perplexity)',
'Amazonbot' => 'AI (Amazon)',
'cohere-ai' => 'AI (Cohere)',
'OAI-SearchBot' => 'AI (OpenAI)',
'Bytespider' => 'AI (ByteDance)',
'FacebookBot' => 'AI (Meta/FB)',
'Applebot-Extended' => 'AI (Apple)',
],
'Search' => [
'Googlebot' => 'Zoekmachine (Google)',
'Bingbot' => 'Zoekmachine (Bing)',
'BingPreview' => 'Zoekmachine (Bing)',
'Slurp' => 'Zoekmachine (Yahoo)',
'DuckDuckBot' => 'Zoekmachine (DuckDuckGo)',
'Baiduspider' => 'Zoekmachine (Baidu)',
'YandexBot' => 'Zoekmachine (Yandex)',
'Sogou' => 'Zoekmachine (Sogou)',
'Exabot' => 'Zoekmachine (Exabot)',
'facebot' => 'Zoekmachine (Facebook)',
],
'Scraper' => [
'HTTrack' => 'Scraper (HTTrack)',
'Scrapy' => 'Scraper (Scrapy)',
'PhantomJS' => 'Scraper (PhantomJS)',
'HeadlessChrome' => 'Scraper (Headless)',
'curl' => 'Scraper (cURL)',
'wget' => 'Scraper (Wget)',
'python' => 'Scraper (Python)',
'Postman' => 'Scraper (Postman)',
],
];
foreach ($bots['AI'] as $pattern => $label) {
if (stripos($ua, $pattern) !== false) {
if ($cat === 'ai') {
return ['type' => 'ai', 'label' => $label, 'badge' => 'danger', 'icon' => 'bi-robot'];
}
}
foreach ($bots['Search'] as $pattern => $label) {
if (stripos($ua, $pattern) !== false) {
if ($cat === 'search') {
return ['type' => 'search', 'label' => $label, 'badge' => 'primary', 'icon' => 'bi-search'];
}
}
foreach ($bots['Scraper'] as $pattern => $label) {
if (stripos($ua, $pattern) !== false) {
if ($cat === 'scraper') {
return ['type' => 'scraper', 'label' => $label, 'badge' => 'warning text-dark', 'icon' => 'bi-bug'];
}
if ($cat === 'generic') {
return ['type' => 'bot', 'label' => 'Bot', 'badge' => 'secondary', 'icon' => 'bi-robot'];
}
if ($cat === 'empty') {
return ['type' => 'empty', 'label' => 'Lege UA', 'badge' => 'secondary', 'icon' => 'bi-slash-circle'];
}
$userPrefix = ($user && $user !== 'Gast') ? htmlspecialchars($user) . ' (' : '';
$userSuffix = ($user && $user !== 'Gast') ? ')' : '';
return [
'type' => 'human',
'label' => $userPrefix . 'Mens' . $userSuffix,
'badge' => 'success',
'icon' => 'bi-person-check',
];
}
if (preg_match('/(bot|crawler|spider|slurp)/i', $ua)) {
return ['type' => 'bot', 'label' => 'Bot', 'badge' => 'secondary', 'icon' => 'bi-robot'];
}
$userPrefix = ($user && $user !== 'Gast') ? htmlspecialchars($user) . ' (' : '';
$userSuffix = ($user && $user !== 'Gast') ? ')' : '';
return [
'type' => 'human',
'label' => $userPrefix . 'Mens' . $userSuffix,
'badge' => 'success',
'icon' => 'bi-person-check',
];
return ['type' => 'unknown', 'label' => 'Bezoeker', 'badge' => 'secondary', 'icon' => 'bi-person'];
}
public static function detectBot(): ?string
{
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
if (empty($ua)) return null;
$bots = [
'AI' => [
'GPTBot', 'ChatGPT-User', 'Claude-Web', 'ClaudeBot',
'anthropic-ai', 'Google-Extended', 'CCBot',
'PerplexityBot', 'Amazonbot', 'cohere-ai',
'OAI-SearchBot', 'Bytespider', 'FacebookBot',
'Applebot-Extended',
],
'Search' => [
'Googlebot', 'Bingbot', 'BingPreview', 'Slurp',
'DuckDuckBot', 'Baiduspider', 'YandexBot',
'Sogou', 'Exabot', 'facebot',
],
'Scraper' => [
'HTTrack', 'Scrapy', 'PhantomJS', 'HeadlessChrome',
],
];
foreach ($bots as $category => $patterns) {
foreach ($patterns as $pattern) {
if (stripos($ua, $pattern) !== false) {
return $category;
}
if (class_exists('BotGuard')) {
$id = BotGuard::identify($ua);
if (in_array($id['category'], ['ai', 'search', 'scraper'], true)) {
return strtoupper($id['category']);
}
}
return null;
}
@@ -197,12 +131,15 @@ class RequestLogger
$logs = [];
foreach ($content as $line) {
if (preg_match('/^\[([^\]]+)\] \[([^\]]+)\] \[([^\]]+)\] \[([^\]]*)\] \[([^\]]+)\] \[([^\]]*)\] \[([^\]]*)\]$/', trim($line), $m)) {
$trimmed = trim($line);
if (preg_match('/^\[([^\]]+)\] \[([^\]]+)\] \[([^\]]+)\] \[([^\]]*)\] \[([^\]]+)\] \[([^\]]*)\] \[([^\]]*)\](?: \[([^\]]*)\])?$/', $trimmed, $m)) {
$user = $m[3];
// Handle backwards compatibility where host (e.g. localhost:8080 or domain.com) was logged
if (str_contains($user, '.') || str_contains($user, ':') || $user === 'cli') {
$user = 'Gast';
}
$status = $m[8] ?? 'ok';
if ($status === '') $status = 'ok';
$visitorInfo = self::detectVisitorInfo($m[6], $user);
$logs[] = [
'time' => $m[1],
@@ -213,6 +150,7 @@ class RequestLogger
'page' => $m[5],
'ua' => $m[6],
'referrer' => $m[7],
'status' => $status,
];
}
}