mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 01:26:11 -08:00
settings prep 2
This commit is contained in:
@@ -8,32 +8,56 @@
|
||||
// Puche 2021 pi.alert.application@gmail.com GNU GPLv3
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// ## TimeZone processing
|
||||
$config_file = "../../../config/pialert.conf";
|
||||
$config_file_lines = file($config_file);
|
||||
## TimeZone processing
|
||||
$basePath = "../../../config/";
|
||||
$config_file = "pialert.conf";
|
||||
|
||||
$fullConfPath = $basePath.$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);
|
||||
|
||||
|
||||
$FUNCTION = $_REQUEST['function'];
|
||||
$SETTINGS = $_REQUEST['settings'];
|
||||
|
||||
|
||||
if ($FUNCTION == 'savesettings') {
|
||||
saveSettings();
|
||||
} elseif ($PIA_SCAN_MODE == 'test') {
|
||||
// other function
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Formatting data functions
|
||||
//------------------------------------------------------------------------------
|
||||
// Creates a PHP array from a string representing a python array (input format ['...','...'])
|
||||
function createArray($input){
|
||||
$pattern = '/(^\s*\[)|(\]\s*$)/';
|
||||
|
||||
// regex patterns
|
||||
$patternBrackets = '/(^\s*\[)|(\]\s*$)/';
|
||||
$patternQuotes = '/(^\s*\')|(\'\s*$)/';
|
||||
$replacement = '';
|
||||
$noBrackets = preg_replace($pattern, $replacement, $input);
|
||||
|
||||
// remove brackets
|
||||
$noBrackets = preg_replace($patternBrackets, $replacement, $input);
|
||||
|
||||
return $options = explode(",", $noBrackets);
|
||||
$options = array();
|
||||
|
||||
// create array
|
||||
$optionsTmp = explode(",", $noBrackets);
|
||||
|
||||
// remove quotes
|
||||
foreach ($optionsTmp as $item)
|
||||
{
|
||||
array_push($options, preg_replace($patternQuotes, $replacement, $item) );
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
function formatDate ($date1) {
|
||||
@@ -75,7 +99,8 @@ function checkPermissions($files)
|
||||
// check access to database
|
||||
if(file_exists($file) != 1)
|
||||
{
|
||||
displayMessage("File ".$file." not found or inaccessible. Grant read & write permissions to the file to the correct user.");
|
||||
$message = "File ".$file." not found or inaccessible. Grant read & write permissions to the file to the correct user.";
|
||||
displayMessage($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,25 +115,118 @@ function displayMessage($message)
|
||||
|
||||
function saveSettings()
|
||||
{
|
||||
$config_file = "../../../config/pialert.conf";
|
||||
// save in the file
|
||||
$new_location = $config_file.'_'.strtotime("now").'.backup';
|
||||
global $SETTINGS, $FUNCTION, $fullConfPath, $basePath, $config_file_lines_timezone, $config_file_lines;
|
||||
|
||||
if(file_exists( $config_file) == 1)
|
||||
{
|
||||
// create a backup copy
|
||||
if (!copy($config_file, $new_location))
|
||||
{
|
||||
echo "Failed to copy file ".$config_file." to ".$new_location." <br/> Check your permissions to allow read/write access to the /config folder.";
|
||||
$timeZone = "";
|
||||
|
||||
foreach ($config_file_lines as $line)
|
||||
{
|
||||
if( preg_match('/TIMEZONE(.*?)/', $line, $match) == 1 )
|
||||
{
|
||||
if (preg_match('/\'(.*?)\'/', $line, $match) == 1) {
|
||||
$timeZone = $match[1];
|
||||
}
|
||||
}
|
||||
{
|
||||
echo "Backup of pialert.conf created: <code>".$new_location."</code>";
|
||||
}
|
||||
} else {
|
||||
echo 'File "'.$config_file.'" not found or missing read permissions.';
|
||||
}
|
||||
|
||||
// save in the DB
|
||||
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;
|
||||
|
||||
// chmod($fullConfPath, 0755);
|
||||
|
||||
if(file_exists( $fullConfPath) == 1)
|
||||
{
|
||||
// create a backup copy
|
||||
if (!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.";
|
||||
}
|
||||
{
|
||||
echo "Backup of original pialert.conf created: <code>".$new_name."</code><br/>";
|
||||
|
||||
// generate a clean pialert.conf file
|
||||
$groups = [];
|
||||
|
||||
$txt = $txt."#-----------------AUTOGENERATED FILE-----------------#\n";
|
||||
$txt = $txt."# #\n";
|
||||
$txt = $txt."# Generated: ".$timestamp." #\n";
|
||||
$txt = $txt."# #\n";
|
||||
$txt = $txt."# Config file for the LAN intruder detection app: #\n";
|
||||
$txt = $txt."# https://github.com/jokob-sk/Pi.Alert #\n";
|
||||
$txt = $txt."# #\n";
|
||||
$txt = $txt."#-----------------AUTOGENERATED FILE-----------------#\n";
|
||||
|
||||
// collect all groups
|
||||
foreach ($SETTINGS as $setting) {
|
||||
if( in_array($setting[0] , $groups) == false) {
|
||||
array_push($groups ,$setting[0]);
|
||||
}
|
||||
}
|
||||
|
||||
// go thru the groups and prepare settings to write to file
|
||||
foreach($groups as $group)
|
||||
{
|
||||
$txt = $txt."\n\n# ".$group;
|
||||
$txt = $txt."\n#---------------------------\n" ;
|
||||
foreach($SETTINGS as $setting)
|
||||
{
|
||||
if($group == $setting[0])
|
||||
{
|
||||
if($setting[3] == 'text' or $setting[3] == 'password' or $setting[3] == 'readonly' or $setting[3] == 'selecttext')
|
||||
{
|
||||
$txt = $txt.$setting[1]."='".$setting[2]."'\n" ;
|
||||
} elseif($setting[3] == 'integer' or $setting[3] == 'selectinteger')
|
||||
{
|
||||
$txt = $txt.$setting[1]."=".$setting[2]."\n" ;
|
||||
} elseif($setting[3] == 'boolean')
|
||||
{
|
||||
$val = "False";
|
||||
if($setting[2] == 'true')
|
||||
{
|
||||
$val = "True";
|
||||
}
|
||||
$txt = $txt.$setting[1]."=".$val."\n" ;
|
||||
}elseif($setting[3] == 'multiselect' or $setting[3] == 'subnets')
|
||||
{
|
||||
$temp = '[';
|
||||
foreach($setting[2] as $val)
|
||||
{
|
||||
$temp = $temp."'". $val."',";
|
||||
}
|
||||
$temp = substr_replace($temp, "", -1).']'; // close brackets and remove last comma ','
|
||||
$txt = $txt.$setting[1]."=".$temp."\n" ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$txt = $txt."\n\n";
|
||||
$txt = $txt."#-------------------IMPORTANT INFO-------------------#\n";
|
||||
$txt = $txt."# This file is ingested by a python script, so if #\n";
|
||||
$txt = $txt."# modified it needs to use python syntax #\n";
|
||||
$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!");
|
||||
fwrite($newConfig, $txt);
|
||||
fclose($newConfig);
|
||||
|
||||
}
|
||||
} else {
|
||||
echo 'File "'.$fullConfPath.'" not found or missing read permissions.';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function getString ($codeName, $default, $pia_lang) {
|
||||
@@ -195,4 +313,6 @@ function setCache($key, $value) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user