diff 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
line wrap: on
line diff
--- a/index.php	Thu Oct 23 21:07:21 2014 +0900
+++ b/index.php	Thu Oct 23 21:47:51 2014 +0900
@@ -1,40 +1,34 @@
 <?php
-  define('DL_VERSION', '1.0.0');
+  define('DL_VERSION', '2.0.0');
   // Required for strftime(). Set to UTC because :internet:.
   date_default_timezone_set('UTC');
 
-  // $path: actual requested path, with $alias_prefix removed, relative to $root.
-  $path = $_SERVER["REQUEST_URI"];
-  $query_string_start = strpos($path, "?");
+  // $uri: web-facing path
+  $uri = $_SERVER["REQUEST_URI"];
+  $query_string_start = strpos($uri, "?");
   if ($query_string_start !== false) {
-    $path = substr($path, 0, $query_string_start);
+    $uri = substr($uri, 0, $query_string_start);
   }
-  $path = urldecode($path);
-
-  $prefix = $_SERVER["DL_PREFIX"];
-  if ($prefix === null) { $prefix = ""; }
+  $uri = urldecode($uri);
 
-  $path = substr($path, strlen($prefix));
-  if ($path === false) { $path = "/"; }
+  // $dir: filesystem path
+  $dir = $_SERVER["DL_DIR"];
+  if ($dir === null || $dir === "") { $dir = $_SERVER["DOCUMENT_ROOT"] . $uri; }
 
-  // root of directory listing.
-  $root = $_SERVER["DL_ROOT"];
-  if ($root === null || $root === "") { $root = $_SERVER["DOCUMENT_ROOT"]; }
-
-  if (realpath($root . $path) === false) {
+  if (realpath($dir) === false) {
     header("HTTP/1.0 404 Not Found");
-  } elseif (substr($path, -1) !== "/") {
-    header("Location: " . $path . "/");
+  } elseif (substr($uri, -1) !== "/") {
+    header("Location: " . $uri . "/");
   }
 
   if (http_response_code() !== 200) { exit; }
 
-  $dir_handle = @opendir($root . $path);
+  $dir_handle = @opendir($dir);
   $files = array();
   $dirs = array();
   while (($file = readdir($dir_handle)) !== false) {
     if ($file === "." || $file === "..") { continue; }
-    elseif (is_dir($root . $path . $file)) { $dirs[] = $file; }
+    elseif (is_dir($dir . $file)) { $dirs[] = $file; }
     else { $files[] = $file; }
   }
   sort($files);
@@ -65,16 +59,15 @@
   // END UTILITY
 
   function tree_link() {
-    global $path, $prefix;
+    global $uri;
 
-    $path_array = explode("/", trim($path, "/"));
-    array_unshift($path_array, trim($prefix, "/"));
+    $uri_array = explode("/", trim($uri, "/"));
 
     $tree_path = "/";
-    if ($prefix === "") { $tree_link = link_to($tree_path, "[root]"); }
+    $tree_link = link_to($tree_path, "[root]");
     $tree_link .= "/";
 
-    foreach ($path_array as $p) {
+    foreach ($uri_array as $p) {
       if ($p === "") { continue; }
       $tree_path .= $p . "/";
       $tree_link .= link_to($tree_path, $p) . "/";
@@ -84,7 +77,7 @@
   }
 
   function file_rows($files, $is_dir) {
-    global $path, $root, $prefix;
+    global $dir, $uri;
 
     $file_rows = "";
     $file_suffix = "";
@@ -92,13 +85,13 @@
     if ($is_dir) {
       $file_suffix = "/";
 
-      if ($path !== "/") {
-        $file_rows .= "<tr><td colspan=3>" . link_to(dirname($prefix . $path) . "/", "[up]") . "</td></tr>";
+      if ($uri !== "/") {
+        $file_rows .= "<tr><td colspan=3>" . link_to(dirname($uri) . "/", "[up]") . "</td></tr>";
       }
     }
 
     foreach($files as $file) {
-      $file_stat = stat($root . $path . "/". $file);
+      $file_stat = stat($dir . "/". $file);
 
       $file_rows .= "<tr>";
       $file_rows .= "<td>".link_to($file . $file_suffix, $file . $file_suffix)."</td>";
@@ -119,7 +112,7 @@
 <!doctype html>
 <head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
-  <title>Index of <?php echo h($prefix . $path); ?></title>
+  <title>Index of <?php echo h($uri); ?></title>
   <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
   <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css">
   <style type="text/css">