diff --git a/library/Icinga/Application/Config.php b/library/Icinga/Application/Config.php index 0538f01d4..bfe2ddd60 100644 --- a/library/Icinga/Application/Config.php +++ b/library/Icinga/Application/Config.php @@ -12,6 +12,7 @@ use Icinga\Data\ConfigObject; use Icinga\Data\Selectable; use Icinga\Data\SimpleQuery; use Icinga\File\Ini\IniWriter; +use Icinga\File\Ini\IniParser; use Icinga\Exception\NotReadableError; /** @@ -313,9 +314,7 @@ class Config implements Countable, Iterator, Selectable if ($filepath === false) { $emptyConfig->setConfigFile($file); } elseif (is_readable($filepath)) { - $config = new static(new ConfigObject(parse_ini_file($filepath, true))); - $config->setConfigFile($filepath); - return $config; + return IniParser::parseIniFile($filepath); } elseif (@file_exists($filepath)) { throw new NotReadableError(t('Cannot read config file "%s". Permission denied'), $filepath); } diff --git a/library/Icinga/File/Ini/Dom/Document.php b/library/Icinga/File/Ini/Dom/Document.php index 69b628916..32f5b92c9 100644 --- a/library/Icinga/File/Ini/Dom/Document.php +++ b/library/Icinga/File/Ini/Dom/Document.php @@ -115,4 +115,18 @@ class Document } return $str; } + + /** + * Convert $this to an array + * + * @return array + */ + public function toArray() + { + $a = array(); + foreach ($this->sections as $section) { + $a[$section->getName()] = $section->toArray(); + } + return $a; + } } diff --git a/library/Icinga/File/Ini/Dom/Section.php b/library/Icinga/File/Ini/Dom/Section.php index dbc9188cb..42dbac709 100644 --- a/library/Icinga/File/Ini/Dom/Section.php +++ b/library/Icinga/File/Ini/Dom/Section.php @@ -169,4 +169,18 @@ class Section $str = str_replace(';', '\\;', $str); return str_replace(PHP_EOL, ' ', $str); } + + /** + * Convert $this to an array + * + * @return array + */ + public function toArray() + { + $a = array(); + foreach ($this->directives as $directive) { + $a[$directive->getKey()] = $directive->getValue(); + } + return $a; + } } diff --git a/library/Icinga/File/Ini/IniParser.php b/library/Icinga/File/Ini/IniParser.php index 43c96836d..72925cac1 100644 --- a/library/Icinga/File/Ini/IniParser.php +++ b/library/Icinga/File/Ini/IniParser.php @@ -9,6 +9,8 @@ use Icinga\File\Ini\Dom\Document; use Icinga\File\Ini\Dom\Directive; use Icinga\Application\Logger; use Icinga\Exception\ConfigurationError; +use Icinga\Exception\NotReadableError; +use Icinga\Application\Config; class IniParser { @@ -239,4 +241,25 @@ class IniParser } return $doc; } + + /** + * Read the ini file and parse it with ::parseIni() + * + * @param string $file The ini file to read + * + * @return Config + * @throws NotReadableError When the file cannot be read + */ + public static function parseIniFile($file) + { + if (($path = realpath($file)) === false) { + throw new NotReadableError('Couldn\'t compute the absolute path of `%s\'', $file); + } + + if (($content = file_get_contents($path)) === false) { + throw new NotReadableError('Couldn\'t read the file `%s\'', $path); + } + + return Config::fromArray(self::parseIni($content)->toArray())->setConfigFile($file); + } } diff --git a/library/Icinga/User/Preferences/Store/IniStore.php b/library/Icinga/User/Preferences/Store/IniStore.php index 88fa7edec..e5dbe4286 100644 --- a/library/Icinga/User/Preferences/Store/IniStore.php +++ b/library/Icinga/User/Preferences/Store/IniStore.php @@ -8,6 +8,7 @@ use Icinga\Exception\NotReadableError; use Icinga\Exception\NotWritableError; use Icinga\User\Preferences; use Icinga\User\Preferences\PreferencesStore; +use Icinga\File\Ini\IniParser; /** * Load and save user preferences from and to INI files @@ -57,7 +58,7 @@ class IniStore extends PreferencesStore $this->getUser()->getUsername() ); } else { - $this->preferences = parse_ini_file($this->preferencesFile, true); + $this->preferences = IniParser::parseIniFile($this->preferencesFile)->toArray(); } }