From f651d9336ab5e76d09a226dc2aec0ae623850c10 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 24 Dec 2022 10:56:53 +1100 Subject: [PATCH] PUSHPROD Settings 0.2 --- back/pialert.py | 16 ++-- front/maintenance.php | 9 +++ front/php/server/util.php | 133 +++++++++++++++++++++++---------- front/php/templates/header.php | 38 +++++++++- front/settings.php | 2 +- 5 files changed, 148 insertions(+), 50 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index d2907c8f..f7dce735 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -71,7 +71,6 @@ file_print('\n Permissions check (All should be True)') file_print('------------------------------------------------') file_print( " " + confPath + " | " + " READ | " + str(os.access(fullConfPath, os.R_OK))) file_print( " " + confPath + " | " + " WRITE | " + str(os.access(fullConfPath, os.W_OK))) - file_print( " " + dbPath + " | " + " READ | " + str(os.access(fullDbPath, os.R_OK))) file_print( " " + dbPath + " | " + " WRITE | " + str(os.access(fullDbPath, os.W_OK))) file_print('------------------------------------------------') @@ -208,14 +207,14 @@ def main (): sql_connection = None sql = None - # Upgrade DB if needed - upgradeDB() - # create log files write_file(logPath + 'IP_changes.log', '') write_file(logPath + 'stdout.log', '') write_file(logPath + 'stderr.log', '') write_file(logPath + 'pialert.log', '') + + # Upgrade DB if needed + upgradeDB() while True: # update NOW time @@ -2269,8 +2268,8 @@ def upgradeDB (): """).fetchone()[0] == 0 # Drop table if available, but incompatible - if onlineHistoryAvailable and isIncompatible: - print_log ('Table is incompatible, Dropping the Online_History table)') + if onlineHistoryAvailable and isIncompatible: + file_print ('[upgradeDB] Table is incompatible, Dropping the Online_History table)') sql.execute("DROP TABLE Online_History;") onlineHistoryAvailable = False @@ -2288,7 +2287,8 @@ def upgradeDB (): """) # Settings table - if settingsMissing: + if settingsMissing: + file_print("[upgradeDB] Adding Settings table") sql.execute(""" CREATE TABLE "Settings" ( "Index" INTEGER, @@ -2311,6 +2311,7 @@ def upgradeDB (): """).fetchone()[0] == 0 if dev_Network_Node_MAC_ADDR_missing : + file_print("[upgradeDB] Adding dev_Network_Node_MAC_ADDR to the Devices table") sql.execute(""" ALTER TABLE "Devices" ADD "dev_Network_Node_MAC_ADDR" TEXT """) @@ -2321,6 +2322,7 @@ def upgradeDB (): """).fetchone()[0] == 0 if dev_Network_Node_port_missing : + file_print("[upgradeDB] Adding dev_Network_Node_port to the Devices table") sql.execute(""" ALTER TABLE "Devices" ADD "dev_Network_Node_port" INTEGER """) diff --git a/front/maintenance.php b/front/maintenance.php index da486308..d9d5a3be 100755 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -385,6 +385,15 @@ if ($_REQUEST['tab'] == '1') { +
+
+ pialert_front.log +
+
+ +
+
IP_changes.log diff --git a/front/php/server/util.php b/front/php/server/util.php index 81279686..d354ec27 100755 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -8,21 +8,51 @@ // Puche 2021 pi.alert.application@gmail.com GNU GPLv3 //------------------------------------------------------------------------------ -## TimeZone processing -$basePath = "../../../config/"; +// ################################### +// ## TimeZone processing start +// ################################### +$configFolderPath = "/home/pi/pialert/config/"; $config_file = "pialert.conf"; +$logFolderPath = "/home/pi/pialert/front/log/"; +$log_file = "pialert_front.log"; -$fullConfPath = $basePath.$config_file; + +$fullConfPath = $configFolderPath.$config_file; chmod($fullConfPath, 0777); - - $config_file_lines = file($fullConfPath); $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]; +$timeZone = ""; + +foreach ($config_file_lines as $line) +{ + if( preg_match('/TIMEZONE(.*?)/', $line, $match) == 1 ) + { + if (preg_match('/\'(.*?)\'/', $line, $match) == 1) { + $timeZone = $match[1]; + } + } +} + +if($timeZone == "") +{ + $timeZone = "Europe/Berlin"; +} + +date_default_timezone_set($timeZone); + +$date = new DateTime("now", new DateTimeZone($timeZone) ); +$timestamp = $date->format('Y-m-d_H-i-s'); + + +// ################################### +// ## TimeZone processing end +// ################################### + $FUNCTION = $_REQUEST['function']; $SETTINGS = $_REQUEST['settings']; @@ -99,60 +129,81 @@ function checkPermissions($files) // check access to database if(file_exists($file) != 1) { - $message = "File ".$file." not found or inaccessible. Grant read & write permissions to the file to the correct user."; - displayMessage($message); + $message = "File '".$file."' not found or inaccessible. Correct file permissions, create one yourself or generate a new one in 'Settings' by clicking the 'Save' button."; + displayMessage($message, TRUE); } - } - + } } -function displayMessage($message) +function displayMessage($message, $logAlert = FALSE, $logConsole = TRUE, $logFile = TRUE, $logEcho = FALSE) { - echo ''; + global $logFolderPath, $log_file, $timestamp; + + // sanitize + $message = str_replace(array("\n", "\r", PHP_EOL), '', $message); + + echo ""; + + // Javascript Alert pop-up + if($logAlert) + { + echo ''; + } + + // F12 Browser console + if($logConsole) + { + echo ''; + } + + //File + if($logFile) + { + if(file_exists($logFolderPath.$log_file) != 1) // file doesn't exist, create one + { + $log = fopen($logFolderPath.$log_file, "w") or die("Unable to open file!"); + }else // file exists, append + { + $log = fopen($logFolderPath.$log_file, "a") or die("Unable to open file!"); + } + + fwrite($log, "[".$timestamp. "] " . $message.PHP_EOL."" ); + fclose($log); + } + + //echo + if($logEcho) + { + echo $message; + } + } function saveSettings() { - global $SETTINGS, $FUNCTION, $fullConfPath, $basePath, $config_file_lines_timezone, $config_file_lines; + global $SETTINGS, $FUNCTION, $config_file, $fullConfPath, $configFolderPath, $timestamp; - $timeZone = ""; - foreach ($config_file_lines as $line) - { - if( preg_match('/TIMEZONE(.*?)/', $line, $match) == 1 ) - { - if (preg_match('/\'(.*?)\'/', $line, $match) == 1) { - $timeZone = $match[1]; - } - } - } - - if($timeZone == "") - { - $timeZone = "Europe/Berlin"; - } - - date_default_timezone_set($timeZone); - - $date = new DateTime("now", new DateTimeZone($timeZone) ); - $timestamp = $date->format('Y-m-d_H-i-s'); // save in the file - $new_name = "pialert.conf".'_'.$timestamp.'.backup'; - $new_location = $basePath.$new_name; + $new_name = $config_file.'_'.$timestamp.'.backup'; + $new_location = $configFolderPath.$new_name; // chmod($fullConfPath, 0755); if(file_exists( $fullConfPath) != 1) { - echo 'File "'.$fullConfPath.'" not found or missing read permissions. Creating a new pialert.conf file.'; + displayMessage('File "'.$fullConfPath.'" not found or missing read permissions. Creating a new '.$config_file.' file.', FALSE, TRUE, TRUE, TRUE); } // create a backup copy elseif (!copy($fullConfPath, $new_location)) { - echo "Failed to copy file ".$fullConfPath." to ".$new_location."
Check your permissions to allow read/write access to the /config folder."; + displayMessage("Failed to copy file ".$fullConfPath." to ".$new_location."
Check your permissions to allow read/write access to the /config folder.", FALSE, TRUE, TRUE, TRUE); } @@ -219,12 +270,14 @@ function saveSettings() $txt = $txt."#-------------------IMPORTANT INFO-------------------#\n"; // open new file and write the new configuration - $newConfig = fopen($basePath."pialert.conf", "w") or die("Unable to open file!"); + $newConfig = fopen($fullConfPath, "w") or die("Unable to open file!"); fwrite($newConfig, $txt); fclose($newConfig); - echo "
Settings saved to the pialert.conf file. Backup of pialert.conf created: ".$new_name.". -
Restart the container for the chanegs to take effect."; + displayMessage("
Settings saved to the ".$config_file." file. + Backup of ".$config_file." created: ".$new_name.".
+ Restart the container for the chanegs to take effect.", + FALSE, TRUE, TRUE, TRUE); } @@ -312,3 +365,5 @@ function setCache($key, $value) { ?> + + diff --git a/front/php/templates/header.php b/front/php/templates/header.php index 5c5ced86..37a56416 100755 --- a/front/php/templates/header.php +++ b/front/php/templates/header.php @@ -11,12 +11,44 @@ // ################################### // ## TimeZone processing start // ################################### -$config_file = "../config/pialert.conf"; -$config_file_lines = file($config_file); + +$configFolderPath = "/home/pi/pialert/config/"; +$config_file = "pialert.conf"; +$logFolderPath = "/home/pi/pialert/front/log/"; +$log_file = "pialert_front.log"; + + +$fullConfPath = $configFolderPath.$config_file; + +chmod($fullConfPath, 0777); + +$config_file_lines = file($fullConfPath); $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); + +$timeZone = ""; + +foreach ($config_file_lines as $line) +{ + if( preg_match('/TIMEZONE(.*?)/', $line, $match) == 1 ) + { + if (preg_match('/\'(.*?)\'/', $line, $match) == 1) { + $timeZone = $match[1]; + } + } +} + +if($timeZone == "") +{ + $timeZone = "Europe/Berlin"; +} + +date_default_timezone_set($timeZone); + +$date = new DateTime("now", new DateTimeZone($timeZone) ); +$timestamp = $date->format('Y-m-d_H-i-s'); + // ################################### // ## TimeZone processing end // ################################### diff --git a/front/settings.php b/front/settings.php index 02961dd8..bc07db35 100644 --- a/front/settings.php +++ b/front/settings.php @@ -307,7 +307,7 @@ $db->close(); data: { function: 'savesettings', settings: collectSettings() }, success: function(data, textStatus) { // $("#result").html(data); - console.log(data); + // console.log(data); showModalDefault ('Result', data ); } })