# HG changeset patch # User edogawaconan # Date 1413873090 -32400 # Node ID 9b5884ba214a336c40db778cb6c0184ad0e45e28 # Parent 9ba6983b7c199aae186e122092495bc439400290 Fix indentation. diff -r 9ba6983b7c19 -r 9b5884ba214a index.php --- a/index.php Thu Oct 16 21:41:30 2014 +0900 +++ b/index.php Tue Oct 21 15:31:30 2014 +0900 @@ -1,99 +1,87 @@ ".h($title).""); -} -function print_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)."/"; - } - } - header('Content-Type: text/html; charset=utf-8'); - echo " - - Index of ", h(rtrim($link_base,"/")."/"), " - -

Index of $path_tree

"; -} -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])); } -} -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),"..").""; - } - } 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'])).""; - if ($is_dir) { $file_rows .= "[dir]"; } - else { $file_rows .= nice_size($file_stat['size']); } - $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(); + //real men use UTC for internets + 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,'/')); + $files = array(); + $dirs = array(); + while ($file = readdir($dir_handle)) { + if ($file == '.' or $file == '..') { continue; } + elseif (is_dir("$path/$file")) { $dirs[] = $file; } + else { $files[] = $file; } + } + sort($files); + sort($dirs); + 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("".h($title).""); + } + function print_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)."/"; + } + } + header('Content-Type: text/html; charset=utf-8'); + echo " + + Index of ", h(rtrim($link_base,"/")."/"), " + +

Index of $path_tree

"; + } + 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])); } + } + 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),"..").""; + } + } 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'])).""; + if ($is_dir) { $file_rows .= "[dir]"; } + else { $file_rows .= nice_size($file_stat['size']); } + $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(); ?>