| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- /**
- * Profile uploader script.
- *
- * Exposes an API to save a run to the database.
- * Reads post data and calls the upload_profile.py script.
- * It also saves the data to a JSON file in the data directory.
- * Mandatory POST parameters are:
- * - data: Received JSON file.
- * - key: User API key.
- *
- * @category API
- */
- global $db;
- try{
- // Check data
- $data = filter_input(INPUT_POST, 'response');
- 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"};
- $uname = $json->{"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;
- }
- // Write data file
- $dtime = (new DateTime())->format('Y-m-dTH:i:s');
- $fname = ($_SERVER["DOCUMENT_ROOT"] . "/../data/profile_" . $uid . "_" . $uname . "_" . $dtime . ".json");
- try{
- file_put_contents($fname, $data);
- }
- catch(Exception $e) {
- error_log("Unable to write profile data to '$fname': " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- // Run profile parser script
- $cmd = __DIR__ . "/bin/upload_profile.py " . $key . " " . $fname;
- $out = [];
- $ret = 0;
- try{
- error_log ($cmd);
- exec($cmd, $out, $ret);
- }
- catch(Exception $e) {
- error_log("Error running profile script '$cmd': " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- if ($ret != 204){
- try{
- http_response_code($ret);
- return $ret;
- }
- catch(Exception $e) {
- error_log("Profile 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 profile: " . $e->getMessage());
- http_response_code(500);
- return 500;
- }
- ?>
|