| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /**
- * Logbook logger script.
- *
- * Exposes an API to update the logbook for player data and records.
- * Reads post data and calls the update_logbook.py script.
- * Mandatory POST parameters are:
- * - data: Received JSON file after a run.
- * - key: User API key.
- *
- * @category API
- */
- global $db;
- try{
- // Check data
- $request = filter_input(INPUT_POST, 'request');
- if ($request == null || $request == false){
- http_response_code(400);
- return 400;
- }
- $response = filter_input(INPUT_POST, 'response');
- if ($response == null || $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($request);
- if ($json_request === null){
- http_response_code(400);
- return 400;
- }
- $json_response = json_decode($response);
- if ($json_response === 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 scenario run parser script
- $cmd = __DIR__ . "/bin/update_collection.py " . $key . " " . escapeshellarg($request). " " . escapeshellarg($response);
- $out = [];
- $ret = 0;
- try{
- exec($cmd, $out, $ret);
- }
- catch(Exception $e) {
- error_log("Error running collection update 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("Collection update 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 updating collection: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- ?>
|