-
Notifications
You must be signed in to change notification settings - Fork 2
/
dirlistozxa.php
167 lines (138 loc) · 4.37 KB
/
dirlistozxa.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
<?php
/* dirlistozxa - Basic directory lister script written in PHP
* Copyright (C) 2023 ROllerozxa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is best enjoyed with soused herring!
*/
// Configuration:
// List of filenames (and folder names) that should be ignored.
$ignore_file_list = [
'dirlistozxa.php', 'gen-thumbs' // dirlistozxa
];
// ================
// Lazy sanitisation done if the web server somehow sends idiotic input,
// nginx with default configuration (merge_slashes) doesn't actually need this.
$_SERVER['REQUEST_URI'] = str_replace('../', '', $_SERVER['REQUEST_URI']);
// Allow spaces and other heretical stuff in folder names.
$_SERVER['REQUEST_URI'] = urldecode($_SERVER['REQUEST_URI']);
$folder = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
$path = $_SERVER['DOCUMENT_ROOT'].$folder;
if (!is_dir($path)) die('invalid folder?');
define('THUMB_FOLDER', 1);
define('THUMB_FILE', 2);
define('THUMB_IMAGE', 3);
function display_size($bytes, $precision = 2) {
$units = ['B', 'K', 'M', 'G', 'T'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . $units[$pow];
}
function row($name, $date, $size, $thumb) {
$img = match ($thumb) {
THUMB_FOLDER => '/.dirlistozxa/folder.png',
THUMB_FILE => '/.dirlistozxa/file.png',
THUMB_IMAGE => "/.thumbs/".$name,
};
return sprintf(
'<tr>
<td class="tum"><a href="%s"><img src="%s" loading="lazy"></td>
<td><a href="%s">%s</a></td>
<td class="modf">%s</td><td class="size r">%s</td>
</tr>',
$name, $img, $name, $name, $date, $size);
}
function build_blocks($items) {
global $ignore_file_list, $path, $folder;
$rtn = '';
$objects = [ 'directories' => [], 'files' => [] ];
foreach ($items as $item) {
if (in_array($item, $ignore_file_list) || str_starts_with($item, '.')) continue;
if (is_dir($path.$item))
$objects['directories'][$item] = $item;
else
$objects['files'][$item] = $item;
}
// SORT
natsort($objects['directories']);
natsort($objects['files']);
if ($folder != '/')
$rtn .= row('../', '', '', THUMB_FOLDER);
foreach ($objects['directories'] as $dir) {
$name = basename($dir).'/';
$date = date('Y-m-d H:i', filemtime($path.$dir));
$rtn .= row($name, $date, '-', THUMB_FOLDER);
}
foreach ($objects['files'] as $file) {
$name = basename($file);
$date = date('Y-m-d H:i', filemtime($path.$file));
$size = display_size(filesize($path.$file));
$doThumb = file_exists($_SERVER['DOCUMENT_ROOT']."/.thumbs/".$file) ? THUMB_IMAGE : THUMB_FILE;
$rtn .= row($name, $date, $size, $doThumb);
}
return $rtn;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Index of <?=$folder ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
background-color: #111;
color: #eee;
font-family: monospace;
font-size: 12pt;
max-width: 1440px;
margin: auto;
padding: 0 5px;
}
td { padding: 5px; }
th { padding: 0 5px; }
.r { text-align: right }
a {
color: lime;
text-decoration: none;
}
.tum {
height: 48px;
width: 48px;
}
.tum img {
max-width: 100%;
max-height: 100%;
margin: auto;
display: block;
}
@media only screen and (max-width: 640px) {
.modf { display: none; }
}
</style>
</head>
<body>
<h1>Index of <?=$folder ?></h1>
<table>
<tr><th colspan="2">Name</th><th class="modf">Last modified</th><th class="size">Size</th></tr>
<tr><th colspan="4"><hr></th></tr>
<?=build_blocks(scandir($path)) ?>
<tr><th colspan="4"><hr></th></tr>
</table>
<address><?=$_SERVER['SERVER_SOFTWARE'] ?? 'Cool' ?> server at <?=$_SERVER['HTTP_HOST'] ?>, index powered by <a href="https://github.com/rollerozxa/dirlistozxa/">dirlistozxa</a></address>
</body>
</html>