Advertisement
Guest User

addGroup()

a guest
Aug 21st, 2011
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  @Filename : addGroup.php
  4.  @Description : Adds a group to the Permissions file, including name, permissions, and inheritance(s)
  5.  Could be useful for admins working on control panels :)
  6.  @Written by Pr4w for Bukkit.org
  7.  @Usage : addGroup('/opt/mc/plugins/permissionsBukkit/config.yml', 'GroupName', 'permission.node: true, other.permission.node: true, not.allowed: false', 'Group1, Group2, Group3');
  8.  
  9.  Have fun :)
  10. */
  11.  
  12. // Including the Spyc Yaml Parser, change this to wherever you want to have it
  13. @require_once('spyc.php');
  14.  
  15.  
  16.  
  17. function addGroup($file, $groupname, $rawpermissions, $inheritance = false) {
  18.  
  19.     // Load YAML sauce
  20.     $yaml = Spyc::YAMLLoad(@file_get_contents($file));
  21.    
  22.     // Check to see if the file was correctly loaded or not
  23.     if (!$yaml) {
  24.         die (" Yaml file doesn't exist/could not be found ! ");
  25.     }
  26.    
  27.     // There's probably a cleaner way of doing this but..
  28.     // Turn the $rawpermissions string into a $permission[name] = true/false array
  29.     foreach (explode(", ", $rawpermissions) as $perm) {
  30.         list ($key, $value) = explode(": ", $perm, 2);
  31.         $permissions[$key] = $value;
  32.     }
  33.    
  34.     // If inheritance is specified, split all different groups into an array
  35.     // And create the new group
  36.     // This is extremely messy, and buggy, will clean up as soon as I can think of a way of doing it
  37.     if ($inheritance != false) {
  38.         $inheritance = explode(", ", $inheritance);
  39.         foreach ($inheritance as $s) {
  40.             if (!array_key_exists($s, $yaml['groups'])) {
  41.                 die (" Group $s can't be inherited, as it does not exist ! ");
  42.             }
  43.         }
  44.         $newGroup = array ( $groupname => array ( 'permissions' => $permissions, 'inheritance' => $inheritance ));
  45.     } else {
  46.         $newGroup = array ( $groupname => array ( 'permissions' => $permissions ));
  47.     }
  48.    
  49.     // Add the new group to the YAML array
  50.     $yaml['groups'] = array_merge($yaml['groups'], $newGroup);
  51.     $yaml = Spyc::YAMLDump($yaml, 4);
  52.    
  53.     // Fix some formatting errors that Permissions won't like
  54.     // I'm having a bit of a brainfart, can't figure out an easier way to do this..
  55.     $digits = array (" 0:", " 1:", " 2:", " 3:", " 4:", " 5:", " 6:", " 7:", " 8:", " 9:");
  56.     $yaml = str_replace($digits, " -", $yaml);
  57.     // Same here, without this, the Yaml is output as :
  58.     // permission.node: 'true'
  59.     // Which doesn't work, because of the ''
  60.     // This is because in the array, true/false are strings, and not boolean
  61.     $yaml = str_replace("'true'", "true", $yaml);
  62.     $yaml = str_replace("'false'", "false", $yaml);
  63.    
  64.     // Open the .yml file for writing
  65.     $write = fopen($file, "w");
  66.    
  67.     // Check if file can be opened
  68.     if (!$write) {
  69.         die (" Could not open file $file ! ");
  70.     }
  71.    
  72.     // Write to the file, and check if that works
  73.     if (!fwrite($write, $yaml)) {
  74.         die (" Could not write to file ! ");
  75.     }  
  76. }
  77. ?>
Advertisement
RAW Paste Data Copied
Advertisement