1
0

core.php 1.7 KB

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