]> git.lizzy.rs Git - dirlistozxa.git/blob - dirlistozxa.php
Make icon clickable too
[dirlistozxa.git] / dirlistozxa.php
1 <?php
2 /* dirlistozxa - Basic directory lister script written in PHP
3  * Copyright (C) 2023 ROllerozxa
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * This software is best enjoyed with soused herring!
19  */
20
21 // Configuration:
22
23 // List of filenames (and folder names) that should be ignored.
24 $ignore_file_list = [
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
30 ];
31
32 // ================
33
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']);
37
38 $folder = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
39 $path = $_SERVER['DOCUMENT_ROOT'].$folder;
40
41 if (!is_dir($path)) die('invalid folder?');
42
43 define('THUMB_FOLDER', 1);
44 define('THUMB_FILE', 2);
45 define('THUMB_IMAGE', 3);
46
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];
54 }
55
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,
61         };
62
63         return sprintf(
64                 '<tr>
65                         <td class="tum"><a href="%s"><img src="%s" loading="lazy"></td>
66                         <td><a href="%s">%s</a></td>
67                         <td>%s</td><td class="r">%s</td>
68                 </tr>',
69         $name, $img, $name, $name, $date, $size);
70 }
71
72 function build_blocks($items) {
73         global $ignore_file_list, $path, $folder;
74
75         $rtn = '';
76
77         $objects = [ 'directories' => [], 'files' => [] ];
78
79         foreach ($items as $item) {
80                 if ($item == '..' || $item == '.' || in_array($item, $ignore_file_list)) continue;
81
82                 if (is_dir($path.$item))
83                         $objects['directories'][$item] = $item;
84                 else
85                         $objects['files'][$item] = $item;
86         }
87
88         // SORT
89         natsort($objects['directories']);
90         natsort($objects['files']);
91
92         if ($folder != '/')
93                 $rtn .= row('../', '', '', THUMB_FOLDER);
94
95         foreach ($objects['directories'] as $dir) {
96                 $name = basename($dir).'/';
97                 $date = date('Y-m-d H:i', filemtime($path.$dir));
98
99                 $rtn .= row($name, $date, '-', THUMB_FOLDER);
100         }
101
102         foreach ($objects['files'] as $file) {
103                 $name = basename($file);
104                 $date = date('Y-m-d H:i', filemtime($path.$file));
105                 $size = display_size(filesize($path.$file));
106
107                 $doThumb = file_exists($_SERVER['DOCUMENT_ROOT']."/.thumbs/".$file) ? THUMB_IMAGE : THUMB_FILE;
108
109                 $rtn .= row($name, $date, $size, $doThumb);
110         }
111
112         return $rtn;
113 }
114 ?>
115 <!DOCTYPE html>
116 <html>
117 <head>
118         <title>Index of <?=$folder ?></title>
119         <meta charset="utf-8">
120         <style>
121 body {
122         background-color: #111;
123         color: #eee;
124         font-family: monospace;
125         font-size: 12pt;
126         max-width: 1440px;
127         margin: auto;
128         padding: 0 5px;
129 }
130 td { padding: 5px; }
131 th { padding: 0 5px; }
132 .r { text-align: right }
133
134 a {
135         color: lime;
136         text-decoration: none;
137 }
138
139 .tum {
140         height: 48px;
141         width: 48px;
142 }
143 .tum img {
144         max-width: 100%;
145         max-height: 100%;
146         margin: auto;
147         display: block;
148 }
149         </style>
150 </head>
151 <body>
152         <h1>Index of <?=$folder ?></h1>
153
154         <table>
155                 <tr><th></th><th>Name</th><th>Last modified</th><th>Size</th></tr>
156                 <tr><th colspan="4"><hr></th></tr>
157                 <?=build_blocks(scandir($path)) ?>
158                 <tr><th colspan="4"><hr></th></tr>
159         </table>
160
161         <address><?=$_SERVER['SERVER_SOFTWARE'] ?? 'Cool' ?> server at <?=$_SERVER['HTTP_HOST'] ?>, index powered by <a href="https://github.com/rollerozxa/dirlistozxa/">dirlistozxa</a></address>
162 </body>
163 </html>