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