Merge branch 'bugfix/don-t-use-parse_ini_file-in-Config-10150'

fixes #10150
This commit is contained in:
Alexander A. Klimov 2015-09-25 12:19:38 +02:00
commit a9ec0eebef
5 changed files with 55 additions and 4 deletions

View File

@ -12,6 +12,7 @@ use Icinga\Data\ConfigObject;
use Icinga\Data\Selectable; use Icinga\Data\Selectable;
use Icinga\Data\SimpleQuery; use Icinga\Data\SimpleQuery;
use Icinga\File\Ini\IniWriter; use Icinga\File\Ini\IniWriter;
use Icinga\File\Ini\IniParser;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
/** /**
@ -313,9 +314,7 @@ class Config implements Countable, Iterator, Selectable
if ($filepath === false) { if ($filepath === false) {
$emptyConfig->setConfigFile($file); $emptyConfig->setConfigFile($file);
} elseif (is_readable($filepath)) { } elseif (is_readable($filepath)) {
$config = new static(new ConfigObject(parse_ini_file($filepath, true))); return IniParser::parseIniFile($filepath);
$config->setConfigFile($filepath);
return $config;
} elseif (@file_exists($filepath)) { } elseif (@file_exists($filepath)) {
throw new NotReadableError(t('Cannot read config file "%s". Permission denied'), $filepath); throw new NotReadableError(t('Cannot read config file "%s". Permission denied'), $filepath);
} }

View File

@ -115,4 +115,18 @@ class Document
} }
return $str; 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;
}
} }

View File

@ -169,4 +169,18 @@ class Section
$str = str_replace(';', '\\;', $str); $str = str_replace(';', '\\;', $str);
return str_replace(PHP_EOL, ' ', $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;
}
} }

View File

@ -9,6 +9,8 @@ use Icinga\File\Ini\Dom\Document;
use Icinga\File\Ini\Dom\Directive; use Icinga\File\Ini\Dom\Directive;
use Icinga\Application\Logger; use Icinga\Application\Logger;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotReadableError;
use Icinga\Application\Config;
class IniParser class IniParser
{ {
@ -239,4 +241,25 @@ class IniParser
} }
return $doc; 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);
}
} }

View File

@ -8,6 +8,7 @@ use Icinga\Exception\NotReadableError;
use Icinga\Exception\NotWritableError; use Icinga\Exception\NotWritableError;
use Icinga\User\Preferences; use Icinga\User\Preferences;
use Icinga\User\Preferences\PreferencesStore; use Icinga\User\Preferences\PreferencesStore;
use Icinga\File\Ini\IniParser;
/** /**
* Load and save user preferences from and to INI files * Load and save user preferences from and to INI files
@ -57,7 +58,7 @@ class IniStore extends PreferencesStore
$this->getUser()->getUsername() $this->getUser()->getUsername()
); );
} else { } else {
$this->preferences = parse_ini_file($this->preferencesFile, true); $this->preferences = IniParser::parseIniFile($this->preferencesFile)->toArray();
} }
} }