v3
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* index_config/updates_feed.php
|
||||
* ─────────────────────────────────────────────────────────────────────────────
|
||||
* Lightweight JSON endpoint consumed exclusively by the JS auto-refresh in
|
||||
* index.php. Returns the full updates array (JS handles pagination client-side
|
||||
* so every poll can detect new posts regardless of which page is visible).
|
||||
*
|
||||
* Usage: GET index_config/updates_feed.php
|
||||
* GET index_config/updates_feed.php?since=<ISO-timestamp>
|
||||
*
|
||||
* The optional ?since= parameter lets the client ask "give me only posts newer
|
||||
* than this timestamp" so it can detect new arrivals without comparing full
|
||||
* arrays.
|
||||
*
|
||||
* Response:
|
||||
* {
|
||||
* "ok": true,
|
||||
* "total": <int>, // total posts in the feed
|
||||
* "new_since": <int>, // posts newer than ?since= (0 if not supplied)
|
||||
* "items": [ … ] // full array, newest first
|
||||
* }
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
|
||||
// ── Load feed ─────────────────────────────────────────────────────────────────
|
||||
$posts = [];
|
||||
if (file_exists(UPDATES_JSON)) {
|
||||
$raw = json_decode(file_get_contents(UPDATES_JSON), true);
|
||||
if (is_array($raw)) $posts = $raw;
|
||||
}
|
||||
|
||||
// ── Count posts newer than ?since= ───────────────────────────────────────────
|
||||
$since = trim($_GET['since'] ?? '');
|
||||
$new_count = 0;
|
||||
|
||||
if ($since !== '') {
|
||||
try {
|
||||
$since_dt = new DateTime($since, new DateTimeZone('UTC'));
|
||||
foreach ($posts as $p) {
|
||||
try {
|
||||
$post_dt = new DateTime($p['date'] ?? '', new DateTimeZone('UTC'));
|
||||
if ($post_dt > $since_dt) $new_count++;
|
||||
} catch (Exception $e) { /* skip malformed dates */ }
|
||||
}
|
||||
} catch (Exception $e) { /* ignore bad since param */ }
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'total' => count($posts),
|
||||
'new_since' => $new_count,
|
||||
'items' => $posts,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
Reference in New Issue
Block a user