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