= '8.32' ? '\X' : s\Intl::GRAPHEME_CLUSTER_RX); if (!function_exists('grapheme_strlen')) { extension_loaded('iconv') or static::initIconv(); extension_loaded('mbstring') or static::initMbstring(); require __DIR__.'/Bootup/intl.php'; } } public static function initLocale() { // With non-UTF-8 locale, basename() bugs. // Be aware that setlocale() can be slow. // You'd better properly configure your LANG environment variable to an UTF-8 locale. if ('' === basename('§')) { setlocale(LC_ALL, 'C.UTF-8', 'C'); setlocale(LC_CTYPE, 'en_US.UTF-8', 'fr_FR.UTF-8', 'es_ES.UTF-8', 'de_DE.UTF-8', 'ru_RU.UTF-8', 'pt_BR.UTF-8', 'it_IT.UTF-8', 'ja_JP.UTF-8', 'zh_CN.UTF-8', '0'); } } public static function filterRequestUri($uri = null, $exit = true) { if (!isset($uri)) { if (!isset($_SERVER['REQUEST_URI'])) { return; } else { $uri = $_SERVER['REQUEST_URI']; } } // Ensures the URL is well formed UTF-8 // When not, assumes Windows-1252 and redirects to the corresponding UTF-8 encoded URL if (!preg_match('//u', urldecode($uri))) { $uri = preg_replace_callback( '/[\x80-\xFF]+/', function ($m) {return urlencode($m[0]);}, $uri ); $uri = preg_replace_callback( '/(?:%[89A-F][0-9A-F])+/i', function ($m) {return urlencode(u::utf8_encode(urldecode($m[0])));}, $uri ); if ($exit) { header('HTTP/1.1 301 Moved Permanently'); header('Location: '.$uri); exit; // TODO: remove this in 1.2 (BC) } } return $uri; } public static function filterRequestInputs($normalization_form = 4 /* n::NFC */, $leading_combining = '◌') { // Ensures inputs are well formed UTF-8 // When not, assumes Windows-1252 and converts to UTF-8 // Tests only values, not keys $a = array(&$_FILES, &$_ENV, &$_GET, &$_POST, &$_COOKIE, &$_SERVER, &$_REQUEST); foreach ($a[0] as &$r) { $a[] = array(&$r['name'], &$r['type']); } unset($a[0]); $len = count($a) + 1; for ($i = 1; $i < $len; ++$i) { foreach ($a[$i] as &$r) { $s = $r; // $r is a ref, $s a copy if (is_array($s)) { $a[$len++] = &$r; } else { $r = static::filterString($s, $normalization_form, $leading_combining); } } unset($a[$i]); } } public static function filterString($s, $normalization_form = 4 /* n::NFC */, $leading_combining = '◌') { if (false !== strpos($s, "\r")) { // Workaround https://bugs.php.net/65732 $s = str_replace("\r\n", "\n", $s); $s = strtr($s, "\r", "\n"); } if (preg_match('/[\x80-\xFF]/', $s)) { if (n::isNormalized($s, $normalization_form)) { $n = '-'; } else { $n = n::normalize($s, $normalization_form); if (isset($n[0])) { $s = $n; } else { $s = u::utf8_encode($s); } } if ($s[0] >= "\x80" && isset($n[0], $leading_combining[0]) && preg_match('/^\p{Mn}/u', $s)) { // Prevent leading combining chars // for NFC-safe concatenations. $s = $leading_combining.$s; } } return $s; } }