diff --git a/inc/errors.php b/inc/errors.php index bddd71a..14b140d 100644 --- a/inc/errors.php +++ b/inc/errors.php @@ -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/) 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') { diff --git a/inc/site.php b/inc/site.php index 4139856..f57c5a9 100644 --- a/inc/site.php +++ b/inc/site.php @@ -33,7 +33,7 @@ function fws_asset(string $path): string { $abs = FWS_SITE_ROOT . $path; $mtime = @filemtime($abs); - return $mtime ? $path . '?v=' . $mtime : $path; + return fws_url($path) . ($mtime ? '?v=' . $mtime : ''); } /* fws_utf8_str / fws_utf8_arr sont définies dans api/bugs/db.php (chargé diff --git a/page.php b/page.php index 738293e..3b414f1 100644 --- a/page.php +++ b/page.php @@ -29,8 +29,17 @@ function fws_site_current_route(): string $raw = (string) $_GET['p']; } if ($raw === '') { - /* Forme chemin : /p/dl/ (directement sous la racine). */ + /* Forme chemin : /p/dl/ (directement sous la base URL). En + sous-dossier (WAMP), REQUEST_URI commence par /FWS-Website/ : on + retire la base pour retrouver le début de la route. */ $path = trim((string) (parse_url((string) ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?: '/'), '/'); + if ($path !== '') { + $base = ltrim(fws_base_url() . '/', '/'); + if ($base !== '' && str_starts_with($path, $base)) { + $path = substr($path, strlen($base)); + $path = trim(preg_replace('/^\\/+|\\/+$/', '', $path) ?? ''); + } + } if ($path !== '' && !str_contains($path, '.') && str_starts_with($path, 'p/')) { $raw = substr($path, 2); } @@ -122,7 +131,13 @@ if ($description === null) { } if ($canonical === null) { $canonRoute = $route === 'paquets/_detail' ? 'paquets/' . $detailId : $route; - $canonical = 'https://fwslinux.fr/?p/' . ($canonRoute === 'index' ? 'index' : $canonRoute) . '/'; + /* Portabilité : hôte courant + base URL (sous-dossier WAMP inclus), + au lieu du domaine de production codé en dur. */ + $fwsScheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') + || (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https') ? 'https' : 'http'; + $fwsHost = trim((string) ($_SERVER['HTTP_HOST'] ?? '')) !== '' + ? (string) $_SERVER['HTTP_HOST'] : 'fwslinux.fr'; + $canonical = $fwsScheme . '://' . $fwsHost . fws_url('/?p/' . ($canonRoute === 'index' ? 'index' : $canonRoute) . '/'); } function fws_site_active(string $route, string $prefix): string @@ -145,7 +160,7 @@ function fws_site_active(string $route, string $prefix): string - + @@ -153,7 +168,7 @@ function fws_site_active(string $route, string $prefix): string - + @@ -170,16 +185,16 @@ function fws_site_active(string $route, string $prefix): string
@@ -220,7 +235,7 @@ function fws_site_active(string $route, string $prefix): string