| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?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');
- $data_start_request = filter_input(INPUT_POST, 'start_request');
- $data_start_response = filter_input(INPUT_POST, 'start_response');
- if (
- $data_request == null ||
- $data_request == false ||
- $data_response == null ||
- $data_response == false ||
- $data_start_request == null ||
- $data_start_request == false ||
- $data_start_response == null ||
- $data_start_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"};
- $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 dungeon run parser script
- $cmd = __DIR__ . "/bin/upload_run_rift.py " .
- $key . " " .
- escapeshellarg($data_request) . " " .
- escapeshellarg($data_response) . " " .
- escapeshellarg($data_start_request) . " " .
- escapeshellarg($data_start_response);
- $out = [];
- $ret = 0;
- try{
- exec($cmd, $out, $ret);
- }
- catch(Exception $e) {
- error_log("Error running Rift Dungeon run script: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- if ($ret != 201){
- try{
- error_log("Rift Dungeon run script returned an unexpected value $ret: ");
- http_response_code($ret);
- return $ret;
- }
- catch(Exception $e) {
- error_log("Rift Dungeon run script returned an unexpected value $ret: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- }
- // At this point, status code should be 201
- http_response_code($ret);
- return $ret;
- }
- catch(Exception $e) {
- error_log("Unknown error parsing Rift Dungeon run: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- ?>
|