From 91d85232ca1dc124614fe5175d509f435a058ad1 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 25 Sep 2023 13:49:34 +0200 Subject: [PATCH] api creating task files --- api.php | 2 +- taskExecuter.php | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/api.php b/api.php index 09fa0b1..0bbd465 100644 --- a/api.php +++ b/api.php @@ -69,9 +69,9 @@ if(!is_dir($taskDir)){ $taskfilename = $taskDir.'/'.time().'-'.bin2hex(random_bytes(4)).'.txt'; $taskfile = fopen($taskfilename, 'w'); if($taskfile){ - echo "Task written to file"; fwrite($taskfile, $taskString); fclose($taskfile); + echo "Task written to file"; } else { echo "Task not written to file"; } diff --git a/taskExecuter.php b/taskExecuter.php index db27061..61d5f62 100644 --- a/taskExecuter.php +++ b/taskExecuter.php @@ -1,4 +1,48 @@ \ No newline at end of file +function sendEmail($emailAddress, $filename) +{ + echo "sending ".$filename." too ".$emailAddress; +} +if(is_dir($taskDir)) { + $taskfiles = scandir($taskDir); + $taskfiles = array_diff($taskfiles, array('.', '..')); + sort($taskfiles); + if(count($taskfiles) > 0) { + $taskString = file_get_contents($taskDir.'/'.$taskfiles[0]); + $task = json_decode($taskString, true); + + // if there are any requests to do, do the first + if(count($task['requests']) > 0) { + doRequest($task['requests'][0]); + echo "Request done"; + array_shift($task['requests']); + $taskString = json_encode($task); + $taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w'); + if($taskfile){ + fwrite($taskfile, $taskString); + fclose($taskfile); + } + } + // if there are no Requests left, send the E-Mail + else { + sendEmail($task['email'], $taskDir.'/'.$taskfiles[0]); + echo "Email sent"; + unlink($taskDir.'/'.$taskfiles[0]); + } + } else { + echo "Task dir Empty"; + } +} else { + echo "no Task dir"; +} + +?> \ No newline at end of file