# HG changeset patch # User edogawaconan # Date 1413882103 -32400 # Node ID 9ea8be5b28d1e89f0ed7f926401ca4fd402139ab # Parent 9b5884ba214a336c40db778cb6c0184ad0e45e28 "refactor". diff -r 9b5884ba214a -r 9ea8be5b28d1 index.php --- a/index.php Tue Oct 21 15:31:30 2014 +0900 +++ b/index.php Tue Oct 21 18:01:43 2014 +0900 @@ -24,7 +24,7 @@ function link_to($target, $title) { return("".h($title).""); } - function print_header() { + function path_tree_header() { global $link_prefix, $link_base, $to_dir; $path_tree = link_to('/', '[root]') . "/"; foreach ($to_dir as $level => $dir) { @@ -34,54 +34,130 @@ $path_tree .= link_to($link, $dir)."/"; } } - header('Content-Type: text/html; charset=utf-8'); - echo " - - Index of ", h(rtrim($link_base,"/")."/"), " - -

Index of $path_tree

"; + return "Index of $path_tree"; + } + function title() { + global $link_base; + return "Index of ".h(rtrim($link_base,"/")."/"); } + function nice_size($size) { - static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); - for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; } - if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); } - else { return(sprintf("%.2f %s", $size, $unit[$i])); } + $thousand_units = array('ko', 'Mo', 'Go', 'To', 'Po'); + + $return_format = "%d %s"; + + if ($size <= 1) { + $return_unit = "octet"; + } elseif ($size < 10000) { + $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); } + function file_rows($files, $is_dir) { global $path, $link_base, $link_prefix; - static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); + $file_rows = ""; if ($is_dir) { $file_suffix = "/"; if($link_base != $link_prefix) { - $file_rows .= "".link_to(dirname($link_base),"..").""; + $file_rows .= "".link_to(dirname($link_base)."/","..").""; } } else { $file_suffix = ""; } + foreach($files as $file) { $file_stat = stat("$path/".$file); - $file_rows .= "". - link_to("$link_base/".$file, $file.$file_suffix)."". - h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime'])).""; + + $file_rows .= ""; + $file_rows .= "".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix).""; + + $file_rows .= ""; if ($is_dir) { $file_rows .= "[dir]"; } else { $file_rows .= nice_size($file_stat['size']); } + $file_rows .= ""; + + $file_rows .= "".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime'])).""; + $file_rows .= ""; } return $file_rows; } - function print_list() { - global $dirs, $files; - echo "", - "", - "", - "", - "", - "", file_rows($dirs, true), file_rows($files, false), "", - "
FileDateSize
"; - } - function print_footer() { - echo ""; - } - print_header(); - print_list(); - print_footer(); ?> + + + + + <?php echo title(); ?> + + + + +

+ + + + + + + + + + + +
FileSizeDate
+