Removed legacy transports and templates code (#9646)

* Removed legacy transports and templates code

* Add notification

* abstract LnmsCommand

* Only issue alerts "legacy" alerts to mail for default contacts, etc.

* Prevent no contacts error when default contact is not specified, or there are no contacts for other reasons.

* remove eval \o/

* Update notifications.rss
This commit is contained in:
Neil Lathwood
2019-02-15 13:58:59 +00:00
committed by Tony Murray
parent 0835d8899e
commit 5d74839139
6 changed files with 25 additions and 298 deletions

View File

@@ -1,113 +0,0 @@
<?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,
]));

View File

@@ -50,8 +50,6 @@ if (!LegacyAuth::user()->hasGlobalAdmin()) {
<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>
@@ -114,11 +112,6 @@ $('#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();
}
}
});
});
@@ -135,8 +128,6 @@ $('#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) {
@@ -156,32 +147,6 @@ $('#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",