From 06d7aa6623e04f2afe417bd6e513f1584803d2bc Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Mon, 6 Feb 2023 21:47:58 +1100 Subject: [PATCH] Plugins 0.1 - Lang Strings in DB --- back/pialert.py | 86 ++++++--- docker-compose.yml | 2 +- front/php/templates/language/en_us.php | 36 ++-- front/php/templates/language/lang.php | 23 ++- front/plugins/README.md | 2 +- front/plugins/website_monitoring/config.json | 187 +++++++++++-------- front/settings.php | 2 +- 7 files changed, 219 insertions(+), 119 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 4a8d4315..7cb8767f 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -269,7 +269,7 @@ def ccd(key, default, config, name, inputtype, options, group, events=[], desc = def importConfig (): # Specify globals so they can be overwritten with the new config - global lastTimeImported, mySettings + global lastTimeImported, mySettings, plugins # General global ENABLE_ARPSCAN, SCAN_SUBNETS, PRINT_LOG, TIMEZONE, PIALERT_WEB_PROTECTION, PIALERT_WEB_PASSWORD, INCLUDED_SECTIONS, SCAN_CYCLE_MINUTES, DAYS_TO_KEEP_EVENTS, REPORT_DASHBOARD_URL, DIG_GET_IP_ARG, UI_LANG # Email @@ -404,17 +404,24 @@ def importConfig (): #Plugins plugins = get_plugins_configs() - file_print('[', timeNow(), '] Plugins: Number of dynamically loaded plugins: ', len(plugins) ) - for plugin in plugins: - file_print(' ---------------------------------------------') - file_print(' Name : ', plugin.display_name ) - file_print(' Description: ', plugin.description.en_us ) - - prefix = plugin.settings_short_prefix + file_print('[', timeNow(), '] Plugins: Number of dynamically loaded plugins: ', len(plugins.dict) ) - for set in plugin.settings: - codeName = prefix + "_" + set.type - ccd(codeName, set.default_value , c_d, set.name.en_us, get_setting_type(set), str(set.options), prefix) + + for plugin in plugins.list: + file_print(' ---------------------------------------------') + file_print(' Name : ', plugin["display_name"][0]["string"] ) + file_print(' Description: ', plugin["description"][0]["string"] ) + + pref = plugin["settings_short_prefix"] + + collect_lang_strings(plugin, pref) + + + for set in plugin["settings"]: + codeName = pref + "_" + set["type"] + ccd(codeName, set["default_value"] , c_d, set["name"][0]["string"], get_setting_type(set), str(set["options"]), pref) + + collect_lang_strings(set, pref + "_" + set["type"]) # Update scheduler @@ -3075,7 +3082,7 @@ def upgradeDB (): Extra TEXT NOT NULL, PRIMARY KEY("Index" AUTOINCREMENT) ); """ - # sql.execute(sql_Plugins_State) + sql.execute(sql_Plugins_State) # Plugin execution results sql_Plugin_Events = """ CREATE TABLE IF NOT EXISTS Plugins_Events( @@ -3091,7 +3098,18 @@ def upgradeDB (): Processed TEXT NOT NULL, PRIMARY KEY("Index" AUTOINCREMENT) ); """ - # sql.execute(sql_Plugin_Events) + sql.execute(sql_Plugin_Events) + + # Dynamically generated language strings + sql.execute("DROP TABLE Language_Strings;") + sql.execute(""" CREATE TABLE IF NOT EXISTS Language_Strings( + "Index" INTEGER, + Language_Code TEXT NOT NULL, + String_Key TEXT NOT NULL, + String_Value TEXT NOT NULL, + Extra TEXT NOT NULL, + PRIMARY KEY("Index" AUTOINCREMENT) + ); """) commitDB () @@ -3482,25 +3500,49 @@ def isNewVersion(): #------------------------------------------------------------------------------- def get_plugins_configs(): - plugins = [] + pluginsDict = [] + pluginsList = [] for root, dirs, files in os.walk(pluginsPath): for d in dirs: # Loop over directories, not files - - # filelist.append(os.path.join(root, d)) + pluginsDict.append(json.loads(get_file_content(pluginsPath + "/" + d + '/config.json'), object_hook=custom_plugin_decoder)) + pluginsList.append(json.loads(get_file_content(pluginsPath + "/" + d + '/config.json'))) - plugins.append(json.loads(get_file_content(pluginsPath + "/" + d + '/config.json'), object_hook=custom_plugin_decoder)) - return plugins + return plugins_struct(pluginsDict, pluginsList) + +#------------------------------------------------------------------------------- +class plugins_struct: + def __init__(self, dict, list): + self.dict = dict + self.list = list + +#------------------------------------------------------------------------------- +def collect_lang_strings(json, pref): + + for prop in json["localized"]: + for language_string in json[prop]: + import_language_string(language_string["language_code"], pref + "_" + prop, language_string["string"]) + + +#------------------------------------------------------------------------------- +def import_language_string(code, key, value, extra = ""): + + sql.execute ("""INSERT INTO Language_Strings ("Language_Code", "String_Key", "String_Value", "Extra") VALUES (?, ?, ?, ?)""", (str(code), str(key), str(value), str(extra))) + + commitDB () #------------------------------------------------------------------------------- def get_setting_type(setting): - if setting.type in ['RUN']: + + type = setting["type"] + + if type in ['RUN']: return 'selecttext' - if setting.type in ['ENABLE', 'FORCE_REPORT']: + if type in ['ENABLE', 'FORCE_REPORT']: return 'boolean' - if setting.type in ['TIMEOUT', 'RUN_TIMEOUT']: + if type in ['TIMEOUT', 'RUN_TIMEOUT']: return 'integer' - if setting.type in ['NOTIFY_ON']: + if type in ['NOTIFY_ON']: return 'multiselect' return 'text' diff --git a/docker-compose.yml b/docker-compose.yml index 53d149bc..40ee843c 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: volumes: - ${APP_DATA_LOCATION}/pialert/config:/home/pi/pialert/config # - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db - - ${APP_DATA_LOCATION}/pialert/db:/home/pi/pialert/db + - ${APP_DATA_LOCATION}/pialert/db2:/home/pi/pialert/db # (optional) useful for debugging if you have issues setting up the container - ${LOGS_LOCATION}:/home/pi/pialert/front/log # DELETE START anyone trying to use this file: comment out / delete BELOW lines, they are only for development purposes diff --git a/front/php/templates/language/en_us.php b/front/php/templates/language/en_us.php index 562e162e..cf2b4495 100755 --- a/front/php/templates/language/en_us.php +++ b/front/php/templates/language/en_us.php @@ -482,7 +482,8 @@ $lang['en_us'] = array( //General -'General_settings_group' => ' General', +'General_display_name' => 'General', +'General_icon' => '', 'ENABLE_ARPSCAN_name' => 'Enable ARP scan', 'ENABLE_ARPSCAN_description' => 'Arp-scan is a command-line tool that uses the ARP protocol to discover and fingerprint IP hosts on the local network. An alternative to ARP scan is to enable the PIHOLE_ACTIVEPiHole integration settings.', 'SCAN_SUBNETS_name' => 'Subnets to scan', @@ -519,7 +520,8 @@ the arp-scan will take hours to complete instead of seconds. 'UI_LANG_description' => 'Select the preferred UI language.', //Email -'Email_settings_group' => ' Email', +'Email_display_name' => 'Email', +'Email_icon' => '', 'REPORT_MAIL_name' => 'Enable email', 'REPORT_MAIL_description' => 'If enabled an email is sent out with a list of changes you\'ve subscribed to. Please also fill out all remaining settings related to the SMTP setup below.', 'SMTP_SERVER_name' => 'SMTP server URL', @@ -542,7 +544,8 @@ the arp-scan will take hours to complete instead of seconds. 'REPORT_FROM_description' => 'Notification email subject line.', //Webhooks -'Webhooks_settings_group' => ' Webhooks', +'Webhooks_display_name' => 'Webhooks', +'Webhooks_icon' => '', 'REPORT_WEBHOOK_name' => 'Enable Webhooks', 'REPORT_WEBHOOK_description' => 'Enable webhooks for notifications. Webhooks help you to connect to a lot of 3rd party tools, such as IFTTT, Zapier or n8n to name a few. Check out this simple n8n guide here to get started. If enabled, configure related settings below.', 'WEBHOOK_URL_name' => 'Target URL', @@ -553,7 +556,8 @@ the arp-scan will take hours to complete instead of seconds. 'WEBHOOK_REQUEST_METHOD_description' => 'The HTTP request method to be used for the webhook call.', // Apprise -'Apprise_settings_group' => ' Apprise', +'Apprise_display_name' => 'Apprise', +'Apprise_icon' => '', 'REPORT_APPRISE_name' => 'Enable Apprise', 'REPORT_APPRISE_description' => 'Enable sending notifications via Apprise.', 'APPRISE_HOST_name' => 'Apprise host URL', @@ -562,7 +566,8 @@ the arp-scan will take hours to complete instead of seconds. 'APPRISE_URL_description' => 'Apprise notification target URL. For example for Telegram it would be tgram://{bot_token}/{chat_id}.', // NTFY -'NTFY_settings_group' => ' NTFY', +'NTFY_display_name' => 'NTFY', +'NTFY_icon' => '', 'REPORT_NTFY_name' => 'Enable NTFY', 'REPORT_NTFY_description' => 'Enable sending notifications via NTFY.', 'NTFY_HOST_name' => 'NTFY host URL', @@ -575,7 +580,8 @@ the arp-scan will take hours to complete instead of seconds. 'NTFY_PASSWORD_description' => 'Enter password if you need (host) an instance with enabled authetication.', // Pushsafer -'PUSHSAFER_settings_group' => ' Pushsafer', +'PUSHSAFER_display_name' => 'Pushsafer', +'PUSHSAFER_icon' => '', 'REPORT_PUSHSAFER_name' => 'Enable Pushsafer', 'REPORT_PUSHSAFER_description' => 'Enable sending notifications via Pushsafer.', 'PUSHSAFER_TOKEN_name' => 'Pushsafer token', @@ -585,7 +591,8 @@ the arp-scan will take hours to complete instead of seconds. // MQTT -'MQTT_settings_group' => ' MQTT', +'MQTT_display_name' => 'MQTT', +'MQTT_icon' => '', 'REPORT_MQTT_name' => 'Enable MQTT', 'REPORT_MQTT_description' => 'Enable sending notifications via MQTT to your Home Assistance instance.', 'MQTT_BROKER_name' => 'MQTT broker URL', @@ -602,7 +609,8 @@ the arp-scan will take hours to complete instead of seconds. 'MQTT_DELAY_SEC_description' => 'A little hack - delay adding to the queue in case the process is restarted and previous publish processes aborted (it takes ~2s to update a sensor config on the broker). Tested with 2-3 seconds of delay. This delay is only applied when devices are created (during the first notification loop). It doesn\'t affect subsequent scans or notifications.', //DynDNS -'DynDNS_settings_group' => ' DynDNS', +'DynDNS_display_name' => 'DynDNS', +'DynDNS_icon' => '', 'DDNS_ACTIVE_name' => 'Enable DynDNS', 'DDNS_ACTIVE_description' => '', 'DDNS_DOMAIN_name' => 'DynDNS domain URL', @@ -615,14 +623,16 @@ the arp-scan will take hours to complete instead of seconds. 'DDNS_UPDATE_URL_description' => 'Update URL starting with http:// or https://.', // PiHole -'PiHole_settings_group' => ' PiHole', +'PiHole_display_name' => 'PiHole', +'PiHole_icon' => '', 'PIHOLE_ACTIVE_name' => 'Enable PiHole mapping', 'PIHOLE_ACTIVE_description' => 'You need to map:/etc/pihole/pihole-FTL.db in the docker-compose.yml file if you enable this setting.', 'DHCP_ACTIVE_name' => 'Enable PiHole DHCP', 'DHCP_ACTIVE_description' => 'You need to map :/etc/pihole/dhcp.leases in the docker-compose.yml file if you enable this setting.', // Pholus -'Pholus_settings_group' => ' Pholus', +'Pholus_display_name' => 'Pholus', +'Pholus_icon' => '', 'PHOLUS_ACTIVE_name' => 'Cycle run', 'PHOLUS_ACTIVE_description' => 'Pholus is a sniffing tool to discover additional information about the devices on the network, including the device name. If enabled this will execute the scan before every network scan cycle until there are no (unknown) or (name not found) devices. Please be aware it can spam the network with unnecessary traffic. Depends on the SCAN_SUBNETS setting. For a scheduled or one-off scan, check the PHOLUS_RUN setting.', 'PHOLUS_TIMEOUT_name' => 'Cycle run timeout', @@ -640,7 +650,8 @@ the arp-scan will take hours to complete instead of seconds. 'PHOLUS_DAYS_DATA_description' => 'How many days of Pholus scan entries should be kept (globally, not device specific!). The pialert_pholus.log file is not touched. Enter 0 to disable.', // Nmap -'Nmap_settings_group' => ' Nmap', +'Nmap_display_name' => 'Nmap', +'Nmap_icon' => '', 'NMAP_ACTIVE_name' => 'Cycle run', 'NMAP_ACTIVE_description' => 'If enabled this will execute a scan on a newly found device. For a scheduled or one-off scan, check the NMAP_RUN setting.', 'NMAP_TIMEOUT_name' => 'Run timeout', @@ -653,7 +664,8 @@ the arp-scan will take hours to complete instead of seconds. 'NMAP_ARGS_description' => 'Arguments used to run the Nmap scan. Be careful to specify the arguments correctly. For example -p -10000 scans ports from 1 to 10000.', // API -'API_settings_group' => ' API', +'API_display_name' => 'API', +'API_icon' => '', 'ENABLE_API_name' => 'Enable API', 'ENABLE_API_description' => 'If enabled the app will start publishing and updating simple API endpoints under the /home/pi/pialert/front/api/ folder and thus on the pialert_url/api/File_name url.', 'API_RUN_name' => 'Scheduling updates', diff --git a/front/php/templates/language/lang.php b/front/php/templates/language/lang.php index 4b0d404b..4b1b1117 100755 --- a/front/php/templates/language/lang.php +++ b/front/php/templates/language/lang.php @@ -5,6 +5,7 @@ // ################################### $defaultLang = "en_us"; +$allLanguages = ["en_us","es_es","de_de"]; global $db; @@ -17,6 +18,20 @@ switch($result){ if (isset($pia_lang_selected) == FALSE or (strlen($pia_lang_selected) == 0)) {$pia_lang_selected = $defaultLang;} +//Language_Strings ("Language_Code", "String_Key", "String_Value", "Extra") +$result = $db->query("SELECT * FROM Language_Strings"); + +// array +$strings = array(); +while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + // Push row data + $strings[] = array( 'Language_Code' => $row['Language_Code'], + 'String_Key' => $row['String_Key'], + 'String_Value' => $row['String_Value'], + 'Extra' => $row['Extra'] + ); +} + require dirname(__FILE__).'/../skinUI.php'; require dirname(__FILE__).'/en_us.php'; require dirname(__FILE__).'/de_de.php'; @@ -24,7 +39,13 @@ require dirname(__FILE__).'/es_es.php'; function lang($key) { - global $pia_lang_selected, $lang, $defaultLang; + global $pia_lang_selected, $lang, $defaultLang, $strings; + + // get strings from the DB and append them to the ones from the files + foreach ($strings as $string) + { + $lang[$string["Language_Code"]][$string["String_Key"]] = $string["String_Value"]; + } // check if key exists in selected language if(array_key_exists($key, $lang[$pia_lang_selected]) == FALSE) diff --git a/front/plugins/README.md b/front/plugins/README.md index 046e58ad..167200f6 100755 --- a/front/plugins/README.md +++ b/front/plugins/README.md @@ -21,7 +21,7 @@ More on specific files below. Used to interface between PiAlert and the plugin (script). After every scan it should contain only the results from the latest scan/execution. -- The format is a `csv`-like file with the pipe `|` separator. 8 (eight) values need to be supplied, so every line needs to contain 7 pipe separators. Empty values arerepresented by `null` +- The format is a `csv`-like file with the pipe `|` separator. 8 (eight) values need to be supplied, so every line needs to contain 7 pipe separators. Empty values are represented by `null` - Don't render "headers" for these "columns" - Every scan result / event entry needs to be on a new line - You can find which "columns" need to be present in the script results and if the value is required below. diff --git a/front/plugins/website_monitoring/config.json b/front/plugins/website_monitoring/config.json index f0edbd8e..cb79bcf8 100755 --- a/front/plugins/website_monitoring/config.json +++ b/front/plugins/website_monitoring/config.json @@ -1,28 +1,42 @@ { "code_name": "website_monitor", "settings_short_prefix": "WEBMON", - "display_name" : "Website monitor", - "font_awesome_icon_classses": "fa-solid fa-globe", - "description": { - "en_us" : "This plugin is to monitor status changes of different services or websites." - }, + "localized": ["display_name", "description", "icon"], + "display_name" : [{ + "language_code":"en_us", + "string" : "Website monitor" + }], + "icon":[{ + "language_code":"en_us", + "string" : "" + }], + "argument" : "urls", + "description": [{ + "language_code":"en_us", + "string" : "This plugin is to monitor status changes of different services or websites." + }], "database_column_aliases":{ "Plugins_Events":{ - "Index":{ - "en_us" : "Index" - }, - "Object_PrimaryID":{ - "en_us" : "Monitored URL" - }, - "DateTime":{ - "en_us" : "Checked on" - }, - "Watched_Value1":{ - "en_us" : "Status code" - }, - "Watched_Value2":{ - "en_us" : "Latency" - } + "Index":[{ + "language_code":"en_us", + "string" : "Index" + }], + "Object_PrimaryID":[{ + "language_code":"en_us", + "string" : "Monitored URL" + }], + "DateTime":[{ + "language_code":"en_us", + "string" : "Checked on" + }], + "Watched_Value1":[{ + "language_code":"en_us", + "string" : "Status code" + }], + "Watched_Value2":[{ + "language_code":"en_us", + "string" : "Latency" + }] } }, "settings":[ @@ -30,103 +44,114 @@ "type": "ENABLE", "default_value":"False", "options": [], - "name" : { - "en_us" : "Enable plugin" - }, - "description": - { - "en_us" : "Enable a regular scan of your services. You need to enable this setting for anything to be executed regarding this plugin." - } - + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "Enable plugin" + }], + "description": [{ + "language_code":"en_us", + "string" : "Enable a regular scan of your services. You need to enable this setting for anything to be executed regarding this plugin." + }] }, { "type": "RUN", "default_value":"none", "options": ["none","once","schedule"], - "name" : { - "en_us" : "Schedule" - }, - "description": - { - "en_us" : "Enable a regular scan of your services. If you select schedule the scheduling settings from below are applied. If you select once the scan is run only once on start of the application (container) for the time specified in WEBMON_TIMEOUT setting." - } - + "localized": ["name", "description"], + "name" :[{ + "language_code":"en_us", + "string" : "Schedule" + }], + "description": [{ + "language_code":"en_us", + "string" : "Enable a regular scan of your services. If you select schedule the scheduling settings from below are applied. If you select once the scan is run only once on start of the application (container) for the time specified in WEBMON_TIMEOUT setting." + }] }, { "type": "FORCE_REPORT", "default_value": false, "options": [], - "name" : { - "en_us" : "Schedule" - }, - "description": - { - "en_us" : "Enable a regular scan of your services. If you select schedule the scheduling settings from below are applied. If you select once the scan is run only once on start of the application (container) for the time specified in WEBMON_TIMEOUT setting." - } + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "Force report" + }], + "description": [{ + "language_code":"en_us", + "string" : "Force a notification message even if there are nochanges detected." + }] }, { "type": "RUN_SCHD", "default_value":"0 2 * * *", "options": [], - "name" : { - "en_us" : "Schedule" - }, - "description": - { - "en_us" : "Only enabled if you select schedule in the WEBMON_RUN setting. Make sure you enter the schedule in the correct cron-like format (e.g. validate at crontab.guru). For example entering 0 4 * * * will run the scan after 4 am in the TIMEZONE you set above. Will be run NEXT time the time passes." - } - + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "Schedule" + }], + "description": [{ + "language_code":"en_us", + "string" : "Only enabled if you select schedule in the WEBMON_RUN setting. Make sure you enter the schedule in the correct cron-like format (e.g. validate at crontab.guru). For example entering 0 4 * * * will run the scan after 4 am in the TIMEZONE you set above. Will be run NEXT time the time passes." + }] }, { "type": "API_SQL", "default_value":"SELECT * FROM plugin_website_monitor", "options": [], - "name" : { - "en_us" : "API endpoint" - }, - "description": - { - "en_us" : "You can specify a custom SQL query which will generate a JSON file and then expose it via the plugin_website_monitor.json file endpoint." - } - + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "API endpoint" + }], + "description": [{ + "language_code":"en_us", + "string" : "You can specify a custom SQL query which will generate a JSON file and then expose it via the plugin_website_monitor.json file endpoint." + }] }, { "type": "RUN_TIMEOUT", "default_value":5, "options": [], - "name" : { - "en_us" : "Run timeout" - }, - "description": - { - "en_us" : "Maximum time in seconds to wait for a Website monitor check to finish for any url." - } - + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "Run timeout" + }], + "description": [{ + "language_code":"en_us", + "string" : "Maximum time in seconds to wait for a Website monitor check to finish for any url." + }] }, { "type": "NOTIFY_ON", "default_value":["Watched_Value1"], "options": ["Watched_Value1","Watched_Value2","Watched_Value3","Watched_Value4"], - "name" : { - "en_us" : "Notify on" - }, - "description": - { - "en_us" : "Send a notification if selected values change. Use CTRL + Click to select/deselect. " - } + "localized": ["name", "description"], + "name" :[{ + "language_code":"en_us", + "string" : "Notify on" + }] , + "description":[{ + "language_code":"en_us", + "string" : "Send a notification if selected values change. Use CTRL + Click to select/deselect. " + }] }, { "type": "ARGS", "default_value":"", "options": [], - "name" : { - "en_us" : "Run timeout" - }, - "description": - { - "en_us" : "Change the dig utility arguments if you have issues resolving your Internet IP. Arguments are added at the end of the following command: dig +short ." - } + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "Arguments" + }], + "description": [{ + "language_code":"en_us", + "string" : "Change the dig utility arguments if you have issues resolving your Internet IP. Arguments are added at the end of the following command: dig +short ." + }] } ] diff --git a/front/settings.php b/front/settings.php index 88f0ddad..a0ed10e0 100755 --- a/front/settings.php +++ b/front/settings.php @@ -72,7 +72,7 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { $html = $html.'
-

'.lang($group.'_settings_group').'

+

'.lang($group.'_icon')." ".lang($group.'_display_name').'