core.php 2.9 KB

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