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
<?php
define('PATH', __DIR__);
define('STORAGE_PATH', (getenv('THINGS_STORAGE_PATH') ? getenv('THINGS_STORAGE_PATH') : __DIR__));
require_once PATH.'/lib/helpers.php';
require_once PATH.'/lib/Router.php';
require_once PATH.'/lib/LibraryRenderer.php';
require_once PATH.'/lib/TinyHtmlMinifier.php';
require_once PATH.'/lib/Mustache/Autoloader.php';
Mustache_Autoloader::register();
$endpoints = [
'editor' => function() {
exit(file_get_contents('editor.html'));
},
'renderList' => function ($id = NULL) {
global $storage;
if ( $storage['library'] === "" ) exit("No data yet!");
$library = json_decode($storage['library'], true);
$listIds = array_column($library['lists'], 'id');
if ( array_search($id, $listIds) === false ) {
header('Location: /list/'.$library['defaultListId']);
exit();
}
$tpl = new Mustache_Engine(['loader' => new Mustache_Loader_FilesystemLoader(PATH.'/templates')]);
$minifier = new TinyHtmlMinifier([]);
$templateData = new LibraryRenderer($library, (int) $id);
exit($minifier->minify($tpl->render('view', $templateData)));
},
'exportCSV' => function ($id) {
global $storage;
if ( !isset($_COOKIE['lp']) || $_COOKIE['lp'] !== $storage['sessionCookie'] ) {
response(401, 'Invalid session');
} else {
$mgToWeight = function(int $value, string $unit) {
if ( $unit === 'g' ) {
return number_format(($value / 1000), 0);
} elseif ( $unit === 'kg' ) {
return number_format(($value / 1000000), 2);
}
};
$library = json_decode($storage['library'], true);
$categoryIds = array_column($library['categories'], 'id');
$itemIds = array_column($library['items'], 'id');
$listIds = array_column($library['lists'], 'id');
$listId = array_search($id, $listIds);
if ( $listId === false ) exit('List not found!');
$output = fopen('php://output', 'w');
$list = $library['lists'][$listId];
header('Content-type: text/csv');
fputcsv($output, ['name','category', 'description', 'qty', 'weight', 'unit', 'url', 'price', 'isWorn', 'isConsumable']);
foreach ( $list['categoryIds'] as $id ) {
$categoryId = array_search($id, $categoryIds);
$category = $library['categories'][$categoryId];
foreach ( $category['categoryItems'] as $item ) {
$itemId = array_search($item['itemId'], $itemIds);
$item = array_merge($item, $library['items'][$itemId]);
fputcsv($output, [
$item['name'],
$category['name'],
$item['description'],
$item['qty'],
$mgToWeight($item['weight'], $item['authorUnit']),
$item['authorUnit'],
$item['url'],
$item['price'],
(bool)$item['worn'],
(bool)$item['consumable'],
]);
}
}
fclose($output);
}
},
'getLibrary' => function () {
global $storage, $requestBody;
if ( isset($_COOKIE['lp']) && $_COOKIE['lp'] === $storage['sessionCookie'] ) {
response(200, NULL, [
'library' => $storage['library'],
'syncToken' => $storage['syncToken'],
]);
}
if ( !isset($requestBody['password']) || md5($requestBody['password']) !== $storage['password'] ) {
response(404, 'Incorrect credentials!');
} else {
$storage['sessionCookie'] = bin2hex(random_bytes(40));
setcookie("lp", $storage['sessionCookie'], [
'expires' => time() + 3600,
'path' => "/",
'domain' => $_SERVER['HTTP_HOST'],
'secure' => true
]);
file_put_contents(STORAGE_PATH.'/storage.json', json_encode($storage));
response(200, NULL, [
'library' => $storage['library'],
'syncToken' => $storage['syncToken'],
]);
}
},
'saveLibrary' => function () {
global $storage, $requestBody;
if ( !isset($_COOKIE['lp']) || $_COOKIE['lp'] !== $storage['sessionCookie'] ) {
response(401, 'Invalid session');
} else {
if ( isset($requestBody['library']) && isset($requestBody['syncToken']) ) {
if ( $storage['syncToken'] === $requestBody['syncToken'] ) {
$storage['syncToken']++;
$storage['library'] = $requestBody['library'];
file_put_contents(STORAGE_PATH.'/storage.json', json_encode($storage));
response(200, 'success', [
'syncToken' => $storage['syncToken'],
]);
} else {
response(400, 'Your list is out of date - please refresh your browser.');
}
} else {
response(400, 'Missing arguments!');
}
}
},
'changePassword' => function () {
global $storage, $requestBody;
if ( !isset($_COOKIE['lp']) || $_COOKIE['lp'] !== $storage['sessionCookie'] ) {
response(401, 'Invalid session');
} else {
if ( isset($requestBody['currentPassword']) && isset($requestBody['newPassword']) ) {
if ( md5($requestBody['currentPassword']) === $storage['password'] ) {
$storage['password'] = md5($requestBody['newPassword']);
file_put_contents(STORAGE_PATH.'/storage.json', json_encode($storage));
} else {
response(400, 'Incorrect credentials!');
}
} else {
response(400, 'Missing arguments!');
}
}
},
];
$requestBody = json_decode(file_get_contents('php://input'), true);
if ( !file_exists(STORAGE_PATH . '/storage.json') ) {
$storage = [
'password' => 'ae2d699aca20886f6bed96a0425c6168',
'sessionCookie' => '',
'library' => "",
'syncToken' => 0,
];
} else {
$storage = json_decode(file_get_contents(STORAGE_PATH . '/storage.json'), true);
}
Router::add('GET', '/', $endpoints['renderList']);
Router::add('GET', '/list/([0-9]*)', $endpoints['renderList']);
Router::add('GET', '/editor', $endpoints['editor']);
Router::add('GET', '/login', $endpoints['editor']);
Router::add('GET', '/csv/([0-9]*)', $endpoints['exportCSV']);
Router::add('POST', '/getLibrary', $endpoints['getLibrary']);
Router::add('POST', '/saveLibrary', $endpoints['saveLibrary']);
Router::add('POST', '/changePassword', $endpoints['changePassword']);
Router::pathNotFound(function() {
header("Loctaion: /");
});
Router::run('/');