feat : onglet Statistiques — pages, livres, répartition AS avec groupes configurables

- AccessLogParser : parse COMBINED, agrège hits /post/ et /book/, cache 10 min
- AsnLookup : batch lookup ip-api.com, cache 30j, agrégation et groupes AS
- Onglet Statistiques dans l'admin : top pages, top livres, répartition réseau
- Filtrage par groupe AS (badges) + formulaire de configuration des groupes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-15 00:48:34 +02:00
parent dbd76556fb
commit 8cab6362a3
5 changed files with 666 additions and 1 deletions
+178
View File
@@ -0,0 +1,178 @@
<?php
declare(strict_types=1);
class AccessLogParser
{
private string $logDir;
private string $pattern;
private string $cacheFile;
private int $cacheTtl;
private int $days;
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}) /';
public function __construct(
string $logDir = '/var/log/apache2',
string $pattern = '*-access.log',
string $cacheFile = '',
int $cacheTtl = 600,
int $days = 14
) {
$this->logDir = rtrim($logDir, '/');
$this->pattern = $pattern;
$this->cacheFile = $cacheFile !== '' ? $cacheFile : dirname(__DIR__) . '/_cache/access_stats.json';
$this->cacheTtl = $cacheTtl;
$this->days = $days;
}
/**
* @return array{pages:array<string,int>,books:array<string,int>,ips:array<string,int>}
*/
public function stats(): array
{
if (self::$memo !== null) {
return self::$memo;
}
if ($this->cacheValid()) {
$d = json_decode((string) file_get_contents($this->cacheFile), true);
if (is_array($d)) {
return self::$memo = $d;
}
}
$cutoff = strtotime("-{$this->days} days midnight") ?: (time() - $this->days * 86400);
$pages = [];
$books = [];
$ips = [];
foreach ($this->logFiles() as $file) {
$this->parseFile($file, $cutoff, $pages, $books, $ips);
}
arsort($pages);
arsort($books);
arsort($ips);
$result = compact('pages', 'books', 'ips');
@mkdir(dirname($this->cacheFile), 0755, true);
@file_put_contents($this->cacheFile, json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return self::$memo = $result;
}
public function isReadable(): bool
{
return count($this->logFiles()) > 0;
}
private function cacheValid(): bool
{
return file_exists($this->cacheFile)
&& (time() - filemtime($this->cacheFile)) < $this->cacheTtl;
}
/** @return list<array{path:string,type:string}> */
private function logFiles(): array
{
$files = [];
$cutoff = time() - ($this->days + 1) * 86400;
foreach (glob($this->logDir . '/' . $this->pattern) ?: [] as $base) {
if (str_ends_with($base, '.gz') || preg_match('/\.\d+$/', $base)) {
continue;
}
foreach (array_merge([$base], glob($base . '.*') ?: []) as $path) {
if ($path !== $base && filemtime($path) < $cutoff) {
continue;
}
if (!is_readable($path)) {
continue;
}
if (str_ends_with($path, '.tar.gz')) {
$files[] = ['path' => $path, 'type' => 'tgz'];
} elseif (str_ends_with($path, '.gz')) {
$files[] = ['path' => $path, 'type' => 'gz'];
} else {
$files[] = ['path' => $path, 'type' => 'plain'];
}
}
}
return $files;
}
private static function parseTimestamp(string $raw): int
{
// "15/May/2026:00:41:01 +0200"
if (!preg_match('/(\d{2})\/(\w{3})\/(\d{4}):(\d{2}:\d{2}:\d{2}) ([+-]\d{4})/', $raw, $m)) {
return 0;
}
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): void
{
if (!preg_match(self::RE, $line, $m)) {
return;
}
[, $ip, $ts, $path, $status] = $m;
if ($status !== '200') {
return;
}
if (self::parseTimestamp($ts) < $cutoff) {
return;
}
if (str_starts_with($path, '/post/') && strlen($path) > 6) {
$pages[$path] = ($pages[$path] ?? 0) + 1;
$ips[$ip] = ($ips[$ip] ?? 0) + 1;
} elseif (str_starts_with($path, '/book/') && strlen($path) > 6) {
$books[$path] = ($books[$path] ?? 0) + 1;
$ips[$ip] = ($ips[$ip] ?? 0) + 1;
}
}
private function parseFile(array $file, int $cutoff, array &$pages, array &$books, array &$ips): void
{
if ($file['type'] === 'tgz') {
try {
$phar = new PharData($file['path']);
foreach ($phar as $entry) {
$content = @file_get_contents('phar://' . $file['path'] . '/' . $entry->getFilename());
if ($content === false) {
continue;
}
foreach (explode("\n", $content) as $line) {
$this->parseLine($line, $cutoff, $pages, $books, $ips);
}
}
} catch (\Exception $e) {
}
} elseif ($file['type'] === 'gz') {
$h = @gzopen($file['path'], 'rb');
if (!$h) {
return;
}
while (!gzeof($h)) {
$line = gzgets($h, 8192);
if ($line !== false) {
$this->parseLine($line, $cutoff, $pages, $books, $ips);
}
}
gzclose($h);
} else {
$h = @fopen($file['path'], 'rb');
if (!$h) {
return;
}
while (($line = fgets($h)) !== false) {
$this->parseLine($line, $cutoff, $pages, $books, $ips);
}
fclose($h);
}
}
}
+190
View File
@@ -0,0 +1,190 @@
<?php
declare(strict_types=1);
class AsnLookup
{
private string $cacheDir;
private int $ttl;
public function __construct(string $cacheDir = '', int $ttl = 86400 * 30)
{
$this->cacheDir = $cacheDir !== '' ? $cacheDir : dirname(__DIR__) . '/_cache/asn';
$this->ttl = $ttl;
}
/**
* Lookup AS info pour une liste d'IPs.
* IPs privées : retournées avec name='LAN', pas d'appel API.
*
* @param list<string> $ips
* @return array<string, array{asn:string,name:string,country:string}>
*/
public function batchLookup(array $ips): array
{
$results = [];
$missing = [];
foreach (array_unique($ips) as $ip) {
if ($this->isPrivate($ip)) {
$results[$ip] = ['asn' => '', 'name' => 'LAN', 'country' => ''];
continue;
}
$cached = $this->fromCache($ip);
if ($cached !== null) {
$results[$ip] = $cached;
} else {
$missing[] = $ip;
}
}
foreach (array_chunk($missing, 100) as $chunk) {
foreach ($this->fetchBatch($chunk) as $ip => $info) {
$this->toCache($ip, $info);
$results[$ip] = $info;
}
}
return $results;
}
public function isPrivate(string $ip): bool
{
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false;
}
/**
* Agrège les hits par AS depuis un tableau [ip => hits] et les infos AS.
* Retourne [asKey => [asn, name, country, hits]] trié par hits desc.
*
* @param array<string,int> $ipHits
* @param array<string, array{asn:string,name:string,country:string}> $asnMap
* @return list<array{asn:string,name:string,country:string,hits:int}>
*/
public static function aggregateByAs(array $ipHits, array $asnMap): array
{
$byAs = [];
foreach ($ipHits as $ip => $hits) {
$info = $asnMap[$ip] ?? ['asn' => '?', 'name' => '?', 'country' => ''];
$key = $info['asn'] !== '' ? $info['asn'] : $info['name'];
if (!isset($byAs[$key])) {
$byAs[$key] = ['asn' => $info['asn'], 'name' => $info['name'], 'country' => $info['country'], 'hits' => 0];
}
$byAs[$key]['hits'] += $hits;
}
usort($byAs, static fn ($a, $b) => $b['hits'] <=> $a['hits']);
return array_values($byAs);
}
/**
* Applique les groupes définis par l'admin.
* Chaque groupe : ['label' => string, 'patterns' => [string, ...]]
* Un AS est affecté au premier groupe dont un pattern est contenu dans son nom (case-insensitive).
*
* @param list<array{asn:string,name:string,country:string,hits:int}> $asList
* @param list<array{label:string,patterns:list<string>}> $groups
* @return array<string, list<array{asn:string,name:string,country:string,hits:int}>>
* clés : labels des groupes + 'Autres'
*/
public static function applyGroups(array $asList, array $groups): array
{
$result = [];
foreach ($groups as $g) {
$result[$g['label']] = [];
}
$result['Autres'] = [];
foreach ($asList as $as) {
$matched = false;
foreach ($groups as $g) {
foreach ($g['patterns'] as $pattern) {
if ($pattern !== '' && mb_stripos($as['name'], $pattern) !== false) {
$result[$g['label']][] = $as;
$matched = true;
break 2;
}
}
}
if (!$matched) {
$result['Autres'][] = $as;
}
}
return $result;
}
// ─── Cache ────────────────────────────────────────────────────────────────
private function cacheFile(string $ip): string
{
return $this->cacheDir . '/' . md5($ip) . '.json';
}
/** @return array{asn:string,name:string,country:string}|null */
private function fromCache(string $ip): ?array
{
$f = $this->cacheFile($ip);
if (!file_exists($f) || (time() - filemtime($f)) > $this->ttl) {
return null;
}
$d = json_decode((string) file_get_contents($f), true);
return is_array($d) ? $d : null;
}
/** @param array{asn:string,name:string,country:string} $data */
private function toCache(string $ip, array $data): void
{
@mkdir($this->cacheDir, 0755, true);
@file_put_contents($this->cacheFile($ip), json_encode($data));
}
// ─── API ip-api.com ───────────────────────────────────────────────────────
/**
* @param list<string> $ips
* @return array<string, array{asn:string,name:string,country:string}>
*/
private function fetchBatch(array $ips): array
{
$body = json_encode($ips);
$context = stream_context_create(['http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\nContent-Length: " . strlen((string) $body) . "\r\n",
'content' => $body,
'timeout' => 10,
]]);
$resp = @file_get_contents(
'http://ip-api.com/batch?fields=query,as,org,country,countryCode',
false,
$context
);
if ($resp === false) {
return [];
}
$rows = json_decode($resp, true);
if (!is_array($rows)) {
return [];
}
$results = [];
foreach ($rows as $row) {
$ip = $row['query'] ?? '';
if ($ip === '') {
continue;
}
$asRaw = $row['as'] ?? '';
$asn = '';
if (preg_match('/^AS(\d+)/', $asRaw, $m)) {
$asn = $m[1];
}
$name = $row['org'] !== '' ? ($row['org'] ?? '') : preg_replace('/^AS\d+\s*/', '', $asRaw);
$country = $row['countryCode'] ?? '';
$results[$ip] = ['asn' => $asn, 'name' => (string) $name, 'country' => $country];
}
return $results;
}
}
+11
View File
@@ -68,6 +68,13 @@ function apacheAccessLog(): string
return (string)($_ENV['APACHE_ACCESS_LOG'] ?? getenv('APACHE_ACCESS_LOG') ?: '*-access.log');
}
/** @return list<array{label:string,patterns:list<string>}> */
function asGroups(): array
{
$raw = siteSettings()['as_groups'] ?? [];
return is_array($raw) ? $raw : [];
}
function saveSiteSettings(array $data): bool
{
$current = siteSettings();
@@ -86,6 +93,10 @@ function saveSiteSettings(array $data): bool
$current['posts_per_page'] = $val;
}
}
if (array_key_exists('as_groups', $data) && is_array($data['as_groups'])) {
$current['as_groups'] = $data['as_groups'];
}
return file_put_contents(
siteSettingsPath(),
json_encode($current, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)