| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- /**
- * Scenario run logger script.
- *
- * Exposes an API to save a run to the database.
- * Reads post data and calls the upload_scenario_run.py script.
- * Mandatory POST parameters are:
- * - data: Received JSON file after a run.
- * - key: User API key.
- *
- * @category API
- */
- global $db;
- try{
- // Check data
- $data = filter_input(INPUT_POST, 'data');
- if ($data == null || $data == 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 = json_decode($data);
- if ($json === null){
- http_response_code(400);
- return 400;
- }
- // Authenticate
- $uid = $json->{"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 scenario run parser script
- $cmd = __DIR__ . "/bin/upload_run_scenario.py " . $key . " " . escapeshellarg($data);;
- $out = [];
- $ret = 0;
- try{
- exec($cmd, $out, $ret);
- }
- catch(Exception $e) {
- error_log("Error running scenario run script '$cmd': " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- if ($ret != 200){
- try{
- http_response_code($ret);
- return $ret;
- }
- catch(Exception $e) {
- error_log("Dungeon run script '$cmd' 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 scenario run: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- ?>
|