Merge remote-tracking branch 'origin/terorero-merge' into pr/74
This commit is contained in:
@@ -7,6 +7,13 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Puche 2021 pi.alert.application@gmail.com GNU GPLv3
|
||||
//------------------------------------------------------------------------------
|
||||
// ## TimeZone processing
|
||||
$config_file = "../../../config/pialert.conf";
|
||||
$config_file_lines = file($config_file);
|
||||
$config_file_lines_timezone = array_values(preg_grep('/^TIMEZONE\s.*/', $config_file_lines));
|
||||
$timezone_line = explode("'", $config_file_lines_timezone[0]);
|
||||
$Pia_TimeZone = $timezone_line[1];
|
||||
date_default_timezone_set($Pia_TimeZone);
|
||||
|
||||
foreach (glob("../../../db/setting_language*") as $filename) {
|
||||
$pia_lang_selected = str_replace('setting_language_','',basename($filename));
|
||||
@@ -34,9 +41,9 @@ if (strlen($pia_lang_selected) == 0) {$pia_lang_selected = 'en_us';}
|
||||
switch ($action) {
|
||||
case 'getDeviceData': getDeviceData(); break;
|
||||
case 'setDeviceData': setDeviceData(); break;
|
||||
case 'deleteDevice': deleteDevice(); break;
|
||||
case 'getNetworkNodes': getNetworkNodes(); break;
|
||||
case 'deleteAllWithEmptyMACs': deleteAllWithEmptyMACs(); break;
|
||||
case 'deleteDevice': deleteDevice(); break;
|
||||
case 'deleteAllWithEmptyMACs': deleteAllWithEmptyMACs(); break;
|
||||
case 'createBackupDB': createBackupDB(); break;
|
||||
case 'restoreBackupDB': restoreBackupDB(); break;
|
||||
case 'deleteAllDevices': deleteAllDevices(); break;
|
||||
@@ -44,13 +51,16 @@ if (strlen($pia_lang_selected) == 0) {$pia_lang_selected = 'en_us';}
|
||||
case 'runScan1min': runScan1min(); break;
|
||||
case 'deleteUnknownDevices': deleteUnknownDevices(); break;
|
||||
case 'deleteEvents': deleteEvents(); break;
|
||||
case 'deleteEvents30': deleteEvents30(); break;
|
||||
case 'deleteActHistory': deleteActHistory(); break;
|
||||
case 'deleteDeviceEvents': deleteDeviceEvents(); break;
|
||||
case 'PiaBackupDBtoArchive': PiaBackupDBtoArchive(); break;
|
||||
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;
|
||||
@@ -88,8 +98,8 @@ function getDeviceData() {
|
||||
$deviceData = $row;
|
||||
$mac = $deviceData['dev_MAC'];
|
||||
|
||||
$deviceData['dev_Network_Node_MAC'] = $row['dev_Infrastructure'];
|
||||
$deviceData['dev_Network_Node_port'] = $row['dev_Infrastructure_port'];
|
||||
$deviceData['dev_Network_Node_MAC_ADDR'] = $row['dev_Network_Node_MAC_ADDR'];
|
||||
$deviceData['dev_Network_Node_port'] = $row['dev_Network_Node_port'];
|
||||
$deviceData['dev_FirstConnection'] = formatDate ($row['dev_FirstConnection']); // Date formated
|
||||
$deviceData['dev_LastConnection'] = formatDate ($row['dev_LastConnection']); // Date formated
|
||||
|
||||
@@ -120,8 +130,10 @@ function getDeviceData() {
|
||||
$row = $result -> fetchArray (SQLITE3_NUM);
|
||||
$deviceData['dev_DownAlerts'] = $row[0];
|
||||
|
||||
// Get current date using php, sql datetime does not return time respective to timezone.
|
||||
$currentdate = date("Y-m-d H:i:s");
|
||||
// Presence hours
|
||||
$sql = 'SELECT CAST(( MAX (0, SUM (julianday (IFNULL (ses_DateTimeDisconnection, DATETIME("now","localtime")))
|
||||
$sql = 'SELECT CAST(( MAX (0, SUM (julianday (IFNULL (ses_DateTimeDisconnection,"'. $currentdate .'" ))
|
||||
- julianday (CASE WHEN ses_DateTimeConnection < '. $periodDate .' THEN '. $periodDate .'
|
||||
ELSE ses_DateTimeConnection END)) *24 )) AS INT)
|
||||
FROM Sessions
|
||||
@@ -149,23 +161,23 @@ function setDeviceData() {
|
||||
|
||||
// sql
|
||||
$sql = 'UPDATE Devices SET
|
||||
dev_Name = "'. quotes($_REQUEST['name']) .'",
|
||||
dev_Owner = "'. quotes($_REQUEST['owner']) .'",
|
||||
dev_DeviceType = "'. quotes($_REQUEST['type']) .'",
|
||||
dev_Vendor = "'. quotes($_REQUEST['vendor']) .'",
|
||||
dev_Favorite = "'. quotes($_REQUEST['favorite']) .'",
|
||||
dev_Group = "'. quotes($_REQUEST['group']) .'",
|
||||
dev_Location = "'. quotes($_REQUEST['location']) .'",
|
||||
dev_Comments = "'. quotes($_REQUEST['comments']) .'",
|
||||
dev_Infrastructure = "'. quotes($_REQUEST['networknode']).'",
|
||||
dev_Infrastructure_port = "'. quotes($_REQUEST['networknodeport']).'",
|
||||
dev_StaticIP = "'. quotes($_REQUEST['staticIP']) .'",
|
||||
dev_ScanCycle = "'. quotes($_REQUEST['scancycle']) .'",
|
||||
dev_AlertEvents = "'. quotes($_REQUEST['alertevents']) .'",
|
||||
dev_AlertDeviceDown = "'. quotes($_REQUEST['alertdown']) .'",
|
||||
dev_SkipRepeated = "'. quotes($_REQUEST['skiprepeated']) .'",
|
||||
dev_NewDevice = "'. quotes($_REQUEST['newdevice']) .'",
|
||||
dev_Archived = "'. quotes($_REQUEST['archived']) .'"
|
||||
dev_Name = "'. quotes($_REQUEST['name']) .'",
|
||||
dev_Owner = "'. quotes($_REQUEST['owner']) .'",
|
||||
dev_DeviceType = "'. quotes($_REQUEST['type']) .'",
|
||||
dev_Vendor = "'. quotes($_REQUEST['vendor']) .'",
|
||||
dev_Favorite = "'. quotes($_REQUEST['favorite']) .'",
|
||||
dev_Group = "'. quotes($_REQUEST['group']) .'",
|
||||
dev_Location = "'. quotes($_REQUEST['location']) .'",
|
||||
dev_Comments = "'. quotes($_REQUEST['comments']) .'",
|
||||
dev_Network_Node_MAC_ADDR = "'. quotes($_REQUEST['networknode']).'",
|
||||
dev_Network_Node_port = "'. quotes($_REQUEST['networknodeport']).'",
|
||||
dev_StaticIP = "'. quotes($_REQUEST['staticIP']) .'",
|
||||
dev_ScanCycle = "'. quotes($_REQUEST['scancycle']) .'",
|
||||
dev_AlertEvents = "'. quotes($_REQUEST['alertevents']) .'",
|
||||
dev_AlertDeviceDown = "'. quotes($_REQUEST['alertdown']) .'",
|
||||
dev_SkipRepeated = "'. quotes($_REQUEST['skiprepeated']) .'",
|
||||
dev_NewDevice = "'. quotes($_REQUEST['newdevice']) .'",
|
||||
dev_Archived = "'. quotes($_REQUEST['archived']) .'"
|
||||
WHERE dev_MAC="' . $_REQUEST['mac'] .'"';
|
||||
// update Data
|
||||
$result = $db->query($sql);
|
||||
@@ -299,6 +311,26 @@ function deleteEvents() {
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Delete all Events older than 30 days
|
||||
//------------------------------------------------------------------------------
|
||||
function deleteEvents30() {
|
||||
global $db;
|
||||
global $pia_lang;
|
||||
|
||||
// sql
|
||||
$sql = "DELETE FROM Events WHERE eve_DateTime <= date('now', '-30 day')";
|
||||
// execute sql
|
||||
$result = $db->query($sql);
|
||||
|
||||
// check result
|
||||
if ($result == TRUE) {
|
||||
echo $pia_lang['BackDevices_DBTools_DelEvents'];
|
||||
} else {
|
||||
echo $pia_lang['BackDevices_DBTools_DelEventsError']."\n\n$sql \n\n". $db->lastErrorMsg();
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Delete History
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -407,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
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -448,22 +594,35 @@ function PiaToggleArpScan() {
|
||||
// Query total numbers of Devices by status
|
||||
//------------------------------------------------------------------------------
|
||||
function getDevicesTotals() {
|
||||
global $db;
|
||||
|
||||
// combined query
|
||||
$result = $db->query(
|
||||
'SELECT
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('all').') as devices,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('connected').') as connected,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('favorites').') as favorites,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('new').') as new,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('down').') as down,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('archived').') as archived
|
||||
');
|
||||
$resultJSON = "";
|
||||
|
||||
$row = $result -> fetchArray (SQLITE3_NUM);
|
||||
if(getCache("getDevicesTotals") != "")
|
||||
{
|
||||
$resultJSON = getCache("getDevicesTotals");
|
||||
} else
|
||||
{
|
||||
global $db;
|
||||
|
||||
echo (json_encode (array ($row[0], $row[1], $row[2], $row[3], $row[4], $row[5])));
|
||||
// combined query
|
||||
$result = $db->query(
|
||||
'SELECT
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('all').') as devices,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('connected').') as connected,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('favorites').') as favorites,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('new').') as new,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('down').') as down,
|
||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('archived').') as archived
|
||||
');
|
||||
|
||||
$row = $result -> fetchArray (SQLITE3_NUM);
|
||||
$resultJSON = json_encode (array ($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]));
|
||||
|
||||
// save to cache
|
||||
setCache("getDevicesTotals", $resultJSON );
|
||||
}
|
||||
|
||||
echo ($resultJSON);
|
||||
}
|
||||
|
||||
|
||||
@@ -574,12 +733,43 @@ function getOwners() {
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Query Device Data
|
||||
//------------------------------------------------------------------------------
|
||||
function getNetworkNodes() {
|
||||
global $db;
|
||||
|
||||
// Device Data
|
||||
$sql = 'SELECT * FROM Devices WHERE dev_DeviceType in ( "AP", "Gateway", "Powerline", "Switch", "WLAN", "PLC", "Router","USB LAN Adapter", "USB WIFI Adapter")';
|
||||
|
||||
$result = $db->query($sql);
|
||||
|
||||
// arrays of rows
|
||||
$tableData = array();
|
||||
while ($row = $result -> fetchArray (SQLITE3_ASSOC)) {
|
||||
// Push row data
|
||||
$tableData[] = array('id' => $row['dev_MAC'],
|
||||
'name' => $row['dev_Name'] );
|
||||
}
|
||||
|
||||
// Control no rows
|
||||
if (empty($tableData)) {
|
||||
$tableData = [];
|
||||
}
|
||||
|
||||
// Return json
|
||||
echo (json_encode ($tableData));
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Query the List of types
|
||||
//------------------------------------------------------------------------------
|
||||
function getDeviceTypes() {
|
||||
global $db;
|
||||
|
||||
$networkTypes = getNetworkTypes();
|
||||
|
||||
// SQL
|
||||
$sql = 'SELECT DISTINCT 9 as dev_Order, dev_DeviceType
|
||||
FROM Devices
|
||||
@@ -588,7 +778,7 @@ function getDeviceTypes() {
|
||||
"Laptop", "Mini PC", "PC", "Printer", "Server", "Singleboard Computer (SBC)",
|
||||
"Game Console", "SmartTV", "TV Decoder", "Virtual Assistance",
|
||||
"Clock", "House Appliance", "Phone", "Radio",
|
||||
"AP", "NAS", "PLC", "Router")
|
||||
"AP", "Gateway", "Powerline", "Switch", "WLAN", "PLC", "Router","USB LAN Adapter", "USB WIFI Adapter" )
|
||||
|
||||
UNION SELECT 1 as dev_Order, "Smartphone"
|
||||
UNION SELECT 1 as dev_Order, "Tablet"
|
||||
@@ -599,6 +789,7 @@ function getDeviceTypes() {
|
||||
UNION SELECT 2 as dev_Order, "Printer"
|
||||
UNION SELECT 2 as dev_Order, "Server"
|
||||
UNION SELECT 2 as dev_Order, "Singleboard Computer (SBC)"
|
||||
UNION SELECT 2 as dev_Order, "NAS"
|
||||
|
||||
UNION SELECT 3 as dev_Order, "Domotic"
|
||||
UNION SELECT 3 as dev_Order, "Game Console"
|
||||
@@ -611,8 +802,12 @@ function getDeviceTypes() {
|
||||
UNION SELECT 4 as dev_Order, "Phone"
|
||||
UNION SELECT 4 as dev_Order, "Radio"
|
||||
|
||||
-- network devices
|
||||
UNION SELECT 5 as dev_Order, "AP"
|
||||
UNION SELECT 5 as dev_Order, "NAS"
|
||||
UNION SELECT 5 as dev_Order, "Gateway"
|
||||
UNION SELECT 5 as dev_Order, "Powerline"
|
||||
UNION SELECT 5 as dev_Order, "Switch"
|
||||
UNION SELECT 5 as dev_Order, "WLAN"
|
||||
UNION SELECT 5 as dev_Order, "PLC"
|
||||
UNION SELECT 5 as dev_Order, "Router"
|
||||
UNION SELECT 5 as dev_Order, "USB LAN Adapter"
|
||||
@@ -621,6 +816,8 @@ function getDeviceTypes() {
|
||||
UNION SELECT 10 as dev_Order, "Other"
|
||||
|
||||
ORDER BY 1,2';
|
||||
|
||||
|
||||
$result = $db->query($sql);
|
||||
|
||||
// arrays of rows
|
||||
@@ -633,8 +830,6 @@ function getDeviceTypes() {
|
||||
// Return json
|
||||
echo (json_encode ($tableData));
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Query the List of groups
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -761,4 +956,5 @@ function getDeviceCondition ($deviceStatus) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user