mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-08-08 06:46:36 +02:00
Refactor API, add search.
This commit is contained in:
@@ -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]]);
|
||||
}
|
||||
}
|
||||
|
@@ -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");
|
||||
|
||||
|
44
src/_h5ai/server/php/inc/class-search.php
Normal file
44
src/_h5ai/server/php/inc/class-search.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
class Search {
|
||||
|
||||
function __construct($app) {
|
||||
|
||||
$this->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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user