From c20b239351fe2bb29a886b67c93deee427909144 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Fri, 6 Jan 2023 21:16:01 +1100 Subject: [PATCH] saving port info + links --- back/pialert.py | 6 +-- front/deviceDetails.php | 94 ++++++++++++++---------------------- front/js/pialert_common.js | 38 ++++++++------- front/php/server/devices.php | 63 +++++++++++++++--------- front/php/server/util.php | 39 ++++++--------- 5 files changed, 113 insertions(+), 127 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 17815697..71a79ec2 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -695,11 +695,11 @@ def get_previous_internet_IP (): sql.execute ("SELECT dev_LastIP FROM Devices WHERE dev_MAC = 'Internet' ") result = sql.fetchone() - if len(result) > 0 and type(result) is not None: - previous_IP = result[0] - commitDB() + if result is not None and len(result) > 0 : + previous_IP = result[0] + # return previous IP return previous_IP diff --git a/front/deviceDetails.php b/front/deviceDetails.php index 103b9409..8fd36164 100755 --- a/front/deviceDetails.php +++ b/front/deviceDetails.php @@ -149,7 +149,10 @@
- +
+ + +
@@ -1388,37 +1391,10 @@ function recordSwitch(direction) { { performSwitch(direction) } - - // // update the global position in the devices list variable 'pos' - // if(direction == "next") - // { - // // Next Record - // if (pos < (devicesList.length-1) ) { - // pos++; - // } - // }else if (direction == "prev") - // { - // if (pos > 0) { - // pos--; - // } - // } - - // // get new mac from the devicesList. Don't change to the commented out line below, the mac query string in the URL isn't updated yet! - // // mac = params.mac; - // mac = devicesList[pos].mac.toString(); - - // // Save Changes - // // if ( ! document.getElementById('btnSave').hasAttribute('disabled') ) { - // // setDeviceData (direction, recordSwitch); - // // } - - // getDeviceData (true); - - // // reload current tab - // reloadTab() - } +// ----------------------------------------------------------------------------- + function performSwitch(direction) { somethingChanged = false; @@ -1437,17 +1413,12 @@ function performSwitch(direction) } } - console.log('here ' + pos) + // console.log('here ' + pos) // get new mac from the devicesList. Don't change to the commented out line below, the mac query string in the URL isn't updated yet! // mac = params.mac; mac = devicesList[pos].mac.toString(); - - // Save Changes - // if ( ! document.getElementById('btnSave').hasAttribute('disabled') ) { - // setDeviceData (direction, recordSwitch); - // } - + getDeviceData (true); // reload current tab @@ -1681,8 +1652,8 @@ function loadNmap() listData.forEach(function (item, index) { tableRows += '' +item.Index+'' - +item.Time+'' - +item.Port+'' + +item.Time+'' + +item.Port+'' +item.State+'' +item.Service+'' +'
\ @@ -1790,6 +1761,7 @@ function loadPholus() // }); // } +//----------------------------------------------------------------------------------- function initTable(tableId, mac){ @@ -1821,30 +1793,32 @@ function initTable(tableId, mac){ // Processing 'processing' : true, 'language' : { - processing: ''+ - '
Loading...'+ - '
', - emptyTable: 'No data', - "lengthMenu": "", - "search": ": ", - "paginate": { - "next": "", - "previous": "" - }, - "info": "", - } -}); + processing: ''+ + '
Loading...'+ + '
', + emptyTable: 'No data', + "lengthMenu": "", + "search": ": ", + "paginate": { + "next": "", + "previous": "" + }, + "info": "", + } + }); -$("#"+tableId).attr("data-mac", mac) + $("#"+tableId).attr("data-mac", mac) -// Save Parameters rows & order when changed -$('#'+tableId).on( 'length.dt', function ( e, settings, len ) { - setParameter (parSessionsRows, len); + // Save Parameters rows & order when changed + $('#'+tableId).on( 'length.dt', function ( e, settings, len ) { + setParameter (parSessionsRows, len); -} ); + } ); } +//----------------------------------------------------------------------------------- + window.onload = function async() { initializeTabsNew(); @@ -1852,6 +1826,8 @@ window.onload = function async() reloadTab(); } +//----------------------------------------------------------------------------------- + function reloadTab() { // tab loaded without switching @@ -1866,9 +1842,11 @@ function reloadTab() } } +//----------------------------------------------------------------------------------- + function saveNmapPort(index) { - saveData('saveNmapPort',index , $('#port_'+index).val()) + saveData('saveNmapPort', index, $('#port_'+index).val()) } diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 3c97787d..ecd459a7 100755 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -171,7 +171,7 @@ function modalWarningOK () { } // ----------------------------------------------------------------------------- - +// remove unnecessary lines from the result function sanitize(data) { return data.replace(/(\r\n|\n|\r)/gm,"").replace(/[^\x00-\x7F]/g, "") @@ -205,12 +205,12 @@ function setParameter (parameter, value) { sleep (200); $.get('php/server/parameters.php?action=set¶meter=' + parameter + '&value='+ value, - function(data) { - if (data != "OK") { - // alert (data); - } else { - // alert ("OK. Second attempt"); - }; + function(data) { + if (data != "OK") { + // alert (data); + } else { + // alert ("OK. Second attempt"); + }; } ); }; } ); @@ -219,18 +219,22 @@ function setParameter (parameter, value) { // ----------------------------------------------------------------------------- function saveData(functionName, index, value) { - - console.log(functionName + ' ' + index +' ' + value) - $.ajax({ - method: "POST", - url: "/home/pi/pialert/front/php/server/util.php", - data: { function: functionName, index: index, value:value }, + method: "GET", + url: "php/server/devices.php", + data: { action: functionName, index: index, value:value }, success: function(data) { - // console.log(data); - showModalOk ('Result', data ); - // Remove navigation prompt "Are you sure you want to leave..." - window.onbeforeunload = null; + + if(sanitize(data) == 'OK') + { + showMessage("Saved") + // Remove navigation prompt "Are you sure you want to leave..." + window.onbeforeunload = null; + } else + { + showMessage("ERROR") + } + } }); diff --git a/front/php/server/devices.php b/front/php/server/devices.php index 7dae1119..9ac42e44 100755 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -56,7 +56,8 @@ case 'getGroups': getGroups(); break; case 'getLocations': getLocations(); break; case 'getPholus': getPholus(); break; - case 'getNmap': getNmap(); break; + case 'getNmap': getNmap(); break; + case 'saveNmapPort': saveNmapPort(); break; default: logServerConsole ('Action: '. $action); break; } @@ -145,7 +146,6 @@ function getDeviceData() { //------------------------------------------------------------------------------ function setDeviceData() { global $db; - global $pia_lang; // sql $sql = 'UPDATE Devices SET @@ -184,7 +184,6 @@ function setDeviceData() { //------------------------------------------------------------------------------ function deleteDevice() { global $db; - global $pia_lang; // sql $sql = 'DELETE FROM Devices WHERE dev_MAC="' . $_REQUEST['mac'] .'"'; @@ -203,8 +202,7 @@ function deleteDevice() { // Delete all devices with empty MAC addresses //------------------------------------------------------------------------------ function deleteAllWithEmptyMACs() { - global $db; - global $pia_lang; + global $db; // sql $sql = 'DELETE FROM Devices WHERE dev_MAC=""'; @@ -223,8 +221,7 @@ function deleteAllWithEmptyMACs() { // Delete all devices with empty MAC addresses //------------------------------------------------------------------------------ function deleteUnknownDevices() { - global $db; - global $pia_lang; + global $db; // sql $sql = 'DELETE FROM Devices WHERE dev_Name="(unknown)"'; @@ -244,7 +241,6 @@ function deleteUnknownDevices() { //------------------------------------------------------------------------------ function deleteDeviceEvents() { global $db; - global $pia_lang; // sql $sql = 'DELETE FROM Events WHERE eve_MAC="' . $_REQUEST['mac'] .'"'; @@ -264,7 +260,6 @@ function deleteDeviceEvents() { //------------------------------------------------------------------------------ function deleteAllDevices() { global $db; - global $pia_lang; // sql $sql = 'DELETE FROM Devices'; @@ -284,8 +279,6 @@ function deleteAllDevices() { //------------------------------------------------------------------------------ function deleteEvents() { global $db; - global $pia_lang; - // sql $sql = 'DELETE FROM Events'; // execute sql @@ -304,7 +297,6 @@ function deleteEvents() { //------------------------------------------------------------------------------ function deleteEvents30() { global $db; - global $pia_lang; // sql $sql = "DELETE FROM Events WHERE eve_DateTime <= date('now', '-30 day')"; @@ -324,7 +316,6 @@ function deleteEvents30() { //------------------------------------------------------------------------------ function deleteActHistory() { global $db; - global $pia_lang; // sql $sql = 'DELETE FROM Online_History'; @@ -346,8 +337,7 @@ function PiaBackupDBtoArchive() { // prepare fast Backup $file = '../../../db/pialert.db'; $newfile = '../../../db/pialert.db.latestbackup'; - global $pia_lang; - + // copy files as a fast Backup if (!copy($file, $newfile)) { echo lang('BackDevices_Backup_CopError'); @@ -374,8 +364,7 @@ function PiaBackupDBtoArchive() { function PiaRestoreDBfromArchive() { // prepare fast Backup $file = '../../../db/pialert.db'; - $oldfile = '../../../db/pialert.db.prerestore'; - global $pia_lang; + $oldfile = '../../../db/pialert.db.prerestore'; // copy files as a fast Backup if (!copy($file, $oldfile)) { @@ -399,8 +388,7 @@ function PiaRestoreDBfromArchive() { //------------------------------------------------------------------------------ // Purge Backups //------------------------------------------------------------------------------ -function PiaPurgeDBBackups() { - global $pia_lang; +function PiaPurgeDBBackups() { $Pia_Archive_Path = '../../../db'; $Pia_Backupfiles = array(); @@ -485,8 +473,7 @@ function ImportCSV() { if (file_exists($file)) { - global $db; - global $pia_lang; + global $db; $error = ""; @@ -545,8 +532,7 @@ function ImportCSV() { // Toggle Dark/Light Themes //------------------------------------------------------------------------------ function PiaEnableDarkmode() { - $file = '../../../db/setting_darkmode'; - global $pia_lang; + $file = '../../../db/setting_darkmode'; if (file_exists($file)) { echo lang('BackDevices_darkmode_disabled'); @@ -948,7 +934,8 @@ function getNmap() { throw new Exception('Invalid mac address'); } else{ - $sql = 'SELECT * from Nmap_Scan where MAC ="'.$mac.'" '; + // $sql = 'SELECT * from Nmap_Scan where MAC ="'.$mac.'" '; + $sql = 'select * from (select * from Nmap_Scan INNER JOIN Devices on Nmap_Scan.MAC = Devices.dev_MAC) where MAC = "'.$mac.'" '; // array $tableData = array(); @@ -963,6 +950,7 @@ function getNmap() { 'Time' => $row['Time'], 'State' => $row['State'], 'Service' => $row['Service'], + 'IP' => $row['dev_LastIP'], 'Extra' => $row['Extra']); } @@ -976,6 +964,33 @@ function getNmap() { } } +// ------------------------------------------------------------------------------------------- + +function saveNmapPort() +{ + + $portIndex = $_REQUEST['index']; + $value = $_REQUEST['value']; + + if(is_integer((int)$portIndex)) + { + global $db; + // sql + $sql = 'UPDATE Nmap_Scan SET "Extra" = "'. $value .'" WHERE "Index"=' . $portIndex ; + // update Data + $result = $db->query($sql); + + // check result + if ($result == TRUE) { + echo 'OK'; + } else { + echo 'KO'; + } + } + // echo "asdasdasasd"; +} + + //------------------------------------------------------------------------------ // Status Where conditions //------------------------------------------------------------------------------ diff --git a/front/php/server/util.php b/front/php/server/util.php index 8aaf4d91..e0dcd600 100755 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -10,16 +10,28 @@ require '/home/pi/pialert/front/php/templates/timezone.php'; require '/home/pi/pialert/front/php/templates/skinUI.php'; +// require '/home/pi/pialert/front/php/templates/language/lang.php'; +// require '/home/pi/pialert/front/php/server/db.php'; $FUNCTION = []; $SETTINGS = []; +// displayMessage($_REQUEST); + + +// echo 'hereeeeeeeeeeeeeeeeeeeeee1'; + // init request params if(array_key_exists('function', $_REQUEST) != FALSE) -{ +{ + displayMessage(array_key_exists('function', $_REQUEST)); + displayMessage(array_key_exists('index', $_REQUEST)); + displayMessage(array_key_exists('value', $_REQUEST)); $FUNCTION = $_REQUEST['function']; + displayMessage($FUNCTION); + } if(array_key_exists('settings', $_REQUEST) != FALSE) @@ -36,10 +48,7 @@ elseif ($FUNCTION == 'cleanLog') { cleanLog($SETTINGS); } -elseif ($FUNCTION == 'saveNmapPort' && array_key_exists('index', $_REQUEST) && array_key_exists('value', $_REQUEST) ) -{ - saveNmapPort($_REQUEST['index'], $_REQUEST['value']); -} + //------------------------------------------------------------------------------ // Formatting data functions @@ -287,27 +296,7 @@ function saveSettings() } // ------------------------------------------------------------------------------------------- -function saveNmapPort($portIndex, $value) -{ - if(is_integer($portIndex)) - { - $value = escapeString($value); - // sql - $sql = 'UPDATE Nmap_Scan SET Extra = "'. quotes($value) .'" WHERE Index="' . $portIndex .'"'; - // update Data - $result = $db->query($sql); - - // check result - if ($result == TRUE) { - echo lang('Gen_Upd'); - } else { - echo lang('Gen_Upd_Fail')."\n\n$sql \n\n". $db->lastErrorMsg(); - } - } - echo "asdasdasasd"; -} -// ------------------------------------------------------------------------------------------- function getString ($codeName, $default) {