2 /* dirlistozxa - Basic directory lister script written in PHP
3 * Copyright (C) 2023 ROllerozxa
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * This software is best enjoyed with soused herring!
23 // List of filenames (and folder names) that should be ignored.
25 '.htaccess', '.htpasswd', // Apache junk files
26 'Thumbs.db', '.DS_Store', // OS junk files
27 'index.php', 'index.html', // Potential other index files
28 '.git', 'vendor', // Dev
29 'dirlistozxa.php', '.dirlistozxa', '.thumbs', 'gen-thumbs' // dirlistozxa
34 // Lazy sanitisation done if the web server somehow sends idiotic input,
35 // nginx with default configuration (merge_slashes) doesn't actually need this.
36 $_SERVER['REQUEST_URI'] = str_replace('../', '', $_SERVER['REQUEST_URI']);
38 $folder = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
39 $path = $_SERVER['DOCUMENT_ROOT'].$folder;
41 if (!is_dir($path)) die('invalid folder?');
43 define('THUMB_FOLDER', 1);
44 define('THUMB_FILE', 2);
45 define('THUMB_IMAGE', 3);
47 function display_size($bytes, $precision = 2) {
48 $units = ['B', 'K', 'M', 'G', 'T'];
49 $bytes = max($bytes, 0);
50 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
51 $pow = min($pow, count($units) - 1);
52 $bytes /= (1 << (10 * $pow));
53 return round($bytes, $precision) . $units[$pow];
56 function row($name, $date, $size, $thumb) {
57 $img = match ($thumb) {
58 THUMB_FOLDER => '/.dirlistozxa/folder.png',
59 THUMB_FILE => '/.dirlistozxa/file.png',
60 THUMB_IMAGE => "/.thumbs/".$name,
64 '<tr><td class="tum"><img src="%s" loading="lazy"></td><td><a href="%s">%s</a></td><td>%s</td><td class="r">%s</td></tr>',
65 $img, $name, $name, $date, $size);
68 function build_blocks($items) {
69 global $ignore_file_list, $path, $folder;
73 $objects = [ 'directories' => [], 'files' => [] ];
75 foreach ($items as $item) {
76 if ($item == '..' || $item == '.' || in_array($item, $ignore_file_list)) continue;
78 if (is_dir($path.$item))
79 $objects['directories'][$item] = $item;
81 $objects['files'][$item] = $item;
85 natsort($objects['directories']);
86 natsort($objects['files']);
89 $rtn .= row('../', '', '', THUMB_FOLDER);
91 foreach ($objects['directories'] as $dir) {
92 $name = basename($dir).'/';
93 $date = date('Y-m-d H:i', filemtime($path.$dir));
95 $rtn .= row($name, $date, '-', THUMB_FOLDER);
98 foreach ($objects['files'] as $file) {
99 $name = basename($file);
100 $date = date('Y-m-d H:i', filemtime($path.$file));
101 $size = display_size(filesize($path.$file));
103 $doThumb = file_exists($_SERVER['DOCUMENT_ROOT']."/.thumbs/".$file) ? THUMB_IMAGE : THUMB_FILE;
105 $rtn .= row($name, $date, $size, $doThumb);
114 <title>Index of <?=$folder ?></title>
115 <meta charset="utf-8">
118 background-color: #111;
120 font-family: monospace;
127 th { padding: 0 5px; }
128 .r { text-align: right }
132 text-decoration: none;
148 <h1>Index of <?=$folder ?></h1>
151 <tr><th></th><th>Name</th><th>Last modified</th><th>Size</th></tr>
152 <tr><th colspan="4"><hr></th></tr>
153 <?=build_blocks(scandir($path)) ?>
154 <tr><th colspan="4"><hr></th></tr>
157 <address><?=$_SERVER['SERVER_SOFTWARE'] ?? 'Cool' ?> server at <?=$_SERVER['HTTP_HOST'] ?>, index powered by <a href="https://github.com/rollerozxa/dirlistozxa/">dirlistozxa</a></address>