mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-27 07:44:04 +02:00
parent
c705f5d475
commit
4810a0d47e
@ -28,8 +28,6 @@
|
|||||||
|
|
||||||
namespace Icinga\Config;
|
namespace Icinga\Config;
|
||||||
|
|
||||||
use \Zend_Config_Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Edit the sections and keys of an ini in-place
|
* Edit the sections and keys of an ini in-place
|
||||||
*/
|
*/
|
||||||
@ -52,7 +50,7 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Create a new IniEditor
|
* Create a new IniEditor
|
||||||
*
|
*
|
||||||
* @param string $content The content of the ini as string
|
* @param string $content The content of the ini as string
|
||||||
*/
|
*/
|
||||||
public function __construct($content)
|
public function __construct($content)
|
||||||
{
|
{
|
||||||
@ -62,9 +60,9 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Set the value of the given key.
|
* Set the value of the given key.
|
||||||
*
|
*
|
||||||
* @param array $key The key to set
|
* @param array $key The key to set
|
||||||
* @param mixed $value The value to set
|
* @param string $value The value to set
|
||||||
* @param string $section The section to insert to.
|
* @param array $section The section to insert to.
|
||||||
*/
|
*/
|
||||||
public function set(array $key, $value, $section = null)
|
public function set(array $key, $value, $section = null)
|
||||||
{
|
{
|
||||||
@ -80,8 +78,8 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Reset the value of the given array element
|
* Reset the value of the given array element
|
||||||
*
|
*
|
||||||
* @param array $key The key of the array value
|
* @param array $key The key of the array value
|
||||||
* @param string $section The section of the array.
|
* @param array $section The section of the array.
|
||||||
*/
|
*/
|
||||||
public function resetArrayElement(array $key, $section = null)
|
public function resetArrayElement(array $key, $section = null)
|
||||||
{
|
{
|
||||||
@ -94,9 +92,9 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Set the value for an array element
|
* Set the value for an array element
|
||||||
*
|
*
|
||||||
* @param array $key The key of the property
|
* @param array $key The key of the property
|
||||||
* @param mixed $value The value of the property
|
* @param string $value The value of the property
|
||||||
* @param string $section The section to use
|
* @param array $section The section to use
|
||||||
*/
|
*/
|
||||||
public function setArrayElement(array $key, $value, $section = null)
|
public function setArrayElement(array $key, $value, $section = null)
|
||||||
{
|
{
|
||||||
@ -119,14 +117,15 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Get the line of an array element
|
* Get the line of an array element
|
||||||
*
|
*
|
||||||
* @param array $key The key of the property.
|
* @param array $key The key of the property.
|
||||||
* @param string $section The section to use
|
* @param $value The value
|
||||||
|
* @param $section The section to use
|
||||||
*
|
*
|
||||||
* @return int The line of the array element.
|
* @return The line of the array element.
|
||||||
*/
|
*/
|
||||||
private function getArrayElement(array $key, $section = null)
|
private function getArrayElement(array $key, $section = null)
|
||||||
{
|
{
|
||||||
$line = isset($section) ? $this->getSectionLine($section) +1 : 0;
|
$line = isset($section) ? $this->getSectionLine($section) + 1 : 0;
|
||||||
$index = array_pop($key);
|
$index = array_pop($key);
|
||||||
$formatted = $this->formatKey($key);
|
$formatted = $this->formatKey($key);
|
||||||
for (; $line < count($this->text); $line++) {
|
for (; $line < count($this->text); $line++) {
|
||||||
@ -134,7 +133,7 @@ class IniEditor
|
|||||||
if ($this->isSectionDeclaration($l)) {
|
if ($this->isSectionDeclaration($l)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (preg_match('/^\s*' . $formatted.'\[\]\s*=/', $l) === 1) {
|
if (preg_match('/^\s*' . $formatted . '\[\]\s*=/', $l) === 1) {
|
||||||
return $line;
|
return $line;
|
||||||
}
|
}
|
||||||
if ($this->isPropertyDeclaration($l, array_merge($key, array($index)))) {
|
if ($this->isPropertyDeclaration($l, array_merge($key, array($index)))) {
|
||||||
@ -147,8 +146,8 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* When it exists, set the key back to null
|
* When it exists, set the key back to null
|
||||||
*
|
*
|
||||||
* @param array $key The key to reset
|
* @param array $key The key to reset
|
||||||
* @param string $section The section of the key
|
* @param array $section The section of the key
|
||||||
*/
|
*/
|
||||||
public function reset(array $key, $section = null)
|
public function reset(array $key, $section = null)
|
||||||
{
|
{
|
||||||
@ -162,15 +161,15 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Create the section if it does not exist and set the properties
|
* Create the section if it does not exist and set the properties
|
||||||
*
|
*
|
||||||
* @param string $section The section name
|
* @param string $section The section name
|
||||||
* @param string $extend The section that should be extended by this section
|
* @param array $extend The section that should be extended by this section
|
||||||
*/
|
*/
|
||||||
public function setSection($section, $extend = null)
|
public function setSection($section, $extend = null)
|
||||||
{
|
{
|
||||||
if (isset($extend)) {
|
if (isset($extend)) {
|
||||||
$decl = '[' . $section . ' : ' . $extend.']';
|
$decl = '[' . $section . ' : ' . $extend.']';
|
||||||
} else {
|
} else {
|
||||||
$decl = '[' . $section.']';
|
$decl = '[' . $section . ']';
|
||||||
}
|
}
|
||||||
$line = $this->getSectionLine($section);
|
$line = $this->getSectionLine($section);
|
||||||
if ($line !== -1) {
|
if ($line !== -1) {
|
||||||
@ -185,7 +184,7 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Remove a section declaration
|
* Remove a section declaration
|
||||||
*
|
*
|
||||||
* @param string $section The section name
|
* @param string $section The section name
|
||||||
*/
|
*/
|
||||||
public function removeSection($section)
|
public function removeSection($section)
|
||||||
{
|
{
|
||||||
@ -198,9 +197,9 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Insert the key at the end of the corresponding section
|
* Insert the key at the end of the corresponding section
|
||||||
*
|
*
|
||||||
* @param array $key The key to insert
|
* @param array $key The key to insert
|
||||||
* @param mixed $value The value to insert
|
* @param mixed $value The value to insert
|
||||||
* @param string $section
|
* @param array $key The key to insert
|
||||||
*/
|
*/
|
||||||
private function insert(array $key, $value, $section = null)
|
private function insert(array $key, $value, $section = null)
|
||||||
{
|
{
|
||||||
@ -260,8 +259,8 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Insert the text at line $lineNr
|
* Insert the text at line $lineNr
|
||||||
*
|
*
|
||||||
* @param int $lineNr The line nr the inserted line should have
|
* @param $lineNr The line nr the inserted line should have
|
||||||
* @param string $toInsert The text that will be inserted
|
* @param $toInsert The text that will be inserted
|
||||||
*/
|
*/
|
||||||
private function insertAtLine($lineNr, $toInsert)
|
private function insertAtLine($lineNr, $toInsert)
|
||||||
{
|
{
|
||||||
@ -271,14 +270,14 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Update the line $lineNr
|
* Update the line $lineNr
|
||||||
*
|
*
|
||||||
* @param int $lineNr The line number of the target line
|
* @param $lineNr The line number of the target line
|
||||||
* @param string $content The content to replace
|
* @param $toInsert The new line content
|
||||||
*/
|
*/
|
||||||
private function updateLine($lineNr, $content)
|
private function updateLine($lineNr, $content)
|
||||||
{
|
{
|
||||||
$comment = $this->getComment($this->text[$lineNr]);
|
$comment = $this->getComment($this->text[$lineNr]);
|
||||||
if (strlen($comment) > 0) {
|
if (strlen($comment) > 0) {
|
||||||
$comment = ' ; ' . trim($comment);
|
$comment = " ; " . trim($comment);
|
||||||
}
|
}
|
||||||
$this->text[$lineNr] = str_pad($content, 43) . $comment;
|
$this->text[$lineNr] = str_pad($content, 43) . $comment;
|
||||||
}
|
}
|
||||||
@ -286,9 +285,9 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Get the comment from the given line
|
* Get the comment from the given line
|
||||||
*
|
*
|
||||||
* @param string $lineContent The content of the line
|
* @param $lineContent The content of the line
|
||||||
*
|
*
|
||||||
* @return string The extracted comment
|
* @return string The extracted comment
|
||||||
*/
|
*/
|
||||||
private function getComment($lineContent)
|
private function getComment($lineContent)
|
||||||
{
|
{
|
||||||
@ -298,14 +297,14 @@ class IniEditor
|
|||||||
*/
|
*/
|
||||||
$cleaned = preg_replace('/^[^;"]*"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"/s', '', $lineContent);
|
$cleaned = preg_replace('/^[^;"]*"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"/s', '', $lineContent);
|
||||||
|
|
||||||
$matches = mb_split(';', $cleaned, 2);
|
$matches = explode(';', $cleaned, 2);
|
||||||
return array_key_exists(1, $matches) ? $matches[1] : '';
|
return array_key_exists(1, $matches) ? $matches[1] : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete the line $lineNr
|
* Delete the line $lineNr
|
||||||
*
|
*
|
||||||
* @param int $lineNr The lineNr starting at 0
|
* @param $lineNr The lineNr starting at 0
|
||||||
*/
|
*/
|
||||||
private function deleteLine($lineNr)
|
private function deleteLine($lineNr)
|
||||||
{
|
{
|
||||||
@ -315,10 +314,10 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Format a key-value pair to an INI file-entry
|
* Format a key-value pair to an INI file-entry
|
||||||
*
|
*
|
||||||
* @param array $key The key
|
* @param array $key The key to format
|
||||||
* @param mixed $value The value
|
* @param string $value The value to format
|
||||||
*
|
*
|
||||||
* @return string The formatted key-value pair
|
* @return string The formatted key-value pair
|
||||||
*/
|
*/
|
||||||
private function formatKeyValuePair(array $key, $value)
|
private function formatKeyValuePair(array $key, $value)
|
||||||
{
|
{
|
||||||
@ -328,8 +327,9 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Format a key to an INI key
|
* Format a key to an INI key
|
||||||
*
|
*
|
||||||
* @param array $key
|
* @param array $key the key array to format
|
||||||
* @return string
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function formatKey(array $key)
|
private function formatKey(array $key)
|
||||||
{
|
{
|
||||||
@ -339,14 +339,14 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Get the first line after the given $section
|
* Get the first line after the given $section
|
||||||
*
|
*
|
||||||
* @param string $section The name of the section
|
* @param $section The name of the section
|
||||||
*
|
*
|
||||||
* @return int The line number of the section
|
* @return int The line number of the section
|
||||||
*/
|
*/
|
||||||
private function getSectionEnd($section = null)
|
private function getSectionEnd($section = null)
|
||||||
{
|
{
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$started = isset($section) ? false: true;
|
$started = isset($section) ? false : true;
|
||||||
foreach ($this->text as $line) {
|
foreach ($this->text as $line) {
|
||||||
if ($started && $this->isSectionDeclaration($line)) {
|
if ($started && $this->isSectionDeclaration($line)) {
|
||||||
if ($i === 0) {
|
if ($i === 0) {
|
||||||
@ -374,15 +374,15 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Check if the line contains the property declaration for a key
|
* Check if the line contains the property declaration for a key
|
||||||
*
|
*
|
||||||
* @param string $lineContent The content of the line
|
* @param string $lineContent The content of the line
|
||||||
* @param array $key The key this declaration is supposed to have
|
* @param array $key The key this declaration is supposed to have
|
||||||
*
|
*
|
||||||
* @return bool True, when the lineContent is a property declaration
|
* @return boolean True, when the lineContent is a property declaration
|
||||||
*/
|
*/
|
||||||
private function isPropertyDeclaration($lineContent, array $key)
|
private function isPropertyDeclaration($lineContent, array $key)
|
||||||
{
|
{
|
||||||
return preg_match(
|
return preg_match(
|
||||||
'/^\s*' . $this->formatKey($key) .'\s*=\s*/',
|
'/^\s*' . $this->formatKey($key) . '\s*=\s*/',
|
||||||
$lineContent
|
$lineContent
|
||||||
) === 1;
|
) === 1;
|
||||||
}
|
}
|
||||||
@ -390,15 +390,15 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Check if the given line contains a section declaration
|
* Check if the given line contains a section declaration
|
||||||
*
|
*
|
||||||
* @param string $lineContent The content of the line
|
* @param $lineContent The content of the line
|
||||||
* @param string $section The optional section name that will be assumed
|
* @param string $section The optional section name that will be assumed
|
||||||
*
|
*
|
||||||
* @return bool True, when the lineContent is a section declaration
|
* @return bool True, when the lineContent is a section declaration
|
||||||
*/
|
*/
|
||||||
private function isSectionDeclaration($lineContent, $section = null)
|
private function isSectionDeclaration($lineContent, $section = null)
|
||||||
{
|
{
|
||||||
if (isset($section)) {
|
if (isset($section)) {
|
||||||
return preg_match('/^\s*\[\s*'.$section.'\s*[\]:]/', $lineContent) === 1;
|
return preg_match('/^\s*\[\s*' . $section . '\s*[\]:]/', $lineContent) === 1;
|
||||||
} else {
|
} else {
|
||||||
return preg_match('/^\s*\[/', $lineContent) === 1;
|
return preg_match('/^\s*\[/', $lineContent) === 1;
|
||||||
}
|
}
|
||||||
@ -407,9 +407,9 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Get the line where the section begins
|
* Get the line where the section begins
|
||||||
*
|
*
|
||||||
* @param string $section The section
|
* @param $section The section
|
||||||
*
|
*
|
||||||
* @return int The line number
|
* @return int The line number
|
||||||
*/
|
*/
|
||||||
private function getSectionLine($section)
|
private function getSectionLine($section)
|
||||||
{
|
{
|
||||||
@ -426,13 +426,14 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Get the line number where the given key occurs
|
* Get the line number where the given key occurs
|
||||||
*
|
*
|
||||||
* @param array $keys The key and its parents
|
* @param array $keys The key and its parents
|
||||||
* @param string $section The section of the key
|
* @param $section The section of the key
|
||||||
*
|
*
|
||||||
* @return int The line number
|
* @return int The line number
|
||||||
*/
|
*/
|
||||||
private function getKeyLine(array $keys, $section = null)
|
private function getKeyLine(array $keys, $section = null)
|
||||||
{
|
{
|
||||||
|
$key = implode($this->nestSeparator, $keys);
|
||||||
$inSection = isset($section) ? false : true;
|
$inSection = isset($section) ? false : true;
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($this->text as $line) {
|
foreach ($this->text as $line) {
|
||||||
@ -453,7 +454,7 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Get the last line number occurring in the text
|
* Get the last line number occurring in the text
|
||||||
*
|
*
|
||||||
* @return int The line number of the last line
|
* @return The line number of the last line
|
||||||
*/
|
*/
|
||||||
private function getLastLine()
|
private function getLastLine()
|
||||||
{
|
{
|
||||||
@ -463,9 +464,9 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Insert a new element into a specific position of an array
|
* Insert a new element into a specific position of an array
|
||||||
*
|
*
|
||||||
* @param array $array The array to use
|
* @param $array The array to use
|
||||||
* @param int $pos The target position
|
* @param $pos The target position
|
||||||
* @param mixed $element The element to insert
|
* @param $element The element to insert
|
||||||
*
|
*
|
||||||
* @return array The changed array
|
* @return array The changed array
|
||||||
*/
|
*/
|
||||||
@ -478,12 +479,10 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Remove an element from an array
|
* Remove an element from an array
|
||||||
*
|
*
|
||||||
* @param array $array The array to use
|
* @param $array The array to use
|
||||||
* @param mixed $pos The position to remove
|
* @param $pos The position to remove
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
private function removeFromArray(array $array, $pos)
|
private function removeFromArray($array, $pos)
|
||||||
{
|
{
|
||||||
unset($array[$pos]);
|
unset($array[$pos]);
|
||||||
return array_values($array);
|
return array_values($array);
|
||||||
@ -492,9 +491,10 @@ class IniEditor
|
|||||||
/**
|
/**
|
||||||
* Prepare a value for INe
|
* Prepare a value for INe
|
||||||
*
|
*
|
||||||
* @param mixed $value The value of the string
|
* @param $value The value of the string
|
||||||
|
*
|
||||||
|
* @return string The formatted value
|
||||||
*
|
*
|
||||||
* @return string The formatted value
|
|
||||||
* @throws Zend_Config_Exception
|
* @throws Zend_Config_Exception
|
||||||
*/
|
*/
|
||||||
private function formatValue($value)
|
private function formatValue($value)
|
||||||
|
@ -87,7 +87,11 @@ class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
$form->setResources(
|
||||||
|
array(
|
||||||
|
'db' => 'db'
|
||||||
|
)
|
||||||
|
);
|
||||||
$form->setConfigDir('/tmp');
|
$form->setConfigDir('/tmp');
|
||||||
$view = new Zend_View();
|
$view = new Zend_View();
|
||||||
|
|
||||||
@ -117,7 +121,13 @@ class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$form->setConfigDir('/tmp');
|
$form->setConfigDir('/tmp');
|
||||||
|
$form->setResources(
|
||||||
|
array(
|
||||||
|
'db' => 'db'
|
||||||
|
)
|
||||||
|
);
|
||||||
$form->create();
|
$form->create();
|
||||||
|
|
||||||
$view = new Zend_View();
|
$view = new Zend_View();
|
||||||
|
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
@ -146,6 +156,11 @@ class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$form->setConfigDir('/tmp');
|
$form->setConfigDir('/tmp');
|
||||||
|
$form->setResources(
|
||||||
|
array(
|
||||||
|
'db' => 'db'
|
||||||
|
)
|
||||||
|
);
|
||||||
$form->create();
|
$form->create();
|
||||||
$view = new Zend_View();
|
$view = new Zend_View();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user