diff options
| author | sillylaird <sillyfanboy@gmail.com> | 2026-09-03 00:33:59 +0000 |
|---|---|---|
| committer | sillylaird <sillyfanboy@gmail.com> | 2026-09-03 00:33:59 +0000 |
| commit | 898b52edcb47bcb3e9d6106e74ca73e74ea01e70 (patch) | |
| tree | 85c6ee5ad58b860144551184d4cf86b560c62b91 /api/guestbook-recent.php | |
| download | www-898b52edcb47bcb3e9d6106e74ca73e74ea01e70.tar.gz www-898b52edcb47bcb3e9d6106e74ca73e74ea01e70.zip | |
Diffstat (limited to '')
| -rw-r--r-- | api/guestbook-recent.php | 78 |
1 files changed, 78 insertions, 0 deletions
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 @@ +<?php +/** + * Guestbook recent comments — JSON proxy for homepage inline display. + * Fetches from guestbook.sillylaird.ca/api.php (server-to-server, no CORS). + */ +require_once __DIR__ . '/../partials/proxy_helpers.php'; + +header('Content-Type: application/json; charset=utf-8'); +header('Cache-Control: public, max-age=90'); + +$limit = min(max((int)($_GET['limit'] ?? 8), 1), 25); + +$cacheDir = sys_get_temp_dir() . '/sillylaird_guestbook'; +if (!is_dir($cacheDir)) { + @mkdir($cacheDir, 0700, true); +} +$cacheFile = $cacheDir . '/recent_' . $limit . '.json'; +$freshTtl = 90; +$staleTtl = 3600; + +$emit = static function (string $json, int $code = 200): void { + http_response_code($code); + echo $json; + exit; +}; + +if (is_file($cacheFile) && (time() - filemtime($cacheFile)) < $freshTtl) { + $emit((string)file_get_contents($cacheFile)); +} + +if (!proxy_rate_limit('guestbook_recent', 40, 60)) { + if (is_file($cacheFile) && (time() - filemtime($cacheFile)) < $staleTtl) { + $emit((string)file_get_contents($cacheFile)); + } + $emit(json_encode(['error' => 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 |
