2013-08-01 09:18:14 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-08-01 09:18:14 +02:00
|
|
|
|
2014-10-31 10:54:53 +01:00
|
|
|
namespace Icinga\File\Ini;
|
2013-08-01 09:18:14 +02:00
|
|
|
|
2014-04-23 13:48:43 +02:00
|
|
|
use Zend_Config;
|
|
|
|
use Zend_Config_Ini;
|
2014-10-21 17:02:21 +02:00
|
|
|
use Zend_Config_Exception;
|
2014-04-23 13:48:43 +02:00
|
|
|
use Zend_Config_Writer_FileAbstract;
|
2014-11-07 13:53:03 +01:00
|
|
|
use Icinga\Application\Config;
|
2013-08-06 16:28:26 +02:00
|
|
|
|
2013-08-01 09:18:14 +02:00
|
|
|
/**
|
2014-10-31 10:54:53 +01:00
|
|
|
* A INI file adapter that respects the file structure and the comments of already existing ini files
|
2013-08-01 09:18:14 +02:00
|
|
|
*/
|
2014-10-31 10:54:53 +01:00
|
|
|
class IniWriter extends Zend_Config_Writer_FileAbstract
|
2013-08-01 09:18:14 +02:00
|
|
|
{
|
2013-08-27 11:06:15 +02:00
|
|
|
/**
|
|
|
|
* Stores the options
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-04-23 13:48:43 +02:00
|
|
|
protected $options;
|
2013-08-27 11:06:15 +02:00
|
|
|
|
2014-10-21 17:02:21 +02:00
|
|
|
/**
|
|
|
|
* The mode to set on new files
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-01-19 11:16:17 +01:00
|
|
|
public static $fileMode = 0660;
|
2014-10-21 17:02:21 +02:00
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
/**
|
2014-10-31 10:54:53 +01:00
|
|
|
* Create a new INI writer
|
2013-08-27 11:06:15 +02:00
|
|
|
*
|
2014-10-21 17:02:21 +02:00
|
|
|
* @param array $options Supports all options of Zend_Config_Writer and additional options:
|
|
|
|
* * filemode: The mode to set on new files
|
2013-08-27 11:06:15 +02:00
|
|
|
* * valueIndentation: The indentation level of the values
|
|
|
|
* * commentIndentation: The indentation level of the comments
|
|
|
|
* * sectionSeparators: The amount of newlines between sections
|
|
|
|
*
|
|
|
|
* @link http://framework.zend.com/apidoc/1.12/files/Config.Writer.html#\Zend_Config_Writer
|
|
|
|
*/
|
|
|
|
public function __construct(array $options = null)
|
|
|
|
{
|
2014-11-07 13:53:03 +01:00
|
|
|
if (isset($options['config']) && $options['config'] instanceof Config) {
|
|
|
|
// As this class inherits from Zend_Config_Writer_FileAbstract we must
|
|
|
|
// not pass the config directly as it needs to be of type Zend_Config
|
|
|
|
$options['config'] = new Zend_Config($options['config']->toArray(), true);
|
|
|
|
}
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->options = $options;
|
|
|
|
parent::__construct($options);
|
|
|
|
}
|
|
|
|
|
2013-08-06 16:28:26 +02:00
|
|
|
/**
|
|
|
|
* Render the Zend_Config into a config file string
|
|
|
|
*
|
2014-04-23 13:48:43 +02:00
|
|
|
* @return string
|
2013-08-06 16:28:26 +02:00
|
|
|
*/
|
|
|
|
public function render()
|
2013-08-01 09:18:14 +02:00
|
|
|
{
|
2014-03-11 15:43:41 +01:00
|
|
|
if (file_exists($this->_filename)) {
|
|
|
|
$oldconfig = new Zend_Config_Ini($this->_filename);
|
2015-06-08 12:22:27 +02:00
|
|
|
$content = trim(file_get_contents($this->_filename));
|
2014-03-11 15:43:41 +01:00
|
|
|
} else {
|
|
|
|
$oldconfig = new Zend_Config(array());
|
2014-11-04 14:46:56 +01:00
|
|
|
$content = '';
|
2014-03-11 15:43:41 +01:00
|
|
|
}
|
2014-08-26 18:05:22 +02:00
|
|
|
|
2013-08-06 16:28:26 +02:00
|
|
|
$newconfig = $this->_config;
|
2014-11-04 14:46:56 +01:00
|
|
|
$editor = new IniEditor($content, $this->options);
|
2013-08-20 17:34:47 +02:00
|
|
|
$this->diffConfigs($oldconfig, $newconfig, $editor);
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->updateSectionOrder($newconfig, $editor);
|
2013-08-06 16:28:26 +02:00
|
|
|
return $editor->getText();
|
2013-08-01 09:18:14 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 17:02:21 +02:00
|
|
|
/**
|
|
|
|
* Write configuration to file and set file mode in case it does not exist yet
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param Zend_Config $config
|
|
|
|
* @param bool $exclusiveLock
|
|
|
|
*/
|
|
|
|
public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
|
|
|
|
{
|
|
|
|
$filePath = $filename !== null ? $filename : $this->_filename;
|
|
|
|
$setMode = false === file_exists($filePath);
|
|
|
|
|
|
|
|
parent::write($filename, $config, $exclusiveLock);
|
|
|
|
|
|
|
|
if ($setMode) {
|
|
|
|
$mode = isset($this->options['filemode']) ? $this->options['filemode'] : static::$fileMode;
|
|
|
|
if (is_int($mode) && false === @chmod($filePath, $mode)) {
|
|
|
|
throw new Zend_Config_Exception(sprintf('Failed to set file mode "%o" on file "%s"', $mode, $filePath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 09:18:14 +02:00
|
|
|
/**
|
2013-08-06 16:28:26 +02:00
|
|
|
* Create a property diff and apply the changes to the editor
|
|
|
|
*
|
2014-04-23 13:48:43 +02:00
|
|
|
* @param Zend_Config $oldconfig The config representing the state before the change
|
|
|
|
* @param Zend_Config $newconfig The config representing the state after the change
|
|
|
|
* @param IniEditor $editor The editor that should be used to edit the old config file
|
|
|
|
* @param array $parents The parent keys that should be respected when editing the config
|
2013-08-01 09:18:14 +02:00
|
|
|
*/
|
2014-04-23 13:48:43 +02:00
|
|
|
protected function diffConfigs(
|
2013-08-06 17:11:44 +02:00
|
|
|
Zend_Config $oldconfig,
|
|
|
|
Zend_Config $newconfig,
|
2013-08-06 16:28:26 +02:00
|
|
|
IniEditor $editor,
|
2013-08-07 15:21:49 +02:00
|
|
|
array $parents = array()
|
|
|
|
) {
|
2013-08-20 17:34:47 +02:00
|
|
|
$this->diffPropertyUpdates($oldconfig, $newconfig, $editor, $parents);
|
|
|
|
$this->diffPropertyDeletions($oldconfig, $newconfig, $editor, $parents);
|
2013-08-07 15:21:49 +02:00
|
|
|
}
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
/**
|
2014-04-23 13:48:43 +02:00
|
|
|
* Update the order of the sections in the ini file to match the order of the new config
|
2013-08-27 11:06:15 +02:00
|
|
|
*/
|
2014-04-23 13:48:43 +02:00
|
|
|
protected function updateSectionOrder(Zend_Config $newconfig, IniEditor $editor)
|
|
|
|
{
|
2013-08-27 11:06:15 +02:00
|
|
|
$order = array();
|
|
|
|
foreach ($newconfig as $key => $value) {
|
|
|
|
if ($value instanceof Zend_Config) {
|
|
|
|
array_push($order, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$editor->refreshSectionOrder($order);
|
|
|
|
}
|
|
|
|
|
2013-08-07 15:21:49 +02:00
|
|
|
/**
|
|
|
|
* Search for created and updated properties and use the editor to create or update these entries
|
|
|
|
*
|
2013-08-09 09:13:27 +02:00
|
|
|
* @param Zend_Config $oldconfig The config representing the state before the change
|
|
|
|
* @param Zend_Config $newconfig The config representing the state after the change
|
|
|
|
* @param IniEditor $editor The editor that should be used to edit the old config file
|
|
|
|
* @param array $parents The parent keys that should be respected when editing the config
|
2013-08-07 15:21:49 +02:00
|
|
|
*/
|
2014-04-23 13:48:43 +02:00
|
|
|
protected function diffPropertyUpdates(
|
2013-08-07 15:21:49 +02:00
|
|
|
Zend_Config $oldconfig,
|
|
|
|
Zend_Config $newconfig,
|
|
|
|
IniEditor $editor,
|
|
|
|
array $parents = array()
|
|
|
|
) {
|
2014-04-23 13:48:43 +02:00
|
|
|
// The current section. This value is null when processing the section-less root element
|
2013-08-06 17:11:44 +02:00
|
|
|
$section = empty($parents) ? null : $parents[0];
|
2014-04-23 13:48:43 +02:00
|
|
|
// Iterate over all properties in the new configuration file and search for changes
|
2013-08-06 16:28:26 +02:00
|
|
|
foreach ($newconfig as $key => $value) {
|
|
|
|
$oldvalue = $oldconfig->get($key);
|
2013-08-20 17:34:47 +02:00
|
|
|
$nextParents = array_merge($parents, array($key));
|
2014-04-23 13:48:43 +02:00
|
|
|
$keyIdentifier = empty($parents) ? array($key) : array_slice($nextParents, 1, null, true);
|
2013-08-06 17:11:44 +02:00
|
|
|
if ($value instanceof Zend_Config) {
|
2014-04-23 13:48:43 +02:00
|
|
|
// The value is a nested Zend_Config, handle it recursively
|
|
|
|
if ($section === null) {
|
|
|
|
// Update the section declaration
|
2013-08-06 16:28:26 +02:00
|
|
|
$extends = $newconfig->getExtends();
|
2014-04-23 13:48:43 +02:00
|
|
|
$extend = array_key_exists($key, $extends) ? $extends[$key] : null;
|
2013-08-20 17:34:47 +02:00
|
|
|
$editor->setSection($key, $extend);
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
2014-04-23 13:48:43 +02:00
|
|
|
if ($oldvalue === null) {
|
2013-08-06 17:11:44 +02:00
|
|
|
$oldvalue = new Zend_Config(array());
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
2013-08-20 17:34:47 +02:00
|
|
|
$this->diffConfigs($oldvalue, $value, $editor, $nextParents);
|
2013-08-06 16:28:26 +02:00
|
|
|
} else {
|
2014-04-23 13:48:43 +02:00
|
|
|
// The value is a plain value, use the editor to set it
|
2013-08-07 16:19:15 +02:00
|
|
|
if (is_numeric($key)) {
|
2013-08-20 17:34:47 +02:00
|
|
|
$editor->setArrayElement($keyIdentifier, $value, $section);
|
2013-08-06 16:28:26 +02:00
|
|
|
} else {
|
2013-08-20 17:34:47 +02:00
|
|
|
$editor->set($keyIdentifier, $value, $section);
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-01 09:18:14 +02:00
|
|
|
}
|
2013-08-07 15:21:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for deleted properties and use the editor to delete these entries
|
|
|
|
*
|
2013-08-09 09:13:27 +02:00
|
|
|
* @param Zend_Config $oldconfig The config representing the state before the change
|
|
|
|
* @param Zend_Config $newconfig The config representing the state after the change
|
|
|
|
* @param IniEditor $editor The editor that should be used to edit the old config file
|
|
|
|
* @param array $parents The parent keys that should be respected when editing the config
|
2013-08-07 15:21:49 +02:00
|
|
|
*/
|
2014-04-23 13:48:43 +02:00
|
|
|
protected function diffPropertyDeletions(
|
2013-08-07 15:21:49 +02:00
|
|
|
Zend_Config $oldconfig,
|
|
|
|
Zend_Config $newconfig,
|
|
|
|
IniEditor $editor,
|
|
|
|
array $parents = array()
|
|
|
|
) {
|
2014-04-23 13:48:43 +02:00
|
|
|
// The current section. This value is null when processing the section-less root element
|
2013-08-07 15:21:49 +02:00
|
|
|
$section = empty($parents) ? null : $parents[0];
|
|
|
|
|
2014-04-23 13:48:43 +02:00
|
|
|
// Iterate over all properties in the old configuration file and search for deleted properties
|
2013-08-06 16:28:26 +02:00
|
|
|
foreach ($oldconfig as $key => $value) {
|
2014-07-01 14:55:45 +02:00
|
|
|
if ($newconfig->get($key) === null) {
|
|
|
|
$nextParents = array_merge($parents, array($key));
|
|
|
|
$keyIdentifier = empty($parents) ? array($key) : array_slice($nextParents, 1, null, true);
|
|
|
|
foreach ($this->getPropertyIdentifiers($value, $keyIdentifier) as $propertyIdentifier) {
|
|
|
|
$editor->reset($propertyIdentifier, $section);
|
2014-07-01 14:45:00 +02:00
|
|
|
}
|
2014-07-01 14:21:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-01 14:55:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all possible combinations of property identifiers for the given value
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to return all combinations for
|
|
|
|
* @param array $key The root property identifier, if any
|
|
|
|
*
|
|
|
|
* @return array All property combinations that are possible
|
|
|
|
*
|
|
|
|
* @todo Cannot handle array properties yet (e.g. a.b[]='c')
|
|
|
|
*/
|
|
|
|
protected function getPropertyIdentifiers($value, array $key = null)
|
|
|
|
{
|
|
|
|
$combinations = array();
|
|
|
|
$rootProperty = $key !== null ? $key : array();
|
|
|
|
|
|
|
|
if ($value instanceof Zend_Config) {
|
|
|
|
foreach ($value as $subProperty => $subValue) {
|
|
|
|
$combinations = array_merge(
|
|
|
|
$combinations,
|
|
|
|
$this->getPropertyIdentifiers($subValue, array_merge($rootProperty, array($subProperty)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} elseif (is_string($value)) {
|
|
|
|
$combinations[] = $rootProperty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $combinations;
|
|
|
|
}
|
2014-11-19 11:47:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for filename
|
2014-12-30 15:39:03 +01:00
|
|
|
*
|
2014-11-19 11:47:31 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFilename()
|
|
|
|
{
|
|
|
|
return $this->_filename;
|
|
|
|
}
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|