diff --git a/public/index.php b/public/index.php index d15b626..591be36 100644 --- a/public/index.php +++ b/public/index.php @@ -6,6 +6,11 @@ require_once '../src/lib.php'; require_once '../src/router.php'; require_once '../src/users.php'; require_once '../src/help.php'; +require_once '../src/towns.php'; +require_once '../src/explore.php'; +require_once '../src/fight.php'; +require_once '../src/heal.php'; +require_once '../src/forum.php'; if (!file_exists('../.installed')) redirect('install.php'); @@ -42,11 +47,6 @@ if (!$controlrow["gameopen"]) { exit; } -require_once '../src/towns.php'; -require_once '../src/explore.php'; -require_once '../src/fight.php'; -require_once '../src/heal.php'; - $r = new Router; $r->get('/', function() { @@ -73,6 +73,7 @@ Towns\register_routes($r); Fights\register_routes($r); Users\register_routes($r); Help\register_routes($r); +Forum\register_routes($r); $r->post('/move', 'move'); @@ -86,7 +87,7 @@ $r->post('/babblebox', 'babblebox'); // [code, handler, params, middleware] $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); -if ($l['code'] !== 200) exit($l['code']); +if ($l['code'] !== 200) exit("Error: {$l['code']}"); if (!empty($l['middleware'])) foreach ($l['middleware'] as $middleware) $middleware(); $l['handler'](...$l['params'] ?? []); diff --git a/public/forum.php b/src/forum.php similarity index 51% rename from public/forum.php rename to src/forum.php index 976408a..2b95453 100644 --- a/public/forum.php +++ b/src/forum.php @@ -2,59 +2,31 @@ // forum.php :: Internal forums script for the game. -require_once '../src/lib.php'; +namespace Forum; -if (!file_exists('../.installed')) redirect('install.php'); +use Router; -$controlrow = get_control_row(); - -// Login (or verify) if not logged in. -if (($userrow = checkcookies()) === false) { - if (isset($_GET['do']) && $_GET['do'] === 'verify') { - header("Location: users.php?do=verify"); - exit; - } - - header("Location: /login"); - exit; +function register_routes(Router $r): Router +{ + $r->get('/forum/thread/:x/:x', 'Forum\showthread'); + $r->get('/forum/new', 'Forum\newthread'); + $r->post('/forum/new', 'Forum\newthread'); + $r->post('/forum/reply', 'Forum\reply'); + $r->get('/forum/list/:x', 'Forum\donothing'); + $r->get('/forum', 'Forum\donothing'); + return $r; } -// Close game. -if ((bool) $controlrow["gameopen"] === false) { - display("The game is currently closed for maintanence. Please check back later.", "Game Closed"); - exit; -} - -// Force verify if the user isn't verified yet. -if ((bool) $controlrow["verifyemail"] && (bool) $userrow["verify"] === false) { - header("Location: users.php?do=verify"); - exit; -} - -// Block user if he/she has been banned. -if ($userrow["authlevel"] === 2) { - exit("Your account has been blocked."); -} - -$do = explode(':', $_GET['do'] ?? ''); -match ($do[0]) { - 'thread' => showthread($do[1], $do[2]), - 'new' => newthread(), - 'reply' => reply(), - 'list' => donothing($do[1]), - default => donothing() -}; - function donothing($start = 0) { $query = db()->query('SELECT * FROM forum WHERE parent=0 ORDER BY newpostdate DESC LIMIT 20 OFFSET ?;', [20 * $start]); - $page = "
\n"; + $page = "
New Thread
ThreadRepliesLast Post
\n"; $hasRows = false; while ($row = $query->fetchArray(SQLITE3_ASSOC)) { $hasRows = true; - $page .= "\n"; + $page .= "\n"; } if (!$hasRows) { @@ -71,12 +43,12 @@ function showthread($id, $start) $posts = db()->query('SELECT * FROM forum WHERE id=? OR parent=? ORDER BY id LIMIT 15 OFFSET ?;', [$id, $id, $start * 15]); $title = db()->query('SELECT title FROM forum WHERE id=? LIMIT 1;', [$id])->fetchArray(SQLITE3_ASSOC); - $page = "
New Thread
ThreadRepliesLast Post
".$row["title"]."".$row["replies"]."".$row["newpostdate"]."
".$row["title"]."".$row["replies"]."".$row["newpostdate"]."
\n"; + $page = "
Forum :: ".$title['title']."
\n"; while ($row = $posts->fetchArray(SQLITE3_ASSOC)) { $page .= "\n"; } $page .= "
Forum :: ".$title['title']."
".$row["author"]."

".prettyforumdate($row["postdate"])."
".nl2br($row["content"])."

"; - $page .= "
Reply To This Thread:

"; + $page .= "
Reply To This Thread:

"; display($page, "Forum"); } @@ -86,7 +58,7 @@ function reply() global $userrow; $form = validate($_POST, [ - 'title' => ['length:2-30', 'alphanum-spaces'], + 'title' => [], 'content' => [] ]); @@ -100,7 +72,7 @@ function reply() $userrow['username'], $form['title'], $form['content'], $form['parent'] ]); db()->query('UPDATE forum SET newpostdate=CURRENT_TIMESTAMP, replies=replies + 1 WHERE id=?;', [$form['parent']]); - redirect("forum.php?do=thread:{$form['parent']}:0"); + redirect("/forum/thread/{$form['parent']}/0"); } function newthread() @@ -109,7 +81,7 @@ function newthread() if (isset($_POST["submit"])) { $form = validate($_POST, [ - 'title' => ['length:2-30', 'alphanum-spaces'], + 'title' => ['length:2-30'], 'content' => [] ]); @@ -121,9 +93,9 @@ function newthread() db()->query('INSERT INTO forum (author, title, content) VALUES (?, ?, ?);', [ $userrow['username'], $form['title'], $form['content'] ]); - redirect('forum.php'); + redirect('/forum'); } - $page = "
Make A New Post:

Title:


Message:


"; + $page = "
Make A New Post:

Title:


Message:


"; display($page, "Forum"); } diff --git a/src/lib.php b/src/lib.php index 9a5879d..04b0832 100644 --- a/src/lib.php +++ b/src/lib.php @@ -134,7 +134,7 @@ function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true, $userrow["currenttown"] = ""; } - $userrow["forumslink"] = "Forum
"; + $userrow["forumslink"] = 'Forum
'; // Format various userrow stuffs... if ($userrow["latitude"] < 0) { $userrow["latitude"] = $userrow["latitude"] * -1 . "S"; } else { $userrow["latitude"] .= "N"; } diff --git a/src/router.php b/src/router.php index 67db739..2547927 100644 --- a/src/router.php +++ b/src/router.php @@ -174,10 +174,5 @@ class Router if (!str_starts_with($route, '/')) { throw new InvalidArgumentException("Route must start with a '/'"); } - - // Optional: Check for consecutive dynamic segments or invalid characters - if (preg_match('/(:x.*){2,}/', $route)) { - throw new InvalidArgumentException("Invalid route pattern: consecutive dynamic segments"); - } } }