+1
-88
@@ -7,46 +7,7 @@ if (session_status() === PHP_SESSION_NONE) {
|
||||
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
function decode_firestore_value($value) {
|
||||
if (!is_array($value)) return $value;
|
||||
if (isset($value['stringValue'])) return $value['stringValue'];
|
||||
if (isset($value['integerValue'])) return (int)$value['integerValue'];
|
||||
if (isset($value['doubleValue'])) return (float)$value['doubleValue'];
|
||||
if (isset($value['booleanValue'])) return (bool)$value['booleanValue'];
|
||||
if (isset($value['nullValue'])) return null;
|
||||
if (isset($value['arrayValue']['values'])) {
|
||||
return array_map('decode_firestore_value', $value['arrayValue']['values']);
|
||||
}
|
||||
if (isset($value['mapValue']['fields'])) {
|
||||
return array_map('decode_firestore_value', $value['mapValue']['fields']);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function decode_firestore_document(array $doc): array {
|
||||
if (!isset($doc['fields'])) return [];
|
||||
return array_map('decode_firestore_value', $doc['fields']);
|
||||
}
|
||||
|
||||
function firestore_get(string $url): ?array {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200 || !$response) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
function load_recipes_local(): array {
|
||||
function load_recipes(): array {
|
||||
$path = __DIR__ . '/data/recipes.json';
|
||||
if (!file_exists($path)) {
|
||||
return [];
|
||||
@@ -67,54 +28,6 @@ function load_recipes_local(): array {
|
||||
return $data;
|
||||
}
|
||||
|
||||
function load_recipes(): array {
|
||||
$config = get_firebase_config();
|
||||
$projectId = $config['projectId'] ?? '';
|
||||
if (empty($projectId)) {
|
||||
return load_recipes_local();
|
||||
}
|
||||
|
||||
$url = "https://firestore.googleapis.com/v1/projects/{$projectId}/databases/(default)/documents/recipes?pageSize=100";
|
||||
$res = firestore_get($url);
|
||||
if (!$res || !isset($res['documents'])) {
|
||||
return load_recipes_local();
|
||||
}
|
||||
|
||||
$recipes = [];
|
||||
foreach ($res['documents'] as $doc) {
|
||||
$decoded = decode_firestore_document($doc);
|
||||
if (!empty($decoded)) {
|
||||
if (!empty($decoded['hero']) && is_string($decoded['hero'])) {
|
||||
$decoded['hero'] = normalize_asset_path($decoded['hero']);
|
||||
}
|
||||
$recipes[] = $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
return $recipes;
|
||||
}
|
||||
|
||||
function load_recipe_by_slug(string $slug): ?array {
|
||||
$config = get_firebase_config();
|
||||
$projectId = $config['projectId'] ?? '';
|
||||
if (empty($projectId)) {
|
||||
return find_recipe_by_slug(load_recipes_local(), $slug);
|
||||
}
|
||||
|
||||
$url = "https://firestore.googleapis.com/v1/projects/{$projectId}/databases/(default)/documents/recipes/" . urlencode($slug);
|
||||
$res = firestore_get($url);
|
||||
if (!$res || isset($res['error'])) {
|
||||
return find_recipe_by_slug(load_recipes_local(), $slug);
|
||||
}
|
||||
|
||||
$decoded = decode_firestore_document($res);
|
||||
if (!empty($decoded['hero']) && is_string($decoded['hero'])) {
|
||||
$decoded['hero'] = normalize_asset_path($decoded['hero']);
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
function save_recipes(array $recipes): bool {
|
||||
$path = __DIR__ . '/data/recipes.json';
|
||||
$json = json_encode($recipes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
Reference in New Issue
Block a user