From 3a7dc64a50b981345816f1f005bf3527946eb00c Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Wed, 14 Sep 2022 09:53:54 +0200 Subject: [PATCH] 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. --- .../Storage/LegacyConfigParser.php | 23 +++++++++++++++++++ .../Storage/LegacyConfigRenderer.php | 13 ++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/library/Businessprocess/Storage/LegacyConfigParser.php b/library/Businessprocess/Storage/LegacyConfigParser.php index 17fc8a5..d78b45f 100644 --- a/library/Businessprocess/Storage/LegacyConfigParser.php +++ b/library/Businessprocess/Storage/LegacyConfigParser.php @@ -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); + } } } diff --git a/library/Businessprocess/Storage/LegacyConfigRenderer.php b/library/Businessprocess/Storage/LegacyConfigRenderer.php index 7e2e0b2..5b099fb 100644 --- a/library/Businessprocess/Storage/LegacyConfigRenderer.php +++ b/library/Businessprocess/Storage/LegacyConfigRenderer.php @@ -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";