]> git.lizzy.rs Git - dirlistozxa.git/blobdiff - dirlistozxa.php
gen-thumbs: generate video thumbnails
[dirlistozxa.git] / dirlistozxa.php
index 17b67be4f2fc19f83a9f34314cb44d9a3116a00c..eff24698b0be349ea63e4100b30d2c1b3bde3588 100644 (file)
@@ -1,8 +1,47 @@
 <?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;
 
-$ignore_file_list = ['.htaccess', 'Thumbs.db', '.DS_Store', 'index.php', 'index.html', 'dirlistozxa.php'];
+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'];
@@ -13,10 +52,20 @@ function display_size($bytes, $precision = 2) {
        return round($bytes, $precision) . $units[$pow];
 }
 
-function row($name, $date, $size) {
+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><a href="%s">%s</a></td><td>%s</td><td class="r">%s</td></tr>',
-       $name, $name, $date, $size);
+               '<tr>
+                       <td class="tum"><a href="%s"><img src="%s" loading="lazy"></td>
+                       <td><a href="%s">%s</a></td>
+                       <td>%s</td><td class="r">%s</td>
+               </tr>',
+       $name, $img, $name, $name, $date, $size);
 }
 
 function build_blocks($items) {
@@ -27,7 +76,7 @@ function build_blocks($items) {
        $objects = [ 'directories' => [], 'files' => [] ];
 
        foreach ($items as $item) {
-               if ($item == '..' || $item == '.' || in_array($item, $ignore_file_list)) continue;
+               if (in_array($item, $ignore_file_list) || str_starts_with($item, '.')) continue;
 
                if (is_dir($path.$item))
                        $objects['directories'][$item] = $item;
@@ -40,13 +89,13 @@ function build_blocks($items) {
        natsort($objects['files']);
 
        if ($folder != '/')
-               $rtn .= row('../', '-', '-');
+               $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, '-');
+               $rtn .= row($name, $date, '-', THUMB_FOLDER);
        }
 
        foreach ($objects['files'] as $file) {
@@ -54,7 +103,9 @@ function build_blocks($items) {
                $date = date('Y-m-d H:i', filemtime($path.$file));
                $size = display_size(filesize($path.$file));
 
-               $rtn .= row($name, $date, $size);
+               $doThumb = file_exists($_SERVER['DOCUMENT_ROOT']."/.thumbs/".$file) ? THUMB_IMAGE : THUMB_FILE;
+
+               $rtn .= row($name, $date, $size, $doThumb);
        }
 
        return $rtn;
@@ -83,18 +134,29 @@ a {
        color: lime;
        text-decoration: none;
 }
+
+.tum {
+       height: 48px;
+       width: 48px;
+}
+.tum img {
+       max-width: 100%;
+       max-height: 100%;
+       margin: auto;
+       display: block;
+}
        </style>
 </head>
 <body>
-<h1>Index of <?=$folder ?></h1>
+       <h1>Index of <?=$folder ?></h1>
 
-<table>
-       <tr><th>Name</th><th>Last modified</th><th>Size</th></tr>
-       <tr><th colspan="3"><hr></th></tr>
-       <?=build_blocks(scandir($path)) ?>
-       <tr><th colspan="3"><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>
+       <table>
+               <tr><th></th><th>Name</th><th>Last modified</th><th>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>