upload_run_scenario.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Scenario run logger script.
  4. *
  5. * Exposes an API to save a run to the database.
  6. * Reads post data and calls the upload_scenario_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. global $db;
  14. try{
  15. // Check data
  16. $data = filter_input(INPUT_POST, 'data');
  17. if ($data == null || $data == false){
  18. http_response_code(400);
  19. return 400;
  20. }
  21. // Check API key.
  22. $key = filter_input(INPUT_POST, 'key');
  23. if ($key == null || $key == false){
  24. http_response_code(401);
  25. return 401;
  26. }
  27. // Check data format.
  28. $json = json_decode($data);
  29. if ($json === null){
  30. http_response_code(400);
  31. return 400;
  32. }
  33. // Authenticate
  34. $uid = $json->{"wizard_id"};
  35. $uname = $json->{"wizard_info"}->{"wizard_name"};
  36. $s = "SELECT COUNT(uid) AS c FROM player WHERE uid = $uid AND api_key = '$key';";
  37. if (1 != $db->query($s)->fetchArray(SQLITE3_ASSOC)["c"]){
  38. http_response_code(401);
  39. return 401;
  40. }
  41. // Run scenario run parser script
  42. $cmd = __DIR__ . "/bin/upload_run_scenario.py " . $key . " " . escapeshellarg($data);;
  43. $out = [];
  44. $ret = 0;
  45. try{
  46. exec($cmd, $out, $ret);
  47. }
  48. catch(Exception $e) {
  49. error_log("Error running scenario run script '$cmd': " . $e->getMessage());
  50. http_response_code(500);
  51. return 500;
  52. }
  53. if ($ret != 200){
  54. try{
  55. http_response_code($ret);
  56. return $ret;
  57. }
  58. catch(Exception $e) {
  59. error_log("Dungeon run script '$cmd' returned an unexpected value $ret: " . $e->getMessage());
  60. http_response_code(500);
  61. return 500;
  62. }
  63. }
  64. // At this point, status code should be 200
  65. http_response_code($ret);
  66. return $ret;
  67. }
  68. catch(Exception $e) {
  69. error_log("Unknown error parsing scenario run: " . $e->getMessage());
  70. http_response_code(500);
  71. return 500;
  72. }
  73. ?>