commit 63d34691f4540307c0d853770193232097b9967e Author: nvpie exozyme Date: Mon May 22 07:31:00 2023 +0000 stylized directory listing added diff --git a/better_index_of/index.php b/better_index_of/index.php new file mode 100644 index 0000000..65c61ac --- /dev/null +++ b/better_index_of/index.php @@ -0,0 +1,435 @@ + $file, 'isdir' => $isdir, 'size' => $isdir ? 0 : filesize($path . $file), 'time' => filemtime($path . $file)); + if ($isdir) $ls_d[] = $item; else $ls[] = $item; + $_total++; + $_total_size += $item['size']; + } + return array_merge($ls_d, $ls); + } + + // Get the list of files + $items = ls('.' . (empty($_browse) ? '' : '/' . $_browse), $showDirectories, $showHiddenFiles); + + // Sort it + function sortByName($a, $b) { global $showDirectoriesFirst; return ($a['isdir'] == $b['isdir'] || !$showDirectoriesFirst ? strtolower($a['name']) > strtolower($b['name']) : $a['isdir'] < $b['isdir']) ? 1 : -1; } + function sortBySize($a, $b) { return ($a['isdir'] == $b['isdir'] ? $a['size'] > $b['size'] : $a['isdir'] < $b['isdir']) ? 1 : -1; } + function sortByTime($a, $b) { return ($a['time'] > $b['time']) ? 1 : -1; } + switch (@$_GET['s']) { + case 'size': $_sort = 'size'; usort($items, 'sortBySize'); break; + case 'time': $_sort = 'time'; usort($items, 'sortByTime'); break; + default : $_sort = 'name'; usort($items, 'sortByName'); break; + } + + // Reverse? + $_sort_reverse = (@$_GET['r'] == '1'); + if ($_sort_reverse) $items = array_reverse($items); + + // Add parent + if ($showParent && $_path != '/' && empty($_browse)) array_unshift($items, array( + 'name' => '..', + 'isparent' => true, + 'isdir' => true, + 'size' => 0, + 'time' => 0 + )); + + // Add parent in case of browsing a sub-folder + if (!empty($_browse)) array_unshift($items, array( + 'name' => '..', + 'isparent' => false, + 'isdir' => true, + 'size' => 0, + 'time' => 0 + )); + + // 37.6 MB is better than 39487001 + function humanizeFilesize($val, $round = 0) { + $unit = array('','K','M','G','T','P','E','Z','Y'); + do { $val /= 1024; array_shift($unit); } while ($val >= 1000); + return sprintf('%.'.intval($round).'f', $val) . ' ' . array_shift($unit) . 'B'; + } + + // Titles parser + function getTitleHTML($title, $breadcrumbs = false) { + global $_path, $_browse, $_total, $_total_size, $sizeDecimals; + $title = htmlentities(str_replace(array('{{files}}', '{{size}}'), array($_total, humanizeFilesize($_total_size, $sizeDecimals)), $title)); + $path = htmlentities($_path); + if ($breadcrumbs) $path = sprintf('%s', htmlentities(buildLink(array('b' => ''))), $path); + if (!empty($_browse)) { + if ($_path != '/') $path .= '/'; + $browseArray = explode('/', trim($_browse, '/')); + foreach ($browseArray as $i => $part) { + if ($breadcrumbs) { + $path .= sprintf('%s', htmlentities(buildLink(array('b' => implode('/', array_slice($browseArray, 0, $i + 1))))), htmlentities($part)); + } else { + $path .= htmlentities($part); + } + if (count($browseArray) > ($i + 1)) $path .= '/'; + } + } + return str_replace('{{path}}', $path, $title); + } + + // Link builder + function buildLink($changes) { + global $_self; + $params = $_GET; + foreach ($changes as $k => $v) if (is_null($v)) unset($params[$k]); else $params[$k] = $v; + foreach ($params as $k => $v) $params[$k] = urlencode($k) . '=' . urlencode($v); + return empty($params) ? $_self : $_self . '?' . implode('&', $params); + } + +?> + + + + + + + + + <?php echo getTitleHTML($title) ?> + + + + +> + +
+ +

+

+
+ pineapple +
+ + + + + + + + + + + + +
+ + +