* * 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": , // total posts in the feed * "new_since": , // 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);