Move router to class. Fix CSS uploads.

This commit is contained in:
Greg Sarjeant 2025-06-12 17:06:18 -04:00
parent 2c330efc64
commit 093ece581c
4 changed files with 68 additions and 61 deletions

View File

@ -55,6 +55,8 @@ function validate_storage_dir(): void{
}
}
// validate that the required storage subdirectories exist
// attempt to create them if they don't
function validate_storage_subdirs(): void {
$storageSubdirs = array();
$storageSubdirs[] = CSS_UPLOAD_DIR;
@ -82,9 +84,6 @@ function validate_storage_subdirs(): void {
}
}
// Verify that the requested directory exists
// and optionally create it if it doesn't.
function get_db(): PDO {
try {
// SQLite will just create this if it doesn't exist.
@ -152,6 +151,8 @@ function create_tables(): void {
}
}
// make sure all tables exist
// attempt to create them if they don't
function validate_tables(): void {
$appTables = array();
$appTables[] = "settings";
@ -173,6 +174,7 @@ function validate_tables(): void {
}
}
// make sure tables that need to be seeded have been
function validate_table_contents(): void {
$db = get_db();

View File

@ -39,67 +39,11 @@ if (strpos($path, $config->basePath) === 0) {
// strip the trailing slash from the resulting route
$path = trim($path, '/');
// Main router function
function route(string $requestPath, string $requestMethod, array $routeHandlers): bool {
foreach ($routeHandlers as $routeHandler) {
$routePattern = $routeHandler[0];
$controller = $routeHandler[1];
$methods = $routeHandler[2] ?? ['GET'];
# Only allow valid route and filename characters
# to prevent directory traversal and other attacks
$routePattern = preg_replace('/\{([^}]+)\}/', '([a-zA-Z0-9._-]+)', $routePattern);
$routePattern = '#^' . $routePattern . '$#';
if (preg_match($routePattern, $requestPath, $matches)) {
if (in_array($requestMethod, $methods)){
// Save any path elements we're interested in
// (but discard the match on the entire path)
array_shift($matches);
if (strpos($controller, '@')) {
[$controllerName, $methodName] = explode('@', $controller);
} else {
// Default to 'index' method if no method specified
$controllerName = $controller;
$methodName = 'index';
}
$instance = new $controllerName();
call_user_func_array([$instance, $methodName], $matches);
return true;
}
}
}
return false;
}
// Define the recognized routes.
// Anything else will 404.
$routeHandlers = [
['', 'HomeController'],
['', 'HomeController@handleTick', ['POST']],
['admin', 'AdminController'],
['admin', 'AdminController@handleSave', ['POST']],
['admin/css', 'CssController'],
['admin/css', 'CssController@handlePost', ['POST']],
['feed/rss', 'FeedController@rss'],
['feed/atom', 'FeedController@atom'],
['login', 'AuthController@showLogin'],
['login', 'AuthController@handleLogin', ['POST']],
['logout', 'AuthController@handleLogout', ['GET', 'POST']],
['mood', 'MoodController'],
['mood', 'MoodController@handleMood', ['POST']],
['tick/{y}/{m}/{d}/{h}/{i}/{s}', 'TickController'],
['css/custom/{filename}.css', 'CssController@serveCustomCss'],
];
// Set content type
header('Content-Type: text/html; charset=utf-8');
// Render the requested route or throw a 404
if (!route($path, $method, $routeHandlers)){
if (!Router::route($path, $method)){
http_response_code(404);
echo "404 - Page Not Found";
exit;

View File

@ -0,0 +1,61 @@
<?php
// Very simple router class
class Router {
// Define the recognized routes.
// Anything else will 404.
private static $routeHandlers = [
['', 'HomeController'],
['', 'HomeController@handleTick', ['POST']],
['admin', 'AdminController'],
['admin', 'AdminController@handleSave', ['POST']],
['admin/css', 'CssController'],
['admin/css', 'CssController@handlePost', ['POST']],
['feed/rss', 'FeedController@rss'],
['feed/atom', 'FeedController@atom'],
['login', 'AuthController@showLogin'],
['login', 'AuthController@handleLogin', ['POST']],
['logout', 'AuthController@handleLogout', ['GET', 'POST']],
['mood', 'MoodController'],
['mood', 'MoodController@handleMood', ['POST']],
['tick/{y}/{m}/{d}/{h}/{i}/{s}', 'TickController'],
['css/custom/{filename}.css', 'CssController@serveCustomCss'],
];
// Main router function
public static function route(string $requestPath, string $requestMethod): bool {
foreach (self::$routeHandlers as $routeHandler) {
$routePattern = $routeHandler[0];
$controller = $routeHandler[1];
$methods = $routeHandler[2] ?? ['GET'];
# Only allow valid route and filename characters
# to prevent directory traversal and other attacks
$routePattern = preg_replace('/\{([^}]+)\}/', '([a-zA-Z0-9._-]+)', $routePattern);
$routePattern = '#^' . $routePattern . '$#';
if (preg_match($routePattern, $requestPath, $matches)) {
if (in_array($requestMethod, $methods)){
// Save any path elements we're interested in
// (but discard the match on the entire path)
array_shift($matches);
if (strpos($controller, '@')) {
[$controllerName, $methodName] = explode('@', $controller);
} else {
// Default to 'index' method if no method specified
$controllerName = $controller;
$methodName = 'index';
}
$instance = new $controllerName();
call_user_func_array([$instance, $methodName], $matches);
return true;
}
}
}
return false;
}
}

View File

@ -33,7 +33,7 @@ class CssModel {
$stmt = $db->prepare("SELECT COUNT(id) FROM css WHERE filename = ?");
$stmt->execute([$filename]);
$fileExists = $stmt->fetch();
$fileExists = $stmt->fetchColumn();
if ($fileExists) {
$stmt = $db->prepare("UPDATE css SET description = ? WHERE filename = ?");