Mercurial > dirlist-php
annotate index.php @ 28:ce92f4d41714
Add support for displaying hidden files.
author | edogawaconan <me@myconan.net> |
---|---|
date | Thu, 30 Oct 2014 12:14:32 +0900 |
parents | aa3f0003fb41 |
children | c482f0db5c28 |
rev | line source |
---|---|
0 | 1 <?php |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
2 define('DL_VERSION', '2.0.0'); |
4 | 3 // Required for strftime(). Set to UTC because :internet:. |
2 | 4 date_default_timezone_set('UTC'); |
4 | 5 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
6 // $uri: web-facing path |
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
7 $uri = $_SERVER["REQUEST_URI"]; |
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
8 $query_string_start = strpos($uri, "?"); |
4 | 9 if ($query_string_start !== false) { |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
10 $uri = substr($uri, 0, $query_string_start); |
4 | 11 } |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
12 $uri = urldecode($uri); |
4 | 13 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
14 // $dir: filesystem path |
26
eee7ca924a5e
Make it work with apache (at least XAMPP).
edogawaconan <me@myconan.net>
parents:
25
diff
changeset
|
15 if (isset($_SERVER["DL_DIR"])) { $dir = $_SERVER["DL_DIR"]; } |
eee7ca924a5e
Make it work with apache (at least XAMPP).
edogawaconan <me@myconan.net>
parents:
25
diff
changeset
|
16 elseif (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"])) { $dir = $_SERVER["CONTEXT_DOCUMENT_ROOT"]; } |
eee7ca924a5e
Make it work with apache (at least XAMPP).
edogawaconan <me@myconan.net>
parents:
25
diff
changeset
|
17 else { $dir = $_SERVER["DOCUMENT_ROOT"] . $uri; } |
4 | 18 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
19 if (realpath($dir) === false) { |
4 | 20 header("HTTP/1.0 404 Not Found"); |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
21 } elseif (substr($uri, -1) !== "/") { |
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
22 header("Location: " . $uri . "/"); |
4 | 23 } |
24 | |
25 if (http_response_code() !== 200) { exit; } | |
26 | |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
27 $dir_handle = @opendir($dir); |
2 | 28 $files = array(); |
29 $dirs = array(); | |
4 | 30 while (($file = readdir($dir_handle)) !== false) { |
28
ce92f4d41714
Add support for displaying hidden files.
edogawaconan <me@myconan.net>
parents:
27
diff
changeset
|
31 if ($file === "." || $file === "..") { continue; } |
ce92f4d41714
Add support for displaying hidden files.
edogawaconan <me@myconan.net>
parents:
27
diff
changeset
|
32 elseif (!(isset($_SERVER["DL_SHOWALL"]) && $_SERVER["DL_SHOWALL"] === "1") && substr($file, 0, 1) === ".") { continue; } |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
33 elseif (is_dir($dir . $file)) { $dirs[] = $file; } |
2 | 34 else { $files[] = $file; } |
35 } | |
36 sort($files); | |
37 sort($dirs); | |
4 | 38 |
39 // BEGIN UTILITY | |
40 function h($string) { return htmlspecialchars($string, ENT_QUOTES, "UTF-8"); } | |
2 | 41 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } |
17
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
42 function link_to($target, $title) { return('<a href="' . a($target) . '">' . h($title) . "</a>"); } |
3 | 43 |
4 | 44 function human_size($size) { |
45 $thousand_units = array("ko", "Mo", "Go", "To", "Po"); | |
3 | 46 |
47 $return_format = "%d %s"; | |
48 | |
49 if ($size <= 1) { | |
50 $return_unit = "octet"; | |
4 | 51 } elseif ($size < 1000) { |
3 | 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); | |
2 | 60 } |
4 | 61 // END UTILITY |
62 | |
63 function tree_link() { | |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
64 global $uri; |
4 | 65 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
66 $uri_array = explode("/", trim($uri, "/")); |
4 | 67 |
68 $tree_path = "/"; | |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
69 $tree_link = link_to($tree_path, "[root]"); |
20
caf498a0c602
Hide [root] for aliased directory.
edogawaconan <me@myconan.net>
parents:
19
diff
changeset
|
70 $tree_link .= "/"; |
4 | 71 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
72 foreach ($uri_array as $p) { |
4 | 73 if ($p === "") { continue; } |
74 $tree_path .= $p . "/"; | |
75 $tree_link .= link_to($tree_path, $p) . "/"; | |
76 } | |
77 | |
78 return $tree_link; | |
79 } | |
3 | 80 |
2 | 81 function file_rows($files, $is_dir) { |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
82 global $dir, $uri; |
3 | 83 |
2 | 84 $file_rows = ""; |
4 | 85 $file_suffix = ""; |
86 | |
2 | 87 if ($is_dir) { |
88 $file_suffix = "/"; | |
4 | 89 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
90 if ($uri !== "/") { |
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
91 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>"; |
2 | 92 } |
4 | 93 } |
3 | 94 |
2 | 95 foreach($files as $file) { |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
96 $file_stat = stat($dir . "/". $file); |
3 | 97 |
98 $file_rows .= "<tr>"; | |
4 | 99 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; |
3 | 100 |
101 $file_rows .= "<td>"; | |
2 | 102 if ($is_dir) { $file_rows .= "[dir]"; } |
4 | 103 else { $file_rows .= human_size($file_stat['size']); } |
3 | 104 $file_rows .= "</td>"; |
105 | |
106 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; | |
107 | |
2 | 108 $file_rows .= "</tr>"; |
109 } | |
110 return $file_rows; | |
111 } | |
0 | 112 ?> |
3 | 113 <?php header('Content-Type: text/html; charset=utf-8'); ?> |
114 <!doctype html> | |
115 <head> | |
116 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
117 <title>Index of <?php echo h($uri); ?></title> |
3 | 118 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
14 | 119 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css"> |
3 | 120 <style type="text/css"> |
16
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
121 .lb-data a { color: #ccc; } |
18
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
122 .lightbox { |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
123 position: fixed; |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
124 top: 50px !important; |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
125 } |
16
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
126 </style> |
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
127 <style type="text/css"> |
3 | 128 * { box-sizing: border-box; } |
129 body { | |
130 font-family: Segoe UI, sans-serif; | |
131 font-size: 14px; | |
132 } | |
133 h1 { margin: 5px; } | |
134 table { | |
135 width: 100%; | |
136 } | |
137 th:first-child, td:first-child { | |
138 width: 100%; | |
139 white-space: pre-wrap; | |
140 word-wrap: break-word; | |
141 word-break: break-all; | |
142 } | |
143 tr { | |
144 position: relative; | |
145 } | |
146 th, td { | |
147 white-space: nowrap; | |
148 padding: 2px 5px; | |
149 } | |
150 | |
151 @media (min-width: 768px) { | |
152 th { background: #ccc; } | |
153 tr:nth-child(even) { background: #eee; } | |
154 tr:hover { background: #ddd; } | |
155 } | |
156 | |
157 @media (max-width: 767px) { | |
158 table { | |
159 border-spacing: 0 10px; | |
160 } | |
161 th { display: none; } | |
162 tr { | |
163 background: #eee; | |
164 } | |
165 td { | |
166 display: inline-block; | |
167 } | |
168 td:first-child { | |
169 background: #ddd; | |
170 padding: 5px; | |
171 } | |
172 table a { | |
173 font-size: 18px; | |
174 display: block; | |
175 } | |
176 } | |
177 </style> | |
178 </head> | |
179 <body> | |
4 | 180 <h1>Index of <?php echo tree_link(); ?></h1> |
3 | 181 |
182 <table> | |
183 <thead><tr> | |
184 <th>File</th> | |
185 <th>Size</th> | |
186 <th>Date</th> | |
187 </tr></thead> | |
188 <tbody> | |
189 <?php echo file_rows($dirs, true); ?> | |
190 <?php echo file_rows($files, false); ?> | |
191 </tbody> | |
192 </table> | |
4 | 193 |
194 <footer> | |
195 <hr> | |
196 <em> | |
22 | 197 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php <?php echo DL_VERSION ?></a>. |
4 | 198 Powered by PHP <?php echo phpversion(); ?>. |
199 </em> | |
21 | 200 </footer> |
14 | 201 |
202 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
17
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
203 |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
204 <script> |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
205 $("table > tbody > tr > td > a").each(function() { |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
206 if (!this.href.match(/\.(jpe?g|png|gif|webp)$/i)) return |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
207 var title = this.outerHTML |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
208 this.setAttribute("data-title", title) |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
209 this.setAttribute("data-lightbox", "aa") |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
210 }) |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
211 </script> |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
212 |
14 | 213 <script src="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js"></script> |
3 | 214 </body> |