diff --git a/config/bootstrap.php b/config/bootstrap.php index 47a27d5..3c27742 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -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(); diff --git a/public/index.php b/public/index.php index 9c55648..bbfb57d 100644 --- a/public/index.php +++ b/public/index.php @@ -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; diff --git a/src/Framework/Router/Router.php b/src/Framework/Router/Router.php new file mode 100644 index 0000000..7fb1c9f --- /dev/null +++ b/src/Framework/Router/Router.php @@ -0,0 +1,61 @@ +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 = ?");