From 68a44d19d1d9cec1f56f3ae1ba4348fd2c1211c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9drix?= Date: Tue, 19 May 2026 20:16:20 +0200 Subject: [PATCH] v1.6.30 : agents utilisateur dans le drill-down IP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Drill-down IP : user agents affichés sous l'adresse IP, top 5 par fréquence - AccessLogParser : regex COMBINED étendue pour capturer le UA (groupe 5) - Tracking ipAgents [ip => [ua => count]], ip_agents dans le résultat de stats() Co-Authored-By: Claude Sonnet 4.6 --- CHANGELOG.md | 8 +++++++ public/assets/js/admin-stats.js | 28 ++++++++++++++++------- public/index.php | 1 + public/version.txt | 2 +- src/AccessLogParser.php | 39 ++++++++++++++++++++++----------- 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aada001..d8336c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ Format : [Keep a Changelog](https://keepachangelog.com/fr/1.0.0/) — versionnag --- +## [1.6.30] - 2026-05-19 + +### Ajouté +- Admin stats / drill-down IP : user agents affichés sous l'adresse IP (top 5 par fréquence, sans corrélation avec les pages) +- AccessLogParser : capture du user agent (groupe 5 de la regex COMBINED), tracking `ipAgents` par IP, `ip_agents` dans le résultat + +--- + ## [1.6.29] - 2026-05-19 ### Modifié diff --git a/public/assets/js/admin-stats.js b/public/assets/js/admin-stats.js index 182a299..a27c4a3 100644 --- a/public/assets/js/admin-stats.js +++ b/public/assets/js/admin-stats.js @@ -4,6 +4,10 @@ function esc(s) { return String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } +function trunc(s, n) { + return s.length > n ? s.slice(0, n) + '…' : s; +} + // ── Visiteurs par pays ──────────────────────────────────────────────────────── (function () { var el = document.getElementById('stats-country-container'); @@ -29,7 +33,7 @@ function esc(s) { var d = ipData[ip]; var key = d.asn || '__unknown__'; if (!ipsByAsn[key]) { ipsByAsn[key] = []; } - ipsByAsn[key].push({ ip: ip, hits: d.hits, daily: d.daily, paths: d.paths }); + ipsByAsn[key].push({ ip: ip, hits: d.hits, daily: d.daily, paths: d.paths, agents: d.agents || [] }); }); Object.keys(ipsByAsn).forEach(function (k) { ipsByAsn[k].sort(function (a, b) { return b.hits - a.hits; }); @@ -85,8 +89,16 @@ function esc(s) { var asnKey = n.asn || '__unknown__'; var ips = ipsByAsn[asnKey] || []; - // Lignes IP avec mini sparkline + chemins triés par date desc + // Lignes IP : adresse + agents à gauche, sparkline, chemins, hits var ipRows = ips.slice(0, 20).map(function (ipInfo) { + // Agents sous l'IP + var agentsHtml = ''; + (ipInfo.agents || []).forEach(function (ua) { + agentsHtml += '
' + + esc(trunc(ua, 55)) + '
'; + }); + + // Chemins triés par date desc var articles = [], books = []; Object.keys(ipInfo.paths || {}).forEach(function (path) { var p = ipInfo.paths[path]; @@ -100,10 +112,9 @@ function esc(s) { function pathLine(p, prefix) { var slug = decodeURIComponent(p.path.replace(prefix, '')); - var label = slug.length > 40 ? slug.slice(0, 40) + '…' : slug; return '
' + '' - + esc(label) + '' + + esc(trunc(slug, 40)) + '' + ' (' + p.cnt + ')
'; } @@ -119,8 +130,10 @@ function esc(s) { if (!pathsHtml) { pathsHtml = ''; } return '
' - + '' - + esc(ipInfo.ip) + '' + + '
' + + '' + esc(ipInfo.ip) + '' + + agentsHtml + + '
' + '
' + ipSparkline(ipInfo.daily || []) + '
' + '
' + pathsHtml + '
' + '
' @@ -383,12 +396,11 @@ function esc(s) { var legend = series.map(function (s, si) { var color = COLORS[si % COLORS.length]; - var short = s.title.length > 32 ? s.title.slice(0, 32) + '…' : s.title; return '' + '' + '' - + esc(short) + ''; + + esc(trunc(s.title, 32)) + ''; }).join(''); el.innerHTML = diff --git a/public/index.php b/public/index.php index 7e275d3..20a0b73 100644 --- a/public/index.php +++ b/public/index.php @@ -2748,6 +2748,7 @@ switch ($action) { 'country' => $info['country'], 'daily' => $daily, 'paths' => $accessStats['ip_top_paths'][$ip] ?? [], + 'agents' => $accessStats['ip_agents'][$ip] ?? [], ]; } diff --git a/public/version.txt b/public/version.txt index 69e4e62..14781be 100644 --- a/public/version.txt +++ b/public/version.txt @@ -1 +1 @@ -1.6.29 +1.6.30 diff --git a/src/AccessLogParser.php b/src/AccessLogParser.php index 68e6919..4aaf572 100644 --- a/src/AccessLogParser.php +++ b/src/AccessLogParser.php @@ -13,7 +13,7 @@ class AccessLogParser private static ?array $memo = null; // Apache COMBINED : IP - - [timestamp] "METHOD /path HTTP/x" STATUS bytes "ref" "ua" - private const RE = '/^(\S+) \S+ \S+ \[(\d{2}\/\w+\/\d{4}:\d{2}:\d{2}:\d{2} [+-]\d{4})\] "[A-Z-]+ ([^\s"?]+)[^"]*" (\d{3}) /'; + private const RE = '/^(\S+) \S+ \S+ \[(\d{2}\/\w+\/\d{4}:\d{2}:\d{2}:\d{2} [+-]\d{4})\] "[A-Z-]+ ([^\s"?]+)[^"]*" (\d{3}) \S+ "[^"]*" "([^"]*)"/u'; public function __construct( string $logDir = '/var/log/apache2', @@ -30,7 +30,7 @@ class AccessLogParser } /** - * @return array{pages:array,books:array,ips:array,pages_by_day:array>,ips_by_day:array>,ip_top_paths:array>} + * @return array{pages:array,books:array,ips:array,pages_by_day:array>,ips_by_day:array>,ip_top_paths:array>,ip_agents:array>} */ public function stats(): array { @@ -52,9 +52,10 @@ class AccessLogParser $ipDays = []; // [ip => [dayOffset => count]] $ipPaths = []; // [ip => [path => count]] $ipPathTs = []; // [ip => [path => last_timestamp]] + $ipAgents = []; // [ip => [ua => count]] foreach ($this->logFiles() as $file) { - $this->parseFile($file, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs); + $this->parseFile($file, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs, $ipAgents); } arsort($pages); @@ -72,10 +73,11 @@ class AccessLogParser $pagesByDay[$path] = $arr; } - // Per-IP daily counts + top paths, limité aux 200 IPs les plus actives - $topIpKeys = array_keys(array_slice($ips, 0, 200, true)); - $ipsByDay = []; - $ipTopPaths = []; + // Per-IP daily counts + top paths + top agents, limité aux 200 IPs les plus actives + $topIpKeys = array_keys(array_slice($ips, 0, 200, true)); + $ipsByDay = []; + $ipTopPaths = []; + $ipTopAgents = []; foreach ($topIpKeys as $ip) { $arr = array_fill(0, $this->days, 0); foreach ($ipDays[$ip] ?? [] as $offset => $count) { @@ -91,6 +93,10 @@ class AccessLogParser foreach (array_slice($paths, 0, 10, true) as $p => $cnt) { $ipTopPaths[$ip][$p] = ['n' => $cnt, 'ts' => $ipPathTs[$ip][$p] ?? 0]; } + + $agents = $ipAgents[$ip] ?? []; + arsort($agents); + $ipTopAgents[$ip] = array_keys(array_slice($agents, 0, 5, true)); } $result = [ @@ -100,6 +106,7 @@ class AccessLogParser 'pages_by_day' => $pagesByDay, 'ips_by_day' => $ipsByDay, 'ip_top_paths' => $ipTopPaths, + 'ip_agents' => $ipTopAgents, ]; @mkdir(dirname($this->cacheFile), 0755, true); @file_put_contents($this->cacheFile, json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); @@ -156,12 +163,12 @@ class AccessLogParser return (int) strtotime("{$m[1]} {$m[2]} {$m[3]} {$m[4]} {$m[5]}"); } - private function parseLine(string $line, int $cutoff, array &$pages, array &$books, array &$ips, array &$dayPages, array &$ipDays, array &$ipPaths, array &$ipPathTs): void + private function parseLine(string $line, int $cutoff, array &$pages, array &$books, array &$ips, array &$dayPages, array &$ipDays, array &$ipPaths, array &$ipPathTs, array &$ipAgents): void { if (!preg_match(self::RE, $line, $m)) { return; } - [, $ip, $ts, $path, $status] = $m; + [, $ip, $ts, $path, $status, $ua] = $m; if ($status !== '200') { return; @@ -185,6 +192,9 @@ class AccessLogParser if ($tsVal > ($ipPathTs[$ip][$path] ?? 0)) { $ipPathTs[$ip][$path] = $tsVal; } + if ($ua !== '') { + $ipAgents[$ip][$ua] = ($ipAgents[$ip][$ua] ?? 0) + 1; + } } elseif (str_starts_with($path, '/book/') && strlen($path) > 6) { $books[$path] = ($books[$path] ?? 0) + 1; if ($publicIp) { @@ -195,10 +205,13 @@ class AccessLogParser if ($tsVal > ($ipPathTs[$ip][$path] ?? 0)) { $ipPathTs[$ip][$path] = $tsVal; } + if ($ua !== '') { + $ipAgents[$ip][$ua] = ($ipAgents[$ip][$ua] ?? 0) + 1; + } } } - private function parseFile(array $file, int $cutoff, array &$pages, array &$books, array &$ips, array &$dayPages, array &$ipDays, array &$ipPaths, array &$ipPathTs): void + private function parseFile(array $file, int $cutoff, array &$pages, array &$books, array &$ips, array &$dayPages, array &$ipDays, array &$ipPaths, array &$ipPathTs, array &$ipAgents): void { if ($file['type'] === 'tgz') { try { @@ -209,7 +222,7 @@ class AccessLogParser continue; } foreach (explode("\n", $content) as $line) { - $this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs); + $this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs, $ipAgents); } } } catch (\Exception $e) { @@ -222,7 +235,7 @@ class AccessLogParser while (!gzeof($h)) { $line = gzgets($h, 8192); if ($line !== false) { - $this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs); + $this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs, $ipAgents); } } gzclose($h); @@ -232,7 +245,7 @@ class AccessLogParser return; } while (($line = fgets($h)) !== false) { - $this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs); + $this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages, $ipDays, $ipPaths, $ipPathTs, $ipAgents); } fclose($h); }