update_collection.php 2.6 KB

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