core.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. class Core {
  3. public static $user = null;
  4. public static $admin = false;
  5. public static $breadcrumbs = [];
  6. public static $use_default_container = true;
  7. public static $show_alerts = true;
  8. public static $show_footer_content = true;
  9. public static $page = 1;
  10. public static $page_count = 1;
  11. public static $cookie_alert_shown = false;
  12. public static $scripts = [];
  13. public static function addAlert($class, $message) {
  14. $alerts = $_SESSION["alerts"] ?? [];
  15. $alerts[] = [$class, $message];
  16. $_SESSION["alerts"] = $alerts;
  17. }
  18. public static function addBreadcrumb($url, $name) {
  19. Core::$breadcrumbs[] = [$url, $name];
  20. }
  21. public static function addScript($path) {
  22. Core::$scripts[] = $path;
  23. }
  24. public static function getScripts() {
  25. return Core::$scripts;
  26. }
  27. public static function getAlerts() {
  28. $alerts = $_SESSION["alerts"] ?? [];
  29. unset($_SESSION["alerts"]);
  30. return $alerts;
  31. }
  32. public static function requireLogIn() {
  33. if(!Core::$user) {
  34. $_SESSION["log_in_redirect_uri"] = $_SERVER["REQUEST_URI"];
  35. http::redirect("/log-in");
  36. }
  37. }
  38. public static function requireAdmin() {
  39. Core::requireLogIn();
  40. if(Core::$user->canAdmin() === false) {
  41. throw new Exception("No permission to access this page.");
  42. }
  43. }
  44. public static function requireUserLevel($level) {
  45. Core::requireLogIn();
  46. if(Core::$user->level < $level) {
  47. throw new Exception("No permission to access this page.");
  48. }
  49. }
  50. public static function isLoggedIn() {
  51. return !empty($_SESSION["user_id"]);
  52. }
  53. public static function validateCSRFToken($token) {
  54. if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
  55. return true;
  56. }
  57. public static function refuseAJAX() {
  58. if(HTTP::isAJAX()) {
  59. header("HTTP/1.1 403 Forbidden");
  60. throw new Exception("AJAX calls not allowed.");
  61. }
  62. }
  63. }
  64. ?>