. */ /** * Exit with error status and message * * @param string $message Message */ function exitWithError($message) { $response = []; $response['error'] = true; $response['message'] = $message; header('Content-Type: application/json'); echo json_encode($response); exit(); } /** * Exit with success status * * @param array $params Optional params * @return void */ function exitWithSuccess($params = []) { $response = []; $response['error'] = false; header('Content-Type: application/json'); echo json_encode(array_merge($response, $params)); exit(); } require_once(dirname(__DIR__) . "/helpers/auth.php"); $action = uUtils::postString('action'); $auth = new uAuth(); if (!$auth->isAuthenticated() && $action != "auth") { $auth->sendUnauthorizedHeader(); exitWithError("Unauthorized"); } switch ($action) { // action: authorize case "auth": $login = uUtils::postString('user'); $pass = uUtils::postPass('pass'); if ($auth->checkLogin($login, $pass)) { exitWithSuccess(); } else { $auth->sendUnauthorizedHeader(); exitWithError("Unauthorized"); } break; // action: adduser (currently unused) case "adduser": if (!$auth->user->isAdmin) { exitWithError("Not allowed"); } $login = uUtils::postString('login'); $pass = uUtils::postPass('password'); if (empty($login) || empty($pass)) { exitWithError("Empty login or password"); } $newId = uUser::add($login, $pass); if ($newId === false) { exitWithError("Server error"); } exitWithSuccess(['userid'=> $newId]); break; // action: addtrack case "addtrack": $trackName = uUtils::postString('track'); if (empty($trackName)) { exitWithError("Missing required parameter"); } require_once(ROOT_DIR . "/helpers/track.php"); $trackId = uTrack::add($auth->user->id, $trackName); if ($trackId === false) { exitWithError("Server error"); } // return track id exitWithSuccess(['trackid' => $trackId]); break; // action: addposition case "addpos": $lat = uUtils::postFloat('lat'); $lon = uUtils::postFloat('lon'); $timestamp = uUtils::postInt('time'); $altitude = uUtils::postFloat('altitude'); $speed = uUtils::postFloat('speed'); $bearing = uUtils::postFloat('bearing'); $accuracy = uUtils::postInt('accuracy'); $provider = uUtils::postString('provider'); $comment = uUtils::postString('comment'); $imageMeta = uUtils::requestFile('image'); $trackId = uUtils::postInt('trackid'); if (!is_float($lat) || !is_float($lon) || !is_int($timestamp) || !is_int($trackId)) { exitWithError("Missing required parameter"); } $image = null; if (!empty($imageMeta)) { $image = uUpload::add($imageMeta, $trackId); } require_once(ROOT_DIR . "/helpers/position.php"); $positionId = uPosition::add($auth->user->id, $trackId, $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $image); if ($positionId === false) { exitWithError("Server error"); } exitWithSuccess(); break; default: exitWithError("Unknown command"); break; } ?>