Advertisement
Guest User

setGroup()

a guest
Aug 21st, 2011
122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  @Filename : setGroup.php
  4.  @Description : Changes a user to a specific group, through a web page
  5.  Could be useful for admins working on control panels :)
  6.  @Written by Pr4w for Bukkit.org
  7.  @Usage : setGroup('Pr4w', 'Admin', '/opt/mc/plugins/PermissionsBukkit/config.yml');
  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 setGroup($user, $group, $file) {
  18.  
  19.     // Let's load some YAML
  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.     // Check if group exists, in case
  28.     if (!array_key_exists($group, $yaml['groups'])) {
  29.         die (" Group $group does not exist ! ");
  30.     }
  31.    
  32.     // Check if user exists, in case
  33.     if (!array_key_exists($user, $yaml['users'])) {
  34.         die (" User $user does not exist ! ");
  35.     }
  36.    
  37.     // Set group to the new group
  38.     $yaml['users'][$user][0] = $group;
  39.    
  40.     // Dump it back to YAML format
  41.     $yaml = Spyc::YAMLDump($yaml, 4);
  42.    
  43.     // Fix some formatting errors that Permissions won't like
  44.     $yaml = str_replace("0:", "-", $yaml);
  45.    
  46.     // Open the .yml file for writing
  47.     $write = fopen($file, "w");
  48.    
  49.     // Check if file can be opened
  50.     if (!$write) {
  51.         die (" Could not open file $file ! ");
  52.     }
  53.    
  54.     // Write to the file, and check if that works
  55.     if (!fwrite($write, $yaml)) {
  56.         die (" Could not write to file ! ");
  57.     }
  58. }
  59. ?>
Advertisement
RAW Paste Data Copied
Advertisement