Consolidate code in remaining pages, setting page updates
The Config and Advanced setting pages received changes to use the new setSetting() function which looks after creating or updated the setting in the required config files. Optionally it also specification of a command to run after saving the settings. Some variables like models, audio formats and frequency shift tools were also moved into common.php Service Controls page received changes with the replacement of the actual command to execute with a sort of shorthand human friendly version e.g. service <action> <service_name> -- service restart livestream.service These commands map to the original commands in serviceMaintenance() so this could be called through the API without needing the full-blown commend. views.php was updated to accommodate this also System Controls page received a small change to make the process checking for new git updates or getting the last commit hash a single function call. Include and Exclude species updated to use getFilePath() to generate a path to the required file instead of the hard coded path Views.php also received similar treatment with replacing some hard coded paths with generated paths, but logic changes to handle the change in running service commands
This commit is contained in:
+78
-109
@@ -1,42 +1,13 @@
|
||||
<?php
|
||||
$sys_timezone = "";
|
||||
// If we can get the timezome from the systems timezone file ust that
|
||||
if (file_exists('/etc/timezone')) {
|
||||
$tz_data = file_get_contents('/etc/timezone');
|
||||
if ($tz_data !== false) {
|
||||
$sys_timezone = trim($tz_data);
|
||||
}
|
||||
} else {
|
||||
// Else get timezone from the timedatectl command
|
||||
$tz_data = shell_exec('timedatectl show');
|
||||
$tz_data_array = parse_ini_string($tz_data);
|
||||
if (is_array($tz_data_array) && array_key_exists('Timezone', $tz_data_array)) {
|
||||
$sys_timezone = $tz_data_array['Timezone'];
|
||||
}
|
||||
}
|
||||
//Finally if we have a valod timezone, set it as the one PHP uses
|
||||
if ($sys_timezone !== "") {
|
||||
date_default_timezone_set($sys_timezone);
|
||||
if(file_exists('./scripts/common.php')){
|
||||
include_once "./scripts/common.php";
|
||||
}else{
|
||||
include_once "./common.php";
|
||||
}
|
||||
|
||||
session_start();
|
||||
$user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
|
||||
$user = trim($user);
|
||||
$home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd");
|
||||
$home = trim($home);
|
||||
if(!isset($_SESSION['behind'])) {
|
||||
$fetch = shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi fetch 2>&1");
|
||||
$str = trim(shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi status"));
|
||||
if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) {
|
||||
$num_commits_behind = $matches[1];
|
||||
$_SESSION['behind'] = $num_commits_behind;
|
||||
}
|
||||
if (preg_match('/\b(\d+)\b and \b(\d+)\b different commits each/', $str, $matches)) {
|
||||
$num1 = (int) $matches[1];
|
||||
$num2 = (int) $matches[2];
|
||||
$sum = $num1 + $num2;
|
||||
$_SESSION['behind'] = $sum;
|
||||
}
|
||||
$_SESSION['behind'] = getGitStatus();
|
||||
if(isset($_SESSION['behind'])&&intval($_SESSION['behind']) >= 99) {?>
|
||||
<style>
|
||||
.updatenumber {
|
||||
@@ -45,11 +16,7 @@ if(!isset($_SESSION['behind'])) {
|
||||
</style>
|
||||
<?php }}
|
||||
|
||||
if (file_exists('./scripts/thisrun.txt')) {
|
||||
$config = parse_ini_file('./scripts/thisrun.txt');
|
||||
} elseif (file_exists('./scripts/firstrun.ini')) {
|
||||
$config = parse_ini_file('./scripts/firstrun.ini');
|
||||
}
|
||||
parseConfig();
|
||||
|
||||
if ($config["LATITUDE"] == "0.000" && $config["LONGITUDE"] == "0.000") {
|
||||
echo "<center style='color:red'><b>WARNING: Your latitude and longitude are not set properly. Please do so now in Tools -> Settings.</center></b>";
|
||||
@@ -187,25 +154,25 @@ if(isset($_GET['view'])){
|
||||
if($_GET['view'] == "Advanced"){include('scripts/advanced.php');}
|
||||
if($_GET['view'] == "Included"){
|
||||
if(isset($_GET['species']) && isset($_GET['add'])){
|
||||
$file = './scripts/include_species_list.txt';
|
||||
$file = getFilePath('include_species_list.txt');
|
||||
$str = file_get_contents("$file");
|
||||
$str = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str);
|
||||
file_put_contents("$file", "$str");
|
||||
if(isset($_GET['species'])){
|
||||
foreach ($_GET['species'] as $selectedOption)
|
||||
file_put_contents("./scripts/include_species_list.txt", $selectedOption."\n", FILE_APPEND);
|
||||
file_put_contents( getFilePath('include_species_list.txt'), $selectedOption."\n", FILE_APPEND);
|
||||
}
|
||||
} elseif(isset($_GET['species']) && isset($_GET['del'])){
|
||||
$file = './scripts/include_species_list.txt';
|
||||
$file = getFilePath('include_species_list.txt');
|
||||
$str = file_get_contents("$file");
|
||||
$str = preg_replace('/^\h*\v+/m', '', $str);
|
||||
file_put_contents("$file", "$str");
|
||||
foreach($_GET['species'] as $selectedOption) {
|
||||
$content = file_get_contents("../BirdNET-Pi/include_species_list.txt");
|
||||
$content = file_get_contents( getFilePath('include_species_list.txt'));
|
||||
$newcontent = str_replace($selectedOption, "", "$content");
|
||||
file_put_contents("./scripts/include_species_list.txt", "$newcontent");
|
||||
file_put_contents( getFilePath('include_species_list.txt'), "$newcontent");
|
||||
}
|
||||
$file = './scripts/include_species_list.txt';
|
||||
$file = getFilePath('include_species_list.txt');
|
||||
$str = file_get_contents("$file");
|
||||
$str = preg_replace('/^\h*\v+/m', '', $str);
|
||||
file_put_contents("$file", "$str");
|
||||
@@ -214,23 +181,23 @@ if(isset($_GET['view'])){
|
||||
}
|
||||
if($_GET['view'] == "Excluded"){
|
||||
if(isset($_GET['species']) && isset($_GET['add'])){
|
||||
$file = './scripts/exclude_species_list.txt';
|
||||
$file = getFilePath('exclude_species_list.txt');
|
||||
$str = file_get_contents("$file");
|
||||
$str = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str);
|
||||
file_put_contents("$file", "$str");
|
||||
foreach ($_GET['species'] as $selectedOption)
|
||||
file_put_contents("./scripts/exclude_species_list.txt", $selectedOption."\n", FILE_APPEND);
|
||||
file_put_contents(getFilePath('exclude_species_list.txt'), $selectedOption."\n", FILE_APPEND);
|
||||
} elseif (isset($_GET['species']) && isset($_GET['del'])){
|
||||
$file = './scripts/exclude_species_list.txt';
|
||||
$file = getFilePath('exclude_species_list.txt');
|
||||
$str = file_get_contents("$file");
|
||||
$str = preg_replace('/^\h*\v+/m', '', $str);
|
||||
file_put_contents("$file", "$str");
|
||||
foreach($_GET['species'] as $selectedOption) {
|
||||
$content = file_get_contents("./scripts/exclude_species_list.txt");
|
||||
$content = file_get_contents(getFilePath('exclude_species_list.txt'));
|
||||
$newcontent = str_replace($selectedOption, "", "$content");
|
||||
file_put_contents("./scripts/exclude_species_list.txt", "$newcontent");
|
||||
file_put_contents(getFilePath('exclude_species_list.txt'), "$newcontent");
|
||||
}
|
||||
$file = './scripts/exclude_species_list.txt';
|
||||
$file = getFilePath('exclude_species_list.txt');
|
||||
$str = file_get_contents("$file");
|
||||
$str = preg_replace('/^\h*\v+/m', '', $str);
|
||||
file_put_contents("$file", "$str");
|
||||
@@ -241,11 +208,7 @@ if(isset($_GET['view'])){
|
||||
echo "<iframe src='scripts/filemanager/filemanager.php'></iframe>";
|
||||
}
|
||||
if($_GET['view'] == "Webterm"){
|
||||
if (file_exists('./scripts/thisrun.txt')) {
|
||||
$config = parse_ini_file('./scripts/thisrun.txt');
|
||||
} elseif (file_exists('./scripts/firstrun.ini')) {
|
||||
$config = parse_ini_file('./scripts/firstrun.ini');
|
||||
}
|
||||
parseConfig();
|
||||
$caddypwd = $config['CADDY_PWD'];
|
||||
if (!isset($_SERVER['PHP_AUTH_USER'])) {
|
||||
header('WWW-Authenticate: Basic realm="My Realm"');
|
||||
@@ -267,11 +230,7 @@ if(isset($_GET['view'])){
|
||||
}
|
||||
}
|
||||
} elseif(isset($_GET['submit'])) {
|
||||
if (file_exists('./scripts/thisrun.txt')) {
|
||||
$config = parse_ini_file('./scripts/thisrun.txt');
|
||||
} elseif (file_exists('./scripts/firstrun.ini')) {
|
||||
$config = parse_ini_file('./scripts/firstrun.ini');
|
||||
}
|
||||
parseConfig();
|
||||
$caddypwd = $config['CADDY_PWD'];
|
||||
if (!isset($_SERVER['PHP_AUTH_USER'])) {
|
||||
header('WWW-Authenticate: Basic realm="My Realm"');
|
||||
@@ -281,48 +240,48 @@ if(isset($_GET['view'])){
|
||||
} else {
|
||||
$submittedpwd = $_SERVER['PHP_AUTH_PW'];
|
||||
$submitteduser = $_SERVER['PHP_AUTH_USER'];
|
||||
$allowedCommands = array('sudo systemctl stop livestream.service && sudo systemctl stop icecast2.service',
|
||||
'sudo systemctl restart livestream.service && sudo systemctl restart icecast2.service',
|
||||
'sudo systemctl disable --now livestream.service && sudo systemctl disable icecast2 && sudo systemctl stop icecast2.service',
|
||||
'sudo systemctl enable icecast2 && sudo systemctl start icecast2.service && sudo systemctl enable --now livestream.service',
|
||||
'sudo systemctl stop web_terminal.service',
|
||||
'sudo systemctl restart web_terminal.service',
|
||||
'sudo systemctl disable --now web_terminal.service',
|
||||
'sudo systemctl enable --now web_terminal.service',
|
||||
'sudo systemctl stop birdnet_log.service',
|
||||
'sudo systemctl restart birdnet_log.service',
|
||||
'sudo systemctl disable --now birdnet_log.service',
|
||||
'sudo systemctl enable --now birdnet_log.service',
|
||||
'sudo systemctl stop extraction.service',
|
||||
'sudo systemctl restart extraction.service',
|
||||
'sudo systemctl disable --now extraction.service',
|
||||
'sudo systemctl enable --now extraction.service',
|
||||
'sudo systemctl stop birdnet_server.service',
|
||||
'sudo systemctl restart birdnet_server.service',
|
||||
'sudo systemctl disable --now birdnet_server.service',
|
||||
'sudo systemctl enable --now birdnet_server.service',
|
||||
'sudo systemctl stop birdnet_analysis.service',
|
||||
'sudo systemctl restart birdnet_analysis.service',
|
||||
'sudo systemctl disable --now birdnet_analysis.service',
|
||||
'sudo systemctl enable --now birdnet_analysis.service',
|
||||
'sudo systemctl stop birdnet_stats.service',
|
||||
'sudo systemctl restart birdnet_stats.service',
|
||||
'sudo systemctl disable --now birdnet_stats.service',
|
||||
'sudo systemctl enable --now birdnet_stats.service',
|
||||
'sudo systemctl stop birdnet_recording.service',
|
||||
'sudo systemctl restart birdnet_recording.service',
|
||||
'sudo systemctl disable --now birdnet_recording.service',
|
||||
'sudo systemctl enable --now birdnet_recording.service',
|
||||
'sudo systemctl stop chart_viewer.service',
|
||||
'sudo systemctl restart chart_viewer.service',
|
||||
'sudo systemctl disable --now chart_viewer.service',
|
||||
'sudo systemctl enable --now chart_viewer.service',
|
||||
'sudo systemctl stop spectrogram_viewer.service',
|
||||
'sudo systemctl restart spectrogram_viewer.service',
|
||||
'sudo systemctl disable --now spectrogram_viewer.service',
|
||||
'sudo systemctl enable --now spectrogram_viewer.service',
|
||||
'stop_core_services.sh',
|
||||
'restart_services.sh',
|
||||
$allowedCommands = array('service stop livestream.service && service stop icecast2.service',
|
||||
'service restart livestream.service && service restart icecast2.service',
|
||||
'service disable livestream.service && service disable icecast2 && service stop icecast2.service',
|
||||
'service enable icecast2 && service start icecast2.service && service enable livestream.service',
|
||||
'service stop web_terminal.service',
|
||||
'service restart web_terminal.service',
|
||||
'service disable web_terminal.service',
|
||||
'service enable web_terminal.service',
|
||||
'service stop birdnet_log.service',
|
||||
'service restart birdnet_log.service',
|
||||
'service disable birdnet_log.service',
|
||||
'service enable birdnet_log.service',
|
||||
'service stop extraction.service',
|
||||
'service restart extraction.service',
|
||||
'service disable extraction.service',
|
||||
'service enable extraction.service',
|
||||
'service stop birdnet_server.service',
|
||||
'service restart birdnet_server.service',
|
||||
'service disable birdnet_server.service',
|
||||
'service enable birdnet_server.service',
|
||||
'service stop birdnet_analysis.service',
|
||||
'service restart birdnet_analysis.service',
|
||||
'service disable birdnet_analysis.service',
|
||||
'service enable birdnet_analysis.service',
|
||||
'service stop birdnet_stats.service',
|
||||
'service restart birdnet_stats.service',
|
||||
'service disable birdnet_stats.service',
|
||||
'service enable birdnet_stats.service',
|
||||
'service stop birdnet_recording.service',
|
||||
'service restart birdnet_recording.service',
|
||||
'service disable birdnet_recording.service',
|
||||
'service enable birdnet_recording.service',
|
||||
'service stop chart_viewer.service',
|
||||
'service restart chart_viewer.service',
|
||||
'service disable chart_viewer.service',
|
||||
'service enable chart_viewer.service',
|
||||
'service stop spectrogram_viewer.service',
|
||||
'service restart spectrogram_viewer.service',
|
||||
'service disable spectrogram_viewer.service',
|
||||
'service enable spectrogram_viewer.service',
|
||||
'system stop core.services',
|
||||
'system restart core.services',
|
||||
'sudo reboot',
|
||||
'update_birdnet.sh',
|
||||
'sudo shutdown now',
|
||||
@@ -331,33 +290,43 @@ if(isset($_GET['view'])){
|
||||
if($submittedpwd == $caddypwd && $submitteduser == 'birdnet' && in_array($command,$allowedCommands)){
|
||||
if(isset($command)){
|
||||
$initcommand = $command;
|
||||
if (strpos($command, "systemctl") !== false) {
|
||||
$results = "";
|
||||
//Process the system commands differently
|
||||
if (strpos($command, "system") !== false) {
|
||||
$results = serviceMaintenance($command);
|
||||
//clear the command so we skip the next bits and go straight to output processing
|
||||
$command = '';
|
||||
}
|
||||
if (strpos($command, "service") !== false) {
|
||||
//If there more than one command to execute, processes then separately
|
||||
//currently only livestream service uses multiple commands to interact with the required services
|
||||
if (strpos($command, " && ") !== false) {
|
||||
$separate_commands = explode("&&", trim($command));
|
||||
$new_multiservice_status_command = "";
|
||||
foreach ($separate_commands as $indiv_service_command) {
|
||||
//Action the command
|
||||
serviceMaintenance($indiv_service_command);
|
||||
//explode the string by " " space so we can get each individual component of the command
|
||||
//and eventually the service name at the end
|
||||
$separate_command_tmp = explode(" ", trim($indiv_service_command));
|
||||
//get the service names
|
||||
//get the service names so we can poll th status
|
||||
$new_multiservice_status_command .= " " . trim(end($separate_command_tmp));
|
||||
}
|
||||
|
||||
$service_names = $new_multiservice_status_command;
|
||||
} else {
|
||||
//only one service needs restarting so we only need to query the status of one service
|
||||
serviceMaintenance($command);
|
||||
//only one service needs restarting so we only need to query the status of one service
|
||||
$tmp = explode(" ", trim($command));
|
||||
$service_names = end($tmp);
|
||||
}
|
||||
|
||||
$command .= " & sleep 3;sudo systemctl status " . $service_names;
|
||||
//Build up the command that will query the service status
|
||||
$command = "sleep 3;sudo systemctl status " . $service_names;
|
||||
}
|
||||
if($initcommand == "update_birdnet.sh") {
|
||||
unset($_SESSION['behind']);
|
||||
}
|
||||
$results = shell_exec("$command 2>&1");
|
||||
$results .= shell_exec("$command 2>&1");
|
||||
$results = str_replace("FAILURE", "<span style='color:red'>FAILURE</span>", $results);
|
||||
$results = str_replace("failed", "<span style='color:red'>failed</span>",$results);
|
||||
$results = str_replace("active (running)", "<span style='color:green'><b>active (running)</b></span>",$results);
|
||||
|
||||
Reference in New Issue
Block a user