diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 3cfd073c..22f70b83 100644 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -197,3 +197,10 @@ function debugTimer () { } +// ----------------------------------------------------------------------------- +function openInNewTab (url) { + window.open(url, "_blank"); +} + + + diff --git a/front/maintenance.php b/front/maintenance.php index 33199909..b188d509 100644 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -344,6 +344,18 @@ if (submit && isset($_POST['langselector_set'])) {
+
+
+ +
+
+
+
+
+ +
+
+
@@ -491,6 +503,32 @@ function PiaPurgeDBBackups() }); } +// Export CSV +function askExportCSV() { + // Ask + showModalWarning('', '', + '', '', 'ExportCSV'); +} +function ExportCSV() +{ + // Execute + openInNewTab(window.location.origin + "/php/server/devices.php?action=ExportCSV") +} + +// Import CSV +function askImportCSV() { + // Ask + showModalWarning('', '', + '', '', 'ImportCSV'); +} +function ImportCSV() +{ + // Execute + $.get('/php/server/devices.php?action=ImportCSV', function(msg) { + showMessage (msg); + }); +} + // Switch Darkmode function askPiaEnableDarkmode() { // Ask diff --git a/front/network.php b/front/network.php index ba33684d..6a41678b 100644 --- a/front/network.php +++ b/front/network.php @@ -151,7 +151,7 @@ FROM Devices WHERE dev_Network_Node_MAC_ADDR = "'.$node_mac.'" order by port asc'; global $db; - $func_result = $db->query($func_sql); + $func_result = $db->query($func_sql); // array $tableData = array(); diff --git a/front/php/server/devices.php b/front/php/server/devices.php index 4a790e82..6c7fb92b 100644 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -58,7 +58,9 @@ if (strlen($pia_lang_selected) == 0) {$pia_lang_selected = 'en_us';} case 'PiaRestoreDBfromArchive': PiaRestoreDBfromArchive(); break; case 'PiaPurgeDBBackups': PiaPurgeDBBackups(); break; case 'PiaEnableDarkmode': PiaEnableDarkmode(); break; - case 'PiaToggleArpScan': PiaToggleArpScan(); 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 //------------------------------------------------------------------------------ diff --git a/front/php/server/util.php b/front/php/server/util.php index 925f86e4..58e75f4f 100644 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -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 //------------------------------------------------------------------------------ diff --git a/front/php/templates/language/en_us.php b/front/php/templates/language/en_us.php index a37f83c5..a1f5e603 100644 --- a/front/php/templates/language/en_us.php +++ b/front/php/templates/language/en_us.php @@ -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 devices.csv into your /config 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 /config/devices.csv.'; + ////////////////////////////////////////////////////////////////// // Network Page