From d5a1a829e37b5d2674f8f97be54cc51d543cc357 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Wed, 6 May 2015 18:58:07 +0200 Subject: [PATCH] Refactor API. --- src/_h5ai/server/php/inc/class-api.php | 12 +++++----- src/_h5ai/server/php/inc/class-app.php | 3 +-- src/_h5ai/server/php/inc/class-search.php | 10 +++------ src/_h5ai/server/php/inc/class-util.php | 27 ++++++++++++++++++++++- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/_h5ai/server/php/inc/class-api.php b/src/_h5ai/server/php/inc/class-api.php index 9c325ea0..ea3a907a 100644 --- a/src/_h5ai/server/php/inc/class-api.php +++ b/src/_h5ai/server/php/inc/class-api.php @@ -59,24 +59,24 @@ class Api { 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; + $what = Util::query_numeric_request_param("items.what"); + $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_array_request_param("l10n"); - $iso_codes = Util::query_request_param("l10n"); $iso_codes = array_filter($iso_codes); $response["l10n"] = $this->app->get_l10n($iso_codes); } @@ -84,9 +84,9 @@ class Api { 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); } @@ -95,8 +95,8 @@ class Api { 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_array_request_param("thumbs"); - $thumbs = Util::query_request_param("thumbs"); $response["thumbs"] = $this->app->get_thumbs($thumbs); } diff --git a/src/_h5ai/server/php/inc/class-app.php b/src/_h5ai/server/php/inc/class-app.php index fd0b3a1d..8292f237 100644 --- a/src/_h5ai/server/php/inc/class-app.php +++ b/src/_h5ai/server/php/inc/class-app.php @@ -2,7 +2,6 @@ class App { - private static $RE_DELIMITER = "|"; private static $ICON_EXTS = ["svg", "png", "jpg"]; private static $CUSTOM_EXTS = ["html", "md"]; @@ -131,7 +130,7 @@ class App { } foreach ($this->get_option("view.hidden", []) as $re) { - $re = App::$RE_DELIMITER . str_replace(App::$RE_DELIMITER, '\\' . App::$RE_DELIMITER, $re) . App::$RE_DELIMITER; + $re = Util::wrap_pattern($re); if (preg_match($re, $name)) { return true; } diff --git a/src/_h5ai/server/php/inc/class-search.php b/src/_h5ai/server/php/inc/class-search.php index 9fe52b22..afd61086 100644 --- a/src/_h5ai/server/php/inc/class-search.php +++ b/src/_h5ai/server/php/inc/class-search.php @@ -10,11 +10,12 @@ class Search { function get_paths($root, $pattern = null) { $paths = []; - if ($this->app->is_managed_path($root)) { + if ($pattern && $this->app->is_managed_path($root)) { + $re = Util::wrap_pattern($pattern); $names = $this->app->read_dir($root); foreach ($names as $name) { $path = $root . "/" . $name; - if ($pattern && $this->matches($path, $pattern)) { + if (preg_match($re, @basename($path))) { $paths[] = $path; } if (@is_dir($path)) { @@ -36,9 +37,4 @@ class Search { }, $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 f21d36ea..89b2705e 100644 --- a/src/_h5ai/server/php/inc/class-util.php +++ b/src/_h5ai/server/php/inc/class-util.php @@ -4,10 +4,12 @@ class Util { const ERR_MISSING_PARAM = "ERR_MISSING_PARAM"; + const ERR_ILLIGAL_PARAM = "ERR_ILLIGAL_PARAM"; const ERR_FAILED = "ERR_FAILED"; const ERR_DISABLED = "ERR_DISABLED"; const ERR_UNSUPPORTED = "ERR_UNSUPPORTED"; const NO_DEFAULT = "NO_*@+#?!_DEFAULT"; + const RE_DELIMITER = "|"; public static function normalize_path($path, $trailing_slash = false) { @@ -70,7 +72,24 @@ class Util { public static function query_boolean_request_param($keypath = "", $default = Util::NO_DEFAULT) { - return filter_var(Util::query_request_param($keypath, $default), FILTER_VALIDATE_BOOLEAN); + $value = Util::query_request_param($keypath, $default); + return filter_var($value, FILTER_VALIDATE_BOOLEAN); + } + + + public static function query_numeric_request_param($keypath = "", $default = Util::NO_DEFAULT) { + + $value = Util::query_request_param($keypath, $default); + Util::json_fail(Util::ERR_ILLIGAL_PARAM, "parameter '$keypath' is not numeric", !is_numeric($value)); + return intval($value, 10); + } + + + public static function query_array_request_param($keypath = "", $default = Util::NO_DEFAULT) { + + $value = Util::query_request_param($keypath, $default); + Util::json_fail(Util::ERR_ILLIGAL_PARAM, "parameter '$keypath' is no array", !is_array($value)); + return $value; } @@ -86,6 +105,12 @@ class Util { } + public static function wrap_pattern($pattern) { + + return Util::RE_DELIMITER . str_replace(Util::RE_DELIMITER, '\\' . Util::RE_DELIMITER, $pattern) . Util::RE_DELIMITER; + } + + public static function load_commented_json($path) { if (!file_exists($path)) {