From 6300728c9367a16bbedc85130f73d2c5c65588c7 Mon Sep 17 00:00:00 2001 From: Cedric Date: Fri, 26 Jun 2026 15:24:16 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 301 +++++++++++++++++++++++++------------------------------- 1 file changed, 134 insertions(+), 167 deletions(-) diff --git a/api.php b/api.php index 4a079e7..6b15100 100644 --- a/api.php +++ b/api.php @@ -59,13 +59,6 @@ function getTmdbApiKey($pdo) { return $row ? decryptData($row['key_value']) : null; } -function getFanartApiKey($pdo) { - $stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'fanart_api_key'"); - $stmt->execute(); - $row = $stmt->fetch(); - return $row ? decryptData($row['key_value']) : null; -} - function httpGet($url, $timeout = 5, $ua = null) { if (!$ua) $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'; if (!function_exists('curl_init')) { @@ -82,37 +75,7 @@ function httpGet($url, $timeout = 5, $ua = null) { CURLOPT_USERAGENT => $ua, CURLOPT_FOLLOWLOCATION => true, CURLOPT_REFERER => 'https://www.cinemapassion.com/', - CURLOPT_HTTPHEADER => [ - 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', - 'Accept-Language: fr-FR,fr;q=0.8', - ], - ]); - $res = curl_exec($ch); - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - return ($code === 200 && is_string($res) && strlen($res) > 0) ? $res : null; -} - -function httpPost($url, $postData, $timeout = 10) { - $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'; - if (!function_exists('curl_init')) return null; - $ch = curl_init($url); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => $timeout, - CURLOPT_CONNECTTIMEOUT => 5, - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_USERAGENT => $ua, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => is_array($postData) ? http_build_query($postData) : $postData, - CURLOPT_REFERER => 'https://www.cinemapassion.com/', - CURLOPT_HTTPHEADER => [ - 'Content-Type: application/x-www-form-urlencoded', - 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', - 'Accept-Language: fr-FR,fr;q=0.8', - 'Origin: https://www.cinemapassion.com' - ], + CURLOPT_HTTPHEADER => ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: fr-FR,fr;q=0.8'], ]); $res = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); @@ -142,76 +105,74 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { $defaultPoster = 'assets/img/default_physical_media.jpg'; $cleanTitle = cleanTitle($title); - if (empty($cleanTitle)) { - return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray']; - } + if (empty($cleanTitle)) return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray']; - $baseUrl = 'http://www.cinemapassion.com'; - - // 1. Initialisation d'une session cURL UNIQUE (Partage des cookies) - // C'est vital car moteur2.php utilise des sessions PHP pour la recherche + $baseUrl = 'https://www.cinemapassion.com'; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, - CURLOPT_FOLLOWLOCATION => true, + CURLOPT_FOLLOWLOCATION => true, // Indispensable si le site redirige directement vers le film CURLOPT_MAXREDIRS => 5, - CURLOPT_TIMEOUT => 10, + CURLOPT_TIMEOUT => 15, CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', - CURLOPT_COOKIEFILE => '', // Active le stockage des cookies en RAM entre les requêtes - CURLOPT_REFERER => $baseUrl . '/', + CURLOPT_COOKIEFILE => '', // Garde les cookies en RAM + CURLOPT_REFERER => 'https://www.cinemapassion.com/', CURLOPT_SSL_VERIFYPEER => false ]); - // 2. Recherche via POST - curl_setopt($ch, CURLOPT_URL, $baseUrl . '/moteur2.php'); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['recherche' => $cleanTitle])); + // 1. Recherche initiale + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recherche.php?recherche=' . urlencode($cleanTitle)); $html = curl_exec($ch); + $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); - // 3. Extraction du lien du film - $filmUrl = null; - if ($html && preg_match('/href=["\']?(?:.*?\/)?(film\/[^"\'\s>]+)["\']?/i', $html, $matches)) { - $filmUrl = $baseUrl . '/' . $matches[1]; + $filmHtml = null; + $regexFilm = '/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(film[\/-][^"\'\s>]+)["\']?/i'; + + // Si la recherche a redirigé directement sur la fiche du film (1 seul résultat) + if (strpos($finalUrl, 'film-') !== false || strpos($finalUrl, '/film/') !== false || preg_match('/jaquette-(?:dvd|blu-ray)-/i', $html)) { + $filmHtml = $html; } else { - // Plan B : Recherche via GET si le POST échoue - curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recherche.php?recherche=' . urlencode($cleanTitle)); - curl_setopt($ch, CURLOPT_POST, false); - $html = curl_exec($ch); - if ($html && preg_match('/href=["\']?(?:.*?\/)?(film\/[^"\'\s>]+)["\']?/i', $html, $matches)) { - $filmUrl = $baseUrl . '/' . $matches[1]; + // Chercher le lien dans les résultats de recherche GET + if ($html && preg_match($regexFilm, $html, $matches)) { + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/' . ltrim($matches[1], './')); + $filmHtml = curl_exec($ch); + } else { + // Tentative via POST si le GET échoue + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/moteur2.php'); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['recherche' => $cleanTitle])); + $htmlPost = curl_exec($ch); + curl_setopt($ch, CURLOPT_POST, false); + + $finalUrlPost = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + if (strpos($finalUrlPost, 'film-') !== false || strpos($finalUrlPost, '/film/') !== false) { + $filmHtml = $htmlPost; + } elseif ($htmlPost && preg_match($regexFilm, $htmlPost, $matches)) { + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/' . ltrim($matches[1], './')); + $filmHtml = curl_exec($ch); + } } } - // 4. Si on a trouvé le film, on charge sa page - if ($filmUrl) { - curl_setopt($ch, CURLOPT_URL, $filmUrl); - curl_setopt($ch, CURLOPT_POST, false); - $filmHtml = curl_exec($ch); - - if ($filmHtml) { - // 5. Chercher le lien de la page contenant la jaquette - if (preg_match('/href=["\']?(?:.*?\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i', $filmHtml, $jaqMatches)) { - $jaqUrl = $baseUrl . '/' . $jaqMatches[1]; - - // Charger la page de la jaquette - curl_setopt($ch, CURLOPT_URL, $jaqUrl); - $jaqHtml = curl_exec($ch); - - // 6. Extraire l'image haute définition (covers_temp/covers...) - if ($jaqHtml && preg_match('/src=["\']?(http:\/\/www\.cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $jaqHtml, $imgMatches)) { - curl_close($ch); - // On convertit l'URL en HTTPS pour éviter les erreurs de contenu mixte sur votre site web - return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray']; - } - } + // Si on a récupéré le code de la fiche film + if ($filmHtml) { + // Chercher la sous-page contenant la jaquette + if (preg_match('/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i', $filmHtml, $jaqMatches)) { + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/' . ltrim($jaqMatches[1], './')); + $jaqHtml = curl_exec($ch); - // Fallback : Si aucune sous-page jaquette n'existe, on cherche l'image directement sur la page du film - if (preg_match('/src=["\']?(http:\/\/www\.cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $filmHtml, $imgMatches)) { - // On s'assure qu'on ne prend pas les miniatures - if (strpos($imgMatches[1], 'miniature') === false) { - curl_close($ch); - return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray']; - } + // Extraire l'image haute définition + if ($jaqHtml && preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $jaqHtml, $imgMatches)) { + curl_close($ch); + return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray']; + } + } + + // Fallback direct sur la fiche du film si la sous-page jaquette n'existe pas + if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $filmHtml, $imgMatches)) { + if (strpos($imgMatches[1], 'miniature') === false && strpos($imgMatches[1], 'vign') === false) { + curl_close($ch); + return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray']; } } } @@ -348,7 +309,7 @@ switch ($action) { ORDER BY id DESC "; $result = $pdo->query($sql)->fetchAll(); - foreach ($result as $row) { + foreach ($result as &$row) { if ($row['rating'] !== null) { $ratingVal = (float)$row['rating']; $row['rating'] = ($ratingVal == floor($ratingVal)) ? (int)$ratingVal : $ratingVal; @@ -448,10 +409,8 @@ switch ($action) { $id = makeStableId('videotheque', $title, $year); - // ✅ UTILISER CINEMAPASSION.COM $cpData = fetchCinemaPassion($title, $year, $ean, $pdo); $poster = $cpData['poster']; - $format = detectFormat($title, $desc); $stmtVideo->execute([ @@ -461,7 +420,6 @@ switch ($action) { $imported++; } } else { - // ✅ CORRECTION CRITIQUE : Initialisation des variables pour les critiques $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), @@ -509,94 +467,103 @@ switch ($action) { // ── ENDPOINT DE DEBUG DÉTAILLÉ ── case 'debug_cinemapassion': $title = $_GET['title'] ?? 'Saw'; - $year = $_GET['year'] ?? ''; - $ean = $_GET['ean'] ?? ''; - $debugInfo = [ 'title' => $title, - 'year' => $year, 'cleanTitle' => cleanTitle($title), 'steps' => [] ]; - // Test 1 : POST sur moteur2.php - $searchRes = httpPost('https://www.cinemapassion.com/moteur2.php', ['recherche' => cleanTitle($title)]); - $debugInfo['steps']['search_post'] = [ - 'success' => !empty($searchRes), - 'length' => $searchRes ? strlen($searchRes) : 0 + $baseUrl = 'https://www.cinemapassion.com'; + $ch = curl_init(); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 5, + CURLOPT_TIMEOUT => 15, + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', + CURLOPT_COOKIEFILE => '', + CURLOPT_REFERER => 'https://www.cinemapassion.com/', + CURLOPT_SSL_VERIFYPEER => false + ]); + + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recherche.php?recherche=' . urlencode(cleanTitle($title))); + $html = curl_exec($ch); + $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + + $debugInfo['steps']['search_get'] = [ + 'final_url' => $finalUrl, + 'success' => !empty($html) ]; - if ($searchRes) { - // Test 2 : Extraction du lien film - if (preg_match('/href=["\']?(?:\.\.\/)?film\/([^"\'\s]+)-(\d+)\.php["\']?/i', $searchRes, $matches)) { - $debugInfo['steps']['film_found'] = [ - 'success' => true, - 'filmName' => $matches[1], - 'filmId' => $matches[2] - ]; - - $filmName = $matches[1]; - $filmId = $matches[2]; - - // Test 3 : Pages de jaquettes - $pagesToTry = [ - "https://www.cinemapassion.com/jaquette-dvd-{$filmName}-{$filmId}.php", - "https://www.cinemapassion.com/jaquette-blu-ray-{$filmName}-{$filmId}.php", - "https://www.cinemapassion.com/film/{$filmName}-{$filmId}.php", - ]; - - foreach ($pagesToTry as $i => $pageUrl) { - $pageRes = httpGet($pageUrl, 10); - $pageDebug = [ - 'url' => $pageUrl, - 'success' => !empty($pageRes), - 'length' => $pageRes ? strlen($pageRes) : 0 - ]; - - if ($pageRes) { - if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i', $pageRes, $imgMatches)) { - $pageDebug['image_found'] = true; - $pageDebug['image_url'] = str_replace('http://', 'https://', $imgMatches[1]); - } elseif (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/lesaffiches\/[^"\'\s>]+\.jpg)["\']?/i', $pageRes, $imgMatches)) { - $pageDebug['affiche_found'] = true; - $pageDebug['image_url'] = str_replace('http://', 'https://', $imgMatches[1]); - } else { - $pageDebug['image_found'] = false; - // Extraire un extrait pour debug - $pageDebug['html_snippet'] = substr($pageRes, 0, 1000); - } - } - - $debugInfo['steps']['pages'][$i] = $pageDebug; - - if (!empty($pageDebug['image_found']) || !empty($pageDebug['affiche_found'])) { - $debugInfo['final_result'] = [ - 'success' => true, - 'poster' => $pageDebug['image_url'], - 'title' => cleanTitle($title), - 'format' => 'Blu-ray' - ]; - echo json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); - exit; - } - } + $filmHtml = null; + $regexFilm = '/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(film[\/-][^"\'\s>]+)["\']?/i'; + + if (strpos($finalUrl, 'film-') !== false || strpos($finalUrl, '/film/') !== false || preg_match('/jaquette-(?:dvd|blu-ray)-/i', $html)) { + $filmHtml = $html; + $debugInfo['steps']['film_found'] = 'Redirection directe ou page film détectée'; + } else { + if ($html && preg_match($regexFilm, $html, $matches)) { + $filmUrl = $baseUrl . '/' . ltrim($matches[1], './'); + $debugInfo['steps']['film_found'] = $filmUrl; + curl_setopt($ch, CURLOPT_URL, $filmUrl); + $filmHtml = curl_exec($ch); } else { - $debugInfo['steps']['film_found'] = [ - 'success' => false, - 'html_snippet' => substr($searchRes, 0, 2000) + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/moteur2.php'); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['recherche' => cleanTitle($title)])); + $htmlPost = curl_exec($ch); + curl_setopt($ch, CURLOPT_POST, false); + $finalUrlPost = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + + $debugInfo['steps']['search_post'] = [ + 'final_url' => $finalUrlPost, + 'success' => !empty($htmlPost) ]; + + if (strpos($finalUrlPost, 'film-') !== false || strpos($finalUrlPost, '/film/') !== false) { + $filmHtml = $htmlPost; + $debugInfo['steps']['film_found'] = 'Redirection via POST'; + } elseif ($htmlPost && preg_match($regexFilm, $htmlPost, $matches)) { + $filmUrl = $baseUrl . '/' . ltrim($matches[1], './'); + $debugInfo['steps']['film_found'] = $filmUrl; + curl_setopt($ch, CURLOPT_URL, $filmUrl); + $filmHtml = curl_exec($ch); + } else { + $debugInfo['steps']['fail'] = 'Aucun lien de film trouvé'; + $debugInfo['html_snippet'] = substr($htmlPost ?: $html, 0, 1000); + } + } + } + + if ($filmHtml) { + if (preg_match('/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i', $filmHtml, $jaqMatches)) { + $jaqUrl = $baseUrl . '/' . ltrim($jaqMatches[1], './'); + $debugInfo['steps']['jaquette_page'] = $jaqUrl; + + curl_setopt($ch, CURLOPT_URL, $jaqUrl); + $jaqHtml = curl_exec($ch); + + if ($jaqHtml && preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $jaqHtml, $imgMatches)) { + $debugInfo['final_result'] = str_replace('http://', 'https://', $imgMatches[1]); + } else { + $debugInfo['steps']['jaquette_fail'] = 'Image covers_temp non trouvée sur la page jaquette'; + } + } + + if (!isset($debugInfo['final_result']) && preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $filmHtml, $imgMatches)) { + if (strpos($imgMatches[1], 'miniature') === false && strpos($imgMatches[1], 'vign') === false) { + $debugInfo['final_result'] = str_replace('http://', 'https://', $imgMatches[1]); + $debugInfo['steps']['fallback'] = 'Trouvé directement sur la page du film'; + } } } if (!isset($debugInfo['final_result'])) { - $debugInfo['final_result'] = [ - 'success' => false, - 'poster' => 'assets/img/default_physical_media.jpg' - ]; + $debugInfo['final_result'] = 'assets/img/default_physical_media.jpg'; } + curl_close($ch); echo json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); exit; - break; } ?> \ No newline at end of file