Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,41 +109,48 @@

$all_pages = $pages->returnPages();

if (array_key_exists($route, $all_pages)) {
$pages->setActivePage($all_pages[$route]);
if (isset($all_pages[$route]['custom'])) {
require(implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'modules', 'Core', 'pages', 'custom.php']));
die;
}

$path = implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'modules', $all_pages[$route]['module'], $all_pages[$route]['file']]);
foreach ($all_pages as $page_route => $page) {
$regex = preg_replace(
'/\{([a-zA-Z0-9_]+)\}/',
'(?P<$1>[^/]+)',
$page_route
);
Comment on lines +112 to +117

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe explain through a comment here what this code does instead of letting users guess


if (file_exists($path)) {
require($path);
die;
}
} else {
// Use recursion to check - might have URL parameters in path
$path_array = explode('/', $route);
$regex = '#^' . rtrim($regex, '/') . '$#';

for ($i = count($path_array) - 2; $i > 0; $i--) {
$new_path = '/';
for ($n = 1; $n <= $i; $n++) {
$new_path .= $path_array[$n] . '/';
if (preg_match($regex, $route, $matches)) {
$route_params = [];
foreach ($matches as $key => $value) {
if (!is_int($key)) {
$route_params[$key] = $value;
}
Comment on lines +124 to +126

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention why this is_int is needed, this is a side effect of the regex constructed earlier and the named parameters

}

$new_path = rtrim($new_path, '/');
$pages->setActivePage($page);

if (isset($page['custom'])) {
require implode(DIRECTORY_SEPARATOR, [
ROOT_PATH,
'modules',
'Core',
'pages',
'custom.php'
]);
die;
}

if (array_key_exists($new_path, $all_pages)) {
$path = implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'modules', $all_pages[$new_path]['module'], $all_pages[$new_path]['file']]);
$path = implode(DIRECTORY_SEPARATOR, [
ROOT_PATH,
'modules',
$page['module'],
$page['file']
]);

if (file_exists($path)) {
$pages->setActivePage($all_pages[$new_path]);
require($path);
die;
}
if (file_exists($path)) {
require $path;
die;
}
}
}

require(ROOT_PATH . '/404.php');
require(ROOT_PATH . '/404.php');
5 changes: 3 additions & 2 deletions modules/Core/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function __construct(Language $language, Pages $pages, User $user, Naviga

$pages->add('Core', '/login', 'pages/login.php');
$pages->add('Core', '/logout', 'pages/logout.php');
$pages->add('Core', '/profile', 'pages/profile.php', 'profile', true);
$pages->add('Core', '/profile', 'pages/profile.php');
$pages->add('Core', '/profile/{username}', 'pages/profile.php', 'profile', true);
$pages->add('Core', '/register', 'pages/register.php');
$pages->add('Core', '/register/oauth', 'pages/register.php');
$pages->add('Core', '/validate', 'pages/validate.php');
Expand Down Expand Up @@ -116,7 +117,7 @@ public function __construct(Language $language, Pages $pages, User $user, Naviga
$pages->add('Core', '/panel/users/punishments', 'pages/panel/users_punishments.php');
$pages->add('Core', '/panel/users/reports', 'pages/panel/users_reports.php');
$pages->add('Core', '/panel/users/sessions', 'pages/panel/users_sessions.php');
$pages->add('Core', '/panel/user', 'pages/panel/user.php');
$pages->add('Core', '/panel/user/{user}', 'pages/panel/user.php');

// Ajax GET requests
$pages->addAjaxScript(URL::build('/queries/servers'));
Expand Down
10 changes: 2 additions & 8 deletions modules/Core/pages/panel/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @var Navigation $staffcp_nav
* @var Pages $pages
* @var string $route
* @var array $route_params
* @var TemplateBase $template
* @var User $user
* @var Widgets $widgets
Expand All @@ -24,14 +25,7 @@
die();
}

$uid = explode('/', $route);
$uid = $uid[count($uid) - 1];

if (!strlen($uid)) {
Redirect::to(URL::build('/panel'));
}

$uid = explode('-', $uid);
$uid = explode('-', $route_params['user']);
if (!is_numeric($uid[0])) {
Redirect::to(URL::build('/panel'));
}
Expand Down
9 changes: 4 additions & 5 deletions modules/Core/pages/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@

$timeago = new TimeAgo(TIMEZONE);

$profile = explode('/', rtrim($_GET['route'], '/'));
if (count($profile) >= 3 && ($profile[count($profile) - 1] != 'profile' || $profile[count($profile) - 2] == 'profile') && !isset($_GET['error'])) {
if (isset($route_params['username'])) {
// User specified
$md_profile = $profile[count($profile) - 1];
$md_profile = $route_params['username'];

$page_metadata = DB::getInstance()->get('page_descriptions', ['page', '/profile'])->results();
if (count($page_metadata)) {
Expand Down Expand Up @@ -54,9 +53,9 @@
}
');

if (count($profile) >= 3 && ($profile[count($profile) - 1] != 'profile' || $profile[count($profile) - 2] == 'profile') && !isset($_GET['error'])) {
if (isset($route_params['username'])) {
// User specified
$profile = $profile[count($profile) - 1];
$profile = $route_params['username'];

$profile_user = new User($profile, 'username');
if (!$profile_user->exists()) {
Expand Down
4 changes: 2 additions & 2 deletions modules/Forum/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function __construct(Language $language, Language $forum_language, Pages

$pages->add('Forum', '/forum', 'pages/forum/index.php', 'forum', true);
$pages->add('Forum', '/forum/error', 'pages/forum/error.php');
$pages->add('Forum', '/forum/view', 'pages/forum/view_forum.php');
$pages->add('Forum', '/forum/topic', 'pages/forum/view_topic.php');
$pages->add('Forum', '/forum/view/{forum}', 'pages/forum/view_forum.php');
$pages->add('Forum', '/forum/topic/{topic}', 'pages/forum/view_topic.php');
$pages->add('Forum', '/forum/new', 'pages/forum/new_topic.php');
$pages->add('Forum', '/forum/spam', 'pages/forum/spam.php');
$pages->add('Forum', '/forum/report', 'pages/forum/report.php');
Expand Down
11 changes: 2 additions & 9 deletions modules/Forum/pages/forum/view_forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @var Navigation $staffcp_nav
* @var Pages $pages
* @var string $route
* @var array $route_params
* @var TemplateBase $template
* @var User $user
* @var Widgets $widgets
Expand All @@ -27,15 +28,7 @@
$timeago = new TimeAgo(TIMEZONE);

// Get forum ID
$fid = explode('/', $route);
$fid = $fid[count($fid) - 1];

if (!strlen($fid)) {
require_once(ROOT_PATH . '/404.php');
die();
}

$fid = explode('-', $fid);
$fid = explode('-', $route_params['forum']);
if (!is_numeric($fid[0])) {
require_once(ROOT_PATH . '/404.php');
die();
Expand Down
11 changes: 2 additions & 9 deletions modules/Forum/pages/forum/view_topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @var Navigation $staffcp_nav
* @var Pages $pages
* @var string $route
* @var array $route_params
* @var TemplateBase $template
* @var User $user
* @var Widgets $widgets
Expand All @@ -27,15 +28,7 @@
$timeago = new TimeAgo(TIMEZONE);

// Get topic ID
$tid = explode('/', $route);
$tid = $tid[count($tid) - 1];

if (!strlen($tid)) {
require_once ROOT_PATH . '/404.php';
die();
}

$tid = explode('-', $tid);
$tid = explode('-', $route_params['topic']);
if (!is_numeric($tid[0])) {
require_once ROOT_PATH . '/404.php';
die();
Expand Down
Loading