Mercurial > dirlist-php
annotate index.php @ 51:29543401ef18
Ignore nonexisting files.
| author | nanaya <me@myconan.net> | 
|---|---|
| date | Mon, 24 Aug 2015 21:20:27 +0900 | 
| parents | 5a4c9efd99fc | 
| children | f48a4fbb9cf9 | 
| rev | line source | 
|---|---|
| 0 | 1 <?php | 
| 47 
546c6b40d250
Changed enough stuff to bump minor version.
 nanaya <me@myconan.net> parents: 
46diff
changeset | 2 define('DL_VERSION', '2.2.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: 
22diff
changeset | 6 // $uri: web-facing path | 
| 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 7 $uri = $_SERVER["REQUEST_URI"]; | 
| 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
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: 
22diff
changeset | 10 $uri = substr($uri, 0, $query_string_start); | 
| 4 | 11 } | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 12 $uri = urldecode($uri); | 
| 4 | 13 | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 14 // $dir: filesystem path | 
| 26 
eee7ca924a5e
Make it work with apache (at least XAMPP).
 edogawaconan <me@myconan.net> parents: 
25diff
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: 
28diff
changeset | 16 elseif (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"])) { | 
| 
c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
 edogawaconan <me@myconan.net> parents: 
28diff
changeset | 17 $dir = $_SERVER["CONTEXT_DOCUMENT_ROOT"]; | 
| 
c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
 edogawaconan <me@myconan.net> parents: 
28diff
changeset | 18 $dir .= substr($uri, strlen($_SERVER["CONTEXT_PREFIX"])); | 
| 
c482f0db5c28
Turns out "CONTEXT_DOCUMENT_ROOT" isn't enough.
 edogawaconan <me@myconan.net> parents: 
28diff
changeset | 19 } | 
| 26 
eee7ca924a5e
Make it work with apache (at least XAMPP).
 edogawaconan <me@myconan.net> parents: 
25diff
changeset | 20 else { $dir = $_SERVER["DOCUMENT_ROOT"] . $uri; } | 
| 4 | 21 | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
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: 
22diff
changeset | 24 } elseif (substr($uri, -1) !== "/") { | 
| 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 25 header("Location: " . $uri . "/"); | 
| 4 | 26 } | 
| 27 | |
| 28 if (http_response_code() !== 200) { exit; } | |
| 29 | |
| 34 | 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: 
27diff
changeset | 34 if ($file === "." || $file === "..") { continue; } | 
| 
ce92f4d41714
Add support for displaying hidden files.
 edogawaconan <me@myconan.net> parents: 
27diff
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: 
22diff
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: 
16diff
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 | |
| 35 | 52 if ($size === 1) { | 
| 3 | 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 } | 
| 43 
54c13838f8bb
Add spacing to before/after hidden data part.
 nanaya <me@myconan.net> parents: 
42diff
changeset | 64 | 
| 45 
7de8f524d1e4
Always group by directory no matter what type of sorting.
 nanaya <me@myconan.net> parents: 
44diff
changeset | 65 function hidden_data($data = "", $is_dir = false) { | 
| 
7de8f524d1e4
Always group by directory no matter what type of sorting.
 nanaya <me@myconan.net> parents: 
44diff
changeset | 66 return "<i> " . ($is_dir === true ? 0 : 1) . " " . $data . " </i>"; | 
| 43 
54c13838f8bb
Add spacing to before/after hidden data part.
 nanaya <me@myconan.net> parents: 
42diff
changeset | 67 } | 
| 4 | 68 // END UTILITY | 
| 69 | |
| 50 | 70 function tree_link($uri) { | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 71 $uri_array = explode("/", trim($uri, "/")); | 
| 4 | 72 | 
| 73 $tree_path = "/"; | |
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 74 $tree_link = link_to($tree_path, "[root]"); | 
| 20 
caf498a0c602
Hide [root] for aliased directory.
 edogawaconan <me@myconan.net> parents: 
19diff
changeset | 75 $tree_link .= "/"; | 
| 4 | 76 | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 77 foreach ($uri_array as $p) { | 
| 4 | 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 | 
| 50 | 86 function up_link($uri) { | 
| 42 | 87 if ($uri !== "/") { | 
| 88 return "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>"; | |
| 89 } | |
| 90 } | |
| 91 | |
| 50 | 92 function file_rows($dir, $files, $is_dir) { | 
| 2 | 93 $file_rows = ""; | 
| 4 | 94 $file_suffix = ""; | 
| 45 
7de8f524d1e4
Always group by directory no matter what type of sorting.
 nanaya <me@myconan.net> parents: 
44diff
changeset | 95 $list_file_prefix = hidden_data("", $is_dir); | 
| 4 | 96 | 
| 2 | 97 if ($is_dir) { | 
| 98 $file_suffix = "/"; | |
| 4 | 99 } | 
| 3 | 100 | 
| 2 | 101 foreach($files as $file) { | 
| 51 | 102 $file_path = $dir."/".$file; | 
| 103 | |
| 104 if (!file_exists($file_path)) { continue; } | |
| 105 $file_stat = stat($file_path); | |
| 3 | 106 | 
| 107 $file_rows .= "<tr>"; | |
| 42 | 108 $file_rows .= "<td>". $list_file_prefix . link_to($file . $file_suffix, $file . $file_suffix)."</td>"; | 
| 3 | 109 | 
| 110 $file_rows .= "<td>"; | |
| 45 
7de8f524d1e4
Always group by directory no matter what type of sorting.
 nanaya <me@myconan.net> parents: 
44diff
changeset | 111 $file_rows .= hidden_data($file_stat["size"], $is_dir); | 
| 
7de8f524d1e4
Always group by directory no matter what type of sorting.
 nanaya <me@myconan.net> parents: 
44diff
changeset | 112 if ($is_dir) { $file_rows .= "[dir]"; } | 
| 
7de8f524d1e4
Always group by directory no matter what type of sorting.
 nanaya <me@myconan.net> parents: 
44diff
changeset | 113 else { $file_rows .= human_size($file_stat['size']); } | 
| 3 | 114 $file_rows .= "</td>"; | 
| 115 | |
| 45 
7de8f524d1e4
Always group by directory no matter what type of sorting.
 nanaya <me@myconan.net> parents: 
44diff
changeset | 116 $file_rows .= "<td>" . hidden_data("", $is_dir) . h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime'])) . "</td>"; | 
| 3 | 117 | 
| 2 | 118 $file_rows .= "</tr>"; | 
| 119 } | |
| 120 return $file_rows; | |
| 121 } | |
| 0 | 122 ?> | 
| 3 | 123 <?php header('Content-Type: text/html; charset=utf-8'); ?> | 
| 124 <!doctype html> | |
| 125 <head> | |
| 37 | 126 <meta charset="utf-8"> | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 127 <title>Index of <?php echo h($uri); ?></title> | 
| 39 | 128 <meta name="viewport" content="width=device-width, initial-scale=1.0"> | 
| 32 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 129 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/0.9.9/magnific-popup.css"> | 
| 3 | 130 <style type="text/css"> | 
| 32 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 131 .mfp-content figure { margin: 0; } | 
| 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 | |
| 42 | 155 i { | 
| 156 display: none; | |
| 157 } | |
| 158 | |
| 46 | 159 input { | 
| 160 width: 100%; | |
| 161 margin: 10px 0px; | |
| 162 } | |
| 163 | |
| 42 | 164 th span { | 
| 165 display: block; | |
| 166 text-decoration: underline; | |
| 167 } | |
| 168 th span.desc:after { | |
| 169 content: " â–¼"; | |
| 170 } | |
| 171 th span.asc:after { | |
| 172 content: " â–²"; | |
| 173 } | |
| 174 | |
| 3 | 175 @media (min-width: 768px) { | 
| 42 | 176 th { | 
| 177 background: #ccc; | |
| 178 cursor: pointer; | |
| 179 } | |
| 3 | 180 tr:nth-child(even) { background: #eee; } | 
| 181 tr:hover { background: #ddd; } | |
| 182 } | |
| 183 | |
| 184 @media (max-width: 767px) { | |
| 185 table { | |
| 186 border-spacing: 0 10px; | |
| 187 } | |
| 188 th { display: none; } | |
| 189 tr { | |
| 190 background: #eee; | |
| 191 } | |
| 192 td { | |
| 193 display: inline-block; | |
| 194 } | |
| 195 td:first-child { | |
| 196 background: #ddd; | |
| 197 padding: 5px; | |
| 198 } | |
| 199 table a { | |
| 200 font-size: 18px; | |
| 201 display: block; | |
| 202 } | |
| 48 | 203 input { font-size: 18px; } | 
| 3 | 204 } | 
| 205 </style> | |
| 206 </head> | |
| 46 | 207 <body id="files"> | 
| 50 | 208 <h1>Index of <?php echo tree_link($uri); ?></h1> | 
| 3 | 209 | 
| 46 | 210 <input placeholder="search (non-recursive)" class="search" /> | 
| 211 | |
| 212 <table> | |
| 42 | 213 <thead> | 
| 214 <tr> | |
| 215 <th><span class="sort" data-sort="filename">File</span></th> | |
| 216 <th><span class="sort" data-sort="size">Size</span></th> | |
| 217 <th><span class="sort" data-sort="date">Date</span></th> | |
| 218 </tr> | |
| 50 | 219 <?php echo up_link($uri); ?> | 
| 42 | 220 </thead> | 
| 221 <tbody class="list"> | |
| 50 | 222 <?php echo file_rows($dir, $dirs, true); ?> | 
| 223 <?php echo file_rows($dir, $files, false); ?> | |
| 3 | 224 </tbody> | 
| 225 </table> | |
| 4 | 226 | 
| 227 <footer> | |
| 228 <hr> | |
| 229 <em> | |
| 22 | 230 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php <?php echo DL_VERSION ?></a>. | 
| 4 | 231 Powered by PHP <?php echo phpversion(); ?>. | 
| 232 </em> | |
| 21 | 233 </footer> | 
| 14 | 234 | 
| 32 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 235 <script src="//cdnjs.cloudflare.com/ajax/libs/zepto/1.1.4/zepto.min.js"></script> | 
| 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 236 <script src="//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/0.9.9/jquery.magnific-popup.min.js"></script> | 
| 42 | 237 <script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.1.1/list.min.js"></script> | 
| 17 
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
 edogawaconan <me@myconan.net> parents: 
16diff
changeset | 238 | 
| 
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
 edogawaconan <me@myconan.net> parents: 
16diff
changeset | 239 <script> | 
| 41 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 240 $("table td a").each(function() { | 
| 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 241 if (!this.href.match(/\.(jpe?g|png|gif|webp)$/i)) return | 
| 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 242 this.className = "image" | 
| 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 243 this.setAttribute("title", this.innerHTML) | 
| 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 244 }) | 
| 32 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 245 | 
| 41 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 246 $("table > tbody").magnificPopup({ | 
| 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 247 delegate: "a.image", | 
| 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 248 type: "image", | 
| 
e122e4e55e75
No need to wait for document ready since it's already at the bottom of the page.
 nanaya <me@myconan.net> parents: 
40diff
changeset | 249 gallery: { enabled: true } | 
| 17 
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
 edogawaconan <me@myconan.net> parents: 
16diff
changeset | 250 }) | 
| 42 | 251 | 
| 252 $("tbody td:nth-child(3n + 1)").addClass("filename") | |
| 253 $("tbody td:nth-child(3n + 2)").addClass("size") | |
| 254 $("tbody td:nth-child(3n + 3)").addClass("date") | |
| 255 | |
| 256 ;(function() { | |
| 257 var | |
| 258 options = { | |
| 259 valueNames: ["filename", "size", "date"], | |
| 260 page: <?php echo count($dirs) + count($files); ?> | |
| 261 }, | |
| 262 list = new List("files", options) | |
| 263 })() | |
| 17 
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
 edogawaconan <me@myconan.net> parents: 
16diff
changeset | 264 </script> | 
| 3 | 265 </body> | 
