IcingaConfigHelper: improved string escaping

This commit is contained in:
Thomas Gelf 2015-06-11 08:12:54 +02:00
parent c8576ea2db
commit db29f4b6c6
1 changed files with 26 additions and 3 deletions

View File

@ -56,12 +56,35 @@ class IcingaConfigHelper
} }
} }
// TODO: Find out how to allow multiline {{{...}}} strings.
// Parameter? Dedicated method? Always if \n is found?
public static function renderString($string) public static function renderString($string)
{ {
$string = preg_replace('~\\\~', '\\\\', $string); $special = array(
$string = preg_replace('~"~', '\\"', $string); '/\\\/',
'/"/',
'/\$/',
'/\t/',
'/\r/',
'/\n/',
// '/\b/', -> doesn't work
'/\f/'
);
return sprintf('"%s"', $string); $replace = array(
'\\\\',
'\\"',
'\\$',
'\\t',
'\\r',
'\\n',
// '\\b',
'\\f',
);
$string = preg_replace($special, $replace, $string);
return '"' . $string . '"';
} }
public static function isReserved($string) public static function isReserved($string)