/
AppRouter.php
289 lines (250 loc) · 7.72 KB
/
AppRouter.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
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2018-2022 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\App;
use FacturaScripts\Core\Base\MyFilesToken;
use FacturaScripts\Core\Base\PluginManager;
/**
* Description of AppRouter
*
* @author Carlos García Gómez <carlos@facturascripts.com>
*/
final class AppRouter
{
/**
* Path to list of routes stored on file.
*/
const ROUTE_LIST_FILE = FS_FOLDER . DIRECTORY_SEPARATOR . 'MyFiles' . DIRECTORY_SEPARATOR . 'routes.json';
/**
* List of routes.
*
* @var array
*/
private $routes;
/**
* AppRouter constructor.
*/
public function __construct()
{
if (false === defined('FS_ROUTE')) {
define('FS_ROUTE', '');
}
$this->routes = $this->loadFromFile();
}
/**
* Clear the App routes.
*/
public function clear()
{
$this->routes = [];
$this->save();
}
/**
* Return the specific App controller for any kind of petition.
*
* @return App
*/
public function getApp()
{
$uri = $this->getUri();
if ('/api' === $uri || '/api/' === substr($uri, 0, 5)) {
return new AppAPI($uri);
}
if ('/cron' === $uri) {
return new AppCron($uri);
}
if ('/deploy' === $uri) {
$this->deploy();
}
foreach ($this->routes as $key => $data) {
if ($uri === $key) {
return $this->newAppController($uri, $data['controller']);
}
if ('*' !== substr($key, -1)) {
continue;
}
if (0 === strncmp($uri, $key, strlen($key) - 1)) {
return $this->newAppController($uri, $data['controller']);
}
}
return $this->newAppController($uri);
}
/**
* Return true if can output a file, false otherwise.
*
* @return bool
*/
public function getFile(): bool
{
$uri = $this->getUri();
$filePath = FS_FOLDER . urldecode($uri);
// favicon.ico
if ('/favicon.ico' == $uri) {
$filePath = FS_FOLDER . '/Dinamic/Assets/Images/favicon.ico';
header('Content-Type: ' . $this->getMime($filePath));
readfile($filePath);
return true;
}
// Not a file? Not a safe file?
if (false === is_file($filePath) || false === $this->isFileSafe($filePath)) {
return false;
}
// Allowed folder?
$allowedFolders = ['node_modules', 'vendor', 'Dinamic', 'Core', 'Plugins', 'MyFiles/Public'];
foreach ($allowedFolders as $folder) {
if ('/' . $folder === substr($uri, 0, 1 + strlen($folder))) {
$this->download($filePath);
return true;
}
}
// MyFiles and token?
$token = filter_input(INPUT_GET, 'myft');
$fixedFilePath = substr(urldecode($uri), 1);
if ('/MyFiles/' === substr($uri, 0, 9) && $token && MyFilesToken::validate($fixedFilePath, $token)) {
$this->download($filePath);
return true;
}
return false;
}
/**
* @param string $filePath
*
* @return bool
*/
public static function isFileSafe(string $filePath): bool
{
$parts = explode('.', $filePath);
$safe = [
'avi', 'css', 'csv', 'eot', 'gif', 'gz', 'ico', 'jpeg', 'jpg', 'js',
'json', 'map', 'mkv', 'mp4', 'ogg', 'pdf', 'png', 'sql', 'svg',
'ttf', 'webm', 'woff', 'woff2', 'xls', 'xlsx', 'xml', 'xsig', 'zip'
];
return empty($parts) || count($parts) === 1 || in_array(end($parts), $safe, true);
}
/**
* Adds this route to the ap routes.
*
* @param string $newRoute
* @param string $controllerName
* @param string $optionalId
* @param bool $checkOptionalId
*/
public function setRoute(string $newRoute, string $controllerName, string $optionalId = '', bool $checkOptionalId = true)
{
if (!empty($optionalId) && $checkOptionalId) {
// if optionalId, then remove previous items with that data
foreach ($this->routes as $route => $routeItem) {
if ($routeItem['controller'] === $controllerName && $routeItem['optionalId'] === $optionalId) {
unset($this->routes[$route]);
}
}
}
$this->routes[$newRoute] = [
'controller' => $controllerName,
'optionalId' => $optionalId
];
$this->save();
}
/**
* Deploy all dynamic files.
*/
private function deploy()
{
if (false === file_exists(FS_FOLDER . DIRECTORY_SEPARATOR . 'Dinamic')) {
$pluginManager = new PluginManager();
$pluginManager->deploy();
}
}
private function download(string $filePath)
{
header('Content-Type: ' . $this->getMime($filePath));
// disable the buffer if enabled
if (ob_get_contents()) {
ob_end_flush();
}
// force to download svg files to prevent XSS attacks
if (strpos($filePath, '.svg') !== false) {
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
}
readfile($filePath);
}
/**
* Return the mime type from given file.
*
* @param string $filePath
*
* @return string
*/
private function getMime(string $filePath): string
{
if ('.css' === substr($filePath, -4)) {
return 'text/css';
}
if ('.js' === substr($filePath, -3)) {
return 'application/javascript';
}
return mime_content_type($filePath);
}
/**
* Return the uri from the request.
*
* @return bool|string
*/
private function getUri()
{
$uri = filter_input(INPUT_SERVER, 'REQUEST_URI');
$uri2 = is_null($uri) ? filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL) : $uri;
$uriArray = explode('?', $uri2);
return substr($uriArray[0], strlen(FS_ROUTE));
}
/**
* Returns an array with the list of plugins in the plugin.list file.
*
* @return array
*/
private function loadFromFile(): array
{
if (file_exists(self::ROUTE_LIST_FILE)) {
$content = file_get_contents(self::ROUTE_LIST_FILE);
if ($content !== false) {
return json_decode($content, true);
}
}
return [];
}
/**
* @param string $uri
* @param string $pageName
*
* @return App
*/
private function newAppController(string $uri, string $pageName = '')
{
return FS_DEBUG ? new AppDebugController($uri, $pageName) : new AppController($uri, $pageName);
}
/**
* Save the routes in a file.
*
* @return void
*/
private function save(): void
{
$content = json_encode($this->routes);
file_put_contents(self::ROUTE_LIST_FILE, $content);
}
}