Fixes include problems as well as Win path problems.

This commit is contained in:
Lars Jung 2012-02-15 18:41:13 +01:00
parent d72ea22fb0
commit 9f10e92175
13 changed files with 158 additions and 70 deletions

View File

@ -25,8 +25,8 @@ var H5AI_CONFIG = {
*
* This is disabled by default.
*/
"customHeader": false,
"customFooter": false,
"customHeader": "_h5ai.header.html",
"customFooter": "_h5ai.footer.html",
/*
* An array of view modes the user may choose from. Currently there
@ -105,13 +105,13 @@ var H5AI_CONFIG = {
* Requires PHP on the server.
* Show thumbnails for image files.
*/
"showThumbs": false,
"showThumbs": true,
/*
* Requires PHP on the server.
* Enable zipped download of selected entries.
*/
"zippedDownload": false
"zippedDownload": true
},

View File

@ -12,9 +12,9 @@ $H5AI_CONFIG = array();
/*
* This configuration assumes that h5ai is installed
* in the webroot directory of the Apache server.
* Assumed to end with a slash.
*/
$H5AI_CONFIG["H5AI_ROOT"] = dirname(dirname(__FILE__));
$H5AI_CONFIG["DOCUMENT_ROOT"] = dirname($H5AI_CONFIG["H5AI_ROOT"]);
$H5AI_CONFIG["ROOT_ABS_PATH"] = dirname(dirname(str_replace('\\', '/', __FILE__)));
/*
* Files/folders that should not be listed. Specified

View File

@ -1,5 +1,23 @@
<?php
function safe_dirname($path, $endWithSlash = false) {
$path = str_replace("\\", "/", dirname($path));
return preg_match("#^(\w:)?/$#", $path) ? $path : (preg_replace('#/$#', '', $path) . ($endWithSlash ? "/" : ""));
}
define("H5AI_ABS_PATH", safe_dirname(safe_dirname(__FILE__)));
function require_h5ai($lib) {
require_once(H5AI_ABS_PATH . $lib);
}
require_h5ai("/php/inc/H5ai.php");
$h5ai = new H5ai();
$options = $h5ai->getOptions();
function fail($code, $msg, $cond = true) {
if ($cond) {
@ -21,12 +39,6 @@ function checkKeys($keys) {
list($action) = checkKeys(array("action"));
require_once "config.php";
require_once "inc/H5ai.php";
$h5ai = new H5ai();
$options = $h5ai->getOptions();
if ($action === "httpcodes") {
list($hrefs) = checkKeys(array("hrefs"));
@ -55,10 +67,10 @@ else if ($action === "thumb") {
fail(0, "thumbs are disabled", !$options["showThumbs"]);
list($srcAbsHref, $width, $height, $mode) = checkKeys(array("href", "width", "height", "mode"));
require_once "inc/Thumbnail.php";
require_once "inc/Image.php";
require_h5ai("/php/inc/Thumbnail.php");
require_h5ai("/php/inc/Image.php");
$srcAbsPath = $h5ai->getDocRoot() . rawurldecode($srcAbsHref);
$srcAbsPath = $h5ai->getRootAbsPath() . rawurldecode($srcAbsHref);
if (!Thumbnail::isUsable()) {
Image::showImage($srcAbsPath);
@ -82,7 +94,7 @@ else if ($action === "tree") {
list($href) = checkKeys(array("href"));
require_once "inc/Tree.php";
require_h5ai("/php/inc/Tree.php");
$absHref = trim($href);
$absPath = $h5ai->getAbsPath($absHref);
@ -99,7 +111,7 @@ else if ($action === "zip") {
fail(0, "zipped download is disabled", !$options["zippedDownload"]);
list($hrefs) = checkKeys(array("hrefs"));
require_once "inc/ZipIt.php";
require_h5ai("/php/inc/ZipIt.php");
$zipit = new ZipIt($h5ai);

View File

@ -7,18 +7,22 @@
##############################################
class Cache {
private $dir;
function __construct($dir) {
$this->dir = $dir;
}
private function _name($key) {
return $this->dir . "/" . sha1($key);
}
public function get($key, $expiration = 3600) {
if (!is_dir($this->dir) || !is_writable($this->dir)) {
@ -55,6 +59,7 @@ class Cache {
return $cache;
}
public function set($key, $data) {
if (!is_dir($this->dir) || !is_writable($this->dir)) {
@ -78,6 +83,7 @@ class Cache {
return true;
}
public function clear($key) {
$cache_path = $this->_name($key);

View File

@ -1,8 +1,10 @@
<?php
class Crumb {
private $h5ai, $parts;
public function __construct($h5ai) {
$this->h5ai = $h5ai;
@ -11,13 +13,14 @@ class Crumb {
$href = $h5ai->getAbsHref();
while ($href !== "/" && $href !== "//") {
$this->parts[] = $href;
$href = dirname($href) . "/";
$href = safe_dirname($href, true);
}
$this->parts[] = "/";
$this->parts = array_reverse($this->parts);
}
public function toHtml() {
$html = "";

View File

@ -1,8 +1,10 @@
<?php
class Customize {
private $customHeader, $customFooter;
public function __construct($h5ai) {
$absPath = $h5ai->getAbsPath();
@ -11,19 +13,22 @@ class Customize {
$this->customFooter = $options["customFooter"] ? $absPath . "/" . $options["customFooter"] : false;
}
public function getHeader() {
return $this->getContent($this->customHeader, "header");
}
public function getFooter() {
return $this->getContent($this->customFooter, "footer");
}
private function getContent($file, $tag) {
return (is_string($file) && file_exists($file)) ? ("<" . $tag . ">" . file_get_contents($file) . "</" . $tag . ">") : "";
return (is_string($file) && file_exists($file)) ? ("<$tag>" . file_get_contents($file) . "</$tag>") : "";
}
}

View File

@ -1,11 +1,13 @@
<?php
require_once "Thumbnail.php";
require_h5ai("/php/inc/Thumbnail.php");
class Entry {
private $h5ai, $label, $absPath, $absHref, $date, $isFolder, $type, $size, $thumbTypes;
public function __construct($h5ai, $absPath, $absHref, $type = null, $label = null) {
$this->h5ai = $h5ai;
@ -27,11 +29,13 @@ class Entry {
$this->thumbTypes = array("bmp", "gif", "ico", "image", "jpg", "png", "tiff");
}
public function isFolder() {
return $this->isFolder;
}
public function toHtml() {
$classes = "entry " . ($this->isFolder ? "folder " : "file ") . $this->type;
@ -80,8 +84,10 @@ class Entry {
class Extended {
private $h5ai, $parent, $content;
public function __construct($h5ai) {
$this->h5ai = $h5ai;
@ -90,12 +96,13 @@ class Extended {
$this->loadContent();
}
private function loadContent() {
if ($this->h5ai->getAbsHref() !== "/") {
$options = $this->h5ai->getOptions();
$parentPath = dirname($this->h5ai->getAbsPath());
$parentHref = dirname($this->h5ai->getAbsHref());
$parentPath = safe_dirname($this->h5ai->getAbsPath());
$parentHref = safe_dirname($this->h5ai->getAbsHref(), true);
$label = $options["setParentFolderLabels"] === true ? $this->h5ai->getLabel($parentHref) : "<span class='l10n-parentDirectory'>Parent Directory</span>";
$this->parent = new Entry($this->h5ai, $parentPath, $parentHref, "folder-parent", $label);
}
@ -110,6 +117,7 @@ class Extended {
}
}
public function toHtml() {
$html = "<section id='extended' class='" . $this->h5ai->getView() . "-view clearfix'>\n";
@ -129,6 +137,7 @@ class Extended {
return $html;
}
public function generateHeaders() {
$html = "\t<li class='header'>\n";

View File

@ -1,34 +1,38 @@
<?php
require_once "Cache.php";
require_h5ai("/config.php");
require_h5ai("/php/inc/Cache.php");
class H5ai {
private static $VIEWMODES = array("details", "icons");
private $docRoot, $h5aiRoot, $h5aiAbsHref, $domain,
private $h5aiAbsPath,
$rootAbsPath, $ignore, $ignoreRE,
$config, $options, $types, $langs,
$cache,
$rootAbsHref, $h5aiAbsHref, $domain,
$absHref, $absPath,
$ignore, $ignoreRE,
$view;
public function __construct() {
global $H5AI_CONFIG;
$this->h5aiAbsPath = H5AI_ABS_PATH;
$this->docRoot = $H5AI_CONFIG["DOCUMENT_ROOT"];
$this->h5aiRoot = $H5AI_CONFIG["H5AI_ROOT"];
global $H5AI_CONFIG;
$this->rootAbsPath = $H5AI_CONFIG["ROOT_ABS_PATH"];
$this->ignore = $H5AI_CONFIG["IGNORE"];
$this->ignoreRE = $H5AI_CONFIG["IGNORE_PATTERNS"];
$this->config = $this->loadConfig($this->h5aiRoot . "/config.js");
$this->config = $this->loadConfig($this->h5aiAbsPath . "/config.js");
$this->options = $this->config["options"];
$this->types = $this->config["types"];
$this->langs = $this->config["langs"];
$this->cache = new Cache($this->h5aiRoot . "/cache");
$this->cache = new Cache($this->h5aiAbsPath . "/cache");
$this->hrefRoot = $this->options["rootAbsHref"];
$this->rootAbsHref = $this->options["rootAbsHref"];
$this->h5aiAbsHref = $this->options["h5aiAbsHref"];
$this->domain = getenv("HTTP_HOST");
@ -56,101 +60,111 @@ class H5ai {
}
public function getDocRoot() {
public function getH5aiAbsPath() {
return $this->docRoot;
return $this->h5aiAbsPath;
}
public function getH5aiRoot() {
return $this->h5aiRoot;
public function getRootAbsPath() {
return $this->rootAbsPath;
}
public function getHrefRoot() {
return $this->hrefRoot;
public function getRootAbsHref() {
return $this->rootAbsHref;
}
public function getH5aiAbsHref() {
return $this->h5aiAbsHref;
}
public function getDomain() {
return $this->domain;
}
public function getView() {
return $this->view;
}
public function api() {
return $this->h5aiAbsHref . "php/api.php";
}
public function image($id) {
return $this->h5aiAbsHref . "images/" . $id . ".png";
}
public function icon($id, $big = false) {
return $this->h5aiAbsHref . "icons/" . ($big ? "48x48" : "16x16") . "/" . $id . ".png";
}
public function getOptions() {
return $this->options;
}
public function getLangs() {
return $this->langs;
}
public function getAbsHref($absPath = null, $endWithSlash = true) {
if ($absPath === null) {
return $this->absHref;
}
//
$absPath=substr($absPath, strlen($this->docRoot));
$absPath=substr($absPath, strlen($this->rootAbsPath));
$absHref = preg_replace("!^" . $this->docRoot . "!", "", $absPath);
$parts = explode("/", $absHref);
$parts = explode("/", $absPath);
$encodedParts = array();
foreach ($parts as $part) {
$encodedParts[] = rawurlencode($part);
}
$endodedAbsHref = implode("/", $encodedParts);
//
$endodedAbsHref = $this->hrefRoot . $endodedAbsHref;
$endodedAbsHref = $this->rootAbsHref . $endodedAbsHref;
return $this->normalizePath($endodedAbsHref, $endWithSlash);
}
public function getAbsPath($absHref = null) {
if ($absHref === null) {
return $this->absPath;
}
//
$absHref=substr($absHref, strlen($this->hrefRoot));
$absHref=substr($absHref, strlen($this->rootAbsHref));
return $this->normalizePath($this->docRoot . "/" . rawurldecode($absHref), false);
return $this->normalizePath($this->rootAbsPath . "/" . rawurldecode($absHref), false);
}
public function showThumbs() {
return $this->options["showThumbs"] === true;
}
public function getTitle() {
$title = $this->domain . rawurldecode($this->absHref);
@ -162,6 +176,7 @@ class H5ai {
return $title;
}
public function getType($absPath) {
foreach($this->types as $type => $exts) {
@ -174,9 +189,10 @@ class H5ai {
return "unknown";
}
public function ignoreThisFile($file) {
# always ignore
// always ignore
if ($file === "." || $file === ".." || $this->startsWith($file, '.ht')) {
return true;
}
@ -193,6 +209,7 @@ class H5ai {
return false;
}
public function readDir($path) {
$content = array();
@ -209,33 +226,39 @@ class H5ai {
return $content;
}
public function getLabel($absHref) {
return $absHref === "/" ? $this->domain : rawurldecode(basename($absHref));
}
public function normalizePath($path, $endWithSlash) {
return ($path === "/") ? "/" : (preg_replace('#/$#', '', $path) . ($endWithSlash ? "/" : ""));
return $path === "/" ? "/" : (preg_replace('#/$#', '', $path) . ($endWithSlash ? "/" : ""));
}
public function startsWith($sequence, $start) {
return strcasecmp(substr($sequence, 0, strlen($start)), $start) === 0;
}
public function endsWith($sequence, $end) {
return strcasecmp(substr($sequence, -strlen($end)), $end) === 0;
}
public function getHttpCode($absHref) {
return $this->cachedHttpCode($absHref);
#return $this->fetchHttpCode($absHref);
#return $this->guessHttpCode($absHref);
// return $this->fetchHttpCode($absHref);
// return $this->guessHttpCode($absHref);
}
public function cachedHttpCode($absHref) {
$cached = $this->cache->get($absHref);
@ -252,6 +275,7 @@ class H5ai {
return $cached["code"];
}
public function fetchHttpCode($absHref) {
$contentType = "Content-Type:";
@ -286,6 +310,7 @@ class H5ai {
return $code;
}
public function guessHttpCode($absHref) {
$indexFiles = array("index.html", "index.cgi", "index.pl", "index.php", "index.xhtml", "index.htm");

View File

@ -2,13 +2,7 @@
class Image {
private $sourceFile;
private $source;
private $width;
private $height;
private $type;
private $dest;
private $sourceFile, $source, $width, $height, $type, $dest;
public static function isUsable() {

View File

@ -1,31 +1,34 @@
<?php
require_once "Image.php";
require_h5ai("/php/inc/Image.php");
class Thumbnail {
private $srcAbsHref, $srcAbsPath, $width, $height, $name, $href, $path;
public static function isUsable() {
return Image::isUsable();
}
public function __construct($h5ai, $absHref, $mode, $width, $height) {
$this->h5ai = $h5ai;
$this->srcAbsHref = $absHref;
$this->srcAbsPath = $this->h5ai->getDocRoot() . urldecode($absHref);
$this->srcAbsPath = $this->h5ai->getRootAbsPath() . urldecode($absHref);
$this->width = $width;
$this->height = $height;
$this->mode = $mode;
$this->name = sha1("$this->srcAbsPath-$this->width-$this->height-$this->mode");
$this->href = $this->h5ai->getH5aiAbsHref() . "/cache/thumb-" . $this->name . ".jpg";
$this->path = $this->h5ai->getDocRoot() . $this->href;
$this->href = $this->h5ai->getH5aiAbsHref() . "cache/thumb-" . $this->name . ".jpg";
$this->path = $this->h5ai->getRootAbsPath() . $this->href;
$this->liveHref = $this->h5ai->api() . "?action=thumb&href=" . $this->srcAbsHref . "&width=" . $this->width . "&height=" . $this->height . "&mode=" . $this->mode;
}
public function create($force = 0) {
if (
@ -40,16 +43,19 @@ class Thumbnail {
}
}
public function getHref() {
return $this->href;
}
public function getPath() {
return $this->path;
}
public function getLiveHref() {
return $this->liveHref;

View File

@ -1,8 +1,10 @@
<?php
class TreeEntry {
private $h5ai, $label, $absPath, $absHref, $isFolder, $type, $content;
public function __construct($h5ai, $absPath, $absHref, $type = null) {
$this->h5ai = $h5ai;
@ -16,6 +18,7 @@ class TreeEntry {
$this->content = null;
}
public function loadContent() {
$this->content = array();
@ -36,6 +39,7 @@ class TreeEntry {
$this->sort();
}
public function cmpTrees($t1, $t2) {
if ($t1->isFolder && !$t2->isFolder) {
@ -47,6 +51,7 @@ class TreeEntry {
return strcasecmp($t1->absPath, $t2->absPath);
}
public function sort() {
if ($this->content !== null) {
@ -54,6 +59,7 @@ class TreeEntry {
}
}
public function toHtml() {
$classes = "entry " . $this->type . ($this->absHref === $this->h5ai->getAbsHref() ? " current" : "");
@ -94,6 +100,7 @@ class TreeEntry {
return $html;
}
public function contentToHtml() {
$html = "<ul class='content'>\n";
@ -106,13 +113,14 @@ class TreeEntry {
return $html;
}
public function getRoot() {
if ($this->absHref === "/") {
return $this;
};
$tree = new TreeEntry($this->h5ai, dirname($this->absPath), dirname($this->absHref));
$tree = new TreeEntry($this->h5ai, safe_dirname($this->absPath), safe_dirname($this->absHref, true));
$tree->loadContent();
$tree->content[$this->absPath] = $this;
@ -122,13 +130,16 @@ class TreeEntry {
class Tree {
private $h5ai;
public function __construct($h5ai) {
$this->h5ai = $h5ai;
}
public function toHtml() {
$options = $this->h5ai->getOptions();

View File

@ -1,13 +1,16 @@
<?php
class ZipIt {
private $h5ai;
public function __construct($h5ai) {
$this->h5ai = $h5ai;
}
public function zip($hrefs) {
$zipFile = tempnam("/tmp", "h5ai-download");
@ -18,11 +21,11 @@ class ZipIt {
}
foreach ($hrefs as $href) {
$d = dirname($href);
$d = safe_dirname($href, true);
$n = basename($href);
if ($this->h5ai->getHttpCode($this->h5ai->getAbsHref($d)) === "h5ai" && !$this->h5ai->ignoreThisFile($n)) {
$localFile = $this->h5ai->getAbsPath($href);
$file = preg_replace("!^" . $this->h5ai->getDocRoot() . "!", "", $localFile);
$file = preg_replace("!^" . $this->h5ai->getRootAbsPath() . "!", "", $localFile);
if (is_dir($localFile)) {
$this->zipDir($zip, $localFile, $file);
} else {
@ -35,6 +38,7 @@ class ZipIt {
return $zipFile;
}
private function zipFile($zip, $localFile, $file) {
if (is_readable($localFile)) {
@ -42,6 +46,7 @@ class ZipIt {
}
}
private function zipDir($zip, $localDir, $dir) {
if ($this->h5ai->getHttpCode($this->h5ai->getAbsHref($localDir)) === "h5ai") {

View File

@ -1,11 +1,23 @@
<?php
require_once "config.php";
require_once "inc/H5ai.php";
require_once "inc/Crumb.php";
require_once "inc/Customize.php";
require_once "inc/Extended.php";
require_once "inc/Tree.php";
function safe_dirname($path, $endWithSlash = false) {
$path = str_replace("\\", "/", dirname($path));
return preg_match("#^(\w:)?/$#", $path) ? $path : (preg_replace('#/$#', '', $path) . ($endWithSlash ? "/" : ""));
}
define("H5AI_ABS_PATH", safe_dirname(safe_dirname(__FILE__)));
function require_h5ai($lib) {
require_once(H5AI_ABS_PATH . $lib);
}
require_h5ai("/php/inc/H5ai.php");
require_h5ai("/php/inc/Crumb.php");
require_h5ai("/php/inc/Customize.php");
require_h5ai("/php/inc/Extended.php");
require_h5ai("/php/inc/Tree.php");
$h5ai = new H5ai();
$crumb = new Crumb($h5ai);