Mercurial > dirlist-php
view index.php @ 20:caf498a0c602
Hide [root] for aliased directory.
author | edogawaconan <me@myconan.net> |
---|---|
date | Wed, 22 Oct 2014 11:58:24 +0900 |
parents | c5b8d2592585 |
children | fb351f473fd5 |
line wrap: on
line source
<?php // Required for strftime(). Set to UTC because :internet:. date_default_timezone_set('UTC'); // $path: actual requested path, with $alias_prefix removed, relative to $root. $path = $_SERVER["REQUEST_URI"]; $query_string_start = strpos($path, "?"); if ($query_string_start !== false) { $path = substr($path, 0, $query_string_start); } $path = urldecode($path); $prefix = $_SERVER["DL_PREFIX"]; if ($prefix === null) { $prefix = ""; } $path = substr($path, strlen($prefix)); if ($path === false) { $path = "/"; } // root of directory listing. $root = $_SERVER["DL_ROOT"]; if ($root === null || $root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; } if (realpath($root . $path) === false) { header("HTTP/1.0 404 Not Found"); } elseif (substr($path, -1) !== "/") { header("Location: " . $path . "/"); } if (http_response_code() !== 200) { exit; } $dir_handle = @opendir($root . $path); $files = array(); $dirs = array(); while (($file = readdir($dir_handle)) !== false) { if ($file === "." || $file === "..") { continue; } elseif (is_dir($root . $path . $file)) { $dirs[] = $file; } else { $files[] = $file; } } sort($files); sort($dirs); // BEGIN UTILITY function h($string) { return htmlspecialchars($string, ENT_QUOTES, "UTF-8"); } function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } function link_to($target, $title) { return('<a href="' . a($target) . '">' . h($title) . "</a>"); } function human_size($size) { $thousand_units = array("ko", "Mo", "Go", "To", "Po"); $return_format = "%d %s"; if ($size <= 1) { $return_unit = "octet"; } elseif ($size < 1000) { $return_unit = "octets"; } else { $size /= 1000; for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; } $return_format = "%.2f %s"; $return_unit = $thousand_units[$i]; } return sprintf($return_format, $size, $return_unit); } // END UTILITY function tree_link() { global $path, $prefix; $path_array = explode("/", trim($path, "/")); array_unshift($path_array, trim($prefix, "/")); $tree_path = "/"; if ($prefix === "") { $tree_link = link_to($tree_path, "[root]"); } $tree_link .= "/"; foreach ($path_array as $p) { if ($p === "") { continue; } $tree_path .= $p . "/"; $tree_link .= link_to($tree_path, $p) . "/"; } return $tree_link; } function file_rows($files, $is_dir) { global $path, $root, $prefix; $file_rows = ""; $file_suffix = ""; if ($is_dir) { $file_suffix = "/"; if ($path !== "/") { $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "[up]") . "</td></tr>"; } } foreach($files as $file) { $file_stat = stat($root . $path . "/". $file); $file_rows .= "<tr>"; $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; $file_rows .= "<td>"; if ($is_dir) { $file_rows .= "[dir]"; } else { $file_rows .= human_size($file_stat['size']); } $file_rows .= "</td>"; $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; $file_rows .= "</tr>"; } return $file_rows; } ?> <?php header('Content-Type: text/html; charset=utf-8'); ?> <!doctype html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Index of <?php echo h($prefix . $path); ?></title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css"> <style type="text/css"> .lb-data a { color: #ccc; } .lightbox { position: fixed; top: 50px !important; } </style> <style type="text/css"> * { box-sizing: border-box; } body { font-family: Segoe UI, sans-serif; font-size: 14px; } h1 { margin: 5px; } table { width: 100%; } th:first-child, td:first-child { width: 100%; white-space: pre-wrap; word-wrap: break-word; word-break: break-all; } tr { position: relative; } th, td { white-space: nowrap; padding: 2px 5px; } @media (min-width: 768px) { th { background: #ccc; } tr:nth-child(even) { background: #eee; } tr:hover { background: #ddd; } } @media (max-width: 767px) { table { border-spacing: 0 10px; } th { display: none; } tr { background: #eee; } td { display: inline-block; } td:first-child { background: #ddd; padding: 5px; } table a { font-size: 18px; display: block; } } </style> </head> <body> <h1>Index of <?php echo tree_link(); ?></h1> <table> <thead><tr> <th>File</th> <th>Size</th> <th>Date</th> </tr></thead> <tbody> <?php echo file_rows($dirs, true); ?> <?php echo file_rows($files, false); ?> </tbody> </table> <footer> <hr> <em> Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php</a>. Powered by PHP <?php echo phpversion(); ?>. </em> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $("table > tbody > tr > td > a").each(function() { if (!this.href.match(/\.(jpe?g|png|gif|webp)$/i)) return var title = this.outerHTML this.setAttribute("data-title", title) this.setAttribute("data-lightbox", "aa") }) </script> <script src="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js"></script> </body>