| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php /** @noinspection PhpUnused */
- class Core {
- /**
- * @var User $user
- */
- 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 $allow_all_customers = false;
- public static $page = 1;
- public static $page_count = 1;
- public static $cookie_alert_shown = false;
- public static $scripts = [];
- public static $stylesheets = [
- "public/css/bootstrap.min.css",
- "public/css/daterangepicker.css",
- ];
- public static function locales() {
- return [
- "nl_NL" => "Dutch (Netherlands)",
- "en_GB" => "English (United Kingdom)",
- "es_ES" => "Spanish (Spain)",
- ];
- }
- 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];
- }
- /**
- * @param string $path Path relative to root directory
- */
- public static function addScript(string $path) {
- Core::$scripts[] = $path;
- }
- public static function addStylesheet($path) {
- Core::$stylesheets[] = $path; // Path is relative to root directory
- }
- public static function getScripts() {
- return Core::$scripts;
- }
- public static function getStylesheets() {
- return Core::$stylesheets;
- }
- 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");
- }
- }
- /**
- * @throws Exception
- */
- public static function requireAdmin() {
- Core::requireLogIn();
- if(Core::$user->canAdmin() === false) {
- throw new Exception("No permission to access this page.");
- }
- }
- /**
- * @param $level
- * @throws Exception
- */
- 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"]);
- }
- /**
- * @param string $token
- * @return bool
- * @throws Exception
- */
- public static function validateCSRFToken(string $token) {
- if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
- return true;
- }
- /**
- * @throws Exception
- */
- public static function refuseAJAX() {
- if(HTTP::isAJAX()) {
- header("HTTP/1.1 403 Forbidden");
- throw new Exception("AJAX calls not allowed.");
- }
- }
- public static function isMaintenanceMode(): bool {
- if(!defined("MAINTENANCE_MODE")) return false;
- return (bool) MAINTENANCE_MODE;
- }
- public static function getMaintenanceEndDate(): string {
- if(!defined("MAINTENANCE_END_DATE")) return "undefined";
- return MAINTENANCE_END_DATE;
- }
- public static function isProduction(): bool {
- return PRODUCTION;
- }
- public static function getLogo(): string {
- return Core::isProduction() ? "img/logo.png" : "img/dev_logo.png";
- }
- public static function getIcon(): string {
- return Core::isProduction() ? "img/icon.png" : "img/dev_icon.png";
- }
- }
|