From bb9353dba9f0b5cf4221fc6b83530f6602129ba2 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Thu, 12 Sep 2024 19:47:44 +0200 Subject: [PATCH] plugins: add plugins_argument_map() helper This helper parses "mapped" arguments for batch reloads of e.g. interfaces or devices or items related to that (like gateway names). The rules are simple: o If the mapped argument is null reload in full (return true) o If the mapped argument is an array handle it under later conditionals (return true) o If the mapped argument is of an unsupported type do nothing (return false) o If the mapped argument is not null but empty do nothing (return false) o If the argument is a string convert it to an array, splitting the string by comma (return true) o Modify the $map argument to be either null or a non-empty array to normalize the passed map (also for backwards-compatible reasons of newwanip_map and vpn_map intermediate steps). --- src/etc/inc/plugins.inc | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/etc/inc/plugins.inc b/src/etc/inc/plugins.inc index 190dcad7f..e92adb6c4 100644 --- a/src/etc/inc/plugins.inc +++ b/src/etc/inc/plugins.inc @@ -384,3 +384,37 @@ function plugins_xmlrpc_sync() } return $sync_settings; } + +function plugins_argument_map(&$arg) +{ + /* record the caller for a sensible log message */ + $caller = !empty(debug_backtrace()[1]['function']) ? debug_backtrace()[1]['function'] : 'unknown'; + + /* return value signals we have work to do */ + $ret = true; + $tmp = $arg; + + if (is_array($arg)) { + /* no need to deal with this now */ + } elseif (is_string($arg)) { + /* backwards compat with single string arguments */ + $tmp = explode(',', $arg); + $ret = !!strlen($arg); + } elseif (!is_null($arg)) { + log_msg(sprintf('%s: plugin argument map type "%s" not supported', $caller, gettype($arg)), LOG_WARNING); + $ret = false; + } + + if (is_array($tmp)) { + /* remove empty values or duplicates */ + $tmp = array_unique(array_filter($tmp)); + $ret = !!count($tmp); + } + + if ($ret) { + /* only modify argument if there is work to do */ + $arg = $tmp; + } + + return $ret; +}