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') {
+1 -1
View File
@@ -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é
+33 -18
View File
@@ -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
<meta property="og:type" content="website" />
<meta property="og:title" content="<?= e($title) ?>" />
<meta property="og:description" content="<?= e($description) ?>" />
<meta property="og:image" content="/p/assets/img/logo.png" />
<meta property="og:image" content="<?= e(fws_url('/p/assets/img/logo.png')) ?>" />
<meta property="og:locale" content="fr_FR" />
<link rel="stylesheet" href="<?= e(fws_asset('/p/assets/styles/styles.css')) ?>" />
@@ -153,7 +168,7 @@ function fws_site_active(string $route, string $prefix): string
<?php foreach ($extraCss as $css): ?>
<link rel="stylesheet" href="<?= e(fws_asset($css)) ?>" />
<?php endforeach; ?>
<link rel="icon" type="image/png" href="/p/assets/img/logo.png" />
<link rel="icon" type="image/png" href="<?= e(fws_url('/p/assets/img/logo.png')) ?>" />
<!-- Réseau : releases des paquets (Gitea) et de l'ISO (GitHub) -->
<link rel="preconnect" href="https://gitea.nocode-var.tech" crossorigin />
@@ -170,16 +185,16 @@ function fws_site_active(string $route, string $prefix): string
<div class="page-shell">
<header class="site-header" role="banner">
<div class="header-brand-block">
<a class="brand" href="/?p/index/" aria-label="FWS Linux — Retour à l'accueil">
<img class="brand-mark" src="/p/assets/img/logo.png" alt="" width="44" height="44" />
<a class="brand" href="<?= e(fws_url('/?p/index/')) ?>" aria-label="FWS Linux — Retour à l'accueil">
<img class="brand-mark" src="<?= e(fws_url('/p/assets/img/logo.png')) ?>" alt="" width="44" height="44" />
<span class="brand-text">
<strong>FWS Linux</strong>
</span>
</a>
<nav class="site-nav" aria-label="Navigation principale">
<a href="/?p/index/"<?= fws_site_active($route, 'index') ?>>Accueil</a>
<a href="/?p/dl/"<?= fws_site_active($route, 'dl') ?>>Téléchargements</a>
<a href="<?= e(fws_url('/?p/index/')) ?>"<?= fws_site_active($route, 'index') ?>>Accueil</a>
<a href="<?= e(fws_url('/?p/dl/')) ?>"<?= fws_site_active($route, 'dl') ?>>Téléchargements</a>
<div class="nav-dropdown" data-nav-dropdown>
<button type="button" class="nav-dropdown-toggle" aria-expanded="false" aria-haspopup="true" aria-controls="nav-paquets-panel"<?= fws_site_active($route, 'paquets') ?>>
@@ -193,9 +208,9 @@ function fws_site_active(string $route, string $prefix): string
<?php else: ?>
<?php foreach ($packages as $i => $pkg): ?>
<?php $navThumb = (string) ($pkg['screens'][0]['src'] ?? ''); ?>
<a class="nav-dropdown-item<?= $navThumb !== '' ? ' has-thumb' : '' ?>" href="/?p/paquets/<?= max($i + 1, (int) $pkg['dbId']) ?>/">
<a class="nav-dropdown-item<?= $navThumb !== '' ? ' has-thumb' : '' ?>" href="<?= e(fws_url('/?p/paquets/' . max($i + 1, (int) $pkg['dbId']) . '/')) ?>">
<?php if ($navThumb !== ''): ?>
<img class="nav-dropdown-thumb" src="<?= e($navThumb) ?>" alt="" loading="lazy" width="30" height="30">
<img class="nav-dropdown-thumb" src="<?= e(fws_url($navThumb)) ?>" alt="" loading="lazy" width="30" height="30">
<?php endif; ?>
<span class="nav-dropdown-text">
<strong><?= e($pkg['name']) ?><?php if (!empty($pkg['latest']['name'])): ?><span class="nav-dropdown-version"><?= e($pkg['latest']['name']) ?></span><?php endif; ?></strong>
@@ -205,7 +220,7 @@ function fws_site_active(string $route, string $prefix): string
<?php endforeach; ?>
<?php endif; ?>
</div>
<a class="nav-dropdown-all" href="/?p/paquets/">Voir tous les paquets &rarr;</a>
<a class="nav-dropdown-all" href="<?= e(fws_url('/?p/paquets/')) ?>">Voir tous les paquets &rarr;</a>
</div>
</div>
</nav>
@@ -220,7 +235,7 @@ function fws_site_active(string $route, string $prefix): string
<div class="footer-content">
<div class="footer-brand-row">
<div class="footer-brand-block">
<img class="footer-brand-mark" src="/p/assets/img/logo.png" alt="" width="40" height="40" />
<img class="footer-brand-mark" src="<?= e(fws_url('/p/assets/img/logo.png')) ?>" alt="" width="40" height="40" />
<div>
<strong>FWS Linux</strong>
<p>Un système léger, libre et compatible</p>
@@ -231,16 +246,16 @@ function fws_site_active(string $route, string $prefix): string
<div class="footer-links-grid">
<nav aria-label="Navigation secondaire">
<h2>Navigation</h2>
<a href="/?p/index/">Accueil</a>
<a href="/?p/dl/">Téléchargements</a>
<a href="/?p/paquets/">Paquets</a>
<a href="/?p/equipe/">Équipe</a>
<a href="<?= e(fws_url('/?p/index/')) ?>">Accueil</a>
<a href="<?= e(fws_url('/?p/dl/')) ?>">Téléchargements</a>
<a href="<?= e(fws_url('/?p/paquets/')) ?>">Paquets</a>
<a href="<?= e(fws_url('/?p/equipe/')) ?>">Équipe</a>
</nav>
<section>
<h2>Contact</h2>
<a href="/?p/contact/">Page de contact</a>
<a href="/?p/equipe/">Équipe</a>
<a href="<?= e(fws_url('/?p/contact/')) ?>">Page de contact</a>
<a href="<?= e(fws_url('/?p/equipe/')) ?>">Équipe</a>
</section>
<section>