diff --git a/src/_h5ai/client/js/inc/ext/preview-txt.js b/src/_h5ai/client/js/inc/ext/preview-txt.js index 4cdfe13b..b9ffdb5c 100644 --- a/src/_h5ai/client/js/inc/ext/preview-txt.js +++ b/src/_h5ai/client/js/inc/ext/preview-txt.js @@ -69,11 +69,11 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/resource', }, loadSyntaxhighlighter = function (callback) { - loadScript(allsettings.h5aiAbsHref + 'js/syntaxhighlighter.js', 'SyntaxHighlighter', callback); + loadScript(allsettings.h5aiAbsHref + 'client/js/syntaxhighlighter.js', 'SyntaxHighlighter', callback); }, loadMarkdown = function (callback) { - loadScript(allsettings.h5aiAbsHref + 'js/markdown.js', 'markdown', callback); + loadScript(allsettings.h5aiAbsHref + 'client/js/markdown.js', 'markdown', callback); }, adjustSize = function () { diff --git a/src/_h5ai/server/php/api.php b/src/_h5ai/server/php/api.php index f2b0465d..f547d4df 100644 --- a/src/_h5ai/server/php/api.php +++ b/src/_h5ai/server/php/api.php @@ -1,41 +1,11 @@ getOptions(); -function json_exit($obj) { - - $obj["code"] = 0; - echo json_encode($obj); - exit; -} - -function json_fail($code, $msg = "", $cond = true) { - - if ($cond) { - echo json_encode(array("code" => $code, "msg" => $msg)); - exit; - } -} - -function check_keys($keys) { - $values = array(); - foreach ($keys as $key) { - json_fail(101, "parameter '$key' is missing", !array_key_exists($key, $_REQUEST)); - $values[] = $_REQUEST[$key]; - } - return $values; -} - -function delete_tempfile($file) { - @unlink($file); -} - -list($action) = check_keys(array("action")); +list($action) = use_request_params(array("action")); if ($action === "getthumbsrc") { @@ -44,12 +14,12 @@ if ($action === "getthumbsrc") { json_fail(1, "thumbnails disabled"); } - H5ai::req_once("/server/php/inc/Thumb.php"); + normalized_require_once("/server/php/inc/Thumb.php"); if (!Thumb::is_supported()) { json_fail(2, "thumbnails not supported"); } - list($type, $srcAbsHref, $mode, $width, $height) = check_keys(array("type", "href", "mode", "width", "height")); + list($type, $srcAbsHref, $mode, $width, $height) = use_request_params(array("type", "href", "mode", "width", "height")); $thumb = new Thumb($h5ai); $thumbHref = $thumb->thumb($type, $srcAbsHref, $mode, $width, $height); @@ -65,9 +35,9 @@ else if ($action === "archive") { json_fail(1, "downloads disabled", !$options["download"]["enabled"]); - list($execution, $format, $hrefs) = check_keys(array("execution", "format", "hrefs")); + list($execution, $format, $hrefs) = use_request_params(array("execution", "format", "hrefs")); - H5ai::req_once("/server/php/inc/Archive.php"); + normalized_require_once("/server/php/inc/Archive.php"); $archive = new Archive($h5ai); $hrefs = explode(":", trim($hrefs)); @@ -85,7 +55,7 @@ else if ($action === "getarchive") { json_fail(1, "downloads disabled", !$options["download"]["enabled"]); - list($id, $as) = check_keys(array("id", "as")); + list($id, $as) = use_request_params(array("id", "as")); json_fail(2, "file not found", !preg_match("/^package-/", $id)); $target = $h5ai->getCacheAbsPath() . "/" . $id; @@ -132,7 +102,7 @@ else if ($action === "getchecks") { else if ($action === "getentries") { - list($href, $content) = check_keys(array("href", "content")); + list($href, $content) = use_request_params(array("href", "content")); $content = intval($content, 10); @@ -142,7 +112,7 @@ else if ($action === "getentries") { else if ($action === "upload") { - list($href) = check_keys(array("href")); + list($href) = use_request_params(array("href")); json_fail(1, "wrong HTTP method", strtolower($_SERVER["REQUEST_METHOD"]) !== "post"); json_fail(2, "something went wrong", !array_key_exists("userfile", $_FILES)); @@ -170,14 +140,14 @@ else if ($action === "delete") { json_fail(1, "deletion disabled", !$options["delete"]["enabled"]); - list($hrefs) = check_keys(array("hrefs")); + list($hrefs) = use_request_params(array("hrefs")); $hrefs = explode(":", trim($hrefs)); $errors = array(); foreach ($hrefs as $href) { - $d = H5ai::normalize_path(dirname($href), true); + $d = normalize_path(dirname($href), true); $n = basename($href); $code = $h5ai->getHttpCode($d); @@ -206,9 +176,9 @@ else if ($action === "rename") { json_fail(1, "renaming disabled", !$options["rename"]["enabled"]); - list($href, $name) = check_keys(array("href", "name")); + list($href, $name) = use_request_params(array("href", "name")); - $d = H5ai::normalize_path(dirname($href), true); + $d = normalize_path(dirname($href), true); $n = basename($href); $code = $h5ai->getHttpCode($d); @@ -218,7 +188,7 @@ else if ($action === "rename") { if ($code == "h5ai" && !$h5ai->is_ignored($n)) { $absPath = $h5ai->getAbsPath($href); - $folder = H5ai::normalize_path(dirname($absPath)); + $folder = normalize_path(dirname($absPath)); if (!rename($absPath, $folder . "/" . $name)) { json_fail(2, "renaming failed"); diff --git a/src/_h5ai/server/php/inc/H5ai.php b/src/_h5ai/server/php/inc/App.php similarity index 74% rename from src/_h5ai/server/php/inc/H5ai.php rename to src/_h5ai/server/php/inc/App.php index bff79d80..dea8cb22 100644 --- a/src/_h5ai/server/php/inc/H5ai.php +++ b/src/_h5ai/server/php/inc/App.php @@ -1,75 +1,27 @@ $value) { - $result[$key] = array_merge($result[$key], $b[$key]); - } - - return $result; - } - - private static $H5AI_CONTENT_TYPE = "Content-Type: text/html;h5ai="; + private $h5aiAbsPath, $rootAbsPath, + $h5aiAbsHref, $rootAbsHref, + $absHref, $absPath, + $ignore_names, $ignore_patterns, $index_files, + $config, $options; - private $requested_from, - $h5aiAbsPath, - $rootAbsPath, $ignore_names, $ignore_patterns, $index_files, - $config, $options, - $rootAbsHref, $h5aiAbsHref, - $absHref, $absPath; + public function __construct($app_abs_path, $app_abs_href) { + $this->h5aiAbsPath = normalize_path($app_abs_path); + $this->rootAbsPath = normalize_path(dirname($app_abs_path)); - public function __construct($requested_from) { + $this->h5aiAbsHref = normalize_path($app_abs_href, true); + $this->rootAbsHref = normalize_path(dirname($app_abs_href), true); - $this->requested_from = H5ai::normalize_path($requested_from); - - $this->h5aiAbsPath = H5ai::normalize_path(H5AI_ABS_PATH); - $this->rootAbsPath = H5ai::normalize_path(dirname(H5AI_ABS_PATH)); + $this->absHref = normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true); + $this->absPath = $this->getAbsPath($this->absHref); global $H5AI_CONFIG; $this->ignore_names = $H5AI_CONFIG["IGNORE"]; @@ -77,16 +29,10 @@ class H5ai { $this->index_files = $H5AI_CONFIG["INDEX_FILES"]; $this->config = array("options" => array(), "types" => array(), "langs" => array()); - $this->config = H5ai::merge_config($this->config, H5ai::load_config($this->h5aiAbsPath . "/conf/config.json")); + $this->config = merge_config($this->config, load_commented_json($this->h5aiAbsPath . "/conf/config.json")); $this->options = $this->config["options"]; - $this->h5aiAbsHref = H5ai::normalize_path($this->options["h5aiAbsHref"], true); - $this->rootAbsHref = H5ai::normalize_path(dirname($this->options["h5aiAbsHref"]), true); - - $this->absHref = H5ai::normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true); - $this->absPath = $this->getAbsPath($this->absHref); - - $this->config = H5ai::merge_config($this->config, H5ai::load_config($this->absPath . "/_h5ai.config.json")); + $this->config = merge_config($this->config, load_commented_json($this->absPath . "/_h5ai.config.json")); $this->options = $this->config["options"]; } @@ -147,7 +93,7 @@ class H5ai { $encodedParts[] = rawurlencode($part); } - return H5ai::normalize_path($this->rootAbsHref . implode("/", $encodedParts), $endWithSlash); + return normalize_path($this->rootAbsHref . implode("/", $encodedParts), $endWithSlash); } @@ -159,7 +105,7 @@ class H5ai { $absHref = substr($absHref, strlen($this->rootAbsHref)); - return H5ai::normalize_path($this->rootAbsPath . "/" . rawurldecode($absHref)); + return normalize_path($this->rootAbsPath . "/" . rawurldecode($absHref)); } @@ -285,7 +231,7 @@ class H5ai { $footer = $this->fileExists($footer ? $this->absPath . "/" . $footer : null) ? $footer : null; $json = array( - "id" => $this->requested_from === $this->h5aiAbsPath . "/server/php/index.php" ? "php" : "idx.php", + "id" => "php", "serverName" => strtolower(preg_replace("/\\/.*$/", "", getenv("SERVER_SOFTWARE"))), "serverVersion" => strtolower(preg_replace("/^.*\\//", "", preg_replace("/\\s.*$/", "", getenv("SERVER_SOFTWARE")))), "customHeader" => $header, diff --git a/src/_h5ai/server/php/inc/Archive.php b/src/_h5ai/server/php/inc/Archive.php index 78947dd2..ea766102 100644 --- a/src/_h5ai/server/php/inc/Archive.php +++ b/src/_h5ai/server/php/inc/Archive.php @@ -71,7 +71,7 @@ class Archive { foreach ($hrefs as $href) { - $d = H5ai::normalize_path(dirname($href), true); + $d = normalize_path(dirname($href), true); $n = basename($href); $code = $this->h5ai->getHttpCode($d); @@ -82,7 +82,7 @@ class Archive { if ($code == "h5ai" && !$this->h5ai->is_ignored($n)) { $realFile = $this->h5ai->getAbsPath($href); - $archivedFile = preg_replace("!^" . H5ai::normalize_path($this->h5ai->getRootAbsPath(), true) . "!", "", $realFile); + $archivedFile = preg_replace("!^" . normalize_path($this->h5ai->getRootAbsPath(), true) . "!", "", $realFile); if (is_dir($realFile)) { $this->add_dir($realFile, $archivedFile); diff --git a/src/_h5ai/server/php/inc/Entry.php b/src/_h5ai/server/php/inc/Entry.php index eafdcfb1..64d3b4b4 100644 --- a/src/_h5ai/server/php/inc/Entry.php +++ b/src/_h5ai/server/php/inc/Entry.php @@ -8,12 +8,6 @@ class Entry { private static $cache = array(); - private static function starts_with($sequence, $part) { - - return (substr($sequence, 0, strlen($part)) === $part); - } - - public static function get_cache() { return Entry::$cache; @@ -22,7 +16,7 @@ class Entry { public static function get($h5ai, $absPath, $absHref) { - if (!Entry::starts_with($absHref, $h5ai->getRootAbsHref())) { + if (!starts_with($absHref, $h5ai->getRootAbsHref())) { error_log("ILLEGAL REQUEST: " . $absHref . ", " . $absPath . ", " . $h5ai->getRootAbsHref()); return null; } @@ -55,10 +49,10 @@ class Entry { $this->h5ai = $h5ai; - $this->absPath = H5ai::normalize_path($absPath); + $this->absPath = normalize_path($absPath); $this->isFolder = is_dir($this->absPath); - $this->absHref = H5ai::normalize_path($absHref, $this->isFolder); + $this->absHref = normalize_path($absHref, $this->isFolder); $this->date = filemtime($this->absPath); @@ -74,9 +68,9 @@ class Entry { } $this->parent = null; - $parentAbsHref = H5ai::normalize_path(dirname($this->absHref), true); - if ($this->absHref !== "/" && Entry::starts_with($parentAbsHref, $h5ai->getRootAbsHref())) { - $this->parent = Entry::get($this->h5ai, H5ai::normalize_path(dirname($this->absPath)), $parentAbsHref); + $parentAbsHref = normalize_path(dirname($this->absHref), true); + if ($this->absHref !== "/" && starts_with($parentAbsHref, $h5ai->getRootAbsHref())) { + $this->parent = Entry::get($this->h5ai, normalize_path(dirname($this->absPath)), $parentAbsHref); } $this->isContentFetched = false; diff --git a/src/_h5ai/server/php/inc/init.php b/src/_h5ai/server/php/inc/init.php new file mode 100644 index 00000000..146326d1 --- /dev/null +++ b/src/_h5ai/server/php/inc/init.php @@ -0,0 +1,24 @@ + \ No newline at end of file diff --git a/src/_h5ai/server/php/inc/util.php b/src/_h5ai/server/php/inc/util.php new file mode 100644 index 00000000..0bf2ae12 --- /dev/null +++ b/src/_h5ai/server/php/inc/util.php @@ -0,0 +1,73 @@ + $code, "msg" => $msg)); + exit; + } +} + +function use_request_params($keys) { + + if (!is_array($keys)) { + $keys = func_get_args(); + } + + $values = array(); + foreach ($keys as $key) { + json_fail(101, "parameter '$key' is missing", !array_key_exists($key, $_REQUEST)); + $values[] = $_REQUEST[$key]; + unset($_REQUEST[$key]); + } + return $values; +} + +function delete_tempfile($file) { + + @unlink($file); +} + +function starts_with($sequence, $head) { + + return substr($sequence, 0, strlen($head)) === $head; +} + +function ends_with($sequence, $tail) { + + return substr($sequence, -strlen($tail)) === $tail; +} + +function load_commented_json($file) { + + if (!file_exists($file)) { + return array(); + } + + $str = file_get_contents($file); + + // remove comments to get pure json + $str = preg_replace("/\/\*.*?\*\/|\/\/.*?(\n|$)/s", "", $str); + + return json_decode($str, true); +} + +function merge_config($a, $b) { + + $result = array_merge(array(), $a); + + foreach ($b as $key => $value) { + $result[$key] = array_merge($result[$key], $b[$key]); + } + + return $result; +} + +?> \ 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 d13120b3..663c1d3b 100644 --- a/src/_h5ai/server/php/index.php.jade +++ b/src/_h5ai/server/php/index.php.jade @@ -1,7 +1,7 @@ |getH5aiAbsHref(); | $isHeadRequest = stripos($_SERVER["REQUEST_METHOD"], "HEAD"); |?>