diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index ee8c59835..aaa4a3fbb 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -98,21 +98,16 @@ class ConsoleAPI{ case "pardon": case "remove": $ip = trim(implode($params)); - $new = array(); - foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."banned-ips.txt"))) as $i){ - if($i == $ip){ - 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->bannedIPs->remove($packet["ip"]); + $this->server->bannedIPs->save(); + console("[INFO] IP \"$ip\" removed from ban list"); $this->server->reloadConfig(); break; case "add": case "ban": $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"); $this->server->reloadConfig(); break; @@ -120,7 +115,7 @@ class ConsoleAPI{ $this->server->reloadConfig(); break; 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; default: console("[INFO] Usage: /banip [IP]"); @@ -159,20 +154,15 @@ class ConsoleAPI{ switch($p){ case "remove": $user = trim(implode(" ", $params)); - $new = array(); - foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."white-list.txt"))) as $u){ - if($u == $user){ - 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->whitelist->remove($user); + $this->server->whitelist->save(); + console("[INFO] Player \"$user\" removed from white-list"); $this->server->reloadConfig(); break; case "add": $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"); $this->server->reloadConfig(); break; @@ -180,7 +170,11 @@ class ConsoleAPI{ $this->server->reloadConfig(); break; 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; case "on": case "true": diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 7549f9e6d..0d6458c79 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -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); + $this->config = new Config(FILE_PATH . "server.properties", CONFIG_PROPERTIES); $this->parseProperties(); 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->setProperty("server-id", $this->server->serverID); $this->server->api = $this; if($this->getProperty("upnp-forwarding") === true){ console("[INFO] [UPnP] Trying to port forward..."); @@ -230,10 +217,10 @@ class ServerAPI{ if($this->getProperty("memory-limit") !== false){ @ini_set("memory_limit", $this->getProperty("memory-limit")); }else{ - $this->config["memory-limit"] = "256M"; + $this->setProperty("memory-limit", "256M"); } - if(!isset($this->config["invisible"])){ - $this->config["invisible"] = false; + if(!$this->config->exists("invisible")){ + $this->config->set("invisible", false); } if(is_object($this->server)){ $this->server->setType($this->getProperty("server-type")); @@ -250,44 +237,11 @@ class ServerAPI{ } private function writeProperties(){ - if(is_object($this->server)){ - $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); + $this->config->save(); } private function parseProperties(){ - $this->config = new Config(FILE_PATH."white-list.txt"); - $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; - } + foreach($this->config->getAll() as $n => $v){ switch($n){ case "last-update": if($v === false){ @@ -310,7 +264,7 @@ class ServerAPI{ } break; } - $this->config[$n] = $v; + $this->config->set($n, $v); } } @@ -402,7 +356,7 @@ class ServerAPI{ } public function getProperties(){ - return $this->config; + return $this->config->getAll(); } public function getProperty($name){ @@ -443,14 +397,11 @@ class ServerAPI{ } return $v; } - if(isset($this->config[$name])){ - return $this->config[$name]; - } - return false; + return $this->config->get($name); } public function setProperty($name, $value){ - $this->config[$name] = $value; + $this->config->set($name, $value); $this->writeProperties(); $this->loadProperties(); } diff --git a/src/classes/Player.php b/src/classes/Player.php index 900f3c172..e5e869635 100644 --- a/src/classes/Player.php +++ b/src/classes/Player.php @@ -285,7 +285,7 @@ class Player{ break; } $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); break; } diff --git a/src/classes/PocketMinecraftServer.php b/src/classes/PocketMinecraftServer.php index e4c5b0801..fd4072e18 100644 --- a/src/classes/PocketMinecraftServer.php +++ b/src/classes/PocketMinecraftServer.php @@ -132,10 +132,10 @@ class PocketMinecraftServer{ } public function reloadConfig(){ - if($this->whitelist === true or is_array($this->whitelist)){ - $this->whitelist = explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt"))); + if($this->whitelist === true or ($this->whitelist instanceof Config)){ + $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){ @@ -398,7 +398,7 @@ class PocketMinecraftServer{ ), false, $packet["ip"], $packet["port"]); break; } - if(in_array($packet["ip"], $this->bannedIPs)){ + if($this->bannedIPs->exists($packet["ip"])){ $this->send(0x1c, array( $data[0], $this->serverID, @@ -422,7 +422,7 @@ class PocketMinecraftServer{ $this->custom["times_".$CID] = ($this->custom["times_".$CID] + 1) % strlen($this->description); break; 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( 0, 0x00, @@ -458,7 +458,7 @@ class PocketMinecraftServer{ } break; 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( 0, 0x00, diff --git a/src/classes/utils/Config.php b/src/classes/utils/Config.php index 52a54fe0a..154eff4fe 100644 --- a/src/classes/utils/Config.php +++ b/src/classes/utils/Config.php @@ -156,8 +156,16 @@ class Config{ $this->config[$k] = $v; } - public function getAll(){ - return ($this->config); + public function exists($k){ + 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){