core.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php /** @noinspection PhpUnused */
  2. class Core {
  3. /**
  4. * @var User $user
  5. */
  6. public static $user;
  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/js/Chart.bundle.min.js",
  19. "public/js/chartjs-plugin-datalabels.min.js",
  20. ];
  21. public static $stylesheets = [
  22. "public/css/bootstrap.min.css",
  23. "public/css/daterangepicker.css",
  24. "public/css/select2.min.css",
  25. "public/css/select2-bootstrap4.min.css",
  26. ];
  27. /**
  28. * @return string[]
  29. */
  30. public static function locales(): array {
  31. return [
  32. "nl_NL" => "Dutch (Netherlands)",
  33. "en_GB" => "English (United Kingdom)",
  34. "es_ES" => "Spanish (Spain)",
  35. ];
  36. }
  37. public static function addAlert($class, $message) {
  38. $alerts = $_SESSION["alerts"] ?? [];
  39. $alerts[] = [$class, $message];
  40. $_SESSION["alerts"] = $alerts;
  41. }
  42. public static function addBreadcrumb($url, $name) {
  43. Core::$breadcrumbs[] = [$url, $name];
  44. }
  45. /**
  46. * @param string $path Path relative to root directory
  47. */
  48. public static function addScript(string $path) {
  49. Core::$scripts[] = $path;
  50. }
  51. public static function addStylesheet($path) {
  52. Core::$stylesheets[] = $path; // Path is relative to root directory
  53. }
  54. /**
  55. * @return string[]
  56. */
  57. public static function getScripts(): array {
  58. return Core::$scripts;
  59. }
  60. /**
  61. * @return string[]
  62. */
  63. public static function getStylesheets(): array {
  64. return Core::$stylesheets;
  65. }
  66. public static function getAlerts() {
  67. $alerts = $_SESSION["alerts"] ?? [];
  68. unset($_SESSION["alerts"]);
  69. return $alerts;
  70. }
  71. public static function requireLogIn() {
  72. if(!Core::$user) {
  73. $_SESSION["log_in_redirect_uri"] = $_SERVER["REQUEST_URI"];
  74. http::redirect("/log-in");
  75. }
  76. }
  77. public static function requireTerms() {
  78. Core::requireLogIn();
  79. if(Core::$user->terms_accepted == false) {
  80. Core::addAlert("danger", "You must accept Terms and Conditions to view this page.");
  81. http::redirect("/account");
  82. }
  83. }
  84. /**
  85. * @throws Exception
  86. */
  87. public static function requireAdmin() {
  88. Core::requireLogIn();
  89. if(Core::$user->canAdmin() === false) {
  90. throw new Exception("No permission to access this page.");
  91. }
  92. }
  93. /**
  94. * @param $level
  95. * @throws Exception
  96. */
  97. public static function requireUserLevel($level) {
  98. Core::requireLogIn();
  99. if(Core::$user->level < $level) {
  100. throw new Exception("No permission to access this page.");
  101. }
  102. }
  103. public static function isLoggedIn(): bool {
  104. return !empty($_SESSION["user_id"]);
  105. }
  106. /**
  107. * @param string $token
  108. * @return bool
  109. * @throws Exception
  110. */
  111. public static function validateCSRFToken(string $token): bool {
  112. if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
  113. return true;
  114. }
  115. /**
  116. * @throws Exception
  117. */
  118. public static function refuseAJAX() {
  119. if(HTTP::isAJAX()) {
  120. header("HTTP/1.1 403 Forbidden");
  121. throw new Exception("AJAX calls not allowed.");
  122. }
  123. }
  124. public static function isMaintenanceMode(): bool {
  125. if(!defined("MAINTENANCE_MODE")) return false;
  126. return (bool) MAINTENANCE_MODE;
  127. }
  128. public static function getMaintenanceEndDate(): string {
  129. if(!defined("MAINTENANCE_END_DATE")) return "undefined";
  130. return MAINTENANCE_END_DATE;
  131. }
  132. public static function isProduction(): bool {
  133. return PRODUCTION;
  134. }
  135. public static function getLogo(): string {
  136. return Core::isProduction() ? "img/logo.png" : "img/dev_logo.png";
  137. }
  138. public static function getIcon(): string {
  139. return Core::isProduction() ? "img/icon.png" : "img/dev_icon.png";
  140. }
  141. public static function getLocale(): string {
  142. return Core::$user->locale ?? LOCALE;
  143. }
  144. public static function getLang(): string {
  145. $parts = explode("_", Core::getLocale());
  146. return $parts[0];
  147. }
  148. }