Mercurial > dirlist-php
annotate index.php @ 62:4cb6f04f95c1 default tip
Fix handling url with %2F path
| author | nanaya <me@nanaya.net> |
|---|---|
| date | Sat, 08 Feb 2025 01:52:13 +0900 |
| parents | 72580314dd6a |
| children |
| rev | line source |
|---|---|
| 0 | 1 <?php |
| 56 | 2 |
| 61 | 3 define('DL_VERSION', '2.2.5'); |
| 56 | 4 // Required for strftime(). Set to UTC because :internet:. |
| 5 date_default_timezone_set('UTC'); | |
| 4 | 6 |
| 56 | 7 // $uri: web-facing path |
| 8 $uri = $_SERVER["REQUEST_URI"]; | |
| 9 $query_string_start = strpos($uri, "?"); | |
| 10 if ($query_string_start !== false) { | |
| 11 $uri = substr($uri, 0, $query_string_start); | |
| 12 } | |
| 62 | 13 $decodedUri = urldecode($uri); |
| 14 if (stripos($uri, '%2F') !== false) { | |
| 15 header("Location: {$decodedUri}"); | |
| 16 } | |
| 17 $uri = $decodedUri; | |
| 4 | 18 |
| 56 | 19 // $dir: filesystem path |
| 20 if (isset($_SERVER["DL_DIR"])) { | |
| 21 $dir = $_SERVER["DL_DIR"]; | |
| 22 } elseif (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"])) { | |
| 23 $dir = $_SERVER["CONTEXT_DOCUMENT_ROOT"]; | |
| 24 $dir .= substr($uri, strlen($_SERVER["CONTEXT_PREFIX"])); | |
| 25 } else { | |
| 26 $dir = $_SERVER["DOCUMENT_ROOT"] . $uri; | |
| 27 } | |
| 4 | 28 |
| 56 | 29 if (realpath($dir) === false) { |
| 30 header("HTTP/1.0 404 Not Found"); | |
| 31 } elseif (substr($uri, -1) !== "/") { | |
| 32 header("Location: " . $uri . "/"); | |
| 33 } | |
| 4 | 34 |
| 56 | 35 if (http_response_code() !== 200) { |
| 36 exit; | |
| 37 } | |
| 3 | 38 |
| 56 | 39 $dir_handle = opendir($dir); |
| 40 $files = array(); | |
| 41 $dirs = array(); | |
| 42 while (($file = readdir($dir_handle)) !== false) { | |
| 43 if ($file === "." || $file === "..") { | |
| 44 continue; | |
| 45 } elseif (!(isset($_SERVER["DL_SHOWALL"]) && $_SERVER["DL_SHOWALL"] === "1") && substr($file, 0, 1) === ".") { | |
| 46 continue; | |
| 47 } elseif (is_dir($dir . $file)) { | |
| 48 $dirs[] = $file; | |
| 49 } else { | |
| 50 $files[] = $file; | |
| 51 } | |
| 3 | 52 } |
| 56 | 53 sort($files); |
| 54 sort($dirs); | |
|
43
54c13838f8bb
Add spacing to before/after hidden data part.
nanaya <me@myconan.net>
parents:
42
diff
changeset
|
55 |
| 56 | 56 // BEGIN UTILITY |
| 57 function h($string) | |
| 58 { | |
| 59 return htmlspecialchars($string, ENT_QUOTES, "UTF-8"); | |
| 60 } | |
| 61 function a($string) | |
| 62 { | |
| 63 return preg_replace("#(%2F)+#", "/", rawurlencode($string)); | |
| 64 } | |
| 65 function link_to($target, $title) | |
| 66 { | |
| 67 return('<a href="' . a($target) . '">' . h($title) . "</a>"); | |
| 4 | 68 } |
| 69 | |
| 56 | 70 function human_size($size) |
| 71 { | |
| 72 $thousand_units = array("ko", "Mo", "Go", "To", "Po"); | |
| 73 | |
| 74 $return_format = "%d %s"; | |
| 3 | 75 |
| 56 | 76 if ($size === 1) { |
| 77 $return_unit = "octet"; | |
| 78 } elseif ($size < 1000) { | |
| 79 $return_unit = "octets"; | |
| 80 } else { | |
| 81 $size /= 1000; | |
| 82 for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { | |
| 83 $size /= 1000; | |
| 84 } | |
| 85 $return_format = "%.2f %s"; | |
| 86 $return_unit = $thousand_units[$i]; | |
| 87 } | |
| 88 return sprintf($return_format, $size, $return_unit); | |
| 42 | 89 } |
| 90 | |
| 56 | 91 function hidden_data($data = "", $is_dir = false) |
| 92 { | |
| 93 return "<i> " . ($is_dir === true ? 0 : 1) . " " . $data . " </i>"; | |
| 94 } | |
| 95 // END UTILITY | |
| 96 | |
| 97 function tree_link($uri) | |
| 98 { | |
| 99 $uri_array = explode("/", trim($uri, "/")); | |
| 4 | 100 |
| 56 | 101 $tree_path = "/"; |
| 102 $tree_link = link_to($tree_path, "[root]"); | |
| 103 $tree_link .= "/"; | |
| 51 | 104 |
| 56 | 105 foreach ($uri_array as $p) { |
| 106 if ($p === "") { | |
| 107 continue; | |
| 108 } | |
| 109 $tree_path .= $p . "/"; | |
| 110 $tree_link .= link_to($tree_path, $p) . "/"; | |
| 111 } | |
| 3 | 112 |
| 56 | 113 return $tree_link; |
| 114 } | |
| 115 | |
| 116 function up_link($uri) | |
| 117 { | |
| 118 if ($uri !== "/") { | |
| 119 return "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>"; | |
| 120 } | |
| 2 | 121 } |
| 56 | 122 |
| 123 function file_rows($dir, $files, $is_dir) | |
| 124 { | |
| 125 $file_suffix = ""; | |
| 126 if ($is_dir) { | |
| 127 $file_suffix = "/"; | |
| 128 } | |
| 129 | |
| 130 $file_rows = ""; | |
| 131 foreach ($files as $file) { | |
| 132 $file_path = $dir."/".$file; | |
| 133 | |
| 134 if (!file_exists($file_path)) { | |
| 135 continue; | |
| 136 } | |
| 137 $file_stat = stat($file_path); | |
| 138 | |
| 139 $file_rows .= | |
| 140 "<tr>". | |
| 141 "<td>". | |
| 142 hidden_data("", $is_dir). | |
| 143 link_to($file.$file_suffix, $file.$file_suffix). | |
| 144 "</td>". | |
| 145 "<td>". | |
| 146 hidden_data($file_stat["size"], $is_dir). | |
| 147 ($is_dir ? "[dir]" : human_size($file_stat["size"])). | |
| 148 "</td>". | |
| 149 "<td>". | |
| 59 | 150 hidden_data("", $is_dir). |
| 56 | 151 h(strftime("%Y-%m-%d %H:%M %Z", $file_stat["mtime"])). |
| 152 "</td>". | |
| 153 "</tr>"; | |
| 154 } | |
| 155 return $file_rows; | |
| 156 } | |
| 157 | |
| 158 header('Content-Type: text/html; charset=utf-8'); | |
| 0 | 159 ?> |
| 3 | 160 <!doctype html> |
| 161 <head> | |
| 56 | 162 <meta charset="utf-8"> |
| 57 | 163 <title>Index of <?= h($uri); ?></title> |
| 56 | 164 <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 58 | 165 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.css"> |
| 3 | 166 |
| 56 | 167 <style type="text/css"> |
| 168 .mfp-content figure { margin: 0; } | |
| 169 * { box-sizing: border-box; } | |
| 170 body { | |
| 171 font-family: Segoe UI, sans-serif; | |
| 172 font-size: 14px; | |
| 173 } | |
| 174 h1 { margin: 5px; } | |
| 175 table { | |
| 176 width: 100%; | |
| 177 } | |
| 178 th:first-child, td:first-child { | |
| 179 width: 100%; | |
| 180 white-space: pre-wrap; | |
| 181 word-wrap: break-word; | |
| 182 word-break: break-all; | |
| 183 } | |
| 184 tr { | |
| 185 position: relative; | |
| 186 } | |
| 187 th, td { | |
| 188 white-space: nowrap; | |
| 189 padding: 2px 5px; | |
| 190 } | |
| 42 | 191 |
| 56 | 192 i { |
| 193 display: none; | |
| 194 } | |
| 195 | |
| 196 input { | |
| 197 width: 100%; | |
| 198 margin: 10px 0px; | |
| 199 } | |
| 46 | 200 |
| 56 | 201 th span { |
| 202 display: block; | |
| 203 text-decoration: underline; | |
| 204 } | |
| 205 th span.desc::after { | |
| 206 content: " ▼"; | |
| 207 } | |
| 208 th span.asc::after { | |
| 209 content: " ▲"; | |
| 210 } | |
| 42 | 211 |
| 56 | 212 @media (min-width: 768px) { |
| 213 th { | |
| 214 background: #ccc; | |
| 215 cursor: pointer; | |
| 216 } | |
| 217 tr:nth-child(even) { background: #eee; } | |
| 218 tr:hover { background: #ddd; } | |
| 219 } | |
| 3 | 220 |
| 56 | 221 @media (max-width: 767px) { |
| 222 table { | |
| 223 border-spacing: 0 10px; | |
| 224 } | |
| 225 th { display: none; } | |
| 226 tr { | |
| 227 background: #eee; | |
| 228 } | |
| 229 td { | |
| 230 display: inline-block; | |
| 231 } | |
| 232 td:first-child { | |
| 233 background: #ddd; | |
| 234 padding: 5px; | |
| 235 } | |
| 236 table a { | |
| 237 font-size: 18px; | |
| 238 display: block; | |
| 239 } | |
| 240 input { font-size: 18px; } | |
| 241 } | |
| 242 </style> | |
| 3 | 243 </head> |
| 46 | 244 <body id="files"> |
| 57 | 245 <h1>Index of <?= tree_link($uri); ?></h1> |
| 3 | 246 |
| 56 | 247 <input placeholder="search (non-recursive)" class="search" /> |
| 46 | 248 |
| 56 | 249 <table> |
| 250 <thead> | |
| 251 <tr> | |
| 252 <th><span class="sort" data-sort="filename">File</span></th> | |
| 253 <th><span class="sort" data-sort="size">Size</span></th> | |
| 254 <th><span class="sort" data-sort="date">Date</span></th> | |
| 255 </tr> | |
| 57 | 256 <?= up_link($uri); ?> |
| 56 | 257 </thead> |
| 258 <tbody class="list"> | |
| 57 | 259 <?= file_rows($dir, $dirs, true); ?> |
| 260 <?= file_rows($dir, $files, false); ?> | |
| 56 | 261 </tbody> |
| 262 </table> | |
| 4 | 263 |
| 56 | 264 <footer> |
| 265 <hr> | |
| 266 <em> | |
| 61 | 267 Running <a href="https://bitbucket.org/nanayapro/dirlist-php">dirlist-php <?= DL_VERSION ?></a>. |
| 57 | 268 Powered by PHP <?= phpversion(); ?>. |
| 56 | 269 </em> |
| 270 </footer> | |
| 14 | 271 |
| 58 | 272 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> |
| 273 <script src="//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script> | |
| 274 <script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.2.0/list.min.js"></script> | |
|
17
8b222e3ffe25
Set lightbox attributes through javascript instead of php.
edogawaconan <me@myconan.net>
parents:
16
diff
changeset
|
275 |
| 56 | 276 <script> |
| 277 $("table td a").each(function() { | |
| 278 if (!this.href.match(/\.(jpe?g|png|gif|webp)$/i)) return | |
| 279 | |
| 280 this.className = "image" | |
| 281 this.setAttribute("title", this.innerHTML) | |
| 282 }) | |
|
32
986aec12eb7f
Use magnific popup instead of lightbox.
edogawaconan <me@myconan.net>
parents:
30
diff
changeset
|
283 |
| 56 | 284 $("table > tbody").magnificPopup({ |
| 285 delegate: "a.image", | |
| 286 type: "image", | |
| 287 gallery: { enabled: true } | |
| 288 }) | |
| 42 | 289 |
| 56 | 290 $("tbody td:nth-child(3n + 1)").addClass("filename") |
| 291 $("tbody td:nth-child(3n + 2)").addClass("size") | |
| 292 $("tbody td:nth-child(3n + 3)").addClass("date") | |
| 293 | |
| 294 new List("files", { | |
| 295 valueNames: ["filename", "size", "date"], | |
| 57 | 296 page: <?= count($dirs) + count($files); ?> |
| 56 | 297 }) |
| 298 </script> | |
| 3 | 299 </body> |
