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