mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-15 22:51:37 -07:00
CSV Export - encode quotes #808
This commit is contained in:
@@ -1288,7 +1288,7 @@ function getDeviceData (readAllData=false) {
|
|||||||
if (deviceData['dev_Favorite'] == 1) {$('#chkFavorite').iCheck('check');} else {$('#chkFavorite').iCheck('uncheck');}
|
if (deviceData['dev_Favorite'] == 1) {$('#chkFavorite').iCheck('check');} else {$('#chkFavorite').iCheck('uncheck');}
|
||||||
$('#txtGroup').val (deviceData['dev_Group']);
|
$('#txtGroup').val (deviceData['dev_Group']);
|
||||||
$('#txtLocation').val (deviceData['dev_Location']);
|
$('#txtLocation').val (deviceData['dev_Location']);
|
||||||
$('#txtComments').val (deviceData['dev_Comments']);
|
$('#txtComments').val (decodeSpecialChars(deviceData['dev_Comments']));
|
||||||
$('#txtNetworkNodeMac').val ( networkParentMacName) ;
|
$('#txtNetworkNodeMac').val ( networkParentMacName) ;
|
||||||
$('#txtNetworkNodeMac').attr ('data-mynodemac', deviceData['dev_Network_Node_MAC_ADDR']);
|
$('#txtNetworkNodeMac').attr ('data-mynodemac', deviceData['dev_Network_Node_MAC_ADDR']);
|
||||||
$('#txtNetworkPort').val (deviceData['dev_Network_Node_port']);
|
$('#txtNetworkPort').val (deviceData['dev_Network_Node_port']);
|
||||||
@@ -1429,7 +1429,7 @@ function setDeviceData (direction='', refreshCallback='') {
|
|||||||
+ '&favorite=' + ($('#chkFavorite')[0].checked * 1)
|
+ '&favorite=' + ($('#chkFavorite')[0].checked * 1)
|
||||||
+ '&group=' + encodeURIComponent($('#txtGroup').val())
|
+ '&group=' + encodeURIComponent($('#txtGroup').val())
|
||||||
+ '&location=' + encodeURIComponent($('#txtLocation').val())
|
+ '&location=' + encodeURIComponent($('#txtLocation').val())
|
||||||
+ '&comments=' + encodeURIComponent($('#txtComments').val())
|
+ '&comments=' + encodeURIComponent(encodeSpecialChars($('#txtComments').val()))
|
||||||
+ '&networknode=' + $('#txtNetworkNodeMac').attr('data-mynodemac')
|
+ '&networknode=' + $('#txtNetworkNodeMac').attr('data-mynodemac')
|
||||||
+ '&networknodeport=' + $('#txtNetworkPort').val()
|
+ '&networknodeport=' + $('#txtNetworkPort').val()
|
||||||
+ '&ssid=' + $('#txtSSID').val()
|
+ '&ssid=' + $('#txtSSID').val()
|
||||||
|
|||||||
@@ -383,6 +383,26 @@ function isValidJSON(jsonString) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// method to sanitize input so that HTML and other things don't break
|
||||||
|
function encodeSpecialChars(str) {
|
||||||
|
return str
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeSpecialChars(str) {
|
||||||
|
return str
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, '\'');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// General utilities
|
// General utilities
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -428,41 +428,39 @@ function ExportCSV() {
|
|||||||
$func_result = $db->query("SELECT * FROM Devices");
|
$func_result = $db->query("SELECT * FROM Devices");
|
||||||
|
|
||||||
// prepare CSV header row
|
// prepare CSV header row
|
||||||
// header array with column names
|
|
||||||
$columns = getDevicesColumns();
|
$columns = getDevicesColumns();
|
||||||
|
|
||||||
// wrap the headers with " (quotes)
|
// wrap the headers with " (quotes)
|
||||||
$resultCSV = '"'.implode('","', $columns).'"';
|
$resultCSV = '"'.implode('","', $columns).'"'."\n";
|
||||||
|
|
||||||
//and append a new line
|
|
||||||
$resultCSV = $resultCSV."\n";
|
|
||||||
|
|
||||||
// retrieve the devices from the DB
|
// retrieve the devices from the DB
|
||||||
while ($row = $func_result -> fetchArray (SQLITE3_ASSOC)) {
|
while ($row = $func_result->fetchArray(SQLITE3_ASSOC)) {
|
||||||
|
|
||||||
// loop through columns and add values to the string
|
// loop through columns and add values to the string
|
||||||
$index = 0;
|
$index = 0;
|
||||||
foreach ($columns as $columnName) {
|
foreach ($columns as $columnName) {
|
||||||
|
// Escape special chars (e.g.quotes) inside fields by replacing them with html definitions
|
||||||
|
$fieldValue = encodeSpecialChars($row[$columnName]);
|
||||||
|
|
||||||
// add quotes around the value to prevent issues with commas in fields
|
// add quotes around the value to prevent issues with commas in fields
|
||||||
$resultCSV = $resultCSV.'"'.$row[$columnName].'"';
|
$resultCSV .= '"'.$fieldValue.'"';
|
||||||
|
|
||||||
// detect last loop - skip as no comma needed
|
// detect last loop - skip as no comma needed
|
||||||
if ($index != count($columns) - 1 )
|
if ($index != count($columns) - 1) {
|
||||||
{
|
$resultCSV .= ',';
|
||||||
$resultCSV = $resultCSV.',';
|
|
||||||
}
|
}
|
||||||
$index++;
|
$index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//$resultCSV = $resultCSV.implode(",", [$row["dev_MAC"], $row["dev_Name"]]);
|
// add a new line for the next row
|
||||||
$resultCSV = $resultCSV."\n";
|
$resultCSV .= "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
//write the built CSV string
|
//write the built CSV string
|
||||||
echo $resultCSV;
|
echo $resultCSV;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Import CSV of devices
|
// Import CSV of devices
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -524,6 +524,25 @@ function handleNull ($text, $default = "") {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
// Encode special chars
|
||||||
|
function encodeSpecialChars($str) {
|
||||||
|
return str_replace(
|
||||||
|
['&', '<', '>', '"', "'"],
|
||||||
|
['&', '<', '>', '"', '''],
|
||||||
|
$str
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
// Decode special chars
|
||||||
|
function decodeSpecialChars($str) {
|
||||||
|
return str_replace(
|
||||||
|
['&', '<', '>', '"', '''],
|
||||||
|
['&', '<', '>', '"', "'"],
|
||||||
|
$str
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user