Files
varlog/public/feed.php
T

166 lines
6.6 KiB
PHP

<?php
declare(strict_types=1);
define('BASE_PATH', realpath(__DIR__ . '/../'));
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';
const FEED_PAGE_SIZE = 20;
$articles = new ArticleManager(BASE_PATH . '/data');
$now = time();
$base = rtrim(APP_URL, '/');
$all = array_values(array_filter(
$articles->getAll(publishedOnly: true),
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);
$feedUrlFn = static fn (int $p): string => $base . '/feed' . ($p > 1 ? '?page=' . $p : '');
$lastBuild = '';
foreach ($all as $a) {
$ts = (int)strtotime((string)($a['updated_at'] ?? $a['published_at'] ?? ''));
if ($ts > (int)strtotime($lastBuild ?: '1970-01-01')) {
$lastBuild = date(DATE_RSS, $ts);
}
}
if ($lastBuild === '') {
$lastBuild = date(DATE_RSS);
}
header('Content-Type: application/rss+xml; charset=UTF-8');
header('X-Content-Type-Options: nosniff');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:fh="http://purl.org/syndication/history/1.0">
<channel>
<title>varlog</title>
<link><?= htmlspecialchars($base) ?></link>
<description>Journal personnel de Cédrix — informatique, hack et loisirs techniques.</description>
<language>fr</language>
<lastBuildDate><?= htmlspecialchars($lastBuild) ?></lastBuildDate>
<!-- Flux canonique (toujours page 1) -->
<atom:link href="<?= htmlspecialchars($feedUrlFn(1)) ?>" rel="self" type="application/rss+xml"/>
<?php if ($page > 1): ?>
<!-- Navigation entre pages -->
<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($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): ?>
<!-- Métadonnées de pagination (RFC 5005) -->
<fh:archive/>
<?php endif; ?>
<?php foreach ($items as $article):
$pubDate = date(DATE_RSS, (int)strtotime((string)($article['published_at'] ?? $article['created_at'] ?? '')));
$link = $base . '/post/' . rawurlencode($article['slug'] ?? '');
$title = htmlspecialchars($article['title'] ?? '', ENT_XML1);
$desc = htmlspecialchars(mb_strimwidth(strip_tags($article['content'] ?? ''), 0, 300, '…'), ENT_XML1);
$guid = htmlspecialchars($base . '/post/' . rawurlencode($article['slug'] ?? ''), ENT_XML1);
?>
<item>
<title><?= $title ?></title>
<link><?= htmlspecialchars($link) ?></link>
<description><?= $desc ?></description>
<pubDate><?= htmlspecialchars($pubDate) ?></pubDate>
<guid isPermaLink="true"><?= $guid ?></guid>
</item>
<?php endforeach; ?>
</channel>
</rss>