Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- @Filename : addGroup.php
- @Description : Adds a group to the Permissions file, including name, permissions, and inheritance(s)
- Could be useful for admins working on control panels :)
- @Written by Pr4w for Bukkit.org
- @Usage : addGroup('/opt/mc/plugins/permissionsBukkit/config.yml', 'GroupName', 'permission.node: true, other.permission.node: true, not.allowed: false', 'Group1, Group2, Group3');
- Have fun :)
- */
- // Including the Spyc Yaml Parser, change this to wherever you want to have it
- @require_once('spyc.php');
- function addGroup($file, $groupname, $rawpermissions, $inheritance = false) {
- // Load YAML sauce
- $yaml = Spyc::YAMLLoad(@file_get_contents($file));
- // Check to see if the file was correctly loaded or not
- if (!$yaml) {
- die (" Yaml file doesn't exist/could not be found ! ");
- }
- // 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 ( $groupname => array ( 'permissions' => $permissions, 'inheritance' => $inheritance ));
- } else {
- $newGroup = array ( $groupname => array ( 'permissions' => $permissions ));
- }
- // Add the new group to the YAML array
- $yaml['groups'] = array_merge($yaml['groups'], $newGroup);
- $yaml = Spyc::YAMLDump($yaml, 4);
- // Fix some formatting errors that Permissions won't like
- // I'm having a bit of a brainfart, can't figure out an easier way to do this..
- $digits = array (" 0:", " 1:", " 2:", " 3:", " 4:", " 5:", " 6:", " 7:", " 8:", " 9:");
- $yaml = str_replace($digits, " -", $yaml);
- // Same here, without this, the Yaml is output as :
- // permission.node: 'true'
- // Which doesn't work, because of the ''
- // This is because in the array, true/false are strings, and not boolean
- $yaml = str_replace("'true'", "true", $yaml);
- $yaml = str_replace("'false'", "false", $yaml);
- // Open the .yml file for writing
- $write = fopen($file, "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 ! ");
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement