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