diff --git a/homepage/views.php b/homepage/views.php index 987f522..d4b45f3 100644 --- a/homepage/views.php +++ b/homepage/views.php @@ -1,42 +1,13 @@ &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) {?> WARNING: Your latitude and longitude are not set properly. Please do so now in Tools -> Settings."; @@ -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 ""; } 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", "FAILURE", $results); $results = str_replace("failed", "failed",$results); $results = str_replace("active (running)", "active (running)",$results); diff --git a/scripts/advanced.php b/scripts/advanced.php index ae46ac3..dd03e67 100644 --- a/scripts/advanced.php +++ b/scripts/advanced.php @@ -1,11 +1,8 @@ /dev/null 2>&1 &'); - } + setSetting('CADDY_PWD', "\"$caddy_pwd\"",'update_caddyfile'); } if(isset($_GET["ice_pwd"])) { $ice_pwd = $_GET["ice_pwd"]; - if(strcmp($ice_pwd,$config['ICE_PWD']) !== 0) { - $contents = preg_replace("/ICE_PWD=.*/", "ICE_PWD=$ice_pwd", $contents); - $contents2 = preg_replace("/ICE_PWD=.*/", "ICE_PWD=$ice_pwd", $contents2); - } + setSetting('ICE_PWD', $ice_pwd); } if(isset($_GET["birdnetpi_url"])) { $birdnetpi_url = $_GET["birdnetpi_url"]; // remove trailing slash to prevent conf from becoming broken $birdnetpi_url = rtrim($birdnetpi_url, '/'); - if(strcmp($birdnetpi_url,$config['BIRDNETPI_URL']) !== 0) { - $contents = preg_replace("/BIRDNETPI_URL=.*/", "BIRDNETPI_URL=$birdnetpi_url", $contents); - $contents2 = preg_replace("/BIRDNETPI_URL=.*/", "BIRDNETPI_URL=$birdnetpi_url", $contents2); - $fh = fopen('/etc/birdnet/birdnet.conf', "w"); - $fh2 = fopen("./scripts/thisrun.txt", "w"); - fwrite($fh, $contents); - fwrite($fh2, $contents2); - exec('sudo /usr/local/bin/update_caddyfile.sh > /dev/null 2>&1 &'); - } + setSetting('BIRDNETPI_URL', $birdnetpi_url,'update_caddyfile'); } if(isset($_GET["rtsp_stream"])) { $rtsp_stream = str_replace("\r\n", ",", $_GET["rtsp_stream"]); - if(strcmp($rtsp_stream,$config['RTSP_STREAM']) !== 0) { - $contents = preg_replace("/RTSP_STREAM=.*/", "RTSP_STREAM=\"$rtsp_stream\"", $contents); - $contents2 = preg_replace("/RTSP_STREAM=.*/", "RTSP_STREAM=\"$rtsp_stream\"", $contents2); - $fh = fopen('/etc/birdnet/birdnet.conf', "w"); - $fh2 = fopen("./scripts/thisrun.txt", "w"); - fwrite($fh, $contents); - fwrite($fh2, $contents2); - exec('sudo systemctl restart birdnet_recording.service'); - exec('sudo systemctl restart livestream.service'); - } + setSetting('RTSP_STREAM', "\"$rtsp_stream\"", ['restart birdnet_recording', 'restart livestream']); } if (isset($_GET["rtsp_stream_to_livestream"])) { $rtsp_stream_selected = trim($_GET["rtsp_stream_to_livestream"]); - - //Setting exists already, see if the value changed - if (strcmp($rtsp_stream_selected, $config['RTSP_STREAM_TO_LIVESTREAM']) !== 0) { - $contents = preg_replace("/RTSP_STREAM_TO_LIVESTREAM=.*/", "RTSP_STREAM_TO_LIVESTREAM=\"$rtsp_stream_selected\"", $contents); - $contents2 = preg_replace("/RTSP_STREAM_TO_LIVESTREAM=.*/", "RTSP_STREAM_TO_LIVESTREAM=\"$rtsp_stream_selected\"", $contents2); - $fh = fopen("/etc/birdnet/birdnet.conf", "w"); - $fh2 = fopen("./scripts/thisrun.txt", "w"); - fwrite($fh, $contents); - fwrite($fh2, $contents2); - sleep(1); - exec("sudo systemctl restart livestream.service"); - } + setSetting('RTSP_STREAM_TO_LIVESTREAM', "\"$rtsp_stream_selected\"", 'restart livestream'); } if(isset($_GET["overlap"])) { $overlap = $_GET["overlap"]; - if(strcmp($overlap,$config['OVERLAP']) !== 0) { - $contents = preg_replace("/OVERLAP=.*/", "OVERLAP=$overlap", $contents); - $contents2 = preg_replace("/OVERLAP=.*/", "OVERLAP=$overlap", $contents2); - } + setSetting('OVERLAP', $overlap); } if(isset($_GET["confidence"])) { $confidence = $_GET["confidence"]; - if(strcmp($confidence,$config['CONFIDENCE']) !== 0) { - $contents = preg_replace("/CONFIDENCE=.*/", "CONFIDENCE=$confidence", $contents); - $contents2 = preg_replace("/CONFIDENCE=.*/", "CONFIDENCE=$confidence", $contents2); - } + setSetting('CONFIDENCE', $confidence); } if(isset($_GET["sensitivity"])) { $sensitivity = $_GET["sensitivity"]; - if(strcmp($sensitivity,$config['SENSITIVITY']) !== 0) { - $contents = preg_replace("/SENSITIVITY=.*/", "SENSITIVITY=$sensitivity", $contents); - $contents2 = preg_replace("/SENSITIVITY=.*/", "SENSITIVITY=$sensitivity", $contents2); - } + setSetting('SENSITIVITY', $sensitivity); } if(isset($_GET["freqshift_hi"]) && is_numeric($_GET['freqshift_hi'])) { $freqshift_hi = $_GET["freqshift_hi"]; - if(strcmp($freqshift_hi,$config['FREQSHIFT_HI']) !== 0) { - $contents = preg_replace("/FREQSHIFT_HI=.*/", "FREQSHIFT_HI=$freqshift_hi", $contents); - $contents2 = preg_replace("/FREQSHIFT_HI=.*/", "FREQSHIFT_HI=$freqshift_hi", $contents2); - } + setSetting('FREQSHIFT_HI', $freqshift_hi); } if(isset($_GET["freqshift_lo"]) && is_numeric($_GET['freqshift_lo'])) { $freqshift_lo = $_GET["freqshift_lo"]; - if(strcmp($freqshift_lo,$config['FREQSHIFT_LO']) !== 0) { - $contents = preg_replace("/FREQSHIFT_LO=.*/", "FREQSHIFT_LO=$freqshift_lo", $contents); - $contents2 = preg_replace("/FREQSHIFT_LO=.*/", "FREQSHIFT_LO=$freqshift_lo", $contents2); - } + setSetting('FREQSHIFT_HI', $freqshift_hi); } if(isset($_GET["freqshift_pitch"]) && is_numeric($_GET['freqshift_pitch'])) { $freqshift_pitch = $_GET["freqshift_pitch"]; - if(strcmp($freqshift_pitch,$config['FREQSHIFT_PITCH']) !== 0) { - $contents = preg_replace("/FREQSHIFT_PITCH=.*/", "FREQSHIFT_PITCH=$freqshift_pitch", $contents); - $contents2 = preg_replace("/FREQSHIFT_PITCH=.*/", "FREQSHIFT_PITCH=$freqshift_pitch", $contents2); - } + setSetting('FREQSHIFT_PITCH', $freqshift_pitch); } if(isset($_GET["freqshift_tool"])) { $freqshift_tool = $_GET["freqshift_tool"]; - if(strcmp($freqshift_tool,$config['FREQSHIFT_TOOL']) !== 0) { - $contents = preg_replace("/FREQSHIFT_TOOL=.*/", "FREQSHIFT_TOOL=$freqshift_tool", $contents); - $contents2 = preg_replace("/FREQSHIFT_TOOL=.*/", "FREQSHIFT_TOOL=$freqshift_tool", $contents2); - } + setSetting('FREQSHIFT_TOOL', $freqshift_tool); } if(isset($_GET["full_disk"])) { $full_disk = $_GET["full_disk"]; - if(strcmp($full_disk,$config['FULL_DISK']) !== 0) { - $contents = preg_replace("/FULL_DISK=.*/", "FULL_DISK=$full_disk", $contents); - $contents2 = preg_replace("/FULL_DISK=.*/", "FULL_DISK=$full_disk", $contents2); - } + setSetting('FULL_DISK', $full_disk); } if(isset($_GET["privacy_threshold"])) { $privacy_threshold = $_GET["privacy_threshold"]; - if(strcmp($privacy_threshold,$config['PRIVACY_THRESHOLD']) !== 0) { - $contents = preg_replace("/PRIVACY_THRESHOLD=.*/", "PRIVACY_THRESHOLD=$privacy_threshold", $contents); - $contents2 = preg_replace("/PRIVACY_THRESHOLD=.*/", "PRIVACY_THRESHOLD=$privacy_threshold", $contents2); - exec('restart_services.sh'); - } + setSetting('PRIVACY_THRESHOLD', $privacy_threshold,'restart_services'); } if(isset($_GET["rec_card"])) { - $rec_card = $_GET["rec_card"]; - if(strcmp($rec_card,$config['REC_CARD']) !== 0) { - $contents = preg_replace("/REC_CARD=.*/", "REC_CARD=\"$rec_card\"", $contents); - $contents2 = preg_replace("/REC_CARD=.*/", "REC_CARD=\"$rec_card\"", $contents2); - } + $rec_card = trim($_GET["rec_card"]); + setSetting('REC_CARD', "\"$rec_card\""); } if(isset($_GET["channels"])) { $channels = $_GET["channels"]; - if(strcmp($channels,$config['CHANNELS']) !== 0) { - $contents = preg_replace("/CHANNELS=.*/", "CHANNELS=$channels", $contents); - $contents2 = preg_replace("/CHANNELS=.*/", "CHANNELS=$channels", $contents2); - } + setSetting('CHANNELS', $channels); } if(isset($_GET["recording_length"])) { $recording_length = $_GET["recording_length"]; - if(strcmp($recording_length,$config['RECORDING_LENGTH']) !== 0) { - $contents = preg_replace("/RECORDING_LENGTH=.*/", "RECORDING_LENGTH=$recording_length", $contents); - $contents2 = preg_replace("/RECORDING_LENGTH=.*/", "RECORDING_LENGTH=$recording_length", $contents2); - } + setSetting('RECORDING_LENGTH', $recording_length); } if(isset($_GET["extraction_length"])) { $extraction_length = $_GET["extraction_length"]; - if(strcmp($extraction_length,$config['EXTRACTION_LENGTH']) !== 0) { - $contents = preg_replace("/EXTRACTION_LENGTH=.*/", "EXTRACTION_LENGTH=$extraction_length", $contents); - $contents2 = preg_replace("/EXTRACTION_LENGTH=.*/", "EXTRACTION_LENGTH=$extraction_length", $contents2); - } + setSetting('EXTRACTION_LENGTH', $extraction_length); } if(isset($_GET["audiofmt"])) { $audiofmt = $_GET["audiofmt"]; - if(strcmp($audiofmt,$config['AUDIOFMT']) !== 0) { - $contents = preg_replace("/AUDIOFMT=.*/", "AUDIOFMT=$audiofmt", $contents); - $contents2 = preg_replace("/AUDIOFMT=.*/", "AUDIOFMT=$audiofmt", $contents2); - } + setSetting('AUDIOFMT', $audiofmt); } if(isset($_GET["silence_update_indicator"])) { $silence_update_indicator = 1; - if(strcmp($silence_update_indicator,$config['SILENCE_UPDATE_INDICATOR']) !== 0) { - $contents = preg_replace("/SILENCE_UPDATE_INDICATOR=.*/", "SILENCE_UPDATE_INDICATOR=$silence_update_indicator", $contents); - $contents2 = preg_replace("/SILENCE_UPDATE_INDICATOR=.*/", "SILENCE_UPDATE_INDICATOR=$silence_update_indicator", $contents2); - } + setSetting('SILENCE_UPDATE_INDICATOR', $silence_update_indicator); } else { - $contents = preg_replace("/SILENCE_UPDATE_INDICATOR=.*/", "SILENCE_UPDATE_INDICATOR=0", $contents); - $contents2 = preg_replace("/SILENCE_UPDATE_INDICATOR=.*/", "SILENCE_UPDATE_INDICATOR=0", $contents2); + setSetting('SILENCE_UPDATE_INDICATOR', 0); } if(isset($_GET["raw_spectrogram"])) { $raw_spectrogram = 1; - if(strcmp($RAW_SPECTROGRAM,$config['RAW_SPECTROGRAM']) !== 0) { - $contents = preg_replace("/RAW_SPECTROGRAM=.*/", "RAW_SPECTROGRAM=$raw_spectrogram", $contents); - $contents2 = preg_replace("/RAW_SPECTROGRAM=.*/", "RAW_SPECTROGRAM=$raw_spectrogram", $contents2); - } + setSetting('RAW_SPECTROGRAM', $raw_spectrogram); } else { - $contents = preg_replace("/RAW_SPECTROGRAM=.*/", "RAW_SPECTROGRAM=0", $contents); - $contents2 = preg_replace("/RAW_SPECTROGRAM=.*/", "RAW_SPECTROGRAM=0", $contents2); + setSetting('RAW_SPECTROGRAM', 0); } if(isset($_GET["custom_image"])) { $custom_image = $_GET["custom_image"]; - if(strcmp($custom_image,$config['CUSTOM_IMAGE']) !== 0) { - $contents = preg_replace("/CUSTOM_IMAGE=.*/", "CUSTOM_IMAGE=$custom_image", $contents); - $contents2 = preg_replace("/CUSTOM_IMAGE=.*/", "CUSTOM_IMAGE=$custom_image", $contents2); - } + + setSetting('CUSTOM_IMAGE', $custom_image); } if(isset($_GET["custom_image_label"])) { $custom_image_label = $_GET["custom_image_label"]; - if(strcmp($custom_image_label,$config['CUSTOM_IMAGE_TITLE']) !== 0) { - $contents = preg_replace("/CUSTOM_IMAGE_TITLE=.*/", "CUSTOM_IMAGE_TITLE=\"$custom_image_label\"", $contents); - $contents2 = preg_replace("/CUSTOM_IMAGE_TITLE=.*/", "CUSTOM_IMAGE_TITLE=\"$custom_image_label\"", $contents2); - } + + setSetting('CUSTOM_IMAGE_TITLE', "\"$custom_image_label\""); } if (isset($_GET["LogLevel_BirdnetRecordingService"])) { $birdnet_recording_service_log_level = trim($_GET["LogLevel_BirdnetRecordingService"]); - //If setting exists change it's value - if (array_key_exists('LogLevel_BirdnetRecordingService', $config)) { - //Setting exists already, see if the value changed - if (strcmp($birdnet_recording_service_log_level, $config['LogLevel_BirdnetRecordingService']) !== 0) { - $contents = preg_replace("/LogLevel_BirdnetRecordingService=.*/", "LogLevel_BirdnetRecordingService=\"$birdnet_recording_service_log_level\"", $contents); - $contents2 = preg_replace("/LogLevel_BirdnetRecordingService=.*/", "LogLevel_BirdnetRecordingService=\"$birdnet_recording_service_log_level\"", $contents2); - $fh = fopen("/etc/birdnet/birdnet.conf", "w"); - $fh2 = fopen("./scripts/thisrun.txt", "w"); - fwrite($fh, $contents); - fwrite($fh2, $contents2); - sleep(1); - exec("sudo systemctl restart birdnet_recording.service"); - } - } else { - //Create the setting in the setting file - same as what update_birdnet_snippets.sh does but will take the users selected log level as the value - shell_exec('sudo echo "LogLevel_BirdnetRecordingService=\"' . $birdnet_recording_service_log_level . '\"" >> /etc/birdnet/birdnet.conf'); - //also update this run txt file - shell_exec('sudo echo "LogLevel_BirdnetRecordingService=\"' . $birdnet_recording_service_log_level . '\"" >> ./scripts/thisrun.txt'); - - //Reload the config files as we've changed the contents, we need to make sure the contents of the existing variables reflects contents of the config file - sleep(1); - $contents = file_get_contents('/etc/birdnet/birdnet.conf'); - $contents2 = file_get_contents('./scripts/thisrun.txt'); - - exec("sudo systemctl restart birdnet_recording.service"); - } + setSetting('LogLevel_BirdnetRecordingService', "\"$birdnet_recording_service_log_level\"",'restart birdnet_recording'); } if (isset($_GET["LogLevel_SpectrogramViewerService"])) { $spectrogram_viewer_service_log_level = trim($_GET["LogLevel_SpectrogramViewerService"]); - //If setting exists change it's value - if (array_key_exists('LogLevel_SpectrogramViewerService', $config)) { - //Setting exists already, see if the value changed - if (strcmp($spectrogram_viewer_service_log_level, $config['LogLevel_SpectrogramViewerService']) !== 0) { - $contents = preg_replace("/LogLevel_SpectrogramViewerService=.*/", "LogLevel_SpectrogramViewerService=\"$spectrogram_viewer_service_log_level\"", $contents); - $contents2 = preg_replace("/LogLevel_SpectrogramViewerService=.*/", "LogLevel_SpectrogramViewerService=\"$spectrogram_viewer_service_log_level\"", $contents2); - $fh = fopen("/etc/birdnet/birdnet.conf", "w"); - $fh2 = fopen("./scripts/thisrun.txt", "w"); - fwrite($fh, $contents); - fwrite($fh2, $contents2); - sleep(1); - exec("sudo systemctl restart spectrogram_viewer.service"); - } - } else { - //Create the setting in the setting file - same as what update_birdnet_snippets.sh does but will take the users selected log level as the value - shell_exec('sudo echo "LogLevel_SpectrogramViewerService=\"' . $spectrogram_viewer_service_log_level . '\"" >> /etc/birdnet/birdnet.conf'); - //also update this run txt file - shell_exec('sudo echo "LogLevel_SpectrogramViewerService=\"' . $spectrogram_viewer_service_log_level . '\"" >> ./scripts/thisrun.txt'); - - //Reload the config files as we've changed the contents, we need to make sure the contents of the existing variables reflects contents of the config file - sleep(1); - $contents = file_get_contents('/etc/birdnet/birdnet.conf'); - $contents2 = file_get_contents('./scripts/thisrun.txt'); - - exec("sudo systemctl restart spectrogram_viewer.service"); - } + setSetting('LogLevel_SpectrogramViewerService', "\"$spectrogram_viewer_service_log_level\"",'restart spectrogram_viewer'); } if (isset($_GET["LogLevel_LiveAudioStreamService"])) { $livestream_audio_service_log_level = trim($_GET["LogLevel_LiveAudioStreamService"]); - //If setting exists change it's value - if (array_key_exists('LogLevel_LiveAudioStreamService', $config)) { - //Setting exists already, see if the value changed - if (strcmp($livestream_audio_service_log_level, $config['LogLevel_LiveAudioStreamService']) !== 0) { - $contents = preg_replace("/LogLevel_LiveAudioStreamService=.*/", "LogLevel_LiveAudioStreamService=\"$livestream_audio_service_log_level\"", $contents); - $contents2 = preg_replace("/LogLevel_LiveAudioStreamService=.*/", "LogLevel_LiveAudioStreamService=\"$livestream_audio_service_log_level\"", $contents2); - //Write the settings to the config files, so we can restart the relevant services - $fh = fopen("/etc/birdnet/birdnet.conf", "w"); - $fh2 = fopen("./scripts/thisrun.txt", "w"); - fwrite($fh, $contents); - fwrite($fh2, $contents2); - sleep(1); - exec("sudo systemctl restart livestream.service && sudo systemctl restart icecast2.service"); - } - } else { - //Create the setting in the setting file - same as what update_birdnet_snippets.sh does but will take the users selected log level as the value - shell_exec('sudo echo "LogLevel_LiveAudioStreamService=\"' . $livestream_audio_service_log_level . '\"" >> /etc/birdnet/birdnet.conf'); - //also update this run txt file - shell_exec('sudo echo "LogLevel_LiveAudioStreamService=\"' . $livestream_audio_service_log_level . '\"" >> ./scripts/thisrun.txt'); - - //Reload the config files as we've changed the contents, we need to make sure the contents of the existing variables reflects contents of the config file - sleep(1); - $contents = file_get_contents('/etc/birdnet/birdnet.conf'); - $contents2 = file_get_contents('./scripts/thisrun.txt'); - - exec("sudo systemctl restart livestream.service && sudo systemctl restart icecast2.service"); - } + setSetting('LogLevel_LiveAudioStreamService', "\"$livestream_audio_service_log_level\"",['restart livestream','restart icecast2']); } - - //Finally write the data out. some sections do this themselves in order to have the new settings ready for the services that will be restarted - //but will doubly ensure the settings are saved after any modification - $fh = fopen('/etc/birdnet/birdnet.conf', "w"); - $fh2 = fopen("./scripts/thisrun.txt", "w"); - fwrite($fh, $contents); - fwrite($fh2, $contents2); } -$user = trim(shell_exec("awk -F: '/1000/{print $1}' /etc/passwd")); -$home = trim(shell_exec("awk -F: '/1000/{print $6}' /etc/passwd")); - -$count_labels = count(file($home."/BirdNET-Pi/model/labels.txt")); -$count = $count_labels; +$count = getLabelsCount(); ?> @@ -47,7 +44,7 @@ $eachlines = file($filename, FILE_IGNORE_NEW_LINES); Please Select $lines";} diff --git a/scripts/service_controls.php b/scripts/service_controls.php index 1888599..ed88538 100644 --- a/scripts/service_controls.php +++ b/scripts/service_controls.php @@ -1,17 +1,19 @@ - 200) { + $filesinproc=trim(shell_exec("ls ".getDirectory('home')."/BirdSongs/Processed | wc -l")); + if($filesinproc > 200) { echo "(stalled - backlog of ".$filesinproc." files in ~/BirdSongs/Processed/)"; return; } - } - $op = shell_exec("sudo systemctl status ".$name." | grep Active | grep ' active\| activating\|running\|waiting\|start'"); + } + $op = getServiceStatus($name)['status']; if(strlen($op) > 0) { echo "(active)"; } else { @@ -25,78 +27,78 @@ function service_status($name) { Live Audio Stream - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable Web Terminal - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable BirdNET Log - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable Extraction Service - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable BirdNET Analysis Server - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable BirdNET Analysis Client - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable Streamlit Statistics - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable Recording Service - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable Chart Viewer - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable Spectrogram Viewer - Stop - Restart - Disable - Enable + Stop + Restart + Disable + Enable - Stop Core Services + Stop Core Services - Restart Core Services + Restart Core Services diff --git a/scripts/system_controls.php b/scripts/system_controls.php index 067b314..c993565 100644 --- a/scripts/system_controls.php +++ b/scripts/system_controls.php @@ -1,21 +1,11 @@ &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; +if(file_exists('./scripts/common.php')){ + include_once "./scripts/common.php"; +}else{ + include_once "./common.php"; } + +$_SESSION['behind'] = getGitStatus(); ?> @@ -45,8 +35,7 @@ function update() { Clear ALL data Running version: diff --git a/scripts/todays_detections.php b/scripts/todays_detections.php index 5068f12..96c6d2f 100644 --- a/scripts/todays_detections.php +++ b/scripts/todays_detections.php @@ -64,52 +64,6 @@ die(); } -// from https://stackoverflow.com/questions/2690504/php-producing-relative-date-time-from-timestamps -function relativeTime($ts) -{ - if(!ctype_digit($ts)) - $ts = strtotime($ts); - - $diff = time() - $ts; - if($diff == 0) - return 'now'; - elseif($diff > 0) - { - $day_diff = floor($diff / 86400); - if($day_diff == 0) - { - if($diff < 60) return 'just now'; - if($diff < 120) return '1 minute ago'; - if($diff < 3600) return floor($diff / 60) . ' minutes ago'; - if($diff < 7200) return '1 hour ago'; - if($diff < 86400) return floor($diff / 3600) . ' hours ago'; - } - if($day_diff == 1) return 'Yesterday'; - if($day_diff < 7) return $day_diff . ' days ago'; - if($day_diff < 31) return ceil($day_diff / 7) . ' weeks ago'; - if($day_diff < 60) return 'last month'; - return date('F Y', $ts); - } - else - { - $diff = abs($diff); - $day_diff = floor($diff / 86400); - if($day_diff == 0) - { - if($diff < 120) return 'in a minute'; - if($diff < 3600) return 'in ' . floor($diff / 60) . ' minutes'; - if($diff < 7200) return 'in an hour'; - if($diff < 86400) return 'in ' . floor($diff / 3600) . ' hours'; - } - if($day_diff == 1) return 'Tomorrow'; - if($day_diff < 4) return date('l', $ts); - if($day_diff < 7 + (7 - date('w'))) return 'next week'; - if(ceil($day_diff / 7) < 4) return 'in ' . ceil($day_diff / 7) . ' weeks'; - if(date('n', $ts) == date('n') + 1) return 'next month'; - return date('F Y', $ts); - } -} - if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) { $result0 = getTodaysDetections($_GET['display_limit'], $_GET['searchterm'], $_GET['hard_limit']);
Running version: