Mercurial > dirlist-php
annotate index.php @ 10:47a281c1d54e
Update documentation to match current code.
| author | edogawaconan <me@myconan.net> |
|---|---|
| date | Tue, 21 Oct 2014 22:07:03 +0900 |
| parents | 5db51091e291 |
| children | 9abfd376740d |
| rev | line source |
|---|---|
| 0 | 1 <?php |
| 4 | 2 // Required for strftime(). Set to UTC because :internet:. |
| 2 | 3 date_default_timezone_set('UTC'); |
| 4 | 4 |
| 5 // $path: actual requested path, with $alias_prefix removed, relative to $root. | |
| 6 $path = $_SERVER["REQUEST_URI"]; | |
| 7 $query_string_start = strpos($path, "?"); | |
| 8 if ($query_string_start !== false) { | |
| 9 $path = substr($path, 0, $query_string_start); | |
| 10 } | |
| 11 $path = urldecode($path); | |
| 12 | |
| 13 $prefix = $_SERVER["DL_PREFIX"]; | |
|
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
14 if ($prefix === null) { $prefix = ""; } |
| 4 | 15 |
|
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
16 $path = substr($path, strlen($prefix)); |
| 4 | 17 if ($path === false) { $path = "/"; } |
| 18 | |
| 19 // root of directory listing. | |
| 20 $root = $_SERVER["DL_ROOT"]; | |
|
5
b84ce3ef4c9d
Fix behaviour difference between version.
edogawaconan <me@myconan.net>
parents:
4
diff
changeset
|
21 if ($root === null || $root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; } |
| 4 | 22 |
| 23 if (realpath($root . $path) === false) { | |
| 24 header("HTTP/1.0 404 Not Found"); | |
|
7
c4046c7e1f5a
Missing one part when removing current_dir.
edogawaconan <me@myconan.net>
parents:
6
diff
changeset
|
25 } elseif (substr($path, -1) !== "/") { |
|
9
5db51091e291
Directory may turn file. Return 302 instead of 301.
edogawaconan <me@myconan.net>
parents:
8
diff
changeset
|
26 header("Location: " . $path . "/"); |
| 4 | 27 } |
| 28 | |
| 29 if (http_response_code() !== 200) { exit; } | |
| 30 | |
| 31 $dir_handle = @opendir($root . $path); | |
| 2 | 32 $files = array(); |
| 33 $dirs = array(); | |
| 4 | 34 while (($file = readdir($dir_handle)) !== false) { |
| 35 if ($file === "." or $file === "..") { continue; } | |
| 36 elseif (is_dir($root . $path . $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)); } |
| 4 | 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 | |
| 52 if ($size <= 1) { | |
| 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() { | |
| 67 global $path, $prefix; | |
| 68 | |
| 69 $path_array = explode("/", trim($path, "/")); | |
| 70 array_unshift($path_array, trim($prefix, "/")); | |
| 71 | |
| 72 $tree_path = "/"; | |
| 73 $tree_link = link_to($tree_path, "[root]") . "/"; | |
| 74 | |
| 75 foreach ($path_array as $p) { | |
| 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 |
| 2 | 84 function file_rows($files, $is_dir) { |
| 4 | 85 global $path, $root, $prefix; |
| 3 | 86 |
| 2 | 87 $file_rows = ""; |
| 4 | 88 $file_suffix = ""; |
| 89 | |
| 2 | 90 if ($is_dir) { |
| 91 $file_suffix = "/"; | |
| 4 | 92 |
| 93 if ($path !== "/") { | |
| 94 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "..") . "</td></tr>"; | |
| 2 | 95 } |
| 4 | 96 } |
| 3 | 97 |
| 2 | 98 foreach($files as $file) { |
| 4 | 99 $file_stat = stat($root . $path . "/". $file); |
| 3 | 100 |
| 101 $file_rows .= "<tr>"; | |
| 4 | 102 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; |
| 3 | 103 |
| 104 $file_rows .= "<td>"; | |
| 2 | 105 if ($is_dir) { $file_rows .= "[dir]"; } |
| 4 | 106 else { $file_rows .= human_size($file_stat['size']); } |
| 3 | 107 $file_rows .= "</td>"; |
| 108 | |
| 109 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; | |
| 110 | |
| 2 | 111 $file_rows .= "</tr>"; |
| 112 } | |
| 113 return $file_rows; | |
| 114 } | |
| 0 | 115 ?> |
| 3 | 116 <?php header('Content-Type: text/html; charset=utf-8'); ?> |
| 117 <!doctype html> | |
| 118 <head> | |
| 119 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | |
| 4 | 120 <title>Index of <?php echo h($prefix . $path); ?></title> |
| 3 | 121 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
| 122 <style type="text/css"> | |
| 123 * { box-sizing: border-box; } | |
| 124 body { | |
| 125 font-family: Segoe UI, sans-serif; | |
| 126 font-size: 14px; | |
| 127 } | |
| 128 h1 { margin: 5px; } | |
| 129 table { | |
| 130 width: 100%; | |
| 131 } | |
| 132 th:first-child, td:first-child { | |
| 133 width: 100%; | |
| 134 white-space: pre-wrap; | |
| 135 word-wrap: break-word; | |
| 136 word-break: break-all; | |
| 137 } | |
| 138 tr { | |
| 139 position: relative; | |
| 140 } | |
| 141 th, td { | |
| 142 white-space: nowrap; | |
| 143 padding: 2px 5px; | |
| 144 } | |
| 145 | |
| 146 @media (min-width: 768px) { | |
| 147 th { background: #ccc; } | |
| 148 tr:nth-child(even) { background: #eee; } | |
| 149 tr:hover { background: #ddd; } | |
| 150 } | |
| 151 | |
| 152 @media (max-width: 767px) { | |
| 153 table { | |
| 154 border-spacing: 0 10px; | |
| 155 } | |
| 156 th { display: none; } | |
| 157 tr { | |
| 158 background: #eee; | |
| 159 } | |
| 160 td { | |
| 161 display: inline-block; | |
| 162 } | |
| 163 td:first-child { | |
| 164 background: #ddd; | |
| 165 padding: 5px; | |
| 166 } | |
| 167 table a { | |
| 168 font-size: 18px; | |
| 169 display: block; | |
| 170 } | |
| 171 } | |
| 172 </style> | |
| 173 </head> | |
| 174 <body> | |
| 4 | 175 <h1>Index of <?php echo tree_link(); ?></h1> |
| 3 | 176 |
| 177 <table> | |
| 178 <thead><tr> | |
| 179 <th>File</th> | |
| 180 <th>Size</th> | |
| 181 <th>Date</th> | |
| 182 </tr></thead> | |
| 183 <tbody> | |
| 184 <?php echo file_rows($dirs, true); ?> | |
| 185 <?php echo file_rows($files, false); ?> | |
| 186 </tbody> | |
| 187 </table> | |
| 4 | 188 |
| 189 <footer> | |
| 190 <hr> | |
| 191 <em> | |
| 192 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php</a>. | |
| 193 Powered by PHP <?php echo phpversion(); ?>. | |
| 194 </em> | |
| 3 | 195 </body> |
