diff --git a/app/bootstrap.php b/app/bootstrap.php
index 61dd45c..b534d00 100644
--- a/app/bootstrap.php
+++ b/app/bootstrap.php
@@ -8,11 +8,11 @@
*/
ini_set('display_errors', 'On');
-error_reporting(E_ALL | E_STRICT);
+error_reporting(E_ALL);
session_start();
-require_once('../app/library.php');
+require_once '../app/library.php';
// ---------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------- //
@@ -26,6 +26,7 @@ const MAP = [
'Show' => 'models/Show.php',
'User' => 'models/User.php',
+ 'Router' => 'router.php',
'Database' => 'modules/Database.php',
'CommunityModule' => 'modules/CommunityModule.php',
'DisplayModule' => 'modules/DisplayModule.php',
diff --git a/app/library.php b/app/library.php
index 95d8531..1557665 100755
--- a/app/library.php
+++ b/app/library.php
@@ -71,15 +71,15 @@
// Hash a password thousands of times using a random salt.
function hashPass($password, $salt, $username = "failure") {
for($round = 0; $round < 124363; $round++) {
- $HashedPass = hash("sha512", $username . $salt . $password);
+ $HashedPass = hash("sha512", $username . $salt . $password);
}
return $HashedPass;
}
function generateSalt(int $length = 15): string
- {
- return bin2hex(random_bytes($length));
+ {
+ return bin2hex(random_bytes($length));
}
// Validate the email address inputted!
diff --git a/app/modules/DisplayModule.php b/app/modules/DisplayModule.php
index 9553720..dc197b5 100755
--- a/app/modules/DisplayModule.php
+++ b/app/modules/DisplayModule.php
@@ -40,27 +40,18 @@
/* --- Module Functions - the meat and purpose of the module. --- */
public function Articles($limit) {
+ $articles = Database::select('articles', 'id', '', 'articleDate DESC', $limit)->fetchAll();
- $db = new DatabaseModule();
-
- $articles = $db->getData('ms_articles', 'id', '', 'LIMIT ' . $limit, 'ORDER BY articleDate DESC');
-
- if($db->countRows('ms_articles', '') > 0) {
-
+ if(count($articles)) {
foreach($articles as $articleID) {
-
$article = new Article($articleID['id']);
$author = new User($article->Author);
- echo ParseTemplate($this->ListedArticle, array('t' => magicClean($article->Title), 'l' => "blog.php?do=read&article=" . $article->ID, 'd' => BBCode(magicClean($article->Description)), 'co' => $this->BlogCovers . $article->Cover, 'u' => $author->Username, 'pd' => $article->PostDate, 'c' => $article->Comments));
-
+ echo render('articles/listedarticle', ['t' => magicClean($article->Title), 'l' => "blog.php?do=read&article=" . $article->ID, 'd' => BBCode(magicClean($article->Description)), 'co' => $this->BlogCovers . $article->Cover, 'u' => $author->Username, 'pd' => $article->PostDate, 'c' => $article->Comments]);
}
-
} else {
-
echo 'Hmm. Strangely enough, we don\'t have any articles!';
}
-
}
/* ------------------------------------------------------------------------------------------------------- */
@@ -68,7 +59,7 @@
/* Display Articles - display a slider-like list of projects. */
public function Projects($type) {
-
+/*
$db = new DatabaseModule();
$projects = $db->getData('ms_projects', 'id', 'WHERE type="' . $type . '"', '', 'ORDER BY id DESC');
@@ -91,7 +82,7 @@
echo "
It seems we have no " . strtolower($type) . "s.
";
}
-
+*/
}
/* ------------------------------------------------------------------------------------------------------- */
@@ -99,6 +90,7 @@
/* Display Featured - display an assorted set of featured content in the sidebar. */
public function sideFeatured($what) {
+ /*
$db = new DatabaseModule;
// prepare SQL statements
@@ -116,6 +108,7 @@
// display the article
echo ParseTemplate($this->FeaturedArticle, array('id' => $featured->ID, 'c' => $this->BlogCovers . $featured->Cover, 't' => $featured->Title));
}
+ */
}
/* ------------------------------------------------------------------------------------------------------- */
@@ -123,6 +116,7 @@
/* Display Front Page Articles - specially formatted for the front page */
public function frontPageArticles() {
+ /*
$db = new DatabaseModule();
$getArticles = $db->Handle->prepare('SELECT id FROM ms_articles ORDER BY articleDate DESC LIMIT 5');
@@ -168,6 +162,7 @@ FIRST;
ARTICLE;
}
}
+ */
}
/* ------------------------------------------------------------------------------------------------------- */
@@ -175,15 +170,15 @@ ARTICLE;
/* Display an article for reading - when a user wants to read an article, we give it to them in this format. */
public function articleForReading($id) {
-
+ /*
$article = new Article($id);
$author = new User($article->Author);
echo ParseTemplate($this->Article4Reading, array('title' => $article->Title, 'date' => $article->PostDate, 'author' => $author->Username, 'cover' => $this->BlogCovers . $article->Cover, 'content' => BBCode(magicClean($article->Content))));
-
+ */
/* ----------------------------------- */
/* -------- Article Comments --------- */
-
+ /*
echo "";
if($article->Comments == 1) { $oneOrMore = $article->Comments . " Comment"; } else { $oneOrMore = $article->Comments . " Comments"; }
@@ -218,7 +213,7 @@ ARTICLE;
if(!empty($_COOKIE['UserCookie'])) { echo ParseTemplate($this->CommentForm, array('aid' => $_GET['article'])); }
echo '';
-
+ */
}
/* ------------------------------------------------------------------------------------------------------- */
diff --git a/app/router.php b/app/router.php
new file mode 100644
index 0000000..eacdcdf
--- /dev/null
+++ b/app/router.php
@@ -0,0 +1,183 @@
+add($routes, 'GET', '/posts/:id', function($id) { echo "Viewing post $id"; });`
+ */
+ public function add(string $method, string $route, callable $handler): Router
+ {
+ $this->validateMethod($method);
+ $this->validateRoute($route);
+
+ $segments = $route === '/' ? [''] : explode('/', trim($route, '/'));
+
+ $node = &$this->routes;
+ foreach ($segments as $segment) {
+ $segment = str_starts_with($segment, ':') ? ':x' : $segment;
+ if ($segment === '') continue;
+ $node = &$node[$segment];
+ }
+
+ $node[$method] = [
+ 'handler' => $handler,
+ 'middleware' => []
+ ];
+
+ $this->last_inserted_node = &$node[$method];
+
+ return $this;
+ }
+
+ /**
+ * Perform a lookup in the route tree for a given method and URI. Returns an array with a result code,
+ * a handler if found, and any dynamic parameters. Codes are 200 for success, 404 for not found, and
+ * 405 for method not allowed.
+ *
+ * @return array ['code', 'handler', 'params']
+ */
+ public function lookup(string $method, string $uri): array|int
+ {
+ $node = $this->routes;
+ $params = [];
+
+ if ($uri === '/') return $node[$method] ?? 405;
+
+ foreach (explode('/', trim($uri, '/')) as $segment) {
+ if (isset($node[$segment])) {
+ $node = $node[$segment];
+ continue;
+ }
+
+ if (isset($node[':x'])) {
+ $params[] = $segment;
+ $node = $node[':x'];
+ continue;
+ }
+
+ return 404;
+ }
+
+ $node[$method]['params'] = $params;
+ return $node[$method] ?? 405;
+ }
+
+ /**
+ * Add a middleware function to the last inserted node's stack.
+ */
+ public function middleware(callable $middleware): Router
+ {
+ $this->last_inserted_node['middleware'][] = $middleware;
+ return $this;
+ }
+
+ /**
+ * Shorthand to register a GET route.
+ */
+ public function get(string $route, callable $handler): Router
+ {
+ return $this->add('GET', $route, $handler);
+ }
+
+ /**
+ * Shorthand to register a POST route.
+ */
+ public function post(string $route, callable $handler): Router
+ {
+ return $this->add('POST', $route, $handler);
+ }
+
+ /**
+ * Shorthand to register a PUT route.
+ */
+ public function put(string $route, callable $handler): Router
+ {
+ return $this->add('PUT', $route, $handler);
+ }
+
+ /**
+ * Shorthand to register a DELETE route.
+ */
+ public function delete(string $route, callable $handler): Router
+ {
+ return $this->add('DELETE', $route, $handler);
+ }
+
+ /**
+ * Shorthand to register a PATCH route.
+ */
+ public function patch(string $route, callable $handler): Router
+ {
+ return $this->add('PATCH', $route, $handler);
+ }
+
+ /**
+ * Register multiple verbs to the same route.
+ */
+ public function many(array $methods, string $route, callable $handler): Router
+ {
+ foreach ($methods as $method) $this->add($method, $route, $handler);
+ return $this;
+ }
+
+ /**
+ * Register all verbs to the same route.
+ */
+ public function any(string $route, callable $handler): Router
+ {
+ foreach (SELF::VALID_METHODS as $method) $this->add($method, $route, $handler);
+ return $this;
+ }
+
+ /**
+ * Some pages function entirely as forms; thus we can shorthand a GET/POST route.
+ */
+ public function form(string $route, callable $handler): Router
+ {
+ return $this->many(['GET', 'POST'], $route, $handler);
+ }
+
+ /**
+ * Validate the given method against valid HTTP verbs.
+ */
+ private function validateMethod(string $method): void
+ {
+ if (!in_array($method, self::VALID_METHODS)) {
+ throw new InvalidArgumentException("Invalid HTTP method: $method");
+ }
+ }
+
+ /**
+ * Validate that a new route follows expected formatting.
+ */
+ private function validateRoute(string $route): void
+ {
+ if ($route === '') {
+ throw new InvalidArgumentException("Route cannot be empty");
+ }
+
+ // Ensure route starts with a slash
+ if (!str_starts_with($route, '/')) {
+ throw new InvalidArgumentException("Route must start with a '/'");
+ }
+ }
+}
diff --git a/app/templates/blog/list.php b/app/templates/blog/list.php
index 4078b36..9ee5fcc 100755
--- a/app/templates/blog/list.php
+++ b/app/templates/blog/list.php
@@ -5,9 +5,7 @@
Mad Splash Blog
- Articles(30);
- ?>
+ = $display->Articles(30); ?>
diff --git a/app/templates/blog/read.php b/app/templates/blog/read.php
index 86a91ae..b9ac955 100755
--- a/app/templates/blog/read.php
+++ b/app/templates/blog/read.php
@@ -6,12 +6,6 @@
articleForReading($id);
?>
diff --git a/app/templates/footer.php b/app/templates/footer.php
index d679d51..0560671 100755
--- a/app/templates/footer.php
+++ b/app/templates/footer.php
@@ -1,32 +1,30 @@
-
-