Advertisement
Guest User

Minecraft

a guest
Aug 19th, 2011
314
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. //Config
  4.  
  5. define("S1_PERMFILE", "/dir/to/permissions/file.yml");
  6. define("S2_PERMFILE", "/dir/to/other/server/file.yml");
  7.  
  8. define("S1_HOST", "localhost");
  9. define("S2_HOST", "localhost");
  10.  
  11. define("S1_PORT", "8765");
  12. define("S2_PORT", "8764");
  13.  
  14. define("SOCK_TIMEOUT", "1000");
  15.  
  16. define("TELNET_USER","username");
  17. define("TELNET_PASS","Password");
  18.  
  19.  
  20. /*
  21.  * This function simply changes the users rank. No confirmation or checks are provided.
  22.  * The function shouldn't be run unless the user running it has permission anyway.
  23.  */
  24.  
  25. function changeGroup($input, $group, $server = 0) {
  26.     //First we check if the input is a single username, or a list of usernames. (CVS only!!!)
  27.     if (strstr($input, ',')) {
  28.         $input_array = $explode(',', $input);
  29.     }
  30.     else {
  31.         $input_array[0] = $input;
  32.     }
  33.  
  34.     //We read the correct permissions file to $p for processing
  35.     if ($server == 1) {
  36.         $file = S1_PERMFILE;
  37.         $host = S1_HOST;
  38.         $port = S1_PORT;
  39.         $p = file_get_contents(S1_PERMFILE);
  40.     }
  41.     if ($server == 2) {
  42.         $file = S2_PERMFILE;
  43.         $host = S2_HOST;
  44.         $port = S2_PORT;
  45.         $p = file_get_contents(S2_PERMFILE);
  46.     }
  47.     //Lets parse the YAML to an array!
  48.     $spyc = new Spyc();
  49.     $p = $spyc->YAMLLoad($p);
  50.  
  51.     //We should check to make sure the group we're trying to move to actually exists.
  52.     if (!array_key_exists($group, $p['groups']))
  53.         throw new Exception("Group $group doesn't exist!", 100);
  54.     //We're only interested in the users, not groups so we can ignore them
  55.     $users = $p['users'];
  56.  
  57.     //Loop through each user that's input.
  58.     foreach ($input_array as $input) {
  59.         //Now setting the users new rank is as simple as changing the array's value
  60.         $users[$input]["group"] = $group;
  61.     }
  62.  
  63.     //We now update our $p array with the new user data
  64.     $p['users'] = $users;
  65.     //And generate yml from that array
  66.     $yml = $spyc->dump($p, 4);
  67.  
  68.  
  69.     /*
  70.      * Fix for permissions :(
  71.      */
  72.     $x = 0;
  73.     while ($x < 100) {
  74.         $x = strval($x);
  75.         $yml = str_replace(' ' . $x . ':', '     -', $yml);
  76.         $x++;
  77.     }
  78.     $handle = fopen($file, "w");
  79.     if (!$handle)
  80.         throw new Exception("Could not open permissions file for writing", 101);
  81.     if (!fwrite($handle, $yml)) {
  82.         throw new Exception("Could not write to permissions file", 102);
  83.     }
  84.  
  85.     $user = TELNET_USER;
  86.     $pass = TELNET_PASS;
  87.     $fp = fsockopen($host, $port, $errno, $errstr, 10);
  88.     if (!$fp) {
  89.         echo "Error $errno: $errstr";
  90.         fclose($fp);
  91.     }
  92.     else {
  93.         fputs($fp, $user . "\r\n");
  94.         waste_time();
  95.         fputs($fp, $pass . "\r\n");
  96.         waste_time();
  97.         fputs($fp, "broadcast $input has been moved to $group" . "\r\n");
  98.         waste_time();
  99.         fputs($fp, "permissions -reload all" . "\r\n");
  100.         waste_time();
  101.         fputs($fp, "exit\r\n");
  102.     }
  103.     return true;
  104. }
  105.  
  106. function getServerStatus($server) {
  107.     if ($server == 1) {
  108.         $host = S1_HOST;
  109.         $port = S1_PORT;
  110.     }
  111.     if ($server == 2) {
  112.         $host = S2_HOST;
  113.         $port = S2_PORT;
  114.     }
  115.     $s = fsockopen($host, $port, $errornum, $errorstr, SOCK_TIMEOUT);
  116.     if (!$s)
  117.         return false;
  118.     else
  119.         return true;
  120. }
  121.  
  122. function waste_time() {
  123.     $x = 1;
  124.     while ($x < 5000) {
  125.         $x++;
  126.     }
  127. }
  128.  
  129. ?>
Advertisement
RAW Paste Data Copied
Advertisement