upload_run_dungeon.php 2.6 KB

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