core.php 3.8 KB

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