fix(core): resolve site URLs against a dynamic base URL

WAMP/XAMPP serve the site from a subfolder of www while production
serves it from the domain root; root-absolute URLs broke every page
and asset on Windows. Add fws_base_url()/fws_url() and route all
shell URLs (nav, footer, logos, canonical, package thumbnails)
through them, with path-form route detection adjusted for
subfolder deployments.
This commit is contained in:
BlackAngelTV committed 2026-09-16 14:35:24 +02:00
1 parent 6b6b07aa83
commit da74767843
3 files changed
+75 -19

No files matched your search

+41
View File
@@ -24,6 +24,47 @@ ini_set('display_startup_errors', '0');
ini_set('log_errors', '1');
error_reporting(E_ALL);
/* ── Base URL (Windows : WAMP/XAMPP servent le site depuis un sous-dossier de
* www, ex /FWS-Website/ ; Linux : racine du domaine). Calculée une fois,
* depuis SCRIPT_NAME — indépendant du point d'entrée (page.php, index.php,
* /p/dl/, admin/…). Toutes les URLs du site passent par fws_url()/fws_asset().
*/
if (!function_exists('fws_base_url')) {
function fws_base_url(): string
{
static $base = null;
if ($base !== null) {
return $base;
}
$script = str_replace('\\', '/', (string) ($_SERVER['SCRIPT_NAME'] ?? ''));
$dir = rtrim(str_replace('\\', '/', (string) dirname($script)), '/');
/* Le site est déployé à la racine du domaine : les dossiers sensibles
(inc, api, admin, cache, p/<route>) sont forcément à la racine.
Sinon, on est servi depuis un sous-dossier (WAMP : /FWS-Website). */
if (preg_match('~/((?:inc|api|admin|cache)|p/(?:index|dl|paquets|equipe|contact|assets))(?:/|$)~', $dir, $m)) {
$base = substr($dir, 0, (int) strrpos($dir, $m[0]));
} else {
$base = $dir;
}
return $base;
}
}
if (!function_exists('fws_url')) {
/** URL d'une route ou d'un asset local, préfixée par la base URL. */
function fws_url(string $path): string
{
if ($path === '' || preg_match('~^(?:[a-z][a-z0-9+.-]*:|//)~i', $path)) {
return $path; /* absolu, mailto:, tel:, //… : inchangé */
}
$base = fws_base_url();
if ($base !== '' && ($path === $base || str_starts_with($path, $base . '/'))) {
return $path; /* déjà préfixé (idempotence) */
}
return $base . '/' . ltrim($path, '/');
}
}
/* En CLI (scripts d'administration dans le conteneur), on affiche tout :
* c'est interactif, l'opérateur doit voir les problèmes. */
if (PHP_SAPI === 'cli') {