Mercurial > dirlist-php
changeset 2:9b5884ba214a
Fix indentation.
author | edogawaconan <me@myconan.net> |
---|---|
date | Tue, 21 Oct 2014 15:31:30 +0900 |
parents | 9ba6983b7c19 |
children | 9ea8be5b28d1 |
files | index.php |
diffstat | 1 files changed, 85 insertions(+), 97 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ <?php -//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("<a href=\"".a($target)."\">".h($title)."</a>"); -} -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 "<!doctype html> - <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"> - <title>Index of ", h(rtrim($link_base,"/")."/"), "</title> - <body> - <h1>Index of $path_tree</h1>"; -} -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 .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>"; - } - } else { $file_suffix = ""; } - foreach($files as $file) - { - $file_stat = stat("$path/".$file); - $file_rows .= "<tr><td>". - link_to("$link_base/".$file, $file.$file_suffix)."</td><td>". - h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>"; - if ($is_dir) { $file_rows .= "[dir]"; } - else { $file_rows .= nice_size($file_stat['size']); } - $file_rows .= "</tr>"; - } - return $file_rows; -} -function print_list() -{ - global $dirs, $files; - echo "<table><thead><tr>", - "<td>File</td>", - "<td>Date</td>", - "<td>Size</td>", - "</tr></thead>", - "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>", - "</table>"; -} -function print_footer() -{ -echo "</body>"; -} -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("<a href=\"".a($target)."\">".h($title)."</a>"); + } + 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 "<!doctype html> + <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"> + <title>Index of ", h(rtrim($link_base,"/")."/"), "</title> + <body> + <h1>Index of $path_tree</h1>"; + } + 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 .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>"; + } + } else { $file_suffix = ""; } + foreach($files as $file) { + $file_stat = stat("$path/".$file); + $file_rows .= "<tr><td>". + link_to("$link_base/".$file, $file.$file_suffix)."</td><td>". + h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>"; + if ($is_dir) { $file_rows .= "[dir]"; } + else { $file_rows .= nice_size($file_stat['size']); } + $file_rows .= "</tr>"; + } + return $file_rows; + } + function print_list() { + global $dirs, $files; + echo "<table><thead><tr>", + "<td>File</td>", + "<td>Date</td>", + "<td>Size</td>", + "</tr></thead>", + "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>", + "</table>"; + } + function print_footer() { + echo "</body>"; + } + print_header(); + print_list(); + print_footer(); ?>