csv export/import

This commit is contained in:
jokob-sk
2022-08-05 22:56:48 +10:00
parent 8650c68801
commit 5a1c1036dd
6 changed files with 207 additions and 4 deletions

View File

@@ -197,3 +197,10 @@ function debugTimer () {
}
// -----------------------------------------------------------------------------
function openInNewTab (url) {
window.open(url, "_blank");
}

View File

@@ -344,6 +344,18 @@ if (submit && isset($_POST['langselector_set'])) {
</div>
<div class="db_tools_table_cell_b"><?php echo $pia_lang['Maintenance_Tool_purgebackup_text'];?></div>
</div>
<div class="db_info_table_row">
<div class="db_tools_table_cell_a" style="">
<button type="button" class="btn btn-default pa-btn bg-green dbtools-button" id="btnExportCSV" onclick="askExportCSV()"><?php echo $pia_lang['Maintenance_Tool_ExportCSV'];?></button>
</div>
<div class="db_tools_table_cell_b"><?php echo $pia_lang['Maintenance_Tool_ExportCSV_text'];?></div>
</div>
<div class="db_info_table_row">
<div class="db_tools_table_cell_a" style="">
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnImportCSV" onclick="askImportCSV()"><?php echo $pia_lang['Maintenance_Tool_ImportCSV'];?></button>
</div>
<div class="db_tools_table_cell_b"><?php echo $pia_lang['Maintenance_Tool_ImportCSV_text'];?></div>
</div>
</div>
</div>
</div>
@@ -491,6 +503,32 @@ function PiaPurgeDBBackups()
});
}
// Export CSV
function askExportCSV() {
// Ask
showModalWarning('<?php echo $pia_lang['Maintenance_Tool_ExportCSV_noti'];?>', '<?php echo $pia_lang['Maintenance_Tool_ExportCSV_noti_text'];?>',
'<?php echo $pia_lang['Gen_Cancel'];?>', '<?php echo $pia_lang['Gen_Okay'];?>', 'ExportCSV');
}
function ExportCSV()
{
// Execute
openInNewTab(window.location.origin + "/php/server/devices.php?action=ExportCSV")
}
// Import CSV
function askImportCSV() {
// Ask
showModalWarning('<?php echo $pia_lang['Maintenance_Tool_ImportCSV_noti'];?>', '<?php echo $pia_lang['Maintenance_Tool_ImportCSV_noti_text'];?>',
'<?php echo $pia_lang['Gen_Cancel'];?>', '<?php echo $pia_lang['Gen_Okay'];?>', 'ImportCSV');
}
function ImportCSV()
{
// Execute
$.get('/php/server/devices.php?action=ImportCSV', function(msg) {
showMessage (msg);
});
}
// Switch Darkmode
function askPiaEnableDarkmode() {
// Ask

View File

@@ -59,6 +59,8 @@ if (strlen($pia_lang_selected) == 0) {$pia_lang_selected = 'en_us';}
case 'PiaPurgeDBBackups': PiaPurgeDBBackups(); break;
case 'PiaEnableDarkmode': PiaEnableDarkmode(); break;
case 'PiaToggleArpScan': PiaToggleArpScan(); break;
case 'ExportCSV': ExportCSV(); break;
case 'ImportCSV': ImportCSV(); break;
case 'getDevicesTotals': getDevicesTotals(); break;
case 'getDevicesList': getDevicesList(); break;
@@ -437,6 +439,120 @@ function PiaPurgeDBBackups() {
}
//------------------------------------------------------------------------------
// Export CSV of devices
//------------------------------------------------------------------------------
function ExportCSV() {
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"devices.csv\"");
global $db;
$func_result = $db->query("SELECT * FROM Devices");
// prepare CSV header row
// header array with column names
$columns = getDevicesColumns();
// wrap the headers with " (quotes)
$resultCSV = '"'.implode('","', $columns).'"';
//and append a new line
$resultCSV = $resultCSV."\n";
// retrieve the devices from the DB
while ($row = $func_result -> fetchArray (SQLITE3_ASSOC)) {
// loop through columns and add values to the string
$index = 0;
foreach ($columns as $columnName) {
// add quotes around the value to prevent issues with commas in fields
$resultCSV = $resultCSV.'"'.$row[$columnName].'"';
// detect last loop - skip as no comma needed
if ($index != count($columns) - 1 )
{
$resultCSV = $resultCSV.',';
}
$index++;
}
//$resultCSV = $resultCSV.implode(",", [$row["dev_MAC"], $row["dev_Name"]]);
$resultCSV = $resultCSV."\n";
}
//write the built CSV string
echo $resultCSV;
}
//------------------------------------------------------------------------------
// Import CSV of devices
//------------------------------------------------------------------------------
function ImportCSV() {
$file = '../../../config/devices.csv';
if (file_exists($file)) {
global $db;
global $pia_lang;
$error = "";
// sql
$sql = 'DELETE FROM Devices';
// execute sql
$result = $db->query($sql);
// Open the CSV file with read-only mode
$csvFile = fopen($file, 'r');
// Skip the first line
fgetcsv($csvFile);
$columns = getDevicesColumns();
// Parse data from CSV file line by line (max 10000 lines)
while (($row = fgetcsv($csvFile, 10000, ",")) !== FALSE)
{
$sql = 'INSERT INTO Devices ('.implode(',', $columns).') VALUES ("' .implode('","', $row).'")';
$result = $db->query($sql);
// check result
if ($result != TRUE) {
$error = $db->lastErrorMsg();
// break the while loop on error
break;
}
}
// Close opened CSV file
fclose($csvFile);
if($error == "")
{
// import succesful
echo $pia_lang['BackDevices_DBTools_ImportCSV'];
}
else{
// an error occurred while writing to the DB, display the last error message
echo $pia_lang['BackDevices_DBTools_ImportCSVError']."\n\n$sql \n\n".$error;
}
} else {
echo $pia_lang['BackDevices_DBTools_ImportCSVMissing'];
}
$db->close();
}
//------------------------------------------------------------------------------
// Toggle Dark/Light Themes
//------------------------------------------------------------------------------

View File

@@ -74,6 +74,36 @@ function getNetworkTypes(){
return $array;
}
function getDevicesColumns(){
$columns = ["dev_MAC",
"dev_Name",
"dev_Owner",
"dev_DeviceType",
"dev_Vendor",
"dev_Favorite",
"dev_Group",
"dev_Comments",
"dev_FirstConnection",
"dev_LastConnection",
"dev_LastIP",
"dev_StaticIP",
"dev_ScanCycle",
"dev_LogEvents",
"dev_AlertEvents",
"dev_AlertDeviceDown",
"dev_SkipRepeated",
"dev_LastNotification",
"dev_PresentLastScan",
"dev_NewDevice",
"dev_Location",
"dev_Archived",
"dev_Network_Node_port",
"dev_Network_Node_MAC_ADDR"];
return $columns;
}
//------------------------------------------------------------------------------
// Simple cookie cache
//------------------------------------------------------------------------------

View File

@@ -235,11 +235,11 @@ $pia_lang['Maintenance_Tool_del_allevents30_noti_text'] = 'Are you sure you want
$pia_lang['Maintenance_Tool_backup'] = 'DB Backup';
$pia_lang['Maintenance_Tool_backup_text'] = 'The database backups are located in the database directory as a zip-archive, named with the creation date. There is no maximum number of backups.';
$pia_lang['Maintenance_Tool_backup_noti'] = 'DB Backup';
$pia_lang['Maintenance_Tool_backup_noti_text'] = 'Are you sure you want to exectute the the DB Backup? Be sure that no scan is currently running.';
$pia_lang['Maintenance_Tool_backup_noti_text'] = 'Are you sure you want to execute the the DB Backup? Be sure that no scan is currently running.';
$pia_lang['Maintenance_Tool_restore'] = 'DB Restore';
$pia_lang['Maintenance_Tool_restore_text'] = 'The latest backup can be restored via the button, but older backups can only be restored manually. After the restore, make an integrity check on the database for safety, in case the db was currently in write access when the backup was created.';
$pia_lang['Maintenance_Tool_restore_noti'] = 'DB Restore';
$pia_lang['Maintenance_Tool_restore_noti_text'] = 'Are you sure you want to exectute the the DB Restore? Be sure that no scan is currently running.';
$pia_lang['Maintenance_Tool_restore_noti_text'] = 'Are you sure you want to execute the the DB Restore? Be sure that no scan is currently running.';
$pia_lang['Maintenance_Tool_purgebackup'] = 'Purge Backups';
$pia_lang['Maintenance_Tool_purgebackup_text'] = 'All other backups will be deleted except for the last 3 backups.';
$pia_lang['Maintenance_Tool_purgebackup_noti'] = 'Purge Backups';
@@ -248,6 +248,14 @@ $pia_lang['Maintenance_Tool_del_ActHistory'] = 'Deleting the network activity';
$pia_lang['Maintenance_Tool_del_ActHistory_text'] = 'The network activity graph is reset. This does not affect the events.';
$pia_lang['Maintenance_Tool_del_ActHistory_noti'] = 'Delete network activity';
$pia_lang['Maintenance_Tool_del_ActHistory_noti_text'] = 'Are you sure you want to reset the network activity?';
$pia_lang['Maintenance_Tool_ExportCSV'] = 'CSV Export';
$pia_lang['Maintenance_Tool_ExportCSV_text'] = 'Generate a CSV (comma separated value) file containing the list of Devices including the Network relationships between Network Nodes and connected devices.';
$pia_lang['Maintenance_Tool_ExportCSV_noti'] = 'CSV Export';
$pia_lang['Maintenance_Tool_ExportCSV_noti_text'] = 'Are you sure you want to generate a CSV file?';
$pia_lang['Maintenance_Tool_ImportCSV'] = 'CSV Import';
$pia_lang['Maintenance_Tool_ImportCSV_text'] = 'Before using this function, please make a backup. Import a CSV (comma separated value) file containing the list of Devices including the Network relationships between Network Nodes and connected devices. To do that place the CSV file named <b>devices.csv</b> into your <b>/config</b> folder.';
$pia_lang['Maintenance_Tool_ImportCSV_noti'] = 'CSV Import';
$pia_lang['Maintenance_Tool_ImportCSV_noti_text'] = 'Are you sure you want to import the CSV file? This will completely overwrite the devices in your database.';
//////////////////////////////////////////////////////////////////
// Maintenance Page
@@ -274,6 +282,10 @@ $pia_lang['BackDevices_DBTools_UpdDevError'] = 'Error updating device';
$pia_lang['BackDevices_DBTools_Upgrade'] = 'Database upgraded successfully';
$pia_lang['BackDevices_DBTools_UpgradeError'] = 'Database upgrade failed';
$pia_lang['BackDevices_DBTools_Purge'] = 'The oldest backups were deleted';
$pia_lang['BackDevices_DBTools_ImportCSV'] = 'The devices from the CSV file were imported successfully.';
$pia_lang['BackDevices_DBTools_ImportCSVError'] = 'The CSV file couldn\'t be imported. Make sure the format is correct.';
$pia_lang['BackDevices_DBTools_ImportCSVMissing'] = 'The CSV file couldn\'t be found under <b>/config/devices.csv.</b>';
//////////////////////////////////////////////////////////////////
// Network Page