Skip to content

Instantly share code, notes, and snippets.

@lagonnebula
Last active December 31, 2015 04:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagonnebula/7932481 to your computer and use it in GitHub Desktop.
Save lagonnebula/7932481 to your computer and use it in GitHub Desktop.
This class in PHP, use the accescontrol.config. I hope can be usefull in the future ;) Since u can reload the server, u have to reboot server for each modification on this file :/
<?php
/**
* a PHP class for registering account in accescontrol.config remotely
* Each modification need to restart the server for load them bucause there is no reloading at the moment
*
* @author MeuhMeuh starbound.meuhmeuh.fr
* @copyright (c) 2013, Jeremy Villemain
*/
class Register{
/*example of use
$r = new Register();
$r->addAccount("Machin", "Truc");
$r->removeAccount("Meuhmeuh");
$r->addBan("Truc des Familles", 2);
$r->removeBan("Machin",0);
$r->writeData();
//*/
private $path = "/path/to/starbound/server/universe/accesscontrol.config";
private $fp;
private $json;
function Register(){
$this->json = null;
$this->readData();
}
private function openFile($mode){
$this->fp = fopen($this->path,$mode);
if(!$this->fp){
//Helper::debug("Error: File not found, using default JSON");
$this->defaultFile();
return false;
}
return true;
}
private function closeFile(){
fclose($this->fp);
}
private function readData(){
if($this->openFile("r")){
$line ="";
while (!feof($this->fp)) {
$line .=fgets($this->fp);
}
$this->json = json_decode($line, true);
$this->closeFile();
}
if($this->json == null){
//Helper::debug("Error: File was empty, using default JSON");
$this->defaultFile();
return false;
}
}
private function defaultFile(){
$this->json["accounts"] = array();
$this->json[$this->getNameFromId(0)] = array();
$this->json[$this->getNameFromId(1)] = array();
$this->json[$this->getNameFromId(2)] = array();
}
private function searchString($string, $tabToSearch){
$max = count($tabToSearch);
for($i=0;$i<$max;$i++){
if($tabToSearch[$i] == $string)
return $i;
}
return -1;
}
private function getNameFromId($id){
switch($id){
case 0: return "bannedAccountNames";break;
case 1: return "bannedAddresses";break;
case 2: return "bannedPlayerNames";break;
}
}
public function writeData(){
if($this->openFile("w")){
$string = json_encode($this->json);
fwrite($this->fp, $string);
return "Writing file";
}
return "Error: writing file";
}
public function addAccount($user, $pass){
if(!empty($user) && !empty($pass)){
$user = htmlentities($user);
$pass = htmlentities($pass);
$this->json["accounts"][$user]["password"] = $pass;
return "Account added or modified";
}
return "Error: add account";
}
public function removeAccount($string){
if(!empty($string)){
$string = htmlentities($string);
if(isset($this->json["accounts"][$string])){
unset($this->json["accounts"][$string]);
return "Accoun removed";
}else{
return "Account not found";
}
}
return "Error: remove account";
}
public function addBan($string, $type=0){
if(!empty($string)){
$string = htmlentities($string);
if($this->searchString($string,$this->json[$this->getNameFromId($type)]) == -1){
$this->json[$this->getNameFromId($type)][] = $string;
return "Player Banned";
}else{
return "Error: Already Ban";
}
}
return "Error: ban";
}
public function removeBan($string, $type=0){
if(!empty($string)){
$string = htmlentities($string);
unset($this->json[$this->getNameFromId($type)][$this->searchString($string, $this->json[$this->getNameFromId($type)])]);
return "Player unban";
}
return "Error: unban";
}
}
?>
@AMerkuri
Copy link

What does addBan("Truc des Familles", 2) and removeBan("Machin",0); second parameter means and what options there are?

@lovedva
Copy link

lovedva commented Nov 17, 2014

is it means i should change the path to the one on my server?
$path = "/path/to/starbound/server/universe/accesscontrol.config";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment