isFile() && fnmatch('*.php', $file->getFilename())) {
require_once $file;
}
}
}
loadClasses();
// Everything's loaded. Now we can start ticking.
Util::confirm_setup();
Session::start();
Session::generateCsrfToken();
$config = Config::load();
// Get request data
$method = $_SERVER['REQUEST_METHOD'];
$request = $_SERVER['REQUEST_URI'];
$path = parse_url($request, PHP_URL_PATH);
// return a 404 if a request for a .php file gets this far.
if (preg_match('/\.php$/', $path)) {
http_response_code(404);
echo '
404 Not Found
';
exit;
}
// Remove the base path from the URL
// and strip the trailing slash from the resulting route
if (strpos($path, $config->basePath) === 0) {
$path = substr($path, strlen($config->basePath));
}
$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'];
$routePattern = preg_replace('/\{([^}]+)\}/', '([^/]+)', $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;
}
$routeHandlers = [
['', 'HomeController'],
['', 'HomeController@handleTick', ['POST']],
['admin', 'AdminController'],
['admin', 'AdminController@save', ['POST']],
['login', 'AuthController@showLogin'],
['login', 'AuthController@handleLogin', ['POST']],
['logout', 'AuthController@handleLogout', ['GET', 'POST']],
['mood', 'MoodController'],
['mood', 'MoodController@handleMood', ['POST']],
['feed/rss', 'FeedController@rss'],
['feed/atom', 'FeedController@atom'],
];
// Set content type
header('Content-Type: text/html; charset=utf-8');
// Render the requested route or throw a 404
if (!route($path, $method, $routeHandlers)){
http_response_code(404);
echo "404 - Page Not Found";
exit;
}