| 
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"];
 | 
| 
 | 
    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);
 | 
| 
2
 | 
    40   $files = array();
 | 
| 
 | 
    41   $dirs = array();
 | 
| 
4
 | 
    42   while (($file = readdir($dir_handle)) !== false) {
 | 
| 
 | 
    43     if ($file === "." or $file === "..") { continue; }
 | 
| 
 | 
    44     elseif (is_dir($root . $path . $file)) { $dirs[] = $file; }
 | 
| 
2
 | 
    45     else { $files[] = $file; }
 | 
| 
 | 
    46   }
 | 
| 
 | 
    47   sort($files);
 | 
| 
 | 
    48   sort($dirs);
 | 
| 
4
 | 
    49 
 | 
| 
 | 
    50   // BEGIN UTILITY
 | 
| 
 | 
    51   function h($string) { return htmlspecialchars($string, ENT_QUOTES, "UTF-8"); }
 | 
| 
2
 | 
    52   function a($string) { return preg_replace("#(%2F)+#", "/", rawurlencode($string)); }
 | 
| 
4
 | 
    53   function link_to($target, $title) { return('<a href="' . a($target) . '">' . h($title) . "</a>"); }
 | 
| 
3
 | 
    54 
 | 
| 
4
 | 
    55   function human_size($size) {
 | 
| 
 | 
    56     $thousand_units = array("ko", "Mo", "Go", "To", "Po");
 | 
| 
3
 | 
    57 
 | 
| 
 | 
    58     $return_format = "%d %s";
 | 
| 
 | 
    59 
 | 
| 
 | 
    60     if ($size <= 1) {
 | 
| 
 | 
    61       $return_unit = "octet";
 | 
| 
4
 | 
    62     } elseif ($size < 1000) {
 | 
| 
3
 | 
    63       $return_unit = "octets";
 | 
| 
 | 
    64     } else {
 | 
| 
 | 
    65       $size /= 1000;
 | 
| 
 | 
    66       for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; }
 | 
| 
 | 
    67       $return_format = "%.2f %s";
 | 
| 
 | 
    68       $return_unit = $thousand_units[$i];
 | 
| 
 | 
    69     }
 | 
| 
 | 
    70     return sprintf($return_format, $size, $return_unit);
 | 
| 
2
 | 
    71   }
 | 
| 
4
 | 
    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   }
 | 
| 
3
 | 
    91 
 | 
| 
2
 | 
    92   function file_rows($files, $is_dir) {
 | 
| 
4
 | 
    93     global $path, $root, $prefix;
 | 
| 
3
 | 
    94 
 | 
| 
2
 | 
    95     $file_rows = "";
 | 
| 
4
 | 
    96     $file_suffix = "";
 | 
| 
 | 
    97 
 | 
| 
2
 | 
    98     if ($is_dir) {
 | 
| 
 | 
    99       $file_suffix = "/";
 | 
| 
4
 | 
   100 
 | 
| 
 | 
   101       if ($path !== "/") {
 | 
| 
 | 
   102         $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "..") . "</td></tr>";
 | 
| 
2
 | 
   103       }
 | 
| 
4
 | 
   104     }
 | 
| 
3
 | 
   105 
 | 
| 
2
 | 
   106     foreach($files as $file) {
 | 
| 
4
 | 
   107       $file_stat = stat($root . $path . "/". $file);
 | 
| 
3
 | 
   108 
 | 
| 
 | 
   109       $file_rows .= "<tr>";
 | 
| 
4
 | 
   110       $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>";
 | 
| 
3
 | 
   111 
 | 
| 
 | 
   112       $file_rows .= "<td>";
 | 
| 
2
 | 
   113       if ($is_dir) { $file_rows .= "[dir]"; }
 | 
| 
4
 | 
   114       else { $file_rows .= human_size($file_stat['size']); }
 | 
| 
3
 | 
   115       $file_rows .= "</td>";
 | 
| 
 | 
   116 
 | 
| 
 | 
   117       $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>";
 | 
| 
 | 
   118 
 | 
| 
2
 | 
   119       $file_rows .= "</tr>";
 | 
| 
 | 
   120     }
 | 
| 
 | 
   121     return $file_rows;
 | 
| 
 | 
   122   }
 | 
| 
0
 | 
   123 ?>
 | 
| 
3
 | 
   124 <?php header('Content-Type: text/html; charset=utf-8'); ?>
 | 
| 
 | 
   125 <!doctype html>
 | 
| 
 | 
   126 <head>
 | 
| 
 | 
   127   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 | 
| 
4
 | 
   128   <title>Index of <?php echo h($prefix . $path); ?></title>
 | 
| 
3
 | 
   129   <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
 | 
| 
 | 
   130   <style type="text/css">
 | 
| 
 | 
   131     * { box-sizing: border-box; }
 | 
| 
 | 
   132     body {
 | 
| 
 | 
   133       font-family: Segoe UI, sans-serif;
 | 
| 
 | 
   134       font-size: 14px;
 | 
| 
 | 
   135     }
 | 
| 
 | 
   136     h1 { margin: 5px; }
 | 
| 
 | 
   137     table {
 | 
| 
 | 
   138       width: 100%;
 | 
| 
 | 
   139     }
 | 
| 
 | 
   140     th:first-child, td:first-child {
 | 
| 
 | 
   141       width: 100%;
 | 
| 
 | 
   142       white-space: pre-wrap;
 | 
| 
 | 
   143       word-wrap: break-word;
 | 
| 
 | 
   144       word-break: break-all;
 | 
| 
 | 
   145     }
 | 
| 
 | 
   146     tr {
 | 
| 
 | 
   147       position: relative;
 | 
| 
 | 
   148     }
 | 
| 
 | 
   149     th, td {
 | 
| 
 | 
   150       white-space: nowrap;
 | 
| 
 | 
   151       padding: 2px 5px;
 | 
| 
 | 
   152     }
 | 
| 
 | 
   153 
 | 
| 
 | 
   154     @media (min-width: 768px) {
 | 
| 
 | 
   155       th { background: #ccc; }
 | 
| 
 | 
   156       tr:nth-child(even) { background: #eee; }
 | 
| 
 | 
   157       tr:hover { background: #ddd; }
 | 
| 
 | 
   158     }
 | 
| 
 | 
   159 
 | 
| 
 | 
   160     @media (max-width: 767px) {
 | 
| 
 | 
   161       table {
 | 
| 
 | 
   162         border-spacing: 0 10px;
 | 
| 
 | 
   163       }
 | 
| 
 | 
   164       th { display: none; }
 | 
| 
 | 
   165       tr {
 | 
| 
 | 
   166         background: #eee;
 | 
| 
 | 
   167       }
 | 
| 
 | 
   168       td {
 | 
| 
 | 
   169         display: inline-block;
 | 
| 
 | 
   170       }
 | 
| 
 | 
   171       td:first-child {
 | 
| 
 | 
   172         background: #ddd;
 | 
| 
 | 
   173         padding: 5px;
 | 
| 
 | 
   174       }
 | 
| 
 | 
   175       table a {
 | 
| 
 | 
   176         font-size: 18px;
 | 
| 
 | 
   177         display: block;
 | 
| 
 | 
   178       }
 | 
| 
 | 
   179     }
 | 
| 
 | 
   180   </style>
 | 
| 
 | 
   181 </head>
 | 
| 
 | 
   182 <body>
 | 
| 
4
 | 
   183   <h1>Index of <?php echo tree_link(); ?></h1>
 | 
| 
3
 | 
   184 
 | 
| 
 | 
   185   <table>
 | 
| 
 | 
   186     <thead><tr>
 | 
| 
 | 
   187       <th>File</th>
 | 
| 
 | 
   188       <th>Size</th>
 | 
| 
 | 
   189       <th>Date</th>
 | 
| 
 | 
   190     </tr></thead>
 | 
| 
 | 
   191     <tbody>
 | 
| 
 | 
   192       <?php echo file_rows($dirs, true); ?>
 | 
| 
 | 
   193       <?php echo file_rows($files, false); ?>
 | 
| 
 | 
   194     </tbody>
 | 
| 
 | 
   195   </table>
 | 
| 
4
 | 
   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>
 | 
| 
3
 | 
   203 </body>
 |