From 0ecd68a6fd60e5dc66e2dbf0b25ff846fac30a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Fri, 13 Jun 2014 17:24:59 +0200 Subject: [PATCH] improved server config loading & validation --- application/core/ManiaControl.php | 4 +- application/core/Server/Server.php | 34 ++---------- application/core/Server/ServerConfig.php | 66 ++++++++++++++++++------ 3 files changed, 58 insertions(+), 46 deletions(-) diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 92023131..d052af7a 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -345,9 +345,9 @@ class ManiaControl implements CommandListener, TimerListener { $this->log("Connecting to Server at {$this->server->config->host}:{$this->server->config->port}..."); try { - $this->client = Connection::factory($this->server->config->host, $this->server->config->port, self::SCRIPT_TIMEOUT, $this->server->config->login, $this->server->config->pass, self::API_VERSION); + $this->client = Connection::factory($this->server->config->host, $this->server->config->port, self::SCRIPT_TIMEOUT, $this->server->config->user, $this->server->config->pass, self::API_VERSION); } catch (AuthenticationException $e) { - $message = "Couldn't authenticate on Server with User '{$this->server->config->login}' & Pass '{$this->server->config->pass}'! " . $e->getMessage(); + $message = "Couldn't authenticate on Server with User '{$this->server->config->user}' & Pass '{$this->server->config->pass}'! " . $e->getMessage(); $this->quit($message, true); } diff --git a/application/core/Server/Server.php b/application/core/Server/Server.php index 6a29a37a..df4471bc 100644 --- a/application/core/Server/Server.php +++ b/application/core/Server/Server.php @@ -114,44 +114,20 @@ class Server implements CallbackListener { $serverElement = $serverElements[0]; } - // Host + // Get config elements $hostElements = $serverElement->xpath('host'); - if (!$hostElements) { - $this->maniaControl->quit('Invalid server configuration (Missing Host).', true); - } - $host = (string)$hostElements[0]; - - // Port $portElements = $serverElement->xpath('port'); - if (!$portElements) { - $this->maniaControl->quit('Invalid server configuration (Missing Port).', true); - } - $port = (string)$portElements[0]; - - // Login $userElements = $serverElement->xpath('user'); if (!$userElements) { $userElements = $serverElement->xpath('login'); } - if (!$userElements) { - $this->maniaControl->quit('Invalid server configuration (Missing User).', true); - } - $user = (string)$userElements[0]; - if (!in_array($user, array('SuperAdmin', 'Admin', 'User'))) { - $this->maniaControl->quit('Invalid server configuration (Invalid User).', true); - } - - // Password $passElements = $serverElement->xpath('pass'); - if (!$passElements) { - $this->maniaControl->quit('Invalid server configuration (Pass).', true); - } - $pass = (string)$passElements[0]; // Create config object - $config = new ServerConfig($serverId, $host, $port, $user, $pass); - if (!$config->validate()) { - $this->maniaControl->quit("Your config file doesn't seem to be maintained properly. Please check the server configuration again!", true); + $config = new ServerConfig($serverId, $hostElements, $portElements, $userElements, $passElements); + $message = null; + if (!$config->validate($message)) { + $this->maniaControl->quit("Your config file doesn't seem to be maintained properly. Please check the server configuration again! {$message}", true); } $this->config = $config; } diff --git a/application/core/Server/ServerConfig.php b/application/core/Server/ServerConfig.php index d188ede8..01060df6 100644 --- a/application/core/Server/ServerConfig.php +++ b/application/core/Server/ServerConfig.php @@ -16,38 +16,74 @@ class ServerConfig { public $id = null; public $host = null; public $port = null; - public $login = null; + public $user = null; public $pass = null; /** * Create a new Server Config Instance * - * @param string $id Config Id - * @param string $host Server Ip - * @param string $port Server Port - * @param string $login XmlRpc Login - * @param string $pass XmlRpc Password + * @param mixed $id Config Id + * @param mixed $host Server Ip + * @param mixed $port Server Port + * @param mixed $user XmlRpc User + * @param mixed $pass XmlRpc Password */ - public function __construct($id = null, $host = null, $port = null, $login = null, $pass = null) { - $this->id = (string)$id; - $this->host = (string)$host; - $this->port = (int)$port; - $this->login = (string)$login; - $this->pass = (string)$pass; + public function __construct($id = null, $host = null, $port = null, $user = null, $pass = null) { + $this->id = $this->extractConfigData($id); + $this->host = $this->extractConfigData($host); + $this->port = $this->extractConfigData($port); + $this->user = $this->extractConfigData($user); + $this->pass = $this->extractConfigData($pass); + } + + /** + * Extract the actual Data from the given Config Param + * + * @param mixed $configParam + * @return string + */ + private function extractConfigData($configParam) { + if (is_array($configParam)) { + return (string)reset($configParam); + } + return (string)$configParam; } /** * Validate the Config Data * + * @param string $message * @return bool */ - public function validate() { - if (!$this->host || !$this->port || !$this->login || !$this->pass) { + public function validate(&$message = null) { + // Host + if (!$this->host) { + $message = 'Missing Host!'; return false; } - if ($this->port === 'port') { + + // Port + if (!$this->port || $this->port === 'port') { + $message = 'Missing Port!'; return false; } + + // User + if (!$this->user) { + $message = 'Missing User!'; + return false; + } + if (!in_array($this->user, array('SuperAdmin', 'Admin', 'User'))) { + $message = 'Invalid User!'; + return false; + } + + // Pass + if (!$this->pass) { + $message = 'Missing Pass!'; + return false; + } + return true; } }