/
geo5.php
277 lines (249 loc) · 8.21 KB
/
geo5.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
<?php
// 2013-07-19: Checked in at soureforge under . GPLv3
// test from new environment
include 'util.php';
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
// Every request must have a userAnonym
$user = getParam('user');
if (isNullOrEmptyString($user)) {
setServerError("GET/POST parameter 'user' is missing.");
return;
}
// New Password
$isUserAccepted = getParam('isUserAccepted');
if (!isNullOrEmptyString($isUserAccepted)) {
if (!isUserAccepted($user, getParam('pass'))) {
setServerError("User not accepted, user $user.");
return;
}
setServerResponse("$user");
return;
}
//Accept only user with valid password
$pass = getParam('pass');
if (!checkUser($user, $pass)) {
setServerError("Format of user name OR password no accepted for user $user.");
return;
}
// New Password
$passNew = getParam('passNew');
if (!isNullOrEmptyString($passNew)) {
if (!changePassword($user, $pass, $passNew)) {
setServerError("Failed to change password for user $user.");
return;
}
setServerResponse("Ok, changed password for user $user.");
return;
}
// New Group
$groupNew = getParam('groupNew');
if (!isNullOrEmptyString($groupNew)) {
if (!changeGroup($user, $pass, $groupNew)) {
setServerError("Failed to change group for user $user.");
return;
}
setServerResponse("Ok, changed group for user $user.");
return;
}
// Remove user
$remove = getParam('remove');
if (!isNullOrEmptyString($remove)) {
// any value is accepted (no need to set 'true' or 'yes' like 'remove=true')
if (!removeUser($user, $pass)) {
setServerError("Failed to remove user $user.");
return;
}
setServerResponse("Ok, removed user $user.");
return;
}
// server info for admin only
$serverInfo = getParam('serverInfo');
if (!isNullOrEmptyString($serverInfo)) {
$info = getServerInfo($user);
if ($info == '') {
setServerError("Failed to get details on the server for user $user.");
return;
}
setServerResponse($info);
return;
}
// server info for admin only
$phpInfo = getParam('phpInfo');
if (!isNullOrEmptyString($phpInfo)) {
if (! preg_match('/admin/i', $user)) {
setServerError("Failed to get php infos (not allowed) for user $user.");
return;
}
phpinfo();
return;
}
// set configurations (expiration day, timezone offset, write tracks,...)
$setConfiguration = getParam('set_configuration');
if (!isNullOrEmptyString($setConfiguration)) {
if (!setConfigurationEnBloc($user, $setConfiguration)) {
setServerError("Failed to write postion for user $user.");
return;
}
setServerResponse("Ok, server wrote configuration for user $user.");
return;
}
// get configurations (expiration day, timezone offset, write tracks,...)
$getConfiguration = getParam('get_configuration');
if (!isNullOrEmptyString($getConfiguration)) {
$configs = getConfigurations($user);
setServerResponse($configs);
return;
}
// Write single position
$lat = getParam('lat');
if (!isNullOrEmptyString($lat)) {
$lon = getParam('lon');
if (isNullOrEmptyString($lon)) {
setServerError("Missing parameter: lon . You gave lat (latitude) without lon (longitude).");
return;
}
if (!writePostion($user, getParam('group'), $lat, $lon, getParam('bearing'), getParam('speed'), getParam('altitude'), getParam('accuracy'), getParam('time'), getParam('track'))) {
setServerError("Failed to write postion for user $user.");
return;
}
setServerResponse("Ok, server wrote position for user $user.");
return;
}
// Write multiline positions for a user
$positions = getParam('positions');
if (!isNullOrEmptyString($positions)) {
if (!writePostions($user, getParam('group'), $positions, getParam('track'))) {
setServerError("Failed to write postions for user $user.");
return;
}
setServerResponse("Ok, server wrote positions for user $user.");
return;
}
// Get the position of all users belonging to the group of this user of a user
$getGroupPositionsForUser = getParam('getGroupPostions');
if (!isNullOrEmptyString($getGroupPositionsForUser)) {
$currentPositions = getGroupPostions($user);
if (isNullOrEmptyString($currentPositions)) {
setServerError("Failed to get group postions for user $user.");
return;
}
setServerResponse($currentPositions);
return;
}
// Get the position of a user
$getPositionForUser = getParam('getPosition');
if (!isNullOrEmptyString($getPositionForUser)) {
$currentPosition = getPosition($getPositionForUser);
if (isNullOrEmptyString($currentPosition)) {
setServerError("Failed to get postion for user $getPositionForUser.");
return;
}
setServerResponse($currentPosition);
return;
}
// Get the positions of a group
$getPositionsForGroup = getParam('getPositions');
if (!isNullOrEmptyString($getPositionsForGroup)) {
$currentPositions = getPositions($getPositionsForGroup);
if (isNullOrEmptyString($currentPositions)) {
setServerError("Failed to get postions for group $getPositionsForGroup.");
return;
}
setServerResponse($currentPositions);
return;
}
// List the tracks of a user
$userToList = getParam('listTracks');
if (!isNullOrEmptyString($userToList)) {
// clean up old tracks and all inactive users (decay time is set via configuration)
cleanUp($user);
// Now try to find the tracks for the user and all of the group members
$getTimezoneoffset = getParam('timezoneoffset');
$tracks = listTracks($user, $getTimezoneoffset);
// if (isNullOrEmptyString($tracks)) {
// setServerError("Failed to get tracks for user $userToList.");
// return;
// }
$group = getParam('group');
$pass = getParam('pass');
if (!changeGroup($user, $pass, $group)) {
setServerError("Failed to change group for user $user.");
return;
}
setServerResponse($tracks);
return;
}
// Get a track for a user
$getTrackForUser = getParam('getTrack');
if (!isNullOrEmptyString($getTrackForUser)) {
$track = getTrack($getTrackForUser, getParam('date'), getParam('last'));
if (isNullOrEmptyString($track)) {
setServerError("Failed to get tracks for user $getTrackForUser.");
return;
}
setServerResponse($track);
return;
}
// Get all users (of a group) that have a given data file(name)
$getUsersForFileName = getParam('getUsersForFileName');
if (!isNullOrEmptyString($getUsersForFileName)) {
$fileName = getParam('filename');
$users = getUsersForDataFileName($user, $fileName);
if (isNullOrEmptyString($getUsersForFileName)) {
setServerError("Failed to get users for filename $fileName for user $user.");
return;
}
setServerResponse($users);
return;
}
// Get all users (of a group) that have a given data file(name)
$getLastPostionsAndTracks = getParam('getLastPostionsAndTracksIndividually');
if (!isNullOrEmptyString($getLastPostionsAndTracks)) {
// 1. Write the group if given
$group = getParam('group');
// if (!isNullOrEmptyString($group)) {
if (!writeGroupFile($user, $group)) {
setServerError("Failed to change group for user $user.");
return;
}
// }
// 2. Get positionm and tracks
$postions = getLastPostionsAndTracksIndividually($getLastPostionsAndTracks, getParam('track'), $user);
if (isNullOrEmptyString($postions)) {
// setServerError("Failed to get last postions and tracks for $getLastPostionsAndTracks");
// return;
// It might happen that a new user has nothing on the server
$postions = '';
}
setServerResponse($postions);
return;
}
setServerError('Server does not know what to do. Give on of the following parameters: passNew (change password), groupNew (change group), remove (delete user), lat (write one position), postions (write more than one positions), getPosition (get postion of user), getPositions (get postions of group), listTracks (list tracks of user), getTrack (get single track)');
return;
/////////////////////////////////////////////////////////////////////////
// Helpers
/////////////////////////////////////////////////////////////////////////
function setServerResponse($message) {
echo $message;
}
function setServerError($message) {
echo '!Error: ' . $message;
}
function getParam($paramName) {
$paramValue = '';
if (isset($_GET[$paramName])) {
$paramValue = $_GET[$paramName];
} else if(isset($_POST[$paramName])) {
$paramValue = $_POST[$paramName];
}
return $paramValue;
}
function getParam_locally($paramName) {
$paramValue = $_GET[$paramName];
if (isNullOrEmptyString($paramValue)) {
$paramValue = $_POST[$paramName];
}
return $paramValue;
}
?>