]> git.lizzy.rs Git - dirlistozxa.git/blob - dirlistozxa.php
6d3c4b5e45a7c0c092945d8c1675656065549ca7
[dirlistozxa.git] / dirlistozxa.php
1 <?php
2 // Configuration:
3
4 // List of filenames (and folder names) that should be ignored.
5 $ignore_file_list = [
6         '.htaccess', '.htpasswd',       // Apache junk files
7         'Thumbs.db', '.DS_Store',       // OS junk files
8         'index.php', 'index.html',      // Potential other index files
9         '.git', 'vendor',                       // Dev
10         'dirlistozxa.php', '.dirlistozxa', '.thumbs', 'gen-thumbs' // dirlistozxa
11 ];
12
13 // ================
14
15 // Lazy sanitisation done if the web server somehow sends idiotic input,
16 // nginx with default configuration (merge_slashes) doesn't actually need this.
17 $_SERVER['REQUEST_URI'] = str_replace('../', '', $_SERVER['REQUEST_URI']);
18
19 $folder = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
20 $path = $_SERVER['DOCUMENT_ROOT'].$folder;
21
22 if (!is_dir($path)) die('invalid folder?');
23
24 define('THUMB_FOLDER', 1);
25 define('THUMB_FILE', 2);
26 define('THUMB_IMAGE', 3);
27
28 function display_size($bytes, $precision = 2) {
29         $units = ['B', 'K', 'M', 'G', 'T'];
30         $bytes = max($bytes, 0);
31         $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
32         $pow = min($pow, count($units) - 1);
33         $bytes /= (1 << (10 * $pow));
34         return round($bytes, $precision) . $units[$pow];
35 }
36
37 function row($name, $date, $size, $thumb) {
38         $img = match ($thumb) {
39                 THUMB_FOLDER => '/.dirlistozxa/folder.png',
40                 THUMB_FILE => '/.dirlistozxa/file.png',
41                 THUMB_IMAGE => "/.thumbs/".$name,
42         };
43
44         return sprintf(
45                 '<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>',
46         $img, $name, $name, $date, $size);
47 }
48
49 function build_blocks($items) {
50         global $ignore_file_list, $path, $folder;
51
52         $rtn = '';
53
54         $objects = [ 'directories' => [], 'files' => [] ];
55
56         foreach ($items as $item) {
57                 if ($item == '..' || $item == '.' || in_array($item, $ignore_file_list)) continue;
58
59                 if (is_dir($path.$item))
60                         $objects['directories'][$item] = $item;
61                 else
62                         $objects['files'][$item] = $item;
63         }
64
65         // SORT
66         natsort($objects['directories']);
67         natsort($objects['files']);
68
69         if ($folder != '/')
70                 $rtn .= row('../', '', '', THUMB_FOLDER);
71
72         foreach ($objects['directories'] as $dir) {
73                 $name = basename($dir).'/';
74                 $date = date('Y-m-d H:i', filemtime($path.$dir));
75
76                 $rtn .= row($name, $date, '-', THUMB_FOLDER);
77         }
78
79         foreach ($objects['files'] as $file) {
80                 $name = basename($file);
81                 $date = date('Y-m-d H:i', filemtime($path.$file));
82                 $size = display_size(filesize($path.$file));
83
84                 $doThumb = file_exists($_SERVER['DOCUMENT_ROOT']."/.thumbs/".$file) ? THUMB_IMAGE : THUMB_FILE;
85
86                 $rtn .= row($name, $date, $size, $doThumb);
87         }
88
89         return $rtn;
90 }
91 ?>
92 <!DOCTYPE html>
93 <html>
94 <head>
95         <title>Index of <?=$folder ?></title>
96         <meta charset="utf-8">
97         <style>
98 body {
99         background-color: #111;
100         color: #eee;
101         font-family: monospace;
102         font-size: 12pt;
103         max-width: 1440px;
104         margin: auto;
105         padding: 0 5px;
106 }
107 td { padding: 5px; }
108 th { padding: 0 5px; }
109 .r { text-align: right }
110
111 a {
112         color: lime;
113         text-decoration: none;
114 }
115
116 .tum {
117         height: 48px;
118         width: 48px;
119 }
120 .tum img {
121         max-width: 100%;
122         max-height: 100%;
123         margin: auto;
124         display: block;
125 }
126         </style>
127 </head>
128 <body>
129         <h1>Index of <?=$folder ?></h1>
130
131         <table>
132                 <tr><th></th><th>Name</th><th>Last modified</th><th>Size</th></tr>
133                 <tr><th colspan="4"><hr></th></tr>
134                 <?=build_blocks(scandir($path)) ?>
135                 <tr><th colspan="4"><hr></th></tr>
136         </table>
137
138         <address><?=$_SERVER['SERVER_SOFTWARE'] ?? 'Cool' ?> server at <?=$_SERVER['HTTP_HOST'] ?>, index powered by <a href="https://github.com/rollerozxa/dirlistozxa/">dirlistozxa</a></address>
139 </body>
140 </html>