From 5d7483913902ad4be0ccf22d129f9be6b917a155 Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Fri, 15 Feb 2019 13:58:59 +0000 Subject: [PATCH] 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 --- LibreNMS/Alert/Template.php | 101 +---------------- LibreNMS/Interfaces/Alert/Transport.php | 2 +- html/includes/forms/convert-template.inc.php | 113 ------------------- html/includes/modal/alert_template.inc.php | 35 ------ includes/alerts.inc.php | 67 +++-------- misc/notifications.rss | 5 + 6 files changed, 25 insertions(+), 298 deletions(-) delete mode 100644 html/includes/forms/convert-template.inc.php diff --git a/LibreNMS/Alert/Template.php b/LibreNMS/Alert/Template.php index f6a26de130..0aa0a3aa43 100644 --- a/LibreNMS/Alert/Template.php +++ b/LibreNMS/Alert/Template.php @@ -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 diff --git a/LibreNMS/Interfaces/Alert/Transport.php b/LibreNMS/Interfaces/Alert/Transport.php index 65ce9bf7b0..40246bd691 100644 --- a/LibreNMS/Interfaces/Alert/Transport.php +++ b/LibreNMS/Interfaces/Alert/Transport.php @@ -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); diff --git a/html/includes/forms/convert-template.inc.php b/html/includes/forms/convert-template.inc.php deleted file mode 100644 index c9674c3a18..0000000000 --- a/html/includes/forms/convert-template.inc.php +++ /dev/null @@ -1,113 +0,0 @@ -. - * - * @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 b4dcbd2b36..cb3e7958e3 100644 --- a/html/includes/modal/alert_template.inc.php +++ b/html/includes/modal/alert_template.inc.php @@ -50,8 +50,6 @@ if (!LegacyAuth::user()->hasGlobalAdmin()) { - - @@ -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", diff --git a/includes/alerts.inc.php b/includes/alerts.inc.php index 6e904c6aca..e794807409 100644 --- a/includes/alerts.inc.php +++ b/includes/alerts.inc.php @@ -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']); diff --git a/misc/notifications.rss b/misc/notifications.rss index 099785ddde..69311cbb5d 100644 --- a/misc/notifications.rss +++ b/misc/notifications.rss @@ -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. Mon, 15 May 2017 23:00:00 +0000 + + Legacy Alert Templates and Transports removed + 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 + Fri, 15 Feb 2017 23:00:00 +0000 +