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:
@@ -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
|
## [1.6.29] - 2026-05-19
|
||||||
|
|
||||||
### Modifié
|
### Modifié
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ function esc(s) {
|
|||||||
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function trunc(s, n) {
|
||||||
|
return s.length > n ? s.slice(0, n) + '…' : s;
|
||||||
|
}
|
||||||
|
|
||||||
// ── Visiteurs par pays ────────────────────────────────────────────────────────
|
// ── Visiteurs par pays ────────────────────────────────────────────────────────
|
||||||
(function () {
|
(function () {
|
||||||
var el = document.getElementById('stats-country-container');
|
var el = document.getElementById('stats-country-container');
|
||||||
@@ -29,7 +33,7 @@ function esc(s) {
|
|||||||
var d = ipData[ip];
|
var d = ipData[ip];
|
||||||
var key = d.asn || '__unknown__';
|
var key = d.asn || '__unknown__';
|
||||||
if (!ipsByAsn[key]) { ipsByAsn[key] = []; }
|
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) {
|
Object.keys(ipsByAsn).forEach(function (k) {
|
||||||
ipsByAsn[k].sort(function (a, b) { return b.hits - a.hits; });
|
ipsByAsn[k].sort(function (a, b) { return b.hits - a.hits; });
|
||||||
@@ -85,8 +89,16 @@ function esc(s) {
|
|||||||
var asnKey = n.asn || '__unknown__';
|
var asnKey = n.asn || '__unknown__';
|
||||||
var ips = ipsByAsn[asnKey] || [];
|
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) {
|
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 = [];
|
var articles = [], books = [];
|
||||||
Object.keys(ipInfo.paths || {}).forEach(function (path) {
|
Object.keys(ipInfo.paths || {}).forEach(function (path) {
|
||||||
var p = ipInfo.paths[path];
|
var p = ipInfo.paths[path];
|
||||||
@@ -100,10 +112,9 @@ function esc(s) {
|
|||||||
|
|
||||||
function pathLine(p, prefix) {
|
function pathLine(p, prefix) {
|
||||||
var slug = decodeURIComponent(p.path.replace(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">'
|
return '<div style="font-size:.75rem;line-height:1.5">'
|
||||||
+ '<a href="' + esc(p.path) + '" target="_blank" style="color:#495057">'
|
+ '<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>';
|
+ ' <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>'; }
|
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">'
|
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">'
|
+ '<div style="width:9rem;flex-shrink:0">'
|
||||||
+ esc(ipInfo.ip) + '</code>'
|
+ '<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 style="flex-shrink:0;padding-top:2px">' + ipSparkline(ipInfo.daily || []) + '</div>'
|
||||||
+ '<div class="flex-grow-1">' + pathsHtml + '</div>'
|
+ '<div class="flex-grow-1">' + pathsHtml + '</div>'
|
||||||
+ '<div class="text-end text-muted small" style="width:4rem;flex-shrink:0;padding-top:2px">'
|
+ '<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 legend = series.map(function (s, si) {
|
||||||
var color = COLORS[si % COLORS.length];
|
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">'
|
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"'
|
+ '<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>'
|
+ ' stroke="' + color + '" stroke-width="2.5" stroke-linecap="round"/></svg>'
|
||||||
+ '<span class="text-truncate" style="max-width:160px" title="' + esc(s.title) + '">'
|
+ '<span class="text-truncate" style="max-width:160px" title="' + esc(s.title) + '">'
|
||||||
+ esc(short) + '</span></span>';
|
+ esc(trunc(s.title, 32)) + '</span></span>';
|
||||||
}).join('');
|
}).join('');
|
||||||
|
|
||||||
el.innerHTML =
|
el.innerHTML =
|
||||||
|
|||||||
@@ -2748,6 +2748,7 @@ switch ($action) {
|
|||||||
'country' => $info['country'],
|
'country' => $info['country'],
|
||||||
'daily' => $daily,
|
'daily' => $daily,
|
||||||
'paths' => $accessStats['ip_top_paths'][$ip] ?? [],
|
'paths' => $accessStats['ip_top_paths'][$ip] ?? [],
|
||||||
|
'agents' => $accessStats['ip_agents'][$ip] ?? [],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
1.6.29
|
1.6.30
|
||||||
|
|||||||
+26
-13
@@ -13,7 +13,7 @@ class AccessLogParser
|
|||||||
private static ?array $memo = null;
|
private static ?array $memo = null;
|
||||||
|
|
||||||
// Apache COMBINED : IP - - [timestamp] "METHOD /path HTTP/x" STATUS bytes "ref" "ua"
|
// 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(
|
public function __construct(
|
||||||
string $logDir = '/var/log/apache2',
|
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
|
public function stats(): array
|
||||||
{
|
{
|
||||||
@@ -52,9 +52,10 @@ class AccessLogParser
|
|||||||
$ipDays = []; // [ip => [dayOffset => count]]
|
$ipDays = []; // [ip => [dayOffset => count]]
|
||||||
$ipPaths = []; // [ip => [path => count]]
|
$ipPaths = []; // [ip => [path => count]]
|
||||||
$ipPathTs = []; // [ip => [path => last_timestamp]]
|
$ipPathTs = []; // [ip => [path => last_timestamp]]
|
||||||
|
$ipAgents = []; // [ip => [ua => count]]
|
||||||
|
|
||||||
foreach ($this->logFiles() as $file) {
|
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);
|
arsort($pages);
|
||||||
@@ -72,10 +73,11 @@ class AccessLogParser
|
|||||||
$pagesByDay[$path] = $arr;
|
$pagesByDay[$path] = $arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Per-IP daily counts + top paths, limité aux 200 IPs les plus actives
|
// Per-IP daily counts + top paths + top agents, limité aux 200 IPs les plus actives
|
||||||
$topIpKeys = array_keys(array_slice($ips, 0, 200, true));
|
$topIpKeys = array_keys(array_slice($ips, 0, 200, true));
|
||||||
$ipsByDay = [];
|
$ipsByDay = [];
|
||||||
$ipTopPaths = [];
|
$ipTopPaths = [];
|
||||||
|
$ipTopAgents = [];
|
||||||
foreach ($topIpKeys as $ip) {
|
foreach ($topIpKeys as $ip) {
|
||||||
$arr = array_fill(0, $this->days, 0);
|
$arr = array_fill(0, $this->days, 0);
|
||||||
foreach ($ipDays[$ip] ?? [] as $offset => $count) {
|
foreach ($ipDays[$ip] ?? [] as $offset => $count) {
|
||||||
@@ -91,6 +93,10 @@ class AccessLogParser
|
|||||||
foreach (array_slice($paths, 0, 10, true) as $p => $cnt) {
|
foreach (array_slice($paths, 0, 10, true) as $p => $cnt) {
|
||||||
$ipTopPaths[$ip][$p] = ['n' => $cnt, 'ts' => $ipPathTs[$ip][$p] ?? 0];
|
$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 = [
|
$result = [
|
||||||
@@ -100,6 +106,7 @@ class AccessLogParser
|
|||||||
'pages_by_day' => $pagesByDay,
|
'pages_by_day' => $pagesByDay,
|
||||||
'ips_by_day' => $ipsByDay,
|
'ips_by_day' => $ipsByDay,
|
||||||
'ip_top_paths' => $ipTopPaths,
|
'ip_top_paths' => $ipTopPaths,
|
||||||
|
'ip_agents' => $ipTopAgents,
|
||||||
];
|
];
|
||||||
@mkdir(dirname($this->cacheFile), 0755, true);
|
@mkdir(dirname($this->cacheFile), 0755, true);
|
||||||
@file_put_contents($this->cacheFile, json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
@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]}");
|
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)) {
|
if (!preg_match(self::RE, $line, $m)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
[, $ip, $ts, $path, $status] = $m;
|
[, $ip, $ts, $path, $status, $ua] = $m;
|
||||||
|
|
||||||
if ($status !== '200') {
|
if ($status !== '200') {
|
||||||
return;
|
return;
|
||||||
@@ -185,6 +192,9 @@ class AccessLogParser
|
|||||||
if ($tsVal > ($ipPathTs[$ip][$path] ?? 0)) {
|
if ($tsVal > ($ipPathTs[$ip][$path] ?? 0)) {
|
||||||
$ipPathTs[$ip][$path] = $tsVal;
|
$ipPathTs[$ip][$path] = $tsVal;
|
||||||
}
|
}
|
||||||
|
if ($ua !== '') {
|
||||||
|
$ipAgents[$ip][$ua] = ($ipAgents[$ip][$ua] ?? 0) + 1;
|
||||||
|
}
|
||||||
} elseif (str_starts_with($path, '/book/') && strlen($path) > 6) {
|
} elseif (str_starts_with($path, '/book/') && strlen($path) > 6) {
|
||||||
$books[$path] = ($books[$path] ?? 0) + 1;
|
$books[$path] = ($books[$path] ?? 0) + 1;
|
||||||
if ($publicIp) {
|
if ($publicIp) {
|
||||||
@@ -195,10 +205,13 @@ class AccessLogParser
|
|||||||
if ($tsVal > ($ipPathTs[$ip][$path] ?? 0)) {
|
if ($tsVal > ($ipPathTs[$ip][$path] ?? 0)) {
|
||||||
$ipPathTs[$ip][$path] = $tsVal;
|
$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') {
|
if ($file['type'] === 'tgz') {
|
||||||
try {
|
try {
|
||||||
@@ -209,7 +222,7 @@ class AccessLogParser
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach (explode("\n", $content) as $line) {
|
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) {
|
} catch (\Exception $e) {
|
||||||
@@ -222,7 +235,7 @@ class AccessLogParser
|
|||||||
while (!gzeof($h)) {
|
while (!gzeof($h)) {
|
||||||
$line = gzgets($h, 8192);
|
$line = gzgets($h, 8192);
|
||||||
if ($line !== false) {
|
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);
|
gzclose($h);
|
||||||
@@ -232,7 +245,7 @@ class AccessLogParser
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (($line = fgets($h)) !== false) {
|
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);
|
fclose($h);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user