Move router to class. Fix CSS uploads.
This commit is contained in:
parent
2c330efc64
commit
093ece581c
@ -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 {
|
function validate_storage_subdirs(): void {
|
||||||
$storageSubdirs = array();
|
$storageSubdirs = array();
|
||||||
$storageSubdirs[] = CSS_UPLOAD_DIR;
|
$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 {
|
function get_db(): PDO {
|
||||||
try {
|
try {
|
||||||
// SQLite will just create this if it doesn't exist.
|
// 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 {
|
function validate_tables(): void {
|
||||||
$appTables = array();
|
$appTables = array();
|
||||||
$appTables[] = "settings";
|
$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 {
|
function validate_table_contents(): void {
|
||||||
$db = get_db();
|
$db = get_db();
|
||||||
|
|
||||||
|
@ -39,67 +39,11 @@ if (strpos($path, $config->basePath) === 0) {
|
|||||||
// strip the trailing slash from the resulting route
|
// strip the trailing slash from the resulting route
|
||||||
$path = trim($path, '/');
|
$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
|
// Set content type
|
||||||
header('Content-Type: text/html; charset=utf-8');
|
header('Content-Type: text/html; charset=utf-8');
|
||||||
|
|
||||||
// Render the requested route or throw a 404
|
// Render the requested route or throw a 404
|
||||||
if (!route($path, $method, $routeHandlers)){
|
if (!Router::route($path, $method)){
|
||||||
http_response_code(404);
|
http_response_code(404);
|
||||||
echo "404 - Page Not Found";
|
echo "404 - Page Not Found";
|
||||||
exit;
|
exit;
|
||||||
|
61
src/Framework/Router/Router.php
Normal file
61
src/Framework/Router/Router.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -33,7 +33,7 @@ class CssModel {
|
|||||||
|
|
||||||
$stmt = $db->prepare("SELECT COUNT(id) FROM css WHERE filename = ?");
|
$stmt = $db->prepare("SELECT COUNT(id) FROM css WHERE filename = ?");
|
||||||
$stmt->execute([$filename]);
|
$stmt->execute([$filename]);
|
||||||
$fileExists = $stmt->fetch();
|
$fileExists = $stmt->fetchColumn();
|
||||||
|
|
||||||
if ($fileExists) {
|
if ($fileExists) {
|
||||||
$stmt = $db->prepare("UPDATE css SET description = ? WHERE filename = ?");
|
$stmt = $db->prepare("UPDATE css SET description = ? WHERE filename = ?");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user