From 58b07f266ccc36330be7a8390af3c6a71e0ee642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Fri, 13 Jun 2014 17:54:38 +0200 Subject: [PATCH] FileUtil::loadConfig now returns a DOMDocument improved config loading & error output --- application/core/Files/FileUtil.php | 25 ++++++++++++++++++++----- application/core/ManiaControl.php | 4 ++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/application/core/Files/FileUtil.php b/application/core/Files/FileUtil.php index 1166eafd..82844f92 100644 --- a/application/core/Files/FileUtil.php +++ b/application/core/Files/FileUtil.php @@ -68,22 +68,37 @@ abstract class FileUtil { } /** - * Load config xml-file + * Load the Config XML-File with the given Name * * @param string $fileName - * @return \SimpleXMLElement + * @return \DOMElement */ public static function loadConfig($fileName) { $fileLocation = ManiaControlDir . 'configs' . DIRECTORY_SEPARATOR . $fileName; if (!file_exists($fileLocation)) { - logMessage("Config File doesn't exist! ({$fileName})"); + logMessage("Config file doesn't exist! ('{$fileName}')"); return null; } if (!is_readable($fileLocation)) { - logMessage("Config File isn't readable! Please check the File Permissions. ({$fileName})"); + logMessage("Config file isn't readable! Please check the file permissions. ('{$fileName}')"); return null; } - return simplexml_load_file($fileLocation); + $configFileContent = @file_get_contents($fileLocation); + if (!$configFileContent) { + trigger_error("Couldn't load config file! ('{$fileName}')"); + return null; + } + $domDocument = new \DOMDocument(); + if (!@$domDocument->loadXML($configFileContent)) { + $message = "Config file isn't maintained properly! ('{$fileName}')"; + $error = error_get_last(); + if ($error && stripos($error['message'], 'DOMDocument::loadXML()') === 0) { + $message .= PHP_EOL . $error['message']; + } + trigger_error($message); + return null; + } + return $domDocument->firstChild; } /** diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index d052af7a..15d65105 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -65,7 +65,7 @@ class ManiaControl implements CommandListener, TimerListener { public $authenticationManager = null; public $callbackManager = null; public $chat = null; - /** @var \SimpleXMLElement $config */ + /** @var \DOMElement $config */ public $config = null; public $configurator = null; /** @var Connection $client */ @@ -160,7 +160,7 @@ class ManiaControl implements CommandListener, TimerListener { if (!$this->config) { $this->quit("Error loading Configuration XML-File! ('{$configFileName}')", true); } - if ($this->config->count() < 3) { + if ($this->config->childNodes->length < 3) { $this->quit("Your Configuration File ('{$configFileName}') doesn't seem to be maintained properly. Please check it again!", true); } }