| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- class Core {
- public static $user = null;
- public static $admin = false;
- public static $controller = "404";
- public static $breadcrumbs = [];
- public static $use_default_container = true;
- public static $show_alerts = true;
- public static $show_footer_content = true;
- public static $page = 1;
- public static $page_count = 1;
- public static $cookie_alert_shown = false;
- public static $scripts = [];
- public static function addAlert($class, $message) {
- $alerts = $_SESSION["alerts"] ?? [];
- $alerts[] = [$class, $message];
- $_SESSION["alerts"] = $alerts;
- }
- public static function addBreadcrumb($url, $name) {
- Core::$breadcrumbs[] = [$url, $name];
- }
- public static function addScript($path) {
- Core::$scripts[] = $path; // Path is relative to root directory
- }
- public static function getScripts() {
- return Core::$scripts;
- }
- public static function getAlerts() {
- $alerts = $_SESSION["alerts"] ?? [];
- unset($_SESSION["alerts"]);
- return $alerts;
- }
- public static function requireLogIn() {
- if(!Core::$user) {
- $_SESSION["log_in_redirect_uri"] = $_SERVER["REQUEST_URI"];
- http::redirect("/log-in");
- }
- }
- public static function requireTerms() {
- Core::requireLogIn();
- if(Core::$user->terms_accepted == false) {
- Core::addAlert("danger", "You must accept Terms and Conditions to view this page.");
- http::redirect("/account");
- }
- }
- public static function requireAdmin() {
- Core::requireLogIn();
- if(Core::$user->canAdmin() === false) {
- throw new Exception("No permission to access this page.");
- }
- }
- public static function requireUserLevel($level) {
- Core::requireLogIn();
- if(Core::$user->level < $level) {
- throw new Exception("No permission to access this page.");
- }
- }
- public static function isLoggedIn() {
- return !empty($_SESSION["user_id"]);
- }
- public static function validateCSRFToken($token) {
- if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
- return true;
- }
- public static function refuseAJAX() {
- if(HTTP::isAJAX()) {
- header("HTTP/1.1 403 Forbidden");
- throw new Exception("AJAX calls not allowed.");
- }
- }
- }
- ?>
|