30){
// If the directory for Tasks is already created
if(is_dir($taskDir)) {
$taskfiles = scandir($taskDir);
$taskfiles = array_diff($taskfiles, array('.', '..'));
sort($taskfiles);
// if there are any tasks which are older then min oldness
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + $minTaskOldness < time()) {
$taskFilePath = $taskDir.'/'.$taskfiles[0];
$csvFilePath = str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]);
$taskString = file_get_contents($taskFilePath);
$task = json_decode($taskString, true);
// if there are any requests to do, do the first
if(count($task['requests']) > 0) {
$nextOffset = doRequest(
$task['requests'][0],
$csvFilePath,
$username,
$password
);
if($nextOffset){
$task['requests'][0]['offset'] = $nextOffset;
} else {
array_shift($task['requests']);
}
$taskString = json_encode($task);
$taskfile = fopen($taskFilePath, 'w');
if($taskfile){
fwrite($taskfile, $taskString);
fclose($taskfile);
}
} else {
// No requests left -> send CSV directly by email
unlink($taskFilePath);
echo "Task File deleted";
echo "
";
sendEmail(
$task['email'],
$csvFilePath,
$smtppassword
);
echo "CSV sent by email";
echo "
";
}
} else {
// actual Time - start time
echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime));
echo "
";
echo "sleeping 10 seconds";
echo "
";
sleep(10);
}
}
}
?>