From 5c72378b39024e54a36152a68852e8533f8afb5b Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Wed, 6 May 2015 17:18:08 +0200 Subject: [PATCH] Refactor API, add search. --- src/_h5ai/server/php/inc/class-api.php | 151 ++++++++++--------- src/_h5ai/server/php/inc/class-bootstrap.php | 2 +- src/_h5ai/server/php/inc/class-search.php | 44 ++++++ src/_h5ai/server/php/inc/class-util.php | 17 ++- 4 files changed, 137 insertions(+), 77 deletions(-) create mode 100644 src/_h5ai/server/php/inc/class-search.php diff --git a/src/_h5ai/server/php/inc/class-api.php b/src/_h5ai/server/php/inc/class-api.php index a69fa32d..9c325ea0 100644 --- a/src/_h5ai/server/php/inc/class-api.php +++ b/src/_h5ai/server/php/inc/class-api.php @@ -13,8 +13,8 @@ class Api { public function apply() { - $action = Util::get_request_param("action"); - $supported = ["login", "logout", "get", "download"]; + $action = Util::query_request_param("action"); + $supported = ["download", "get", "login", "logout"]; Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported)); $methodname = "on_${action}"; @@ -22,76 +22,14 @@ class Api { } - private function on_login() { - - $pass = Util::get_request_param("pass"); - $_SESSION[AS_ADMIN_SESSION_KEY] = strcasecmp(hash("sha512", $pass), PASSHASH) === 0; - Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]); - } - - - private function on_logout() { - - $_SESSION[AS_ADMIN_SESSION_KEY] = false; - Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]); - } - - - private function on_get() { - - $response = []; - - foreach (["setup", "options", "types", "theme", "langs"] as $name) { - if (Util::get_boolean_request_param($name, false)) { - - $methodname = "get_${name}"; - $response[$name] = $this->app->$methodname(); - } - } - - if (Util::get_request_param("l10n", false)) { - - $iso_codes = Util::get_request_param("l10n"); - $iso_codes = array_filter($iso_codes); - $response["l10n"] = $this->app->get_l10n($iso_codes); - } - - if (Util::get_request_param("custom", false)) { - - $href = Util::get_request_param("custom"); - $response["custom"] = $this->app->get_customizations($href); - } - - if (Util::get_request_param("items", false)) { - - $items = Util::get_request_param("items"); - $href = $items["href"]; - $what = $items["what"]; - $what = is_numeric($what) ? intval($what, 10) : 1; - $response["items"] = $this->app->get_items($href, $what); - } - - if (Util::get_request_param("thumbs", false)) { - - Util::json_fail(Util::ERR_DISABLED, "thumbnails disabled", !$this->app->get_option("thumbnails.enabled", false)); - Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !HAS_PHP_JPEG); - - $thumbs = Util::get_request_param("thumbs"); - $response["thumbs"] = $this->app->get_thumbs($thumbs); - } - - Util::json_exit($response); - } - - private function on_download() { - Util::json_fail(Util::ERR_DISABLED, "downloads disabled", !$this->app->get_option("download.enabled", false)); + Util::json_fail(Util::ERR_DISABLED, "download disabled", !$this->app->get_option("download.enabled", false)); - $as = Util::get_request_param("as"); - $type = Util::get_request_param("type"); - $base_href = Util::get_request_param("baseHref"); - $hrefs = Util::get_request_param("hrefs"); + $as = Util::query_request_param("as"); + $type = Util::query_request_param("type"); + $base_href = Util::query_request_param("baseHref"); + $hrefs = Util::query_request_param("hrefs"); $archive = new Archive($this->app); @@ -104,4 +42,79 @@ class Api { Util::json_fail(Util::ERR_FAILED, "packaging failed", $rc !== 0); exit; } + + + private function on_get() { + + $response = []; + + foreach (["langs", "options", "setup", "theme", "types"] as $name) { + if (Util::query_boolean_request_param($name, false)) { + + $methodname = "get_${name}"; + $response[$name] = $this->app->$methodname(); + } + } + + if (Util::query_request_param("items", false)) { + + $href = Util::query_request_param("items.href"); + $what = Util::query_request_param("items.what"); + $what = is_numeric($what) ? intval($what, 10) : 1; + $response["items"] = $this->app->get_items($href, $what); + } + + if (Util::query_request_param("custom", false)) { + + Util::json_fail(Util::ERR_DISABLED, "custom disabled", !$this->app->get_option("custom.enabled", false)); + + $href = Util::query_request_param("custom"); + $response["custom"] = $this->app->get_customizations($href); + } + + if (Util::query_request_param("l10n", false)) { + + Util::json_fail(Util::ERR_DISABLED, "l10n disabled", !$this->app->get_option("l10n.enabled", false)); + + $iso_codes = Util::query_request_param("l10n"); + $iso_codes = array_filter($iso_codes); + $response["l10n"] = $this->app->get_l10n($iso_codes); + } + + if (Util::query_request_param("search", false)) { + + Util::json_fail(Util::ERR_DISABLED, "search disabled", !$this->app->get_option("search.enabled", false)); + + $href = Util::query_request_param("search.href"); + $pattern = Util::query_request_param("search.pattern"); + $search = new Search($this->app); + $response["search"] = $search->get_items($href, $pattern); + } + + if (Util::query_request_param("thumbs", false)) { + + Util::json_fail(Util::ERR_DISABLED, "thumbnails disabled", !$this->app->get_option("thumbnails.enabled", false)); + Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !HAS_PHP_JPEG); + + $thumbs = Util::query_request_param("thumbs"); + $response["thumbs"] = $this->app->get_thumbs($thumbs); + } + + Util::json_exit($response); + } + + + private function on_login() { + + $pass = Util::query_request_param("pass"); + $_SESSION[AS_ADMIN_SESSION_KEY] = strcasecmp(hash("sha512", $pass), PASSHASH) === 0; + Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]); + } + + + private function on_logout() { + + $_SESSION[AS_ADMIN_SESSION_KEY] = false; + Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]); + } } diff --git a/src/_h5ai/server/php/inc/class-bootstrap.php b/src/_h5ai/server/php/inc/class-bootstrap.php index 9122b852..7135e17d 100644 --- a/src/_h5ai/server/php/inc/class-bootstrap.php +++ b/src/_h5ai/server/php/inc/class-bootstrap.php @@ -107,7 +107,7 @@ class Bootstrap { define("CMDS_PATH", Util::normalize_path(CACHE_PATH . "/cmds.json", false)); $cmds = Util::load_commented_json(CMDS_PATH); - if (sizeof($cmds) === 0 || Util::get_boolean_request_param("updatecmds", false)) { + if (sizeof($cmds) === 0 || Util::query_boolean_request_param("updatecmds", false)) { $cmds["command"] = Util::exec_0("command -v command"); $cmds["which"] = Util::exec_0("which which"); diff --git a/src/_h5ai/server/php/inc/class-search.php b/src/_h5ai/server/php/inc/class-search.php new file mode 100644 index 00000000..9fe52b22 --- /dev/null +++ b/src/_h5ai/server/php/inc/class-search.php @@ -0,0 +1,44 @@ +app = $app; + } + + function get_paths($root, $pattern = null) { + + $paths = []; + if ($this->app->is_managed_path($root)) { + $names = $this->app->read_dir($root); + foreach ($names as $name) { + $path = $root . "/" . $name; + if ($pattern && $this->matches($path, $pattern)) { + $paths[] = $path; + } + if (@is_dir($path)) { + $paths = array_merge($paths, $this->get_paths($path, $pattern)); + } + } + } + return $paths; + } + + function get_items($href, $pattern = null) { + + $cache = []; + $root = $this->app->to_path($href); + $paths = $this->get_paths($root, $pattern); + $items = array_map(function ($path) { + + return Item::get($this->app, $path, $cache)->to_json_object(); + }, $paths); + return $items; + } + + function matches($path, $pattern) { + + return preg_match($pattern, basename($path)) === 1; + } +} diff --git a/src/_h5ai/server/php/inc/class-util.php b/src/_h5ai/server/php/inc/class-util.php index d91a4e57..f21d36ea 100644 --- a/src/_h5ai/server/php/inc/class-util.php +++ b/src/_h5ai/server/php/inc/class-util.php @@ -7,6 +7,7 @@ class Util { const ERR_FAILED = "ERR_FAILED"; const ERR_DISABLED = "ERR_DISABLED"; const ERR_UNSUPPORTED = "ERR_UNSUPPORTED"; + const NO_DEFAULT = "NO_*@+#?!_DEFAULT"; public static function normalize_path($path, $trailing_slash = false) { @@ -32,7 +33,7 @@ class Util { } - public static function array_query($array, $keypath = "", $default = null) { + public static function array_query($array, $keypath = "", $default = Util::NO_DEFAULT) { $value = $array; @@ -54,20 +55,22 @@ class Util { } - public static function get_request_param($key, $default = null) { + public static function query_request_param($keypath = "", $default = Util::NO_DEFAULT) { - if (!array_key_exists($key, $_POST)) { - Util::json_fail(Util::ERR_MISSING_PARAM, "parameter '$key' is missing", $default === null); + $value = Util::array_query($_POST, $keypath, Util::NO_DEFAULT); + + if ($value === Util::NO_DEFAULT) { + Util::json_fail(Util::ERR_MISSING_PARAM, "parameter '$keypath' is missing", $default === Util::NO_DEFAULT); return $default; } - return $_POST[$key]; + return $value; } - public static function get_boolean_request_param($key, $default = null) { + public static function query_boolean_request_param($keypath = "", $default = Util::NO_DEFAULT) { - return filter_var(Util::get_request_param($key, $default), FILTER_VALIDATE_BOOLEAN); + return filter_var(Util::query_request_param($keypath, $default), FILTER_VALIDATE_BOOLEAN); }