"l99_screenshots_dir_missing"]); exit; } // Load all available screenshots once $files = @scandir($L99_DIR) ?: []; $available = []; foreach ($files as $f) { if (preg_match('/\.(png|jpg|jpeg|webp)$/i', $f)) { // Normalize: strip prefixes (alive-, prod-, sso-, v01-, v02-, v03-, app-) and suffix (extension) $base = preg_replace('/\.(png|jpg|jpeg|webp)$/i', '', $f); $norm = preg_replace('/^(alive|prod|sso|app|v0[0-9]|resp|test|ux)-/', '', $base); $norm = strtolower($norm); // Store both exact and normalized $available[strtolower($base)] = $f; if ($norm !== strtolower($base)) { $available[$norm] = $f; } } } // Read carto DATA to match $carto = @file_get_contents("/var/www/html/cartographie-screens.html") ?: ""; $mapping = []; if (preg_match('/const DATA = (\[.*?\]);/s', $carto, $m)) { $data = json_decode($m[1], true) ?: []; foreach ($data as $entry) { $url = $entry["url"] ?? ""; $name = $entry["name"] ?? ""; if (!$url || !$name) continue; // Normalize name: strip .html/.php, lowercase $key = strtolower(preg_replace('/\.(html|php)$/i', '', $name)); // Try multiple match strategies $found = null; if (isset($available[$key])) { $found = $available[$key]; } elseif (isset($available[$key . "_html"])) { $found = $available[$key . "_html"]; } else { // Fuzzy: find any filename containing the key as substring foreach ($available as $norm => $file) { if (strpos($norm, $key) !== false && strlen($key) > 3) { $found = $file; break; } } } if ($found) { $mapping[$url] = $WEB_BASE . "/" . $found; } } } echo json_encode([ "generated_at" => date("c"), "total_screenshots_available" => count(array_unique($available)), "total_urls_matched" => count($mapping), "by_url" => $mapping ], JSON_UNESCAPED_SLASHES);