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