core.php 1.7 KB

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