From e150a571de07936e0dd613d15efd1bd4414fcd54 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sun, 5 Jan 2025 18:26:13 +0100 Subject: [PATCH] mvc:JsonKeyValueStoreField - restore support for json input data without configd callout, closes https://github.com/opnsense/core/issues/8180 partially reverts https://github.com/opnsense/core/commit/525481c15ba2b7f3dd90418d43b2f943e8662511 --- .../FieldTypes/JsonKeyValueStoreField.php | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/JsonKeyValueStoreField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/JsonKeyValueStoreField.php index f7ed4de0a..860491d8a 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/JsonKeyValueStoreField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/JsonKeyValueStoreField.php @@ -123,17 +123,18 @@ class JsonKeyValueStoreField extends BaseListField protected function actionPostLoadingEvent() { $data = null; + if ($this->internalSourceFile != null && $this->internalSourceField != null) { + $sourcefile = sprintf($this->internalSourceFile, $this->internalSourceField); + } else { + $sourcefile = $this->internalSourceFile; + } + $cachename = $sourcefile != null ? $sourcefile : $this->internalConfigdPopulateAct; if (!empty($this->internalConfigdPopulateAct)) { - if (isset(static::$internalStaticContent[$this->internalConfigdPopulateAct])) { + if (isset(static::$internalStaticContent[$cachename])) { /* cached for the lifetime of this session */ - $data = static::$internalStaticContent[$this->internalConfigdPopulateAct]; - } elseif ($this->internalSourceFile != null) { + $data = static::$internalStaticContent[$cachename]; + } elseif ($sourcefile != null) { /* use a file cache for the configd call*/ - if ($this->internalSourceField != null) { - $sourcefile = sprintf($this->internalSourceFile, $this->internalSourceField); - } else { - $sourcefile = $this->internalSourceFile; - } if (is_file($sourcefile)) { $sourcehandle = fopen($sourcefile, "r+"); } else { @@ -166,12 +167,18 @@ class JsonKeyValueStoreField extends BaseListField true ); } - if ($data != null) { - static::$internalStaticContent[$this->internalConfigdPopulateAct] = $data; - $this->internalOptionList = $data; - if ($this->internalSelectAll && $this->internalValue == "") { - $this->internalValue = implode(',', array_keys($this->internalOptionList)); - } + } elseif ($sourcefile != null && is_file($sourcefile)) { + if (isset(static::$internalStaticContent[$cachename])) { + $data = static::$internalStaticContent[$cachename]; + } else { + $data = json_decode(file_get_contents($sourcefile), true) ?? []; + } + } + if ($data != null) { + static::$internalStaticContent[$cachename] = $data; + $this->internalOptionList = $data; + if ($this->internalSelectAll && $this->internalValue == "") { + $this->internalValue = implode(',', array_keys($this->internalOptionList)); } } }