diff --git a/src/_h5ai/client/js/inc/boot.js b/src/_h5ai/client/js/inc/boot.js index 70f595d7..4efaf837 100644 --- a/src/_h5ai/client/js/inc/boot.js +++ b/src/_h5ai/client/js/inc/boot.js @@ -11,7 +11,7 @@ modulejs.define('boot', ['$'], function ($) { if (module === 'index') { href = '.'; } else if (module === 'info') { - data.updatecmds = true; + data.updateCachedSetup = true; href = 'server/php/index.php'; } else { return; diff --git a/src/_h5ai/client/js/inc/main/info.js b/src/_h5ai/client/js/inc/main/info.js index 9850eda9..77257722 100644 --- a/src/_h5ai/client/js/inc/main/info.js +++ b/src/_h5ai/client/js/inc/main/info.js @@ -62,7 +62,7 @@ modulejs.define('main/info', ['$', 'config'], function ($, config) { addTest( 'PHP version', 'PHP version >= ' + setup.MIN_PHP_VERSION, - setup.HAS_MIN_PHP_VERSION, setup.PHP_VERSION + true, setup.PHP_VERSION ); addTest( diff --git a/src/_h5ai/server/php/config.php b/src/_h5ai/server/php/config.php index 11267288..ac02526f 100644 --- a/src/_h5ai/server/php/config.php +++ b/src/_h5ai/server/php/config.php @@ -1,8 +1,7 @@ basepath = $basepath; + $this->classpaths = ["/inc", "/inc/core", "/inc/ext"]; } public function run() { spl_autoload_register([$this, "autoload"]); + putenv("LANG=en_US.UTF-8"); + setlocale(LC_CTYPE, "en_US.UTF-8"); + date_default_timezone_set(@date_default_timezone_get()); + session_start(); + + $request_method = getenv("REQUEST_METHOD"); + $request_uri = getenv("REQUEST_URI"); + $script_name = getenv("SCRIPT_NAME"); + $server_software = getenv("SERVER_SOFTWARE"); $this->once("config"); - $this->setup_php(); - $this->setup_app(); - $this->setup_admin(); - $this->setup_server(); - $this->setup_paths(); - $this->setup_cache(); - $this->setup_cmds(); - $app = new App(); - if (Util::is_post_request()) { - $api = new Api($app); - $api->apply(); + $request = new Request($_REQUEST); + $setup = new Setup($request->query_boolean("updateCachedSetup", false), $_ENV); + $app = new App($request, $setup); + + if (strtolower(getenv("REQUEST_METHOD")) === "post") { + (new Api($app))->apply(); } else { - $fallback = new Fallback($app); - define("FALLBACK", $fallback->get_html()); + // (new Page($app))->apply(); + // define("PAGE_APP_HREF", $setup->get("APP_HREF")); + // define("PAGE_FALLBACK", (new Fallback($app))->get_html()); + define("APP_HREF", $setup->get("APP_HREF")); + define("FALLBACK", (new Fallback($app))->get_html()); $this->once("inc/page"); } } @@ -39,8 +46,8 @@ class Bootstrap { $filename = "class-" . strtolower($class_name) . ".php"; - foreach (Bootstrap::$classpaths as $path) { - $file = $this->basepath . $path . "/" . $filename; + foreach ($this->classpaths as $classpath) { + $file = $this->basepath . $classpath . "/" . $filename; if (file_exists($file)) { require_once($file); return true; @@ -52,101 +59,4 @@ class Bootstrap { require_once($this->basepath . "/" . $lib . ".php"); } - - private function setup_php() { - - putenv("LANG=en_US.UTF-8"); - setlocale(LC_CTYPE, "en_US.UTF-8"); - date_default_timezone_set(@date_default_timezone_get()); - - define("HAS_PHP_EXIF", function_exists("exif_thumbnail")); - $has_php_jpeg = false; - if (function_exists("gd_info")) { - $infos = gd_info(); - $has_php_jpeg = array_key_exists("JPEG Support", $infos) && $infos["JPEG Support"]; - } - define("HAS_PHP_JPEG", $has_php_jpeg); - } - - private function setup_app() { - - define("NAME", "{{pkg.name}}"); - define("VERSION", "{{pkg.version}}"); - define("FILE_PREFIX", "_{{pkg.name}}"); - } - - private function setup_admin() { - - session_start(); - define("AS_ADMIN_SESSION_KEY", "__H5AI_AS_ADMIN__"); - define("AS_ADMIN", isset($_SESSION[AS_ADMIN_SESSION_KEY]) && $_SESSION[AS_ADMIN_SESSION_KEY] === true); - define("HAS_CUSTOM_PASSHASH", strcasecmp(PASSHASH, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") !== 0); - } - - private function setup_server() { - - $server_name = null; - $server_version = null; - $server_software = getenv("SERVER_SOFTWARE"); - if ($server_software && preg_match("#^(.*?)(?:/(.*?))?(?: |$)#", strtolower($server_software), $matches)) { - $server_name = $matches[1]; - $server_version = count($matches) > 2 ? $matches[2] : ''; - } - define("SERVER_NAME", $server_name); - define("SERVER_VERSION", $server_version); - define("HAS_SERVER", in_array($server_name, ["apache", "lighttpd", "nginx", "cherokee"])); - } - - private function setup_paths() { - - $script_name = getenv("SCRIPT_NAME"); - if (SERVER_NAME === "lighttpd") { - $script_name = preg_replace("#^.*?//#", "/", $script_name); - } - define("APP_HREF", Util::normalize_path(dirname(dirname(dirname($script_name))), true)); - define("APP_PATH", Util::normalize_path(dirname(dirname(dirname(dirname(__FILE__)))), false)); - - define("ROOT_HREF", Util::normalize_path(dirname(APP_HREF), true)); - define("ROOT_PATH", Util::normalize_path(dirname(APP_PATH), false)); - - $index_href = null; - if (@is_readable(Util::normalize_path(APP_PATH . "/server/php/index.php", false))) { - $index_href = Util::normalize_path(APP_HREF . "/server/php/index.php", false); - } - define("INDEX_HREF", $index_href); - } - - private function setup_cache() { - - define("CACHE_HREF", Util::normalize_path(APP_HREF . "/cache", true)); - define("CACHE_PATH", Util::normalize_path(APP_PATH . "/cache", false)); - define("HAS_WRITABLE_CACHE", @is_writable(CACHE_PATH)); - } - - private function setup_cmds() { - - define("CMDS_PATH", Util::normalize_path(CACHE_PATH . "/cmds.json", false)); - - $cmds = Util::load_commented_json(CMDS_PATH); - 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"); - - $cmd = false; - if ($cmds["command"]) { - $cmd = "command -v"; - } else if ($cmds["which"]) { - $cmd = "which"; - } - - foreach (["avconv", "convert", "du", "ffmpeg", "tar", "zip"] as $c) { - $cmds[$c] = ($cmd !== false) && Util::exec_0($cmd . " " . $c); - } - - Util::save_json(CMDS_PATH, $cmds); - } - foreach ($cmds as $c => $has) { - define("HAS_CMD_" . strtoupper($c), $has); - } - } } diff --git a/src/_h5ai/server/php/inc/core/class-api.php b/src/_h5ai/server/php/inc/core/class-api.php index fb635398..69d320b8 100644 --- a/src/_h5ai/server/php/inc/core/class-api.php +++ b/src/_h5ai/server/php/inc/core/class-api.php @@ -2,18 +2,20 @@ class Api { + private $request; + private $setup; private $app; - public function __construct($app) { + $this->request = $app->get_request(); + $this->setup = $app->get_setup(); $this->app = $app; } - public function apply() { - $action = Util::query_request_param("action"); + $action = $this->request->query("action"); $supported = ["download", "get", "login", "logout"]; Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported)); @@ -21,15 +23,14 @@ class Api { $this->$methodname(); } - private function on_download() { Util::json_fail(Util::ERR_DISABLED, "download disabled", !$this->app->query_option("download.enabled", false)); - $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"); + $as = $this->request->query("as"); + $type = $this->request->query("type"); + $base_href = $this->request->query("baseHref"); + $hrefs = $this->request->query("hrefs"); $archive = new Archive($this->app); @@ -43,66 +44,70 @@ class Api { exit; } - private function on_get() { $response = []; - foreach (["langs", "options", "setup", "types"] as $name) { - if (Util::query_boolean_request_param($name, false)) { + foreach (["langs", "options", "types"] as $name) { + if ($this->request->query_boolean($name, false)) { $methodname = "get_${name}"; $response[$name] = $this->app->$methodname(); } } - if (Util::query_boolean_request_param("theme", false)) { + if ($this->request->query_boolean("setup", false)) { + + $response["setup"] = $this->setup->to_jsono(); + } + + if ($this->request->query_boolean("theme", false)) { $theme = new Theme($this->app); $response["theme"] = $theme->get_icons(); } - if (Util::query_request_param("items", false)) { + if ($this->request->query("items", false)) { - $href = Util::query_request_param("items.href"); - $what = Util::query_numeric_request_param("items.what"); + $href = $this->request->query("items.href"); + $what = $this->request->query_numeric("items.what"); $response["items"] = $this->app->get_items($href, $what); } - if (Util::query_request_param("custom", false)) { + if ($this->request->query("custom", false)) { Util::json_fail(Util::ERR_DISABLED, "custom disabled", !$this->app->query_option("custom.enabled", false)); - $href = Util::query_request_param("custom"); + $href = $this->request->query("custom"); $custom = new Custom($this->app); $response["custom"] = $custom->get_customizations($href); } - if (Util::query_request_param("l10n", false)) { + if ($this->request->query("l10n", false)) { Util::json_fail(Util::ERR_DISABLED, "l10n disabled", !$this->app->query_option("l10n.enabled", false)); - $iso_codes = Util::query_array_request_param("l10n"); + $iso_codes = $this->request->query_array("l10n"); $iso_codes = array_filter($iso_codes); $response["l10n"] = $this->app->get_l10n($iso_codes); } - if (Util::query_request_param("search", false)) { + if ($this->request->query("search", false)) { Util::json_fail(Util::ERR_DISABLED, "search disabled", !$this->app->query_option("search.enabled", false)); - $href = Util::query_request_param("search.href"); - $pattern = Util::query_request_param("search.pattern"); + $href = $this->request->query("search.href"); + $pattern = $this->request->query("search.pattern"); $search = new Search($this->app); $response["search"] = $search->get_items($href, $pattern); } - if (Util::query_request_param("thumbs", false)) { + if ($this->request->query("thumbs", false)) { Util::json_fail(Util::ERR_DISABLED, "thumbnails disabled", !$this->app->query_option("thumbnails.enabled", false)); - Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !HAS_PHP_JPEG); - $thumbs = Util::query_array_request_param("thumbs"); + Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !$this->setup->get("HAS_PHP_JPEG")); + $thumbs = $this->request->query_array("thumbs"); $response["thumbs"] = $this->app->get_thumbs($thumbs); } @@ -110,18 +115,14 @@ class Api { 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]]); + $pass = $this->request->query("pass"); + Util::json_exit(["asAdmin" => $this->app->login_admin($pass)]); } - private function on_logout() { - $_SESSION[AS_ADMIN_SESSION_KEY] = false; - Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]); + Util::json_exit(["asAdmin" => $this->app->logout_admin()]); } } diff --git a/src/_h5ai/server/php/inc/core/class-app.php b/src/_h5ai/server/php/inc/core/class-app.php index 015a6d9d..e007fc1f 100644 --- a/src/_h5ai/server/php/inc/core/class-app.php +++ b/src/_h5ai/server/php/inc/core/class-app.php @@ -2,80 +2,62 @@ class App { + private $request; + private $setup; private $options; + public function __construct($request, $setup) { - public function __construct() { - - $this->options = Util::load_commented_json(APP_PATH . "/conf/options.json"); + $this->request = $request; + $this->setup = $setup; + $this->options = Util::load_commented_json($this->setup->get("APP_PATH") . "/conf/options.json"); } + public function get_request() { + + return $this->request; + } + + public function get_setup() { + + return $this->setup; + } public function get_options() { return $this->options; } - public function query_option($keypath = "", $default = null) { return Util::array_query($this->options, $keypath, $default); } - - public function get_setup() { - - $keys = [ - "APP_HREF", - "ROOT_HREF", - "VERSION", - - "AS_ADMIN", - "HAS_CUSTOM_PASSHASH" - ]; - - if (AS_ADMIN) { - $keys = array_merge($keys, [ - "PHP_VERSION", - "MIN_PHP_VERSION", - "HAS_MIN_PHP_VERSION", - "HAS_PHP_EXIF", - "HAS_PHP_JPEG", - - "SERVER_NAME", - "SERVER_VERSION", - "HAS_SERVER", - - "INDEX_HREF", - - "HAS_WRITABLE_CACHE", - - "HAS_CMD_AVCONV", - "HAS_CMD_CONVERT", - "HAS_CMD_DU", - "HAS_CMD_FFMPEG", - "HAS_CMD_TAR", - "HAS_CMD_ZIP" - ]); - } - - $setup = []; - foreach ($keys as $key) { - $setup[$key] = constant($key); - } - return $setup; - } - - public function get_types() { - return Util::load_commented_json(APP_PATH . "/conf/types.json"); + return Util::load_commented_json($this->setup->get("APP_PATH") . "/conf/types.json"); } + public function login_admin($pass) { + + $key = $this->setup->get("AS_ADMIN_SESSION_KEY"); + $hash = $this->setup->get("PASSHASH"); + + $_SESSION[$key] = strcasecmp(hash("sha512", $pass), $hash) === 0; + return $_SESSION[$key]; + } + + public function logout_admin() { + + $key = $this->setup->get("AS_ADMIN_SESSION_KEY"); + + $_SESSION[$key] = false; + return $_SESSION[$key]; + } public function to_href($path, $trailing_slash = true) { - $rel_path = substr($path, strlen(ROOT_PATH)); + $rel_path = substr($path, strlen($this->setup->get("ROOT_PATH"))); $parts = explode("/", $rel_path); $encoded_parts = []; foreach ($parts as $part) { @@ -84,17 +66,15 @@ class App { } } - return Util::normalize_path(ROOT_HREF . implode("/", $encoded_parts), $trailing_slash); + return Util::normalize_path($this->setup->get("ROOT_HREF") . implode("/", $encoded_parts), $trailing_slash); } - public function to_path($href) { - $rel_href = substr($href, strlen(ROOT_HREF)); - return Util::normalize_path(ROOT_PATH . "/" . rawurldecode($rel_href)); + $rel_href = substr($href, strlen($this->setup->get("ROOT_HREF"))); + return Util::normalize_path($this->setup->get("ROOT_PATH") . "/" . rawurldecode($rel_href)); } - public function is_hidden($name) { // always hide @@ -112,7 +92,6 @@ class App { return false; } - public function read_dir($path) { $names = []; @@ -131,20 +110,18 @@ class App { return $names; } - public function is_managed_href($href) { return $this->is_managed_path($this->to_path($href)); } - public function is_managed_path($path) { if (!is_dir($path) || strpos($path, '../') !== false || strpos($path, '/..') !== false || $path === '..') { return false; } - if ($path === APP_PATH || strpos($path, APP_PATH . '/') === 0) { + if ($path === $this->setup->get("APP_PATH") || strpos($path, $this->setup->get("APP_PATH") . '/') === 0) { return false; } @@ -154,7 +131,7 @@ class App { } } - while ($path !== ROOT_PATH) { + while ($path !== $this->setup->get("ROOT_PATH")) { if (@is_dir($path . "/_h5ai/server")) { return false; } @@ -167,7 +144,6 @@ class App { return true; } - public function get_items($href, $what) { if (!$this->is_managed_href($href)) { @@ -200,11 +176,10 @@ class App { return $result; } - public function get_langs() { $langs = []; - $l10n_path = APP_PATH . "/conf/l10n"; + $l10n_path = $this->setup->get("APP_PATH") . "/conf/l10n"; if (is_dir($l10n_path)) { if ($dir = opendir($l10n_path)) { while (($file = readdir($dir)) !== false) { @@ -220,13 +195,12 @@ class App { return $langs; } - public function get_l10n($iso_codes) { $results = []; foreach ($iso_codes as $iso_code) { - $file = APP_PATH . "/conf/l10n/" . $iso_code . ".json"; + $file = $this->setup->get("APP_PATH") . "/conf/l10n/" . $iso_code . ".json"; $results[$iso_code] = Util::load_commented_json($file); $results[$iso_code]["isoCode"] = $iso_code; } @@ -234,7 +208,6 @@ class App { return $results; } - public function get_thumbs($requests) { $hrefs = []; diff --git a/src/_h5ai/server/php/inc/core/class-fallback.php b/src/_h5ai/server/php/inc/core/class-fallback.php index 45287194..cdd30355 100644 --- a/src/_h5ai/server/php/inc/core/class-fallback.php +++ b/src/_h5ai/server/php/inc/core/class-fallback.php @@ -26,6 +26,8 @@ class Fallback { $path = $this->get_current_path(); } + $app_href = $this->app->get_setup()->get("APP_HREF"); + $cache = []; $folder = Item::get($this->app, $path, $cache); $items = $folder->get_content($cache); @@ -42,7 +44,7 @@ class Fallback { if ($folder->get_parent($cache)) { $html .= ""; - $html .= "folder-parent"; + $html .= "folder-parent"; $html .= "Parent Directory"; $html .= ""; $html .= ""; @@ -53,7 +55,7 @@ class Fallback { $type = $item->is_folder ? "folder" : "file"; $html .= ""; - $html .= "" . $type . ""; + $html .= "${type}"; $html .= "" . basename($item->path) . ""; $html .= "" . date("Y-m-d H:i", $item->date) . ""; $html .= "" . ($item->size !== null ? intval($item->size / 1000) . " KB" : "" ) . ""; diff --git a/src/_h5ai/server/php/inc/core/class-item.php b/src/_h5ai/server/php/inc/core/class-item.php index 79e13db7..b1d87963 100644 --- a/src/_h5ai/server/php/inc/core/class-item.php +++ b/src/_h5ai/server/php/inc/core/class-item.php @@ -17,7 +17,7 @@ class Item { public static function get($app, $path, &$cache) { - if (!Util::starts_with($path, ROOT_PATH)) { + if (!Util::starts_with($path, $app->get_setup()->get("ROOT_PATH"))) { return null; } @@ -72,7 +72,7 @@ class Item { public function get_parent(&$cache) { $parent_path = Util::normalize_path(dirname($this->path), false); - if ($parent_path !== $this->path && Util::starts_with($parent_path, ROOT_PATH)) { + if ($parent_path !== $this->path && Util::starts_with($parent_path, $this->app->get_setup()->get("ROOT_PATH"))) { return Item::get($this->app, $parent_path, $cache); } return null; diff --git a/src/_h5ai/server/php/inc/core/class-request.php b/src/_h5ai/server/php/inc/core/class-request.php new file mode 100644 index 00000000..ba5cae9b --- /dev/null +++ b/src/_h5ai/server/php/inc/core/class-request.php @@ -0,0 +1,43 @@ +params = $params; + } + + public function query($keypath = "", $default = Util::NO_DEFAULT) { + + $value = Util::array_query($this->params, $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 $value; + } + + public function query_boolean($keypath = "", $default = Util::NO_DEFAULT) { + + $value = $this->query($keypath, $default); + return filter_var($value, FILTER_VALIDATE_BOOLEAN); + } + + public function query_numeric($keypath = "", $default = Util::NO_DEFAULT) { + + $value = $this->query($keypath, $default); + Util::json_fail(Util::ERR_ILLIGAL_PARAM, "parameter '$keypath' is not numeric", !is_numeric($value)); + return intval($value, 10); + } + + public function query_array($keypath = "", $default = Util::NO_DEFAULT) { + + $value = $this->query($keypath, $default); + Util::json_fail(Util::ERR_ILLIGAL_PARAM, "parameter '$keypath' is no array", !is_array($value)); + return $value; + } +} diff --git a/src/_h5ai/server/php/inc/core/class-setup.php b/src/_h5ai/server/php/inc/core/class-setup.php new file mode 100644 index 00000000..3701bb9b --- /dev/null +++ b/src/_h5ai/server/php/inc/core/class-setup.php @@ -0,0 +1,193 @@ +consts = []; + $this->update_cached_setup = $update_cached_setup; + $this->env = $env; + + $this->setup_php(); + $this->setup_app(); + $this->setup_admin(); + $this->setup_server(); + $this->setup_paths(); + $this->setup_cache(); + $this->setup_cmds(); + } + + private function set($key, $value) { + + if (array_key_exists($key, $this->consts)) { + Logger::log("setup key already taken", [ + "key" => $key, + "value" => $value, + "found" => $this->consts[$key] + ]); + exit; + } + if (!is_string($value) && !is_bool($value)) { + Logger::log("setup value neither string nor boolean", [ + "key" => $key, + "value" => $value + ]); + exit; + } + + $this->consts[$key] = $value; + } + + public function get($key) { + + if (!array_key_exists($key, $this->consts)) { + Logger::log("setup key not found", ["key" => $key]); + exit; + } + + return $this->consts[$key]; + } + + private function setup_php() { + + $this->set("PHP_VERSION", PHP_VERSION); + $this->set("MIN_PHP_VERSION", MIN_PHP_VERSION); + $this->set("HAS_PHP_EXIF", function_exists("exif_thumbnail")); + + $has_php_jpeg = false; + if (function_exists("gd_info")) { + $infos = gd_info(); + $has_php_jpeg = array_key_exists("JPEG Support", $infos) && $infos["JPEG Support"]; + } + $this->set("HAS_PHP_JPEG", $has_php_jpeg); + } + + private function setup_app() { + + $this->set("NAME", "{{pkg.name}}"); + $this->set("VERSION", "{{pkg.version}}"); + $this->set("FILE_PREFIX", "_{{pkg.name}}"); + } + + private function setup_admin() { + + $this->set("AS_ADMIN_SESSION_KEY", Setup::AS_ADMIN_SESSION_KEY); + $this->set("AS_ADMIN", isset($_SESSION[Setup::AS_ADMIN_SESSION_KEY]) && $_SESSION[Setup::AS_ADMIN_SESSION_KEY] === true); + $this->set("PASSHASH", PASSHASH); + $this->set("HAS_CUSTOM_PASSHASH", strtolower(PASSHASH) === strtolower(Setup::DEFAULT_PASSHASH)); + } + + private function setup_server() { + + $server_name = null; + $server_version = null; + $server_software = getenv("SERVER_SOFTWARE"); + if ($server_software && preg_match("#^(.*?)(?:/(.*?))?(?: |$)#", strtolower($server_software), $matches)) { + $server_name = $matches[1]; + $server_version = count($matches) > 2 ? $matches[2] : ''; + } + $this->set("SERVER_NAME", $server_name); + $this->set("SERVER_VERSION", $server_version); + $this->set("HAS_SERVER", in_array($server_name, ["apache", "lighttpd", "nginx", "cherokee"])); + } + + private function setup_paths() { + + $script_name = getenv("SCRIPT_NAME"); + if ($this->get("SERVER_NAME") === "lighttpd") { + $script_name = preg_replace("#^.*?//#", "/", $script_name); + } + $this->set("APP_HREF", Util::normalize_path(dirname(dirname(dirname($script_name))), true)); + $this->set("APP_PATH", Util::normalize_path(dirname(dirname(dirname(dirname(dirname(__FILE__))))), false)); + + $this->set("ROOT_HREF", Util::normalize_path(dirname($this->get("APP_HREF")), true)); + $this->set("ROOT_PATH", Util::normalize_path(dirname($this->get("APP_PATH")), false)); + + $index_href = null; + if (@is_readable(Util::normalize_path($this->get("APP_PATH") . "/server/php/index.php", false))) { + $index_href = Util::normalize_path($this->get("APP_HREF") . "/server/php/index.php", false); + } + $this->set("INDEX_HREF", $index_href); + } + + private function setup_cache() { + + $this->set("CACHE_HREF", Util::normalize_path($this->get("APP_HREF") . "/cache", true)); + $this->set("CACHE_PATH", Util::normalize_path($this->get("APP_PATH") . "/cache", false)); + $this->set("HAS_WRITABLE_CACHE", @is_writable($this->get("CACHE_PATH"))); + } + + private function setup_cmds() { + + $this->set("CMDS_PATH", Util::normalize_path($this->get("CACHE_PATH") . "/cmds.json", false)); + + $cmds = Util::load_commented_json($this->get("CMDS_PATH")); + if (sizeof($cmds) === 0 || $this->update_cached_setup) { + $cmds["command"] = Util::exec_0("command -v command"); + $cmds["which"] = Util::exec_0("which which"); + + $cmd = false; + if ($cmds["command"]) { + $cmd = "command -v"; + } else if ($cmds["which"]) { + $cmd = "which"; + } + + foreach (["avconv", "convert", "du", "ffmpeg", "tar", "zip"] as $c) { + $cmds[$c] = ($cmd !== false) && Util::exec_0($cmd . " " . $c); + } + + Util::save_json($this->get("CMDS_PATH"), $cmds); + } + foreach ($cmds as $c => $has) { + $this->set("HAS_CMD_" . strtoupper($c), $has); + } + } + + public function to_jsono() { + + $keys = [ + "APP_HREF", + "ROOT_HREF", + "VERSION", + + "AS_ADMIN", + "HAS_CUSTOM_PASSHASH" + ]; + + if ($this->get("AS_ADMIN")) { + $keys = array_merge($keys, [ + "PHP_VERSION", + "MIN_PHP_VERSION", + "HAS_PHP_EXIF", + "HAS_PHP_JPEG", + + "SERVER_NAME", + "SERVER_VERSION", + "HAS_SERVER", + + "INDEX_HREF", + + "HAS_WRITABLE_CACHE", + + "HAS_CMD_AVCONV", + "HAS_CMD_CONVERT", + "HAS_CMD_DU", + "HAS_CMD_FFMPEG", + "HAS_CMD_TAR", + "HAS_CMD_ZIP" + ]); + } + + $jsono = []; + foreach ($keys as $key) { + $jsono[$key] = $this->get($key); + } + return $jsono; + } +} diff --git a/src/_h5ai/server/php/inc/core/class-theme.php b/src/_h5ai/server/php/inc/core/class-theme.php index 2e0e102e..ad486dd0 100644 --- a/src/_h5ai/server/php/inc/core/class-theme.php +++ b/src/_h5ai/server/php/inc/core/class-theme.php @@ -11,8 +11,9 @@ class Theme { public function get_icons() { + $app_path = $this->app->get_setup()->get("APP_PATH"); $theme = $this->app->query_option("view.theme", "-NONE-"); - $theme_path = APP_PATH . "/client/images/themes/${theme}"; + $theme_path = "${app_path}/client/images/themes/${theme}"; $icons = []; diff --git a/src/_h5ai/server/php/inc/core/class-util.php b/src/_h5ai/server/php/inc/core/class-util.php index 44f23d43..04509e49 100644 --- a/src/_h5ai/server/php/inc/core/class-util.php +++ b/src/_h5ai/server/php/inc/core/class-util.php @@ -51,48 +51,6 @@ class Util { } - public static function is_post_request() { - - return (strtolower(getenv("REQUEST_METHOD")) === "post"); - } - - - public static function query_request_param($keypath = "", $default = Util::NO_DEFAULT) { - - $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 $value; - } - - - public static function query_boolean_request_param($keypath = "", $default = Util::NO_DEFAULT) { - - $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; - } - - public static function starts_with($sequence, $head) { return substr($sequence, 0, strlen($head)) === $head; @@ -210,7 +168,7 @@ class Util { } else if (is_dir($path)) { if ($app->query_option("foldersize.enabled", false)) { - if (HAS_CMD_DU && $app->query_option("foldersize.type", null) === "shell-du") { + if ($app->get_setup()->get("HAS_CMD_DU") && $app->query_option("foldersize.type", null) === "shell-du") { $cmdv = ["du", "-sk", $path]; $size = intval(preg_replace("#\s.*$#", "", Util::exec_cmdv($cmdv)), 10) * 1024; } else { diff --git a/src/_h5ai/server/php/inc/ext/class-custom.php b/src/_h5ai/server/php/inc/ext/class-custom.php index 10c74b5f..f78a05a7 100644 --- a/src/_h5ai/server/php/inc/ext/class-custom.php +++ b/src/_h5ai/server/php/inc/ext/class-custom.php @@ -11,8 +11,10 @@ class Custom { private function read_custom_file($path, $name, &$content, &$type) { + $file_prefix = $this->app->get_setup()->get("FILE_PREFIX"); + foreach (Custom::$extensions as $ext) { - $file = "$path/" . FILE_PREFIX . ".$name.$ext"; + $file = "${path}/${file_prefix}.${name}.${ext}"; if (is_readable($file)) { $content = file_get_contents($file); $type = $ext; @@ -30,6 +32,7 @@ class Custom { ]; } + $root_path = $this->app->get_setup()->get("FILE_PREFIX"); $path = $this->app->to_path($href); $header = null; @@ -49,7 +52,7 @@ class Custom { $this->read_custom_file($path, "footers", $footer, $footer_type); } - if ($path === ROOT_PATH) { + if ($path === $root_path) { break; } $parent_path = Util::normalize_path(dirname($path)); diff --git a/src/_h5ai/server/php/inc/ext/class-thumb.php b/src/_h5ai/server/php/inc/ext/class-thumb.php index 54876c68..a8877475 100644 --- a/src/_h5ai/server/php/inc/ext/class-thumb.php +++ b/src/_h5ai/server/php/inc/ext/class-thumb.php @@ -13,9 +13,10 @@ class Thumb { public function __construct($app) { + $this->setup = $app->get_setup(); $this->app = $app; - $this->thumbs_path = CACHE_PATH . "/" . Thumb::$THUMB_CACHE; - $this->thumbs_href = CACHE_HREF . Thumb::$THUMB_CACHE; + $this->thumbs_path = $this->setup->get("CACHE_PATH") . "/" . Thumb::$THUMB_CACHE; + $this->thumbs_href = $this->setup->get("CACHE_HREF") . Thumb::$THUMB_CACHE; if (!is_dir($this->thumbs_path)) { @mkdir($this->thumbs_path, 0755, true); @@ -26,7 +27,7 @@ class Thumb { public function thumb($type, $source_href, $width, $height) { $source_path = $this->app->to_path($source_href); - if (!file_exists($source_path) || Util::starts_with($source_path, CACHE_PATH)) { + if (!file_exists($source_path) || Util::starts_with($source_path, $this->setup->get("CACHE_PATH"))) { return null; } @@ -34,12 +35,12 @@ class Thumb { if ($type === "img") { $capture_path = $source_path; } else if ($type === "mov") { - if (HAS_CMD_AVCONV) { + if ($this->setup->get("HAS_CMD_AVCONV")) { $capture_path = $this->capture(Thumb::$AVCONV_CMDV, $source_path); - } else if (HAS_CMD_FFMPEG) { + } else if ($this->setup->get("HAS_CMD_FFMPEG")) { $capture_path = $this->capture(Thumb::$FFMPEG_CMDV, $source_path); } - } else if ($type === "doc" && HAS_CMD_CONVERT) { + } else if ($type === "doc" && $this->setup->get("HAS_CMD_CONVERT")) { $capture_path = $this->capture(Thumb::$CONVERT_CMDV, $source_path); } @@ -62,7 +63,7 @@ class Thumb { $image = new Image(); $et = false; - if (HAS_PHP_EXIF && $this->app->query_option("thumbnails.exif", false) === true && $height != 0) { + if ($this->setup->get("HAS_PHP_EXIF") && $this->app->query_option("thumbnails.exif", false) === true && $height != 0) { $et = @exif_thumbnail($source_path); } if($et !== false) { diff --git a/src/_h5ai/server/php/inc/version-check.php b/src/_h5ai/server/php/inc/version-check.php deleted file mode 100644 index 499882c7..00000000 --- a/src/_h5ai/server/php/inc/version-check.php +++ /dev/null @@ -1,14 +0,0 @@ -= 0); - -if (!HAS_MIN_PHP_VERSION) { - header("Content-type: application/json;charset=utf-8"); - echo json_encode(array( - "err" => "ERR_PHP", - "msg" => "PHP " . MIN_PHP_VERSION . "+ required, only found " . PHP_VERSION, - "ver" => PHP_VERSION - )); - exit; -} diff --git a/src/_h5ai/server/php/index.php b/src/_h5ai/server/php/index.php index df9effc4..cbf2aca0 100644 --- a/src/_h5ai/server/php/index.php +++ b/src/_h5ai/server/php/index.php @@ -1,8 +1,15 @@ run(); +$basepath = preg_replace('#[\\\\/]+#', '/', dirname(__FILE__)); +require_once($basepath . '/inc/class-bootstrap.php'); + +$bootstrap = new Bootstrap($basepath); +$bootstrap->run();