index.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Profile uploader script.
  4. *
  5. * Exposes an API to save a run to the database.
  6. * Reads post data and calls the log-profile.py script.
  7. * It also saves the data to a JSON file in the da directory.
  8. * Mandatory POST parameters are:
  9. * - data: Received JSON file after a run.
  10. * - key: User API key.
  11. *
  12. * @category API
  13. */
  14. namespace swdb;
  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-profile: Invalid JSON");
  21. $status = 400;
  22. }
  23. else{
  24. $json = json_decode($data);
  25. $winfo = $json->{"wizard_info"};
  26. $uname = $winfo->{"wizard_name"};
  27. $uid = $winfo->{"wizard_id"};
  28. $dtime = (new DateTime())->format('Y-m-dTH:i:s');
  29. $fname = ($_SERVER["DOCUMENT_ROOT"] . "/../data/profile_" . $uid . "_" . $uname . "_" . $dtime . ".json");
  30. file_put_contents($fname, $data);
  31. $cmd = $_SERVER["DOCUMENT_ROOT"] . "/../application/bin/log-profile.py " . $key . " " . $fname;
  32. $out = [];
  33. $ret = 0;
  34. exec($cmd, $out, $ret);
  35. if ($ret != 0){
  36. error_log("ERROR: API v1 log-profile exited with status: " . $ret);
  37. error_log("====================");
  38. error_log("|| SCRIPT OUTPUT: ||");
  39. error_log("===============================================================================");
  40. foreach($out as $line){
  41. error_log("|| " . $line);
  42. }
  43. error_log("===============================================================================");
  44. $status = 400;
  45. }
  46. }
  47. }
  48. else{
  49. error_log("ERROR: API v1 log-profile: Invalid JSON");
  50. $status = 400;
  51. }
  52. http_response_code($status);
  53. return $status;
  54. ?>