-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
get.php
298 lines (270 loc) · 9.15 KB
/
get.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
// written by sqall
// twitter: https://twitter.com/sqall01
// blog: https://h4des.org/blog
// github: https://github.com/sqall01
//
// Licensed under the GNU Affero General Public License, version 3.
require_once(__DIR__ . "/config/config.php");
require_once(__DIR__ . "/lib/helper.php");
require_once(__DIR__ . "/lib/objects.php");
// Set global settings.
header("Content-type: application/json");
date_default_timezone_set("UTC");
// Start session.
$cookie_conf = session_get_cookie_params();
session_set_cookie_params($cookie_conf["lifetime"], // lifetime
$cookie_conf["path"], // path
$cookie_conf["domain"], // domain
TRUE, // secure
TRUE); // httponly
session_start();
// Check if needed data is given.
if(!isset($_GET["mode"])) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Mode not set.";
die(json_encode($result));
}
if($_GET["mode"] !== "devices"
&& !isset($_GET["device"])) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Device is not set.";
die(json_encode($result));
}
$mysqli = new mysqli(
$config_mysql_server,
$config_mysql_username,
$config_mysql_password,
$config_mysql_database,
$config_mysql_port);
if($mysqli->connect_errno) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->connect_error;
die(json_encode($result));
}
// Get user id.
$user_id = auth_user($mysqli);
if($user_id === -1 || $user_id === -4) {
chasr_session_destroy();
$result = array();
$result["code"] = ErrorCodes::AUTH_ERROR;
$result["msg"] = "Wrong user or password.";
die(json_encode($result));
}
else if($user_id === -2) {
chasr_session_destroy();
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "Database error during authentication.";
die(json_encode($result));
}
else if($user_id === -3) {
chasr_session_destroy();
$result = array();
$result["code"] = ErrorCodes::SESSION_EXPIRED;
$result["msg"] = "Authenticated session expired.";
die(json_encode($result));
}
// Check if the mode is supported.
switch($_GET["mode"]) {
case "last":
case "view":
case "devices":
break;
default:
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Mode unknown.";
die(json_encode($result));
}
// Fetch data.
$fetched_data_result = NULL;
$device_name = NULL;
if(isset($_GET["device"])) {
$device_name = $_GET["device"];
}
switch($_GET["mode"]) {
case "last":
// Get limit of gps positions.
$limit = 1;
if(isset($_GET["limit"])) {
$limit = intval($_GET["limit"]);
}
if($limit < 1) {
$limit = 1;
}
if($limit > 1000) {
$limit = 1000;
}
// Get id of device.
$device_id = get_device_id($mysqli, $user_id, $_GET["device"]);
if($device_id === -1) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Device does not exist.";
die(json_encode($result));
}
else if($device_id === -2) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "Error while fetching device id.";
die(json_encode($result));
}
// Get gps positions.
$select_gps = "SELECT "
. "utctime,"
. "iv,"
. "latitude,"
. "longitude,"
. "altitude,"
. "speed, "
. "authtag "
. "FROM chasr_gps "
. "WHERE users_id="
. intval($user_id)
. " AND device_id="
. intval($device_id)
. " ORDER BY utctime DESC"
. " LIMIT "
. $limit;
$fetched_data_result = $mysqli->query($select_gps);
if(!$fetched_data_result) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->error;
die(json_encode($result));
}
break;
case "view":
// Check if the time interval is given.
if(!isset($_GET["start"])
|| !isset($_GET["end"])) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Time interval not set.";
die(json_encode($result));
}
// Get id of device.
$device_id = get_device_id($mysqli, $user_id, $_GET["device"]);
if($device_id === -1) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Device does not exist.";
die(json_encode($result));
}
else if($device_id === -2) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "Error while fetching device id.";
die(json_encode($result));
}
// Get gps positions.
$select_gps = "SELECT "
. "utctime,"
. "iv,"
. "latitude,"
. "longitude,"
. "altitude,"
. "speed, "
. "authtag "
. "FROM chasr_gps "
. "WHERE users_id="
. intval($user_id)
. " AND device_id="
. intval($device_id)
. " AND utctime >= "
. intval($_GET["start"])
. " AND utctime <= "
. intval($_GET["end"])
. " ORDER BY utctime ASC";
$fetched_data_result = $mysqli->query($select_gps);
if(!$fetched_data_result) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->error;
die(json_encode($result));
}
break;
case "devices":
// Get all devices positions.
$select_devices = "SELECT DISTINCT "
. "name "
. "FROM chasr_device "
. "WHERE users_id="
. intval($user_id)
. " ORDER BY name ASC";
$fetched_data_result = $mysqli->query($select_devices);
if(!$fetched_data_result) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->error;
die(json_encode($result));
}
break;
default:
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Mode unknown.";
die(json_encode($result));
}
if($fetched_data_result === NULL) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "No data.";
die(json_encode($result));
}
// Get users acls.
$acls = get_user_acl($mysqli, $user_id);
if($acls === -1) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "Error while fetching user acls.";
die(json_encode($result));
}
// Determine how many device slots a user has.
$num_device_slots = $config_chasr_num_min_devices;
if(in_array(AclCodes::CHASR_MID_DEVICES, $acls)) {
$num_device_slots = $config_chasr_num_mid_devices;
}
if(in_array(AclCodes::CHASR_MAX_DEVICES, $acls)) {
$num_device_slots = $config_chasr_num_max_devices;
}
// Prepare data array to return.
switch($_GET["mode"]) {
case "devices":
$devices_data = array();
while($row = $fetched_data_result->fetch_assoc()) {
$element = array("device_name" => $row["name"]);
// Append element to array.
$devices_data[] = $element;
}
$output_data = array();
$output_data["devices"] = $devices_data;
$output_data["avail_slots"] = $num_device_slots;
break;
default:
$location_data = array();
while($row = $fetched_data_result->fetch_assoc()) {
$element = array("device_name" => $device_name,
"utctime" => intval($row["utctime"]),
"iv" => $row["iv"],
"lat" => $row["latitude"],
"lon" => $row["longitude"],
"alt" => $row["altitude"],
"speed" => $row["speed"],
"authtag" => $row["authtag"]);
// Append element to array.
$location_data[] = $element;
}
$output_data = array();
$output_data["locations"] = $location_data;
}
$result = array();
$result["code"] = ErrorCodes::NO_ERROR;
$result["data"] = $output_data;
$result["msg"] = "Success.";
echo json_encode($result);
?>