mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
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:
committed by
Tony Murray
parent
0835d8899e
commit
5d74839139
@@ -55,16 +55,12 @@ class Template
|
||||
|
||||
public function getTitle($data)
|
||||
{
|
||||
$data['parsed_title'] = $this->bladeTitle($data);
|
||||
//FIXME remove Deprecated template
|
||||
return $this->legacyTitle($data);
|
||||
return $this->bladeTitle($data);
|
||||
}
|
||||
|
||||
public function getBody($data)
|
||||
{
|
||||
$data['template']['parsed_template'] = $this->bladeBody($data);
|
||||
//FIXME remove Deprecated template
|
||||
return $this->legacyBody($data);
|
||||
return $this->bladeBody($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,99 +97,6 @@ class Template
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Parse legacy body
|
||||
*
|
||||
* @param $data
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function legacyBody($data)
|
||||
{
|
||||
//FIXME remove Deprecated template
|
||||
$tpl = $data['template']->parsed_template;
|
||||
$msg = '$ret .= "'.str_replace(array('{else}', '{/if}', '{/foreach}'), array('"; } else { $ret .= "', '"; } $ret .= "', '"; } $ret .= "'), addslashes($tpl)).'";';
|
||||
$parsed = $msg;
|
||||
$s = strlen($msg);
|
||||
$x = $pos = -1;
|
||||
$buff = '';
|
||||
$if = $for = $calc = false;
|
||||
while (++$x < $s) {
|
||||
if ($msg[$x] == '{' && $buff == '') {
|
||||
$buff .= $msg[$x];
|
||||
} elseif ($buff == '{ ') {
|
||||
$buff = '';
|
||||
} elseif ($buff != '') {
|
||||
$buff .= $msg[$x];
|
||||
}
|
||||
|
||||
if ($buff == '{if') {
|
||||
$pos = $x;
|
||||
$if = true;
|
||||
} elseif ($buff == '{foreach') {
|
||||
$pos = $x;
|
||||
$for = true;
|
||||
} elseif ($buff == '{calc') {
|
||||
$pos = $x;
|
||||
$calc = true;
|
||||
}
|
||||
|
||||
if ($pos != -1 && $msg[$x] == '}') {
|
||||
$orig = $buff;
|
||||
$buff = '';
|
||||
$pos = -1;
|
||||
if ($if) {
|
||||
$if = false;
|
||||
$o = 3;
|
||||
$native = array(
|
||||
'"; if( ',
|
||||
' ) { $ret .= "',
|
||||
);
|
||||
} elseif ($for) {
|
||||
$for = false;
|
||||
$o = 8;
|
||||
$native = array(
|
||||
'"; foreach( ',
|
||||
' as $key=>$value) { $ret .= "',
|
||||
);
|
||||
} elseif ($calc) {
|
||||
$calc = false;
|
||||
$o = 5;
|
||||
$native = array(
|
||||
'"; $ret .= (float) (0+(',
|
||||
')); $ret .= "',
|
||||
);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cond = trim(populate(substr($orig, $o, -1), false));
|
||||
$native = $native[0].$cond.$native[1];
|
||||
$parsed = str_replace($orig, $native, $parsed);
|
||||
unset($cond, $o, $orig, $native);
|
||||
}//end if
|
||||
}//end while
|
||||
$parsed = populate($parsed);
|
||||
return RunJail($parsed, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Parse legacy title
|
||||
*
|
||||
* @param $data
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function legacyTitle($data)
|
||||
{
|
||||
//FIXME remove Deprecated template
|
||||
if (strstr($data['parsed_title'], '%')) {
|
||||
return RunJail('$ret = "'.populate(addslashes($data['parsed_title'])).'";', $data);
|
||||
} else {
|
||||
return $data['parsed_title'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Get the default template
|
||||
|
@@ -31,7 +31,7 @@ interface Transport
|
||||
* Gets called when an alert is sent
|
||||
*
|
||||
* @param $alert_data array An array created by DescribeAlert
|
||||
* @param $opts array|true The options from $config['alert']['transports'][$transport]
|
||||
* @param $opts array|true The options from the alert_transports transport_config column
|
||||
* @return mixed Returns if the call was successful
|
||||
*/
|
||||
public function deliverAlert($alert_data, $opts);
|
||||
|
@@ -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,
|
||||
]));
|
@@ -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",
|
||||
|
@@ -28,6 +28,7 @@ use LibreNMS\Alert\AlertData;
|
||||
use LibreNMS\Alerting\QueryBuilderParser;
|
||||
use LibreNMS\Authentication\LegacyAuth;
|
||||
use LibreNMS\Alert\AlertUtil;
|
||||
use LibreNMS\Config;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
|
||||
/**
|
||||
@@ -254,13 +255,14 @@ function RunRules($device_id)
|
||||
*/
|
||||
function GetContacts($results)
|
||||
{
|
||||
global $config, $authorizer;
|
||||
global $config;
|
||||
|
||||
if (sizeof($results) == 0) {
|
||||
return array();
|
||||
if (empty($results)) {
|
||||
return [];
|
||||
}
|
||||
if ($config['alert']['default_only'] === true || $config['alerts']['email']['default_only'] === true) {
|
||||
return array(''.($config['alert']['default_mail'] ? $config['alert']['default_mail'] : $config['alerts']['email']['default']) => '');
|
||||
if (Config::get('alert.default_only') === true || Config::get('alerts.email.default_only') === true) {
|
||||
$email = Config::get('alert.default_mail', Config::get('alerts.email.default'));
|
||||
return $email ? [$email => ''] : [];
|
||||
}
|
||||
$users = LegacyAuth::get()->getUserlist();
|
||||
$contacts = array();
|
||||
@@ -386,20 +388,6 @@ function populate($txt, $wrap = true)
|
||||
return $txt;
|
||||
}//end populate()
|
||||
|
||||
/**
|
||||
* "Safely" run eval
|
||||
* @param string $code Code to run
|
||||
* @param array $obj Object with variables
|
||||
* @return string|mixed
|
||||
*/
|
||||
function RunJail($code, $obj)
|
||||
{
|
||||
$ret = '';
|
||||
@eval($code);
|
||||
return $ret;
|
||||
}//end RunJail()
|
||||
|
||||
|
||||
/**
|
||||
* Describe Alert
|
||||
* @param array $alert Alert-Result from DB
|
||||
@@ -822,8 +810,6 @@ function RunAlerts()
|
||||
*/
|
||||
function ExtTransports($obj)
|
||||
{
|
||||
global $config;
|
||||
$tmp = false;
|
||||
$type = new Template;
|
||||
|
||||
// If alert transport mapping exists, override the default transports
|
||||
@@ -831,47 +817,28 @@ function ExtTransports($obj)
|
||||
|
||||
if (!$transport_maps) {
|
||||
$transport_maps = AlertUtil::getDefaultAlertTransports();
|
||||
$legacy_transports = array_unique(array_map(function ($transports) {
|
||||
return $transports['transport_type'];
|
||||
}, $transport_maps));
|
||||
foreach ($config['alert']['transports'] as $transport => $opts) {
|
||||
if (in_array($transport, $legacy_transports)) {
|
||||
// If it is a default transport type, then the alert has already been sent out, so skip
|
||||
continue;
|
||||
}
|
||||
if (is_array($opts)) {
|
||||
$opts = array_filter($opts);
|
||||
}
|
||||
$class = 'LibreNMS\\Alert\\Transport\\' . ucfirst($transport);
|
||||
if (($opts === true || !empty($opts)) && $opts != false && class_exists($class)) {
|
||||
$transport_maps[] = [
|
||||
'transport_id' => null,
|
||||
'transport_type' => $transport,
|
||||
'opts' => $opts,
|
||||
'legacy' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
unset($legacy_transports);
|
||||
}
|
||||
|
||||
// alerting for default contacts, etc
|
||||
if (Config::get('alert.transports.mail') === true && !empty($obj['contacts'])) {
|
||||
$transport_maps[] = [
|
||||
'transport_id' => null,
|
||||
'transport_type' => 'mail',
|
||||
'opts' => $obj,
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($transport_maps as $item) {
|
||||
$class = 'LibreNMS\\Alert\\Transport\\'.ucfirst($item['transport_type']);
|
||||
//FIXME remove Deprecated noteice
|
||||
$dep_notice = 'DEPRECATION NOTICE: https://t.libren.ms/deprecation-alerting';
|
||||
if (class_exists($class)) {
|
||||
//FIXME remove Deprecated transport
|
||||
$transport_title = ($item['legacy'] === true) ? "Transport {$item['transport_type']} (%YTransport $dep_notice%n)" : "Transport {$item['transport_type']}";
|
||||
$transport_title = "Transport {$item['transport_type']}";
|
||||
$obj['transport'] = $item['transport_type'];
|
||||
$obj['transport_name'] = $item['transport_name'];
|
||||
$obj['alert'] = new AlertData($obj);
|
||||
$obj['title'] = $type->getTitle($obj);
|
||||
$obj['alert']['title'] = $obj['title'];
|
||||
$obj['msg'] = $type->getBody($obj);
|
||||
//FIXME remove Deprecated template check
|
||||
if (preg_match('/{\/if}/', $type->getTemplate()->template)) {
|
||||
c_echo(" :: %YTemplate $dep_notice :: Please update your template " . $type->getTemplate()->name . "%n" . PHP_EOL);
|
||||
}
|
||||
c_echo(" :: $transport_title => ");
|
||||
$instance = new $class($item['transport_id']);
|
||||
$tmp = $instance->deliverAlert($obj, $item['opts']);
|
||||
|
@@ -48,5 +48,10 @@
|
||||
enable this. Please note that the default is off to match with the other default contact settings. If you expect normal users to receive alerts then you should enable this new option.</description>
|
||||
<pubDate>Mon, 15 May 2017 23:00:00 +0000</pubDate>
|
||||
</item>
|
||||
<item>
|
||||
<title>Legacy Alert Templates and Transports removed</title>
|
||||
<description>As described previously, Legacy Alert Templates and Transports have been removed. You can find more info here: https://community.librenms.org/t/deprecation-notice-alerting-legacy-transports-and-templates/5915</description>
|
||||
<pubDate>Fri, 15 Feb 2017 23:00:00 +0000</pubDate>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>
|
||||
|
Reference in New Issue
Block a user