From 0a56fc4694af3af80d3b159623ccddc8ffed6b19 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 19 Feb 2019 00:33:51 -0600 Subject: [PATCH] Restore alert template converter for a while longer (#9845) --- html/includes/forms/convert-template.inc.php | 113 +++++++++++++++++++ html/includes/modal/alert_template.inc.php | 35 ++++++ 2 files changed, 148 insertions(+) create mode 100644 html/includes/forms/convert-template.inc.php diff --git a/html/includes/forms/convert-template.inc.php b/html/includes/forms/convert-template.inc.php new file mode 100644 index 0000000000..c9674c3a18 --- /dev/null +++ b/html/includes/forms/convert-template.inc.php @@ -0,0 +1,113 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2018 Neil Lathwood + * @copyright 2018 Tony Murray + * @author Tony Murray + */ + +use LibreNMS\Authentication\LegacyAuth; + +header('Content-type: application/json'); + +if (!LegacyAuth::user()->hasGlobalAdmin()) { + die(json_encode([ + 'status' => 'error', + 'message' => 'You need to be admin', + ])); +} + +if (empty($vars['template'])) { + die(json_encode([ + 'status' => 'error', + 'message' => 'No template to convert', + ])); +} + +$new_body = ''; +foreach (explode(PHP_EOL, $vars['template']) as $line) { + $new_body .= convert_template($line) . PHP_EOL; +} +$new_title = convert_template($vars['title']); + +function convert_template($line) +{ + if (str_contains($line, '{calc')) { + return preg_replace( + [ + '/{calc[ ]*([\w\d\s\%\.\(\)\*\/\-\+\/]+)}/',// Replaces {calc (something*100)} + '/%([\w\d]+)\.([\w\d]+)/',// Replaces %something.anything + ], + [ + "@php\necho \\1;\n@endphp ", + '$value[\'\2\']', + ], + $line + ); + } + + $old1 = $line; + $find = [ + '/{if %([\w=\s]+)}/',// Replaces {if %something == else} + '/{else}/',// Replaces {else} + '/{\/if}/',// Replaces {/if} + '/{foreach %faults}/',// Replaces {foreach %faults} + '/{foreach %contacts}/',// Replaces {foreach %contacts} + '/{\/foreach}/',// Replaces {/foreach} + '/{calc[ ]*([\w\d\s\%\.\(\)\*\/\-\+\/]+)}/',// Replaces {calc (something*100)} + '/%value.string/',// Replaces %value.string + '/%([\w\d]+)\.([\w\d]+)/',// Replaces %something.anything + '/%([\w\d]+)/',// Replaces %anything + ]; + $replace = [ + ' @if ($alert->\1) ', + ' @else ', + ' @endif ', + ' @foreach ($alert->faults as $key => $value)', + ' @foreach ($alert->contacts as $key => $value)', + ' @endforeach ', + " @php\necho \\1;\n@endphp ", + '{{ $value[\'string\'] }}', + '{{ $\1[\'\2\'] }}', + '{{ $alert->\1 }}', + ]; + $old1 = preg_replace($find, $replace, $old1); + + // Revert some over-zealous changes: + $find = [ + '/\$alert->key/', + '/\$alert->value/', + ]; + $replace = [ + '$key', + '$value', + ]; + return preg_replace($find, $replace, $old1); +} + +die(json_encode([ + 'status' => 'ok', + 'message' => 'Template converted, review and save to update', + 'template' => $new_body, + 'title' => $new_title, +])); diff --git a/html/includes/modal/alert_template.inc.php b/html/includes/modal/alert_template.inc.php index cb3e7958e3..b4dcbd2b36 100644 --- a/html/includes/modal/alert_template.inc.php +++ b/html/includes/modal/alert_template.inc.php @@ -50,6 +50,8 @@ if (!LegacyAuth::user()->hasGlobalAdmin()) { + + @@ -112,6 +114,11 @@ $('#alert-template').on('show.bs.modal', function (event) { return data.text; } }).val(selected_rules).trigger("change"); + //FIXME remove Deprecated template + if(output['template'].indexOf("{/if}")>=0){ + toastr.info('The old template syntax is no longer supported. Please see https://docs.librenms.org/Alerting/Old_Templates/'); + $('#convert-template').show(); + } } }); }); @@ -128,6 +135,8 @@ $('#alert-template').on('hide.bs.modal', function(event) { $('#reset-default').remove(); $('#name').prop("disabled",false); $('#error').val(''); + //FIXME remove Deprecated template + $('#convert-template').hide(); }); $('#create-template').click('', function(e) { @@ -147,6 +156,32 @@ $('#create-template').click('', function(e) { alertTemplateAjaxOps(template, name, template_id, title, title_rec, rules_items.join(',')); }); +//FIXME remove Deprecated template +$('#convert-template').click('', function(e) { + e.preventDefault(); + var template = $("#template").val(); + var title = $("#title").val(); + $.ajax({ + type: "POST", + url: "ajax_form.php", + data: {type: "convert-template", template: template, title: title}, + dataType: "json", + success: function(output) { + if(output.status === 'ok') { + toastr.success(output.message); + $("#convert-template").hide(); + $("#template").val(output.template); + $("#title").val(output.title); + } else { + toastr.error(output.message); + } + }, + error: function(){ + toastr.error('An error occurred updating this alert template.'); + } + }); +}); + function alertTemplateAjaxOps(template, name, template_id, title, title_rec, rules) { $.ajax({ type: "POST",