Mercurial > dirlist-php
comparison index.php @ 23:949398173ecb
Much simpler setup with $request_filename.
Configuration change, bump version.
| author | edogawaconan <me@myconan.net> | 
|---|---|
| date | Thu, 23 Oct 2014 21:47:51 +0900 | 
| parents | 431682ff9169 | 
| children | 69b2c15cadfb | 
   comparison
  equal
  deleted
  inserted
  replaced
| 22:431682ff9169 | 23:949398173ecb | 
|---|---|
| 1 <?php | 1 <?php | 
| 2 define('DL_VERSION', '1.0.0'); | 2 define('DL_VERSION', '2.0.0'); | 
| 3 // Required for strftime(). Set to UTC because :internet:. | 3 // Required for strftime(). Set to UTC because :internet:. | 
| 4 date_default_timezone_set('UTC'); | 4 date_default_timezone_set('UTC'); | 
| 5 | 5 | 
| 6 // $path: actual requested path, with $alias_prefix removed, relative to $root. | 6 // $uri: web-facing path | 
| 7 $path = $_SERVER["REQUEST_URI"]; | 7 $uri = $_SERVER["REQUEST_URI"]; | 
| 8 $query_string_start = strpos($path, "?"); | 8 $query_string_start = strpos($uri, "?"); | 
| 9 if ($query_string_start !== false) { | 9 if ($query_string_start !== false) { | 
| 10 $path = substr($path, 0, $query_string_start); | 10 $uri = substr($uri, 0, $query_string_start); | 
| 11 } | 11 } | 
| 12 $path = urldecode($path); | 12 $uri = urldecode($uri); | 
| 13 | 13 | 
| 14 $prefix = $_SERVER["DL_PREFIX"]; | 14 // $dir: filesystem path | 
| 15 if ($prefix === null) { $prefix = ""; } | 15 $dir = $_SERVER["DL_DIR"]; | 
| 16 | 16 if ($dir === null || $dir === "") { $dir = $_SERVER["DOCUMENT_ROOT"] . $uri; } | 
| 17 $path = substr($path, strlen($prefix)); | 17 | 
| 18 if ($path === false) { $path = "/"; } | 18 if (realpath($dir) === false) { | 
| 19 | |
| 20 // root of directory listing. | |
| 21 $root = $_SERVER["DL_ROOT"]; | |
| 22 if ($root === null || $root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; } | |
| 23 | |
| 24 if (realpath($root . $path) === false) { | |
| 25 header("HTTP/1.0 404 Not Found"); | 19 header("HTTP/1.0 404 Not Found"); | 
| 26 } elseif (substr($path, -1) !== "/") { | 20 } elseif (substr($uri, -1) !== "/") { | 
| 27 header("Location: " . $path . "/"); | 21 header("Location: " . $uri . "/"); | 
| 28 } | 22 } | 
| 29 | 23 | 
| 30 if (http_response_code() !== 200) { exit; } | 24 if (http_response_code() !== 200) { exit; } | 
| 31 | 25 | 
| 32 $dir_handle = @opendir($root . $path); | 26 $dir_handle = @opendir($dir); | 
| 33 $files = array(); | 27 $files = array(); | 
| 34 $dirs = array(); | 28 $dirs = array(); | 
| 35 while (($file = readdir($dir_handle)) !== false) { | 29 while (($file = readdir($dir_handle)) !== false) { | 
| 36 if ($file === "." || $file === "..") { continue; } | 30 if ($file === "." || $file === "..") { continue; } | 
| 37 elseif (is_dir($root . $path . $file)) { $dirs[] = $file; } | 31 elseif (is_dir($dir . $file)) { $dirs[] = $file; } | 
| 38 else { $files[] = $file; } | 32 else { $files[] = $file; } | 
| 39 } | 33 } | 
| 40 sort($files); | 34 sort($files); | 
| 41 sort($dirs); | 35 sort($dirs); | 
| 42 | 36 | 
| 63 return sprintf($return_format, $size, $return_unit); | 57 return sprintf($return_format, $size, $return_unit); | 
| 64 } | 58 } | 
| 65 // END UTILITY | 59 // END UTILITY | 
| 66 | 60 | 
| 67 function tree_link() { | 61 function tree_link() { | 
| 68 global $path, $prefix; | 62 global $uri; | 
| 69 | 63 | 
| 70 $path_array = explode("/", trim($path, "/")); | 64 $uri_array = explode("/", trim($uri, "/")); | 
| 71 array_unshift($path_array, trim($prefix, "/")); | |
| 72 | 65 | 
| 73 $tree_path = "/"; | 66 $tree_path = "/"; | 
| 74 if ($prefix === "") { $tree_link = link_to($tree_path, "[root]"); } | 67 $tree_link = link_to($tree_path, "[root]"); | 
| 75 $tree_link .= "/"; | 68 $tree_link .= "/"; | 
| 76 | 69 | 
| 77 foreach ($path_array as $p) { | 70 foreach ($uri_array as $p) { | 
| 78 if ($p === "") { continue; } | 71 if ($p === "") { continue; } | 
| 79 $tree_path .= $p . "/"; | 72 $tree_path .= $p . "/"; | 
| 80 $tree_link .= link_to($tree_path, $p) . "/"; | 73 $tree_link .= link_to($tree_path, $p) . "/"; | 
| 81 } | 74 } | 
| 82 | 75 | 
| 83 return $tree_link; | 76 return $tree_link; | 
| 84 } | 77 } | 
| 85 | 78 | 
| 86 function file_rows($files, $is_dir) { | 79 function file_rows($files, $is_dir) { | 
| 87 global $path, $root, $prefix; | 80 global $dir, $uri; | 
| 88 | 81 | 
| 89 $file_rows = ""; | 82 $file_rows = ""; | 
| 90 $file_suffix = ""; | 83 $file_suffix = ""; | 
| 91 | 84 | 
| 92 if ($is_dir) { | 85 if ($is_dir) { | 
| 93 $file_suffix = "/"; | 86 $file_suffix = "/"; | 
| 94 | 87 | 
| 95 if ($path !== "/") { | 88 if ($uri !== "/") { | 
| 96 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "[up]") . "</td></tr>"; | 89 $file_rows .= "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>"; | 
| 97 } | 90 } | 
| 98 } | 91 } | 
| 99 | 92 | 
| 100 foreach($files as $file) { | 93 foreach($files as $file) { | 
| 101 $file_stat = stat($root . $path . "/". $file); | 94 $file_stat = stat($dir . "/". $file); | 
| 102 | 95 | 
| 103 $file_rows .= "<tr>"; | 96 $file_rows .= "<tr>"; | 
| 104 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; | 97 $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>"; | 
| 105 | 98 | 
| 106 $file_rows .= "<td>"; | 99 $file_rows .= "<td>"; | 
| 117 ?> | 110 ?> | 
| 118 <?php header('Content-Type: text/html; charset=utf-8'); ?> | 111 <?php header('Content-Type: text/html; charset=utf-8'); ?> | 
| 119 <!doctype html> | 112 <!doctype html> | 
| 120 <head> | 113 <head> | 
| 121 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | 114 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | 
| 122 <title>Index of <?php echo h($prefix . $path); ?></title> | 115 <title>Index of <?php echo h($uri); ?></title> | 
| 123 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> | 116 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> | 
| 124 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css"> | 117 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css"> | 
| 125 <style type="text/css"> | 118 <style type="text/css"> | 
| 126 .lb-data a { color: #ccc; } | 119 .lb-data a { color: #ccc; } | 
| 127 .lightbox { | 120 .lightbox { | 
