Skip to content

Commit

Permalink
Fixed fastRoute duplicate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
slawkens committed Jun 13, 2024
1 parent afe70a0 commit 4c0739d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 62 deletions.
5 changes: 2 additions & 3 deletions system/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@
}

foreach ($routes as $route) {
$tmp = preg_replace("/\[[^)]+\]/","", $route[1]);

if(!isset($isAlreadyDefined[$tmp])) {
if(!isset($isAlreadyDefined[$route[1]])) {
if (strpos($route[2], '__redirect__') === false && strpos($route[2], '__database__') === false) {
$routesFinal[] = [$route[0], $route[1], 'system/pages/' . $route[2]];
}
else {
$routesFinal[] = [$route[0], $route[1], $route[2]];
}
$isAlreadyDefined[$route[1]] = true;
}
}

Expand Down
61 changes: 30 additions & 31 deletions system/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,48 @@

return [
['GET', '', 'news.php'], // empty URL = show news
['GET', 'news/archive/{id:int}[/]', 'news/archive.php'],
['GET', 'news/{id:int}[/]', 'news/archive.php'],
['GET', 'news/archive/{id:int}', 'news/archive.php'],
['GET', 'news/{id:int}', 'news/archive.php'],

// block access to some files
['*', 'account/base[/]', '404.php'], // this is to block account/base.php
['*', 'forum/base[/]', '404.php'],
['*', 'guilds/base[/]', '404.php'],

[['GET', 'POST'], 'account/password[/]', 'account/change_password.php'],
[['GET', 'POST'], 'account/register/new[/]', 'account/register_new.php'],
[['GET', 'POST'], 'account/email[/]', 'account/change_email.php'],
[['GET', 'POST'], 'account/info[/]', 'account/change_info.php'],
[['GET', 'POST'], 'account/character/create[/]', 'account/create_character.php'],
[['GET', 'POST'], 'account/character/name[/]', 'account/change_name.php'],
[['GET', 'POST'], 'account/character/sex[/]', 'account/change_sex.php'],
[['GET', 'POST'], 'account/character/delete[/]', 'account/delete_character.php'],
[['GET', 'POST'], 'account/character/comment[/{name:[A-Za-z0-9-_%+\']+}]', 'account/change_comment.php'],
['GET', 'account/confirm_email/{hash:alphanum}[/]', 'account/confirm_email.php'],

['GET', 'bans/{page:int}[/]', 'bans.php'],
['*', 'account/base', '404.php'], // this is to block account/base.php
['*', 'forum/base', '404.php'],
['*', 'guilds/base', '404.php'],

[['GET', 'POST'], 'account/password', 'account/change_password.php'],
[['GET', 'POST'], 'account/register/new', 'account/register_new.php'],
[['GET', 'POST'], 'account/email', 'account/change_email.php'],
[['GET', 'POST'], 'account/info', 'account/change_info.php'],
[['GET', 'POST'], 'account/character/create', 'account/create_character.php'],
[['GET', 'POST'], 'account/character/name', 'account/change_name.php'],
[['GET', 'POST'], 'account/character/sex', 'account/change_sex.php'],
[['GET', 'POST'], 'account/character/delete', 'account/delete_character.php'],
[['GET', 'POST'], 'account/character/comment[/{name:string}]', 'account/change_comment.php'],
['GET', 'account/confirm_email/{hash:alphanum}', 'account/confirm_email.php'],

['GET', 'bans/{page:int}', 'bans.php'],
[['GET', 'POST'], 'characters[/{name:string}]', 'characters.php'],
['GET', 'changelog[/{page:int}]', 'changelog.php'],
[['GET', 'POST'], 'monsters[/{name:string}]', 'monsters.php'],

[['GET', 'POST'], 'faq[/{action:string}]', 'faq.php'],

[['GET', 'POST'], 'forum/{action:string}[/]', 'forum.php'],
['GET', 'forum/board/{id:int}[/]', 'forum/show_board.php'],
['GET', 'forum/board/{id:int}/{page:[0-9]+}[/]', 'forum/show_board.php'],
['GET', 'forum/thread/{id:int}[/]', 'forum/show_thread.php'],
['GET', 'forum/thread/{id:int}/{page:int}[/]', 'forum/show_thread.php'],
[['GET', 'POST'], 'forum/{action:string}', 'forum.php'],
['GET', 'forum/board/{id:int}', 'forum/show_board.php'],
['GET', 'forum/board/{id:int}/{page:[0-9]+}', 'forum/show_board.php'],
['GET', 'forum/thread/{id:int}', 'forum/show_thread.php'],
['GET', 'forum/thread/{id:int}/{page:int}', 'forum/show_thread.php'],

['GET', 'gallery/{image:int}[/]', 'gallery.php'],
[['GET', 'POST'], 'gallery/{action:string}[/]', 'gallery.php'],
['GET', 'gallery/{image:int}', 'gallery.php'],
[['GET', 'POST'], 'gallery/{action:string}', 'gallery.php'],

[['GET', 'POST'], 'guilds/{guild:string}[/]', 'guilds/show.php'],
[['GET', 'POST'], 'guilds/{guild:string}', 'guilds/show.php'],

['GET', 'highscores/{list:alphanum}/{vocation:alphanum}/{page:int}[/]', 'highscores.php'],
['GET', 'highscores/{list:alphanum}/{page:int}[/]', 'highscores.php'],
['GET', 'highscores/{list:alphanum}/{vocation:alphanum}[/]', 'highscores.php'],
['GET', 'highscores/{list:alphanum}[/]', 'highscores.php'],
['GET', 'highscores/{list:alphanum}/{vocation:alphanum}/{page:int}', 'highscores.php'],
['GET', 'highscores/{list:alphanum}/{page:int}', 'highscores.php'],
['GET', 'highscores/{list:alphanum}/{vocation:alphanum}', 'highscores.php'],
['GET', 'highscores/{list:alphanum}', 'highscores.php'],
/*
'/^gifts\/history\/?$/' => array('subtopic' => 'gifts', 'action' => 'show_history'),
'/^polls\/[0-9]+\/?$/' => array('subtopic' => 'polls', 'id' => '$1'),
'/^spells\/[A-Za-z0-9-_%]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'spells', 'vocation' => '$1', 'order' => '$2'),
'/^houses\/view\/?$/' => array('subtopic' => 'houses', 'page' => 'view')*/
Expand Down
67 changes: 39 additions & 28 deletions system/src/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,14 @@ public static function getRoutes()
}
}

$duplicates = [];
$routes = [];
foreach(self::getAllPluginsJson() as $plugin) {
$priority = 100;
if (isset($plugin['priority'])) {
$priority = $plugin['priority'];
}

//
// Get all plugins/*/pages/*.php pages
//
$pluginPages = glob(PLUGINS . $plugin['filename'] . '/pages/*.php');
foreach ($pluginPages as $file) {
$file = str_replace(PLUGINS, 'plugins/', $file);
$name = pathinfo($file, PATHINFO_FILENAME);

$routes[] = [['get', 'post'], $name, $file, $priority];
}

//
// Get all plugins/*/pages/subFolder/*.php pages
//
$pluginPagesSubFolders = glob(PLUGINS . $plugin['filename'] . '/pages/*', GLOB_ONLYDIR);
foreach ($pluginPagesSubFolders as $folder) {
$folderName = pathinfo($folder, PATHINFO_FILENAME);

$subFiles = glob(PLUGINS . $plugin['filename'] . '/pages/' . $folderName . '/*.php');
foreach ($subFiles as $file) {
$file = str_replace(PLUGINS, 'plugins/', $file);
$name = $folderName . '/' . pathinfo($file, PATHINFO_FILENAME);

$routes[] = [['get', 'post'], $name, $file, $priority];
}
}

$warningPreTitle = 'Plugin: ' . $plugin['name'] . ' - ';

if (isset($plugin['routes'])) {
Expand Down Expand Up @@ -107,7 +81,44 @@ public static function getRoutes()
}
}

$routes[] = [$methods, $info['pattern'], $info['file'], $info['priority']];
$tmp = preg_replace("/\[[^)]+\]/",'', $info['pattern']);
if (!isset($duplicates[$tmp])) {
$routes[] = [$methods, $info['pattern'], $info['file'], $info['priority']];
$duplicates[$tmp] = true;
}
}
}

//
// Get all plugins/*/pages/*.php pages
//
$pluginPages = glob(PLUGINS . $plugin['filename'] . '/pages/*.php');
foreach ($pluginPages as $file) {
$file = str_replace(PLUGINS, 'plugins/', $file);
$name = pathinfo($file, PATHINFO_FILENAME);

if (!isset($duplicates[$name])) {
$routes[] = [['get', 'post'], $name, $file, $priority];
$duplicates[$name] = true;
}
}

//
// Get all plugins/*/pages/subFolder/*.php pages
//
$pluginPagesSubFolders = glob(PLUGINS . $plugin['filename'] . '/pages/*', GLOB_ONLYDIR);
foreach ($pluginPagesSubFolders as $folder) {
$folderName = pathinfo($folder, PATHINFO_FILENAME);

$subFiles = glob(PLUGINS . $plugin['filename'] . '/pages/' . $folderName . '/*.php');
foreach ($subFiles as $file) {
$file = str_replace(PLUGINS, 'plugins/', $file);
$name = $folderName . '/' . pathinfo($file, PATHINFO_FILENAME);

if (!isset($duplicates[$name])) {
$routes[] = [['get', 'post'], $name, $file, $priority];
$duplicates[$name] = true;
}
}
}
}
Expand Down

0 comments on commit 4c0739d

Please sign in to comment.