-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
69 lines (58 loc) · 1.92 KB
/
api.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
<?php
if (!isset($_SERVER['CONTENT_TYPE']) || stripos($_SERVER['CONTENT_TYPE'], 'application/json') === FALSE) {
http_response_code(500);
die('Content-Type must be application/json.');
}
$ini_config = parse_ini_file('config.ini', FALSE);
if ($ini_config === FALSE) {
http_response_code(500);
die('Config file not found.');
}
if (empty($_GET['api_token'])) {
http_response_code(403);
die('GET parameter "api_token" missing.');
}
if ($_GET['api_token'] !== $ini_config['api_token']) {
http_response_code(403);
die('Wrong API token.');
}
$body = json_decode(file_get_contents("php://input"), true);
if (!is_array($body)) {
http_response_code(500);
die('Failed to decode JSON object.');
}
if (!isset($_GET['id'])) {
http_response_code(404);
die('GET parameter "id" missing.');
}
$id = $_GET['id'];
$id_configs = json_decode(@file_get_contents('ids.json'), true);
$config = in_array($id, array_keys($id_configs)) ? $id_configs[$id] : ['separator' => ',', 'filepath' => "data/{$id}.csv"];
switch(strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'POST':
$text = implode($config['separator'], $body['fields']);
file_put_contents($config['filepath'], "$text\n", FILE_APPEND | LOCK_EX);
http_response_code(200);
exit;
case 'DELETE':
if (!isset($body['id']) || !isset($body['columnIdx']) || !is_int($body['columnIdx'])) {
http_response_code(404);
die('Body parameter(s) missing.');
}
$lines = explode("\n", @file_get_contents($config['filepath']));
$new_lines = [];
foreach ($lines as $line) {
$fields = explode($config['separator'], $line);
if ($body['columnIdx'] < count($fields) && $fields[$body['columnIdx']] === $body['id']) {
continue;
}
$new_lines[] = $line;
}
$text = implode("\n", $new_lines);
file_put_contents($config['filepath'], $text, LOCK_EX);
http_response_code(200);
exit;
default:
http_response_code(400);
die('Invalid request method ' . $_SERVER['REQUEST_METHOD']);
}