Mercurial > dirlist-php
view index.php @ 31:73833d29dbad
Add bugs note.
author | edogawaconan <me@myconan.net> |
---|---|
date | Thu, 30 Oct 2014 13:04:29 +0900 |
parents | c482f0db5c28 |
children | 986aec12eb7f |
line wrap: on
line source
<?php define('DL_VERSION', '2.0.0'); // Required for strftime(). Set to UTC because :internet:. date_default_timezone_set('UTC'); // $uri: web-facing path $uri = $_SERVER["REQUEST_URI"]; $query_string_start = strpos($uri, "?"); if ($query_string_start !== false) { $uri = substr($uri, 0, $query_string_start); } $uri = urldecode($uri); // $dir: filesystem path if (isset($_SERVER["DL_DIR"])) { $dir = $_SERVER["DL_DIR"]; } elseif (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"])) { $dir = $_SERVER["CONTEXT_DOCUMENT_ROOT"]; $dir .= substr($uri, strlen($_SERVER["CONTEXT_PREFIX"])); } else { $dir = $_SERVER["DOCUMENT_ROOT"] . $uri; } if (realpath($dir) === false) { header("HTTP/1.0 404 Not Found"); } elseif (substr($uri, -1) !== "/") { header("Location: " . $uri . "/"); } if (http_response_code() !== 200) { exit; } $dir_handle = @opendir($dir); $files = array(); $dirs = array(); while (($file = readdir($dir_handle)) !== false) { if ($file === "." || $file === "..") { continue; } elseif (!(isset($_SERVER["DL_SHOWALL"]) && $_SERVER["DL_SHOWALL"] === "1") && substr($file, 0, 1) === ".") { continue; } elseif (is_dir($dir . $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 $uri; $uri_array = explode("/", trim($uri, "/")); $tree_path = "/"; $tree_link = link_to($tree_path, "[root]"); $tree_link .= "/"; foreach ($uri_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 $dir, $uri; $file_rows = ""; $file_suffix = ""; if ($is_dir) { $file_suffix = "/"; if ($uri !== "/") { $file_rows .= "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>"; } } foreach($files as $file) { $file_stat = stat($dir . "/". $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($uri); ?></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 <?php echo DL_VERSION ?></a>. Powered by PHP <?php echo phpversion(); ?>. </em> </footer> <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>