core.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. class Core {
  3. /**
  4. * @var User $user
  5. */
  6. public static $user = null;
  7. public static $admin = false;
  8. public static $controller = "404";
  9. public static $breadcrumbs = [];
  10. public static $use_default_container = true;
  11. public static $show_alerts = true;
  12. public static $show_footer_content = true;
  13. public static $page = 1;
  14. public static $page_count = 1;
  15. public static $cookie_alert_shown = false;
  16. public static $scripts = [];
  17. public static $stylesheets = [
  18. "public/css/bootstrap.min.css",
  19. "public/css/daterangepicker.css",
  20. ];
  21. public static function locales() {
  22. return [
  23. "nl_NL" => "Dutch (Netherlands)",
  24. "en_GB" => "English (United Kingdom)",
  25. "es_ES" => "Spanish (Spain)",
  26. ];
  27. }
  28. public static function addAlert($class, $message) {
  29. $alerts = $_SESSION["alerts"] ?? [];
  30. $alerts[] = [$class, $message];
  31. $_SESSION["alerts"] = $alerts;
  32. }
  33. public static function addBreadcrumb($url, $name) {
  34. Core::$breadcrumbs[] = [$url, $name];
  35. }
  36. public static function addScript($path) {
  37. Core::$scripts[] = $path; // Path is relative to root directory
  38. }
  39. public static function addStylesheet($path) {
  40. Core::$stylesheets[] = $path; // Path is relative to root directory
  41. }
  42. public static function getScripts() {
  43. return Core::$scripts;
  44. }
  45. public static function getStylesheets() {
  46. return Core::$stylesheets;
  47. }
  48. public static function getAlerts() {
  49. $alerts = $_SESSION["alerts"] ?? [];
  50. unset($_SESSION["alerts"]);
  51. return $alerts;
  52. }
  53. public static function requireLogIn() {
  54. if(!Core::$user) {
  55. $_SESSION["log_in_redirect_uri"] = $_SERVER["REQUEST_URI"];
  56. http::redirect("/log-in");
  57. }
  58. }
  59. public static function requireTerms() {
  60. Core::requireLogIn();
  61. if(Core::$user->terms_accepted == false) {
  62. Core::addAlert("danger", "You must accept Terms and Conditions to view this page.");
  63. http::redirect("/account");
  64. }
  65. }
  66. public static function requireAdmin() {
  67. Core::requireLogIn();
  68. if(Core::$user->canAdmin() === false) {
  69. throw new Exception("No permission to access this page.");
  70. }
  71. }
  72. public static function requireUserLevel($level) {
  73. Core::requireLogIn();
  74. if(Core::$user->level < $level) {
  75. throw new Exception("No permission to access this page.");
  76. }
  77. }
  78. public static function isLoggedIn() {
  79. return !empty($_SESSION["user_id"]);
  80. }
  81. public static function validateCSRFToken($token) {
  82. if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
  83. return true;
  84. }
  85. public static function refuseAJAX() {
  86. if(HTTP::isAJAX()) {
  87. header("HTTP/1.1 403 Forbidden");
  88. throw new Exception("AJAX calls not allowed.");
  89. }
  90. }
  91. }
  92. ?>