diff --git a/src/_h5ai/server/php/inc/class-api.php b/src/_h5ai/server/php/inc/class-api.php index 43cf6ee1..ccd987bb 100644 --- a/src/_h5ai/server/php/inc/class-api.php +++ b/src/_h5ai/server/php/inc/class-api.php @@ -48,7 +48,7 @@ class Api { $response = []; - foreach (["langs", "options", "setup", "theme", "types"] as $name) { + foreach (["langs", "options", "setup", "types"] as $name) { if (Util::query_boolean_request_param($name, false)) { $methodname = "get_${name}"; @@ -56,6 +56,12 @@ class Api { } } + if (Util::query_boolean_request_param("theme", false)) { + + $theme = new Theme($this->app); + $response["theme"] = $theme->get_icons(); + } + if (Util::query_request_param("items", false)) { $href = Util::query_request_param("items.href"); @@ -69,7 +75,8 @@ class Api { Util::json_fail(Util::ERR_DISABLED, "custom disabled", !$this->app->query_option("custom.enabled", false)); $href = Util::query_request_param("custom"); - $response["custom"] = $this->app->get_customizations($href); + $custom = new Custom($this->app); + $response["custom"] = $custom->get_customizations($href); } if (Util::query_request_param("l10n", false)) { diff --git a/src/_h5ai/server/php/inc/class-app.php b/src/_h5ai/server/php/inc/class-app.php index 06e80b16..1c881261 100644 --- a/src/_h5ai/server/php/inc/class-app.php +++ b/src/_h5ai/server/php/inc/class-app.php @@ -2,10 +2,6 @@ class App { - private static $ICON_EXTS = ["svg", "png", "jpg"]; - private static $CUSTOM_EXTS = ["html", "md"]; - - private $options; @@ -15,7 +11,7 @@ class App { } - public function query_options() { + public function get_options() { return $this->options; } @@ -77,29 +73,6 @@ class App { } - public function get_theme() { - - $theme = $this->query_option("view.theme", "-NONE-"); - $theme_path = APP_PATH . "/client/images/themes/${theme}"; - - $icons = []; - - if (is_dir($theme_path)) { - if ($dir = opendir($theme_path)) { - while (($name = readdir($dir)) !== false) { - $path_parts = pathinfo($name); - if (in_array(@$path_parts["extension"], App::$ICON_EXTS)) { - $icons[$path_parts["filename"]] = "${theme}/${name}"; - } - } - closedir($dir); - } - } - - return $icons; - } - - public function to_href($path, $trailing_slash = true) { $rel_path = substr($path, strlen(ROOT_PATH)); @@ -228,66 +201,6 @@ class App { } - private function get_current_path() { - - $uri_parts = parse_url(getenv("REQUEST_URI")); - $current_href = Util::normalize_path($uri_parts["path"], true); - $current_path = $this->to_path($current_href); - - if (!is_dir($current_path)) { - $current_path = Util::normalize_path(dirname($current_path), false); - } - - return $current_path; - } - - - public function get_fallback($path = null) { - - if (!$path) { - $path = $this->get_current_path(); - } - - $cache = []; - $folder = Item::get($this, $path, $cache); - $items = $folder->get_content($cache); - uasort($items, ["Item", "cmp"]); - - $html = ""; - - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - - if ($folder->get_parent($cache)) { - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - } - - foreach ($items as $item) { - $type = $item->is_folder ? "folder" : "file"; - - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - $html .= ""; - } - - $html .= "
NameLast modifiedSize
folder-parentParent Directory
" . $type . "" . basename($item->path) . "" . date("Y-m-d H:i", $item->date) . "" . ($item->size !== null ? intval($item->size / 1000) . " KB" : "" ) . "
"; - - return $html; - } - - public function get_langs() { $langs = []; @@ -322,64 +235,6 @@ class App { } - private function read_custom_file($path, $name, &$content, &$type) { - - foreach (APP::$CUSTOM_EXTS as $ext) { - $file = "$path/" . FILE_PREFIX . ".$name.$ext"; - if (is_readable($file)) { - $content = file_get_contents($file); - $type = $ext; - return; - } - } - } - - - public function get_customizations($href) { - - if (!$this->query_option("custom.enabled", false)) { - return [ - "header" => ["content" => null, "type" => null], - "footer" => ["content" => null, "type" => null] - ]; - } - - $path = $this->to_path($href); - - $header = null; - $header_type = null; - $footer = null; - $footer_type = null; - - $this->read_custom_file($path, "header", $header, $header_type); - $this->read_custom_file($path, "footer", $footer, $footer_type); - - while ($header === null || $footer === null) { - - if ($header === null) { - $this->read_custom_file($path, "headers", $header, $header_type); - } - if ($footer === null) { - $this->read_custom_file($path, "footers", $footer, $footer_type); - } - - if ($path === ROOT_PATH) { - break; - } - $parent_path = Util::normalize_path(dirname($path)); - if ($parent_path === $path) { - break; - } - $path = $parent_path; - } - - return [ - "header" => ["content" => $header, "type" => $header_type], - "footer" => ["content" => $footer, "type" => $footer_type] - ]; - } - - public function get_thumbs($requests) { $hrefs = []; diff --git a/src/_h5ai/server/php/inc/class-bootstrap.php b/src/_h5ai/server/php/inc/class-bootstrap.php index 7135e17d..3f27a039 100644 --- a/src/_h5ai/server/php/inc/class-bootstrap.php +++ b/src/_h5ai/server/php/inc/class-bootstrap.php @@ -20,7 +20,8 @@ class Bootstrap { $api = new Api($app); $api->apply(); } else { - define("FALLBACK", $app->get_fallback()); + $fallback = new Fallback($app); + define("FALLBACK", $fallback->get_html()); normalized_require_once("inc/page"); } } diff --git a/src/_h5ai/server/php/inc/class-custom.php b/src/_h5ai/server/php/inc/class-custom.php new file mode 100644 index 00000000..10c74b5f --- /dev/null +++ b/src/_h5ai/server/php/inc/class-custom.php @@ -0,0 +1,67 @@ +app = $app; + } + + private function read_custom_file($path, $name, &$content, &$type) { + + foreach (Custom::$extensions as $ext) { + $file = "$path/" . FILE_PREFIX . ".$name.$ext"; + if (is_readable($file)) { + $content = file_get_contents($file); + $type = $ext; + return; + } + } + } + + public function get_customizations($href) { + + if (!$this->app->query_option("custom.enabled", false)) { + return [ + "header" => ["content" => null, "type" => null], + "footer" => ["content" => null, "type" => null] + ]; + } + + $path = $this->app->to_path($href); + + $header = null; + $header_type = null; + $footer = null; + $footer_type = null; + + $this->read_custom_file($path, "header", $header, $header_type); + $this->read_custom_file($path, "footer", $footer, $footer_type); + + while ($header === null || $footer === null) { + + if ($header === null) { + $this->read_custom_file($path, "headers", $header, $header_type); + } + if ($footer === null) { + $this->read_custom_file($path, "footers", $footer, $footer_type); + } + + if ($path === ROOT_PATH) { + break; + } + $parent_path = Util::normalize_path(dirname($path)); + if ($parent_path === $path) { + break; + } + $path = $parent_path; + } + + return [ + "header" => ["content" => $header, "type" => $header_type], + "footer" => ["content" => $footer, "type" => $footer_type] + ]; + } +} diff --git a/src/_h5ai/server/php/inc/class-fallback.php b/src/_h5ai/server/php/inc/class-fallback.php new file mode 100644 index 00000000..45287194 --- /dev/null +++ b/src/_h5ai/server/php/inc/class-fallback.php @@ -0,0 +1,67 @@ +app = $app; + } + + private function get_current_path() { + + $uri_parts = parse_url(getenv("REQUEST_URI")); + $current_href = Util::normalize_path($uri_parts["path"], true); + $current_path = $this->app->to_path($current_href); + + if (!is_dir($current_path)) { + $current_path = Util::normalize_path(dirname($current_path), false); + } + + return $current_path; + } + + public function get_html($path = null) { + + if (!$path) { + $path = $this->get_current_path(); + } + + $cache = []; + $folder = Item::get($this->app, $path, $cache); + $items = $folder->get_content($cache); + uasort($items, ["Item", "cmp"]); + + $html = ""; + + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + + if ($folder->get_parent($cache)) { + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + } + + foreach ($items as $item) { + $type = $item->is_folder ? "folder" : "file"; + + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + } + + $html .= "
NameLast modifiedSize
folder-parentParent Directory
" . $type . "" . basename($item->path) . "" . date("Y-m-d H:i", $item->date) . "" . ($item->size !== null ? intval($item->size / 1000) . " KB" : "" ) . "
"; + + return $html; + } +} diff --git a/src/_h5ai/server/php/inc/class-theme.php b/src/_h5ai/server/php/inc/class-theme.php new file mode 100644 index 00000000..2e0e102e --- /dev/null +++ b/src/_h5ai/server/php/inc/class-theme.php @@ -0,0 +1,33 @@ +app = $app; + } + + public function get_icons() { + + $theme = $this->app->query_option("view.theme", "-NONE-"); + $theme_path = APP_PATH . "/client/images/themes/${theme}"; + + $icons = []; + + if (is_dir($theme_path)) { + if ($dir = opendir($theme_path)) { + while (($name = readdir($dir)) !== false) { + $path_parts = pathinfo($name); + if (in_array(@$path_parts["extension"], Theme::$extensions)) { + $icons[$path_parts["filename"]] = "${theme}/${name}"; + } + } + closedir($dir); + } + } + + return $icons; + } +}