Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- @Filename : permissions.class.php
- @Description : Comprehensive permissions handling suite, all aspects of
- PermissionsBukkit are handled
- @Dependencies : spyc.php
- @Author : Pr4w
- @Special thanks to all the cool people at Bukkit.org :)
- @Usage : This class gives you some pretty easy to use functions. For more information on how they all work, check http://forums.bukkit.org/threads/php-web-complete-set-of-permissions-managing-scripts-get-data-from-player-dat-v1-0-1.32877/
- Todo :
- - Create addInheritance()
- Have fun :)
- */
- // Load dependencies
- @require_once('spyc.php');
- // And here we go
- class permissions {
- // Set the default handling type to "users", and call $yaml
- public $type = "users";
- public $yaml;
- // Base function, sets the .yml file location
- public function setYaml($file) {
- $this->yaml = $file;
- }
- // Little function to grab and parse the Yaml
- protected function getYaml($yaml) {
- $do = Spyc::YAMLLoad(@file_get_contents($yaml));
- // Check to see if the file was correctly loaded or not
- if (!$do) {
- die (" Yaml file doesn't exist/could not be found ! ");
- }
- return $do;
- }
- // Yaml formatting fixes
- protected function fixYaml($yaml) {
- // Fix string instead of boolean bug
- $yaml = str_replace("'true'", "true", $yaml);
- $yaml = str_replace("'false'", "false", $yaml);
- // Fix digits ? This is ugly as hell, but it gets the job done
- // Maybe a preg_replace with [0-9] instead ?
- $digits = array (" 0:", " 1:", " 2:", " 3:", " 4:", " 5:", " 6:", " 7:", " 8:", " 9:");
- $yaml = str_replace($digits, " -", $yaml);
- return $yaml;
- }
- // This exists only for the sake of clarity, it's not particularly useful
- protected function dumpYaml($yaml) {
- $yaml = Spyc::YAMLDump($yaml, 4);
- return $yaml;
- }
- // Same here, this will make it cleaner and less repetitive
- protected function writeYaml($yaml) {
- // Open the .yml file for writing
- $write = fopen($this->yaml, "w");
- // Check if file can be opened
- if (!$write) {
- die (" Could not open file $file ! ");
- }
- // Write to the file, and check if that works
- if (!fwrite($write, $yaml)) {
- die (" Could not write to file ! ");
- }
- return;
- }
- // List all groups and users in a cute little table
- public function display($file = 0) {
- // Get YAML from the .yml file we defined
- if ($file !== 0) {
- $yaml = $this->getYaml($file);
- } elseif ($this->yaml) {
- $yaml = $this->getYaml($this->yaml);
- } else {
- die ( "No Yaml file specified" );
- }
- // Can't be bothered with fancy formatting
- // And can't think of a nice way of doing it anyway
- // So either I dump it, and get nice alignement and pretty colors
- // Or I could print the raw yaml, in between <pre>'s
- var_dump($yaml);
- }
- // Basic permission adding function
- public function addPermissions($target, $type, $rawpermissions) {
- if ($type == "users" || $type == "groups") {
- // Get YAML from the .yml file we defined
- $yaml = $this->getYaml($this->yaml);
- // Check if target exists
- if (!array_key_exists($target, $yaml[$type])) {
- die ( "No such user/group !" );
- }
- // Get the raw permissions from input
- foreach (explode(", ", $rawpermissions) as $perm) {
- list ($key, $value) = explode(": ", $perm, 2);
- $permissions[$key] = $value;
- }
- // Ugly fix for the array, so it has a higher-level array key "permissions"
- $permissions = array ("permissions" => $permissions);
- // Perform array merging recursively
- $yaml[$type][$target] = array_merge_recursive($permissions, $yaml[$type][$target]);
- // Convert array to YAML format and fix it
- $yaml = $this->fixYaml($this->dumpYaml($yaml));
- // Write Yaml to file
- $this->writeYaml($yaml);
- } else {
- die ( "Not a valid target. Should be <b>users</b> *or* <b>groups</b> !" );
- }
- } // addPermissions()
- // Set a user to a group
- public function setGroup($target, $group) {
- // Get YAML from the .yml we defined
- $yaml = $this->getYaml($this->yaml);
- // Check if group exists
- if (!array_key_exists($group, $yaml['groups'])) {
- die (" Group $group does not exist ! ");
- }
- // Check if user exists
- if (!array_key_exists($target, $yaml['users'])) {
- die (" User $target does not exist ! ");
- }
- // Set group to the new group
- $yaml['users'][$target][0] = $group;
- // Convert array to YAML format and fix some formatting errors
- $yaml = $this->fixYaml($this->dumpYaml($yaml));
- // Now write to file
- $this->writeYaml($yaml);
- } // setGroup()
- // Add a new user to a group
- public function addUser($user, $group, $rawpermissions = NULL) {
- // Get YAML from the .yml we defined
- $yaml = $this->getYaml($this->yaml);
- // Check if group exists
- if (!array_key_exists($group, $yaml['groups'])) {
- die (" Can't add $user to group $group because it does not exist ! ");
- }
- // Check if user doesn't already exist
- if (array_key_exists($user, $yaml['users'])) {
- // Fix for encapsed variables, probably a cleaner way of doing it, but it works fine
- $g = $yaml['users'][$user][0];
- die ( "User $user already in group $g, use setGroup() instead !" );
- }
- // Get the raw permissions from input and define $newUser accordingly
- // Is there a cleaner way of doing this ?
- if ($rawpermissions != NULL) {
- foreach (explode(", ", $rawpermissions) as $perm) {
- list ($key, $value) = explode(": ", $perm, 2);
- $permissions[$key] = $value;
- }
- $newUser = array ( $user => array ( 'groups' => '', '0' => $group, 'permissions' => $permissions ) );
- } else {
- $newUser = array ( $user => array ( 'groups' => '', '0' => $group ) );
- }
- // Add new user to the array
- $yaml['users'] = array_merge($yaml['users'], $newUser);
- // Convert array to YAML format and fix some formatting errors
- $yaml = $this->fixYaml($this->dumpYaml($yaml));
- // Now write to file
- $this->writeYaml($yaml);
- } // addUser()
- // Create a new group
- public function addGroup($group, $rawpermissions, $inheritance = false) {
- // Get YAML from the .yml we defined
- $yaml = $this->getYaml($this->yaml);
- // There's probably a cleaner way of doing this but..
- // Turn the $rawpermissions string into a $permission[name] = true/false array
- foreach (explode(", ", $rawpermissions) as $perm) {
- list ($key, $value) = explode(": ", $perm, 2);
- $permissions[$key] = $value;
- }
- // If inheritance is specified, split all different groups into an array
- // And create the new group
- // This is extremely messy, and buggy, will clean up as soon as I can think of a way of doing it
- if ($inheritance != false) {
- $inheritance = explode(", ", $inheritance);
- foreach ($inheritance as $s) {
- if (!array_key_exists($s, $yaml['groups'])) {
- die (" Group $s can't be inherited, as it does not exist ! ");
- }
- }
- $newGroup = array ( $group => array ( 'permissions' => $permissions, 'inheritance' => $inheritance ));
- } else {
- $newGroup = array ( $group => array ( 'permissions' => $permissions ));
- }
- // Add the new group to the YAML array
- $yaml['groups'] = array_merge($yaml['groups'], $newGroup);
- // Convert array to YAML format and fix some formatting errors
- $yaml = $this->fixYaml($this->dumpYaml($yaml));
- // Now write to file
- $this->writeYaml($yaml);
- } // addGroup()
- // Remove a user/group
- public function remove($target, $type) {
- if ($type == "users" || $type == "groups") {
- // Get YAML from the .yml we defined
- $yaml = $this->getYaml($this->yaml);
- // Check if target exists
- if (!array_key_exists($target, $yaml[$type])) {
- die ( "$target does not exist !" );
- }
- // Remove entry from the array
- unset ($yaml[$type][$target]);
- // Fix and dump Yaml
- $yaml = $this->fixYaml($this->dumpYaml($yaml));
- // Write to file
- $this->writeYaml($yaml);
- } else {
- die ( "$type is not a valid type, needs to be <b>users</b> *or* <b>groups</b>" );
- }
- } // remove()
- // Remove a permission from a user/group
- public function removePermission($target, $type, $rawpermissions) {
- if ($type == "users" || $type == "groups") {
- // Get YAML from the .yml we defined
- $yaml = $this->getYaml($this->yaml);
- // There's probably a cleaner way of doing this but..
- // Turn the $rawpermissions string into a $permission[name] = true/false array
- foreach (explode(", ", $rawpermissions) as $perm) {
- list ($key, $value) = explode(": ", $perm, 2);
- $permissions[$key] = $value;
- }
- // Check each permission node input to see if it was actually set to the target
- foreach ($permissions as $permission => $value) {
- if (array_key_exists($permission, $yaml[$type][$target]['permissions'])) {
- unset ($yaml[$type][$target]['permissions'][$permission]);
- } else {
- echo "Permission node <b>$permission</b> not found. Other permissions may have been unset though !";
- }
- }
- // Fix and dump Yaml
- $yaml = $this->fixYaml($this->dumpYaml($yaml));
- // Write to file !
- $this->writeYaml($yaml);
- } else {
- die ( "$type is not a valid type, needs to be <b>users</b> *or* <b>groups</b>" );
- }
- } // removePermission()
- // Count how many players are in a certain group
- public function countPlayers($group = NULL) {
- // Get YAML from the .yml we defined
- $yaml = $this->getYaml($this->yaml);
- // Probably the ugliest bit of code there is, but it'll have to do
- $x = 0;
- foreach ($yaml['users'] as $user => $value) {
- if (isset($group)) {
- if ($value[0] == $group) {
- $x++;
- }
- } else {
- $x = count($yaml['users']);
- }
- }
- return $x;
- } // countPlayers()
- // Very, very basic function to list all users you have
- public function listPlayers($file = NULL) {
- // Check if a file is specified, and grab YAML
- if (isset($file)) {
- $yaml = $this->getYaml($file);
- } else {
- $yaml = $this->getYaml($this->yaml);
- }
- // Check to see if you should get from file
- $users = array_keys($yaml['users']);
- // Return $users to script
- return $users;
- } // listPlayers()
- }
- $do = new permissions();
- $do->setYaml("config.yml");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement