public static function filterRequestUri($uri = null, $exit = true)
{
if (!isset($uri)) {
if (!isset($_SERVER['REQUEST_URI'])) {
return false;
} else {
$uri = $_SERVER['REQUEST_URI'];
}
}
$uriOrig = $uri;
//
// Ensures the URL is well formed UTF-8
//
if (preg_match('//u', urldecode($uri))) {
return $uri;
}
//
// When not, assumes Windows-1252 and redirects to the corresponding UTF-8 encoded URL
//
$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(UTF8::encode('UTF-8', urldecode($m[0])));
}, $uri);
if ($uri !== $uriOrig && $exit === true && headers_sent() === false) {
// Use ob_start() to buffer content and avoid problem of headers already sent...
$severProtocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header($severProtocol . ' 301 Moved Permanently');
header('Location: ' . $uri);
exit;
}
return $uri;
}