mirror of
https://github.com/opnsense/core.git
synced 2026-02-19 02:28:59 -05:00
backend: allow non-intrusive config_read_array(); closes #9786
When config keys are not found or are not arrays that should
be (especially for iterating with foreach) we do a trick here
by returning a detached empty array to avoid upper layer
errors, forcing empty arrays into $config yet reading and
removal still work fine. The default stays the "insert" mode,
which can be triggered explicitly just for symmetry. Bools
are not in the keys so this is perfectly fine.
The function itself was added in 4c179c23 in 2017 and hasn't
been modified since which is quite the achievement IMO. It's
had a clear purpose but now we make it just a little bit
better. :)
Look for more references at least in the legacy pages:
# git grep 'foreach.($config\[' src/www
This commit is contained in:
parent
6ae73e8406
commit
feddc1f280
2 changed files with 30 additions and 5 deletions
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015-2022 Franco Fichtner <franco@opnsense.org>
|
||||
* Copyright (C) 2015-2026 Franco Fichtner <franco@opnsense.org>
|
||||
* Copyright (C) 2009 Erik Kristensen <erik@erikkristensen.com>
|
||||
* Copyright (C) 2004-2010 Scott Ullrich <sullrich@gmail.com>
|
||||
* Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
|
||||
|
|
@ -248,11 +248,36 @@ function &config_read_array()
|
|||
global $config;
|
||||
|
||||
$current = &$config;
|
||||
$insert_mode = true;
|
||||
|
||||
foreach (func_get_args() as $key) {
|
||||
$keys = func_get_args();
|
||||
if (is_bool($keys[array_key_last($keys)])) {
|
||||
/*
|
||||
* Last argument is the insert mode if given:
|
||||
*
|
||||
* If true, vifivy the config key with an array
|
||||
* and return the element for inserts (default).
|
||||
*
|
||||
* If false, do not vivify and return an empty
|
||||
* detached array reference instead. This avoids
|
||||
* clobbering reads which written to config would
|
||||
* make an empty array an empty string node instead
|
||||
* and where insert is not intended but can still
|
||||
* delete by index if the config path exists.
|
||||
*/
|
||||
$insert_mode = array_pop($keys);
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($current[$key]) || !is_array($current[$key])) {
|
||||
$current[$key] = [];
|
||||
if ($insert_mode) {
|
||||
$current[$key] = [];
|
||||
} else {
|
||||
$detached = [];
|
||||
return $detached;
|
||||
}
|
||||
}
|
||||
|
||||
$current = &$current[$key];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,8 +38,8 @@ $a_gateways = (new \OPNsense\Routing\Gateways())->gatewaysIndexedByName();
|
|||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (isset($_POST['act']) && $_POST['act'] == "del" ) {
|
||||
if (!empty($a_gateway_groups[$_POST['id']])) {
|
||||
foreach ($config['filter']['rule'] as $idx => $rule) {
|
||||
if ($rule['gateway'] == $a_gateway_groups[$_POST['id']]['name']) {
|
||||
foreach (config_read_array('filter', 'rule', false) as $idx => $rule) {
|
||||
if (!empty($rule['gateway']) && $rule['gateway'] == $a_gateway_groups[$_POST['id']]['name']) {
|
||||
unset($config['filter']['rule'][$idx]['gateway']);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue