-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
63 lines (47 loc) · 1.78 KB
/
index.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
<?php
use Nette\Diagnostics\Debugger,
Nette\Application\Routers\Route;
define('LIBS_DIR', __DIR__ . '/libs');
define('WWW_DIR', __DIR__);
define('FILES_DIR', __DIR__ . '/file');
function sanitizePath($path)
{
$path = preg_replace('~(\.\.)+~', '', $path); // This is not necessary
$path = preg_replace('~/+~', '', $path);
return $path;
}
// Load libraries
require LIBS_DIR . '/Nette/loader.php';
// Enable Nette Debugger for error visualisation & logging
Debugger::$logDirectory = __DIR__ . '/log';
Debugger::enable();
// Configure application
$configurator = new Nette\Config\Configurator;
$configurator->setTempDirectory(__DIR__ . '/temp');
$container = $configurator->createContainer();
// Setup router
// Homepage and about
$container->router[] = new Route('<page (|about)>', function($presenter, $page) {
$page = $page ?: 'default';
return $presenter->createTemplate()->setFile(__DIR__ . '/app/' . $page . '.latte');
});
$container->router[] = new Route('process', function($presenter) {
require __DIR__ . '/app/PngCompressor.php';
/* Accept input */
// Never trust user input
$filename = trim($_SERVER['HTTP_X_FILE_NAME']);
$filename = sanitizePath($filename);
$filepath = FILES_DIR . '/' . $filename;
// Save file
$file = file_get_contents("php://input");
file_put_contents($filepath, $file);
/* Process and send result */
$response = PngCompressor::compress($filepath, $filename);
return new Nette\Application\Responses\JsonResponse($response);
});
// Download
$container->router[] = new Route('download/<filename>', function($presenter, $filename) {
return new Nette\Application\Responses\FileResponse(FILES_DIR . '/' . sanitizePath($filename));
});
// Run the application!
$container->application->run();