1
0

core.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 requireTerms() {
  39. Core::requireLogIn();
  40. if(Core::$user->terms_accepted == false) {
  41. Core::addAlert("danger", "You must accept Terms and Conditions to view this page.");
  42. http::redirect("/account");
  43. }
  44. }
  45. public static function requireAdmin() {
  46. Core::requireLogIn();
  47. if(Core::$user->canAdmin() === false) {
  48. throw new Exception("No permission to access this page.");
  49. }
  50. }
  51. public static function requireUserLevel($level) {
  52. Core::requireLogIn();
  53. if(Core::$user->level < $level) {
  54. throw new Exception("No permission to access this page.");
  55. }
  56. }
  57. public static function isLoggedIn() {
  58. return !empty($_SESSION["user_id"]);
  59. }
  60. public static function validateCSRFToken($token) {
  61. if($_SESSION["csrf_token"] !== $token) throw new Exception("CSRF attempt blocked.");
  62. return true;
  63. }
  64. public static function refuseAJAX() {
  65. if(HTTP::isAJAX()) {
  66. header("HTTP/1.1 403 Forbidden");
  67. throw new Exception("AJAX calls not allowed.");
  68. }
  69. }
  70. }
  71. ?>