feat: page HTML pour navigateurs sur /feed (/rss, /rss.xml)

This commit is contained in:
Cedric Abonnel
2026-05-12 10:19:06 +02:00
parent 3a266a87dc
commit 7558fc721f
+81 -12
View File
@@ -4,12 +4,8 @@ declare(strict_types=1);
define('BASE_PATH', realpath(__DIR__ . '/../'));
if (session_status() === PHP_SESSION_NONE) {
$isHttps = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
session_set_cookie_params(['lifetime' => 0, 'path' => '/', 'secure' => $isHttps, 'httponly' => true, 'samesite' => 'Lax']);
session_start();
}
require_once BASE_PATH . '/bootstrap.php';
require_once BASE_PATH . '/src/helpers.php';
require_once BASE_PATH . '/src/auth.php';
require_once BASE_PATH . '/config/config.php';
require_once BASE_PATH . '/src/ArticleManager.php';
@@ -26,12 +22,85 @@ $all = array_values(array_filter(
static fn (array $a): bool => strtotime((string)($a['published_at'] ?? '')) <= $now
));
// ─── Détection navigateur ────────────────────────────────────────────────────
$accept = $_SERVER['HTTP_ACCEPT'] ?? '';
$isBrowser = str_contains($accept, 'text/html')
&& !str_contains($accept, 'application/rss+xml');
if ($isBrowser) {
require_once BASE_PATH . '/src/Parsedown.php';
$Parsedown = new Parsedown();
$feedUrl = $base . '/feed';
$recentItems = array_slice($all, 0, 10);
ob_start();
?>
<div class="row justify-content-center">
<div class="col-lg-7">
<div class="mb-5">
<h1 class="h3 mb-1">Flux RSS — varlog</h1>
<p class="text-muted mb-0">Journal personnel de Cédrix — informatique, hack et loisirs techniques.</p>
</div>
<div class="card mb-5">
<div class="card-body">
<h2 class="h6 fw-semibold mb-2">S'abonner</h2>
<p class="text-muted small mb-3">
Copiez cette URL dans votre lecteur RSS (Miniflux, Feedly, NetNewsWire…)
pour recevoir les nouveaux articles automatiquement.
</p>
<div class="input-group">
<input type="text" class="form-control font-monospace small"
value="<?= htmlspecialchars($feedUrl) ?>"
id="rss-url-input" readonly>
<button class="btn btn-outline-secondary btn-sm"
onclick="navigator.clipboard.writeText(document.getElementById('rss-url-input').value).then(()=>{this.textContent='Copié ✓';setTimeout(()=>this.textContent='Copier',1500)})"
type="button">Copier</button>
</div>
</div>
</div>
<h2 class="h5 mb-3">Derniers articles</h2>
<div class="d-flex flex-column gap-3">
<?php foreach ($recentItems as $a):
$slug = $a['slug'] ?? '';
$pubDate = date('d/m/Y', (int)strtotime((string)($a['published_at'] ?? $a['created_at'] ?? '')));
$preview = mb_strimwidth(strip_tags($Parsedown->text($a['content'] ?? '')), 0, 100, '…');
$cat = trim($a['category'] ?? '');
?>
<a href="/post/<?= rawurlencode($slug) ?>" class="text-decoration-none text-reset d-block p-3 border rounded">
<div class="d-flex justify-content-between align-items-start gap-2 mb-1">
<span class="fw-semibold"><?= htmlspecialchars($a['title'] ?? '') ?></span>
<span class="text-muted small text-nowrap"><?= $pubDate ?></span>
</div>
<?php if ($cat !== ''): ?>
<span class="badge bg-secondary fw-normal mb-1"><?= htmlspecialchars($cat) ?></span>
<?php endif; ?>
<p class="text-muted small mb-0"><?= htmlspecialchars($preview) ?></p>
</a>
<?php endforeach; ?>
</div>
</div>
</div>
<?php
$content = ob_get_clean();
$title = 'Flux RSS — varlog';
$canonical = $base . '/feed';
$metaRobots = 'noindex, follow';
include BASE_PATH . '/templates/layout.php';
exit;
}
// ─── Flux XML pour les lecteurs RSS ─────────────────────────────────────────
$total = count($all);
$lastPage = max(1, (int)ceil($total / FEED_PAGE_SIZE));
$page = max(1, min($lastPage, (int)($_GET['page'] ?? 1)));
$items = array_slice($all, ($page - 1) * FEED_PAGE_SIZE, FEED_PAGE_SIZE);
$feedUrl = static fn (int $p): string => $base . '/feed' . ($p > 1 ? '?page=' . $p : '');
$feedUrlFn = static fn (int $p): string => $base . '/feed' . ($p > 1 ? '?page=' . $p : '');
$lastBuild = '';
foreach ($all as $a) {
@@ -60,16 +129,16 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
<lastBuildDate><?= htmlspecialchars($lastBuild) ?></lastBuildDate>
<!-- Flux canonique (toujours page 1) -->
<atom:link href="<?= htmlspecialchars($feedUrl(1)) ?>" rel="self" type="application/rss+xml"/>
<atom:link href="<?= htmlspecialchars($feedUrlFn(1)) ?>" rel="self" type="application/rss+xml"/>
<?php if ($page > 1): ?>
<!-- Navigation entre pages -->
<atom:link href="<?= htmlspecialchars($feedUrl(1)) ?>" rel="first" type="application/rss+xml"/>
<atom:link href="<?= htmlspecialchars($feedUrl($page - 1)) ?>" rel="previous" type="application/rss+xml"/>
<atom:link href="<?= htmlspecialchars($feedUrlFn(1)) ?>" rel="first" type="application/rss+xml"/>
<atom:link href="<?= htmlspecialchars($feedUrlFn($page - 1)) ?>" rel="previous" type="application/rss+xml"/>
<?php endif; ?>
<?php if ($page < $lastPage): ?>
<atom:link href="<?= htmlspecialchars($feedUrl($page + 1)) ?>" rel="next" type="application/rss+xml"/>
<atom:link href="<?= htmlspecialchars($feedUrl($lastPage)) ?>" rel="last" type="application/rss+xml"/>
<atom:link href="<?= htmlspecialchars($feedUrlFn($page + 1)) ?>" rel="next" type="application/rss+xml"/>
<atom:link href="<?= htmlspecialchars($feedUrlFn($lastPage)) ?>" rel="last" type="application/rss+xml"/>
<?php endif; ?>
<?php if ($lastPage > 1): ?>