From 8bddfe7225cb587cd1cab2fa5b9a5e4dc69f51e9 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Sat, 8 Dec 2018 07:16:49 -0600 Subject: [PATCH] Fixed plugins using d_echo (#9498) Move d_echo to helpers.php and include in autoload Don't remove from common.php yet to be extra safe. --- LibreNMS/Plugins.php | 13 ++++++++---- composer.json | 5 ++++- includes/common.php | 25 +++++++++++++---------- includes/helpers.php | 48 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 includes/helpers.php diff --git a/LibreNMS/Plugins.php b/LibreNMS/Plugins.php index 5e6997a755..16abddae08 100644 --- a/LibreNMS/Plugins.php +++ b/LibreNMS/Plugins.php @@ -27,6 +27,7 @@ namespace LibreNMS; use App\Models\Plugin; +use Log; /** * Handles loading of plugins @@ -179,10 +180,14 @@ class Plugins } foreach (self::$plugins[$hook] as $name) { - if (!is_array($params)) { - @call_user_func(array($name, $hook)); - } else { - @call_user_func_array(array($name, $hook), $params); + try { + if (!is_array($params)) { + @call_user_func([$name, $hook]); + } else { + @call_user_func_array([$name, $hook], $params); + } + } catch (\Exception $e) { + Log::error($e); } } } diff --git a/composer.json b/composer.json index 3e1c49b61f..ffc5ac96fc 100644 --- a/composer.json +++ b/composer.json @@ -72,7 +72,10 @@ "LibreNMS\\": "LibreNMS", "LibreNMS\\Plugins\\" : "html/plugins", "LibreNMS\\Tests\\": "tests" - } + }, + "files": [ + "includes/helpers.php" + ] }, "scripts": { "pre-update-cmd": "LibreNMS\\ComposerHelper::preUpdate", diff --git a/includes/common.php b/includes/common.php index 10aa755a68..85528fe113 100644 --- a/includes/common.php +++ b/includes/common.php @@ -657,20 +657,23 @@ function is_valid_hostname($hostname) /* * convenience function - please use this instead of 'if ($debug) { echo ...; }' */ -function d_echo($text, $no_debug_text = null) -{ - global $debug; +if (!function_exists('d_echo')) { + //TODO remove this after installs have updated, leaving it for for transition + function d_echo($text, $no_debug_text = null) + { + global $debug; - if (class_exists('\Log')) { - \Log::debug(is_string($text) ? rtrim($text) : $text); - } elseif ($debug) { - print_r($text); - } + if (class_exists('\Log')) { + \Log::debug(is_string($text) ? rtrim($text) : $text); + } elseif ($debug) { + print_r($text); + } - if (!$debug && $no_debug_text) { - echo "$no_debug_text"; + if (!$debug && $no_debug_text) { + echo "$no_debug_text"; + } } -} // d_echo +} /** * Output using console color if possible diff --git a/includes/helpers.php b/includes/helpers.php new file mode 100644 index 0000000000..e021bf0d71 --- /dev/null +++ b/includes/helpers.php @@ -0,0 +1,48 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2018 Tony Murray + * @author Tony Murray + */ + +if (!function_exists('d_echo')) { + /** + * Legacy convenience function - please use this instead of 'if ($debug) { echo ...; }' + * Use Log directly in pure Laravel code! + * + * @param string|array $text The error message or array to print + * @param string $no_debug_text Text to print if debug is disabled + */ + function d_echo($text, $no_debug_text = null) + { + global $debug; + + if (class_exists('\Log')) { + \Log::debug(is_string($text) ? rtrim($text) : $text); + } elseif ($debug) { + print_r($text); + } + + if (!$debug && $no_debug_text) { + echo "$no_debug_text"; + } + } +}