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