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

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