index.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. $status = 200;
  15. if (isset($_POST["data"]) && isset($_POST["key"])){
  16. $data = $_POST["data"];
  17. $key = $_POST["key"];
  18. if (json_decode($data) === null){
  19. error_log("ERROR: API v1 log-run: Invalid JSON");
  20. $status = 400;
  21. }
  22. else{
  23. $cmd = $_SERVER["DOCUMENT_ROOT"] . "/../application/bin/log-run.py " . $key . " " . escapeshellarg($data);
  24. $out = [];
  25. $ret = 0;
  26. exec($cmd, $out, $ret);
  27. if ($ret != 0){
  28. error_log("ERROR: API v1 log-run exited with status: " . $ret);
  29. error_log("====================");
  30. error_log("|| SCRIPT OUTPUT: ||");
  31. error_log("===============================================================================");
  32. foreach($out as $line){
  33. error_log("|| " . $line);
  34. }
  35. error_log("===============================================================================");
  36. $status = 400;
  37. }
  38. }
  39. }
  40. else{
  41. error_log("ERROR: API v1 log-run: Invalid JSON");
  42. $status = 400;
  43. }
  44. http_response_code($status);
  45. return $status;
  46. ?>