Fixed White-listing and IP ban

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-08 17:05:52 +01:00
parent 2116702d43
commit ed8f38ef55
2 changed files with 60 additions and 18 deletions

View File

@ -69,7 +69,8 @@ class Session{
file_put_contents(FILE_PATH."data/players/".str_replace("/", "", $this->username).".dat", serialize($this->data));
}
public function close($reason = "server stop", $msg = true){
public function close($reason = "", $msg = true){
$reason = $reason == "" ? "server stop":$reason;
$this->save();
if(is_object($this->entity)){
$this->entity->close();
@ -77,6 +78,7 @@ class Session{
foreach($this->evid as $ev){
$this->server->deleteEvent($ev[0], $ev[1]);
}
$this->eventHandler("You have been kicked. Reason: ".$reason, "onChat");
$this->connected = false;
if($msg === true){
$this->server->trigger("onChat", $this->username." left the game");
@ -292,6 +294,7 @@ class Session{
console("[DEBUG] Player with EID ".$this->eid." \"".$this->username."\" spawned!", true, true, 2);
$this->entity = new Entity($this->eid, ENTITY_PLAYER, 0, $this->server);
$this->entity->setName($this->username);
$this->entity->setHealth($this->data["health"]);
$this->entity->data["clientID"] = $this->clientID;
$this->server->entities[$this->eid] = &$this->entity;
$this->server->trigger("onPlayerAdd", array(

View File

@ -150,10 +150,12 @@ function loadConfig($regenerate = false){
$config["seed"] = $server->seed;
$config["server-id"] = $server->serverID;
$config["regenerate-config"] = "false";
$config["white-list"] = $config["whitelist"] === true ? "true":"false";
$config["spawn"] = implode(";", $config["spawn"]);
$config["white-list"] = $config["white-list"] === 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){
if($n == "spawn"){
$v = implode(";", $v);
}
$prop .= $n."=".$v."\r\n";
}
file_put_contents(FILE_PATH."server.properties", $prop);
@ -179,15 +181,39 @@ function serverCommands(){
$server->close();
break;
case "banip":
$s = implode(" ", $params);
if(trim($s) == ""){
console("[INFO] Usage: /banip <IP>");
break;
$p = strtolower(array_shift($params));
switch($p){
case "pardon":
case "remove":
$ip = trim(implode($params));
$new = array();
foreach(explode(file_get_contents(FILE_PATH."banned-ips.txt")) as $i){
if($i == $ip){
console("[INFO] IP \"$ip\" removed from ban list");
continue;
}
$new[$ip] = $ip;
}
file_put_contents(FILE_PATH."banned-ips.txt", implode("\r\n", $new));
loadConfig();
break;
case "add":
case "ban":
$ip = trim(implode($params));
file_put_contents(FILE_PATH."banned-ips.txt", "\r\n".$ip, FILE_APPEND);
console("[INFO] IP \"$ip\" added to ban list");
loadConfig();
break;
case "reload":
loadConfig();
break;
case "list":
console("[INFO] IP ban list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."banned-ips.txt")))));
break;
default:
console("[INFO] Usage: /banip <add | remove | list | reload> [IP]");
break;
}
file_put_contents(FILE_PATH."banned-ips.txt", "\r\n".$s, FILE_APPEND);
console("[INFO] IP \"$s\" added to IP ban list");
loadConfig();
break;
break;
case "gamemode":
$s = trim(array_shift($params));
@ -253,12 +279,31 @@ function serverCommands(){
case "whitelist":
$p = strtolower(array_shift($params));
switch($p){
case "remove":
$user = trim(implode(" ", $params));
$new = array();
foreach(explode(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));
loadConfig();
break;
case "add":
$user = trim(implode(" ", $params));
file_put_contents(FILE_PATH."white-list.txt", "\r\n".$user, FILE_APPEND);
console("[INFO] Player \"$user\" added to white-list");
loadConfig();
break;
case "reload":
loadConfig(true);
break;
case "list":
console("[INFO] White-list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt")))));
break;
case "on":
case "true":
case "1":
@ -273,12 +318,6 @@ function serverCommands(){
$config["white-list"] = false;
loadConfig(true);
break;
case "reload":
loadConfig(true);
break;
case "list":
console("[INFO] White-list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt")))));
break;
default:
console("[INFO] Usage: /whitelist <on | off | add | reload | list> [username]");
break;
@ -315,7 +354,7 @@ function serverCommands(){
console("[INFO] /list: Lists online users");
console("[INFO] /save-all: Saves pending changes");
console("[INFO] /whitelist: Manages whitelisting");
console("[INFO] /banip: Bans an IP");
console("[INFO] /banip: Manages IP ban");
console("[INFO] /stop: Stops the server");
break;
default: