From 95ba2065945987f400bcb8b262b6b9ec74fea96f Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sat, 2 May 2026 10:15:56 +0000 Subject: [PATCH] mon site 33333 --- site.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/site.php b/site.php index c6a32c3..c834ed8 100644 --- a/site.php +++ b/site.php @@ -25,6 +25,78 @@ function site_asset_version(): string return $version; } +function site_request_scheme(): string +{ + $cfVisitor = (string) ($_SERVER['HTTP_CF_VISITOR'] ?? ''); + if ($cfVisitor !== '') { + $decoded = json_decode($cfVisitor, true); + if (is_array($decoded) && isset($decoded['scheme']) && in_array($decoded['scheme'], ['http', 'https'], true)) { + return $decoded['scheme']; + } + } + + $forwardedProto = (string) ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? ''); + if ($forwardedProto !== '') { + $proto = strtolower(trim(explode(',', $forwardedProto)[0])); + if (in_array($proto, ['http', 'https'], true)) { + return $proto; + } + } + + $requestScheme = strtolower((string) ($_SERVER['REQUEST_SCHEME'] ?? '')); + if (in_array($requestScheme, ['http', 'https'], true)) { + return $requestScheme; + } + + return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; +} + +function site_request_host(): string +{ + $host = strtolower(trim((string) ($_SERVER['HTTP_HOST'] ?? ''))); + if ($host === '') { + return 'programmetelecesoir.net'; + } + + $host = preg_replace('/:\d+$/', '', $host) ?? $host; + return $host !== '' ? $host : 'programmetelecesoir.net'; +} + +function site_is_local_host(string $host): bool +{ + return in_array($host, ['127.0.0.1', 'localhost'], true) + || str_ends_with($host, '.local'); +} + +function site_enforce_public_url(): void +{ + if (PHP_SAPI === 'cli') { + return; + } + + $canonicalDomain = 'programmetelecesoir.net'; + $host = site_request_host(); + if ($host === '' || site_is_local_host($host)) { + return; + } + + $scheme = site_request_scheme(); + if ($host === $canonicalDomain && $scheme === 'https') { + return; + } + + $requestUri = (string) ($_SERVER['REQUEST_URI'] ?? '/'); + if ($requestUri === '') { + $requestUri = '/'; + } + + header('Vary: Host, X-Forwarded-Proto, CF-Visitor', false); + header('Location: https://' . $canonicalDomain . $requestUri, true, 301); + exit; +} + +site_enforce_public_url(); + function site_settings(): array { static $settings = null; @@ -32,9 +104,9 @@ function site_settings(): array return $settings; } - $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; + $scheme = site_request_scheme(); $canonicalDomain = 'programmetelecesoir.net'; - $host = $_SERVER['HTTP_HOST'] ?? $canonicalDomain; + $host = site_request_host(); $projectName = (string) ($_SERVER['PROJECT_NAME'] ?? ''); if ($projectName === '' || strcasecmp($projectName, 'programmetelecesoir.fr') === 0) { $projectName = $canonicalDomain;