| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * Dungeon run logger script.
- *
- * Exposes an API to save a run to the database.
- * Reads post data and calls the upload_dungeon_run.py script.
- * Mandatory POST parameters are:
- * - request: Intercepted game request.
- * - response: Intercepted game response.
- * - key: User API key.
- *
- * @category API
- */
- global $db;
- try{
- // Check data
- $data_request = filter_input(INPUT_POST, 'request');
- $data_response= filter_input(INPUT_POST, 'response');
- if ($data_request == null || $data_request == false || $data_response == null || $data_response == false){
- http_response_code(400);
- return 400;
- }
- // Check API key.
- $key = filter_input(INPUT_POST, 'key');
- if ($key == null || $key == false){
- http_response_code(401);
- return 401;
- }
- // Check data format.
- $json_request = json_decode($data_request);
- $json_response = json_decode($data_response);
- if ($json_response === null | $json_request === null){
- http_response_code(400);
- return 400;
- }
- // Authenticate
- $uid = $json_request->{"wizard_id"};
- $uname = $json_response->{"wizard_info"}->{"wizard_name"};
- $s = "SELECT COUNT(uid) AS c FROM player WHERE uid = $uid AND api_key = '$key';";
- if (1 != $db->query($s)->fetchArray(SQLITE3_ASSOC)["c"]){
- http_response_code(401);
- return 401;
- }
- // Run TOA run parser script
- $cmd = __DIR__ . "/bin/upload_run_toa.py " . $key . " " . escapeshellarg($data_request) . " " . escapeshellarg($data_response);
- $out = [];
- $ret = 0;
- try{
- exec($cmd, $out, $ret);
- }
- catch(Exception $e) {
- error_log("Error running TOA run script: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- if ($ret != 201){
- try{
- http_response_code($ret);
- return $ret;
- }
- catch(Exception $e) {
- error_log("TOA run script returned an unexpected value $ret: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- }
- // At this point, status code should be 200
- http_response_code($ret);
- return $ret;
- }
- catch(Exception $e) {
- error_log("Unknown error parsing TOA run: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- ?>
|