BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

permissions.class.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. #declare(strict_types=1);
  3. class Permissions
  4. {
  5. /* Check to see if a user has the permission to perform an action
  6. * This is called by check_perms in util.php, for convenience.
  7. *
  8. * @param string PermissionName
  9. * @param string $MinClass Return false if the user's class level is below this.
  10. */
  11. public static function check_perms($PermissionName, $MinClass = 0)
  12. {
  13. if (G::$LoggedUser['EffectiveClass'] >= 1000) {
  14. return true;
  15. } // Sysops can do anything
  16. if (G::$LoggedUser['EffectiveClass'] < $MinClass) {
  17. return false;
  18. } // MinClass failure
  19. return G::$LoggedUser['Permissions'][$PermissionName] ?? false; // Return actual permission
  20. }
  21. /**
  22. * Gets the permissions associated with a certain permissionid
  23. *
  24. * @param int $PermissionID the kind of permissions to fetch
  25. * @return array permissions
  26. */
  27. public static function get_permissions($PermissionID)
  28. {
  29. $Permission = G::$Cache->get_value("perm_$PermissionID");
  30. if (empty($Permission)) {
  31. $QueryID = G::$DB->get_query_id();
  32. G::$DB->query("
  33. SELECT Level AS Class, `Values` AS Permissions, Secondary, PermittedForums
  34. FROM permissions
  35. WHERE ID = '$PermissionID'");
  36. $Permission = G::$DB->next_record(MYSQLI_ASSOC, ['Permissions']);
  37. G::$DB->set_query_id($QueryID);
  38. $Permission['Permissions'] = unserialize($Permission['Permissions']);
  39. G::$Cache->cache_value("perm_$PermissionID", $Permission, 2592000);
  40. }
  41. return $Permission;
  42. }
  43. /**
  44. * Get a user's permissions.
  45. *
  46. * @param $UserID
  47. * @param array|false $CustomPermissions
  48. * Pass in the user's custom permissions if you already have them.
  49. * Leave false if you don't have their permissions. The function will fetch them.
  50. * @return array Mapping of PermissionName=>bool/int
  51. */
  52. public static function get_permissions_for_user($UserID, $CustomPermissions = false)
  53. {
  54. $UserInfo = Users::user_info($UserID);
  55. // Fetch custom permissions if they weren't passed in.
  56. if ($CustomPermissions === false) {
  57. $QueryID = G::$DB->get_query_id();
  58. G::$DB->query('
  59. SELECT CustomPermissions
  60. FROM users_main
  61. WHERE ID = ' . (int)$UserID);
  62. list($CustomPermissions) = G::$DB->next_record(MYSQLI_NUM, false);
  63. G::$DB->set_query_id($QueryID);
  64. }
  65. if (!empty($CustomPermissions) && !is_array($CustomPermissions)) {
  66. $CustomPermissions = unserialize($CustomPermissions);
  67. }
  68. $Permissions = self::get_permissions($UserInfo['PermissionID']);
  69. // Manage 'special' inherited permissions
  70. $BonusPerms = [];
  71. $BonusCollages = 0;
  72. foreach ($UserInfo['ExtraClasses'] as $PermID => $Value) {
  73. $ClassPerms = self::get_permissions($PermID);
  74. $BonusCollages += $ClassPerms['Permissions']['MaxCollages'];
  75. unset($ClassPerms['Permissions']['MaxCollages']);
  76. $BonusPerms = array_merge($BonusPerms, $ClassPerms['Permissions']);
  77. }
  78. if (empty($CustomPermissions)) {
  79. $CustomPermissions = [];
  80. }
  81. $MaxCollages = ($Permissions['Permissions']['MaxCollages'] ?? 0) + $BonusCollages;
  82. if (isset($CustomPermissions['MaxCollages'])) {
  83. $MaxCollages += $CustomPermissions['MaxCollages'];
  84. unset($CustomPermissions['MaxCollages']);
  85. }
  86. $Permissions['Permissions']['MaxCollages'] = $MaxCollages;
  87. // Combine the permissions
  88. return array_merge(
  89. $Permissions['Permissions'],
  90. $BonusPerms,
  91. $CustomPermissions
  92. );
  93. }
  94. public static function is_mod($UserID)
  95. {
  96. return self::get_permissions_for_user($UserID)['users_mod'] ?? false;
  97. }
  98. }