From 1f235501500f95e1b851367929853f5707847786 Mon Sep 17 00:00:00 2001 From: Cedric Date: Mon, 22 Jun 2026 08:26:38 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/api.php b/api.php index 67ce903..5a47417 100644 --- a/api.php +++ b/api.php @@ -64,6 +64,61 @@ function getTmdbApiKey($pdo) { return $row ? decryptData($row['key_value']) : null; } +function getConfigValue($pdo, $keyName) { + try { + $stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = ?"); + $stmt->execute([$keyName]); + $row = $stmt->fetch(); + return $row ? decryptData($row['key_value']) : null; + } catch (\Exception $e) { return null; } +} + +function fetchPosterByEAN($ean, $pdo) { + if (empty($ean) || strlen($ean) < 8) return null; + + // Vérification du cache + $cacheKey = 'ean_poster_' . md5($ean); + $cached = getCache($pdo, $cacheKey); + if ($cached && !empty($cached['poster'])) return $cached['poster']; + + // 1. UPCitemDB (Pas de clé nécessaire, version trial) + $upcData = fetchUPCitemdb($ean, $pdo); + if ($upcData && !empty($upcData['poster'])) { + setCache($pdo, $cacheKey, ['poster' => $upcData['poster']], 'upc'); + return $upcData['poster']; + } + + // 2. EAN-Search API + $eanSearchKey = getConfigValue($pdo, 'ean_search_key'); + if ($eanSearchKey) { + $url = "https://api.ean-search.org/api?token={$eanSearchKey}&op=barcode-lookup&ean={$ean}&format=json"; + $res = httpGet($url, 5); + if ($res) { + $data = json_decode($res, true); + if (!empty($data[0]['imageUrl'])) { + setCache($pdo, $cacheKey, ['poster' => $data[0]['imageUrl']], 'ean_search'); + return $data[0]['imageUrl']; + } + } + } + + // 3. Barcode Lookup API + $blKey = getConfigValue($pdo, 'barcode_lookup_key'); + if ($blKey) { + $url = "https://api.barcodelookup.com/v3/products?barcode={$ean}&key={$blKey}&formatted=y"; + $res = httpGet($url, 5); + if ($res) { + $data = json_decode($res, true); + if (!empty($data['products'][0]['images'][0])) { + setCache($pdo, $cacheKey, ['poster' => $data['products'][0]['images'][0]], 'barcode_lookup'); + return $data['products'][0]['images'][0]; + } + } + } + + return null; +} + // ─ HTTP avec cURL (timeout court pour vitesse) ── function httpGet($url, $timeout = 5) { if (!function_exists('curl_init')) {