diff --git a/homepage/views.php b/homepage/views.php index 822f899..8f86511 100644 --- a/homepage/views.php +++ b/homepage/views.php @@ -6,27 +6,13 @@ $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); session_start(); -if(!isset($_SESSION['my_timezone'])) { - $_SESSION['my_timezone'] = trim(shell_exec('timedatectl show --value --property=Timezone')); -} -date_default_timezone_set($_SESSION['my_timezone']); +require_once 'scripts/common.php'; +$user = get_user(); +$home = get_home(); +$config = get_config(); +set_timezone(); -$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 (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'); -} -function service_mount() { - global $home; - $service_mount=trim(shell_exec("systemd-escape -p --suffix=mount ".$home."/BirdSongs/StreamData")); - return $service_mount; -} -$authenticated = isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_PW'] == $config['CADDY_PWD'] && $_SERVER['PHP_AUTH_USER'] == 'birdnet'; -if($authenticated && (!isset($_SESSION['behind']) || !isset($_SESSION['behind_time']) || time() > $_SESSION['behind_time'] + 86400)) { +if(is_authenticated() && (!isset($_SESSION['behind']) || !isset($_SESSION['behind_time']) || time() > $_SESSION['behind_time'] + 86400)) { shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi fetch > /dev/null 2>/dev/null &"); $str = trim(shell_exec("sudo -u".$user." git -C ".$home."/BirdNET-Pi status")); if (preg_match("/behind '.*?' by (\d+) commit(s?)\b/", $str, $matches)) { @@ -135,8 +121,14 @@ function copyOutput(elem) { ";} - if($_GET['view'] == "System Controls"){include('scripts/system_controls.php');} - if($_GET['view'] == "Services"){include('scripts/service_controls.php');} + if($_GET['view'] == "System Controls"){ + ensure_authenticated(); + include('scripts/system_controls.php'); + } + if($_GET['view'] == "Services"){ + ensure_authenticated(); + include('scripts/service_controls.php'); + } if($_GET['view'] == "Spectrogram"){include('spectrogram.php');} if($_GET['view'] == "View Log"){echo "";} if($_GET['view'] == "Overview"){include('overview.php');} @@ -147,42 +139,27 @@ if(isset($_GET['view'])){ if($_GET['view'] == "Streamlit"){echo "";} if($_GET['view'] == "Daily Charts"){include('history.php');} if($_GET['view'] == "Tools"){ - $caddypwd = $config['CADDY_PWD']; - if (!isset($_SERVER['PHP_AUTH_USER'])) { - header('WWW-Authenticate: Basic realm="My Realm"'); - header('HTTP/1.0 401 Unauthorized'); - echo '
You cannot edit the settings for this installation
'; - exit; - } else { - $submittedpwd = $_SERVER['PHP_AUTH_PW']; - $submitteduser = $_SERVER['PHP_AUTH_USER']; - if($submittedpwd == $caddypwd && $submitteduser == 'birdnet'){ - $url = $_SERVER['SERVER_NAME']."/scripts/adminer.php"; - echo "
-
- - - - - - - - - -
-
"; - } else { - header('WWW-Authenticate: Basic realm="My Realm"'); - header('HTTP/1.0 401 Unauthorized'); - echo '
You cannot edit the settings for this installation
'; - exit; - } - } + ensure_authenticated(); + $url = $_SERVER['SERVER_NAME']."/scripts/adminer.php"; + echo "
+
+ + + + + + + + + +
+
"; } if($_GET['view'] == "Recordings"){include('play.php');} if($_GET['view'] == "Settings"){include('scripts/config.php');} if($_GET['view'] == "Advanced"){include('scripts/advanced.php');} if($_GET['view'] == "Included"){ + ensure_authenticated(); if(isset($_GET['species']) && isset($_GET['add'])){ $file = './scripts/include_species_list.txt'; $str = file_get_contents("$file"); @@ -211,6 +188,7 @@ if(isset($_GET['view'])){ include('./scripts/include_list.php'); } if($_GET['view'] == "Excluded"){ + ensure_authenticated(); if(isset($_GET['species']) && isset($_GET['add'])){ $file = './scripts/exclude_species_list.txt'; $str = file_get_contents("$file"); @@ -240,88 +218,53 @@ if(isset($_GET['view'])){ echo ""; } 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'); - } - $caddypwd = $config['CADDY_PWD']; - if (!isset($_SERVER['PHP_AUTH_USER'])) { - header('WWW-Authenticate: Basic realm="My Realm"'); - header('HTTP/1.0 401 Unauthorized'); - echo '
You cannot access the web terminal
'; - exit; - } else { - $submittedpwd = $_SERVER['PHP_AUTH_PW']; - $submitteduser = $_SERVER['PHP_AUTH_USER']; - if($submittedpwd == $caddypwd && $submitteduser == 'birdnet'){ - #ACCESS THE WEB TERMINAL - echo ""; - } else { - header('WWW-Authenticate: Basic realm="My Realm"'); - header('HTTP/1.0 401 Unauthorized'); - echo '
You cannot access the web terminal
'; - exit; - } - } + ensure_authenticated('You cannot access the web terminal'); + echo ""; } } 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'); - } - $caddypwd = $config['CADDY_PWD']; - if (!isset($_SERVER['PHP_AUTH_USER'])) { - header('WWW-Authenticate: Basic realm="My Realm"'); - header('HTTP/1.0 401 Unauthorized'); - echo 'You cannot access the web terminal'; - exit; - } 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 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', - 'sudo systemctl enable '.service_mount().' && sudo reboot', - 'sudo systemctl disable '.service_mount().' && sudo reboot', - 'stop_core_services.sh', - 'restart_services.sh', - 'sudo reboot', - 'update_birdnet.sh', - 'sudo shutdown now', - 'sudo clear_all_data.sh'); - $command = $_GET['submit']; - if($submittedpwd == $caddypwd && $submitteduser == 'birdnet' && in_array($command,$allowedCommands)){ + ensure_authenticated(); + $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 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', + 'sudo systemctl enable '.get_service_mount_name().' && sudo reboot', + 'sudo systemctl disable '.get_service_mount_name().' && sudo reboot', + 'stop_core_services.sh', + 'restart_services.sh', + 'sudo reboot', + 'update_birdnet.sh', + 'sudo shutdown now', + 'sudo clear_all_data.sh'); + $command = $_GET['submit']; + if(in_array($command,$allowedCommands)){ if(isset($command)){ $initcommand = $command; if (strpos($command, "systemctl") !== false) { @@ -348,7 +291,7 @@ if(isset($_GET['view'])){ $command .= " & sleep 3;sudo systemctl status " . $service_names; } if($initcommand == "update_birdnet.sh") { - unset($_SESSION['behind']); + session_unset(); } $results = shell_exec("$command 2>&1"); $results = str_replace("FAILURE", "FAILURE", $results); @@ -401,14 +344,8 @@ if(isset($_GET['view'])){ $results = "This command has no output."; } echo "
Output of command:`".$initcommand."`
$results
"; - } else { - header('WWW-Authenticate: Basic realm="My Realm"'); - header('HTTP/1.0 401 Unauthorized'); - echo 'You cannot access the web terminal'; - exit; } } - } ob_end_flush(); } else {include('overview.php');} ?>