index.php.txt 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Run logger script.
  4. *
  5. * Exposes an API to save a run to the database.
  6. * Reads post data and calls the log-run.py script.
  7. * Mandatory POST parameters are:
  8. * - data: Received JSON file after a run.
  9. * - key: User API key.
  10. *
  11. * @category API
  12. */
  13. namespace swdb;
  14. // TODO: Validate USER/KEY
  15. $status = 200;
  16. if (isset($_POST["data"]) && isset($_POST["key"])){
  17. $data = $_POST["data"];
  18. $key = $_POST["key"];
  19. if (json_decode($data) === null){
  20. error_log("ERROR: API v1 log-run: Invalid JSON");
  21. $status = 400;
  22. }
  23. else{
  24. $cmd = $_SERVER["DOCUMENT_ROOT"] . "/../application/bin/log-run.py " . $key . " " . escapeshellarg($data);
  25. $out = [];
  26. $ret = 0;
  27. exec($cmd, $out, $ret);
  28. if ($ret != 0){
  29. error_log("ERROR: API v1 log-run exited with status: " . $ret);
  30. error_log("====================");
  31. error_log("|| SCRIPT OUTPUT: ||");
  32. error_log("===============================================================================");
  33. foreach($out as $line){
  34. error_log("|| " . $line);
  35. }
  36. error_log("===============================================================================");
  37. $status = 400;
  38. }
  39. }
  40. }
  41. else{
  42. error_log("ERROR: API v1 log-run: Invalid JSON");
  43. $status = 400;
  44. }
  45. http_response_code($status);
  46. return $status;
  47. ?>