2015-05-29 19:27:45 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Permission Handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
|
|
|
* Class Permissions
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class Permissions
|
|
|
|
{
|
2015-06-04 12:41:55 +00:00
|
|
|
// Fallback permission data
|
|
|
|
private static $fallback = [
|
|
|
|
|
2015-10-10 21:17:50 +00:00
|
|
|
'rank_id' => 0,
|
|
|
|
'user_id' => 0,
|
|
|
|
'permissions_site' => 1,
|
|
|
|
'permissions_manage' => 0,
|
|
|
|
'permissions_forums' => 0,
|
|
|
|
'permissions_inherit' => 111,
|
2015-06-04 12:41:55 +00:00
|
|
|
|
|
|
|
];
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Global permissions table
|
|
|
|
protected static $permissions = [
|
|
|
|
|
|
|
|
// Site permissions
|
|
|
|
'SITE' => [
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'DEACTIVATED' => 1, // Is a user deactivated
|
|
|
|
'RESTRICTED' => 2, // Is a user restricted
|
|
|
|
'ALTER_PROFILE' => 4, // Can alter their profile data
|
|
|
|
'CHANGE_AVATAR' => 8, // Can change their avatar
|
|
|
|
'CREATE_BACKGROUND' => 16, // Can create a background (different from changing)
|
|
|
|
'CHANGE_BACKGROUND' => 32, // Can change their background
|
|
|
|
'VIEW_MEMBERLIST' => 64, // Can view the memberlist
|
|
|
|
'CREATE_USERPAGE' => 128, // Can create a userpage (different from changing)
|
|
|
|
'CHANGE_USERPAGE' => 256, // Can change their userpage
|
|
|
|
'USE_MESSAGES' => 512, // Can use the Private Messaging system
|
|
|
|
'SEND_MESSAGES' => 1024, // Can send Private Messages to other users
|
|
|
|
'CHANGE_EMAIL' => 2048, // Can change their account e-mail address
|
|
|
|
'CHANGE_USERNAME' => 4096, // Can change their username
|
|
|
|
'CHANGE_USERTITLE' => 8192, // Can change their usertitle
|
|
|
|
'CHANGE_PASSWORD' => 16384, // Can change their password
|
|
|
|
'ALTER_RANKS' => 32768, // Can change their ranks
|
|
|
|
'MANAGE_SESSIONS' => 65536, // Can manage their sessions
|
|
|
|
'CREATE_REGKEYS' => 131072, // Can create registration keys
|
|
|
|
'DEACTIVATE_ACCOUNT' => 262144, // Can deactivate their account
|
|
|
|
'VIEW_PROFILE_DATA' => 524288, // Can view other's profile data
|
|
|
|
'MANAGE_FRIENDS' => 1048576, // Can manage friends (add/remove)
|
|
|
|
'REPORT_USERS' => 2097152, // Can report users to staff
|
|
|
|
'OBTAIN_PREMIUM' => 4194304, // Can obtain the premium rank
|
|
|
|
'JOIN_GROUPS' => 8388608, // Can join groups
|
|
|
|
'CREATE_GROUP' => 16777216, // Can create a group
|
|
|
|
'MULTIPLE_GROUPS' => 33554432, // Can create multiple groups (requires single group perm)
|
|
|
|
'CHANGE_NAMECOLOUR' => 67108864, // Can change their username colour
|
|
|
|
'STATIC_PREMIUM' => 134217728, // User has static premium status
|
2015-10-10 15:51:24 +00:00
|
|
|
'CREATE_COMMENTS' => 268435456, // User can make comments
|
|
|
|
'DELETE_COMMENTS' => 536870912, // User can delete own comments
|
|
|
|
'VOTE_COMMENTS' => 1073741824, // User can vote on comments
|
2015-10-10 21:17:50 +00:00
|
|
|
'CHANGE_SIGNATURE' => 2147483648, // User can vote on comments
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
// Forum permissions
|
|
|
|
'FORUM' => [
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'USE_FORUM' => 1,
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
// Site management permissions
|
|
|
|
'MANAGE' => [
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'USE_MANAGE' => 1,
|
2015-05-29 19:27:45 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
],
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
// Checking if a user has the permissions to do a thing
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function check($layer, $action, $operator, $mode = 0)
|
|
|
|
{
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Check if the permission layer and the permission itself exists
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!array_key_exists($layer, self::$permissions) || !array_key_exists($action, self::$permissions[$layer])) {
|
2015-05-29 19:27:45 +00:00
|
|
|
return false;
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 12:41:55 +00:00
|
|
|
// Convert to the appropiate mode
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($mode === 2) {
|
2015-06-04 12:41:55 +00:00
|
|
|
$operator = self::getRankPermissions($operator)[$layer];
|
2015-09-14 20:51:23 +00:00
|
|
|
} elseif ($mode === 1) {
|
2015-06-04 12:41:55 +00:00
|
|
|
$operator = self::getUserPermissions($operator)[$layer];
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Perform the bitwise AND
|
2015-09-14 20:51:23 +00:00
|
|
|
if (bindec($operator) & self::$permissions[$layer][$action]) {
|
2015-05-29 19:27:45 +00:00
|
|
|
return true;
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Else just return false
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get permission data of a rank from the database
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function getRankPermissions($ranks)
|
|
|
|
{
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Container array
|
2015-09-14 20:51:23 +00:00
|
|
|
$getRanks = [];
|
|
|
|
$perms = [];
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Get permission row for all ranks
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($ranks as $rank) {
|
2015-10-10 21:17:50 +00:00
|
|
|
$getRanks[] = Database::fetch('permissions', false, ['rank_id' => [$rank, '='], 'user_id' => [0, '=']]);
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 12:41:55 +00:00
|
|
|
// Check if getRanks is empty or if the rank id is 0 return the fallback
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($getRanks) || in_array(0, $ranks)) {
|
2015-06-04 12:41:55 +00:00
|
|
|
$getRanks = [self::$fallback];
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Go over the permission data
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($getRanks as $rank) {
|
2015-05-29 19:27:45 +00:00
|
|
|
// Check if perms is empty
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($perms)) {
|
2015-05-29 19:27:45 +00:00
|
|
|
// Store the data of the current rank in $perms
|
|
|
|
$perms = [
|
|
|
|
|
2015-10-10 21:17:50 +00:00
|
|
|
'SITE' => $rank['permissions_site'],
|
|
|
|
'MANAGE' => $rank['permissions_manage'],
|
|
|
|
'FORUM' => $rank['permissions_forums'],
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// Perform a bitwise OR on the ranks
|
|
|
|
$perms = [
|
|
|
|
|
2015-10-10 21:17:50 +00:00
|
|
|
'SITE' => $perms['SITE'] | $rank['permissions_site'],
|
|
|
|
'MANAGE' => $perms['MANAGE'] | $rank['permissions_manage'],
|
|
|
|
'FORUM' => $perms['FORUM'] | $rank['permissions_forums'],
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the compiled permission strings
|
|
|
|
return $perms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get permission data for a user
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function getUserPermissions($uid)
|
|
|
|
{
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Get user data
|
2015-09-16 20:34:36 +00:00
|
|
|
$user = new User($uid);
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Attempt to get the permission row of a user
|
2015-10-10 21:17:50 +00:00
|
|
|
$userPerms = Database::fetch('permissions', false, ['rank_id' => [0, '='], 'user_id' => [$user->data['user_id'], '=']]);
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Get their rank permissions
|
2015-10-10 21:17:50 +00:00
|
|
|
$rankPerms = self::getRankPermissions(json_decode($user->data['user_ranks'], true));
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Just return the rank permissions if no special ones are set
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($userPerms)) {
|
2015-05-29 19:27:45 +00:00
|
|
|
return $rankPerms;
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Split the inherit option things up
|
2015-10-10 21:17:50 +00:00
|
|
|
$inheritance = str_split($userPerms['permissions_inherit']);
|
2015-05-29 19:27:45 +00:00
|
|
|
|
|
|
|
// Override site permissions
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!$inheritance[0]) {
|
2015-10-10 21:17:50 +00:00
|
|
|
$rankPerms['SITE'] = $userPerms['permissions_site'];
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Override management permissions
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!$inheritance[1]) {
|
2015-10-10 21:17:50 +00:00
|
|
|
$rankPerms['MANAGE'] = $userPerms['permissions_manage'];
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Override forum permissions
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!$inheritance[2]) {
|
2015-10-10 21:17:50 +00:00
|
|
|
$rankPerms['FORUM'] = $userPerms['permissions_forums'];
|
2015-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 19:27:45 +00:00
|
|
|
// Return permissions
|
|
|
|
return $rankPerms;
|
|
|
|
}
|
|
|
|
}
|