Last active
December 31, 2015 04:09
-
-
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 :/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
} | |
?> |
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
What does addBan("Truc des Familles", 2) and removeBan("Machin",0); second parameter means and what options there are?