feat #64 : onglet Statistiques — pages, livres, répartition AS #65

Merged
cedricAbonnel merged 2 commits from feat/books into main 2026-05-14 22:50:48 +00:00
11 changed files with 1088 additions and 1 deletions
+3
View File
@@ -15,6 +15,9 @@ RewriteRule ^ - [L]
# URL propre pour les articles : /post/<slug>
RewriteRule ^post/([a-z0-9][a-z0-9-]*)/?$ /index.php?action=view&slug=$1 [L,QSA]
# Livres : /book/<slug>
RewriteRule ^book/([a-z0-9][a-z0-9-]*)/?$ /index.php?action=book&book_slug=$1 [L,QSA]
# Filtre par catégorie : /categorie/<nom>
RewriteRule ^categorie/(.+?)/?$ /index.php?cat=$1 [L,QSA,B]
+168
View File
@@ -1807,3 +1807,171 @@ footer.mt-5 { margin-top: 0 !important; }
color: var(--vl-muted);
margin-top: 0.15rem;
}
/* ─── Livres ─────────────────────────────────────────────────────── */
/* Bandeau dans un article appartenant à un livre */
.book-article-banner {
border-radius: var(--vl-radius);
background: var(--vl-accent-soft);
border: 1px solid rgba(79,70,229,.18);
overflow: hidden;
}
.book-article-banner-link {
display: flex;
align-items: center;
gap: 0.6rem;
padding: 0.6rem 1rem;
text-decoration: none;
color: var(--vl-accent);
transition: background 0.15s;
}
.book-article-banner-link:hover {
background: rgba(79,70,229,.08);
color: var(--vl-accent-dark);
}
.book-article-banner-icon { font-size: 1.1rem; flex-shrink: 0; }
.book-article-banner-text { flex: 1; font-size: 0.875rem; }
.book-article-banner-cta { font-size: 0.8rem; opacity: .75; white-space: nowrap; }
/* Navigation précédent/suivant en bas d'article */
.book-chapter-nav {
border-top: 1px solid var(--vl-border);
margin-top: 1.5rem;
padding-top: 1rem;
}
.book-chapter-nav-inner {
display: flex;
gap: 0.75rem;
align-items: stretch;
}
.book-nav-btn {
flex: 1;
display: flex;
flex-direction: column;
padding: 0.65rem 0.875rem;
background: var(--vl-surface);
border: 1px solid var(--vl-border);
border-radius: var(--vl-radius);
text-decoration: none;
color: var(--vl-text);
transition: border-color 0.15s, box-shadow 0.15s;
min-width: 0;
}
.book-nav-btn:hover {
border-color: var(--vl-accent);
box-shadow: var(--vl-shadow-sm);
color: var(--vl-text);
}
.book-nav-btn--disabled {
opacity: .45;
cursor: default;
pointer-events: none;
}
.book-nav-btn--next { text-align: right; }
.book-nav-dir {
font-size: 0.72rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: .04em;
color: var(--vl-muted);
display: block;
}
.book-nav-title {
font-size: 0.875rem;
font-weight: 500;
display: block;
margin-top: 0.15rem;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.book-nav-toc {
display: flex;
align-items: center;
justify-content: center;
padding: 0.5rem 0.75rem;
border: 1px solid var(--vl-border);
border-radius: var(--vl-radius);
color: var(--vl-muted);
text-decoration: none;
font-size: 1.1rem;
transition: border-color 0.15s, color 0.15s;
flex-shrink: 0;
}
.book-nav-toc:hover {
border-color: var(--vl-accent);
color: var(--vl-accent);
}
/* Page sommaire d'un livre (/book/<slug>) */
.book-page { max-width: 720px; margin: 0 auto; padding: 2rem 0; }
.book-label {
font-size: 0.75rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: .08em;
color: var(--vl-accent);
}
.book-chapters {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.book-chapter-link {
display: flex;
align-items: center;
gap: 0.875rem;
padding: 0.75rem 1rem;
background: var(--vl-surface);
border: 1px solid var(--vl-border);
border-radius: var(--vl-radius);
text-decoration: none;
color: var(--vl-text);
transition: border-color 0.15s, box-shadow 0.15s;
}
.book-chapter-link:hover {
border-color: var(--vl-accent);
box-shadow: var(--vl-shadow-sm);
color: var(--vl-text);
}
.book-chapter-num {
width: 1.75rem;
height: 1.75rem;
display: flex;
align-items: center;
justify-content: center;
background: var(--vl-accent-soft);
color: var(--vl-accent);
border-radius: 50%;
font-size: 0.8rem;
font-weight: 700;
flex-shrink: 0;
}
.book-chapter-thumb {
width: 56px;
height: 44px;
border-radius: 6px;
flex-shrink: 0;
background: var(--vl-accent-soft);
background-size: cover;
background-position: center;
}
.book-chapter-body { flex: 1; min-width: 0; }
.book-chapter-title {
font-size: 0.9375rem;
font-weight: 600;
line-height: 1.3;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.book-chapter-meta {
font-size: 0.78rem;
color: var(--vl-muted);
margin-top: 0.15rem;
}
+46 -1
View File
@@ -43,7 +43,7 @@ $action = $_GET['action'] ?? 'list';
$uuid = $_GET['uuid'] ?? '';
$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;
unset($_noindexActions);
@@ -2542,6 +2542,27 @@ switch ($action) {
$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') {
$adminData['cats'] = $articles->getCategories();
$adminData['privateCats'] = $articles->getPrivateCategories();
@@ -2820,6 +2841,30 @@ switch ($action) {
header('Location: /admin/searches?' . ($ok ? 'saved=1' : 'error=write'));
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':
requireAuth();
if (!isAdmin() || $_SERVER['REQUEST_METHOD'] !== 'POST') {
+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;
}
}
+131
View File
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
class BookManager
{
public function __construct(private string $booksDir)
{
}
// ------------------------------------------------------------------ //
// Lecture
// ------------------------------------------------------------------ //
public function getAll(): array
{
$books = [];
if (!is_dir($this->booksDir)) {
return $books;
}
foreach (scandir($this->booksDir) as $file) {
if (!str_ends_with($file, '.json')) {
continue;
}
$raw = file_get_contents($this->booksDir . '/' . $file);
if ($raw === false) {
continue;
}
$book = json_decode($raw, true);
if (!is_array($book) || empty($book['slug'])) {
continue;
}
$books[] = $book;
}
usort($books, static fn ($a, $b) => strcmp($a['title'] ?? '', $b['title'] ?? ''));
return $books;
}
public function getBySlug(string $slug): ?array
{
$path = $this->bookPath($slug);
if (!file_exists($path)) {
return null;
}
$raw = file_get_contents($path);
if ($raw === false) {
return null;
}
$book = json_decode($raw, true);
return is_array($book) && !empty($book['slug']) ? $book : null;
}
/**
* Cherche dans quel livre se trouve un article (par son slug).
* Retourne le contexte complet ou null si l'article n'appartient à aucun livre.
*
* @return array{book: array, position: int, total: int, prev: ?string, next: ?string}|null
*/
public function findForArticle(string $articleSlug): ?array
{
foreach ($this->getAll() as $book) {
$arts = $book['articles'] ?? [];
$pos = array_search($articleSlug, $arts, true);
if ($pos === false) {
continue;
}
$pos = (int) $pos;
return [
'book' => $book,
'position' => $pos + 1,
'total' => count($arts),
'prev' => $pos > 0 ? $arts[$pos - 1] : null,
'next' => $pos < count($arts) - 1 ? $arts[$pos + 1] : null,
];
}
return null;
}
// ------------------------------------------------------------------ //
// Écriture
// ------------------------------------------------------------------ //
public function save(array $book): void
{
$slug = $this->sanitizeSlug($book['slug'] ?? '');
if ($slug === '') {
return;
}
$book['slug'] = $slug;
$book['articles'] = array_values(array_filter(array_map('strval', $book['articles'] ?? [])));
if (!is_dir($this->booksDir)) {
mkdir($this->booksDir, 0755, true);
}
file_put_contents(
$this->bookPath($slug),
json_encode($book, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n"
);
}
public function delete(string $slug): void
{
$path = $this->bookPath($slug);
if (file_exists($path)) {
@unlink($path);
}
}
// ------------------------------------------------------------------ //
// Helpers
// ------------------------------------------------------------------ //
private function bookPath(string $slug): string
{
return $this->booksDir . '/' . $slug . '.json';
}
private function sanitizeSlug(string $slug): string
{
$map = [
'à' => 'a', 'â' => 'a', 'ä' => 'a',
'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
'î' => 'i', 'ï' => 'i',
'ô' => 'o', 'ö' => 'o',
'ù' => 'u', 'û' => 'u', 'ü' => 'u',
'ç' => 'c', 'æ' => 'ae', 'œ' => 'oe',
];
$slug = mb_strtolower(strtr(trim($slug), $map), 'UTF-8');
$slug = (string) preg_replace('/[^a-z0-9]+/', '-', $slug);
return trim($slug, '-');
}
}
+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)
+10
View File
@@ -69,6 +69,10 @@ function adminStatusBadge(array $a, int $now): string
<a class="nav-link <?= $tab === 'books' ? 'active' : '' ?>"
href="/admin/books">Livres</a>
</li>
<li class="nav-item">
<a class="nav-link <?= $tab === 'stats' ? 'active' : '' ?>"
href="/admin/stats">Statistiques</a>
</li>
<?php endif; ?>
</ul>
@@ -1300,6 +1304,12 @@ foreach (COLOR_PALETTE_16 as $_i => $_rgb):
<?php endif; ?>
<?php if ($tab === 'stats' && isAdmin()): ?>
<?php include __DIR__ . '/admin_stats.php'; ?>
<?php endif; ?>
<?php
$content = ob_get_clean();
$title = 'Administration — ' . siteTitle();
+241
View File
@@ -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&#10;ex : Free SAS&#10;Orange&#10;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&#10;ex : Googlebot&#10;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>
+62
View File
@@ -0,0 +1,62 @@
<?php ob_start(); ?>
<div class="book-page">
<div class="book-header mb-4">
<p class="book-label">Livre</p>
<h1 class="h2 mb-2"><?= htmlspecialchars($book['title']) ?></h1>
<?php if (!empty($book['description'])): ?>
<p class="lead text-muted"><?= htmlspecialchars($book['description']) ?></p>
<?php endif; ?>
<p class="text-muted small"><?= count($bookArticles) ?> page<?= count($bookArticles) > 1 ? 's' : '' ?></p>
</div>
<?php if (empty($bookArticles)): ?>
<p class="text-muted">Ce livre ne contient pas encore de pages publiées.</p>
<?php else: ?>
<ol class="book-chapters">
<?php foreach ($bookArticles as $i => $a):
$cat = trim($a['category'] ?? '');
$gradient = coverGradient($cat !== '' ? $cat : $a['uuid'], $allCats);
$cover = $a['cover'] ?? '';
$date = $a['published_at'] ? date('d/m/Y', strtotime((string)$a['published_at'])) : '';
?>
<li class="book-chapter">
<a href="/post/<?= rawurlencode($a['slug'] ?? '') ?>" class="book-chapter-link">
<span class="book-chapter-num"><?= $i + 1 ?></span>
<div class="book-chapter-thumb" style="<?= $cover !== ''
? 'background-image:url(/file?uuid=' . rawurlencode($a['uuid']) . '&name=' . rawurlencode($cover) . ');background-size:cover;background-position:center'
: 'background:' . htmlspecialchars($gradient) ?>">
</div>
<div class="book-chapter-body">
<div class="book-chapter-title"><?= htmlspecialchars($a['title'] ?? '') ?></div>
<div class="book-chapter-meta">
<?php if ($cat !== ''): ?><?= htmlspecialchars($cat) ?><?php endif; ?>
<?php if ($cat !== '' && $date !== ''): ?> · <?php endif; ?>
<?php if ($date !== ''): ?><?= $date ?><?php endif; ?>
</div>
<?php if (!$a['published']): ?>
<span class="badge bg-secondary small">Brouillon</span>
<?php endif; ?>
</div>
</a>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
<?php if (function_exists('isAdmin') && isAdmin()): ?>
<div class="mt-4 text-end">
<a href="/admin/books?edit=<?= rawurlencode($book['slug']) ?>" class="btn btn-sm btn-outline-secondary">
✎ Modifier ce livre
</a>
</div>
<?php endif; ?>
</div>
<?php
$content = ob_get_clean();
$title = htmlspecialchars($book['title']) . ' — ' . siteTitle();
$canonical = rtrim(APP_URL, '/') . '/book/' . rawurlencode($book['slug']);
include __DIR__ . '/layout.php';
+48
View File
@@ -70,6 +70,19 @@ $externalLinks = $article['external_links'] ?? [];
<!-- Colonne principale -->
<div class="col">
<?php if (!empty($bookContext)): ?>
<div class="book-article-banner mb-3">
<a href="/book/<?= rawurlencode($bookContext['book']['slug']) ?>" class="book-article-banner-link">
<span class="book-article-banner-icon">📖</span>
<span class="book-article-banner-text">
Chapitre <?= $bookContext['position'] ?>/<?= $bookContext['total'] ?> —
<strong><?= htmlspecialchars($bookContext['book']['title']) ?></strong>
</span>
<span class="book-article-banner-cta">Voir le sommaire →</span>
</a>
</div>
<?php endif; ?>
<div class="card mb-4">
<?php if (!$article['published']): ?>
<div class="draft-ribbon">Brouillon</div>
@@ -163,6 +176,41 @@ $hasSources = (!empty($externalLinks) || !empty($files))
<div class="card-text post-content">
<?= $_renderedContent ?>
</div>
<?php if (!empty($bookContext)): ?>
<nav class="book-chapter-nav">
<div class="book-chapter-nav-inner">
<?php if (!empty($bookContext['prev_article'])): ?>
<a href="/post/<?= rawurlencode($bookContext['prev_article']['slug'] ?? '') ?>"
class="book-nav-btn book-nav-btn--prev">
<span class="book-nav-dir">← Précédent</span>
<span class="book-nav-title"><?= htmlspecialchars($bookContext['prev_article']['title'] ?? '') ?></span>
</a>
<?php else: ?>
<span class="book-nav-btn book-nav-btn--prev book-nav-btn--disabled">
<span class="book-nav-dir">Premier chapitre</span>
</span>
<?php endif; ?>
<a href="/book/<?= rawurlencode($bookContext['book']['slug']) ?>"
class="book-nav-toc" title="Sommaire du livre">
</a>
<?php if (!empty($bookContext['next_article'])): ?>
<a href="/post/<?= rawurlencode($bookContext['next_article']['slug'] ?? '') ?>"
class="book-nav-btn book-nav-btn--next">
<span class="book-nav-dir">Suivant →</span>
<span class="book-nav-title"><?= htmlspecialchars($bookContext['next_article']['title'] ?? '') ?></span>
</a>
<?php else: ?>
<span class="book-nav-btn book-nav-btn--next book-nav-btn--disabled">
<span class="book-nav-dir">Dernier chapitre</span>
</span>
<?php endif; ?>
</div>
</nav>
<?php endif; ?>
</div>
</div>