This repository was archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
329 lines (283 loc) · 8.38 KB
/
action.php
File metadata and controls
329 lines (283 loc) · 8.38 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
header( 'Content-type: application/json; charset=utf-8' );
$DATA_FILE = __DIR__ . '/data/entries.json';
$DATA_LIMIT = 10;
handle_request();
/**
* Handles incoming HTTP requests.
*/
function handle_request() {
global $DATA_FILE;
if (!isset($_POST['type'])) {
die;
}
$list = isset($_POST['list']) && is_numeric($_POST['list']) && strlen($_POST['list']) < 5 ? $_POST['list'] : 0;
$param = '';
switch($_POST['type']) {
case 'onload': // Load watchlist data
echo load_data();
return;
case 'add': // Create new entry
try {
$param = add_entry($list) ? 'succ_add' : 'fail_add';
} catch (Exception $e) {
$param = 'fail_add';
}
break;
case 'data':
echo load_flight_info();
return;
case 'delete':
$param = delete_entry($_POST['id']) ? 'succ_del' : 'fail_del';
echo json_encode(file_get_contents($DATA_FILE));
return;
case 'list':
$a = isset($_POST['action']) && is_string($_POST['action']) && strlen($_POST['action']) < 20 ? $_POST['action'] : '';
switch ($a) {
case 'rename':
$param = rename_list($list, $_POST['newname']) ? 'succ_listren' : 'fail_list';
break;
case 'add':
$param = add_list($_POST['newname']) ? 'succ_listadd' : 'fail_list';
break;
case 'delete':
$param = delete_list($list) ? 'succ_listdel' : 'fail_list';
break;
default:
$param = 'fail_list';
break;
}
break;
case 'state': // Load DOM state
echo load_lists();
return;
}
header('Location: ' . (!empty($_ENV['SERVERURL']) ? $_ENV['SERVERURL'] : 'http://localhost:8000') . '/?' . $param);
http_response_code(303);
}
/**
* Loads all available lists.
*/
function load_lists() {
$file = __DIR__ . '/data/lists.json';
if (!file_exists($file)) {
file_put_contents($file, json_encode(array(array('Default watchlist' => 0)), JSON_PRETTY_PRINT));
}
return json_encode(file_get_contents($file));
}
/**
* Renames a given list.
* @param int $list The ID of the list which should be renamed.
* @param string $newname The new name for the list.
*/
function rename_list($list, $newname) {
if (!is_string($newname) || preg_match('/[^a-zA-Z0-9 ]/', $newname) || strlen($newname) > 50
|| strlen(trim($newname)) < 1) {
return false;
}
$file = __DIR__ . '/data/lists.json';
$data = json_decode(file_get_contents($file));
$new_data = array();
foreach ($data as $json) {
foreach ($json as $key => $val) {
if (strval($val) === strval($list)) {
$new_data[] = array(trim($newname) => $val);
} else {
$new_data[] = array($key => $val);
}
}
}
file_put_contents($file, json_encode($new_data, JSON_PRETTY_PRINT));
return true;
}
/**
* Adds a new list.
* @param string $name The name of the list which should be added.
*/
function add_list($name) {
if (!is_string($name) || preg_match('/[^a-zA-Z0-9 ]/', $name) || strlen($name) > 50
|| strlen(trim($name)) < 1) {
return false;
}
$file = __DIR__ . '/data/lists.json';
$data = json_decode(file_get_contents($file));
$last_num = -1;
foreach ($data as $json) {
foreach ($json as $key => $val) {
if (strval($key) === strval($name)) { // Name already existing
return false;
}
if (intval($val) > $last_num) {
$last_num = intval($val);
}
}
}
$data[] = array($name => $last_num + 1);
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));
return true;
}
/**
* Deletes a given list.
* @param int $list The ID of the list which should be deleted.
*/
function delete_list($list) {
$file = __DIR__ . '/data/lists.json';
$data = json_decode(file_get_contents($file));
$new_data = array();
foreach ($data as $json) {
foreach ($json as $key => $val) {
if (strval($val) !== strval($list)) {
$new_data[] = array($key => $val);
}
}
}
file_put_contents($file, json_encode($new_data, JSON_PRETTY_PRINT));
return true;
}
/**
* Loads available data.
* @return array Array of JSON objects for each entry.
*/
function load_data() {
global $DATA_FILE;
if (!file_exists($DATA_FILE)) {
return json_encode(array());
} else {
return json_encode(file_get_contents($DATA_FILE));
}
}
/**
* Adds a new entry to the watchlist.
* @param int $list ID of the list to which data should be added.
* @return bool True if entry was added successfully, else false.
*/
function add_entry($list) {
global $DATA_FILE;
if (!isset($_POST['from']) || !isset($_POST['to'])
|| !isset($_POST['depart']) || !isset($_POST['return'])
|| !isset($_POST['cabin']) || !isset($_POST['travellers'])
) {
return false;
}
if (!is_string($_POST['from']) || preg_match('/[^a-zA-Zöäüß ]/', $_POST['from']) || strlen($_POST['from']) > 50
|| !is_string($_POST['to']) || preg_match('/[^a-zA-Zöäüß ]/', $_POST['to']) || strlen($_POST['to']) > 50
|| !is_string($_POST['depart']) || !preg_match('/\d\d\d\d-\d\d-\d\d/', $_POST['depart'])
|| !is_string($_POST['return']) || !preg_match('/\d\d\d\d-\d\d-\d\d/', $_POST['return'])
|| !is_string($_POST['cabin']) || !in_array($_POST['cabin'], array('Economy', 'Premium Economy', 'Business class', 'First class'))
|| !is_numeric($_POST['travellers']) || preg_match('/[^0-9]/', $_POST['travellers']) || strlen($_POST['travellers']) > 1
|| preg_match('/[^0-9]/', $_POST['threshold']) || strlen($_POST['threshold']) > 6
) {
return false;
}
$timestamp = time();
$data = array(
'id' => sha1($timestamp),
'date' => strval($timestamp),
'list' => $list,
'from' => $_POST['from'],
'to' => $_POST['to'],
'depart' => $_POST['depart'],
'return' => $_POST['return'],
'cabin' => $_POST['cabin'],
'travellers' => $_POST['travellers'],
'threshold' => strlen($_POST['threshold']) > 0 ? $_POST['threshold'] : 0
);
if (!file_exists($DATA_FILE)) {
$content = array();
} else {
$content = json_decode(file_get_contents($DATA_FILE), true);
}
$content[] = $data;
file_put_contents($DATA_FILE, json_encode($content, JSON_PRETTY_PRINT));
return true;
}
/**
* Removes an entry from the watchlist.
* @param string $id The ID of the entry which should be deleted.
*/
function delete_entry($id) {
global $DATA_FILE;
if (!isset($id) || !is_string($id) || strlen($id) > 50) {
return false;
}
if (!file_exists($DATA_FILE)) {
return false;
} else {
$data = json_decode(file_get_contents($DATA_FILE), true);
$new_data = array();
foreach ($data as $entry) {
if (strval($entry['id']) !== strval($id)) {
$new_data[] = $entry;
}
}
file_put_contents($DATA_FILE, json_encode($new_data, JSON_PRETTY_PRINT));
return true;
}
}
/**
* Loads available information for currently watched flights.
* @return array Array of JSON objects for each entry.
*/
function load_flight_info() {
global $DATA_FILE;
$flight_info = array();
$entries = json_decode(file_get_contents($DATA_FILE), true);
foreach ($entries as $entry) {
$file = __DIR__ . '/data/' . strval($entry['id']) . '.json';
if (file_exists($file)) {
$info = filter_flight_info(json_decode(file_get_contents($file), true));
$info['id'] = strval($entry['id']);
$flight_info[] = $info;
}
}
return json_encode($flight_info);
}
/**
* Filters flight information such that the amount of sent data does not get too large.
* @param array $flight_info The flight information to be filtered.
* @return array The filtered flight information.
*/
function filter_flight_info($flight_info) {
global $DATA_LIMIT;
$filter_keys = array();
$latest_info = get_last_not_empty($flight_info);
foreach ($latest_info as $key => $val) {
if (isset($val['airlines']) && isset($val['time'])) {
$filter_keys[] = implode(', ', $val['airlines']) . $val['time'];
}
if ($DATA_LIMIT <= $key + 1) {
break;
}
}
$filtered_info = array();
foreach ($flight_info as $key => $val) {
$filtered_info[$key] = array();
foreach ($val as $entry) {
if (!isset($entry['airlines']) && !isset($entry['time'])) {
$filtered_info[$key][] = $entry;
} else {
$composed_key = implode(', ', $entry['airlines']) . $entry['time'];
if (in_array($composed_key, $filter_keys)) {
$filtered_info[$key][] = $entry;
}
}
}
}
return $filtered_info;
}
/**
* Returns the latest flight information. If no information exists, it returns an empty array.
* @param array $array The flight information array.
* @return array The latest flight information.
*/
function get_last_not_empty(array $array) : array {
$last = end($array);
while (empty($last)) {
$last = prev($array);
if ($last === false && key($array) === NULL) {
return array();
}
}
return $last;
}
?>