From e099e066141e745770646882a62d6f3ebe8fca94 Mon Sep 17 00:00:00 2001 From: Damien Rochat Date: Wed, 16 Sep 2026 14:36:12 +0200 Subject: [PATCH] fix(core): add mbstring polyfills for PHP installs without the extension Some Windows PHP setups and minimal Docker images ship without mbstring, which crashed any page touching the database. Provide UTF-8 equivalents for the four mb_* functions the project uses; the extension keeps precedence when loaded. --- inc/errors.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/inc/errors.php b/inc/errors.php index 14b140d..7ef4b8b 100644 --- a/inc/errors.php +++ b/inc/errors.php @@ -71,6 +71,54 @@ if (PHP_SAPI === 'cli') { ini_set('display_errors', '1'); } +/* ── Portabilité : polyfills mbstring ────────────────────────────────────── + * L'extension mbstring est absente de certaines installations PHP de Windows + * (et de quelques images Docker minimales). Le site doit tourner quand même : + * on fournit des équivalents UTF-8 corrects pour les 4 fonctions mb_* du + * projet. Si mbstring est présente (cas Linux standard), on n'y touche pas. + */ +if (!function_exists('mb_strlen')) { + function fws_mb_chars(string $s): array + { + $c = preg_split('//u', $s, -1, PREG_SPLIT_NO_EMPTY); + return is_array($c) ? $c : ((str_split($s) === false) ? [] : str_split($s)); + } + function mb_strlen(string $s, ?string $enc = null): int + { + return count(fws_mb_chars($s)); + } + function mb_substr(string $s, int $start, ?int $len = null, ?string $enc = null): string + { + $c = fws_mb_chars($s); + $n = count($c); + if ($start < 0) { + $start = max(0, $n + $start); + } + if ($len === null) { + $len = $n; + } elseif ($len < 0) { + $len = max(0, $n - $start + $len); + } + return implode('', array_slice($c, $start, $len)); + } + function mb_check_encoding(string $s, ?string $enc = null): bool + { + return (bool) preg_match('//u', $s); + } + function mb_convert_encoding(string $s, string $to, ?string $from = null): string + { + if (function_exists('iconv') && strcasecmp($to, 'UTF-8') === 0) { + $conv = @iconv((string) $from, 'UTF-8//TRANSLIT//IGNORE', $s); + if (is_string($conv)) { + return $conv; + } + } + /* Dernier recours : on retire les octets qui cassent l'UTF-8. */ + $clean = preg_replace('/[\x80-\xFF]+/', '?', $s); + return is_string($clean) ? $clean : $s; + } +} + /* Journal : api/bugs/data/ (déjà interdit au web par .htaccess + gitigné). */ $fwsErrorLogDir = dirname(__DIR__) . '/api/bugs/data'; if (!is_dir($fwsErrorLogDir)) {