busyTimeout(1000); if(isset($_GET['deletefile'])) { ensure_authenticated('You must be authenticated to delete files.'); if (preg_match('~^.*(\.\.\/).+$~', $_GET['deletefile'])) { echo "Error"; die(); } $db_writable = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READWRITE); $db->busyTimeout(1000); $statement1 = $db_writable->prepare('DELETE FROM detections WHERE File_Name = :file_name LIMIT 1'); ensure_db_ok($statement1); $statement1->bindValue(':file_name', explode("/", $_GET['deletefile'])[2]); $file_pointer = $home."/BirdSongs/Extracted/By_Date/".$_GET['deletefile']; if (!exec("sudo rm $file_pointer 2>&1 && sudo rm $file_pointer.png 2>&1", $output)) { echo "OK"; } else { echo "Error - file deletion failed : " . implode(", ", $output) . "
"; } $result1 = $statement1->execute(); if ($result1 === false || $db_writable->changes() === 0) { echo "Error - database line deletion failed : " . $db_writable->lastErrorMsg(); } $db_writable->close(); die(); } if(isset($_GET['excludefile'])) { ensure_authenticated('You must be authenticated to change the protection of files.'); if(!file_exists($home."/BirdNET-Pi/scripts/disk_check_exclude.txt")) { file_put_contents($home."/BirdNET-Pi/scripts/disk_check_exclude.txt", "##start\n##end\n"); } if(isset($_GET['exclude_add'])) { $myfile = fopen($home."/BirdNET-Pi/scripts/disk_check_exclude.txt", "a") or die("Unable to open file!"); $txt = $_GET['excludefile']; fwrite($myfile, $txt."\n"); fwrite($myfile, $txt.".png\n"); fclose($myfile); echo "OK"; die(); } else { $lines = file($home."/BirdNET-Pi/scripts/disk_check_exclude.txt"); $search = $_GET['excludefile']; $result = ''; foreach($lines as $line) { if(stripos($line, $search) === false && stripos($line, $search.".png") === false) { $result .= $line; } } file_put_contents($home."/BirdNET-Pi/scripts/disk_check_exclude.txt", $result); echo "OK"; die(); } } if(isset($_GET['getlabels'])) { $labels = file('/home/pi/BirdNET-Pi/model/labels.txt', FILE_IGNORE_NEW_LINES); echo json_encode($labels); die(); } if(isset($_GET['changefile']) && isset($_GET['newname'])) { ensure_authenticated('You must be authenticated to delete files.'); if (preg_match('~^.*(\.\.\/).+$~', $_GET['changefile'])) { echo "Error"; die(); } $oldname = basename(urldecode($_GET['changefile'])); $newname = urldecode($_GET['newname']); if (!exec("$home/BirdNET-Pi/scripts/birdnet_changeidentification.sh \"$oldname\" \"$newname\" log_errors 2>&1", $output)) { echo "OK"; } else { echo "Error : " . implode(", ", $output) . "
"; } die(); } $shifted_path = $home."/BirdSongs/Extracted/By_Date/shifted/"; if(isset($_GET['shiftfile'])) { ensure_authenticated('You cannot shift files for this installation'); $filename = $_GET['shiftfile']; $pp = pathinfo($filename); $dir = $pp['dirname']; $fn = $pp['filename']; $ext = $pp['extension']; $pi = $home."/BirdSongs/Extracted/By_Date/"; if(isset($_GET['doshift'])) { $freqshift_tool = $config['FREQSHIFT_TOOL']; if ($freqshift_tool == "ffmpeg") { $cmd = "sudo /usr/bin/nohup /usr/bin/ffmpeg -y -i ".escapeshellarg($pi.$filename)." -af \"rubberband=pitch=".$config['FREQSHIFT_LO']."/".$config['FREQSHIFT_HI']."\" ".escapeshellarg($shifted_path.$filename).""; shell_exec("sudo mkdir -p ".$shifted_path.$dir." && ".$cmd); } else if ($freqshift_tool == "sox") { //linux.die.net/man/1/sox $soxopt = "-q"; $soxpitch = $config['FREQSHIFT_PITCH']; $cmd = "sudo /usr/bin/nohup /usr/bin/sox ".escapeshellarg($pi.$filename)." ".escapeshellarg($shifted_path.$filename)." pitch ".$soxopt." ".$soxpitch; shell_exec("sudo mkdir -p ".$shifted_path.$dir." && ".$cmd); } } else { $cmd = "sudo rm -f " . escapeshellarg($shifted_path.$filename); shell_exec($cmd); } echo "OK"; die(); } if(isset($_GET['bydate'])){ $statement = $db->prepare('SELECT DISTINCT(Date) FROM detections GROUP BY Date ORDER BY Date DESC'); ensure_db_ok($statement); $result = $statement->execute(); $view = "bydate"; #Specific Date } elseif(isset($_GET['date'])) { $date = $_GET['date']; session_start(); $_SESSION['date'] = $date; if(isset($_GET['sort']) && $_GET['sort'] == "occurrences") { $statement = $db->prepare("SELECT DISTINCT(Com_Name) FROM detections WHERE Date == \"$date\" GROUP BY Com_Name ORDER BY COUNT(*) DESC"); } else { $statement = $db->prepare("SELECT DISTINCT(Com_Name) FROM detections WHERE Date == \"$date\" ORDER BY Com_Name"); } ensure_db_ok($statement); $result = $statement->execute(); $view = "date"; #By Species } elseif(isset($_GET['byspecies'])) { if(isset($_GET['sort']) && $_GET['sort'] == "occurrences") { $statement = $db->prepare('SELECT DISTINCT(Com_Name) FROM detections GROUP BY Com_Name ORDER BY COUNT(*) DESC'); } else { $statement = $db->prepare('SELECT DISTINCT(Com_Name) FROM detections ORDER BY Com_Name ASC'); } session_start(); ensure_db_ok($statement); $result = $statement->execute(); $view = "byspecies"; #Specific Species } elseif(isset($_GET['species'])) { $species = htmlspecialchars_decode($_GET['species'], ENT_QUOTES); session_start(); $_SESSION['species'] = $species; $statement = $db->prepare("SELECT * FROM detections WHERE Com_Name == \"$species\" ORDER BY Com_Name"); ensure_db_ok($statement); $statement3 = $db->prepare("SELECT Date, Time, Sci_Name, MAX(Confidence), File_Name FROM detections WHERE Com_Name == \"$species\" ORDER BY Com_Name"); ensure_db_ok($statement3); $result = $statement->execute(); $result3 = $statement3->execute(); $view = "species"; } else { unset($_SESSION['species']); unset($_SESSION['date']); $view = "choose"; } if (get_included_files()[0] === __FILE__) { echo ' '; } ?>

fetchArray(SQLITE3_ASSOC)){ $date = $results['Date']; if(realpath($home."/BirdSongs/Extracted/By_Date/".$date) !== false){ echo "";}} #By Species } elseif($view == "byspecies") { $birds = array(); while($results=$result->fetchArray(SQLITE3_ASSOC)) { $name = $results['Com_Name']; $birds[] = $name; } if(count($birds) > 45) { $num_cols = 3; } else { $num_cols = 1; } $num_rows = ceil(count($birds) / $num_cols); for ($row = 0; $row < $num_rows; $row++) { echo ""; for ($col = 0; $col < $num_cols; $col++) { $index = $row + $col * $num_rows; if ($index < count($birds)) { ?> "; } } echo ""; } } elseif($view == "date") { $birds = array(); while($results=$result->fetchArray(SQLITE3_ASSOC)) { $name = $results['Com_Name']; $dir_name = str_replace("'", '', $name); if(realpath($home."/BirdSongs/Extracted/By_Date/".$date."/".str_replace(" ", "_", $dir_name)) !== false){ $birds[] = $name; } } if(count($birds) > 45) { $num_cols = 3; } else { $num_cols = 1; } $num_rows = ceil(count($birds) / $num_cols); for ($row = 0; $row < $num_rows; $row++) { echo ""; for ($col = 0; $col < $num_cols; $col++) { $index = $row + $col * $num_rows; if ($index < count($birds)) { ?> "; } } echo ""; } #Choose } else { echo ""; } echo "
"; } #Specific Species if(isset($_GET['species'])){ ?>

type="checkbox" name="only_excluded" onChange="submit()">
prepare("SELECT * FROM detections where Com_Name == \"$name\" AND Date == \"$date\" ORDER BY Confidence DESC"); } else { $statement2 = $db->prepare("SELECT * FROM detections where Com_Name == \"$name\" AND Date == \"$date\" ORDER BY Time DESC"); } } else { if(isset($_GET['sort']) && $_GET['sort'] == "confidence") { $statement2 = $db->prepare("SELECT * FROM detections where Com_Name == \"$name\" ORDER BY Confidence DESC"); } else { $statement2 = $db->prepare("SELECT * FROM detections where Com_Name == \"$name\" ORDER BY Date DESC, Time DESC"); } } ensure_db_ok($statement2); $result2 = $statement2->execute(); $num_rows = 0; while ($result2->fetchArray(SQLITE3_ASSOC)) { $num_rows++; } $result2->reset(); // reset the pointer to the beginning of the result set echo ""; $iter=0; while($results=$result2->fetchArray(SQLITE3_ASSOC)) { $comname = preg_replace('/ /', '_', $results['Com_Name']); $comname = preg_replace('/\'/', '', $comname); $date = $results['Date']; $filename = "/By_Date/".$date."/".$comname."/".$results['File_Name']; $filename_shifted = "/By_Date/shifted/".$date."/".$comname."/".$results['File_Name']; $filename_png = $filename . ".png"; $sciname = preg_replace('/ /', '_', $results['Sci_Name']); $sci_name = $results['Sci_Name']; $time = $results['Time']; $confidence = round((float)round($results['Confidence'],2) * 100 ) . '%'; $filename_formatted = $date."/".$comname."/".$results['File_Name']; // file was deleted by disk check, no need to show the detection in recordings if(!file_exists($home."/BirdSongs/Extracted/".$filename)) { continue; } if(!in_array($filename_formatted, $disk_check_exclude_arr) && isset($_GET['only_excluded'])) { continue; } $iter++; if($num_rows < 100){ $imageelem = ""; } else { $imageelem = ""; } if($config["FULL_DISK"] == "purge") { if(!in_array($filename_formatted, $disk_check_exclude_arr)) { $imageicon = "images/unlock.svg"; $title = "This file will be deleted when disk space needs to be freed (>95% usage)."; $type = "add"; } else { $imageicon = "images/lock.svg"; $title = "This file is excluded from being purged."; $type = "del"; } if(file_exists($shifted_path.$filename_formatted)) { $shiftImageIcon = "images/unshift.svg"; $shiftTitle = "This file has been shifted down in frequency."; $shiftAction = "unshift"; $filename = $filename_shifted; } else { $shiftImageIcon = "images/shift.svg"; $shiftTitle = "This file is not shifted in frequency."; $shiftAction = "shift"; } echo ""; } else { echo ""; } }if($iter == 0){ echo "";}echo "
$name
$date $time
$confidence
".$imageelem."
$date $time
$confidence
".$imageelem."
No recordings were found.

They may have been deleted to make space for new recordings. You can prevent this from happening in the future by clicking the icon in the top right of a recording.
You can also modify this behavior globally under \"Full Disk Behavior\" here.
";} if(isset($_GET['filename'])){ $name = $_GET['filename']; $statement2 = $db->prepare("SELECT * FROM detections where File_name == \"$name\" ORDER BY Date DESC, Time DESC"); ensure_db_ok($statement2); $result2 = $statement2->execute(); echo ""; while($results=$result2->fetchArray(SQLITE3_ASSOC)) { $comname = preg_replace('/ /', '_', $results['Com_Name']); $comname = preg_replace('/\'/', '', $comname); $date = $results['Date']; $filename = "/By_Date/".$date."/".$comname."/".$results['File_Name']; $filename_shifted = "/By_Date/shifted/".$date."/".$comname."/".$results['File_Name']; $filename_png = $filename . ".png"; $sciname = preg_replace('/ /', '_', $results['Sci_Name']); $sci_name = $results['Sci_Name']; $time = $results['Time']; $confidence = round((float)round($results['Confidence'],2) * 100 ) . '%'; $filename_formatted = $date."/".$comname."/".$results['File_Name']; // add disk_check_exclude.txt lines into an array for grepping $fp = @fopen($home."/BirdNET-Pi/scripts/disk_check_exclude.txt", 'r'); if ($fp) { $disk_check_exclude_arr = explode("\n", fread($fp, filesize($home."/BirdNET-Pi/scripts/disk_check_exclude.txt"))); } else { $disk_check_exclude_arr = []; } if($config["FULL_DISK"] == "purge") { if(!in_array($filename_formatted, $disk_check_exclude_arr)) { $imageicon = "images/unlock.svg"; $title = "This file will be deleted when disk space needs to be freed (>95% usage)."; $type = "add"; } else { $imageicon = "images/lock.svg"; $title = "This file is excluded from being purged."; $type = "del"; } if(file_exists($shifted_path.$filename_formatted)) { $shiftImageIcon = "images/unshift.svg"; $shiftTitle = "This file has been shifted down in frequency."; $shiftAction = "unshift"; $filename = $filename_shifted; } else { $shiftImageIcon = "images/shift.svg"; $shiftTitle = "This file is not shifted in frequency."; $shiftAction = "shift"; } echo ""; } else { echo ""; } }echo "
$name
$date $time
$confidence
$date $time
$confidence
";} echo "
"; if (get_included_files()[0] === __FILE__) { echo ''; }