improved server config loading & validation

This commit is contained in:
Steffen Schröder 2014-06-13 17:24:59 +02:00
parent 2172962622
commit 0ecd68a6fd
3 changed files with 58 additions and 46 deletions

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;
}
}