-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevServerRouter.php
187 lines (173 loc) · 4.28 KB
/
devServerRouter.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
<?php
// Router for the PHP built in server that shows directory listings.
return call_user_func (function () {
$path = $_SERVER['DOCUMENT_ROOT'] . $_SERVER["REQUEST_URI"];
$uri = $_SERVER["REQUEST_URI"];
// let server handle files or 404s
if (!file_exists ($path) || is_file ($path))
return false;
// append / to directories
if (is_dir ($path) && $uri[strlen ($uri) - 1] != '/') {
header ('Location: ' . $uri . '/');
return;
}
// send index.html and index.php
$indexes = ['index.php', 'index.html'];
foreach ($indexes as $index) {
$file = $path . '/' . $index;
if (is_file ($file)) {
require $file;
return;
}
}
$SVGs = <<<'HTML'
<svg version="1.1" display=none>
<defs>
<symbol id="home" viewBox="0 0 512 512">
<polygon points="448 288 256 64 64 288 112 288 112 448 208 448 208 320 304 320 304 448 400 448 400 288 "/>
</symbol>
<symbol id="folder" viewBox="0 0 512 512">
<path fill=#DDD stroke-width=40 d="M213 96H75C51 96 32 115 32 139v235C32 397 51 416 75 416h363C461 416 480 397 480 373V187C480 163 461 144 437 144H256L213 96z"/>
</symbol>
<symbol id="file" viewBox="0 32 512 450">
<path fill=#FFF stroke-width=40 d="M288 48H136c-22.1 0-40 17.9-40 40v336c0 22.1 17.9 40 40 40h240c22.1 0 40-17.9 40-40V176L288 48zM272 192V80l112 112H272z"/>
</symbol>
<symbol id="back" viewBox="2 4 20 20">
<path stroke=none transform="rotate(90 12 12)" d="M11 9l1.42 1.42L8.83 14H18V4h2v12H8.83l3.59 3.58L11 21l-6-6 6-6z"/>
</symbol>
</defs>
</svg>
HTML;
$HOME_ICON = '<svg width=16 height=16><use xlink:href="#home"></use></svg>';
$FOLDER_ICON = '<svg width=16 height=16><use xlink:href="#folder"></use></svg>';
$FILE_ICON = '<svg width=16 height=16><use xlink:href="#file"></use></svg>';
$BACK_ICON = '<svg width=16 height=16><use xlink:href="#back"></use></svg>';
$prev = '';
$uriT = trim ($uri, '/');
$uriT = implode (' / ',
array_map (
function ($e) {
return "<a href='$e[0]'>$e[1]</a>";
},
array_merge (
[['/', $HOME_ICON]],
$uriT ? array_map (
function ($e) use (&$prev) {
$prev = "$prev/$e";
return [$prev, $e];
},
explode ('/', $uriT)
) : []
)
)
);
echo "<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<style>
body {
color: #666;
font-family: 'Helvetica Neue', Arial, Verdana, sans-serif;
font-size: 14px;
background: #E8E7E6;
}
article {
width: 600px;
margin: 30px auto;
background: #f8f8f8;
border: 1px solid #CCC;
box-shadow: 1px 1px 3px rgba(0,0,0,0.2);
padding: 30px;
}
nav a {
display: block;
padding: 5px 10px;
margin: 0 -10px;
text-decoration: none;
color: inherit;
letter-spacing: 0.5px;
}
nav a:hover {
background: #FFF;
outline: 1px solid rgba(0,0,0,0.1);
outline-offset: -1px;
}
nav a img {
vertical-align: bottom;
margin-right: 10px;
}
nav a svg {
fill: #666;
stroke: #666;
vertical-align: bottom;
margin-right: 6px;
}
header {
background: #FFF;
border-bottom: 1px solid #e0e0e0;
padding: 30px 30px 20px;
margin: -30px -30px 25px;
}
h2 {
margin: 0 0 20px 0;
font-weight: 300;
}
header > p {
line-height: 24px;
margin: 0;
}
header > p svg {
vertical-align: top;
padding: 3px 0 0;
fill: #888;
}
header > p span {
color: #CCC;
}
header > p a {
color: #888;
text-decoration: none;
}
header > p a:hover {
color: #000;
}
header > p a:hover svg {
fill: #000;
}
</style>
</head>
<body>
$SVGs
<article>
<header>
<h2>Local Web Server</h2>
<p>Directory: <span>$uriT</span></p>
</header>
<nav>
";
if ($uri != '/')
echo "<a href='..'>{$BACK_ICON}..</a>";
$g = array_map (function ($path) {
if (is_dir ($path)) {
$path .= '/';
}
return str_replace ('//', '/', $path);
}, glob ($path . '/*'));
usort ($g, function ($a, $b) {
if (is_dir ($a) == is_dir ($b))
return strnatcasecmp ($a, $b);
else
return is_dir ($a) ? -1 : 1;
});
echo implode ("\n", array_map (function ($a) use ($FILE_ICON, $FOLDER_ICON) {
$url = str_replace ($_SERVER['DOCUMENT_ROOT'], '', $a);
$icon = is_file ($a) ? $FILE_ICON : $FOLDER_ICON;
return sprintf ('<a href="%s">%s%s</a>', $url, $icon, basename ($url));
}, $g));
echo '
</nav>
</article>
</body>
</html>';
});