core.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. class Core {
  3. public static $user = null;
  4. public static $admin = false;
  5. public static $controller = "404";
  6. public static $breadcrumbs = [];
  7. public static $use_default_container = true;
  8. public static $show_alerts = true;
  9. public static $show_footer_content = true;
  10. public static $page = 1;
  11. public static $page_count = 1;
  12. public static $cookie_alert_shown = false;
  13. public static $scripts = [];
  14. public static $stylesheets = ["public/css/bootstrap.min.css"];
  15. public static function locales() {
  16. return [
  17. "nl_NL" => "Dutch (Netherlands)",
  18. "en_GB" => "English (United Kingdom)",
  19. "es_ES" => "Spanish (Spain)",
  20. ];
  21. }
  22. public static function addAlert($class, $message) {
  23. $alerts = $_SESSION["alerts"] ?? [];
  24. $alerts[] = [$class, $message];
  25. $_SESSION["alerts"] = $alerts;
  26. }
  27. public static function addBreadcrumb($url, $name) {
  28. Core::$breadcrumbs[] = [$url, $name];
  29. }
  30. public static function addScript($path) {
  31. Core::$scripts[] = $path; // Path is relative to root directory
  32. }
  33. public static function addStylesheet($path) {
  34. Core::$stylesheets[] = $path; // Path is relative to root directory
  35. }
  36. public static function getScripts() {
  37. return Core::$scripts;
  38. }
  39. public static function getStylesheets() {
  40. return Core::$stylesheets;
  41. }
  42. public static function getAlerts() {
  43. $alerts = $_SESSION["alerts"] ?? [];
  44. unset($_SESSION["alerts"]);
  45. return $alerts;
  46. }
  47. public static function requireLogIn() {
  48. if(!Core::$user) {
  49. $_SESSION["log_in_redirect_uri"] = $_SERVER["REQUEST_URI"];
  50. http::redirect("/log-in");
  51. }
  52. }
  53. public static function requireTerms() {
  54. Core::requireLogIn();
  55. if(Core::$user->terms_accepted == false) {
  56. Core::addAlert("danger", "You must accept Terms and Conditions to view this page.");
  57. http::redirect("/account");
  58. }
  59. }
  60. public static function requireAdmin() {
  61. Core::requireLogIn();
  62. if(Core::$user->canAdmin() === false) {
  63. throw new Exception("No permission to access this page.");
  64. }
  65. }
  66. public static function requireUserLevel($level) {
  67. Core::requireLogIn();
  68. if(Core::$user->level < $level) {
  69. throw new Exception("No permission to access this page.");
  70. }
  71. }
  72. public static function isLoggedIn() {
  73. return !empty($_SESSION["user_id"]);
  74. }
  75. public static function validateCSRFToken($token) {
  76. if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
  77. return true;
  78. }
  79. public static function refuseAJAX() {
  80. if(HTTP::isAJAX()) {
  81. header("HTTP/1.1 403 Forbidden");
  82. throw new Exception("AJAX calls not allowed.");
  83. }
  84. }
  85. }
  86. ?>