core.php 3.9 KB

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