feat #64 : onglet Statistiques — pages, livres, répartition AS #65
+46
-1
@@ -43,7 +43,7 @@ $action = $_GET['action'] ?? 'list';
|
|||||||
$uuid = $_GET['uuid'] ?? '';
|
$uuid = $_GET['uuid'] ?? '';
|
||||||
$slug = $_GET['slug'] ?? '';
|
$slug = $_GET['slug'] ?? '';
|
||||||
|
|
||||||
$_noindexActions = ['create', 'edit', 'admin', 'categories', 'diff', 'add_files', 'import_image', 'import_image_step2', 'sources', 'profile', 'delete_file', 'delete_external_link', 'rename_category', 'delete_category', 'toggle_private_category', 'admin_save_site', 'not_found', 'add_feed', 'delete_feed', 'add_link', 'delete_link', 'reorder_links', 'react', 'comment', 'verify_comment', 'comment_moderate', 'comment_delete', 'comment_resend', 'create_tag_type', 'delete_tag_type', 'edit_tags', 'book_save', 'book_delete'];
|
$_noindexActions = ['create', 'edit', 'admin', 'categories', 'diff', 'add_files', 'import_image', 'import_image_step2', 'sources', 'profile', 'delete_file', 'delete_external_link', 'rename_category', 'delete_category', 'toggle_private_category', 'admin_save_site', 'not_found', 'add_feed', 'delete_feed', 'add_link', 'delete_link', 'reorder_links', 'react', 'comment', 'verify_comment', 'comment_moderate', 'comment_delete', 'comment_resend', 'create_tag_type', 'delete_tag_type', 'edit_tags', 'book_save', 'book_delete', 'admin_save_as_groups'];
|
||||||
$metaRobots = in_array($action, $_noindexActions, true) ? 'noindex, nofollow' : null;
|
$metaRobots = in_array($action, $_noindexActions, true) ? 'noindex, nofollow' : null;
|
||||||
unset($_noindexActions);
|
unset($_noindexActions);
|
||||||
|
|
||||||
@@ -2542,6 +2542,27 @@ switch ($action) {
|
|||||||
$adminData['search_log_readable'] = $parser->isReadable();
|
$adminData['search_log_readable'] = $parser->isReadable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($tab === 'stats') {
|
||||||
|
if (!isAdmin()) {
|
||||||
|
http_response_code(403);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
require_once BASE_PATH . '/src/AccessLogParser.php';
|
||||||
|
require_once BASE_PATH . '/src/AsnLookup.php';
|
||||||
|
$accessParser = new AccessLogParser('/var/log/apache2', apacheAccessLog());
|
||||||
|
$accessStats = $accessParser->stats();
|
||||||
|
$adminData['stats_readable'] = $accessParser->isReadable();
|
||||||
|
$adminData['stats_pages'] = array_slice($accessStats['pages'], 0, 30, true);
|
||||||
|
$adminData['stats_books'] = array_slice($accessStats['books'], 0, 20, true);
|
||||||
|
// Lookup AS pour les top 200 IPs
|
||||||
|
$topIps = array_slice($accessStats['ips'], 0, 200, true);
|
||||||
|
$asnMap = (new AsnLookup())->batchLookup(array_keys($topIps));
|
||||||
|
$asList = AsnLookup::aggregateByAs($topIps, $asnMap);
|
||||||
|
$adminData['stats_as'] = $asList;
|
||||||
|
$adminData['stats_as_groups'] = AsnLookup::applyGroups($asList, asGroups());
|
||||||
|
$adminData['as_groups'] = asGroups();
|
||||||
|
}
|
||||||
|
|
||||||
if ($tab === 'categories') {
|
if ($tab === 'categories') {
|
||||||
$adminData['cats'] = $articles->getCategories();
|
$adminData['cats'] = $articles->getCategories();
|
||||||
$adminData['privateCats'] = $articles->getPrivateCategories();
|
$adminData['privateCats'] = $articles->getPrivateCategories();
|
||||||
@@ -2820,6 +2841,30 @@ switch ($action) {
|
|||||||
header('Location: /admin/searches?' . ($ok ? 'saved=1' : 'error=write'));
|
header('Location: /admin/searches?' . ($ok ? 'saved=1' : 'error=write'));
|
||||||
exit;
|
exit;
|
||||||
|
|
||||||
|
case 'admin_save_as_groups':
|
||||||
|
requireAuth();
|
||||||
|
if (!isAdmin() || $_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
http_response_code(403);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
$rawLabels = $_POST['as_group_label'] ?? [];
|
||||||
|
$rawPatterns = $_POST['as_group_patterns'] ?? [];
|
||||||
|
$groups = [];
|
||||||
|
foreach ((array) $rawLabels as $i => $label) {
|
||||||
|
$label = trim((string) $label);
|
||||||
|
if ($label === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$patterns = array_values(array_filter(array_map(
|
||||||
|
'trim',
|
||||||
|
explode("\n", (string) ($rawPatterns[$i] ?? ''))
|
||||||
|
)));
|
||||||
|
$groups[] = ['label' => $label, 'patterns' => $patterns];
|
||||||
|
}
|
||||||
|
$ok = saveSiteSettings(['as_groups' => $groups]);
|
||||||
|
header('Location: /admin/stats?' . ($ok ? 'saved=1' : 'error=write'));
|
||||||
|
exit;
|
||||||
|
|
||||||
case 'admin_create_role':
|
case 'admin_create_role':
|
||||||
requireAuth();
|
requireAuth();
|
||||||
if (!isAdmin() || $_SERVER['REQUEST_METHOD'] !== 'POST') {
|
if (!isAdmin() || $_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -68,6 +68,13 @@ function apacheAccessLog(): string
|
|||||||
return (string)($_ENV['APACHE_ACCESS_LOG'] ?? getenv('APACHE_ACCESS_LOG') ?: '*-access.log');
|
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
|
function saveSiteSettings(array $data): bool
|
||||||
{
|
{
|
||||||
$current = siteSettings();
|
$current = siteSettings();
|
||||||
@@ -86,6 +93,10 @@ function saveSiteSettings(array $data): bool
|
|||||||
$current['posts_per_page'] = $val;
|
$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(
|
return file_put_contents(
|
||||||
siteSettingsPath(),
|
siteSettingsPath(),
|
||||||
json_encode($current, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
json_encode($current, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
||||||
|
|||||||
@@ -0,0 +1,241 @@
|
|||||||
|
<?php
|
||||||
|
$_statsSaved = isset($_GET['saved']);
|
||||||
|
$_statsError = ($_GET['error'] ?? '') === 'write';
|
||||||
|
$_readable = $adminData['stats_readable'] ?? false;
|
||||||
|
$_pages = $adminData['stats_pages'] ?? [];
|
||||||
|
$_books = $adminData['stats_books'] ?? [];
|
||||||
|
$_asList = $adminData['stats_as'] ?? [];
|
||||||
|
$_asGroups = $adminData['stats_as_groups'] ?? [];
|
||||||
|
$_groups = $adminData['as_groups'] ?? [];
|
||||||
|
$_activeGroup = trim($_GET['group'] ?? '');
|
||||||
|
?>
|
||||||
|
|
||||||
|
<?php if ($_statsSaved): ?>
|
||||||
|
<div class="alert alert-success py-2 mb-3">Configuration enregistrée.</div>
|
||||||
|
<?php elseif ($_statsError): ?>
|
||||||
|
<div class="alert alert-danger py-2 mb-3">Impossible d'enregistrer : fichier non accessible en écriture.</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if (!$_readable): ?>
|
||||||
|
<div class="alert alert-warning">
|
||||||
|
Les logs ne sont pas lisibles. Vérifiez le pattern dans l'onglet <a href="/admin/searches">Recherches</a>
|
||||||
|
et que <code>www-data</code> appartient au groupe <code>adm</code>.
|
||||||
|
</div>
|
||||||
|
<?php else: ?>
|
||||||
|
|
||||||
|
<p class="text-muted small mb-4">14 derniers jours · cache 10 min</p>
|
||||||
|
|
||||||
|
<div class="row g-4">
|
||||||
|
|
||||||
|
<!-- Pages -->
|
||||||
|
<div class="col-lg-6">
|
||||||
|
<div class="card h-100">
|
||||||
|
<div class="card-header bg-transparent py-2 small fw-semibold d-flex justify-content-between">
|
||||||
|
<span>Pages les plus visitées</span>
|
||||||
|
<span class="text-muted"><?= count($_pages) ?> URLs</span>
|
||||||
|
</div>
|
||||||
|
<div class="card-body p-0">
|
||||||
|
<?php if (empty($_pages)): ?>
|
||||||
|
<p class="text-muted p-3 mb-0">Aucune donnée.</p>
|
||||||
|
<?php else: ?>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-sm table-hover mb-0 small">
|
||||||
|
<tbody>
|
||||||
|
<?php
|
||||||
|
$maxP = max($_pages) ?: 1;
|
||||||
|
$rankP = 0;
|
||||||
|
foreach ($_pages as $url => $hits):
|
||||||
|
$rankP++;
|
||||||
|
$slug = rawurldecode(substr($url, 6));
|
||||||
|
$pct = round($hits / $maxP * 100);
|
||||||
|
?>
|
||||||
|
<tr>
|
||||||
|
<td class="text-muted ps-3" style="width:2rem"><?= $rankP ?></td>
|
||||||
|
<td>
|
||||||
|
<a href="<?= htmlspecialchars($url) ?>" target="_blank"
|
||||||
|
class="text-decoration-none text-truncate d-block" style="max-width:260px"
|
||||||
|
title="<?= htmlspecialchars($slug) ?>">
|
||||||
|
<?= htmlspecialchars($slug) ?>
|
||||||
|
</a>
|
||||||
|
<div class="progress mt-1" style="height:3px">
|
||||||
|
<div class="progress-bar" style="width:<?= $pct ?>%"></div>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="text-end fw-semibold pe-3"><?= number_format($hits, 0, ',', '\u{202F}') ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Livres -->
|
||||||
|
<div class="col-lg-6">
|
||||||
|
<div class="card h-100">
|
||||||
|
<div class="card-header bg-transparent py-2 small fw-semibold d-flex justify-content-between">
|
||||||
|
<span>Livres consultés</span>
|
||||||
|
<span class="text-muted"><?= count($_books) ?> livres</span>
|
||||||
|
</div>
|
||||||
|
<div class="card-body p-0">
|
||||||
|
<?php if (empty($_books)): ?>
|
||||||
|
<p class="text-muted p-3 mb-0">Aucun accès à <code>/book/</code> dans les logs.</p>
|
||||||
|
<?php else: ?>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-sm table-hover mb-0 small">
|
||||||
|
<tbody>
|
||||||
|
<?php
|
||||||
|
$maxB = max($_books) ?: 1;
|
||||||
|
$rankB = 0;
|
||||||
|
foreach ($_books as $url => $hits):
|
||||||
|
$rankB++;
|
||||||
|
$slug = rawurldecode(substr($url, 6));
|
||||||
|
$pct = round($hits / $maxB * 100);
|
||||||
|
?>
|
||||||
|
<tr>
|
||||||
|
<td class="text-muted ps-3" style="width:2rem"><?= $rankB ?></td>
|
||||||
|
<td>
|
||||||
|
<a href="<?= htmlspecialchars($url) ?>" target="_blank"
|
||||||
|
class="text-decoration-none text-truncate d-block" style="max-width:260px"
|
||||||
|
title="<?= htmlspecialchars($slug) ?>">
|
||||||
|
<?= htmlspecialchars($slug) ?>
|
||||||
|
</a>
|
||||||
|
<div class="progress mt-1" style="height:3px">
|
||||||
|
<div class="progress-bar bg-success" style="width:<?= $pct ?>%"></div>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="text-end fw-semibold pe-3"><?= number_format($hits, 0, ',', '\u{202F}') ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div><!-- /row -->
|
||||||
|
|
||||||
|
<!-- Répartition par réseau -->
|
||||||
|
<div class="card mt-4">
|
||||||
|
<div class="card-header bg-transparent py-2 small fw-semibold d-flex align-items-center gap-3 flex-wrap">
|
||||||
|
<span>Répartition par réseau</span>
|
||||||
|
<?php if (!empty($_groups)): ?>
|
||||||
|
<div class="d-flex gap-1 flex-wrap">
|
||||||
|
<a href="/admin/stats" class="badge <?= $_activeGroup === '' ? 'bg-primary' : 'bg-secondary' ?> text-decoration-none">Tous</a>
|
||||||
|
<?php foreach ($_groups as $g): ?>
|
||||||
|
<a href="/admin/stats?group=<?= rawurlencode($g['label']) ?>"
|
||||||
|
class="badge <?= $_activeGroup === $g['label'] ? 'bg-primary' : 'bg-secondary' ?> text-decoration-none">
|
||||||
|
<?= htmlspecialchars($g['label']) ?>
|
||||||
|
</a>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<a href="/admin/stats?group=Autres"
|
||||||
|
class="badge <?= $_activeGroup === 'Autres' ? 'bg-primary' : 'bg-secondary' ?> text-decoration-none">Autres</a>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
<div class="card-body p-0">
|
||||||
|
<?php
|
||||||
|
// Sélectionner les AS à afficher
|
||||||
|
if ($_activeGroup !== '' && isset($_asGroups[$_activeGroup])) {
|
||||||
|
$displayAs = $_asGroups[$_activeGroup];
|
||||||
|
} else {
|
||||||
|
$displayAs = $_asList;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<?php if (empty($displayAs)): ?>
|
||||||
|
<p class="text-muted p-3 mb-0">
|
||||||
|
<?= empty($_asList) ? 'Aucune IP résolue (LAN ou logs vides).' : 'Aucun AS dans ce groupe.' ?>
|
||||||
|
</p>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php $maxAS = max(array_column($displayAs, 'hits')) ?: 1; ?>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-sm table-hover mb-0 small">
|
||||||
|
<thead class="table-light">
|
||||||
|
<tr>
|
||||||
|
<th class="ps-3" style="width:2rem">#</th>
|
||||||
|
<th>Réseau</th>
|
||||||
|
<th style="width:3rem">Pays</th>
|
||||||
|
<th style="width:5rem" class="text-end pe-3">Visites</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ($displayAs as $i => $as): ?>
|
||||||
|
<tr>
|
||||||
|
<td class="text-muted ps-3"><?= $i + 1 ?></td>
|
||||||
|
<td>
|
||||||
|
<span class="fw-medium"><?= htmlspecialchars($as['name'] ?: '?') ?></span>
|
||||||
|
<?php if ($as['asn'] !== ''): ?>
|
||||||
|
<span class="text-muted ms-1">AS<?= htmlspecialchars($as['asn']) ?></span>
|
||||||
|
<?php endif; ?>
|
||||||
|
<div class="progress mt-1" style="height:3px">
|
||||||
|
<div class="progress-bar bg-info" style="width:<?= round($as['hits'] / $maxAS * 100) ?>%"></div>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="text-muted"><?= htmlspecialchars($as['country']) ?></td>
|
||||||
|
<td class="text-end fw-semibold pe-3"><?= number_format($as['hits'], 0, ',', '\u{202F}') ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php endif; // readable ?>
|
||||||
|
|
||||||
|
<!-- Groupes de réseaux -->
|
||||||
|
<div class="card mt-4" style="max-width:600px">
|
||||||
|
<div class="card-header bg-transparent py-2 small fw-semibold">Groupes de réseaux</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<p class="text-muted small">Regroupez plusieurs réseaux sous un label. Chaque ligne est un motif cherché dans le nom du réseau (insensible à la casse).</p>
|
||||||
|
<form method="post" action="/?action=admin_save_as_groups" id="as-groups-form">
|
||||||
|
<div id="as-groups-list">
|
||||||
|
<?php foreach ($_groups as $gi => $g): ?>
|
||||||
|
<div class="as-group-row border rounded p-3 mb-3">
|
||||||
|
<div class="d-flex align-items-center gap-2 mb-2">
|
||||||
|
<input type="text" name="as_group_label[]" class="form-control form-control-sm"
|
||||||
|
placeholder="Label (ex : Opérateurs FR)"
|
||||||
|
value="<?= htmlspecialchars($g['label']) ?>" required>
|
||||||
|
<button type="button" class="btn btn-outline-danger btn-sm as-group-delete" title="Supprimer">✕</button>
|
||||||
|
</div>
|
||||||
|
<textarea name="as_group_patterns[]" class="form-control form-control-sm font-monospace"
|
||||||
|
rows="3" placeholder="Un motif par ligne ex : Free SAS Orange SFR"><?= htmlspecialchars(implode("\n", $g['patterns'])) ?></textarea>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</div>
|
||||||
|
<div class="d-flex gap-2 mt-2">
|
||||||
|
<button type="button" id="as-group-add" class="btn btn-outline-secondary btn-sm">+ Ajouter un groupe</button>
|
||||||
|
<button type="submit" class="btn btn-primary btn-sm">Enregistrer</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<template id="as-group-tpl">
|
||||||
|
<div class="as-group-row border rounded p-3 mb-3">
|
||||||
|
<div class="d-flex align-items-center gap-2 mb-2">
|
||||||
|
<input type="text" name="as_group_label[]" class="form-control form-control-sm"
|
||||||
|
placeholder="Label (ex : Moteurs de recherche)" required>
|
||||||
|
<button type="button" class="btn btn-outline-danger btn-sm as-group-delete" title="Supprimer">✕</button>
|
||||||
|
</div>
|
||||||
|
<textarea name="as_group_patterns[]" class="form-control form-control-sm font-monospace"
|
||||||
|
rows="3" placeholder="Un motif par ligne ex : Googlebot Bingbot"></textarea>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.getElementById('as-group-add').addEventListener('click', () => {
|
||||||
|
const tpl = document.getElementById('as-group-tpl').content.cloneNode(true);
|
||||||
|
document.getElementById('as-groups-list').appendChild(tpl);
|
||||||
|
});
|
||||||
|
document.getElementById('as-groups-list').addEventListener('click', e => {
|
||||||
|
if (e.target.classList.contains('as-group-delete')) {
|
||||||
|
e.target.closest('.as-group-row').remove();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
Reference in New Issue
Block a user