icingaweb2/library/Icinga/Config/IniEditor.php

513 lines
14 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Config;
2013-08-20 17:34:47 +02:00
use \Zend_Config_Exception;
/**
* Edit the sections and keys of an ini in-place
*/
class IniEditor
{
/**
* The text that is edited
*
* @var array
*/
private $text;
/**
* The symbol that is used to separate keys
*
* @var string
*/
private $nestSeparator = '.';
/**
* Create a new IniEditor
*
2013-08-20 17:34:47 +02:00
* @param string $content The content of the ini as string
*/
public function __construct($content)
{
2013-08-20 17:34:47 +02:00
$this->text = explode("\n", $content);
}
/**
* Set the value of the given key.
*
2013-08-20 17:34:47 +02:00
* @param array $key The key to set
* @param mixed $value The value to set
* @param string $section The section to insert to.
*/
public function set(array $key, $value, $section = null)
{
2013-08-20 17:34:47 +02:00
$line = $this->getKeyLine($key, $section);
if ($line === -1) {
2013-08-20 17:34:47 +02:00
$this->insert($key, $value, $section);
} else {
2013-08-20 17:34:47 +02:00
$content = $this->formatKeyValuePair($key, $value);
$this->updateLine($line, $content);
}
}
/**
* Reset the value of the given array element
*
2013-08-20 17:34:47 +02:00
* @param array $key The key of the array value
* @param string $section The section of the array.
*/
public function resetArrayElement(array $key, $section = null)
{
$line = $this->getArrayElement($key, $section);
if ($line !== -1) {
$this->deleteLine($line);
}
}
/**
* Set the value for an array element
*
2013-08-20 17:34:47 +02:00
* @param array $key The key of the property
* @param mixed $value The value of the property
* @param string $section The section to use
*/
public function setArrayElement(array $key, $value, $section = null)
{
2013-08-20 17:34:47 +02:00
$line = $this->getArrayElement($key, $section);
if ($line !== -1) {
if (isset($section)) {
2013-08-20 17:34:47 +02:00
$this->updateLine($line, $this->formatKeyValuePair($key, $value));
} else {
$section = $key[0];
unset($key[0]);
$this->deleteLine($line);
$this->setSection($section);
2013-08-20 17:34:47 +02:00
$this->insert($key, $value, $section);
}
} else {
2013-08-20 17:34:47 +02:00
$this->insert($key, $value, $section);
}
}
/**
* Get the line of an array element
*
2013-08-20 17:34:47 +02:00
* @param array $key The key of the property.
* @param string $section The section to use
*
2013-08-20 17:34:47 +02:00
* @return int The line of the array element.
*/
private function getArrayElement(array $key, $section = null)
{
$line = isset($section) ? $this->getSectionLine($section) +1 : 0;
$index = array_pop($key);
$formatted = $this->formatKey($key);
for (; $line < count($this->text); $line++) {
$l = $this->text[$line];
if ($this->isSectionDeclaration($l)) {
return -1;
}
2013-08-20 17:34:47 +02:00
if (preg_match('/^\s*' . $formatted.'\[\]\s*=/', $l) === 1) {
return $line;
}
2013-08-20 17:34:47 +02:00
if ($this->isPropertyDeclaration($l, array_merge($key, array($index)))) {
return $line;
}
}
return -1;
}
/**
* When it exists, set the key back to null
*
2013-08-20 17:34:47 +02:00
* @param array $key The key to reset
* @param string $section The section of the key
*/
public function reset(array $key, $section = null)
{
2013-08-20 17:34:47 +02:00
$line = $this->getKeyLine($key, $section);
if ($line === -1) {
return;
}
$this->deleteLine($line);
}
/**
* Create the section if it does not exist and set the properties
*
2013-08-20 17:34:47 +02:00
* @param string $section The section name
* @param string $extend The section that should be extended by this section
*/
public function setSection($section, $extend = null)
{
if (isset($extend)) {
2013-08-20 17:34:47 +02:00
$decl = '[' . $section . ' : ' . $extend.']';
} else {
2013-08-20 17:34:47 +02:00
$decl = '[' . $section.']';
}
$line = $this->getSectionLine($section);
if ($line !== -1) {
$this->deleteLine($line);
2013-08-20 17:34:47 +02:00
$this->insertAtLine($line, $decl);
} else {
$line = $this->getLastLine();
2013-08-20 17:34:47 +02:00
$this->insertAtLine($line, $decl);
}
}
/**
* Remove a section declaration
*
2013-08-20 17:34:47 +02:00
* @param string $section The section name
*/
public function removeSection($section)
{
$line = $this->getSectionLine($section);
if ($line !== -1) {
$this->deleteLine($line);
}
}
/**
* Insert the key at the end of the corresponding section
*
2013-08-20 17:34:47 +02:00
* @param array $key The key to insert
* @param mixed $value The value to insert
* @param string $section
*/
private function insert(array $key, $value, $section = null)
{
$line = $this->getSectionEnd($section);
2013-08-20 17:34:47 +02:00
$content = $this->formatKeyValuePair($key, $value);
$this->insertAtLine($line, $content);
}
/**
* Get the edited text
*
* @return string The edited text
*/
public function getText()
{
$this->cleanUpWhitespaces();
2013-08-20 17:34:47 +02:00
return implode("\n", $this->text);
}
/**
* Remove all unneeded line breaks between sections
*/
private function cleanUpWhitespaces()
{
$i = count($this->text) - 1;
for (; $i > 0; $i--) {
$line = $this->text[$i];
if ($this->isSectionDeclaration($line) && $i > 0) {
$i--;
$line = $this->text[$i];
/*
* Ignore comments that are glued to the section declaration
*/
2013-08-20 17:34:47 +02:00
while ($i > 0 && preg_match('/^\s*;/', $line) === 1) {
$i--;
$line = $this->text[$i];
}
/*
* Remove whitespaces between the sections
*/
2013-08-20 17:34:47 +02:00
while ($i > 0 && preg_match('/^\s*$/', $line) === 1) {
$this->deleteLine($i);
$i--;
$line = $this->text[$i];
}
/*
* Add a single whitespace
*/
if ($i !== 0) {
2013-08-20 17:34:47 +02:00
$this->insertAtLine($i + 1, '');
}
}
}
}
/**
* Insert the text at line $lineNr
*
2013-08-20 17:34:47 +02:00
* @param int $lineNr The line nr the inserted line should have
* @param string $toInsert The text that will be inserted
*/
private function insertAtLine($lineNr, $toInsert)
{
2013-08-20 17:34:47 +02:00
$this->text = IniEditor::insertIntoArray($this->text, $lineNr, $toInsert);
}
/**
* Update the line $lineNr
*
2013-08-20 17:34:47 +02:00
* @param int $lineNr The line number of the target line
* @param string $content The content to replace
*/
private function updateLine($lineNr, $content)
{
$comment = $this->getComment($this->text[$lineNr]);
if (strlen($comment) > 0) {
2013-08-20 17:34:47 +02:00
$comment = ' ; ' . trim($comment);
}
2013-08-20 17:34:47 +02:00
$this->text[$lineNr] = str_pad($content, 43) . $comment;
}
/**
* Get the comment from the given line
*
2013-08-20 17:34:47 +02:00
* @param string $lineContent The content of the line
*
2013-08-20 17:34:47 +02:00
* @return string The extracted comment
*/
private function getComment($lineContent)
{
/*
* Remove all content in double quotes that is not behind a semicolon, recognizing
* escaped double quotes inside the string
*/
2013-08-20 17:34:47 +02:00
$cleaned = preg_replace('/^[^;"]*"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"/s', '', $lineContent);
2013-08-20 17:34:47 +02:00
$matches = mb_split(';', $cleaned, 2);
return array_key_exists(1, $matches) ? $matches[1] : '';
}
/**
* Delete the line $lineNr
*
2013-08-20 17:34:47 +02:00
* @param int $lineNr The lineNr starting at 0
*/
private function deleteLine($lineNr)
{
2013-08-20 17:34:47 +02:00
$this->text = $this->removeFromArray($this->text, $lineNr);
}
/**
* Format a key-value pair to an INI file-entry
*
2013-08-20 17:34:47 +02:00
* @param array $key The key
* @param mixed $value The value
*
* @return string The formatted key-value pair
*/
private function formatKeyValuePair(array $key, $value)
{
2013-08-20 17:34:47 +02:00
return str_pad($this->formatKey($key), 19) . ' = ' . $this->formatValue($value);
}
/**
* Format a key to an INI key
2013-08-20 17:34:47 +02:00
*
* @param array $key
* @return string
*/
private function formatKey(array $key)
{
2013-08-20 17:34:47 +02:00
return implode($this->nestSeparator, $key);
}
/**
* Get the first line after the given $section
*
2013-08-20 17:34:47 +02:00
* @param string $section The name of the section
*
2013-08-20 17:34:47 +02:00
* @return int The line number of the section
*/
private function getSectionEnd($section = null)
{
$i = 0;
$started = isset($section) ? false: true;
foreach ($this->text as $line) {
if ($started && $this->isSectionDeclaration($line)) {
if ($i === 0) {
return $i;
}
/*
* ignore all comments 'glued' to the next section, to allow section
* comments in front of sections
*/
2013-08-20 17:34:47 +02:00
while ($i > 0 && preg_match('/^\s*;/', $this->text[$i - 1]) === 1) {
$i--;
}
return $i;
2013-08-20 17:34:47 +02:00
} elseif ($this->isSectionDeclaration($line, $section)) {
$started = true;
}
$i++;
}
if (!$started) {
return -1;
}
return $i;
}
/**
* Check if the line contains the property declaration for a key
*
2013-08-20 17:34:47 +02:00
* @param string $lineContent The content of the line
* @param array $key The key this declaration is supposed to have
*
2013-08-20 17:34:47 +02:00
* @return bool True, when the lineContent is a property declaration
*/
private function isPropertyDeclaration($lineContent, array $key)
{
return preg_match(
2013-08-20 17:34:47 +02:00
'/^\s*' . $this->formatKey($key) .'\s*=\s*/',
$lineContent
) === 1;
}
/**
* Check if the given line contains a section declaration
*
2013-08-20 17:34:47 +02:00
* @param string $lineContent The content of the line
* @param string $section The optional section name that will be assumed
*
2013-08-20 17:34:47 +02:00
* @return bool True, when the lineContent is a section declaration
*/
private function isSectionDeclaration($lineContent, $section = null)
{
if (isset($section)) {
2013-08-20 17:34:47 +02:00
return preg_match('/^\s*\[\s*'.$section.'\s*[\]:]/', $lineContent) === 1;
} else {
2013-08-20 17:34:47 +02:00
return preg_match('/^\s*\[/', $lineContent) === 1;
}
}
/**
* Get the line where the section begins
*
2013-08-20 17:34:47 +02:00
* @param string $section The section
*
2013-08-20 17:34:47 +02:00
* @return int The line number
*/
private function getSectionLine($section)
{
$i = 0;
foreach ($this->text as $line) {
2013-08-20 17:34:47 +02:00
if ($this->isSectionDeclaration($line, $section)) {
return $i;
}
$i++;
}
return -1;
}
/**
* Get the line number where the given key occurs
*
2013-08-20 17:34:47 +02:00
* @param array $keys The key and its parents
* @param string $section The section of the key
*
* @return int The line number
*/
private function getKeyLine(array $keys, $section = null)
{
$inSection = isset($section) ? false : true;
$i = 0;
foreach ($this->text as $line) {
if ($inSection && $this->isSectionDeclaration($line)) {
return -1;
}
2013-08-20 17:34:47 +02:00
if ($inSection && $this->isPropertyDeclaration($line, $keys)) {
return $i;
}
2013-08-20 17:34:47 +02:00
if (!$inSection && $this->isSectionDeclaration($line, $section)) {
$inSection = true;
}
$i++;
}
return -1;
}
/**
* Get the last line number occurring in the text
*
2013-08-20 17:34:47 +02:00
* @return int The line number of the last line
*/
private function getLastLine()
{
return count($this->text);
}
/**
* Insert a new element into a specific position of an array
*
2013-08-20 17:34:47 +02:00
* @param array $array The array to use
* @param int $pos The target position
* @param mixed $element The element to insert
*
* @return array The changed array
*/
private static function insertIntoArray($array, $pos, $element)
{
array_splice($array, $pos, 0, $element);
return $array;
}
/**
* Remove an element from an array
*
2013-08-20 17:34:47 +02:00
* @param array $array The array to use
* @param mixed $pos The position to remove
*
* @return array
*/
2013-08-20 17:34:47 +02:00
private function removeFromArray(array $array, $pos)
{
unset($array[$pos]);
return array_values($array);
}
/**
* Prepare a value for INe
*
2013-08-20 17:34:47 +02:00
* @param mixed $value The value of the string
*
2013-08-20 17:34:47 +02:00
* @return string The formatted value
* @throws Zend_Config_Exception
*/
private function formatValue($value)
{
if (is_integer($value) || is_float($value)) {
return $value;
} elseif (is_bool($value)) {
return ($value ? 'true' : 'false');
} elseif (strpos($value, '"') === false) {
return '"' . $value . '"';
} else {
2013-08-20 17:34:47 +02:00
return '"' . str_replace('"', '\"', $value) . '"';
}
}
}