index.php 2.0 KB

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