Advertisement
Pr4w

permissions()

Aug 22nd, 2011
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.50 KB | None | 0 0
  1. <?php
  2. /*
  3.  @Filename : permissions.class.php
  4.  @Description : Comprehensive permissions handling suite, all aspects of
  5.  PermissionsBukkit are handled
  6.  @Dependencies : spyc.php
  7.  @Author : Pr4w
  8.  @Special thanks to all the cool people at Bukkit.org :)
  9.  
  10.  @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/
  11.  
  12.  
  13.  Todo :
  14.         - Create addInheritance()
  15.  
  16.  
  17.  Have fun :)
  18. */
  19.  
  20. // Load dependencies
  21. @require_once('spyc.php');
  22.  
  23. // And here we go
  24. class permissions {
  25.    
  26.     // Set the default handling type to "users", and call $yaml
  27.     public $type = "users";
  28.     public $yaml;
  29.    
  30.    
  31.     // Base function, sets the .yml file location
  32.     public function setYaml($file) {
  33.         $this->yaml = $file;
  34.     }
  35.    
  36.     // Little function to grab and parse the Yaml
  37.     protected function getYaml($yaml) {
  38.    
  39.         $do = Spyc::YAMLLoad(@file_get_contents($yaml));
  40.        
  41.         // Check to see if the file was correctly loaded or not
  42.         if (!$do) {
  43.             die (" Yaml file doesn't exist/could not be found ! ");
  44.         }
  45.        
  46.         return $do;
  47.     }  
  48.    
  49.     // Yaml formatting fixes
  50.     protected function fixYaml($yaml) {
  51.        
  52.         // Fix string instead of boolean bug
  53.         $yaml = str_replace("'true'", "true", $yaml);
  54.         $yaml = str_replace("'false'", "false", $yaml);
  55.        
  56.         // Fix digits ? This is ugly as hell, but it gets the job done
  57.         // Maybe a preg_replace with [0-9] instead ?
  58.         $digits = array (" 0:", " 1:", " 2:", " 3:", " 4:", " 5:", " 6:", " 7:", " 8:", " 9:");
  59.         $yaml = str_replace($digits, " -", $yaml);
  60.        
  61.         return $yaml;
  62.     }
  63.    
  64.     // This exists only for the sake of clarity, it's not particularly useful
  65.     protected function dumpYaml($yaml) {
  66.         $yaml = Spyc::YAMLDump($yaml, 4);
  67.        
  68.         return $yaml;
  69.     }      
  70.    
  71.     // Same here, this will make it cleaner and less repetitive
  72.     protected function writeYaml($yaml) {
  73.        
  74.         // Open the .yml file for writing
  75.         $write = fopen($this->yaml, "w");
  76.    
  77.         // Check if file can be opened
  78.         if (!$write) {
  79.             die (" Could not open file $file ! ");
  80.         }
  81.    
  82.         // Write to the file, and check if that works
  83.         if (!fwrite($write, $yaml)) {
  84.             die (" Could not write to file ! ");
  85.         }
  86.        
  87.         return;
  88.     }
  89.  
  90.    
  91.     // List all groups and users in a cute little table
  92.     public function display($file = 0) {
  93.        
  94.         // Get YAML from the .yml file we defined
  95.         if ($file !== 0) {
  96.             $yaml = $this->getYaml($file);
  97.         } elseif ($this->yaml) {
  98.             $yaml = $this->getYaml($this->yaml);
  99.         } else {
  100.             die ( "No Yaml file specified" );
  101.         }
  102.        
  103.         // Can't be bothered with fancy formatting
  104.         // And can't think of a nice way of doing it anyway
  105.         // So either I dump it, and get nice alignement and pretty colors
  106.         // Or I could print the raw yaml, in between <pre>'s
  107.         var_dump($yaml);
  108.    
  109.     }
  110.        
  111.     // Basic permission adding function
  112.     public function addPermissions($target, $type, $rawpermissions) {
  113.    
  114.         if ($type == "users" || $type == "groups") {
  115.        
  116.             // Get YAML from the .yml file we defined
  117.             $yaml = $this->getYaml($this->yaml);
  118.            
  119.             // Check if target exists
  120.             if (!array_key_exists($target, $yaml[$type])) {
  121.                 die ( "No such user/group !" );
  122.             }
  123.            
  124.             // Get the raw permissions from input
  125.             foreach (explode(", ", $rawpermissions) as $perm) {
  126.                 list ($key, $value) = explode(": ", $perm, 2);
  127.                 $permissions[$key] = $value;
  128.             }
  129.            
  130.             // Ugly fix for the array, so it has a higher-level array key "permissions"
  131.             $permissions = array ("permissions" => $permissions);
  132.            
  133.             // Perform array merging recursively
  134.             $yaml[$type][$target] = array_merge_recursive($permissions, $yaml[$type][$target]);
  135.            
  136.             // Convert array to YAML format and fix it
  137.             $yaml = $this->fixYaml($this->dumpYaml($yaml));
  138.            
  139.             // Write Yaml to file
  140.             $this->writeYaml($yaml);
  141.            
  142.         } else {
  143.             die ( "Not a valid target. Should be <b>users</b> *or* <b>groups</b> !" );
  144.         }
  145.        
  146.     } // addPermissions()
  147.    
  148.     // Set a user to a group
  149.     public function setGroup($target, $group) {
  150.        
  151.         // Get YAML from the .yml we defined
  152.         $yaml = $this->getYaml($this->yaml);
  153.        
  154.         // Check if group exists
  155.         if (!array_key_exists($group, $yaml['groups'])) {
  156.             die (" Group $group does not exist ! ");
  157.         }
  158.    
  159.         // Check if user exists
  160.         if (!array_key_exists($target, $yaml['users'])) {
  161.             die (" User $target does not exist ! ");
  162.         }
  163.        
  164.         // Set group to the new group
  165.         $yaml['users'][$target][0] = $group;
  166.        
  167.         // Convert array to YAML format and fix some formatting errors
  168.         $yaml = $this->fixYaml($this->dumpYaml($yaml));
  169.        
  170.         // Now write to file
  171.         $this->writeYaml($yaml);
  172.  
  173.     } // setGroup()
  174.    
  175.     // Add a new user to a group
  176.     public function addUser($user, $group, $rawpermissions = NULL) {
  177.        
  178.         // Get YAML from the .yml we defined
  179.         $yaml = $this->getYaml($this->yaml);
  180.        
  181.         // Check if group exists
  182.         if (!array_key_exists($group, $yaml['groups'])) {
  183.             die (" Can't add $user to group $group because it does not exist ! ");
  184.         }
  185.        
  186.         // Check if user doesn't already exist
  187.         if (array_key_exists($user, $yaml['users'])) {
  188.             // Fix for encapsed variables, probably a cleaner way of doing it, but it works fine
  189.             $g = $yaml['users'][$user][0];
  190.             die ( "User $user already in group $g, use setGroup() instead !" );
  191.         }
  192.        
  193.         // Get the raw permissions from input and define $newUser accordingly
  194.         // Is there a cleaner way of doing this ?
  195.         if ($rawpermissions != NULL) {
  196.             foreach (explode(", ", $rawpermissions) as $perm) {
  197.                 list ($key, $value) = explode(": ", $perm, 2);
  198.                 $permissions[$key] = $value;
  199.             }
  200.             $newUser = array ( $user => array ( 'groups' => '', '0' => $group, 'permissions' => $permissions ) );
  201.         } else {
  202.             $newUser = array ( $user => array ( 'groups' => '', '0' => $group ) );
  203.         }
  204.        
  205.         // Add new user to the array
  206.         $yaml['users'] = array_merge($yaml['users'], $newUser);
  207.        
  208.         // Convert array to YAML format and fix some formatting errors
  209.         $yaml = $this->fixYaml($this->dumpYaml($yaml));
  210.        
  211.         // Now write to file
  212.         $this->writeYaml($yaml);
  213.        
  214.     } // addUser()
  215.    
  216.     // Create a new group
  217.     public function addGroup($group, $rawpermissions, $inheritance = false) {
  218.        
  219.         // Get YAML from the .yml we defined
  220.         $yaml = $this->getYaml($this->yaml);
  221.        
  222.         // There's probably a cleaner way of doing this but..
  223.         // Turn the $rawpermissions string into a $permission[name] = true/false array
  224.         foreach (explode(", ", $rawpermissions) as $perm) {
  225.             list ($key, $value) = explode(": ", $perm, 2);
  226.             $permissions[$key] = $value;
  227.         }
  228.        
  229.         // If inheritance is specified, split all different groups into an array
  230.         // And create the new group
  231.         // This is extremely messy, and buggy, will clean up as soon as I can think of a way of doing it
  232.         if ($inheritance != false) {
  233.             $inheritance = explode(", ", $inheritance);
  234.             foreach ($inheritance as $s) {
  235.                 if (!array_key_exists($s, $yaml['groups'])) {
  236.                     die (" Group $s can't be inherited, as it does not exist ! ");
  237.                 }
  238.             }
  239.             $newGroup = array ( $group => array ( 'permissions' => $permissions, 'inheritance' => $inheritance ));
  240.         } else {
  241.             $newGroup = array ( $group => array ( 'permissions' => $permissions ));
  242.         }
  243.        
  244.         // Add the new group to the YAML array
  245.         $yaml['groups'] = array_merge($yaml['groups'], $newGroup);
  246.        
  247.         // Convert array to YAML format and fix some formatting errors
  248.         $yaml = $this->fixYaml($this->dumpYaml($yaml));
  249.  
  250.         // Now write to file
  251.         $this->writeYaml($yaml);
  252.    
  253.     } // addGroup()
  254.    
  255.     // Remove a user/group
  256.     public function remove($target, $type) {
  257.        
  258.         if ($type == "users" || $type == "groups") {
  259.        
  260.             // Get YAML from the .yml we defined
  261.             $yaml = $this->getYaml($this->yaml);
  262.            
  263.             // Check if target exists
  264.             if (!array_key_exists($target, $yaml[$type])) {
  265.                 die ( "$target does not exist !" );
  266.             }
  267.            
  268.             // Remove entry from the array
  269.             unset ($yaml[$type][$target]);
  270.            
  271.             // Fix and dump Yaml
  272.             $yaml = $this->fixYaml($this->dumpYaml($yaml));
  273.            
  274.             // Write to file
  275.             $this->writeYaml($yaml);
  276.            
  277.         } else {
  278.        
  279.             die ( "$type is not a valid type, needs to be <b>users</b> *or* <b>groups</b>" );
  280.            
  281.         }
  282.        
  283.     } // remove()
  284.        
  285.     // Remove a permission from a user/group
  286.     public function removePermission($target, $type, $rawpermissions) {
  287.        
  288.         if ($type == "users" || $type == "groups") {
  289.        
  290.             // Get YAML from the .yml we defined
  291.             $yaml = $this->getYaml($this->yaml);
  292.            
  293.             // There's probably a cleaner way of doing this but..
  294.             // Turn the $rawpermissions string into a $permission[name] = true/false array
  295.             foreach (explode(", ", $rawpermissions) as $perm) {
  296.                 list ($key, $value) = explode(": ", $perm, 2);
  297.                 $permissions[$key] = $value;
  298.             }
  299.            
  300.             // Check each permission node input to see if it was actually set to the target
  301.             foreach ($permissions as $permission => $value) {
  302.                 if (array_key_exists($permission, $yaml[$type][$target]['permissions'])) {
  303.                     unset ($yaml[$type][$target]['permissions'][$permission]);
  304.                 } else {
  305.                     echo "Permission node <b>$permission</b> not found. Other permissions may have been unset though !";
  306.                 }
  307.             }
  308.        
  309.             // Fix and dump Yaml
  310.             $yaml = $this->fixYaml($this->dumpYaml($yaml));
  311.            
  312.             // Write to file !
  313.             $this->writeYaml($yaml);
  314.            
  315.         } else {
  316.        
  317.             die ( "$type is not a valid type, needs to be <b>users</b> *or* <b>groups</b>" );
  318.        
  319.         }
  320.        
  321.     } // removePermission()
  322.    
  323.     // Count how many players are in a certain group
  324.     public function countPlayers($group = NULL) {
  325.        
  326.         // Get YAML from the .yml we defined
  327.         $yaml = $this->getYaml($this->yaml);
  328.        
  329.         // Probably the ugliest bit of code there is, but it'll have to do
  330.         $x = 0;
  331.         foreach ($yaml['users'] as $user => $value) {
  332.             if (isset($group)) {
  333.                 if ($value[0] == $group) {
  334.                     $x++;
  335.                 }
  336.             } else {
  337.                 $x = count($yaml['users']);
  338.             }
  339.         }
  340.        
  341.         return $x;
  342.        
  343.     } // countPlayers()
  344.    
  345.     // Very, very basic function to list all users you have
  346.     public function listPlayers($file = NULL) {
  347.    
  348.         // Check if a file is specified, and grab YAML
  349.         if (isset($file)) {
  350.             $yaml = $this->getYaml($file);
  351.         } else {
  352.             $yaml = $this->getYaml($this->yaml);
  353.         }
  354.                
  355.         // Check to see if you should get from file
  356.         $users = array_keys($yaml['users']);
  357.        
  358.         // Return $users to script
  359.         return $users;
  360.        
  361.     } // listPlayers()
  362.  
  363.    
  364. }
  365.  
  366. $do = new permissions();
  367. $do->setYaml("config.yml");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement