Mercurial > dirlist-php
annotate index.php @ 21:fb351f473fd5
Forgot to close footer.
author | edogawaconan <me@myconan.net> |
---|---|
date | Wed, 22 Oct 2014 12:02:38 +0900 |
parents | caf498a0c602 |
children | 431682ff9169 |
rev | line source |
---|---|
0 | 1 <?php |
4 | 2 // Required for strftime(). Set to UTC because :internet:. |
2 | 3 date_default_timezone_set('UTC'); |
4 | 4 |
5 // $path: actual requested path, with $alias_prefix removed, relative to $root. | |
6 $path = $_SERVER["REQUEST_URI"]; | |
7 $query_string_start = strpos($path, "?"); | |
8 if ($query_string_start !== false) { | |
9 $path = substr($path, 0, $query_string_start); | |
10 } | |
11 $path = urldecode($path); | |
12 | |
13 $prefix = $_SERVER["DL_PREFIX"]; | |
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
14 if ($prefix === null) { $prefix = ""; } |
4 | 15 |
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
16 $path = substr($path, strlen($prefix)); |
4 | 17 if ($path === false) { $path = "/"; } |
18 | |
19 // root of directory listing. | |
20 $root = $_SERVER["DL_ROOT"]; | |
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
21 if ($root === null || $root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; } |
4 | 22 |
23 if (realpath($root . $path) === false) { | |
24 header("HTTP/1.0 404 Not Found"); | |
7
c4046c7e1f5a
Missing one part when removing current_dir.
edogawaconan <me@myconan.net>
parents:
6
diff
changeset
|
25 } elseif (substr($path, -1) !== "/") { |
9
5db51091e291
Directory may turn file. Return 302 instead of 301.
edogawaconan <me@myconan.net>
parents:
8
diff
changeset
|
26 header("Location: " . $path . "/"); |
4 | 27 } |
28 | |
29 if (http_response_code() !== 200) { exit; } | |
30 | |
31 $dir_handle = @opendir($root . $path); | |
2 | 32 $files = array(); |
33 $dirs = array(); | |
4 | 34 while (($file = readdir($dir_handle)) !== false) { |
11 | 35 if ($file === "." || $file === "..") { continue; } |
4 | 36 elseif (is_dir($root . $path . $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() { | |
67 global $path, $prefix; | |
68 | |
69 $path_array = explode("/", trim($path, "/")); | |
70 array_unshift($path_array, trim($prefix, "/")); | |
71 | |
72 $tree_path = "/"; | |
20
caf498a0c602
Hide [root] for aliased directory.
edogawaconan <me@myconan.net>
parents:
19
diff
changeset
|
73 if ($prefix === "") { $tree_link = link_to($tree_path, "[root]"); } |
caf498a0c602
Hide [root] for aliased directory.
edogawaconan <me@myconan.net>
parents:
19
diff
changeset
|
74 $tree_link .= "/"; |
4 | 75 |
76 foreach ($path_array as $p) { | |
77 if ($p === "") { continue; } | |
78 $tree_path .= $p . "/"; | |
79 $tree_link .= link_to($tree_path, $p) . "/"; | |
80 } | |
81 | |
82 return $tree_link; | |
83 } | |
3 | 84 |
2 | 85 function file_rows($files, $is_dir) { |
4 | 86 global $path, $root, $prefix; |
3 | 87 |
2 | 88 $file_rows = ""; |
4 | 89 $file_suffix = ""; |
90 | |
2 | 91 if ($is_dir) { |
92 $file_suffix = "/"; | |
4 | 93 |
94 if ($path !== "/") { | |
19
c5b8d2592585
Easier to click target for going up directory.
edogawaconan <me@myconan.net>
parents:
18
diff
changeset
|
95 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "[up]") . "</td></tr>"; |
2 | 96 } |
4 | 97 } |
3 | 98 |
2 | 99 foreach($files as $file) { |
4 | 100 $file_stat = stat($root . $path . "/". $file); |
3 | 101 |
102 $file_rows .= "<tr>"; | |
4 | 103 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; |
3 | 104 |
105 $file_rows .= "<td>"; | |
2 | 106 if ($is_dir) { $file_rows .= "[dir]"; } |
4 | 107 else { $file_rows .= human_size($file_stat['size']); } |
3 | 108 $file_rows .= "</td>"; |
109 | |
110 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; | |
111 | |
2 | 112 $file_rows .= "</tr>"; |
113 } | |
114 return $file_rows; | |
115 } | |
0 | 116 ?> |
3 | 117 <?php header('Content-Type: text/html; charset=utf-8'); ?> |
118 <!doctype html> | |
119 <head> | |
120 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | |
4 | 121 <title>Index of <?php echo h($prefix . $path); ?></title> |
3 | 122 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
14 | 123 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css"> |
3 | 124 <style type="text/css"> |
16
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
125 .lb-data a { color: #ccc; } |
18
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
126 .lightbox { |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
127 position: fixed; |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
128 top: 50px !important; |
9a4ac2d53a36
Set lightbox position to fixed.
edogawaconan <me@myconan.net>
parents:
17
diff
changeset
|
129 } |
16
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
130 </style> |
3c0b3a38a7fc
Stupid lightbox2 can't copy image url directly ._.
edogawaconan <me@myconan.net>
parents:
15
diff
changeset
|
131 <style type="text/css"> |
3 | 132 * { box-sizing: border-box; } |
133 body { | |
134 font-family: Segoe UI, sans-serif; | |
135 font-size: 14px; | |
136 } | |
137 h1 { margin: 5px; } | |
138 table { | |
139 width: 100%; | |
140 } | |
141 th:first-child, td:first-child { | |
142 width: 100%; | |
143 white-space: pre-wrap; | |
144 word-wrap: break-word; | |
145 word-break: break-all; | |
146 } | |
147 tr { | |
148 position: relative; | |
149 } | |
150 th, td { | |
151 white-space: nowrap; | |
152 padding: 2px 5px; | |
153 } | |
154 | |
155 @media (min-width: 768px) { | |
156 th { background: #ccc; } | |
157 tr:nth-child(even) { background: #eee; } | |
158 tr:hover { background: #ddd; } | |
159 } | |
160 | |
161 @media (max-width: 767px) { | |
162 table { | |
163 border-spacing: 0 10px; | |
164 } | |
165 th { display: none; } | |
166 tr { | |
167 background: #eee; | |
168 } | |
169 td { | |
170 display: inline-block; | |
171 } | |
172 td:first-child { | |
173 background: #ddd; | |
174 padding: 5px; | |
175 } | |
176 table a { | |
177 font-size: 18px; | |
178 display: block; | |
179 } | |
180 } | |
181 </style> | |
182 </head> | |
183 <body> | |
4 | 184 <h1>Index of <?php echo tree_link(); ?></h1> |
3 | 185 |
186 <table> | |
187 <thead><tr> | |
188 <th>File</th> | |
189 <th>Size</th> | |
190 <th>Date</th> | |
191 </tr></thead> | |
192 <tbody> | |
193 <?php echo file_rows($dirs, true); ?> | |
194 <?php echo file_rows($files, false); ?> | |
195 </tbody> | |
196 </table> | |
4 | 197 |
198 <footer> | |
199 <hr> | |
200 <em> | |
201 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php</a>. | |
202 Powered by PHP <?php echo phpversion(); ?>. | |
203 </em> | |
21 | 204 </footer> |
14 | 205 |
206 <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
|
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 $("table > tbody > tr > td > a").each(function() { |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
210 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
|
211 var title = this.outerHTML |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
212 this.setAttribute("data-title", title) |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
213 this.setAttribute("data-lightbox", "aa") |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
214 }) |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
215 </script> |
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
216 |
14 | 217 <script src="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js"></script> |
3 | 218 </body> |