# HG changeset patch # User edogawaconan # Date 1413894698 -32400 # Node ID 7812af1c23ae90fd3247d3bf7d59f11f9bf158f8 # Parent 9ea8be5b28d1e89f0ed7f926401ca4fd402139ab Actual refactor. diff -r 9ea8be5b28d1 -r 7812af1c23ae index.php --- 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 @@ ".h($title).""); - } - 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('' . h($title) . ""); } - 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 .= "".link_to(dirname($link_base)."/","..").""; + + if ($path !== "/") { + $file_rows .= "" . link_to(dirname($prefix . $path) . "/", "..") . ""; } - } else { $file_suffix = ""; } + } foreach($files as $file) { - $file_stat = stat("$path/".$file); + $file_stat = stat($root . $path . "/". $file); $file_rows .= ""; - $file_rows .= "".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix).""; + $file_rows .= "".link_to($file . $file_suffix, $file . $file_suffix).""; $file_rows .= ""; if ($is_dir) { $file_rows .= "[dir]"; } - else { $file_rows .= nice_size($file_stat['size']); } + else { $file_rows .= human_size($file_stat['size']); } $file_rows .= ""; $file_rows .= "".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime'])).""; @@ -92,7 +125,7 @@ - <?php echo title(); ?> + Index of <?php echo h($prefix . $path); ?> -

+

Index of

@@ -160,4 +193,11 @@
+ +