upload_run_rift.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. $data_start_request = filter_input(INPUT_POST, 'start_request');
  20. $data_start_response = filter_input(INPUT_POST, 'start_response');
  21. if (
  22. $data_request == null ||
  23. $data_request == false ||
  24. $data_response == null ||
  25. $data_response == false ||
  26. $data_start_request == null ||
  27. $data_start_request == false ||
  28. $data_start_response == null ||
  29. $data_start_response == false
  30. ){
  31. http_response_code(400);
  32. return 400;
  33. }
  34. // Check API key.
  35. $key = filter_input(INPUT_POST, 'key');
  36. if ($key == null || $key == false){
  37. http_response_code(401);
  38. return 401;
  39. }
  40. // Check data format.
  41. $json_request = json_decode($data_request);
  42. $json_response = json_decode($data_response);
  43. if ($json_response === null | $json_request === null){
  44. http_response_code(400);
  45. return 400;
  46. }
  47. // Authenticate
  48. $uid = $json_request->{"wizard_id"};
  49. $s = "SELECT COUNT(uid) AS c FROM player WHERE uid = $uid AND api_key = '$key';";
  50. if (1 != $db->query($s)->fetchArray(SQLITE3_ASSOC)["c"]){
  51. http_response_code(401);
  52. return 401;
  53. }
  54. // Run dungeon run parser script
  55. $cmd = __DIR__ . "/bin/upload_run_rift.py " .
  56. $key . " " .
  57. escapeshellarg($data_request) . " " .
  58. escapeshellarg($data_response) . " " .
  59. escapeshellarg($data_start_request) . " " .
  60. escapeshellarg($data_start_response);
  61. $out = [];
  62. $ret = 0;
  63. try{
  64. exec($cmd, $out, $ret);
  65. }
  66. catch(Exception $e) {
  67. error_log("Error running Rift Dungeon run script: " . $e->getMessage());
  68. http_response_code(500);
  69. return 500;
  70. }
  71. if ($ret != 201){
  72. try{
  73. error_log("Rift Dungeon run script returned an unexpected value $ret: ");
  74. http_response_code($ret);
  75. return $ret;
  76. }
  77. catch(Exception $e) {
  78. error_log("Rift Dungeon run script returned an unexpected value $ret: " . $e->getMessage());
  79. http_response_code(500);
  80. return 500;
  81. }
  82. }
  83. // At this point, status code should be 201
  84. http_response_code($ret);
  85. return $ret;
  86. }
  87. catch(Exception $e) {
  88. error_log("Unknown error parsing Rift Dungeon run: " . $e->getMessage());
  89. http_response_code(500);
  90. return 500;
  91. }
  92. ?>