forked from contao/check
/
index.php
106 lines (86 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
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
<?php
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2015 Leo Feyer
*
* @license LGPL-3.0+
*/
require 'controller/bootstrap.php';
/**
* Route the request to a controller
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class Router
{
/**
* Dispatch a request and send the response
*
* @throws RuntimeException If the command name is invalid
*/
public static function dispatch()
{
// Default command
$file = 'controller/index.php';
$class = 'Index';
$command = filter_var($_GET['c'], FILTER_SANITIZE_STRING);
// Check the command (thanks to Arnaud Buchoux)
if (static::isInsecurePath($command)) {
throw new RuntimeException("Invalid command $command");
}
// Custom command
if ($command != '' && file_exists("controller/$command.php")) {
$file = "controller/$command.php";
$class = str_replace(' ', '', ucwords(str_replace('-', ' ', $command)));
}
include $file;
$controller = new $class();
$controller->run();
}
/**
* Insecure path potentially containing directory traversal
*
* @param string $path The file path
*
* @return boolean True if the file path is insecure
*/
public static function isInsecurePath($path)
{
// Normalize backslashes
$path = str_replace('\\', '/', $path);
$path = preg_replace('#//+#', '/', $path);
// Equals ..
if ($path == '..')
{
return true;
}
// Begins with ./
if (substr($path, 0, 2) == './')
{
return true;
}
// Begins with ../
if (substr($path, 0, 3) == '../')
{
return true;
}
// Ends with /.
if (substr($path, -2) == '/.')
{
return true;
}
// Ends with /..
if (substr($path, -3) == '/..')
{
return true;
}
// Contains /../
if (strpos($path, '/../') !== false)
{
return true;
}
return false;
}
}
Router::dispatch();