Mercurial > dirlist-php
changeset 4:7812af1c23ae
Actual refactor.
author | edogawaconan <me@myconan.net> |
---|---|
date | Tue, 21 Oct 2014 21:31:38 +0900 |
parents | 9ea8be5b28d1 |
children | b84ce3ef4c9d |
files | index.php |
diffstat | 1 files changed, 85 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/index.php Tue Oct 21 18:01:43 2014 +0900 +++ b/index.php Tue Oct 21 21:31:38 2014 +0900 @@ -1,54 +1,65 @@ <?php - //real men use UTC for internets + // Required for strftime(). Set to UTC because :internet:. date_default_timezone_set('UTC'); - $wwwroot = $_ENV["DOCUMENT_ROOT"]; - $link_prefix = "/"; - $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/'); - $link_path = substr($link_base,strlen(utf8_decode($link_prefix))); - $path = realpath("$wwwroot/".$link_path); - if (!$path) { die("fail"); } - $dir_handle = @opendir($path); - $to_dir = explode('/', trim($link_path,'/')); - array_unshift($to_dir, trim($link_prefix,'/')); + + // $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"]; + + $path = substr($path, strlen(utf8_decode($prefix))); + if ($path === false) { $path = "/"; } + + // root of directory listing. + $root = $_SERVER["DL_ROOT"]; + if ($root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; } + + // current directory being listed. + $current_dir = $root . $path; + + if (realpath($root . $path) === false) { + header("HTTP/1.0 404 Not Found"); + } elseif (substr($current_dir, -1) !== "/") { + header("Location: " . $path . "/", false, 301); + } + + if (http_response_code() !== 200) { exit; } + + // $link_prefix = "/"; + // $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/'); + // $link_path = substr($link_base,strlen(utf8_decode($link_prefix))); + // $path = realpath("$wwwroot/".$link_path); + // if (!$path) { die("fail"); } + + $dir_handle = @opendir($root . $path); $files = array(); $dirs = array(); - while ($file = readdir($dir_handle)) { - if ($file == '.' or $file == '..') { continue; } - elseif (is_dir("$path/$file")) { $dirs[] = $file; } + while (($file = readdir($dir_handle)) !== false) { + if ($file === "." or $file === "..") { continue; } + elseif (is_dir($root . $path . $file)) { $dirs[] = $file; } else { $files[] = $file; } } sort($files); sort($dirs); - function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } + + // 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 path_tree_header() { - global $link_prefix, $link_base, $to_dir; - $path_tree = link_to('/', '[root]') . "/"; - foreach ($to_dir as $level => $dir) { - if($dir) { - $link = "/"; - for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; } - $path_tree .= link_to($link, $dir)."/"; - } - } - return "Index of $path_tree"; - } - function title() { - global $link_base; - return "Index of ".h(rtrim($link_base,"/")."/"); - } + function link_to($target, $title) { return('<a href="' . a($target) . '">' . h($title) . "</a>"); } - function nice_size($size) { - $thousand_units = array('ko', 'Mo', 'Go', 'To', 'Po'); + function human_size($size) { + $thousand_units = array("ko", "Mo", "Go", "To", "Po"); $return_format = "%d %s"; if ($size <= 1) { $return_unit = "octet"; - } elseif ($size < 10000) { + } elseif ($size < 1000) { $return_unit = "octets"; } else { $size /= 1000; @@ -58,27 +69,49 @@ } 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 = "/"; + $tree_link = link_to($tree_path, "[root]") . "/"; + + 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, $link_base, $link_prefix; + global $path, $root, $prefix; $file_rows = ""; + $file_suffix = ""; + if ($is_dir) { $file_suffix = "/"; - if($link_base != $link_prefix) { - $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base)."/","..")."</td></tr>"; + + if ($path !== "/") { + $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "..") . "</td></tr>"; } - } else { $file_suffix = ""; } + } foreach($files as $file) { - $file_stat = stat("$path/".$file); + $file_stat = stat($root . $path . "/". $file); $file_rows .= "<tr>"; - $file_rows .= "<td>".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix)."</td>"; + $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; $file_rows .= "<td>"; if ($is_dir) { $file_rows .= "[dir]"; } - else { $file_rows .= nice_size($file_stat['size']); } + 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>"; @@ -92,7 +125,7 @@ <!doctype html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> - <title><?php echo title(); ?></title> + <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"> <style type="text/css"> * { box-sizing: border-box; } @@ -147,7 +180,7 @@ </style> </head> <body> - <h1><?php echo path_tree_header(); ?></h1> + <h1>Index of <?php echo tree_link(); ?></h1> <table> <thead><tr> @@ -160,4 +193,11 @@ <?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> </body>