| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- try {
- require "initialise.php";
- // Load routes
- require "routes.php";
- $path = http::value("path");
- if(empty($path)) $path = "home";
- // Loop through routes
- foreach($routes as $regex => $options) {
- // Check if path matches the route
- if(preg_match($regex, $path, $matches)) {
- // Get controller
- $controller = $options["controller"];
- Core::$controller = $controller;
- // Get route type
- $route_type = $options["type"] ?? "page";
- $require_log_in = $options["require_log_in"] ?? false;
- $require_admin = $options["require_admin"] ?? false;
- $require_user_level = $options["require_user_level"] ?? false;
- $allow_ajax = $options["allow_ajax"] ?? true;
- // Get params
- $params = $options["params"] ?? [];
- // Set param variables
- if($params) {
- foreach($params as $index => $name) {
- ${$name} = $matches[$index + 1];
- }
- }
- $controller_directory = "controllers";
- $controller_path = $controller_directory . "/" . $controller . ".php";
- if(!file_exists($controller_path)) throw new Exception("Controller \"$controller\" not found.");
- if($require_log_in) {
- Core::requireLogIn();
- }
- if(preg_match("/^account/", $path) && $path != "account" && !preg_match("/(settings|deactivate|reactivate|delete|accept-terms|decline-terms)$/", $path)) {
- Core::requireTerms();
- }
- if($require_admin) {
- Core::requireAdmin();
- }
- if($require_user_level) {
- Core::requireUserLevel($require_user_level);
- }
- if(!$allow_ajax) {
- Core::refuseAJAX();
- }
- if($path != "log-in") {
- unset($_SESSION["log_in_redirect_uri"]);
- }
- // Load the controller
- require $controller_path;
- // Session cleanup
- if(empty($_SESSION) && session_status() == PHP_SESSION_ACTIVE) {
- session_destroy();
- }
- // Quit
- exit;
- }
- }
- // 404 if no page was found
- require "controllers/404.php";
- }
- catch (Exception $e) {
- $route_type = $route_type ?? "page";
- // Force JSON exceptions for AJAX calls
- if(HTTP::isAJAX()) {
- $route_type = "json";
- }
- if($route_type == "page") {
- // Show error page
- $page_title = "Error";
- View::includeOnce("header", compact("page_title"));
- View::include("error", ["error_message" => $e->getMessage()]);
- View::includeOnce("footer");
- }
- else if($route_type == "json") {
- // JSON error
- JSON::header();
- JSON::fail(["reason" => $e->getMessage()]);
- }
- else {
- echo $e->getMessage();
- }
- }
- ?>
|