diff --git a/docs/DEV_ENV_SETUP.md b/docs/DEV_ENV_SETUP.md index ec15a037..abbd9c55 100755 --- a/docs/DEV_ENV_SETUP.md +++ b/docs/DEV_ENV_SETUP.md @@ -1,4 +1,4 @@ -## Development environemnt set up +## Development environment set up >[!NOTE] > Replace `/development` with the path where your code files will be stored. The default container name is `netalertx` so there might be a conflict with your running containers. @@ -52,13 +52,19 @@ A command to stop, remove the container and the image (replace `netalertx` and ` - `sudo docker container stop netalertx ; sudo docker container rm netalertx ; sudo docker image rm netalertx-netalertx` -### Restart hanging python script +### Restart the server backend -SSH into the container and kill & restart the main script loop +Most code changes can be tetsed without rebuilding the container. When working on the python server backend, you only need to restart the server. + +1. You can usually restart the backend via Maintenance > Logs > Restart server + +![image](/docs/img/DEV_ENV_SETUP/Maintenance_Logs_Restart_server.png) + +2. If above doesn't work, SSH into the container and kill & restart the main script loop - `sudo docker exec -it netalertx /bin/bash` - `pkill -f "python /app/server" && python /app/server & ` - +3. If none of the above work, restart the docker image. This is usually the last resort as sometimes the Docker engine becomes unresponsive and the whole engine needs to be restarted. diff --git a/docs/img/DEV_ENV_SETUP/Maintenance_Logs_Restart_server.png b/docs/img/DEV_ENV_SETUP/Maintenance_Logs_Restart_server.png new file mode 100755 index 00000000..f58e2f5d Binary files /dev/null and b/docs/img/DEV_ENV_SETUP/Maintenance_Logs_Restart_server.png differ diff --git a/front/js/common.js b/front/js/common.js index 721c974a..16e4db6d 100755 --- a/front/js/common.js +++ b/front/js/common.js @@ -342,6 +342,8 @@ function getLangCode() { // ----------------------------------------------------------------------------- // String utilities // ----------------------------------------------------------------------------- + +// ---------------------------------------------------- function jsonSyntaxHighlight(json) { if (typeof json != 'string') { json = JSON.stringify(json, undefined, 2); @@ -364,6 +366,7 @@ function jsonSyntaxHighlight(json) { }); } +// ---------------------------------------------------- function isValidBase64(str) { // Base64 characters set var base64CharacterSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; @@ -373,7 +376,7 @@ function isValidBase64(str) { return invalidCharacters === ''; } - +// ---------------------------------------------------- function isValidJSON(jsonString) { try { JSON.parse(jsonString); @@ -383,6 +386,7 @@ function isValidJSON(jsonString) { } } +// ---------------------------------------------------- // method to sanitize input so that HTML and other things don't break function encodeSpecialChars(str) { return str @@ -392,7 +396,7 @@ function encodeSpecialChars(str) { .replace(/"/g, '"') .replace(/'/g, '''); } - +// ---------------------------------------------------- function decodeSpecialChars(str) { return str .replace(/&/g, '&') @@ -402,6 +406,16 @@ function decodeSpecialChars(str) { .replace(/'/g, '\''); } +// ---------------------------------------------------- +// base64 conversion of UTF8 chars +function utf8ToBase64(str) { + // Convert the string to a Uint8Array using TextEncoder + const utf8Bytes = new TextEncoder().encode(str); + + // Convert the Uint8Array to a base64-encoded string + return btoa(String.fromCharCode(...utf8Bytes)); +} + // ----------------------------------------------------------------------------- // General utilities diff --git a/front/maintenance.php b/front/maintenance.php index d8c4f394..1617dd5a 100755 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -473,12 +473,15 @@ function askImportPastedCSV() { function ImportPastedCSV() { var csv = $('#modal-input-textarea').val(); - csvBase64 = btoa(csv) - // Execute + + csvBase64 = utf8ToBase64(csv); + $.post('php/server/devices.php?action=ImportCSV', { content: csvBase64 }, function(msg) { - showMessage(msg); - write_notification(`[Maintenance] Devices imported from pasted content`, 'info'); + showMessage(msg); + write_notification(`[Maintenance] Devices imported from pasted content`, 'info'); }); + + } diff --git a/front/php/server/devices.php b/front/php/server/devices.php index 041ee328..cc71159d 100755 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -476,7 +476,11 @@ function ImportCSV() { if(isset ($_POST['content']) && !empty ($_POST['content'])) { // Decode the Base64 string - $data = base64_decode($_POST['content']); + // $data = base64_decode($_POST['content']); + $data = base64_decode($_POST['content'], true); // The second parameter ensures safe decoding + + // // Ensure the decoded data is treated as UTF-8 text + // $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8'); } else if (file_exists($file)) { // try to get the data form the file @@ -488,6 +492,12 @@ function ImportCSV() { if($data != "") { + // data cleanup - new lines breaking the CSV + $data = preg_replace_callback('/"([^"]*)"/', function($matches) { + // Replace all \n within the quotes with a space + return str_replace("\n", " ", $matches[0]); // Replace with a space + }, $data); + $lines = explode("\n", $data); // Get the column headers from the first line of the CSV diff --git a/front/php/templates/security.php b/front/php/templates/security.php index 1bd17ea1..5b780507 100755 --- a/front/php/templates/security.php +++ b/front/php/templates/security.php @@ -25,7 +25,7 @@ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https: $url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $isLogonPage = strpos($url, 'index.php') !== false; $authHeader = apache_request_headers()['Authorization'] ?? ''; -$sessionLogin = $_SESSION['login'] ?? 0; +$sessionLogin = isset($_SESSION['login']) ? $_SESSION['login'] : 0; // Start session if not already started if (session_status() == PHP_SESSION_NONE) {