comparison index.php @ 3:9ea8be5b28d1

"refactor".
author edogawaconan <me@myconan.net>
date Tue, 21 Oct 2014 18:01:43 +0900
parents 9b5884ba214a
children 7812af1c23ae
comparison
equal deleted inserted replaced
2:9b5884ba214a 3:9ea8be5b28d1
22 function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } 22 function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); }
23 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } 23 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); }
24 function link_to($target, $title) { 24 function link_to($target, $title) {
25 return("<a href=\"".a($target)."\">".h($title)."</a>"); 25 return("<a href=\"".a($target)."\">".h($title)."</a>");
26 } 26 }
27 function print_header() { 27 function path_tree_header() {
28 global $link_prefix, $link_base, $to_dir; 28 global $link_prefix, $link_base, $to_dir;
29 $path_tree = link_to('/', '[root]') . "/"; 29 $path_tree = link_to('/', '[root]') . "/";
30 foreach ($to_dir as $level => $dir) { 30 foreach ($to_dir as $level => $dir) {
31 if($dir) { 31 if($dir) {
32 $link = "/"; 32 $link = "/";
33 for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; } 33 for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; }
34 $path_tree .= link_to($link, $dir)."/"; 34 $path_tree .= link_to($link, $dir)."/";
35 } 35 }
36 } 36 }
37 header('Content-Type: text/html; charset=utf-8'); 37 return "Index of $path_tree";
38 echo "<!doctype html>
39 <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">
40 <title>Index of ", h(rtrim($link_base,"/")."/"), "</title>
41 <body>
42 <h1>Index of $path_tree</h1>";
43 } 38 }
39 function title() {
40 global $link_base;
41 return "Index of ".h(rtrim($link_base,"/")."/");
42 }
43
44 function nice_size($size) { 44 function nice_size($size) {
45 static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); 45 $thousand_units = array('ko', 'Mo', 'Go', 'To', 'Po');
46 for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; } 46
47 if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); } 47 $return_format = "%d %s";
48 else { return(sprintf("%.2f %s", $size, $unit[$i])); } 48
49 if ($size <= 1) {
50 $return_unit = "octet";
51 } elseif ($size < 10000) {
52 $return_unit = "octets";
53 } else {
54 $size /= 1000;
55 for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; }
56 $return_format = "%.2f %s";
57 $return_unit = $thousand_units[$i];
58 }
59 return sprintf($return_format, $size, $return_unit);
49 } 60 }
61
50 function file_rows($files, $is_dir) { 62 function file_rows($files, $is_dir) {
51 global $path, $link_base, $link_prefix; 63 global $path, $link_base, $link_prefix;
52 static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); 64
53 $file_rows = ""; 65 $file_rows = "";
54 if ($is_dir) { 66 if ($is_dir) {
55 $file_suffix = "/"; 67 $file_suffix = "/";
56 if($link_base != $link_prefix) { 68 if($link_base != $link_prefix) {
57 $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>"; 69 $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base)."/","..")."</td></tr>";
58 } 70 }
59 } else { $file_suffix = ""; } 71 } else { $file_suffix = ""; }
72
60 foreach($files as $file) { 73 foreach($files as $file) {
61 $file_stat = stat("$path/".$file); 74 $file_stat = stat("$path/".$file);
62 $file_rows .= "<tr><td>". 75
63 link_to("$link_base/".$file, $file.$file_suffix)."</td><td>". 76 $file_rows .= "<tr>";
64 h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>"; 77 $file_rows .= "<td>".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix)."</td>";
78
79 $file_rows .= "<td>";
65 if ($is_dir) { $file_rows .= "[dir]"; } 80 if ($is_dir) { $file_rows .= "[dir]"; }
66 else { $file_rows .= nice_size($file_stat['size']); } 81 else { $file_rows .= nice_size($file_stat['size']); }
82 $file_rows .= "</td>";
83
84 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>";
85
67 $file_rows .= "</tr>"; 86 $file_rows .= "</tr>";
68 } 87 }
69 return $file_rows; 88 return $file_rows;
70 } 89 }
71 function print_list() {
72 global $dirs, $files;
73 echo "<table><thead><tr>",
74 "<td>File</td>",
75 "<td>Date</td>",
76 "<td>Size</td>",
77 "</tr></thead>",
78 "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>",
79 "</table>";
80 }
81 function print_footer() {
82 echo "</body>";
83 }
84 print_header();
85 print_list();
86 print_footer();
87 ?> 90 ?>
91 <?php header('Content-Type: text/html; charset=utf-8'); ?>
92 <!doctype html>
93 <head>
94 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
95 <title><?php echo title(); ?></title>
96 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
97 <style type="text/css">
98 * { box-sizing: border-box; }
99 body {
100 font-family: Segoe UI, sans-serif;
101 font-size: 14px;
102 }
103 h1 { margin: 5px; }
104 table {
105 width: 100%;
106 }
107 th:first-child, td:first-child {
108 width: 100%;
109 white-space: pre-wrap;
110 word-wrap: break-word;
111 word-break: break-all;
112 }
113 tr {
114 position: relative;
115 }
116 th, td {
117 white-space: nowrap;
118 padding: 2px 5px;
119 }
120
121 @media (min-width: 768px) {
122 th { background: #ccc; }
123 tr:nth-child(even) { background: #eee; }
124 tr:hover { background: #ddd; }
125 }
126
127 @media (max-width: 767px) {
128 table {
129 border-spacing: 0 10px;
130 }
131 th { display: none; }
132 tr {
133 background: #eee;
134 }
135 td {
136 display: inline-block;
137 }
138 td:first-child {
139 background: #ddd;
140 padding: 5px;
141 }
142 table a {
143 font-size: 18px;
144 display: block;
145 }
146 }
147 </style>
148 </head>
149 <body>
150 <h1><?php echo path_tree_header(); ?></h1>
151
152 <table>
153 <thead><tr>
154 <th>File</th>
155 <th>Size</th>
156 <th>Date</th>
157 </tr></thead>
158 <tbody>
159 <?php echo file_rows($dirs, true); ?>
160 <?php echo file_rows($files, false); ?>
161 </tbody>
162 </table>
163 </body>