<?php
if ($_POST["doing"] == 1 || $_POST["doing"] == 2) {
$slotFound = 0;
$occupied = 1;
$imageReturn = "[{";
$curTime = time();
$fleoAudioOld = "";
$fleoip = $_SERVER['REMOTE_ADDR'];
if ($_POST['fleoAudio'] != "empty") {
    if ($_POST['inputConvolutNumber'] == 999) {
        while ($slotFound == 0 && $occupied == 1) {
        $inputConvolutNumber = random_int(1000,9999);
        if (file_exists("../videoaudio/" . $inputConvolutNumber . ".txt")) { $occupied = 1; } else { $occupied = 0; }
        }
        if ($occupied == 0) { 
            $slotFound = 1;
            if ($_POST["doing"] == 1) {
            file_put_contents("../videoaudio/" . $inputConvolutNumber . ".txt", 
            '"inputConvolutNumber":"' . $inputConvolutNumber . '","' . $inputConvolutNumber . '_boingy_0":"' . $_POST['boingy_0'] . '","' . $inputConvolutNumber . '_boingy_1":"' . $_POST['boingy_1'] . '","' . $inputConvolutNumber . '_boingy_2":"' . $_POST['boingy_2'] . '","' . $inputConvolutNumber . '_boingy_3":"' . $_POST['boingy_3'] . '","' . $inputConvolutNumber . '_boingy_4":"' . $_POST['boingy_4'] . '","' . $inputConvolutNumber . '_fleoAudio":"' . $_POST['fleoAudio'] . '"},{');
            } else if ($_POST["doing"] == 2) {
            file_put_contents("../videoaudio/" . $inputConvolutNumber . ".txt", 
            '"inputConvolutNumber":"' . $inputConvolutNumber . '","' . $inputConvolutNumber . '_boingy_5":"' . $_POST['boingy_5'] . '","' . $inputConvolutNumber . '_boingy_6":"' . $_POST['boingy_6'] . '","' . $inputConvolutNumber . '_boingy_7":"' . $_POST['boingy_7'] . '","' . $inputConvolutNumber . '_boingy_8":"' . $_POST['boingy_8'] . '","' . $inputConvolutNumber . '_boingy_9":"' . $_POST['boingy_9'] . '","' . $inputConvolutNumber . '_fleoAudio":"' . $_POST['fleoAudio'] . '"},{');    
            }
        }
} else {
    $inputConvolutNumber = intval($_POST['inputConvolutNumber']);
    $slotFound = 1;
    if ($_POST["doing"] == 1) {
        file_put_contents("../videoaudio/" . $inputConvolutNumber . ".txt", 
        '"inputConvolutNumber":"' . $inputConvolutNumber . '","' . $inputConvolutNumber . '_boingy_0":"' . $_POST['boingy_0'] . '","' . $inputConvolutNumber . '_boingy_1":"' . $_POST['boingy_1'] . '","' . $inputConvolutNumber . '_boingy_2":"' . $_POST['boingy_2'] . '","' . $inputConvolutNumber . '_boingy_3":"' . $_POST['boingy_3'] . '","' . $inputConvolutNumber . '_boingy_4":"' . $_POST['boingy_4'] . '","' . $inputConvolutNumber . '_fleoAudio":"' . $_POST['fleoAudio'] . '"},{');
        } else if ($_POST["doing"] == 2) {
        file_put_contents("../videoaudio/" . $inputConvolutNumber . ".txt", 
        '"inputConvolutNumber":"' . $inputConvolutNumber . '","' . $inputConvolutNumber . '_boingy_5":"' . $_POST['boingy_5'] . '","' . $inputConvolutNumber . '_boingy_6":"' . $_POST['boingy_6'] . '","' . $inputConvolutNumber . '_boingy_7":"' . $_POST['boingy_7'] . '","' . $inputConvolutNumber . '_boingy_8":"' . $_POST['boingy_8'] . '","' . $inputConvolutNumber . '_boingy_9":"' . $_POST['boingy_9'] . '","' . $inputConvolutNumber . '_fleoAudio":"' . $_POST['fleoAudio'] . '"},{');    
        }

}
} else {
    $inputConvolutNumber = intval($_POST['inputConvolutNumber']);
    $slotFound = 1;
    $webcamBlank = " ";
    if ($_POST["doing"] == 1) {
        file_put_contents("../videoaudio/" . $inputConvolutNumber . ".txt", 
        '"inputConvolutNumber":"' . $inputConvolutNumber . '","' . $inputConvolutNumber . '_boingy_0":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_1":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_2":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_3":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_4":"' . $webcamBlank . '","' . $inputConvolutNumber . '_fleoAudio":"' . $_POST['fleoAudio'] . '"},{');
        } else if ($_POST["doing"] == 2) {
        file_put_contents("../videoaudio/" . $inputConvolutNumber . ".txt", 
        '"inputConvolutNumber":"' . $inputConvolutNumber . '","' . $inputConvolutNumber . '_boingy_5":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_6":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_7":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_8":"' . $webcamBlank . '","' . $inputConvolutNumber . '_boingy_9":"' . $webcamBlank . '","' . $inputConvolutNumber . '_fleoAudio":"' . $_POST['fleoAudio'] . '"},{');    
        }    
}

$imageNum = 0;
if (is_dir("../videoaudio")) {
    if ($dh = opendir("../videoaudio")) {
        while (($file = readdir($dh)) !== false) {
            if ($file == '.' or $file == '..') continue;

            $filepath = "../videoaudio/" . $file;
            $last_modified_time = filemtime($filepath);
            $current_time = time();

            // Check if the last modification was made within the last 10 seconds
            if (($current_time - $last_modified_time) <= 10) {
                $contents = file_get_contents("../videoaudio/" . $file, true);
                $imageReturn .= $contents;
                $imageNum++;
            } else {
                rename($filepath,"../oldvideoaudio/" . $file);
            }
        }
        closedir($dh);
        $imageReturn .= '"inputConvolutNumberSelf":"' . $inputConvolutNumber . '","imageAmount":"' . $imageNum . '"}]';
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($imageReturn), PHP_EOL;
    }
} 
}