Mercurial > dirlist-php
comparison index.php @ 4:7812af1c23ae
Actual refactor.
| author | edogawaconan <me@myconan.net> |
|---|---|
| date | Tue, 21 Oct 2014 21:31:38 +0900 |
| parents | 9ea8be5b28d1 |
| children | b84ce3ef4c9d |
comparison
equal
deleted
inserted
replaced
| 3:9ea8be5b28d1 | 4:7812af1c23ae |
|---|---|
| 1 <?php | 1 <?php |
| 2 //real men use UTC for internets | 2 // Required for strftime(). Set to UTC because :internet:. |
| 3 date_default_timezone_set('UTC'); | 3 date_default_timezone_set('UTC'); |
| 4 $wwwroot = $_ENV["DOCUMENT_ROOT"]; | 4 |
| 5 $link_prefix = "/"; | 5 // $path: actual requested path, with $alias_prefix removed, relative to $root. |
| 6 $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/'); | 6 $path = $_SERVER["REQUEST_URI"]; |
| 7 $link_path = substr($link_base,strlen(utf8_decode($link_prefix))); | 7 $query_string_start = strpos($path, "?"); |
| 8 $path = realpath("$wwwroot/".$link_path); | 8 if ($query_string_start !== false) { |
| 9 if (!$path) { die("fail"); } | 9 $path = substr($path, 0, $query_string_start); |
| 10 $dir_handle = @opendir($path); | 10 } |
| 11 $to_dir = explode('/', trim($link_path,'/')); | 11 $path = urldecode($path); |
| 12 array_unshift($to_dir, trim($link_prefix,'/')); | 12 |
| 13 $prefix = $_SERVER["DL_PREFIX"]; | |
| 14 | |
| 15 $path = substr($path, strlen(utf8_decode($prefix))); | |
| 16 if ($path === false) { $path = "/"; } | |
| 17 | |
| 18 // root of directory listing. | |
| 19 $root = $_SERVER["DL_ROOT"]; | |
| 20 if ($root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; } | |
| 21 | |
| 22 // current directory being listed. | |
| 23 $current_dir = $root . $path; | |
| 24 | |
| 25 if (realpath($root . $path) === false) { | |
| 26 header("HTTP/1.0 404 Not Found"); | |
| 27 } elseif (substr($current_dir, -1) !== "/") { | |
| 28 header("Location: " . $path . "/", false, 301); | |
| 29 } | |
| 30 | |
| 31 if (http_response_code() !== 200) { exit; } | |
| 32 | |
| 33 // $link_prefix = "/"; | |
| 34 // $link_base = rtrim(preg_replace("#/+#", "/", urldecode($_ENV["REQUEST_URI"])),'/'); | |
| 35 // $link_path = substr($link_base,strlen(utf8_decode($link_prefix))); | |
| 36 // $path = realpath("$wwwroot/".$link_path); | |
| 37 // if (!$path) { die("fail"); } | |
| 38 | |
| 39 $dir_handle = @opendir($root . $path); | |
| 13 $files = array(); | 40 $files = array(); |
| 14 $dirs = array(); | 41 $dirs = array(); |
| 15 while ($file = readdir($dir_handle)) { | 42 while (($file = readdir($dir_handle)) !== false) { |
| 16 if ($file == '.' or $file == '..') { continue; } | 43 if ($file === "." or $file === "..") { continue; } |
| 17 elseif (is_dir("$path/$file")) { $dirs[] = $file; } | 44 elseif (is_dir($root . $path . $file)) { $dirs[] = $file; } |
| 18 else { $files[] = $file; } | 45 else { $files[] = $file; } |
| 19 } | 46 } |
| 20 sort($files); | 47 sort($files); |
| 21 sort($dirs); | 48 sort($dirs); |
| 22 function h($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } | 49 |
| 50 // BEGIN UTILITY | |
| 51 function h($string) { return htmlspecialchars($string, ENT_QUOTES, "UTF-8"); } | |
| 23 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } | 52 function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); } |
| 24 function link_to($target, $title) { | 53 function link_to($target, $title) { return('<a href="' . a($target) . '">' . h($title) . "</a>"); } |
| 25 return("<a href=\"".a($target)."\">".h($title)."</a>"); | 54 |
| 26 } | 55 function human_size($size) { |
| 27 function path_tree_header() { | 56 $thousand_units = array("ko", "Mo", "Go", "To", "Po"); |
| 28 global $link_prefix, $link_base, $to_dir; | |
| 29 $path_tree = link_to('/', '[root]') . "/"; | |
| 30 foreach ($to_dir as $level => $dir) { | |
| 31 if($dir) { | |
| 32 $link = "/"; | |
| 33 for ($i = 0; $i <= $level; $i++) { $link .= "$to_dir[$i]/"; } | |
| 34 $path_tree .= link_to($link, $dir)."/"; | |
| 35 } | |
| 36 } | |
| 37 return "Index of $path_tree"; | |
| 38 } | |
| 39 function title() { | |
| 40 global $link_base; | |
| 41 return "Index of ".h(rtrim($link_base,"/")."/"); | |
| 42 } | |
| 43 | |
| 44 function nice_size($size) { | |
| 45 $thousand_units = array('ko', 'Mo', 'Go', 'To', 'Po'); | |
| 46 | 57 |
| 47 $return_format = "%d %s"; | 58 $return_format = "%d %s"; |
| 48 | 59 |
| 49 if ($size <= 1) { | 60 if ($size <= 1) { |
| 50 $return_unit = "octet"; | 61 $return_unit = "octet"; |
| 51 } elseif ($size < 10000) { | 62 } elseif ($size < 1000) { |
| 52 $return_unit = "octets"; | 63 $return_unit = "octets"; |
| 53 } else { | 64 } else { |
| 54 $size /= 1000; | 65 $size /= 1000; |
| 55 for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; } | 66 for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; } |
| 56 $return_format = "%.2f %s"; | 67 $return_format = "%.2f %s"; |
| 57 $return_unit = $thousand_units[$i]; | 68 $return_unit = $thousand_units[$i]; |
| 58 } | 69 } |
| 59 return sprintf($return_format, $size, $return_unit); | 70 return sprintf($return_format, $size, $return_unit); |
| 60 } | 71 } |
| 72 // END UTILITY | |
| 73 | |
| 74 function tree_link() { | |
| 75 global $path, $prefix; | |
| 76 | |
| 77 $path_array = explode("/", trim($path, "/")); | |
| 78 array_unshift($path_array, trim($prefix, "/")); | |
| 79 | |
| 80 $tree_path = "/"; | |
| 81 $tree_link = link_to($tree_path, "[root]") . "/"; | |
| 82 | |
| 83 foreach ($path_array as $p) { | |
| 84 if ($p === "") { continue; } | |
| 85 $tree_path .= $p . "/"; | |
| 86 $tree_link .= link_to($tree_path, $p) . "/"; | |
| 87 } | |
| 88 | |
| 89 return $tree_link; | |
| 90 } | |
| 61 | 91 |
| 62 function file_rows($files, $is_dir) { | 92 function file_rows($files, $is_dir) { |
| 63 global $path, $link_base, $link_prefix; | 93 global $path, $root, $prefix; |
| 64 | 94 |
| 65 $file_rows = ""; | 95 $file_rows = ""; |
| 96 $file_suffix = ""; | |
| 97 | |
| 66 if ($is_dir) { | 98 if ($is_dir) { |
| 67 $file_suffix = "/"; | 99 $file_suffix = "/"; |
| 68 if($link_base != $link_prefix) { | 100 |
| 69 $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base)."/","..")."</td></tr>"; | 101 if ($path !== "/") { |
| 70 } | 102 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "..") . "</td></tr>"; |
| 71 } else { $file_suffix = ""; } | 103 } |
| 104 } | |
| 72 | 105 |
| 73 foreach($files as $file) { | 106 foreach($files as $file) { |
| 74 $file_stat = stat("$path/".$file); | 107 $file_stat = stat($root . $path . "/". $file); |
| 75 | 108 |
| 76 $file_rows .= "<tr>"; | 109 $file_rows .= "<tr>"; |
| 77 $file_rows .= "<td>".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix)."</td>"; | 110 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; |
| 78 | 111 |
| 79 $file_rows .= "<td>"; | 112 $file_rows .= "<td>"; |
| 80 if ($is_dir) { $file_rows .= "[dir]"; } | 113 if ($is_dir) { $file_rows .= "[dir]"; } |
| 81 else { $file_rows .= nice_size($file_stat['size']); } | 114 else { $file_rows .= human_size($file_stat['size']); } |
| 82 $file_rows .= "</td>"; | 115 $file_rows .= "</td>"; |
| 83 | 116 |
| 84 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; | 117 $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; |
| 85 | 118 |
| 86 $file_rows .= "</tr>"; | 119 $file_rows .= "</tr>"; |
| 90 ?> | 123 ?> |
| 91 <?php header('Content-Type: text/html; charset=utf-8'); ?> | 124 <?php header('Content-Type: text/html; charset=utf-8'); ?> |
| 92 <!doctype html> | 125 <!doctype html> |
| 93 <head> | 126 <head> |
| 94 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | 127 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
| 95 <title><?php echo title(); ?></title> | 128 <title>Index of <?php echo h($prefix . $path); ?></title> |
| 96 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> | 129 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
| 97 <style type="text/css"> | 130 <style type="text/css"> |
| 98 * { box-sizing: border-box; } | 131 * { box-sizing: border-box; } |
| 99 body { | 132 body { |
| 100 font-family: Segoe UI, sans-serif; | 133 font-family: Segoe UI, sans-serif; |
| 145 } | 178 } |
| 146 } | 179 } |
| 147 </style> | 180 </style> |
| 148 </head> | 181 </head> |
| 149 <body> | 182 <body> |
| 150 <h1><?php echo path_tree_header(); ?></h1> | 183 <h1>Index of <?php echo tree_link(); ?></h1> |
| 151 | 184 |
| 152 <table> | 185 <table> |
| 153 <thead><tr> | 186 <thead><tr> |
| 154 <th>File</th> | 187 <th>File</th> |
| 155 <th>Size</th> | 188 <th>Size</th> |
| 158 <tbody> | 191 <tbody> |
| 159 <?php echo file_rows($dirs, true); ?> | 192 <?php echo file_rows($dirs, true); ?> |
| 160 <?php echo file_rows($files, false); ?> | 193 <?php echo file_rows($files, false); ?> |
| 161 </tbody> | 194 </tbody> |
| 162 </table> | 195 </table> |
| 196 | |
| 197 <footer> | |
| 198 <hr> | |
| 199 <em> | |
| 200 Running <a href="https://bitbucket.org/edogawaconan/dirlist-php">dirlist-php</a>. | |
| 201 Powered by PHP <?php echo phpversion(); ?>. | |
| 202 </em> | |
| 163 </body> | 203 </body> |
