Mercurial > dirlist-php
annotate index.php @ 30:c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
author | edogawaconan <me@myconan.net> |
---|---|
date | Thu, 30 Oct 2014 13:03:28 +0900 |
parents | ce92f4d41714 |
children | 986aec12eb7f |
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"]; } |
30
c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
edogawaconan <me@myconan.net>
parents:
28
diff
changeset
|
16 elseif (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"])) { |
c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
edogawaconan <me@myconan.net>
parents:
28
diff
changeset
|
17 $dir = $_SERVER["CONTEXT_DOCUMENT_ROOT"]; |
c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
edogawaconan <me@myconan.net>
parents:
28
diff
changeset
|
18 $dir .= substr($uri, strlen($_SERVER["CONTEXT_PREFIX"])); |
c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
edogawaconan <me@myconan.net>
parents:
28
diff
changeset
|
19 } |
26
eee7ca924a5e
Make it work with apache (at least XAMPP).
edogawaconan <me@myconan.net>
parents:
25
diff
changeset
|
20 else { $dir = $_SERVER["DOCUMENT_ROOT"] . $uri; } |
4 | 21 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
22 if (realpath($dir) === false) { |
4 | 23 header("HTTP/1.0 404 Not Found"); |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
24 } elseif (substr($uri, -1) !== "/") { |
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
25 header("Location: " . $uri . "/"); |
4 | 26 } |
27 | |
28 if (http_response_code() !== 200) { exit; } | |
29 | |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
30 $dir_handle = @opendir($dir); |
2 | 31 $files = array(); |
32 $dirs = array(); | |
4 | 33 while (($file = readdir($dir_handle)) !== false) { |
28
ce92f4d41714
Add support for displaying hidden files.
edogawaconan <me@myconan.net>
parents:
27
diff
changeset
|
34 if ($file === "." || $file === "..") { continue; } |
ce92f4d41714
Add support for displaying hidden files.
edogawaconan <me@myconan.net>
parents:
27
diff
changeset
|
35 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
|
36 elseif (is_dir($dir . $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)); } |
17
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
45 function link_to($target, $title) { return('<a href="' . a($target) . '">' . h($title) . "</a>"); } |
3 | 46 |
4 | 47 function human_size($size) { |
48 $thousand_units = array("ko", "Mo", "Go", "To", "Po"); | |
3 | 49 |
50 $return_format = "%d %s"; | |
51 | |
52 if ($size <= 1) { | |
53 $return_unit = "octet"; | |
4 | 54 } elseif ($size < 1000) { |
3 | 55 $return_unit = "octets"; |
56 } else { | |
57 $size /= 1000; | |
58 for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; } | |
59 $return_format = "%.2f %s"; | |
60 $return_unit = $thousand_units[$i]; | |
61 } | |
62 return sprintf($return_format, $size, $return_unit); | |
2 | 63 } |
4 | 64 // END UTILITY |
65 | |
66 function tree_link() { | |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
67 global $uri; |
4 | 68 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
69 $uri_array = explode("/", trim($uri, "/")); |
4 | 70 |
71 $tree_path = "/"; | |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
72 $tree_link = link_to($tree_path, "[root]"); |
20
caf498a0c602
Hide [root] for aliased directory.
edogawaconan <me@myconan.net>
parents:
19
diff
changeset
|
73 $tree_link .= "/"; |
4 | 74 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
75 foreach ($uri_array as $p) { |
4 | 76 if ($p === "") { continue; } |
77 $tree_path .= $p . "/"; | |
78 $tree_link .= link_to($tree_path, $p) . "/"; | |
79 } | |
80 | |
81 return $tree_link; | |
82 } | |
3 | 83 |
2 | 84 function file_rows($files, $is_dir) { |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
85 global $dir, $uri; |
3 | 86 |
2 | 87 $file_rows = ""; |
4 | 88 $file_suffix = ""; |
89 | |
2 | 90 if ($is_dir) { |
91 $file_suffix = "/"; | |
4 | 92 |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
93 if ($uri !== "/") { |
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
94 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>"; |
2 | 95 } |
4 | 96 } |
3 | 97 |
2 | 98 foreach($files as $file) { |
23
949398173ecb
Much simpler setup with $request_filename.
edogawaconan <me@myconan.net>
parents:
22
diff
changeset
|
99 $file_stat = stat($dir . "/". $file); |
3 | 100 |
101 $file_rows .= "<tr>"; | |
4 | 102 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; |
3 | 103 |
104 $file_rows .= "<td>"; | |
2 | 105 if ($is_dir) { $file_rows .= "[dir]"; } |
4 | 106 else { $file_rows .= human_size($file_stat['size']); } |
3 | 107 $file_rows .= "</td>"; |
108 | |
109 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; | |
110 | |
2 | 111 $file_rows .= "</tr>"; |
112 } | |
113 return $file_rows; | |
114 } | |
0 | 115 ?> |
3 | 116 <?php header('Content-Type: text/html; charset=utf-8'); ?> |
117 <!doctype html> | |
118 <head> | |
119 <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
|
120 <title>Index of <?php echo h($uri); ?></title> |
3 | 121 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
14 | 122 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css"> |
3 | 123 <style type="text/css"> |
16
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
124 .lb-data a { color: #ccc; } |
18
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
125 .lightbox { |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
126 position: fixed; |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
127 top: 50px !important; |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
128 } |
16
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
129 </style> |
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
130 <style type="text/css"> |
3 | 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> | |
22 | 200 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php <?php echo DL_VERSION ?></a>. |
4 | 201 Powered by PHP <?php echo phpversion(); ?>. |
202 </em> | |
21 | 203 </footer> |
14 | 204 |
205 <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
|
206 |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
207 <script> |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
208 $("table > tbody > tr > td > a").each(function() { |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
209 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
|
210 var title = this.outerHTML |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
211 this.setAttribute("data-title", title) |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
212 this.setAttribute("data-lightbox", "aa") |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
213 }) |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
214 </script> |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
215 |
14 | 216 <script src="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js"></script> |
3 | 217 </body> |