changeset 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 f9588ccb7a42
files README.md index.php
diffstat 2 files changed, 38 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/README.md	Thu Oct 23 21:07:21 2014 +0900
+++ b/README.md	Thu Oct 23 21:47:51 2014 +0900
@@ -9,29 +9,34 @@
       include fastcgi_params;
       fastcgi_pass unix:/tmp/php-fcgi.sock;
       uninitialized_variable_warn off;
-      fastcgi_param DL_PREFIX $dl_root;
-      fastcgi_param DL_ROOT $dl_prefix;
+      fastcgi_param DL_DIR $dl_dir;
       fastcgi_param SCRIPT_FILENAME /path/to/index.php;
     }
 
-(adjust the path to `index.php` and `php-fcgi`)
+(adjust the path to `index.php` and `php-fcgi`).
 
-And then whenever you want to autoindex a folder just add
+And whenever a directory needs to be autoindexed, add
 
     location / {
       try_files $uri @lister;
     }
 
-Note that if your directory is aliased, you need to set `$dl_prefix` (fastcgi `DL_PREFIX`) and `$dl_root` (fastcgi `DL_ROOT`).
+.
+
+Note that if the directory is aliased, `$dl_dir` (fastcgi `DL_DIR`) needs to be set.
 
     location /anime/win/ {
-      set $dl_prefix /anime/win;
-      set $dl_root /srv/ftp;
-      alias $dl_root/;
-      try_files $uri @lister;
+      set $dl_dir $request_filename;
+      alias /srv/ftp/;
+      try_files $uri @lister; // try_files "" @lister if the alias is
     }
 
-And you're done. Probably also works with apache but I have no idea how to setup it.
+And done. Probably also works with apache but I have no idea how to set it up.
+
+Upgrading
+---------
+
+When upgrading from 1.0.0, update nginx config accordingly.
 
 License
 -------
--- 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">