117 lines
3.7 KiB
PHP
Raw Normal View History

2017-01-30 21:36:44 +01:00
<?php
/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
2017-04-07 00:05:28 +02:00
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2017-01-30 21:36:44 +01:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
2017-04-07 00:05:28 +02:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
2017-01-30 21:36:44 +01:00
*/
2017-04-09 23:35:55 +02:00
/**
* Set response error status and message
*
* @param array $response Respons
* @param string $message Message
*/
2017-01-30 21:36:44 +01:00
function setError(&$response, $message) {
2017-04-09 23:35:55 +02:00
$response['error'] = true;
2017-01-30 21:36:44 +01:00
$response['message'] = $message;
}
2017-04-09 23:35:55 +02:00
define("headless", true);
define("client", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
2017-01-30 21:36:44 +01:00
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
$response = [ 'error' => false ];
switch ($action) {
// action: authorize
case "auth":
break;
// action: adduser (currently unused)
2017-01-30 21:36:44 +01:00
case "adduser":
if (!$user->isAdmin) {
setError($response, "User not authorized");
break;
}
2017-01-30 21:36:44 +01:00
$login = isset($_REQUEST['login']) ? $_REQUEST['login'] : NULL;
$pass = isset($_REQUEST['password']) ? $_REQUEST['password'] : NULL;
if (!empty($login) && !empty($pass)) {
$newId = uUser::add($login, $pass);
if ($newId !== false) {
// return user id
$response['userid'] = $newId;
} else {
setError($response, "Server error");
2017-01-30 21:36:44 +01:00
}
} else {
setError($response, "Empty login or password");
2017-01-30 21:36:44 +01:00
}
break;
// action: addtrack
case "addtrack":
$trackName = isset($_REQUEST['track']) ? $_REQUEST['track'] : NULL;
if (empty($trackName)) {
setError($response, "Missing required parameter");
2017-01-30 21:36:44 +01:00
break;
}
2017-04-11 17:00:40 +02:00
require_once(ROOT_DIR . "/helpers/track.php");
$trackId = uTrack::add($user->id, $trackName);
if ($trackId === false) {
setError($response, "Server error");
2017-01-30 21:36:44 +01:00
break;
}
// return track id
$response['trackid'] = $trackId;
2017-01-30 21:36:44 +01:00
break;
// action: addposition
case "addpos":
$lat = isset($_REQUEST["lat"]) ? $_REQUEST["lat"] : NULL;
$lon = isset($_REQUEST["lon"]) ? $_REQUEST["lon"] : NULL;
2017-05-22 13:39:40 +02:00
$timestamp = isset($_REQUEST["time"]) ? $_REQUEST["time"] : NULL;
2017-01-30 21:36:44 +01:00
$altitude = isset($_REQUEST["altitude"]) ? $_REQUEST["altitude"] : NULL;
$speed = isset($_REQUEST["speed"]) ? $_REQUEST["speed"] : NULL;
$bearing = isset($_REQUEST["bearing"]) ? $_REQUEST["bearing"] : NULL;
$accuracy = isset($_REQUEST["accuracy"]) ? $_REQUEST["accuracy"] : NULL;
$provider = isset($_REQUEST["provider"]) ? $_REQUEST["provider"] : NULL;
$comment = isset($_REQUEST["comment"]) ? $_REQUEST["comment"] : NULL;
$imageId = isset($_REQUEST["imageid"]) ? $_REQUEST["imageid"] : NULL;
$trackId = isset($_REQUEST["trackid"]) ? $_REQUEST["trackid"] : NULL;
2017-01-30 21:36:44 +01:00
if (!is_numeric($lat) || !is_numeric($lon) || !is_numeric($timestamp) || !is_numeric($trackId)) {
setError($response, "Missing required parameter");
2017-01-30 21:36:44 +01:00
break;
}
2017-04-11 17:00:40 +02:00
require_once(ROOT_DIR . "/helpers/position.php");
$positionId = uPosition::add($user->id, $trackId,
2017-05-22 13:39:40 +02:00
$timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
2017-04-09 23:35:55 +02:00
if ($positionId === false) {
setError($response, "Server error");
2017-01-30 21:36:44 +01:00
}
break;
default:
setError($response, "Unknown command");
break;
}
2017-01-30 21:36:44 +01:00
header('Content-Type: application/json');
echo json_encode($response);
exit();
?>