core.php 2.1 KB

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