comparison index.php @ 2:9b5884ba214a

Fix indentation.
author edogawaconan <me@myconan.net>
date Tue, 21 Oct 2014 15:31:30 +0900
parents 8cba59d07798
children 9ea8be5b28d1
comparison
equal deleted inserted replaced
1:9ba6983b7c19 2:9b5884ba214a
1 <?php 1 <?php
2 //real men use UTC for internets 2 //real men use UTC for internets
3 date_default_timezone_set('UTC'); 3 date_default_timezone_set('UTC');
4 $wwwroot = $_ENV["DOCUMENT_ROOT"]; 4 $wwwroot = $_ENV["DOCUMENT_ROOT"];
5 $link_prefix = "/"; 5 $link_prefix = "/";
6 $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/'); 6 $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/');
7 $link_path = substr($link_base,strlen(utf8_decode($link_prefix))); 7 $link_path = substr($link_base,strlen(utf8_decode($link_prefix)));
8 $path = realpath("$wwwroot/".$link_path); 8 $path = realpath("$wwwroot/".$link_path);
9 if (!$path) { die("fail"); } 9 if (!$path) { die("fail"); }
10 $dir_handle = @opendir($path); 10 $dir_handle = @opendir($path);
11 $to_dir = explode('/', trim($link_path,'/')); 11 $to_dir = explode('/', trim($link_path,'/'));
12 array_unshift($to_dir, trim($link_prefix,'/')); 12 array_unshift($to_dir, trim($link_prefix,'/'));
13 $files = array(); 13 $files = array();
14 $dirs = array(); 14 $dirs = array();
15 while ($file = readdir($dir_handle)) 15 while ($file = readdir($dir_handle)) {
16 { 16 if ($file == '.' or $file == '..') { continue; }
17 if ($file == '.' or $file == '..') { continue; } 17 elseif (is_dir("$path/$file")) { $dirs[] = $file; }
18 elseif (is_dir("$path/$file")) { $dirs[] = $file; } 18 else { $files[] = $file; }
19 else { $files[] = $file; } 19 }
20 } 20 sort($files);
21 sort($files); 21 sort($dirs);
22 sort($dirs); 22 function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); }
23 function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } 23 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); }
24 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } 24 function link_to($target, $title) {
25 function link_to($target, $title) 25 return("<a href=\"".a($target)."\">".h($title)."</a>");
26 { 26 }
27 return("<a href=\"".a($target)."\">".h($title)."</a>"); 27 function print_header() {
28 } 28 global $link_prefix, $link_base, $to_dir;
29 function print_header() 29 $path_tree = link_to('/', '[root]') . "/";
30 { 30 foreach ($to_dir as $level => $dir) {
31 global $link_prefix, $link_base, $to_dir; 31 if($dir) {
32 $path_tree = link_to('/', '[root]') . "/"; 32 $link = "/";
33 foreach ($to_dir as $level => $dir) 33 for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; }
34 { 34 $path_tree .= link_to($link, $dir)."/";
35 if($dir) 35 }
36 { 36 }
37 $link = "/"; 37 header('Content-Type: text/html; charset=utf-8');
38 for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; } 38 echo "<!doctype html>
39 $path_tree .= link_to($link, $dir)."/"; 39 <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">
40 } 40 <title>Index of ", h(rtrim($link_base,"/")."/"), "</title>
41 } 41 <body>
42 header('Content-Type: text/html; charset=utf-8'); 42 <h1>Index of $path_tree</h1>";
43 echo "<!doctype html> 43 }
44 <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"> 44 function nice_size($size) {
45 <title>Index of ", h(rtrim($link_base,"/")."/"), "</title> 45 static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
46 <body> 46 for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; }
47 <h1>Index of $path_tree</h1>"; 47 if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); }
48 } 48 else { return(sprintf("%.2f %s", $size, $unit[$i])); }
49 function nice_size($size) 49 }
50 { 50 function file_rows($files, $is_dir) {
51 static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); 51 global $path, $link_base, $link_prefix;
52 for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; } 52 static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
53 if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); } 53 $file_rows = "";
54 else { return(sprintf("%.2f %s", $size, $unit[$i])); } 54 if ($is_dir) {
55 } 55 $file_suffix = "/";
56 function file_rows($files, $is_dir) 56 if($link_base != $link_prefix) {
57 { 57 $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>";
58 global $path, $link_base, $link_prefix; 58 }
59 static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); 59 } else { $file_suffix = ""; }
60 $file_rows = ""; 60 foreach($files as $file) {
61 if ($is_dir) 61 $file_stat = stat("$path/".$file);
62 { 62 $file_rows .= "<tr><td>".
63 $file_suffix = "/"; 63 link_to("$link_base/".$file, $file.$file_suffix)."</td><td>".
64 if($link_base != $link_prefix) 64 h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>";
65 { 65 if ($is_dir) { $file_rows .= "[dir]"; }
66 $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>"; 66 else { $file_rows .= nice_size($file_stat['size']); }
67 } 67 $file_rows .= "</tr>";
68 } else { $file_suffix = ""; } 68 }
69 foreach($files as $file) 69 return $file_rows;
70 { 70 }
71 $file_stat = stat("$path/".$file); 71 function print_list() {
72 $file_rows .= "<tr><td>". 72 global $dirs, $files;
73 link_to("$link_base/".$file, $file.$file_suffix)."</td><td>". 73 echo "<table><thead><tr>",
74 h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>"; 74 "<td>File</td>",
75 if ($is_dir) { $file_rows .= "[dir]"; } 75 "<td>Date</td>",
76 else { $file_rows .= nice_size($file_stat['size']); } 76 "<td>Size</td>",
77 $file_rows .= "</tr>"; 77 "</tr></thead>",
78 } 78 "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>",
79 return $file_rows; 79 "</table>";
80 } 80 }
81 function print_list() 81 function print_footer() {
82 { 82 echo "</body>";
83 global $dirs, $files; 83 }
84 echo "<table><thead><tr>", 84 print_header();
85 "<td>File</td>", 85 print_list();
86 "<td>Date</td>", 86 print_footer();
87 "<td>Size</td>",
88 "</tr></thead>",
89 "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>",
90 "</table>";
91 }
92 function print_footer()
93 {
94 echo "</body>";
95 }
96 print_header();
97 print_list();
98 print_footer();
99 ?> 87 ?>