diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 464c5a13..380738d2 100755 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -11,7 +11,8 @@ var timerRefreshData = '' var modalCallbackFunction = ''; var emptyArr = ['undefined', "", undefined, null, 'null']; -UI_LANG = "English"; +var UI_LANG = "English"; +var settingsJSON = {} // urlParams = new Proxy(new URLSearchParams(window.location.search), { @@ -42,6 +43,7 @@ function setCache(key, data, expirationMinutes='') { sessionStorage.setItem(key, data); + // create cookie if expiration set to handle refresh of data if (expirationMinutes != '') { setCookie (key + '_session_expiry', 'OK', expirationMinutes='') @@ -110,9 +112,11 @@ function deleteAllCookies() { function cacheSettings() { - $.get('api/table_settings.json', function(res) { + $.get('api/table_settings.json', function(res) { + + settingsJSON = res; - data = res["data"]; + data = settingsJSON["data"]; data.forEach((set) => { setCache(`pia_set_${set.Code_Name}`, set.Value) @@ -148,7 +152,7 @@ function cacheStrings() Object.entries(res).forEach(([language, translations]) => { Object.entries(translations).forEach(([key, value]) => { - + // store as key - value pairs in session setCache(`pia_lang_${key}_${language}`, value) }); }); diff --git a/front/plugins/README.md b/front/plugins/README.md index ca7d54b3..b4bf425c 100755 --- a/front/plugins/README.md +++ b/front/plugins/README.md @@ -382,12 +382,15 @@ Below are some general additional notes, when defining `params`: > ``` -#### Setting object structure +#### ⚙ Setting object structure + +Required attributes are: - `"function": ""` - What function the setting drives or a simple unique code name - `"type": ""` - The form control used for the setting displayed in the Settings page and what values are accepted. - `"localized"` - a list of properties on the current JSON level which need to be localized - `"name"` and `"description"` - Displayed in the Settings page. An array of localized strings. (see Localized strings below). +- (optional) `"events"` - `` - to generate an execution button next to the input field of the setting (not fully tested) ##### Supported settings `function` values diff --git a/front/settings.php b/front/settings.php index b5a74801..65c6f62f 100755 --- a/front/settings.php +++ b/front/settings.php @@ -152,13 +152,12 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { headersHtml += ` @@ -232,20 +231,21 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { input += ''; } else if (set['Type'] === 'subnets') { - input = - '
' + - '
' + - '' + - '
' + - '
' + - '' + - '
' + - '
' + - '' + - '
' + - '
' + - '
' + - ` +
+
+ +
+
+ +
+ +
+ ` + - '
' + - '
' + - `` + - '
' + - '' + - '
' + - ` +
+
+ +
+ +
+
' + `
`; } else if (set['Type'] === 'json') { - input = ``; + input = ``; } - let eventsHtml = ""; + let eventsHtml = ""; const eventsList = createArray(set['Events']); - if (eventsList.length > 0 && set['Type'] !== 'json') { + if (eventsList.length > 0) { eventsList.forEach(event => { eventsHtml += ` fetchArray (SQLITE3_ASSOC)) { `; }); } - setHtml += input + eventsHtml + ` @@ -316,8 +316,36 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { } // todo fix - function createArray(valueString) { - return valueString.split(',').map(item => item.trim()); + function createArray(input) { + // Empty array + if (input === '[]') { + return []; + } + + // Regex patterns + const patternBrackets = /(^\s*\[)|(\]\s*$)/g; + const patternQuotes = /(^\s*')|('\s*$)/g; + const replacement = ''; + + // Remove brackets + const noBrackets = input.replace(patternBrackets, replacement); + + const options = []; + + // Create array + const optionsTmp = noBrackets.split(','); + + // Handle only one item in array + if (optionsTmp.length === 0) { + return [noBrackets.replace(patternQuotes, replacement)]; + } + + // Remove quotes + optionsTmp.forEach(item => { + options.push(item.replace(patternQuotes, replacement).trim()); + }); + + return options; } // number of settings has to be equal to @@ -397,60 +425,42 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { var settingsArray = []; // generate javascript to collect values - { + if (noConversion.includes(set['Type'])) { - foreach ($settings as $set) { - if(in_array($set['Type'] , $noConversion)) - { - echo 'settingsArray.push(["'.$set["Group"].'", "'.$set["Code_Name"].'", "'.$set["Type"].'", $("#'.$set["Code_Name"].'").val() ]);'; - } - elseif ($set['Type'] == "boolean") - { - echo 'temp = $("#'.$set["Code_Name"].'").is(":checked") ;'; - echo 'settingsArray.push(["'.$set["Group"].'", "'.$set["Code_Name"].'", "'.$set["Type"].'", temp ]);'; - } - elseif ($set['Type'] == "integer.checkbox") - { - echo 'temp = $("#'.$set["Code_Name"].'").is(":checked") ;'; - echo 'temp ? temp = 1 : temp = 0;'; - echo 'settingsArray.push(["'.$set["Group"].'", "'.$set["Code_Name"].'", "'.$set["Type"].'", temp ]);'; - } - elseif ($set["Code_Name"] == "SCAN_SUBNETS") - { - echo "var temps = []; - - $( '#SCAN_SUBNETS option' ).each( function( i, selected ) { - temps.push($( selected ).val()); - }); + console.log($(set["Code_Name"]).val()) + console.log(set["Code_Name"]) - "; - echo 'settingsArray.push(["'.$set["Group"].'", "'.$set["Code_Name"].'", "'.$set["Type"].'", temps ]);'; - } - elseif ($set['Type'] == "list") - { - echo 'console.log($("#'.$set["Code_Name"].'"));'; - echo "var temps = []; + settingsArray.push([set["Group"], set["Code_Name"], set["Type"], $('#'+set["Code_Name"]).val()]); + + } else if (set['Type'] === 'boolean' || set['Type'] === 'integer.checkbox') { - $( '#".$set["Code_Name"]." option' ).each( function( i, selected ) { - vl = $( selected ).val() - if (vl != '') - { + const temp = $(`#${set["Code_Name"]}`).is(':checked') ? 1 : 0; + settingsArray.push([set["Group"], set["Code_Name"], set["Type"], temp]); + + } else if (set['Code_Name'] === 'SCAN_SUBNETS') { + const temps = []; + $('#SCAN_SUBNETS option').each(function (i, selected) { + temps.push($(selected).val()); + }); + settingsArray.push([set["Group"], set["Code_Name"], set["Type"], JSON.stringify(temps)]); + } else if (set['Type'] === 'list') { + const temps = []; + $(`#${set["Code_Name"]} option`).each(function (i, selected) { + const vl = $(selected).val(); + if (vl !== '') { temps.push(vl); } - }); - console.log(temps); - "; - echo 'settingsArray.push(["'.$set["Group"].'", "'.$set["Code_Name"].'", "'.$set["Type"].'", temps ]);'; + }); + settingsArray.push([set["Group"], set["Code_Name"], set["Type"], JSON.stringify(temps)]); + } else if (set['Type'] === 'json') { + // todo: fix jquery value collection + const temps = $('#'+set["Code_Name"]).val(); + settingsArray.push([set["Group"], set["Code_Name"], set["Type"], temps]); } - elseif ($set['Type'] == "json") - { - // todo - } - } - - ?> + }); console.log(settingsArray); return settingsArray; } diff --git a/pialert/initialise.py b/pialert/initialise.py index 9454d2db..6d97a24f 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -27,6 +27,9 @@ from plugin import get_plugins_configs, print_plugin_info def ccd(key, default, config_dir, name, inputtype, options, group, events=[], desc = "", regex = "", setJsonMetadata = {}): result = default + if events is None: + events = [] + # use existing value if already supplied, otherwise default value is used if key in config_dir: result = config_dir[key] @@ -34,15 +37,12 @@ def ccd(key, default, config_dir, name, inputtype, options, group, events=[], de if inputtype == 'text': result = result.replace('\'', "{s-quote}") - # # store setting metadata as a JSON - # ccd(f'{key}__metadata', set, c_d, "", "json" , "", pref) - # if inputtype == 'json': - # result = json.dumps(result) - conf.mySettingsSQLsafe.append((key, name, desc, inputtype, options, regex, str(result), group, str(events))) + # save metadata in dummy setting conf.mySettingsSQLsafe.append((f'{key}__metadata', "metadata name", "metadata desc", 'json', "", "", json.dumps(setJsonMetadata), group, "")) conf.mySettings.append((key, name, desc, inputtype, options, regex, result, group, str(events))) + # save metadata in dummy setting conf.mySettings.append((f'{key}__metadata', "metadata name", "metadata desc", 'json', "", "", json.dumps(setJsonMetadata), group, "")) return result @@ -208,7 +208,19 @@ def importConfigs (db): # Setting code name / key key = pref + "_" + setFunction - v = ccd(key, set["default_value"], c_d, set["name"][0]["string"], set["type"] , str(set["options"]), pref, set) + # set.get() - returns None if not found, set["options"] raises error + # ccd(key, default, config_dir, name, inputtype, options, group, events=[], desc = "", regex = "", setJsonMetadata = {}): + v = ccd(key, + set["default_value"], + c_d, + set["name"][0]["string"], + set["type"] , + str(set["options"]), + group = pref, + events = set.get("events"), + desc = set["description"][0]["string"], + regex = "", + setJsonMetadata = set) # Save the user defined value into the object set["value"] = v