mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-28 05:54:48 -04:00
Refactor PHP.
This commit is contained in:
parent
6ee0ed3444
commit
55597ae1ba
6 changed files with 179 additions and 149 deletions
|
@ -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)) {
|
||||
|
|
|
@ -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 = "<table>";
|
||||
|
||||
$html .= "<tr>";
|
||||
$html .= "<th class='fb-i'></th>";
|
||||
$html .= "<th class='fb-n'><span>Name</span></th>";
|
||||
$html .= "<th class='fb-d'><span>Last modified</span></th>";
|
||||
$html .= "<th class='fb-s'><span>Size</span></th>";
|
||||
$html .= "</tr>";
|
||||
|
||||
if ($folder->get_parent($cache)) {
|
||||
$html .= "<tr>";
|
||||
$html .= "<td class='fb-i'><img src='" . APP_HREF . "client/images/fallback/folder-parent.png' alt='folder-parent'/></td>";
|
||||
$html .= "<td class='fb-n'><a href='..'>Parent Directory</a></td>";
|
||||
$html .= "<td class='fb-d'></td>";
|
||||
$html .= "<td class='fb-s'></td>";
|
||||
$html .= "</tr>";
|
||||
}
|
||||
|
||||
foreach ($items as $item) {
|
||||
$type = $item->is_folder ? "folder" : "file";
|
||||
|
||||
$html .= "<tr>";
|
||||
$html .= "<td class='fb-i'><img src='" . APP_HREF . "client/images/fallback/" . $type . ".png' alt='" . $type . "'/></td>";
|
||||
$html .= "<td class='fb-n'><a href='" . $item->href . "'>" . basename($item->path) . "</a></td>";
|
||||
$html .= "<td class='fb-d'>" . date("Y-m-d H:i", $item->date) . "</td>";
|
||||
$html .= "<td class='fb-s'>" . ($item->size !== null ? intval($item->size / 1000) . " KB" : "" ) . "</td>";
|
||||
$html .= "</tr>";
|
||||
}
|
||||
|
||||
$html .= "</table>";
|
||||
|
||||
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 = [];
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
67
src/_h5ai/server/php/inc/class-custom.php
Normal file
67
src/_h5ai/server/php/inc/class-custom.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
class Custom {
|
||||
|
||||
private static $extensions = ["html", "md"];
|
||||
|
||||
function __construct($app) {
|
||||
|
||||
$this->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]
|
||||
];
|
||||
}
|
||||
}
|
67
src/_h5ai/server/php/inc/class-fallback.php
Normal file
67
src/_h5ai/server/php/inc/class-fallback.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
class Fallback {
|
||||
|
||||
function __construct($app) {
|
||||
|
||||
$this->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 = "<table>";
|
||||
|
||||
$html .= "<tr>";
|
||||
$html .= "<th class='fb-i'></th>";
|
||||
$html .= "<th class='fb-n'><span>Name</span></th>";
|
||||
$html .= "<th class='fb-d'><span>Last modified</span></th>";
|
||||
$html .= "<th class='fb-s'><span>Size</span></th>";
|
||||
$html .= "</tr>";
|
||||
|
||||
if ($folder->get_parent($cache)) {
|
||||
$html .= "<tr>";
|
||||
$html .= "<td class='fb-i'><img src='" . APP_HREF . "client/images/fallback/folder-parent.png' alt='folder-parent'/></td>";
|
||||
$html .= "<td class='fb-n'><a href='..'>Parent Directory</a></td>";
|
||||
$html .= "<td class='fb-d'></td>";
|
||||
$html .= "<td class='fb-s'></td>";
|
||||
$html .= "</tr>";
|
||||
}
|
||||
|
||||
foreach ($items as $item) {
|
||||
$type = $item->is_folder ? "folder" : "file";
|
||||
|
||||
$html .= "<tr>";
|
||||
$html .= "<td class='fb-i'><img src='" . APP_HREF . "client/images/fallback/" . $type . ".png' alt='" . $type . "'/></td>";
|
||||
$html .= "<td class='fb-n'><a href='" . $item->href . "'>" . basename($item->path) . "</a></td>";
|
||||
$html .= "<td class='fb-d'>" . date("Y-m-d H:i", $item->date) . "</td>";
|
||||
$html .= "<td class='fb-s'>" . ($item->size !== null ? intval($item->size / 1000) . " KB" : "" ) . "</td>";
|
||||
$html .= "</tr>";
|
||||
}
|
||||
|
||||
$html .= "</table>";
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
33
src/_h5ai/server/php/inc/class-theme.php
Normal file
33
src/_h5ai/server/php/inc/class-theme.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
class Theme {
|
||||
|
||||
private static $extensions = ["svg", "png", "jpg"];
|
||||
|
||||
function __construct($app) {
|
||||
|
||||
$this->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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue