2015-06-11 07:44:22 +02:00
|
|
|
<?php
|
|
|
|
|
2015-06-11 22:44:17 +02:00
|
|
|
namespace Icinga\Module\Director\IcingaConfig;
|
2015-06-11 07:44:22 +02:00
|
|
|
|
2015-11-13 23:52:51 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
2015-06-11 07:44:22 +02:00
|
|
|
class IcingaConfigHelper
|
|
|
|
{
|
2015-06-11 07:52:26 +02:00
|
|
|
/**
|
|
|
|
* Reserved words according to
|
|
|
|
* http://docs.icinga.org/icinga2/snapshot/doc/module/icinga2/chapter/language-reference#reserved-keywords
|
|
|
|
*/
|
|
|
|
protected static $reservedWords = array(
|
|
|
|
'object',
|
|
|
|
'template',
|
|
|
|
'include',
|
|
|
|
'include_recursive',
|
2016-08-25 13:26:14 +02:00
|
|
|
'ignore_on_error',
|
2015-06-11 07:52:26 +02:00
|
|
|
'library',
|
|
|
|
'null',
|
|
|
|
'true',
|
|
|
|
'false',
|
|
|
|
'const',
|
|
|
|
'var',
|
|
|
|
'this',
|
|
|
|
'use',
|
|
|
|
'apply',
|
|
|
|
'to',
|
|
|
|
'where',
|
|
|
|
'import',
|
|
|
|
'assign',
|
|
|
|
'ignore',
|
|
|
|
'function',
|
|
|
|
'return',
|
|
|
|
'for',
|
|
|
|
'if',
|
|
|
|
'else',
|
|
|
|
'in',
|
2016-08-25 13:26:14 +02:00
|
|
|
'current_filename',
|
|
|
|
'current_line',
|
2015-06-11 07:52:26 +02:00
|
|
|
);
|
|
|
|
|
2015-06-11 23:02:43 +02:00
|
|
|
public static function renderKeyValue($key, $value, $prefix = ' ')
|
2016-02-29 17:36:12 +01:00
|
|
|
{
|
|
|
|
return self::renderKeyOperatorValue($key, '=', $value, $prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function renderKeyOperatorValue($key, $operator, $value, $prefix = ' ')
|
2015-06-11 07:44:22 +02:00
|
|
|
{
|
2015-06-11 23:02:43 +02:00
|
|
|
$string = sprintf(
|
2016-02-29 17:36:12 +01:00
|
|
|
"%s %s %s",
|
2015-06-11 07:44:22 +02:00
|
|
|
$key,
|
2016-02-29 17:36:12 +01:00
|
|
|
$operator,
|
2015-06-11 07:44:22 +02:00
|
|
|
$value
|
|
|
|
);
|
2015-06-11 23:02:43 +02:00
|
|
|
|
|
|
|
if ($prefix && strpos($string, "\n") !== false) {
|
|
|
|
return $prefix . implode("\n" . $prefix, explode("\n", $string)) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $prefix . $string . "\n";
|
2015-06-11 07:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function renderBoolean($value)
|
|
|
|
{
|
|
|
|
if ($value === 'y') {
|
|
|
|
return 'true';
|
|
|
|
} elseif ($value === 'n') {
|
|
|
|
return 'false';
|
|
|
|
} else {
|
|
|
|
throw new ProgrammingError('%s is not a valid boolean', $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 08:12:54 +02:00
|
|
|
// TODO: Find out how to allow multiline {{{...}}} strings.
|
|
|
|
// Parameter? Dedicated method? Always if \n is found?
|
2015-06-11 07:44:22 +02:00
|
|
|
public static function renderString($string)
|
|
|
|
{
|
2015-06-11 08:12:54 +02:00
|
|
|
$special = array(
|
|
|
|
'/\\\/',
|
|
|
|
'/"/',
|
|
|
|
'/\$/',
|
|
|
|
'/\t/',
|
|
|
|
'/\r/',
|
|
|
|
'/\n/',
|
|
|
|
// '/\b/', -> doesn't work
|
|
|
|
'/\f/'
|
|
|
|
);
|
|
|
|
|
|
|
|
$replace = array(
|
2015-06-11 21:32:46 +02:00
|
|
|
'\\\\\\',
|
2015-06-11 08:12:54 +02:00
|
|
|
'\\"',
|
|
|
|
'\\$',
|
|
|
|
'\\t',
|
|
|
|
'\\r',
|
|
|
|
'\\n',
|
|
|
|
// '\\b',
|
|
|
|
'\\f',
|
|
|
|
);
|
|
|
|
|
|
|
|
$string = preg_replace($special, $replace, $string);
|
2015-06-11 07:44:22 +02:00
|
|
|
|
2015-06-11 08:12:54 +02:00
|
|
|
return '"' . $string . '"';
|
2015-06-11 07:44:22 +02:00
|
|
|
}
|
2015-06-11 07:52:26 +02:00
|
|
|
|
2016-08-25 22:44:43 +02:00
|
|
|
public static function renderDictionaryKey($key)
|
|
|
|
{
|
2016-08-30 14:15:48 +02:00
|
|
|
if (preg_match('/^[a-z_]+[a-z0-9_]*$/i', $key)) {
|
2016-08-25 22:44:43 +02:00
|
|
|
return static::escapeIfReserved($key);
|
|
|
|
} else {
|
|
|
|
return static::renderString($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:54:38 +02:00
|
|
|
// Requires an array
|
2015-06-15 14:32:35 +02:00
|
|
|
public static function renderArray($array)
|
|
|
|
{
|
2015-06-16 17:54:38 +02:00
|
|
|
$data = array();
|
|
|
|
foreach ($array as $entry) {
|
|
|
|
if ($entry instanceof IcingaConfigRenderer) {
|
|
|
|
$data[] = $entry;
|
|
|
|
} else {
|
|
|
|
$data[] = self::renderString($entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$str = '[ ' . implode(', ', $data) . ' ]';
|
2015-06-15 14:32:35 +02:00
|
|
|
|
|
|
|
if (strlen($str) < 60) {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix for toConfigString?
|
2015-06-16 17:54:38 +02:00
|
|
|
return "[\n " . implode(",\n ", $data) . "\n]";
|
2015-06-15 14:32:35 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:51:06 +02:00
|
|
|
public static function renderDictionary($dictionary)
|
|
|
|
{
|
|
|
|
$vals = array();
|
|
|
|
foreach ($dictionary as $key => $value) {
|
2016-08-25 22:44:43 +02:00
|
|
|
$vals[$key] = rtrim(
|
|
|
|
self::renderKeyValue(
|
|
|
|
self::renderDictionaryKey($key),
|
|
|
|
$value
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($vals)) {
|
|
|
|
return '{}';
|
2015-06-15 14:51:06 +02:00
|
|
|
}
|
2016-09-07 22:44:03 +02:00
|
|
|
ksort($vals, SORT_STRING);
|
2015-06-15 14:51:06 +02:00
|
|
|
|
|
|
|
// Prefix for toConfigString?
|
|
|
|
return "{\n" . implode("\n", $vals) . "\n}";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-02 02:47:49 +01:00
|
|
|
public static function renderExpression($string)
|
|
|
|
{
|
|
|
|
return "{{\n " . $string . "\n}}";
|
|
|
|
}
|
|
|
|
|
2015-10-16 18:36:43 +02:00
|
|
|
public static function alreadyRendered($string)
|
|
|
|
{
|
|
|
|
return new IcingaConfigRendered($string);
|
|
|
|
}
|
|
|
|
|
2015-06-11 07:52:26 +02:00
|
|
|
public static function isReserved($string)
|
|
|
|
{
|
2016-08-30 14:15:48 +02:00
|
|
|
return in_array($string, self::$reservedWords, true);
|
2015-06-11 07:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function escapeIfReserved($string)
|
|
|
|
{
|
|
|
|
if (self::isReserved($string)) {
|
|
|
|
return '@' . $string;
|
|
|
|
} else {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}
|
2016-02-28 12:40:11 +01:00
|
|
|
|
|
|
|
public static function isValidInterval($interval)
|
|
|
|
{
|
|
|
|
if (ctype_digit($interval)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = preg_split('/\s+/', $interval, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
$value = 0;
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
if (! preg_match('/^(\d+)([dhms]?)$/', $part)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function parseInterval($interval)
|
|
|
|
{
|
2016-02-28 16:25:15 +01:00
|
|
|
if ($interval === null || $interval === '') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-02-28 12:40:11 +01:00
|
|
|
if (ctype_digit($interval)) {
|
|
|
|
return (int) $interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = preg_split('/\s+/', $interval, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
$value = 0;
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
if (! preg_match('/^(\d+)([dhms]?)$/', $part, $m)) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'"%s" is not a valid time (duration) definition',
|
|
|
|
$interval
|
|
|
|
);
|
|
|
|
}
|
|
|
|
switch ($m[2]) {
|
|
|
|
case 'd':
|
|
|
|
$value += $m[1] * 86400;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
$value += $m[1] * 3600;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
$value += $m[1] * 60;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$value += (int) $m[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function renderInterval($interval)
|
|
|
|
{
|
|
|
|
// TODO: compat only, do this at munge time. All db fields should be int
|
|
|
|
$seconds = self::parseInterval($interval);
|
|
|
|
if ($seconds === 0) {
|
|
|
|
return '0s';
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = array();
|
|
|
|
$steps = array(
|
|
|
|
'd' => 86400,
|
|
|
|
'h' => 3600,
|
|
|
|
'm' => 60,
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($steps as $unit => $duration) {
|
2016-02-28 13:55:16 +01:00
|
|
|
if ($seconds % $duration === 0) {
|
|
|
|
return (int) floor($seconds / $duration) . $unit;
|
2016-02-28 12:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 13:55:16 +01:00
|
|
|
return $seconds . 's';
|
2016-02-28 12:40:11 +01:00
|
|
|
}
|
2015-06-11 07:44:22 +02:00
|
|
|
}
|