upload_run_dungeon.php 2.8 KB

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