From 898b52edcb47bcb3e9d6106e74ca73e74ea01e70 Mon Sep 17 00:00:00 2001 From: sillylaird Date: Thu, 3 Sep 2026 00:33:59 +0000 Subject: import live www.sillylaird.ca webroot --- api/guestbook-recent.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 api/guestbook-recent.php (limited to 'api/guestbook-recent.php') diff --git a/api/guestbook-recent.php b/api/guestbook-recent.php new file mode 100644 index 0000000..c4ef59e --- /dev/null +++ b/api/guestbook-recent.php @@ -0,0 +1,78 @@ + true, 'message' => 'rate limited']), 429); +} + +$url = 'https://guestbook.sillylaird.ca/api.php?limit=' . $limit; +$ctx = stream_context_create([ + 'http' => [ + 'timeout' => 8, + 'user_agent' => 'SillyLaird-GuestbookProxy/1.0', + 'ignore_errors' => true, + ], + 'ssl' => [ + 'verify_peer' => true, + 'verify_peer_name' => true, + ], +]); + +$raw = @file_get_contents($url, false, $ctx); +if ($raw === false) { + if (is_file($cacheFile)) { + $emit((string)file_get_contents($cacheFile)); + } + $emit(json_encode(['error' => true, 'message' => 'upstream unavailable']), 502); +} + +$data = json_decode($raw, true); +if (!is_array($data) || !isset($data['entries']) || !is_array($data['entries'])) { + if (is_file($cacheFile)) { + $emit((string)file_get_contents($cacheFile)); + } + $emit(json_encode(['error' => true, 'message' => 'invalid upstream response']), 502); +} + +$out = json_encode([ + 'entries' => array_slice($data['entries'], 0, $limit), + 'total' => (int)($data['total'] ?? count($data['entries'])), + 'fetched' => time(), +], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + +$tmp = $cacheFile . '.tmp'; +if (@file_put_contents($tmp, $out, LOCK_EX) !== false) { + @rename($tmp, $cacheFile); +} + +$emit($out); \ No newline at end of file -- cgit v1.2.3