core.php 2.3 KB

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