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