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).
This commit is contained in:
Franco Fichtner 2024-09-12 19:47:44 +02:00
parent 3327890ab4
commit bb9353dba9

View File

@ -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;
}