From 498c050da7fe8a1472d178155ebab08076a0ff42 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Thu, 7 Feb 2013 09:37:15 +0100 Subject: [PATCH] Added OPs and permission checks --- .gitignore | 1 + src/API/BanAPI.php | 43 ++++++++++++++++++++++++++++++++++++++++++ src/API/ConsoleAPI.php | 6 ++++-- src/common/config.php | 2 +- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index b52575c90..b04e0a75b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ server.properties white-list.txt banned-ips.txt banned.txt +ops.txt ################# ## Eclipse diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index 1483f7eb3..02c8f34be 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -29,6 +29,7 @@ class BanAPI{ private $server; private $whitelist; private $banned; + private $ops; private $bannedIPs; function __construct(PocketMinecraftServer $server){ $this->server = $server; @@ -39,15 +40,57 @@ class BanAPI{ $this->whitelist = new Config(FILE_PATH."white-list.txt", CONFIG_LIST); $this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST); $this->banned = new Config(FILE_PATH."banned.txt", CONFIG_LIST); + $this->ops = new Config(FILE_PATH."ops.txt", CONFIG_LIST); $this->server->api->console->register("banip", "Manages IP Banning", array($this, "commandHandler")); $this->server->api->console->register("ban", "Manages Bannning", array($this, "commandHandler")); $this->server->api->console->register("kick", "Kicks a player", array($this, "commandHandler")); $this->server->api->console->register("whitelist", "Manages White-listing", array($this, "commandHandler")); + $this->server->api->console->register("op", "Ops a player", array($this, "commandHandler")); + $this->server->api->console->register("deop", "Deops a player", array($this, "commandHandler")); + $this->server->addHandler("console.command", array($this, "opCheck"), 1); + } + + public function isOp($username){ + if($this->server->api->dhandle("api.op.check", $username) === false){ + return true; + }elseif($this->op->exists($username)){ + return true; + } + return false; + } + + public function opCheck($data, $event){ + if($data["issuer"] instanceof Player){ + if($this->isOp($data["issuer"]->username)){ + return true; + } + }elseif($data["issuer"] === "console"){ + return true; + } + return false; } public function commandHandler($cmd, $params, $issuer){ $output = ""; switch($cmd){ + case "op": + $user = trim(implode(" ", $params)); + if($user == ""){ + break; + } + $this->ops->set($user); + $this->ops->save(); + $output .= $user." is now op\n"; + break; + case "deop": + $user = trim(implode(" ", $params)); + if($user == ""){ + break; + } + $this->ops->remove($user); + $this->ops->save(); + $output .= $user." is not longer op\n"; + break; case "kick": if(!isset($params[0])){ $output .= "Usage: /kick [reason]\n"; diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index ab674aba0..d1b9fbd52 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -77,7 +77,9 @@ class ConsoleAPI{ break; case "status": case "lag": - $this->server->debugInfo(true); + if(!($issuer instanceof Player) and $issuer === "console"){ + $this->server->debugInfo(true); + } $info = $this->server->debugInfo(); $output .= "TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"].")\n"; break; @@ -192,7 +194,7 @@ class ConsoleAPI{ if($this->loop->line !== false){ $line = trim($this->loop->line); $this->loop->line = false; - $this->run($line); + $this->run($line, "console"); }else{ $this->loop->notify(); } diff --git a/src/common/config.php b/src/common/config.php index 26a69af68..bf8c1ac2c 100644 --- a/src/common/config.php +++ b/src/common/config.php @@ -42,5 +42,5 @@ define("MAJOR_VERSION", "Alpha_1.2dev"); define("CURRENT_STRUCTURE", 5); define("CURRENT_PROTOCOL", 9); define("CURRENT_MINECRAFT_VERSION", "v0.6.1 alpha"); -define("CURRENT_API_VERSION", 1); +define("CURRENT_API_VERSION", 2); define("CURRENT_PHP_VERSION", "5.4.11"); \ No newline at end of file