⬇CSV Import work #808
Some checks are pending
docker / docker_dev (push) Waiting to run

This commit is contained in:
jokob-sk
2024-09-30 10:30:09 +10:00
parent e5d835cfa9
commit 044de61ab5
6 changed files with 45 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
## Development environemnt set up ## Development environment set up
>[!NOTE] >[!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. > 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` - `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` - `sudo docker exec -it netalertx /bin/bash`
- `pkill -f "python /app/server" && python /app/server & ` - `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.

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@@ -342,6 +342,8 @@ function getLangCode() {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// String utilities // String utilities
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ----------------------------------------------------
function jsonSyntaxHighlight(json) { function jsonSyntaxHighlight(json) {
if (typeof json != 'string') { if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2); json = JSON.stringify(json, undefined, 2);
@@ -364,6 +366,7 @@ function jsonSyntaxHighlight(json) {
}); });
} }
// ----------------------------------------------------
function isValidBase64(str) { function isValidBase64(str) {
// Base64 characters set // Base64 characters set
var base64CharacterSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var base64CharacterSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
@@ -373,7 +376,7 @@ function isValidBase64(str) {
return invalidCharacters === ''; return invalidCharacters === '';
} }
// ----------------------------------------------------
function isValidJSON(jsonString) { function isValidJSON(jsonString) {
try { try {
JSON.parse(jsonString); JSON.parse(jsonString);
@@ -383,6 +386,7 @@ function isValidJSON(jsonString) {
} }
} }
// ----------------------------------------------------
// method to sanitize input so that HTML and other things don't break // method to sanitize input so that HTML and other things don't break
function encodeSpecialChars(str) { function encodeSpecialChars(str) {
return str return str
@@ -392,7 +396,7 @@ function encodeSpecialChars(str) {
.replace(/"/g, '"') .replace(/"/g, '"')
.replace(/'/g, '''); .replace(/'/g, ''');
} }
// ----------------------------------------------------
function decodeSpecialChars(str) { function decodeSpecialChars(str) {
return str return str
.replace(/&/g, '&') .replace(/&/g, '&')
@@ -402,6 +406,16 @@ function decodeSpecialChars(str) {
.replace(/'/g, '\''); .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 // General utilities

View File

@@ -473,12 +473,15 @@ function askImportPastedCSV() {
function ImportPastedCSV() function ImportPastedCSV()
{ {
var csv = $('#modal-input-textarea').val(); var csv = $('#modal-input-textarea').val();
csvBase64 = btoa(csv)
// Execute csvBase64 = utf8ToBase64(csv);
$.post('php/server/devices.php?action=ImportCSV', { content: csvBase64 }, function(msg) { $.post('php/server/devices.php?action=ImportCSV', { content: csvBase64 }, function(msg) {
showMessage(msg); showMessage(msg);
write_notification(`[Maintenance] Devices imported from pasted content`, 'info'); write_notification(`[Maintenance] Devices imported from pasted content`, 'info');
}); });
} }

View File

@@ -476,7 +476,11 @@ function ImportCSV() {
if(isset ($_POST['content']) && !empty ($_POST['content'])) if(isset ($_POST['content']) && !empty ($_POST['content']))
{ {
// Decode the Base64 string // 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 } else if (file_exists($file)) { // try to get the data form the file
@@ -488,6 +492,12 @@ function ImportCSV() {
if($data != "") 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); $lines = explode("\n", $data);
// Get the column headers from the first line of the CSV // Get the column headers from the first line of the CSV

View File

@@ -25,7 +25,7 @@ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https:
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$isLogonPage = strpos($url, 'index.php') !== false; $isLogonPage = strpos($url, 'index.php') !== false;
$authHeader = apache_request_headers()['Authorization'] ?? ''; $authHeader = apache_request_headers()['Authorization'] ?? '';
$sessionLogin = $_SESSION['login'] ?? 0; $sessionLogin = isset($_SESSION['login']) ? $_SESSION['login'] : 0;
// Start session if not already started // Start session if not already started
if (session_status() == PHP_SESSION_NONE) { if (session_status() == PHP_SESSION_NONE) {