improved server config loading & validation
This commit is contained in:
		@@ -345,9 +345,9 @@ class ManiaControl implements CommandListener, TimerListener {
 | 
				
			|||||||
		$this->log("Connecting to Server at {$this->server->config->host}:{$this->server->config->port}...");
 | 
							$this->log("Connecting to Server at {$this->server->config->host}:{$this->server->config->port}...");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		try {
 | 
							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) {
 | 
							} 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);
 | 
								$this->quit($message, true);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -114,44 +114,20 @@ class Server implements CallbackListener {
 | 
				
			|||||||
			$serverElement = $serverElements[0];
 | 
								$serverElement = $serverElements[0];
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Host
 | 
							// Get config elements
 | 
				
			||||||
		$hostElements = $serverElement->xpath('host');
 | 
							$hostElements = $serverElement->xpath('host');
 | 
				
			||||||
		if (!$hostElements) {
 | 
					 | 
				
			||||||
			$this->maniaControl->quit('Invalid server configuration (Missing Host).', true);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		$host = (string)$hostElements[0];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Port
 | 
					 | 
				
			||||||
		$portElements = $serverElement->xpath('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');
 | 
							$userElements = $serverElement->xpath('user');
 | 
				
			||||||
		if (!$userElements) {
 | 
							if (!$userElements) {
 | 
				
			||||||
			$userElements = $serverElement->xpath('login');
 | 
								$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');
 | 
							$passElements = $serverElement->xpath('pass');
 | 
				
			||||||
		if (!$passElements) {
 | 
					 | 
				
			||||||
			$this->maniaControl->quit('Invalid server configuration (Pass).', true);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		$pass = (string)$passElements[0];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Create config object
 | 
							// Create config object
 | 
				
			||||||
		$config = new ServerConfig($serverId, $host, $port, $user, $pass);
 | 
							$config  = new ServerConfig($serverId, $hostElements, $portElements, $userElements, $passElements);
 | 
				
			||||||
		if (!$config->validate()) {
 | 
							$message = null;
 | 
				
			||||||
			$this->maniaControl->quit("Your config file doesn't seem to be maintained properly. Please check the server configuration again!", true);
 | 
							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;
 | 
							$this->config = $config;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,38 +16,74 @@ class ServerConfig {
 | 
				
			|||||||
	public $id = null;
 | 
						public $id = null;
 | 
				
			||||||
	public $host = null;
 | 
						public $host = null;
 | 
				
			||||||
	public $port = null;
 | 
						public $port = null;
 | 
				
			||||||
	public $login = null;
 | 
						public $user = null;
 | 
				
			||||||
	public $pass = null;
 | 
						public $pass = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Create a new Server Config Instance
 | 
						 * Create a new Server Config Instance
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param string $id    Config Id
 | 
						 * @param mixed $id   Config Id
 | 
				
			||||||
	 * @param string $host  Server Ip
 | 
						 * @param mixed $host Server Ip
 | 
				
			||||||
	 * @param string $port  Server Port
 | 
						 * @param mixed $port Server Port
 | 
				
			||||||
	 * @param string $login XmlRpc Login
 | 
						 * @param mixed $user XmlRpc User
 | 
				
			||||||
	 * @param string $pass  XmlRpc Password
 | 
						 * @param mixed $pass XmlRpc Password
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public function __construct($id = null, $host = null, $port = null, $login = null, $pass = null) {
 | 
						public function __construct($id = null, $host = null, $port = null, $user = null, $pass = null) {
 | 
				
			||||||
		$this->id    = (string)$id;
 | 
							$this->id   = $this->extractConfigData($id);
 | 
				
			||||||
		$this->host  = (string)$host;
 | 
							$this->host = $this->extractConfigData($host);
 | 
				
			||||||
		$this->port  = (int)$port;
 | 
							$this->port = $this->extractConfigData($port);
 | 
				
			||||||
		$this->login = (string)$login;
 | 
							$this->user = $this->extractConfigData($user);
 | 
				
			||||||
		$this->pass  = (string)$pass;
 | 
							$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
 | 
						 * Validate the Config Data
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
 | 
						 * @param string $message
 | 
				
			||||||
	 * @return bool
 | 
						 * @return bool
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public function validate() {
 | 
						public function validate(&$message = null) {
 | 
				
			||||||
		if (!$this->host || !$this->port || !$this->login || !$this->pass) {
 | 
							// Host
 | 
				
			||||||
 | 
							if (!$this->host) {
 | 
				
			||||||
 | 
								$message = 'Missing Host!';
 | 
				
			||||||
			return false;
 | 
								return false;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if ($this->port === 'port') {
 | 
					
 | 
				
			||||||
 | 
							// Port
 | 
				
			||||||
 | 
							if (!$this->port || $this->port === 'port') {
 | 
				
			||||||
 | 
								$message = 'Missing Port!';
 | 
				
			||||||
			return false;
 | 
								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;
 | 
							return true;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user