Mercurial > dirlist-php
annotate index.php @ 42:ddb5458658d2
Sortable list \o/
| author | nanaya <me@myconan.net> | 
|---|---|
| date | Sun, 24 May 2015 18:03:52 +0900 | 
| parents | e122e4e55e75 | 
| children | 54c13838f8bb | 
| rev | line source | 
|---|---|
| 0 | 1 <?php | 
| 42 | 2 define('DL_VERSION', '2.1.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 } | 
| 4 | 64 // END UTILITY | 
| 65 | |
| 66 function tree_link() { | |
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 67 global $uri; | 
| 4 | 68 | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
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: 
22diff
changeset | 72 $tree_link = link_to($tree_path, "[root]"); | 
| 20 
caf498a0c602
Hide [root] for aliased directory.
 edogawaconan <me@myconan.net> parents: 
19diff
changeset | 73 $tree_link .= "/"; | 
| 4 | 74 | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
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 | 
| 42 | 84 function up_link() { | 
| 85 if ($uri !== "/") { | |
| 86 return "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>"; | |
| 87 } | |
| 88 } | |
| 89 | |
| 2 | 90 function file_rows($files, $is_dir) { | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 91 global $dir, $uri; | 
| 3 | 92 | 
| 2 | 93 $file_rows = ""; | 
| 4 | 94 $file_suffix = ""; | 
| 42 | 95 $list_file_prefix = ""; | 
| 4 | 96 | 
| 2 | 97 if ($is_dir) { | 
| 98 $file_suffix = "/"; | |
| 42 | 99 $list_file_prefix = "<i>0</i>"; | 
| 100 } else { | |
| 101 $list_file_prefix = "<i>1</i>"; | |
| 4 | 102 } | 
| 3 | 103 | 
| 2 | 104 foreach($files as $file) { | 
| 36 | 105 $file_stat = @stat($dir . "/". $file); | 
| 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>"; | |
| 42 | 111 if ($is_dir) { $file_rows .= "<i>0</i>[dir]"; } | 
| 112 else { $file_rows .= "<i>" . $file_stat["size"] . "</i>" . human_size($file_stat['size']); } | |
| 3 | 113 $file_rows .= "</td>"; | 
| 114 | |
| 115 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; | |
| 116 | |
| 2 | 117 $file_rows .= "</tr>"; | 
| 118 } | |
| 119 return $file_rows; | |
| 120 } | |
| 0 | 121 ?> | 
| 3 | 122 <?php header('Content-Type: text/html; charset=utf-8'); ?> | 
| 123 <!doctype html> | |
| 124 <head> | |
| 37 | 125 <meta charset="utf-8"> | 
| 23 
949398173ecb
Much simpler setup with $request_filename.
 edogawaconan <me@myconan.net> parents: 
22diff
changeset | 126 <title>Index of <?php echo h($uri); ?></title> | 
| 39 | 127 <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 | 128 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/0.9.9/magnific-popup.css"> | 
| 3 | 129 <style type="text/css"> | 
| 32 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 130 .mfp-content figure { margin: 0; } | 
| 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 | |
| 42 | 154 i { | 
| 155 display: none; | |
| 156 } | |
| 157 | |
| 158 th span { | |
| 159 display: block; | |
| 160 text-decoration: underline; | |
| 161 } | |
| 162 th span.desc:after { | |
| 163 content: " ▼"; | |
| 164 } | |
| 165 th span.asc:after { | |
| 166 content: " ▲"; | |
| 167 } | |
| 168 | |
| 3 | 169 @media (min-width: 768px) { | 
| 42 | 170 th { | 
| 171 background: #ccc; | |
| 172 cursor: pointer; | |
| 173 } | |
| 3 | 174 tr:nth-child(even) { background: #eee; } | 
| 175 tr:hover { background: #ddd; } | |
| 176 } | |
| 177 | |
| 178 @media (max-width: 767px) { | |
| 179 table { | |
| 180 border-spacing: 0 10px; | |
| 181 } | |
| 182 th { display: none; } | |
| 183 tr { | |
| 184 background: #eee; | |
| 185 } | |
| 186 td { | |
| 187 display: inline-block; | |
| 188 } | |
| 189 td:first-child { | |
| 190 background: #ddd; | |
| 191 padding: 5px; | |
| 192 } | |
| 193 table a { | |
| 194 font-size: 18px; | |
| 195 display: block; | |
| 196 } | |
| 197 } | |
| 198 </style> | |
| 199 </head> | |
| 200 <body> | |
| 4 | 201 <h1>Index of <?php echo tree_link(); ?></h1> | 
| 3 | 202 | 
| 42 | 203 <table id="files"> | 
| 204 <thead> | |
| 205 <tr> | |
| 206 <th><span class="sort" data-sort="filename">File</span></th> | |
| 207 <th><span class="sort" data-sort="size">Size</span></th> | |
| 208 <th><span class="sort" data-sort="date">Date</span></th> | |
| 209 </tr> | |
| 210 <?php echo up_link(); ?> | |
| 211 </thead> | |
| 212 <tbody class="list"> | |
| 3 | 213 <?php echo file_rows($dirs, true); ?> | 
| 214 <?php echo file_rows($files, false); ?> | |
| 215 </tbody> | |
| 216 </table> | |
| 4 | 217 | 
| 218 <footer> | |
| 219 <hr> | |
| 220 <em> | |
| 22 | 221 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php <?php echo DL_VERSION ?></a>. | 
| 4 | 222 Powered by PHP <?php echo phpversion(); ?>. | 
| 223 </em> | |
| 21 | 224 </footer> | 
| 14 | 225 | 
| 32 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 226 <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 | 227 <script src="//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/0.9.9/jquery.magnific-popup.min.js"></script> | 
| 42 | 228 <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 | 229 | 
| 
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
 edogawaconan <me@myconan.net> parents: 
16diff
changeset | 230 <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 | 231 $("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 | 232 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 | 233 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 | 234 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 | 235 }) | 
| 32 
986aec12eb7f
Use magnific popup instead of lightbox.
 edogawaconan <me@myconan.net> parents: 
30diff
changeset | 236 | 
| 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 | 237 $("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 | 238 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 | 239 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 | 240 gallery: { enabled: true } | 
| 17 
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
 edogawaconan <me@myconan.net> parents: 
16diff
changeset | 241 }) | 
| 42 | 242 | 
| 243 $("tbody td:nth-child(3n + 1)").addClass("filename") | |
| 244 $("tbody td:nth-child(3n + 2)").addClass("size") | |
| 245 $("tbody td:nth-child(3n + 3)").addClass("date") | |
| 246 | |
| 247 ;(function() { | |
| 248 var | |
| 249 options = { | |
| 250 valueNames: ["filename", "size", "date"], | |
| 251 page: <?php echo count($dirs) + count($files); ?> | |
| 252 }, | |
| 253 list = new List("files", options) | |
| 254 })() | |
| 17 
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
 edogawaconan <me@myconan.net> parents: 
16diff
changeset | 255 </script> | 
| 3 | 256 </body> | 
