Fix storing of business process with Description having new lines (#339)

* Fix storing of business process with `Description` having new lines

The `LegacyConfigRenderer::renderHeader()` should render new lines in the `Description` field as comments.

This fix avoids occurrence of future issues due to new lines in `Description` field while adding or modifying the
business processes.

* Parse new lines in header

The new lines in header which may belong to the field like `Description` must be parsed correctly.
This commit is contained in:
raviks789 2022-09-14 09:53:54 +02:00 committed by GitHub
parent bd2cb52292
commit 3a7dc64a50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View file

@ -11,6 +11,9 @@ use Icinga\Module\Businessprocess\Metadata;
class LegacyConfigParser
{
/** @var string */
protected static $prevKey;
/** @var int */
protected $currentLineNumber;
@ -131,6 +134,7 @@ class LegacyConfigParser
$metadata = new Metadata($name);
$fh = fopen($filename, 'r');
$cnt = 0;
static::$prevKey = null;
while ($cnt < 15 && false !== ($line = fgets($fh))) {
$cnt++;
static::parseHeaderLine($line, $metadata);
@ -145,6 +149,8 @@ class LegacyConfigParser
$metadata = new Metadata($name);
$lines = preg_split('/\r?\n/', substr($string, 0, 8092));
static::$prevKey = null;
foreach ($lines as $line) {
static::parseHeaderLine($line, $metadata);
}
@ -192,8 +198,25 @@ class LegacyConfigParser
{
if (preg_match('/^\s*#\s+(.+?)\s*:\s*(.+)$/', trim($line), $m)) {
if ($metadata->hasKey($m[1])) {
static::$prevKey = $m[1];
$metadata->set($m[1], $m[2]);
}
} elseif ($line[0] === '#') {
$line = ltrim($line, "#");
// Check if the line is from the multi-line comment and parse it accordingly
if (trim($line) !== '' && ! preg_match('/^\s*(.+?)\s*:$/', trim($line), $m) && static::$prevKey) {
$line = trim(
substr(
$line,
strlen(sprintf("%-15s :", static::$prevKey)) + 2
),
"\n\r"
);
$description = $metadata->get(static::$prevKey) . "\n" . $line;
$metadata->set(static::$prevKey, $description);
}
}
}

View file

@ -52,7 +52,18 @@ class LegacyConfigRenderer
continue;
}
$str .= sprintf("# %-15s : %s\n", $key, $value);
$lineNum = 1;
$spaces = str_repeat(' ', strlen(sprintf("%-15s :", $key)));
foreach (preg_split('/\r?\n/', $value) as $line) {
if ($lineNum === 1) {
$str .= sprintf("# %-15s : %s\n", $key, $line);
} else {
$str .= sprintf("# %s %s\n", $spaces, $line);
}
$lineNum++;
}
}
$str .= "#\n###################################\n\n";