Skip to content

Commit

Permalink
Restore alert template converter for a while longer (#9845)
Browse files Browse the repository at this point in the history
  • Loading branch information
murrant committed Feb 19, 2019
1 parent 06d61fc commit 0a56fc4
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
113 changes: 113 additions & 0 deletions html/includes/forms/convert-template.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
//FIXME remove Deprecated template

/**
* convert-template.inc.php
*
* Ajax method to convert templates from the old syntax to the blade syntax
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2018 Neil Lathwood
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

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,
]));
35 changes: 35 additions & 0 deletions html/includes/modal/alert_template.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
<input type="text" class="form-control input-sm" id="title_rec" name="title_rec" placeholder="Recovery Title">
</div>
<button type="button" class="btn btn-primary btn-sm" name="create-template" id="create-template">Create template</button>
<!--//FIXME remove Deprecated template-->
<button type="button" class="btn btn-default btn-sm" name="convert-template" id="convert-template" title="Convert template to new syntax" style="display: none">Convert template</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -112,6 +114,11 @@
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();
}
}
});
});
Expand All @@ -128,6 +135,8 @@
$('#reset-default').remove();
$('#name').prop("disabled",false);
$('#error').val('');
//FIXME remove Deprecated template
$('#convert-template').hide();
});

$('#create-template').click('', function(e) {
Expand All @@ -147,6 +156,32 @@
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",
Expand Down

0 comments on commit 0a56fc4

Please sign in to comment.