getData();
$canWriteCache = self::canWriteCache();
// If skip cache is enabled, do not try to write it
// If no skip cache then try to write if write is possible
if (! $skipCache && $canWriteCache) {
$writeWorks = self::writeCache(
'getRoute();
$routeInfo = $dispatcher->dispatch($request->getMethod(), rawurldecode($route));
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
/** @var ResponseRenderer $response */
$response = $container->get(ResponseRenderer::class);
$response->setHttpResponseCode(404);
echo Message::error(sprintf(
__('Error 404! The page %s was not found.'),
'' . htmlspecialchars($route) . '',
))->getDisplay();
return;
}
if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
/** @var ResponseRenderer $response */
$response = $container->get(ResponseRenderer::class);
$response->setHttpResponseCode(405);
echo Message::error(__('Error 405! Request method not allowed.'))->getDisplay();
return;
}
if ($routeInfo[0] !== Dispatcher::FOUND) {
return;
}
/** @psalm-var class-string $controllerName */
$controllerName = $routeInfo[1];
/** @var array $vars */
$vars = $routeInfo[2];
/** @psalm-var callable(ServerRequest=, array=):void $controller */
$controller = $container->get($controllerName);
$controller($request, $vars);
}
/** @psalm-assert-if-true array[] $dispatchData */
private static function isRoutesCacheFileValid(mixed $dispatchData): bool
{
return is_array($dispatchData)
&& isset($dispatchData[1])
&& is_array($dispatchData[1])
&& isset($dispatchData[0]['GET']['/'])
&& $dispatchData[0]['GET']['/'] === HomeController::class;
}
public static function callSetupController(ServerRequest $request): void
{
$route = $request->getRoute();
if ($route === '/setup' || $route === '/') {
(new MainController())($request);
return;
}
if ($route === '/setup/show-config') {
(new ShowConfigController())($request);
return;
}
if ($route === '/setup/validate') {
(new ValidateController())($request);
return;
}
echo (new Template())->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
'error_message' => Sanitize::sanitizeMessage(sprintf(
__('Error 404! The page %s was not found.'),
'[code]' . htmlspecialchars($route) . '[/code]',
)),
]);
}
/**
* PATH_INFO could be compromised if set, so remove it from PHP_SELF
* and provide a clean PHP_SELF here
*/
public static function getCleanPathInfo(): string
{
$pmaPhpSelf = Core::getenv('PHP_SELF');
if ($pmaPhpSelf === '') {
$pmaPhpSelf = urldecode(Core::getenv('REQUEST_URI'));
}
$pathInfo = Core::getenv('PATH_INFO');
if ($pathInfo !== '' && $pmaPhpSelf !== '') {
$questionPos = mb_strpos($pmaPhpSelf, '?');
if ($questionPos != false) {
$pmaPhpSelf = mb_substr($pmaPhpSelf, 0, $questionPos);
}
$pathInfoPos = mb_strrpos($pmaPhpSelf, $pathInfo);
if ($pathInfoPos !== false) {
$pathInfoPart = mb_substr($pmaPhpSelf, $pathInfoPos, mb_strlen($pathInfo));
if ($pathInfoPart === $pathInfo) {
$pmaPhpSelf = mb_substr($pmaPhpSelf, 0, $pathInfoPos);
}
}
}
$path = [];
foreach (explode('/', $pmaPhpSelf) as $part) {
// ignore parts that have no value
if ($part === '' || $part === '.') {
continue;
}
if ($part !== '..') {
// cool, we found a new part
$path[] = $part;
} elseif ($path !== []) {
// going back up? sure
array_pop($path);
}
// Here we intentionall ignore case where we go too up
// as there is nothing sane to do
}
/** TODO: Do we really need htmlspecialchars here? */
return htmlspecialchars('/' . implode('/', $path));
}
}