1
0

core.php 4.0 KB

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