core.php 2.6 KB

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