index.php 1.5 KB

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