v1.6.30 : agents utilisateur dans le drill-down IP

- 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 <noreply@anthropic.com>
This commit is contained in:
2026-05-19 20:16:20 +02:00
parent e3d7e433e0
commit 68a44d19d1
5 changed files with 56 additions and 22 deletions
+8
View File
@@ -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é
+20 -8
View File
@@ -4,6 +4,10 @@ function esc(s) {
return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
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 += '<div style="font-size:.65rem;color:#adb5bd;line-height:1.4;word-break:break-all">'
+ esc(trunc(ua, 55)) + '</div>';
});
// 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 '<div style="font-size:.75rem;line-height:1.5">'
+ '<a href="' + esc(p.path) + '" target="_blank" style="color:#495057">'
+ esc(label) + '</a>'
+ esc(trunc(slug, 40)) + '</a>'
+ ' <span style="color:#adb5bd">(' + p.cnt + ')</span></div>';
}
@@ -119,8 +130,10 @@ function esc(s) {
if (!pathsHtml) { pathsHtml = '<span style="font-size:.75rem;color:#adb5bd">—</span>'; }
return '<div class="d-flex gap-2 py-2 border-bottom align-items-start">'
+ '<code style="width:9rem;flex-shrink:0;font-size:.72rem;color:#6c757d;padding-top:2px">'
+ esc(ipInfo.ip) + '</code>'
+ '<div style="width:9rem;flex-shrink:0">'
+ '<code style="font-size:.72rem;color:#6c757d">' + esc(ipInfo.ip) + '</code>'
+ agentsHtml
+ '</div>'
+ '<div style="flex-shrink:0;padding-top:2px">' + ipSparkline(ipInfo.daily || []) + '</div>'
+ '<div class="flex-grow-1">' + pathsHtml + '</div>'
+ '<div class="text-end text-muted small" style="width:4rem;flex-shrink:0;padding-top:2px">'
@@ -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 '<span class="d-inline-flex align-items-center gap-1 me-3 mb-1 small">'
+ '<svg width="16" height="3" style="flex-shrink:0"><line x1="0" y1="1.5" x2="16" y2="1.5"'
+ ' stroke="' + color + '" stroke-width="2.5" stroke-linecap="round"/></svg>'
+ '<span class="text-truncate" style="max-width:160px" title="' + esc(s.title) + '">'
+ esc(short) + '</span></span>';
+ esc(trunc(s.title, 32)) + '</span></span>';
}).join('');
el.innerHTML =
+1
View File
@@ -2748,6 +2748,7 @@ switch ($action) {
'country' => $info['country'],
'daily' => $daily,
'paths' => $accessStats['ip_top_paths'][$ip] ?? [],
'agents' => $accessStats['ip_agents'][$ip] ?? [],
];
}
+1 -1
View File
@@ -1 +1 @@
1.6.29
1.6.30
+26 -13
View File
@@ -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<string,int>,books:array<string,int>,ips:array<string,int>,pages_by_day:array<string,list<int>>,ips_by_day:array<string,list<int>>,ip_top_paths:array<string,array<string,array{n:int,ts:int}>>}
* @return array{pages:array<string,int>,books:array<string,int>,ips:array<string,int>,pages_by_day:array<string,list<int>>,ips_by_day:array<string,list<int>>,ip_top_paths:array<string,array<string,array{n:int,ts:int}>>,ip_agents:array<string,list<string>>}
*/
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);
}