upload_run_rift.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. $uname = $json_response->{"wizard_info"}->{"wizard_name"};
  50. $s = "SELECT COUNT(uid) AS c FROM player WHERE uid = $uid AND api_key = '$key';";
  51. if (1 != $db->query($s)->fetchArray(SQLITE3_ASSOC)["c"]){
  52. http_response_code(401);
  53. return 401;
  54. }
  55. // Run dungeon run parser script
  56. $cmd = __DIR__ . "/bin/upload_run_rift.py " .
  57. $key . " " .
  58. escapeshellarg($data_request) . " " .
  59. escapeshellarg($data_response) . " " .
  60. escapeshellarg($data_start_request) . " " .
  61. escapeshellarg($data_start_response);
  62. $out = [];
  63. $ret = 0;
  64. try{
  65. exec($cmd, $out, $ret);
  66. }
  67. catch(Exception $e) {
  68. error_log("Error running Rift Dungeon run script: " . $e->getMessage());
  69. http_response_code(500);
  70. return 500;
  71. }
  72. if ($ret != 201){
  73. try{
  74. error_log("Rift Dungeon run script returned an unexpected value $ret: ");
  75. http_response_code($ret);
  76. return $ret;
  77. }
  78. catch(Exception $e) {
  79. error_log("Rift Dungeon run script returned an unexpected value $ret: " . $e->getMessage());
  80. http_response_code(500);
  81. return 500;
  82. }
  83. }
  84. // At this point, status code should be 201
  85. http_response_code($ret);
  86. return $ret;
  87. }
  88. catch(Exception $e) {
  89. error_log("Unknown error parsing Rift Dungeon run: " . $e->getMessage());
  90. http_response_code(500);
  91. return 500;
  92. }
  93. ?>