PUSHPROD Settings 0.2

This commit is contained in:
Jokob-sk
2022-12-24 10:56:53 +11:00
parent b7cbb39b51
commit f651d9336a
5 changed files with 148 additions and 50 deletions

View File

@@ -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,15 +207,15 @@ 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
time_now = datetime.datetime.now()
@@ -2270,7 +2269,7 @@ def upgradeDB ():
# Drop table if available, but incompatible
if onlineHistoryAvailable and isIncompatible:
print_log ('Table is incompatible, Dropping the Online_History table)')
file_print ('[upgradeDB] Table is incompatible, Dropping the Online_History table)')
sql.execute("DROP TABLE Online_History;")
onlineHistoryAvailable = False
@@ -2289,6 +2288,7 @@ def upgradeDB ():
# Settings table
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
""")

View File

@@ -385,6 +385,15 @@ if ($_REQUEST['tab'] == '1') {
</textarea>
</div>
</div>
<div class="db_info_table_row">
<div class="db_tools_table_cell_a" style="">
pialert_front.log
</div>
<div class="db_tools_table_cell_b">
<textarea id="pialert_log" class="logs" cols="70" rows="10" ><?php echo file_get_contents( "./log/pialert_front.log" ); ?>
</textarea>
</div>
</div>
<div class="db_info_table_row">
<div class="db_tools_table_cell_a" style="">
IP_changes.log

View File

@@ -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 '<script>alert("'.$message.'")</script>';
global $logFolderPath, $log_file, $timestamp;
// sanitize
$message = str_replace(array("\n", "\r", PHP_EOL), '', $message);
echo "<script>function escape(html, encode) {
return html.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
.replace(/\t/g, '')
}</script>";
// Javascript Alert pop-up
if($logAlert)
{
echo '<script>alert(escape("'.$message.'"));</script>';
}
// F12 Browser console
if($logConsole)
{
echo '<script>console.log(escape("'.$message.'"));</script>';
}
//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 <code>pialert.conf</code> file.';
displayMessage('File "'.$fullConfPath.'" not found or missing read permissions. Creating a new <code>'.$config_file.'</code> file.', FALSE, TRUE, TRUE, TRUE);
}
// create a backup copy
elseif (!copy($fullConfPath, $new_location))
{
echo "Failed to copy file ".$fullConfPath." to ".$new_location." <br/> Check your permissions to allow read/write access to the /config folder.";
displayMessage("Failed to copy file ".$fullConfPath." to ".$new_location." <br/> 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 "<br/>Settings saved to the <code>pialert.conf</code> file. Backup of pialert.conf created: <code>".$new_name."</code>.
<br/><b>Restart the container for the chanegs to take effect.</b>";
displayMessage("<br/>Settings saved to the <code>".$config_file."</code> file.
Backup of ".$config_file." created: <code>".$new_name."</code>.<br/>
<b>Restart the container for the chanegs to take effect.</b>",
FALSE, TRUE, TRUE, TRUE);
}
@@ -312,3 +365,5 @@ function setCache($key, $value) {
?>

View File

@@ -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
// ###################################

View File

@@ -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 );
}
})