-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.php
416 lines (341 loc) · 12.3 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
// MINIXED is a minimal but nice-looking PHP directory indexer.
// More at https://github.com/lorenzos/Minixed
// =============================
// Configuration
// =============================
$browseDirectories = false; // Navigate into sub-folders
$title = 'Index of {{path}}';
$subtitle = '{{files}} objects in this folder, {{size}} total'; // Empty to disable
$breadcrumbs = false; // Make links in {{path}}
$showParent = false; // Display a (parent directory) link
$showDirectories = true;
$showDirectoriesFirst = true; // Lists directories first when sorting by name
$showHiddenFiles = false; // Display files starting with "." too
$alignment = 'left'; // You can use 'left' or 'center'
$showIcons = true;
$dateFormat = 'd/m/y H:i'; // Used in date() function
$sizeDecimals = 1;
$robots = 'noindex, nofollow'; // Avoid robots by default
$showFooter = true; // Display the "Powered by" footer
$openIndex = $browseDirectories && true; // Open index files present in the current directory if $browseDirectories is enabled
$browseDefault = null; // Start on a different "default" directory if $browseDirectories is enabled
$ignore = array(); // Names of files and folders to not list (case-sensitive)
// =============================
// =============================
// Who am I?
$_self = basename($_SERVER['PHP_SELF']);
$_path = str_replace('\\', '/', dirname($_SERVER['PHP_SELF']));
$_total = 0;
$_total_size = 0;
// Directory browsing
$_browse = null;
if ($browseDirectories) {
if (!empty($browseDefault) && !isset($_GET['b'])) $_GET['b'] = $browseDefault;
$_GET['b'] = trim(str_replace('\\', '/', (string)@$_GET['b']), '/ ');
$_GET['b'] = str_replace(array('/..', '../'), '', (string)@$_GET['b']); // Avoid going up into filesystem
if (!empty($_GET['b']) && $_GET['b'] != '..' && is_dir($_GET['b'])) {
$browseIgnored = false;
foreach (explode('/', $_GET['b']) as $browseName) {
if (!empty($ignore) && is_array($ignore) && in_array($browseName, $ignore)) {
$browseIgnored = true;
break;
}
}
if (!$browseIgnored) $_browse = $_GET['b']; // Avoid browsing ignored folder names
}
}
// Index open
if (!empty($_browse) && $openIndex) {
$_index = null;
if (file_exists($_browse . "/index.htm")) $_index = "/index.htm";
if (file_exists($_browse . "/index.html")) $_index = "/index.html";
if (file_exists($_browse . "/index.php")) $_index = "/index.php";
if (!empty($_index)) {
header('Location: ' . $_browse . $_index);
exit();
}
}
// Encoded images generator
if (!empty($_GET['i'])) {
header('Content-type: image/png');
switch ($_GET['i']) {
case 'asc': exit(base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAYAAADEUlfTAAAAFUlEQVQImWNgoBT8x4JxKsBpAhUAAPUACPhuMItPAAAAAElFTkSuQmCC'));
case 'desc': exit(base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAYAAADEUlfTAAAAF0lEQVQImWNgoBb4j0/iPzYF/7FgCgAADegI+OMeBfsAAAAASUVORK5CYII='));
case 'directory': exit(base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAASklEQVQYlYWPwQ3AMAgDb3Tv5AHdR5OqTaBB8gM4bAGApACPRr/XuujA+vqVcAI3swDYjqRSH7B9oHI8grbTgWN+g3+xq0k6TegCNtdPnJDsj8sAAAAASUVORK5CYII='));
case 'file': exit(base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAPklEQVQYlcXQsQ0AIAhE0b//GgzDWGdjDCJoKck13CsIALi7gJxyVmFmyrsXLHEHD7zBmBbezvoJm4cL0OwYouM4O3J+UDYAAAAASUVORK5CYII='));
}
}
// I'm not sure this function is really needed...
function ls($path, $show_folders = false, $show_hidden = false) {
global $_self, $_total, $_total_size, $ignore;
$ls = array();
$ls_d = array();
if (($dh = @opendir($path)) === false) return $ls;
if (substr($path, -1) != '/') $path .= '/';
while (($file = readdir($dh)) !== false) {
if ($file == $_self) continue;
if ($file == '.' || $file == '..') continue;
if (!$show_hidden) if (substr($file, 0, 1) == '.') continue;
if (!empty($ignore) && is_array($ignore) && in_array($file, $ignore)) continue;
$isdir = is_dir($path . $file);
if (!$show_folders && $isdir) continue;
$item = array('name' => $file, 'isdir' => $isdir, 'size' => $isdir ? 0 : getFilesize($path . $file), 'time' => filemtime($path . $file));
if ($isdir) $ls_d[] = $item; else $ls[] = $item;
$_total++;
$_total_size += $item['size'];
}
return array_merge($ls_d, $ls);
}
// Get the list of files
$items = ls('.' . (empty($_browse) ? '' : '/' . $_browse), $showDirectories, $showHiddenFiles);
// Sort it
function sortByName($a, $b) { global $showDirectoriesFirst; return ($a['isdir'] == $b['isdir'] || !$showDirectoriesFirst ? strtolower($a['name']) > strtolower($b['name']) : $a['isdir'] < $b['isdir']) ? 1 : -1; }
function sortBySize($a, $b) { return ($a['isdir'] == $b['isdir'] ? $a['size'] > $b['size'] : $a['isdir'] < $b['isdir']) ? 1 : -1; }
function sortByTime($a, $b) { return ($a['time'] > $b['time']) ? 1 : -1; }
switch (@$_GET['s']) {
case 'size': $_sort = 'size'; usort($items, 'sortBySize'); break;
case 'time': $_sort = 'time'; usort($items, 'sortByTime'); break;
default : $_sort = 'name'; usort($items, 'sortByName'); break;
}
// Reverse?
$_sort_reverse = (@$_GET['r'] == '1');
if ($_sort_reverse) $items = array_reverse($items);
// Add parent
if ($showParent && $_path != '/' && empty($_browse)) array_unshift($items, array(
'name' => '..',
'isparent' => true,
'isdir' => true,
'size' => 0,
'time' => 0
));
// Add parent in case of browsing a sub-folder
if (!empty($_browse)) array_unshift($items, array(
'name' => '..',
'isparent' => false,
'isdir' => true,
'size' => 0,
'time' => 0
));
// Get filesize correctly (as float) on 32bit systems supporting the st_blksize type (e.g. Linux)
function getFilesize($path) {
$stat = stat($path);
if ($stat['blocks'] > 2048) return $stat['blocks'] * 512.0;
return (float)$stat['size'];
}
// 37.6 MB is better than 39487001
function humanizeFilesize($val, $round = 0) {
$unit = array('','K','M','G','T','P','E','Z','Y');
$round_actual = 0;
while ($val >= 1000) { $val /= 1024; array_shift($unit); $round_actual = $round; }
return sprintf('%.'.intval($round_actual).'f', $val) . ' ' . array_shift($unit) . 'B';
}
// Titles parser
function getTitleHTML($title, $breadcrumbs = false) {
global $_path, $_browse, $_total, $_total_size, $sizeDecimals;
$title = htmlentities(str_replace(array('{{files}}', '{{size}}'), array($_total, humanizeFilesize($_total_size, $sizeDecimals)), $title));
$path = htmlentities($_path);
if ($breadcrumbs) $path = sprintf('<a href="%s">%s</a>', htmlentities(buildLink(array('b' => ''))), $path);
if (!empty($_browse)) {
if ($_path != '/') $path .= '/';
$browseArray = explode('/', trim($_browse, '/'));
foreach ($browseArray as $i => $part) {
if ($breadcrumbs) {
$path .= sprintf('<a href="%s">%s</a>', htmlentities(buildLink(array('b' => implode('/', array_slice($browseArray, 0, $i + 1))))), htmlentities($part));
} else {
$path .= htmlentities($part);
}
if (count($browseArray) > ($i + 1)) $path .= '/';
}
}
return str_replace('{{path}}', $path, $title);
}
// Link builder
function buildLink($changes) {
global $_self;
$params = $_GET;
foreach ($changes as $k => $v) if (is_null($v)) unset($params[$k]); else $params[$k] = $v;
foreach ($params as $k => $v) $params[$k] = urlencode($k) . '=' . urlencode($v);
return empty($params) ? $_self : $_self . '?' . implode('&', $params);
}
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="robots" content="<?php echo htmlentities($robots) ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo getTitleHTML($title) ?></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
border: none;
}
body {
text-align: center;
font-family: sans-serif;
font-size: 13px;
color: #000000;
}
#wrapper {
max-width: 600px;
*width: 600px;
margin: 0 auto;
text-align: left;
}
body#left {
text-align: left;
}
body#left #wrapper {
margin: 0 20px;
}
h1 {
font-size: 21px;
padding: 0 10px;
margin: 20px 0 0;
font-weight: bold;
}
h2 {
font-size: 14px;
padding: 0 10px;
margin: 10px 0 0;
color: #999999;
font-weight: normal;
}
a {
color: #003399;
text-decoration: none;
}
a:hover {
color: #0066cc;
text-decoration: underline;
}
ul#header {
margin-top: 20px;
}
ul li {
display: block;
list-style-type: none;
overflow: hidden;
padding: 10px;
}
ul li:hover {
background-color: #f3f3f3;
}
ul li .date {
text-align: center;
width: 120px;
}
ul li .size {
text-align: right;
width: 90px;
}
ul li .date, ul li .size {
float: right;
font-size: 12px;
display: block;
color: #666666;
}
ul#header li {
font-size: 11px;
font-weight: bold;
border-bottom: 1px solid #cccccc;
}
ul#header li:hover {
background-color: transparent;
}
ul#header li * {
color: #000000;
font-size: 11px;
}
ul#header li a:hover {
color: #666666;
}
ul#header li .asc span, ul#header li .desc span {
padding-right: 15px;
background-position: right center;
background-repeat: no-repeat;
}
ul#header li .asc span {
background-image: url('<?php echo $_self ?>?i=asc');
}
ul#header li .desc span {
background-image: url('<?php echo $_self ?>?i=desc');
}
ul li.item {
border-top: 1px solid #f3f3f3;
}
ul li.item:first-child {
border-top: none;
}
ul li.item .name {
font-weight: bold;
}
ul li.item .directory, ul li.item .file {
padding-left: 20px;
background-position: left center;
background-repeat: no-repeat;
}
ul li.item .directory {
background-image: url('<?php echo $_self ?>?i=directory');
}
ul li.item .file {
background-image: url('<?php echo $_self ?>?i=file');
}
#footer {
color: #cccccc;
font-size: 11px;
margin-top: 40px;
margin-bottom: 20px;
padding: 0 10px;
text-align: left;
}
#footer a {
color: #cccccc;
font-weight: bold;
}
#footer a:hover {
color: #999999;
}
</style>
</head>
<body <?php if ($alignment == 'left') echo 'id="left"' ?>>
<div id="wrapper">
<h1><?php echo getTitleHTML($title, $breadcrumbs) ?></h1>
<h2><?php echo getTitleHTML($subtitle, $breadcrumbs) ?></h2>
<ul id="header">
<li>
<a href="<?php echo buildLink(array('s' => 'size', 'r' => (!$_sort_reverse && $_sort == 'size') ? '1' : null)) ?>" class="size <?php if ($_sort == 'size') echo $_sort_reverse ? 'desc' : 'asc' ?>"><span>Size</span></a>
<a href="<?php echo buildLink(array('s' => 'time', 'r' => (!$_sort_reverse && $_sort == 'time') ? '1' : null)) ?>" class="date <?php if ($_sort == 'time') echo $_sort_reverse ? 'desc' : 'asc' ?>"><span>Last modified</span></a>
<a href="<?php echo buildLink(array('s' => null , 'r' => (!$_sort_reverse && $_sort == 'name') ? '1' : null)) ?>" class="name <?php if ($_sort == 'name') echo $_sort_reverse ? 'desc' : 'asc' ?>"><span>Name</span></a>
</li>
</ul>
<ul>
<?php foreach ($items as $item): ?>
<li class="item">
<span class="size"><?php echo $item['isdir'] ? '-' : humanizeFilesize($item['size'], $sizeDecimals) ?></span>
<span class="date"><?php echo (@$item['isparent'] || empty($item['time'])) ? '-' : date($dateFormat, $item['time']) ?></span>
<?php
if ($item['isdir'] && $browseDirectories && !@$item['isparent']) {
if ($item['name'] == '..') {
$itemURL = buildLink(array('b' => substr($_browse, 0, strrpos($_browse, '/'))));
} else {
$itemURL = buildLink(array('b' => (empty($_browse) ? '' : (string)$_browse . '/') . $item['name']));
}
} else {
$itemURL = (empty($_browse) ? '' : str_replace(['%2F', '%2f'], '/', rawurlencode((string)$_browse)) . '/') . rawurlencode($item['name']);
}
?>
<a href="<?php echo htmlentities($itemURL) ?>" class="name <?php if ($showIcons) echo $item['isdir'] ? 'directory' : 'file' ?>"><?php echo htmlentities($item['name']) . ($item['isdir'] ? ' /' : '') ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php if ($showFooter): ?>
<p id="footer">
Powered by <a href="https://github.com/lorenzos/Minixed" target="_blank">Minixed</a>, a PHP directory indexer
</p>
<?php endif; ?>
</div>
</body>
</html>