Mercurial > dirlist-php
annotate index.php @ 5:b84ce3ef4c9d
Fix behaviour difference between version.
author | edogawaconan <me@myconan.net> |
---|---|
date | Tue, 21 Oct 2014 21:41:49 +0900 |
parents | 7812af1c23ae |
children | 8df269a2efd8 |
rev | line source |
---|---|
0 | 1 <?php |
4 | 2 // Required for strftime(). Set to UTC because :internet:. |
2 | 3 date_default_timezone_set('UTC'); |
4 | 4 |
5 // $path: actual requested path, with $alias_prefix removed, relative to $root. | |
6 $path = $_SERVER["REQUEST_URI"]; | |
7 $query_string_start = strpos($path, "?"); | |
8 if ($query_string_start !== false) { | |
9 $path = substr($path, 0, $query_string_start); | |
10 } | |
11 $path = urldecode($path); | |
12 | |
13 $prefix = $_SERVER["DL_PREFIX"]; | |
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
14 if ($prefix === null) { $prefix = ""; } |
4 | 15 |
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
16 $path = substr($path, strlen($prefix)); |
4 | 17 if ($path === false) { $path = "/"; } |
18 | |
19 // root of directory listing. | |
20 $root = $_SERVER["DL_ROOT"]; | |
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
21 if ($root === null || $root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; } |
4 | 22 |
23 // current directory being listed. | |
24 $current_dir = $root . $path; | |
25 | |
26 if (realpath($root . $path) === false) { | |
27 header("HTTP/1.0 404 Not Found"); | |
28 } elseif (substr($current_dir, -1) !== "/") { | |
29 header("Location: " . $path . "/", false, 301); | |
30 } | |
31 | |
32 if (http_response_code() !== 200) { exit; } | |
33 | |
34 // $link_prefix = "/"; | |
35 // $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/'); | |
36 // $link_path = substr($link_base,strlen(utf8_decode($link_prefix))); | |
37 // $path = realpath("$wwwroot/".$link_path); | |
38 // if (!$path) { die("fail"); } | |
39 | |
40 $dir_handle = @opendir($root . $path); | |
2 | 41 $files = array(); |
42 $dirs = array(); | |
4 | 43 while (($file = readdir($dir_handle)) !== false) { |
44 if ($file === "." or $file === "..") { continue; } | |
45 elseif (is_dir($root . $path . $file)) { $dirs[] = $file; } | |
2 | 46 else { $files[] = $file; } |
47 } | |
48 sort($files); | |
49 sort($dirs); | |
4 | 50 |
51 // BEGIN UTILITY | |
52 function h($string) { return htmlspecialchars($string, ENT_QUOTES, "UTF-8"); } | |
2 | 53 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } |
4 | 54 function link_to($target, $title) { return('<a href="' . a($target) . '">' . h($title) . "</a>"); } |
3 | 55 |
4 | 56 function human_size($size) { |
57 $thousand_units = array("ko", "Mo", "Go", "To", "Po"); | |
3 | 58 |
59 $return_format = "%d %s"; | |
60 | |
61 if ($size <= 1) { | |
62 $return_unit = "octet"; | |
4 | 63 } elseif ($size < 1000) { |
3 | 64 $return_unit = "octets"; |
65 } else { | |
66 $size /= 1000; | |
67 for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; } | |
68 $return_format = "%.2f %s"; | |
69 $return_unit = $thousand_units[$i]; | |
70 } | |
71 return sprintf($return_format, $size, $return_unit); | |
2 | 72 } |
4 | 73 // END UTILITY |
74 | |
75 function tree_link() { | |
76 global $path, $prefix; | |
77 | |
78 $path_array = explode("/", trim($path, "/")); | |
79 array_unshift($path_array, trim($prefix, "/")); | |
80 | |
81 $tree_path = "/"; | |
82 $tree_link = link_to($tree_path, "[root]") . "/"; | |
83 | |
84 foreach ($path_array as $p) { | |
85 if ($p === "") { continue; } | |
86 $tree_path .= $p . "/"; | |
87 $tree_link .= link_to($tree_path, $p) . "/"; | |
88 } | |
89 | |
90 return $tree_link; | |
91 } | |
3 | 92 |
2 | 93 function file_rows($files, $is_dir) { |
4 | 94 global $path, $root, $prefix; |
3 | 95 |
2 | 96 $file_rows = ""; |
4 | 97 $file_suffix = ""; |
98 | |
2 | 99 if ($is_dir) { |
100 $file_suffix = "/"; | |
4 | 101 |
102 if ($path !== "/") { | |
103 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "..") . "</td></tr>"; | |
2 | 104 } |
4 | 105 } |
3 | 106 |
2 | 107 foreach($files as $file) { |
4 | 108 $file_stat = stat($root . $path . "/". $file); |
3 | 109 |
110 $file_rows .= "<tr>"; | |
4 | 111 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; |
3 | 112 |
113 $file_rows .= "<td>"; | |
2 | 114 if ($is_dir) { $file_rows .= "[dir]"; } |
4 | 115 else { $file_rows .= human_size($file_stat['size']); } |
3 | 116 $file_rows .= "</td>"; |
117 | |
118 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; | |
119 | |
2 | 120 $file_rows .= "</tr>"; |
121 } | |
122 return $file_rows; | |
123 } | |
0 | 124 ?> |
3 | 125 <?php header('Content-Type: text/html; charset=utf-8'); ?> |
126 <!doctype html> | |
127 <head> | |
128 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | |
4 | 129 <title>Index of <?php echo h($prefix . $path); ?></title> |
3 | 130 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
131 <style type="text/css"> | |
132 * { box-sizing: border-box; } | |
133 body { | |
134 font-family: Segoe UI, sans-serif; | |
135 font-size: 14px; | |
136 } | |
137 h1 { margin: 5px; } | |
138 table { | |
139 width: 100%; | |
140 } | |
141 th:first-child, td:first-child { | |
142 width: 100%; | |
143 white-space: pre-wrap; | |
144 word-wrap: break-word; | |
145 word-break: break-all; | |
146 } | |
147 tr { | |
148 position: relative; | |
149 } | |
150 th, td { | |
151 white-space: nowrap; | |
152 padding: 2px 5px; | |
153 } | |
154 | |
155 @media (min-width: 768px) { | |
156 th { background: #ccc; } | |
157 tr:nth-child(even) { background: #eee; } | |
158 tr:hover { background: #ddd; } | |
159 } | |
160 | |
161 @media (max-width: 767px) { | |
162 table { | |
163 border-spacing: 0 10px; | |
164 } | |
165 th { display: none; } | |
166 tr { | |
167 background: #eee; | |
168 } | |
169 td { | |
170 display: inline-block; | |
171 } | |
172 td:first-child { | |
173 background: #ddd; | |
174 padding: 5px; | |
175 } | |
176 table a { | |
177 font-size: 18px; | |
178 display: block; | |
179 } | |
180 } | |
181 </style> | |
182 </head> | |
183 <body> | |
4 | 184 <h1>Index of <?php echo tree_link(); ?></h1> |
3 | 185 |
186 <table> | |
187 <thead><tr> | |
188 <th>File</th> | |
189 <th>Size</th> | |
190 <th>Date</th> | |
191 </tr></thead> | |
192 <tbody> | |
193 <?php echo file_rows($dirs, true); ?> | |
194 <?php echo file_rows($files, false); ?> | |
195 </tbody> | |
196 </table> | |
4 | 197 |
198 <footer> | |
199 <hr> | |
200 <em> | |
201 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php</a>. | |
202 Powered by PHP <?php echo phpversion(); ?>. | |
203 </em> | |
3 | 204 </body> |