diff --git a/README.md b/README.md index c6f9dde3..8095988e 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ To create an fresh zipball run * adds auto-refresh (PHP) * adds drag'n'drop upload (PHP) * adds file deletion (PHP) +* cleans and improves PHP code * PHP ignore patterns might include paths now * adds optional binary prefixes for file sizes * improves filter: autofocus on keypress, clear on `ESC` diff --git a/src/_h5ai/conf/config.json b/src/_h5ai/conf/config.json index f2cfeb41..e7abe9c0 100644 --- a/src/_h5ai/conf/config.json +++ b/src/_h5ai/conf/config.json @@ -10,10 +10,10 @@ options, types and langs "options": { /* - The absolute link to h5ai. - Must point to the "_h5ai" directory. + The absolute link to {{pkg.name}}. + Must point to the "_{{pkg.name}}" directory. */ - "h5aiAbsHref": "/_h5ai/", + "h5aiAbsHref": "/_{{pkg.name}}/", /* Spacing of the main content. @@ -43,7 +43,9 @@ options, types and langs "view": { "modes": ["details", "list", "grid", "icons"], "setParentFolderLabels": true, - "binaryPrefix": false + "binaryPrefix": false, + "indexFiles": ["index.html", "index.htm", "index.php"], + "ignore": ["^\\.", "^_{{pkg.name}}"] }, @@ -75,8 +77,8 @@ options, types and langs */ "custom": { "enabled": true, - "header": "_h5ai.header.html", - "footer": "_h5ai.footer.html" + "header": "_{{pkg.name}}.header.html", + "footer": "_{{pkg.name}}.footer.html" }, /* [php] diff --git a/src/_h5ai/conf/config.php b/src/_h5ai/conf/config.php deleted file mode 100644 index 9b24157c..00000000 --- a/src/_h5ai/conf/config.php +++ /dev/null @@ -1,29 +0,0 @@ - array(), - "IGNORE_PATTERNS" => array("/^\\./", "/^_h5ai/"), - - /* - Folders that contain one of these files will be considered - not to be h5ai indexed folders. - */ - "INDEX_FILES" => array("index.html", "index.htm", "index.php") -); - -?> \ No newline at end of file diff --git a/src/_h5ai/server/php/api.php b/src/_h5ai/server/php/api.php index f547d4df..17107fe5 100644 --- a/src/_h5ai/server/php/api.php +++ b/src/_h5ai/server/php/api.php @@ -2,7 +2,7 @@ require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/init.php"); $h5ai = $APP; -$options = $h5ai->getOptions(); +$options = $h5ai->get_options(); list($action) = use_request_params(array("action")); @@ -58,7 +58,7 @@ else if ($action === "getarchive") { list($id, $as) = use_request_params(array("id", "as")); json_fail(2, "file not found", !preg_match("/^package-/", $id)); - $target = $h5ai->getCacheAbsPath() . "/" . $id; + $target = $h5ai->get_cache_abs_path() . "/" . $id; json_fail(3, "file not found", !file_exists($target)); header("Content-Type: application/octet-stream"); @@ -79,7 +79,7 @@ else if ($action === "getchecks") { $gdinfo = gd_info(); $gd = array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"]; } - $cache = @is_writable($h5ai->getCacheAbsPath()); + $cache = @is_writable($h5ai->get_cache_abs_path()); $tar = @preg_match("/tar$/", `which tar`) > 0; $zip = @preg_match("/zip$/", `which zip`) > 0; $convert = @preg_match("/convert$/", `which convert`) > 0; @@ -106,7 +106,7 @@ else if ($action === "getentries") { $content = intval($content, 10); - json_exit(array("entries" => $h5ai->getEntries($href, $content))); + json_exit(array("entries" => $h5ai->get_entries($href, $content))); } @@ -122,8 +122,8 @@ else if ($action === "upload") { json_fail(3, "something went wrong [" . $userfile["error"] . "]", $userfile["error"] !== 0); json_fail(4, "folders not supported", file_get_contents($userfile["tmp_name"]) === "null"); - $upload_dir = $h5ai->getAbsPath($href); - $code = $h5ai->getHttpCode($href); + $upload_dir = $h5ai->get_abs_path($href); + $code = $h5ai->get_http_code($href); json_fail(5, "upload dir no h5ai folder or ignored", $code !== "h5ai" || $h5ai->is_ignored($upload_dir)); @@ -150,13 +150,13 @@ else if ($action === "delete") { $d = normalize_path(dirname($href), true); $n = basename($href); - $code = $h5ai->getHttpCode($d); + $code = $h5ai->get_http_code($d); if ($code == 401) { } if ($code == "h5ai" && !$h5ai->is_ignored($n)) { - $absPath = $h5ai->getAbsPath($href); + $absPath = $h5ai->get_abs_path($href); if (!unlink($absPath)) { $errors[] = $href; @@ -181,13 +181,13 @@ else if ($action === "rename") { $d = normalize_path(dirname($href), true); $n = basename($href); - $code = $h5ai->getHttpCode($d); + $code = $h5ai->get_http_code($d); if ($code == 401) { } if ($code == "h5ai" && !$h5ai->is_ignored($n)) { - $absPath = $h5ai->getAbsPath($href); + $absPath = $h5ai->get_abs_path($href); $folder = normalize_path(dirname($absPath)); if (!rename($absPath, $folder . "/" . $name)) { diff --git a/src/_h5ai/server/php/inc/App.php b/src/_h5ai/server/php/inc/App.php index 1d31a7e7..7c3356ae 100644 --- a/src/_h5ai/server/php/inc/App.php +++ b/src/_h5ai/server/php/inc/App.php @@ -1,111 +1,101 @@ h5aiAbsPath = normalize_path($app_abs_path); - $this->rootAbsPath = normalize_path(dirname($app_abs_path)); + $this->app_abs_path = normalize_path($app_abs_path); + $this->root_abs_path = normalize_path(dirname($this->app_abs_path)); - $this->h5aiAbsHref = normalize_path($app_abs_href, true); - $this->rootAbsHref = normalize_path(dirname($app_abs_href), true); + $this->app_abs_href = normalize_path($app_abs_href, true); + $this->root_abs_href = normalize_path(dirname($this->app_abs_href), true); - $this->absHref = normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true); - $this->absPath = $this->getAbsPath($this->absHref); + $this->abs_href = normalize_path($abs_href, true); + $this->abs_path = $this->get_abs_path($this->abs_href); - global $H5AI_CONFIG; - $this->ignore_names = $H5AI_CONFIG["IGNORE"]; - $this->ignore_patterns = $H5AI_CONFIG["IGNORE_PATTERNS"]; - $this->index_files = $H5AI_CONFIG["INDEX_FILES"]; - - $this->config = array("options" => array(), "types" => array(), "langs" => array()); - $this->config = merge_config($this->config, load_commented_json($this->h5aiAbsPath . "/conf/config.json")); - $this->options = $this->config["options"]; - - $this->config = merge_config($this->config, load_commented_json($this->absPath . "/_h5ai.config.json")); - $this->options = $this->config["options"]; + $config = array("options" => array(), "types" => array(), "langs" => array()); + $config = merge_config($config, load_commented_json($this->app_abs_path . "/conf/config.json")); + $config = merge_config($config, load_commented_json($this->abs_path . "/" . App::$FILE_PREFIX . ".config.json")); + $this->options = $config["options"]; } - public function getRootAbsPath() { + public function get_root_abs_path() { - return $this->rootAbsPath; + return $this->root_abs_path; } - public function getH5aiAbsPath() { + public function get_root_abs_href() { - return $this->h5aiAbsPath; + return $this->root_abs_href; } - public function getRootAbsHref() { + public function get_app_abs_href() { - return $this->rootAbsHref; + return $this->app_abs_href; } - public function getH5aiAbsHref() { + public function get_cache_abs_path() { - return $this->h5aiAbsHref; + return $this->app_abs_path . '/' . App::$CACHE_DIR; } - public function getCacheAbsPath() { + public function get_cache_abs_href() { - return $this->h5aiAbsPath . '/cache'; + return $this->app_abs_href . App::$CACHE_DIR . '/'; } - public function getCacheAbsHref() { - - return $this->h5aiAbsHref . 'cache/'; - } - - - public function getOptions() { + public function get_options() { return $this->options; } - public function getAbsHref($absPath = null, $endWithSlash = true) { + public function get_abs_href($abs_path = null, $trailing_slash = true) { - if ($absPath === null) { - return $this->absHref; + if ($abs_path === null) { + return $this->abs_href; } - $absPath = substr($absPath, strlen($this->rootAbsPath)); + $abs_path = substr($abs_path, strlen($this->root_abs_path)); - $parts = explode("/", $absPath); - $encodedParts = array(); + $parts = explode("/", $abs_path); + $encoded_parts = array(); foreach ($parts as $part) { - $encodedParts[] = rawurlencode($part); + $encoded_parts[] = rawurlencode($part); } - return normalize_path($this->rootAbsHref . implode("/", $encodedParts), $endWithSlash); + return normalize_path($this->root_abs_href . implode("/", $encoded_parts), $trailing_slash); } - public function getAbsPath($absHref = null) { + public function get_abs_path($abs_href = null) { - if ($absHref === null) { - return $this->absPath; + if ($abs_href === null) { + return $this->abs_path; } - $absHref = substr($absHref, strlen($this->rootAbsHref)); + $abs_href = substr($abs_href, strlen($this->root_abs_href)); - return normalize_path($this->rootAbsPath . "/" . rawurldecode($absHref)); + return normalize_path($this->root_abs_path . "/" . rawurldecode($abs_href)); } @@ -116,10 +106,8 @@ class H5ai { return true; } - if (in_array($name, $this->ignore_names)) { - return true; - } - foreach ($this->ignore_patterns as $re) { + foreach ($this->options["view"]["ignore"] as $re) { + $re = App::$RE_DELIMITER . str_replace(App::$RE_DELIMITER, '\\' . App::$RE_DELIMITER, $re) . App::$RE_DELIMITER; if (preg_match($re, $name)) { return true; } @@ -135,7 +123,7 @@ class H5ai { if (is_dir($path)) { if ($dir = opendir($path)) { while (($file = readdir($dir)) !== false) { - if (!$this->is_ignored($file) && !$this->is_ignored($this->getAbsHref($path) . $file)) { + if (!$this->is_ignored($file) && !$this->is_ignored($this->get_abs_href($path) . $file)) { $content[] = $file; } } @@ -146,20 +134,16 @@ class H5ai { } - public function getHttpCode($absHref) { + public function get_http_code($abs_href) { - if (!is_dir($this->getAbsPath($absHref))) { - return null; + if (!is_dir($this->get_abs_path($abs_href))) { + return 500; } - $absPath = $this->getAbsPath($absHref); + $abs_path = $this->get_abs_path($abs_href); - foreach ($this->index_files as $if) { - if (file_exists($absPath . "/" . $if)) { - if ($if === "index.php") { - $fileheader = file_get_contents($absPath . "/" . $if, false, null, -1, 50); - return stripos($fileheader, H5ai::$H5AI_CONTENT_TYPE) === false ? 200 : "h5ai"; - } + foreach ($this->options["view"]["indexFiles"] as $if) { + if (file_exists($abs_path . "/" . $if)) { return 200; } } @@ -173,14 +157,14 @@ class H5ai { } - public function getGenericJson() { + public function get_generic_json() { - $entries = $this->getEntries($this->absHref, 1); + $entries = $this->get_entries($this->abs_href, 1); $header = $this->options["custom"]["header"]; $footer = $this->options["custom"]["footer"]; - $header = $this->fileExists($header ? $this->absPath . "/" . $header : null) ? $header : null; - $footer = $this->fileExists($footer ? $this->absPath . "/" . $footer : null) ? $footer : null; + $header = $this->fileExists($header ? $this->abs_path . "/" . $header : null) ? $header : null; + $footer = $this->fileExists($footer ? $this->abs_path . "/" . $footer : null) ? $footer : null; $json = array( "id" => "php", @@ -195,24 +179,26 @@ class H5ai { } - public function getCustomConfig() { + public function get_custom_config() { - $config = "_h5ai.config.json"; - $config = $this->fileExists($config ? $this->absPath . "/" . $config : "ignore") ? $config : "ignore"; + $config = App::$FILE_PREFIX . ".config.json"; + $config = $this->fileExists($config ? $this->abs_path . "/" . $config : "ignore") ? $config : "ignore"; return $config; } - public function getEntries($absHref, $content) { + public function get_entries($abs_href, $what) { - $folder = Entry::get($this, $this->getAbsPath($absHref), $absHref); - if ($content > 1 && $folder !== null) { + $folder = Entry::get($this, $this->get_abs_path($abs_href), $abs_href); + + if ($what > 1 && $folder !== null) { foreach ($folder->getContent() as $entry) { $entry->getContent(); } $folder = $folder->getParent(); } - while ($content > 0 && $folder !== null) { + + while ($what > 0 && $folder !== null) { $folder->getContent(); $folder = $folder->getParent(); } @@ -227,11 +213,11 @@ class H5ai { } - public function getNoJsFallback() { + public function get_no_js_fallback() { - date_default_timezone_set ("UTC"); + date_default_timezone_set("UTC"); - function _cmp($entry1, $entry2) { + function _cmp_no_js_fallback($entry1, $entry2) { if ($entry1->isFolder && !$entry2->isFolder) { return -1; @@ -243,9 +229,9 @@ class H5ai { return strcasecmp($entry1->absHref, $entry2->absHref); } - $folder = Entry::get($this, $this->absPath, $this->absHref); + $folder = Entry::get($this, $this->abs_path, $this->abs_href); $entries = $folder->getContent(); - uasort($entries, "_cmp"); + uasort($entries, "_cmp_no_js_fallback"); $html = ""; $html .= ""; diff --git a/src/_h5ai/server/php/inc/Archive.php b/src/_h5ai/server/php/inc/Archive.php index ea766102..bdc1acb4 100644 --- a/src/_h5ai/server/php/inc/Archive.php +++ b/src/_h5ai/server/php/inc/Archive.php @@ -29,7 +29,7 @@ class Archive { return 404; } - $target = $this->h5ai->getCacheAbsPath() . "/package-" . sha1(microtime(true) . rand()) . "." . $format; + $target = $this->h5ai->get_cache_abs_path() . "/package-" . sha1(microtime(true) . rand()) . "." . $format; try { if ($execution === "shell") { @@ -41,7 +41,7 @@ class Archive { } else { return null; } - $cmd = str_replace("[ROOTDIR]", "\"" . $this->h5ai->getRootAbsPath() . "\"", $cmd); + $cmd = str_replace("[ROOTDIR]", "\"" . $this->h5ai->get_root_abs_path() . "\"", $cmd); $cmd = str_replace("[TARGET]", "\"" . $target . "\"", $cmd); $cmd = str_replace("[DIRS]", count($this->dirs) ? "\"" . implode("\" \"", array_values($this->dirs)) . "\"" : "", $cmd); $cmd = str_replace("[FILES]", count($this->files) ? "\"" . implode("\" \"", array_values($this->files)) . "\"" : "", $cmd); @@ -74,15 +74,15 @@ class Archive { $d = normalize_path(dirname($href), true); $n = basename($href); - $code = $this->h5ai->getHttpCode($d); + $code = $this->h5ai->get_http_code($d); if ($code == 401) { $this->sc401 = true; } if ($code == "h5ai" && !$this->h5ai->is_ignored($n)) { - $realFile = $this->h5ai->getAbsPath($href); - $archivedFile = preg_replace("!^" . normalize_path($this->h5ai->getRootAbsPath(), true) . "!", "", $realFile); + $realFile = $this->h5ai->get_abs_path($href); + $archivedFile = preg_replace("!^" . normalize_path($this->h5ai->get_root_abs_path(), true) . "!", "", $realFile); if (is_dir($realFile)) { $this->add_dir($realFile, $archivedFile); @@ -104,7 +104,7 @@ class Archive { private function add_dir($realDir, $archivedDir) { - $code = $this->h5ai->getHttpCode($this->h5ai->getAbsHref($realDir)); + $code = $this->h5ai->get_http_code($this->h5ai->get_abs_href($realDir)); if ($code == 401) { $this->sc401 = true; } diff --git a/src/_h5ai/server/php/inc/Entry.php b/src/_h5ai/server/php/inc/Entry.php index 64d3b4b4..5c50ec4a 100644 --- a/src/_h5ai/server/php/inc/Entry.php +++ b/src/_h5ai/server/php/inc/Entry.php @@ -16,8 +16,8 @@ class Entry { public static function get($h5ai, $absPath, $absHref) { - if (!starts_with($absHref, $h5ai->getRootAbsHref())) { - error_log("ILLEGAL REQUEST: " . $absHref . ", " . $absPath . ", " . $h5ai->getRootAbsHref()); + if (!starts_with($absHref, $h5ai->get_root_abs_href())) { + error_log("ILLEGAL REQUEST: " . $absHref . ", " . $absPath . ", " . $h5ai->get_root_abs_href()); return null; } @@ -58,7 +58,7 @@ class Entry { if ($this->isFolder) { $this->size = null; - $options = $h5ai->getOptions(); + $options = $h5ai->get_options(); if ($options["foldersize"]["enabled"]) { $cmd = str_replace("[DIR]", $this->absPath, Entry::$FOLDER_SIZE_CMD); $this->size = intval(preg_replace("/\s.*$/", "", `$cmd`), 10); @@ -69,7 +69,7 @@ class Entry { $this->parent = null; $parentAbsHref = normalize_path(dirname($this->absHref), true); - if ($this->absHref !== "/" && starts_with($parentAbsHref, $h5ai->getRootAbsHref())) { + if ($this->absHref !== "/" && starts_with($parentAbsHref, $h5ai->get_root_abs_href())) { $this->parent = Entry::get($this->h5ai, normalize_path(dirname($this->absPath)), $parentAbsHref); } @@ -88,7 +88,7 @@ class Entry { ); if ($withStatus && $this->isFolder) { - $obj["status"] = $this->h5ai->getHttpCode($this->absHref); + $obj["status"] = $this->h5ai->get_http_code($this->absHref); $obj["content"] = $this->isContentFetched; } @@ -106,7 +106,7 @@ class Entry { $content = array(); - if ($this->h5ai->getHttpCode($this->absHref) !== "h5ai") { + if ($this->h5ai->get_http_code($this->absHref) !== "h5ai") { return $content; } diff --git a/src/_h5ai/server/php/inc/Thumb.php b/src/_h5ai/server/php/inc/Thumb.php index 8f1ba24f..f471f18f 100644 --- a/src/_h5ai/server/php/inc/Thumb.php +++ b/src/_h5ai/server/php/inc/Thumb.php @@ -27,7 +27,7 @@ class Thumb { public function thumb($type, $sourceAbsHref, $mode, $width, $height) { - $sourceAbsPath = $this->h5ai->getAbsPath($sourceAbsHref); + $sourceAbsPath = $this->h5ai->get_abs_path($sourceAbsHref); if ($type === "img") { $captureAbsPath = $sourceAbsPath; @@ -48,8 +48,8 @@ class Thumb { } $name = "thumb-" . sha1("$sourceAbsPath-$width-$height-$mode") . ".jpg"; - $thumbAbsPath = $this->h5ai->getCacheAbsPath() . "/" . $name; - $thumbAbsHref = $this->h5ai->getCacheAbsHref() . $name; + $thumbAbsPath = $this->h5ai->get_cache_abs_path() . "/" . $name; + $thumbAbsHref = $this->h5ai->get_cache_abs_href() . $name; if (!file_exists($thumbAbsPath) || filemtime($sourceAbsPath) >= filemtime($thumbAbsPath)) { $image = new Image(); @@ -68,7 +68,7 @@ class Thumb { return null; } - $captureAbsPath = $this->h5ai->getCacheAbsPath() . "/capture-" . sha1($sourceAbsPath) . ".jpg"; + $captureAbsPath = $this->h5ai->get_cache_abs_path() . "/capture-" . sha1($sourceAbsPath) . ".jpg"; if (!file_exists($captureAbsPath) || filemtime($sourceAbsPath) >= filemtime($captureAbsPath)) { $cmd = str_replace("[SOURCE]", $sourceAbsPath, $cmd); diff --git a/src/_h5ai/server/php/inc/init.php b/src/_h5ai/server/php/inc/init.php index 9b11d170..4f989b25 100644 --- a/src/_h5ai/server/php/inc/init.php +++ b/src/_h5ai/server/php/inc/init.php @@ -8,6 +8,7 @@ function normalize_path($path, $trailing_slash = false) { define("APP_ABS_PATH", normalize_path(dirname(dirname(dirname(dirname(__FILE__)))))); define("APP_ABS_HREF", normalize_path(dirname(dirname(dirname(getenv("SCRIPT_NAME")))), true)); +define("ABS_HREF", normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true)); function normalized_require_once($lib) { @@ -17,8 +18,7 @@ function normalized_require_once($lib) { normalized_require_once("/server/php/inc/util.php"); normalized_require_once("/server/php/inc/App.php"); normalized_require_once("/server/php/inc/Entry.php"); -normalized_require_once("/conf/config.php"); -$APP = new H5ai(APP_ABS_PATH, APP_ABS_HREF); +$APP = new App(APP_ABS_PATH, APP_ABS_HREF, ABS_HREF); ?> \ No newline at end of file diff --git a/src/_h5ai/server/php/index.php.jade b/src/_h5ai/server/php/index.php.jade index 663c1d3b..e8166069 100644 --- a/src/_h5ai/server/php/index.php.jade +++ b/src/_h5ai/server/php/index.php.jade @@ -1,15 +1,14 @@ |getH5aiAbsHref(); -| $isHeadRequest = stripos($_SERVER["REQUEST_METHOD"], "HEAD"); +| $app_abs_href = $APP->get_app_abs_href(); +| $is_head_request = stripos($_SERVER["REQUEST_METHOD"], "HEAD"); |?> -- var appHref = "client" -- var json = "getGenericJson(); }?>" -- var fallback = "getNoJsFallback(); }?>" -- var config = "getCustomConfig(); }?>" +- var appHref = "client" +- var json = "get_generic_json(); }?>" +- var fallback = "get_no_js_fallback(); }?>" +- var config = "get_custom_config(); }?>" doctype 5 //if lt IE 9
NameLast modifiedSize