Configuration Base Class updated!

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-13 18:21:05 +01:00
parent 44e53ccc1b
commit ac570213a5
5 changed files with 44 additions and 91 deletions

View File

@ -98,21 +98,16 @@ class ConsoleAPI{
case "pardon": case "pardon":
case "remove": case "remove":
$ip = trim(implode($params)); $ip = trim(implode($params));
$new = array(); $this->server->bannedIPs->remove($packet["ip"]);
foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."banned-ips.txt"))) as $i){ $this->server->bannedIPs->save();
if($i == $ip){ console("[INFO] IP \"$ip\" removed from ban list");
console("[INFO] IP \"$ip\" removed from ban list");
continue;
}
$new[$i] = $i;
}
file_put_contents(FILE_PATH."banned-ips.txt", implode("\r\n", $new));
$this->server->reloadConfig(); $this->server->reloadConfig();
break; break;
case "add": case "add":
case "ban": case "ban":
$ip = trim(implode($params)); $ip = trim(implode($params));
file_put_contents(FILE_PATH."banned-ips.txt", "\r\n".$ip, FILE_APPEND); $this->server->bannedIPs->set($packet["ip"]);
$this->server->bannedIPs->save();
console("[INFO] IP \"$ip\" added to ban list"); console("[INFO] IP \"$ip\" added to ban list");
$this->server->reloadConfig(); $this->server->reloadConfig();
break; break;
@ -120,7 +115,7 @@ class ConsoleAPI{
$this->server->reloadConfig(); $this->server->reloadConfig();
break; break;
case "list": case "list":
console("[INFO] IP ban list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."banned-ips.txt"))))); console("[INFO] IP ban list: ".implode(", ", $this->server->bannedIPs->getAll(true)));
break; break;
default: default:
console("[INFO] Usage: /banip <add | remove | list | reload> [IP]"); console("[INFO] Usage: /banip <add | remove | list | reload> [IP]");
@ -159,20 +154,15 @@ class ConsoleAPI{
switch($p){ switch($p){
case "remove": case "remove":
$user = trim(implode(" ", $params)); $user = trim(implode(" ", $params));
$new = array(); $this->server->whitelist->remove($user);
foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."white-list.txt"))) as $u){ $this->server->whitelist->save();
if($u == $user){ console("[INFO] Player \"$user\" removed from white-list");
console("[INFO] Player \"$user\" removed from white-list");
continue;
}
$new[$u] = $u;
}
file_put_contents(FILE_PATH."white-list.txt", implode("\r\n", $new));
$this->server->reloadConfig(); $this->server->reloadConfig();
break; break;
case "add": case "add":
$user = trim(implode(" ", $params)); $user = trim(implode(" ", $params));
file_put_contents(FILE_PATH."white-list.txt", "\r\n".$user, FILE_APPEND); $this->server->whitelist->set($user);
$this->server->whitelist->save();
console("[INFO] Player \"$user\" added to white-list"); console("[INFO] Player \"$user\" added to white-list");
$this->server->reloadConfig(); $this->server->reloadConfig();
break; break;
@ -180,7 +170,11 @@ class ConsoleAPI{
$this->server->reloadConfig(); $this->server->reloadConfig();
break; break;
case "list": case "list":
console("[INFO] White-list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt"))))); if(($this->server->whitelist instanceof Config) === false){
console("[INFO] No White-list");
}else{
console("[INFO] White-list: ".implode(", ", $this->server->whitelist->getAll(true)));
}
break; break;
case "on": case "on":
case "true": case "true":

View File

@ -80,25 +80,12 @@ class ServerAPI{
} }
} }
if(!file_exists(FILE_PATH."white-list.txt")){
console("[NOTICE] No white-list.txt found, creating blank file");
file_put_contents(FILE_PATH."white-list.txt", "");
}
if(!file_exists(FILE_PATH."banned-ips.txt")){
console("[NOTICE] No banned-ips.txt found, creating blank file");
file_put_contents(FILE_PATH."banned-ips.txt", "");
}
if(!file_exists(FILE_PATH."server.properties")){
console("[NOTICE] No server.properties found, using default settings");
copy(FILE_PATH."src/common/default.properties", FILE_PATH."server.properties");
}
console("[DEBUG] Loading server.properties...", true, true, 2); console("[DEBUG] Loading server.properties...", true, true, 2);
$this->config = new Config(FILE_PATH . "server.properties", CONFIG_PROPERTIES);
$this->parseProperties(); $this->parseProperties();
define("DEBUG", $this->getProperty("debug")); define("DEBUG", $this->getProperty("debug"));
$this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), false, $this->getProperty("port"), $this->getProperty("server-id")); $this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), false, $this->getProperty("port"), $this->getProperty("server-id"));
$this->setProperty("server-id", $this->server->serverID);
$this->server->api = $this; $this->server->api = $this;
if($this->getProperty("upnp-forwarding") === true){ if($this->getProperty("upnp-forwarding") === true){
console("[INFO] [UPnP] Trying to port forward..."); console("[INFO] [UPnP] Trying to port forward...");
@ -230,10 +217,10 @@ class ServerAPI{
if($this->getProperty("memory-limit") !== false){ if($this->getProperty("memory-limit") !== false){
@ini_set("memory_limit", $this->getProperty("memory-limit")); @ini_set("memory_limit", $this->getProperty("memory-limit"));
}else{ }else{
$this->config["memory-limit"] = "256M"; $this->setProperty("memory-limit", "256M");
} }
if(!isset($this->config["invisible"])){ if(!$this->config->exists("invisible")){
$this->config["invisible"] = false; $this->config->set("invisible", false);
} }
if(is_object($this->server)){ if(is_object($this->server)){
$this->server->setType($this->getProperty("server-type")); $this->server->setType($this->getProperty("server-type"));
@ -250,44 +237,11 @@ class ServerAPI{
} }
private function writeProperties(){ private function writeProperties(){
if(is_object($this->server)){ $this->config->save();
$this->config["server-id"] = $this->server->serverID;
}
$config = $this->config;
$config["white-list"] = $config["white-list"] === true ? "true":"false";
$config["invisible"] = $config["invisible"] === true ? "true":"false";
$config["upnp-forwarding"] = $config["upnp-forwarding"] === true ? "true":"false";
$prop = "#Pocket Minecraft PHP server properties\r\n#".date("D M j H:i:s T Y")."\r\n";
foreach($config as $n => $v){
$prop .= $n."=".$v."\r\n";
}
file_put_contents(FILE_PATH."server.properties", $prop);
} }
private function parseProperties(){ private function parseProperties(){
$this->config = new Config(FILE_PATH."white-list.txt"); foreach($this->config->getAll() as $n => $v){
$prop = file_get_contents(FILE_PATH."server.properties");
$prop = explode("\n", str_replace("\r", "", $prop));
$this->config = array();
foreach($prop as $line){
if(trim($line) == "" or $line{0} == "#"){
continue;
}
$d = explode("=", $line);
$n = strtolower(array_shift($d));
$v = implode("=", $d);
switch(strtolower(trim($v))){
case "on":
case "true":
case "yes":
$v = true;
break;
case "off":
case "false":
case "no":
$v = false;
break;
}
switch($n){ switch($n){
case "last-update": case "last-update":
if($v === false){ if($v === false){
@ -310,7 +264,7 @@ class ServerAPI{
} }
break; break;
} }
$this->config[$n] = $v; $this->config->set($n, $v);
} }
} }
@ -402,7 +356,7 @@ class ServerAPI{
} }
public function getProperties(){ public function getProperties(){
return $this->config; return $this->config->getAll();
} }
public function getProperty($name){ public function getProperty($name){
@ -443,14 +397,11 @@ class ServerAPI{
} }
return $v; return $v;
} }
if(isset($this->config[$name])){ return $this->config->get($name);
return $this->config[$name];
}
return false;
} }
public function setProperty($name, $value){ public function setProperty($name, $value){
$this->config[$name] = $value; $this->config->set($name, $value);
$this->writeProperties(); $this->writeProperties();
$this->loadProperties(); $this->loadProperties();
} }

View File

@ -285,7 +285,7 @@ class Player{
break; break;
} }
$o = $this->server->api->player->getOffline($this->username); $o = $this->server->api->player->getOffline($this->username);
if($this->server->whitelist !== false and (!in_array($this->username, $this->server->whitelist)/* or ($o["lastID"] != 0 and $o["lastID"] != $this->clientID)*/)){ if($this->server->whitelist !== false and $this->server->whitelist->exists($this->username)){
$this->close("\"\x1b[33m".$this->username."\x1b[0m\" not being on white-list", false); $this->close("\"\x1b[33m".$this->username."\x1b[0m\" not being on white-list", false);
break; break;
} }

View File

@ -132,10 +132,10 @@ class PocketMinecraftServer{
} }
public function reloadConfig(){ public function reloadConfig(){
if($this->whitelist === true or is_array($this->whitelist)){ if($this->whitelist === true or ($this->whitelist instanceof Config)){
$this->whitelist = explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt"))); $this->whitelist = new Config(FILE_PATH."white-list.txt", CONFIG_LIST);
} }
$this->bannedIPs = explode("\n", str_replace(array(" ","\t","\r"), "", file_get_contents(FILE_PATH."banned-ips.txt"))); $this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST);
} }
public function debugInfo($console = false){ public function debugInfo($console = false){
@ -398,7 +398,7 @@ class PocketMinecraftServer{
), false, $packet["ip"], $packet["port"]); ), false, $packet["ip"], $packet["port"]);
break; break;
} }
if(in_array($packet["ip"], $this->bannedIPs)){ if($this->bannedIPs->exists($packet["ip"])){
$this->send(0x1c, array( $this->send(0x1c, array(
$data[0], $data[0],
$this->serverID, $this->serverID,
@ -422,7 +422,7 @@ class PocketMinecraftServer{
$this->custom["times_".$CID] = ($this->custom["times_".$CID] + 1) % strlen($this->description); $this->custom["times_".$CID] = ($this->custom["times_".$CID] + 1) % strlen($this->description);
break; break;
case 0x05: case 0x05:
if(in_array($packet["ip"], $this->bannedIPs) or count($this->clients) >= $this->maxClients){ if($this->bannedIPs->exists($packet["ip"]) or count($this->clients) >= $this->maxClients){
$this->send(0x80, array( $this->send(0x80, array(
0, 0,
0x00, 0x00,
@ -458,7 +458,7 @@ class PocketMinecraftServer{
} }
break; break;
case 0x07: case 0x07:
if(in_array($packet["ip"], $this->bannedIPs) or count($this->clients) >= $this->maxClients){ if($this->bannedIPs->exists($packet["ip"]) or count($this->clients) >= $this->maxClients){
$this->send(0x80, array( $this->send(0x80, array(
0, 0,
0x00, 0x00,

View File

@ -156,8 +156,16 @@ class Config{
$this->config[$k] = $v; $this->config[$k] = $v;
} }
public function getAll(){ public function exists($k){
return ($this->config); return isset($this->config[$k]);
}
public function remove($k){
unset($this->config[$k]);
}
public function getAll($keys = false){
return ($keys === true ? array_keys($this->config):$this->config);
} }
private function fillDefaults($default, &$data){ private function fillDefaults($default, &$data){