Add redirect_from && redirect_to to router options

+ Also add * for all methods shortcut
This commit is contained in:
slawkens 2023-02-03 12:18:35 +01:00
parent 7d38945e8d
commit 63d2c5c6b5
5 changed files with 53 additions and 27 deletions

View File

@ -41,8 +41,8 @@
"priority": "130" "priority": "130"
}, },
"Redirect Example": { "Redirect Example": {
"pattern": "/redirectExample", "redirect_from": "/redirectExample",
"file": "account/manage" "redirect_to": "account/manage"
} }
} }
} }

View File

@ -1531,6 +1531,12 @@ function camelCaseToUnderscore($input)
return ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $input)), '_'); return ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $input)), '_');
} }
function removeIfFirstSlash(&$text) {
if(strpos($text, '/') === 0) {
$text = str_replace_first('/', '', $text);
}
};
// validator functions // validator functions
require_once LIBS . 'validator.php'; require_once LIBS . 'validator.php';
require_once SYSTEM . 'compat/base.php'; require_once SYSTEM . 'compat/base.php';

View File

@ -76,22 +76,38 @@ class Plugins {
if (isset($plugin['routes'])) { if (isset($plugin['routes'])) {
foreach ($plugin['routes'] as $_name => $info) { foreach ($plugin['routes'] as $_name => $info) {
// default method: get // default method: get
$methods = isset($info['method']) ? explode(',', $info['method']) : ['GET']; $method = $info['method'] ?? ['GET'];
if ($method !== '*') {
$methods = is_string($method) ? explode(',', $info['method']) : $method;
foreach ($methods as $method) { foreach ($methods as $method) {
if (!in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'])) { if (!in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'])) {
self::$warnings[] = $warningPreTitle . 'Unallowed method ' . $method . '... Disabling this route...'; self::$warnings[] = $warningPreTitle . 'Not allowed method ' . $method . '... Disabling this route...';
continue;
} }
} }
}
else {
$methods = '*'; // all available methods
}
if (!isset($info['priority'])) { if (!isset($info['priority'])) {
$info['priority'] = 100; // default priority $info['priority'] = 100; // default priority
} }
// replace first occurence of / in pattern if found (will be auto-added later) if (isset($info['redirect_from'])) {
if(strpos($info['pattern'], '/') === 0) { removeIfFirstSlash($info['redirect_from']);
$info['pattern'] = str_replace_first('/', '', $info['pattern']);
$info['pattern'] = $info['redirect_from'];
if (!isset($info['redirect_to'])) {
self::$warnings[] = $warningPreTitle . 'redirect set without "redirect_to".';
} }
else {
removeIfFirstSlash($info['redirect_to']);
$info['file'] = '__redirect__/' . $info['redirect_to'];
}
}
// replace first occurence of / in pattern if found (will be auto-added later)
removeIfFirstSlash($info['pattern']);
foreach ($routes as $id => &$route) { foreach ($routes as $id => &$route) {
if($route[1] == $info['pattern']) { if($route[1] == $info['pattern']) {
@ -102,7 +118,6 @@ class Plugins {
else { else {
self::$warnings[] = $warningPreTitle . "Duplicated route with lower priority: {$route[1]} ({$route[3]}). Disabling this route..."; self::$warnings[] = $warningPreTitle . "Duplicated route with lower priority: {$route[1]} ({$route[3]}). Disabling this route...";
unset($routes[$id]); unset($routes[$id]);
continue;
} }
} }
} }

View File

@ -65,31 +65,37 @@ if($logged && $account_logged && $account_logged->isLoaded()) {
$dispatcher = FastRoute\cachedDispatcher(function (FastRoute\RouteCollector $r) { $dispatcher = FastRoute\cachedDispatcher(function (FastRoute\RouteCollector $r) {
$routes = require SYSTEM . 'routes.php'; $routes = require SYSTEM . 'routes.php';
$duplicates = []; $isAlreadyDefined = [];
$routesTmp = []; $routesTmp = [];
foreach(getDatabasePages() as $page) { foreach(getDatabasePages() as $page) {
$duplicates[$page] = true; $isAlreadyDefined[$page] = true;
$routesTmp[] = [['GET', 'POST'], $page, 'database/' . $page, true]; $routesTmp[] = ['*', $page, '__database__/' . $page, true];
} }
Plugins::clearWarnings(); Plugins::clearWarnings();
foreach (Plugins::getRoutes() as $route) { foreach (Plugins::getRoutes() as $route) {
if(!isset($duplicates[$route[1]])) { if(!isset($isAlreadyDefined[$route[1]])) {
$duplicates[$route[1]] = true; $isAlreadyDefined[$route[1]] = true;
$routesTmp[] = [$route[0], $route[1], $route[2]]; $routesTmp[] = [$route[0], $route[1], $route[2]];
} }
} }
foreach ($routes as $route) { foreach ($routes as $route) {
if(!isset($duplicates[$route[1]])) { if(!isset($isAlreadyDefined[$route[1]])) {
if (strpos($route[2], '__redirect__') === false && strpos($route[2], '__database__') === false) {
$routesTmp[] = [$route[0], $route[1], 'system/pages/' . $route[2]]; $routesTmp[] = [$route[0], $route[1], 'system/pages/' . $route[2]];
} }
else {
$routesTmp[] = [$route[0], $route[1], $route[2]];
}
}
} }
//var_dump($routesTmp);
foreach ($routesTmp as $route) { foreach ($routesTmp as $route) {
if (strpos($route[2], '.php') === false && !isset($route[3])) { if ($route[0] === '*') {
$route[2] = str_replace('system/pages/', '', 'redirect/' . $route[2]); $route[0] = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
} }
$r->addRoute($route[0], $route[1], $route[2]); $r->addRoute($route[0], $route[1], $route[2]);
@ -170,9 +176,8 @@ else {
$_REQUEST = array_merge($_REQUEST, $vars); $_REQUEST = array_merge($_REQUEST, $vars);
$_GET = array_merge($_GET, $vars); $_GET = array_merge($_GET, $vars);
if (strpos($path, 'database/') !== false) { if (strpos($path, '__database__/') !== false) {
//var_dump($path); $pageName = str_replace('__database__/', '', $path);
$pageName = str_replace('database/', '', $path);
$success = false; $success = false;
$tmp_content = getCustomPage($pageName, $success); $tmp_content = getCustomPage($pageName, $success);
@ -188,8 +193,8 @@ else {
$page = $pageName; $page = $pageName;
$file = false; $file = false;
} }
} else if (strpos($path, 'redirect/') !== false) { } else if (strpos($path, '__redirect__/') !== false) {
$path = str_replace('redirect/', '', $path); $path = str_replace('__redirect__/', '', $path);
header('Location: ' . BASE_URL . $path); header('Location: ' . BASE_URL . $path);
exit; exit;
} else { } else {

View File

@ -9,10 +9,10 @@
*/ */
return [ return [
['GET', '', 'news'], // redirect empty URL to news ['GET', '', '__redirect__/news'], // redirect empty URL to news
['GET', 'news/archive/{id:[0-9]+}[/]', 'news/archive.php'], ['GET', 'news/archive/{id:[0-9]+}[/]', 'news/archive.php'],
[['GET', 'POST'], 'account/base[/]', '404.php'], // this is to block account/base.php ['*', 'account/base[/]', '404.php'], // this is to block account/base.php
[['GET', 'POST'], 'account/password[/]', 'account/change_password.php'], [['GET', 'POST'], 'account/password[/]', 'account/change_password.php'],
[['GET', 'POST'], 'account/register/new[/]', 'account/register_new.php'], [['GET', 'POST'], 'account/register/new[/]', 'account/register_new.php'],
[['GET', 'POST'], 'account/email[/]', 'account/change_email.php'], [['GET', 'POST'], 'account/email[/]', 'account/change_email.php'],