Skip to content

Commit

Permalink
Move all index.php code to PhpMyAdmin\Routing and change cache name
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <[email protected]>

Fix some phpdoc comments

Signed-off-by: William Desportes <[email protected]>
  • Loading branch information
williamdes committed Mar 28, 2020
1 parent 898a526 commit bde3e5a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 48 deletions.
53 changes: 5 additions & 48 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,17 @@
*/
declare(strict_types=1);

use FastRoute\Dispatcher;
use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use function FastRoute\cachedDispatcher;
use PhpMyAdmin\Routing;

if (! defined('ROOT_PATH')) {
// phpcs:disable PSR1.Files.SideEffects
define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
// phpcs:enable
}

global $containerBuilder, $route, $cfg;
global $route;

/** @var string $route */
$route = $_GET['route'] ?? $_POST['route'] ?? '/';

/**
* See FAQ 1.34.
*
* @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34
*/
if (($route === '/' || $route === '') && isset($_GET['db']) && mb_strlen($_GET['db']) !== 0) {
$route = '/database/structure';
if (isset($_GET['table']) && mb_strlen($_GET['table']) !== 0) {
$route = '/sql';
}
}
$route = Routing::getCurrentRoute();

if ($route === '/import-status') {
// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -40,32 +24,5 @@

require_once ROOT_PATH . 'libraries/common.inc.php';

$routes = require ROOT_PATH . 'libraries/routes.php';
/** @var \PhpMyAdmin\Config|null $config */
$config = $GLOBALS['PMA_Config'];
$dispatcher = cachedDispatcher($routes, [
'cacheFile' => $config !== null ? $config->getTempDir('routing') . '/routes.cache' : null,
'cacheDisabled' => ($cfg['environment'] ?? '') === 'development',
]);
$routeInfo = $dispatcher->dispatch(
$_SERVER['REQUEST_METHOD'],
rawurldecode($route)
);
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
/** @var Response $response */
$response = $containerBuilder->get(Response::class);
$response->setHttpResponseCode(404);
Message::error(sprintf(
__('Error 404! The page %s was not found.'),
'<code>' . $route . '</code>'
))->display();
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
/** @var Response $response */
$response = $containerBuilder->get(Response::class);
$response->setHttpResponseCode(405);
Message::error(__('Error 405! Request method not allowed.'))->display();
} elseif ($routeInfo[0] === Dispatcher::FOUND) {
[$controllerName, $action] = $routeInfo[1];
$controller = $containerBuilder->get($controllerName);
$controller->$action($routeInfo[2]);
}
$dispatcher = Routing::getDispatcher();
Routing::callControllerForRoute($route, $dispatcher);
81 changes: 81 additions & 0 deletions libraries/classes/Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin;

use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use FastRoute\Dispatcher;
use function FastRoute\cachedDispatcher;
use function mb_strlen;
use function rawurldecode;

/**
* Class used to warm up the routing cache and manage routing.
*/
class Routing
{

public static function getDispatcher(): Dispatcher
{
global $cfg;

$routes = require ROOT_PATH . 'libraries/routes.php';
return cachedDispatcher($routes, [
'cacheFile' => ROOT_PATH . 'libraries/cache/routes.cache',
'cacheDisabled' => ($cfg['environment'] ?? '') === 'development',
]);
}

public static function getCurrentRoute(): string
{
/** @var string $route */
$route = $_GET['route'] ?? $_POST['route'] ?? '/';

/**
* See FAQ 1.34.
*
* @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34
*/
if (($route === '/' || $route === '') && isset($_GET['db']) && mb_strlen($_GET['db']) !== 0) {
$route = '/database/structure';
if (isset($_GET['table']) && mb_strlen($_GET['table']) !== 0) {
$route = '/sql';
}
}
return $route;
}

/**
* Call associated controller for a route using the dispatcher
* @param string $route The current route
* @param Dispatcher $dispatcher The dispatcher
*/
public static function callControllerForRoute(string $route, Dispatcher $dispatcher): void
{
global $containerBuilder;
$routeInfo = $dispatcher->dispatch(
$_SERVER['REQUEST_METHOD'],
rawurldecode($route)
);
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
/** @var Response $response */
$response = $containerBuilder->get(Response::class);
$response->setHttpResponseCode(404);
Message::error(sprintf(
__('Error 404! The page %s was not found.'),
'<code>' . $route . '</code>'
))->display();
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
/** @var Response $response */
$response = $containerBuilder->get(Response::class);
$response->setHttpResponseCode(405);
Message::error(__('Error 405! Request method not allowed.'))->display();
} elseif ($routeInfo[0] === Dispatcher::FOUND) {
[$controllerName, $action] = $routeInfo[1];
$controller = $containerBuilder->get($controllerName);
$controller->$action($routeInfo[2]);
}
}
}

0 comments on commit bde3e5a

Please sign in to comment.