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