Oppaitime'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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?
  2. class Permissions {
  3. /* Check to see if a user has the permission to perform an action
  4. * This is called by check_perms in util.php, for convenience.
  5. *
  6. * @param string PermissionName
  7. * @param string $MinClass Return false if the user's class level is below this.
  8. */
  9. public static function check_perms($PermissionName, $MinClass = 0) {
  10. $OverrideClass = 1000;
  11. $Override = G::$LoggedUser['EffectiveClass'] >= $OverrideClass;
  12. return (
  13. ($PermissionName == null
  14. || (isset(G::$LoggedUser['Permissions'][$PermissionName])
  15. && G::$LoggedUser['Permissions'][$PermissionName]))
  16. && G::$LoggedUser['Permissions'][$PermissionName]
  17. && (G::$LoggedUser['Class'] >= $MinClass
  18. || G::$LoggedUser['EffectiveClass'] >= $MinClass
  19. || $Override)
  20. );
  21. }
  22. /**
  23. * Gets the permissions associated with a certain permissionid
  24. *
  25. * @param int $PermissionID the kind of permissions to fetch
  26. * @return array permissions
  27. */
  28. public static function get_permissions($PermissionID) {
  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, array('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. $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 = array();
  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 = array();
  79. }
  80. $MaxCollages = $Permissions['Permissions']['MaxCollages'] + $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. public static function is_mod($UserID) {
  93. $Permissions = self::get_permissions_for_user($UserID);
  94. return isset($Permissions['users_mod']) && $Permissions['users_mod'];
  95. }
  96. }
  97. ?>