98 lines
2.8 KiB
PHP
98 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
require_once __DIR__ . '/menu_helpers.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
http_response_code(405);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Method not allowed',
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$config = require __DIR__ . '/../config/menu.php';
|
|
$languages = is_array($config['languages'] ?? null) ? $config['languages'] : [];
|
|
$lang = strtolower(trim((string) ($_GET['lang'] ?? 'pl')));
|
|
|
|
if ($lang === '' || !isset($languages[$lang])) {
|
|
http_response_code(422);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Unsupported language',
|
|
'allowed' => array_keys($languages),
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$langConfig = $languages[$lang];
|
|
$source = (string) ($langConfig['source'] ?? 'upstream');
|
|
$ttl = max(60, (int) ($config['ttl_seconds'] ?? 86400));
|
|
$timeout = max(3, (int) ($config['timeout_seconds'] ?? 12));
|
|
$cacheFile = menuCachePath($lang);
|
|
|
|
$cached = readMenuCache($cacheFile);
|
|
$now = time();
|
|
$cachedAtTs = 0;
|
|
if ($cached && !empty($cached['cachedAt'])) {
|
|
$cachedAtTs = strtotime((string) $cached['cachedAt']) ?: 0;
|
|
}
|
|
$isFresh = $cached && $cachedAtTs > 0 && ($now - $cachedAtTs) < $ttl;
|
|
|
|
if ($isFresh) {
|
|
echo json_encode($cached, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|
|
|
|
if ($source === 'ai') {
|
|
if ($cached) {
|
|
// Stary AI-cache jest OK dłużej niż TTL — cron i tak odświeża raz/dzień.
|
|
echo json_encode($cached, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|
|
|
|
$from = strtolower((string) ($langConfig['from'] ?? 'pl'));
|
|
$fallback = readMenuCache(menuCachePath($from));
|
|
if ($fallback) {
|
|
$fallback['lang'] = $lang;
|
|
$fallback['source'] = 'ai_fallback_pl';
|
|
echo json_encode($fallback, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|
|
|
|
http_response_code(503);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'AI menu not available yet — run scripts/refresh_menu_cache.php',
|
|
'lang' => $lang,
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$base = rtrim((string) ($config['upstream_base'] ?? ''), '/');
|
|
$url = $base . '/' . rawurlencode($lang);
|
|
$raw = fetchUpstreamMenu($url, $timeout);
|
|
|
|
if ($raw === null) {
|
|
if ($cached) {
|
|
echo json_encode($cached, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|
|
http_response_code(502);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Failed to fetch menu',
|
|
'lang' => $lang,
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$mapped = mapUpstreamMenu($raw, $lang, 'upstream');
|
|
writeMenuCache($cacheFile, $mapped);
|
|
|
|
echo json_encode($mapped, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|