upload_run_dimension.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Dimension Hole Dungeon run logger script.
  4. *
  5. * Exposes an API to save a run to the database.
  6. * Reads post data and calls the upload_run_dimension.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. $s = "SELECT COUNT(uid) AS c FROM player WHERE uid = $uid AND api_key = '$key';";
  39. if (1 != $db->query($s)->fetchArray(SQLITE3_ASSOC)["c"]){
  40. http_response_code(401);
  41. return 401;
  42. }
  43. // Run TOA run parser script
  44. $cmd = __DIR__ . "/bin/upload_run_dimension.py " . $key . " " . escapeshellarg($data_request) . " " . escapeshellarg($data_response);
  45. $out = [];
  46. $ret = 0;
  47. try{
  48. exec($cmd, $out, $ret);
  49. }
  50. catch(Exception $e) {
  51. error_log("Error running Dimension Hole Dungeon run script: " . $e->getMessage());
  52. http_response_code(500);
  53. return 500;
  54. }
  55. if ($ret != 201){
  56. try{
  57. http_response_code($ret);
  58. return $ret;
  59. }
  60. catch(Exception $e) {
  61. error_log("Dimension Hole Dungeon run script returned an unexpected value $ret: " . $e->getMessage());
  62. http_response_code(500);
  63. return 500;
  64. }
  65. }
  66. // At this point, status code should be 200
  67. http_response_code($ret);
  68. return $ret;
  69. }
  70. catch(Exception $e) {
  71. error_log("Unknown error parsing Dimension Hole Dungeon run: " . $e->getMessage());
  72. http_response_code(500);
  73. return 500;
  74. }
  75. ?>