index.php.txt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // TODO: Validate USER/KEY
  16. $status = 200;
  17. if (isset($_POST["data"]) && isset($_POST["key"])){
  18. $data = $_POST["data"];
  19. $key = $_POST["key"];
  20. if (json_decode($data) === null){
  21. error_log("ERROR: API v1 log-profile: Invalid JSON");
  22. $status = 400;
  23. }
  24. else{
  25. $json = json_decode($data);
  26. $winfo = $json->{"wizard_info"};
  27. $uname = $winfo->{"wizard_name"};
  28. $uid = $winfo->{"wizard_id"};
  29. $dtime = (new DateTime())->format('Y-m-dTH:i:s');
  30. $fname = ($_SERVER["DOCUMENT_ROOT"] . "/../data/profile_" . $uid . "_" . $uname . "_" . $dtime . ".json");
  31. file_put_contents($fname, $data);
  32. $cmd = $_SERVER["DOCUMENT_ROOT"] . "/../application/bin/log-profile.py " . $key . " " . $fname;
  33. $out = [];
  34. $ret = 0;
  35. exec($cmd, $out, $ret);
  36. if ($ret != 0){
  37. error_log("ERROR: API v1 log-profile exited with status: " . $ret);
  38. error_log("====================");
  39. error_log("|| SCRIPT OUTPUT: ||");
  40. error_log("===============================================================================");
  41. foreach($out as $line){
  42. error_log("|| " . $line);
  43. }
  44. error_log("===============================================================================");
  45. $status = 400;
  46. }
  47. }
  48. }
  49. else{
  50. error_log("ERROR: API v1 log-profile: Invalid JSON");
  51. $status = 400;
  52. }
  53. http_response_code($status);
  54. return $status;
  55. ?>