Refactor PHP. Remove almost all defines.

This commit is contained in:
Lars Jung 2015-05-11 11:55:48 +02:00
parent 7bcb52214b
commit c18b09c658
16 changed files with 372 additions and 295 deletions

View file

@ -11,7 +11,7 @@ modulejs.define('boot', ['$'], function ($) {
if (module === 'index') { if (module === 'index') {
href = '.'; href = '.';
} else if (module === 'info') { } else if (module === 'info') {
data.updatecmds = true; data.updateCachedSetup = true;
href = 'server/php/index.php'; href = 'server/php/index.php';
} else { } else {
return; return;

View file

@ -62,7 +62,7 @@ modulejs.define('main/info', ['$', 'config'], function ($, config) {
addTest( addTest(
'PHP version', 'PHP version >= ' + setup.MIN_PHP_VERSION, 'PHP version', 'PHP version >= ' + setup.MIN_PHP_VERSION,
setup.HAS_MIN_PHP_VERSION, setup.PHP_VERSION true, setup.PHP_VERSION
); );
addTest( addTest(

View file

@ -1,8 +1,7 @@
<?php <?php
/********************************************************************* /*********************************************************************************
SHA512 hash of the info page password, the preset password is the SHA512 hash of the info page password, the preset password is the empty string.
empty string. You might change it to keep this information private.
Online hash generator: http://md5hashing.net/hashing/sha512 Online hash generator: http://md5hashing.net/hashing/sha512
*********************************************************************/ **********************************************************************************/
define("PASSHASH", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); define('PASSHASH', 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e');

View file

@ -2,35 +2,42 @@
class Bootstrap { class Bootstrap {
private static $classpaths = ["/inc", "/inc/core", "/inc/ext"];
private $basepath; private $basepath;
private $classpaths;
public function __construct($basepath) { public function __construct($basepath) {
$this->basepath = $basepath; $this->basepath = $basepath;
$this->classpaths = ["/inc", "/inc/core", "/inc/ext"];
} }
public function run() { public function run() {
spl_autoload_register([$this, "autoload"]); 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->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(); $request = new Request($_REQUEST);
if (Util::is_post_request()) { $setup = new Setup($request->query_boolean("updateCachedSetup", false), $_ENV);
$api = new Api($app); $app = new App($request, $setup);
$api->apply();
if (strtolower(getenv("REQUEST_METHOD")) === "post") {
(new Api($app))->apply();
} else { } else {
$fallback = new Fallback($app); // (new Page($app))->apply();
define("FALLBACK", $fallback->get_html()); // 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"); $this->once("inc/page");
} }
} }
@ -39,8 +46,8 @@ class Bootstrap {
$filename = "class-" . strtolower($class_name) . ".php"; $filename = "class-" . strtolower($class_name) . ".php";
foreach (Bootstrap::$classpaths as $path) { foreach ($this->classpaths as $classpath) {
$file = $this->basepath . $path . "/" . $filename; $file = $this->basepath . $classpath . "/" . $filename;
if (file_exists($file)) { if (file_exists($file)) {
require_once($file); require_once($file);
return true; return true;
@ -52,101 +59,4 @@ class Bootstrap {
require_once($this->basepath . "/" . $lib . ".php"); 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);
}
}
} }

View file

@ -2,18 +2,20 @@
class Api { class Api {
private $request;
private $setup;
private $app; private $app;
public function __construct($app) { public function __construct($app) {
$this->request = $app->get_request();
$this->setup = $app->get_setup();
$this->app = $app; $this->app = $app;
} }
public function apply() { public function apply() {
$action = Util::query_request_param("action"); $action = $this->request->query("action");
$supported = ["download", "get", "login", "logout"]; $supported = ["download", "get", "login", "logout"];
Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported)); Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported));
@ -21,15 +23,14 @@ class Api {
$this->$methodname(); $this->$methodname();
} }
private function on_download() { private function on_download() {
Util::json_fail(Util::ERR_DISABLED, "download disabled", !$this->app->query_option("download.enabled", false)); Util::json_fail(Util::ERR_DISABLED, "download disabled", !$this->app->query_option("download.enabled", false));
$as = Util::query_request_param("as"); $as = $this->request->query("as");
$type = Util::query_request_param("type"); $type = $this->request->query("type");
$base_href = Util::query_request_param("baseHref"); $base_href = $this->request->query("baseHref");
$hrefs = Util::query_request_param("hrefs"); $hrefs = $this->request->query("hrefs");
$archive = new Archive($this->app); $archive = new Archive($this->app);
@ -43,66 +44,70 @@ class Api {
exit; exit;
} }
private function on_get() { private function on_get() {
$response = []; $response = [];
foreach (["langs", "options", "setup", "types"] as $name) { foreach (["langs", "options", "types"] as $name) {
if (Util::query_boolean_request_param($name, false)) { if ($this->request->query_boolean($name, false)) {
$methodname = "get_${name}"; $methodname = "get_${name}";
$response[$name] = $this->app->$methodname(); $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); $theme = new Theme($this->app);
$response["theme"] = $theme->get_icons(); $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"); $href = $this->request->query("items.href");
$what = Util::query_numeric_request_param("items.what"); $what = $this->request->query_numeric("items.what");
$response["items"] = $this->app->get_items($href, $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)); 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); $custom = new Custom($this->app);
$response["custom"] = $custom->get_customizations($href); $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)); 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); $iso_codes = array_filter($iso_codes);
$response["l10n"] = $this->app->get_l10n($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)); Util::json_fail(Util::ERR_DISABLED, "search disabled", !$this->app->query_option("search.enabled", false));
$href = Util::query_request_param("search.href"); $href = $this->request->query("search.href");
$pattern = Util::query_request_param("search.pattern"); $pattern = $this->request->query("search.pattern");
$search = new Search($this->app); $search = new Search($this->app);
$response["search"] = $search->get_items($href, $pattern); $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_DISABLED, "thumbnails disabled", !$this->app->query_option("thumbnails.enabled", false));
Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !HAS_PHP_JPEG); Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !$this->setup->get("HAS_PHP_JPEG"));
$thumbs = Util::query_array_request_param("thumbs"); $thumbs = $this->request->query_array("thumbs");
$response["thumbs"] = $this->app->get_thumbs($thumbs); $response["thumbs"] = $this->app->get_thumbs($thumbs);
} }
@ -110,18 +115,14 @@ class Api {
Util::json_exit($response); Util::json_exit($response);
} }
private function on_login() { private function on_login() {
$pass = Util::query_request_param("pass"); $pass = $this->request->query("pass");
$_SESSION[AS_ADMIN_SESSION_KEY] = strcasecmp(hash("sha512", $pass), PASSHASH) === 0; Util::json_exit(["asAdmin" => $this->app->login_admin($pass)]);
Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]);
} }
private function on_logout() { private function on_logout() {
$_SESSION[AS_ADMIN_SESSION_KEY] = false; Util::json_exit(["asAdmin" => $this->app->logout_admin()]);
Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]);
} }
} }

View file

@ -2,80 +2,62 @@
class App { class App {
private $request;
private $setup;
private $options; private $options;
public function __construct($request, $setup) {
public function __construct() { $this->request = $request;
$this->setup = $setup;
$this->options = Util::load_commented_json(APP_PATH . "/conf/options.json"); $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() { public function get_options() {
return $this->options; return $this->options;
} }
public function query_option($keypath = "", $default = null) { public function query_option($keypath = "", $default = null) {
return Util::array_query($this->options, $keypath, $default); 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() { 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) { 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); $parts = explode("/", $rel_path);
$encoded_parts = []; $encoded_parts = [];
foreach ($parts as $part) { 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) { public function to_path($href) {
$rel_href = substr($href, strlen(ROOT_HREF)); $rel_href = substr($href, strlen($this->setup->get("ROOT_HREF")));
return Util::normalize_path(ROOT_PATH . "/" . rawurldecode($rel_href)); return Util::normalize_path($this->setup->get("ROOT_PATH") . "/" . rawurldecode($rel_href));
} }
public function is_hidden($name) { public function is_hidden($name) {
// always hide // always hide
@ -112,7 +92,6 @@ class App {
return false; return false;
} }
public function read_dir($path) { public function read_dir($path) {
$names = []; $names = [];
@ -131,20 +110,18 @@ class App {
return $names; return $names;
} }
public function is_managed_href($href) { public function is_managed_href($href) {
return $this->is_managed_path($this->to_path($href)); return $this->is_managed_path($this->to_path($href));
} }
public function is_managed_path($path) { public function is_managed_path($path) {
if (!is_dir($path) || strpos($path, '../') !== false || strpos($path, '/..') !== false || $path === '..') { if (!is_dir($path) || strpos($path, '../') !== false || strpos($path, '/..') !== false || $path === '..') {
return false; 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; 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")) { if (@is_dir($path . "/_h5ai/server")) {
return false; return false;
} }
@ -167,7 +144,6 @@ class App {
return true; return true;
} }
public function get_items($href, $what) { public function get_items($href, $what) {
if (!$this->is_managed_href($href)) { if (!$this->is_managed_href($href)) {
@ -200,11 +176,10 @@ class App {
return $result; return $result;
} }
public function get_langs() { public function get_langs() {
$langs = []; $langs = [];
$l10n_path = APP_PATH . "/conf/l10n"; $l10n_path = $this->setup->get("APP_PATH") . "/conf/l10n";
if (is_dir($l10n_path)) { if (is_dir($l10n_path)) {
if ($dir = opendir($l10n_path)) { if ($dir = opendir($l10n_path)) {
while (($file = readdir($dir)) !== false) { while (($file = readdir($dir)) !== false) {
@ -220,13 +195,12 @@ class App {
return $langs; return $langs;
} }
public function get_l10n($iso_codes) { public function get_l10n($iso_codes) {
$results = []; $results = [];
foreach ($iso_codes as $iso_code) { 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] = Util::load_commented_json($file);
$results[$iso_code]["isoCode"] = $iso_code; $results[$iso_code]["isoCode"] = $iso_code;
} }
@ -234,7 +208,6 @@ class App {
return $results; return $results;
} }
public function get_thumbs($requests) { public function get_thumbs($requests) {
$hrefs = []; $hrefs = [];

View file

@ -26,6 +26,8 @@ class Fallback {
$path = $this->get_current_path(); $path = $this->get_current_path();
} }
$app_href = $this->app->get_setup()->get("APP_HREF");
$cache = []; $cache = [];
$folder = Item::get($this->app, $path, $cache); $folder = Item::get($this->app, $path, $cache);
$items = $folder->get_content($cache); $items = $folder->get_content($cache);
@ -42,7 +44,7 @@ class Fallback {
if ($folder->get_parent($cache)) { if ($folder->get_parent($cache)) {
$html .= "<tr>"; $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-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-n'><a href='..'>Parent Directory</a></td>";
$html .= "<td class='fb-d'></td>"; $html .= "<td class='fb-d'></td>";
$html .= "<td class='fb-s'></td>"; $html .= "<td class='fb-s'></td>";
@ -53,7 +55,7 @@ class Fallback {
$type = $item->is_folder ? "folder" : "file"; $type = $item->is_folder ? "folder" : "file";
$html .= "<tr>"; $html .= "<tr>";
$html .= "<td class='fb-i'><img src='" . APP_HREF . "client/images/fallback/" . $type . ".png' alt='" . $type . "'/></td>"; $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-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-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 .= "<td class='fb-s'>" . ($item->size !== null ? intval($item->size / 1000) . " KB" : "" ) . "</td>";

View file

@ -17,7 +17,7 @@ class Item {
public static function get($app, $path, &$cache) { 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; return null;
} }
@ -72,7 +72,7 @@ class Item {
public function get_parent(&$cache) { public function get_parent(&$cache) {
$parent_path = Util::normalize_path(dirname($this->path), false); $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 Item::get($this->app, $parent_path, $cache);
} }
return null; return null;

View file

@ -0,0 +1,43 @@
<?php
class Request {
private $params;
public function __construct($params = null) {
$this->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;
}
}

View file

@ -0,0 +1,193 @@
<?php
class Setup {
const DEFAULT_PASSHASH = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";
const AS_ADMIN_SESSION_KEY = "__H5AI_AS_ADMIN__";
private $consts;
public function __construct($update_cached_setup = false, $env = []) {
$this->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;
}
}

View file

@ -11,8 +11,9 @@ class Theme {
public function get_icons() { public function get_icons() {
$app_path = $this->app->get_setup()->get("APP_PATH");
$theme = $this->app->query_option("view.theme", "-NONE-"); $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 = []; $icons = [];

View file

@ -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) { public static function starts_with($sequence, $head) {
return substr($sequence, 0, strlen($head)) === $head; return substr($sequence, 0, strlen($head)) === $head;
@ -210,7 +168,7 @@ class Util {
} else if (is_dir($path)) { } else if (is_dir($path)) {
if ($app->query_option("foldersize.enabled", false)) { 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]; $cmdv = ["du", "-sk", $path];
$size = intval(preg_replace("#\s.*$#", "", Util::exec_cmdv($cmdv)), 10) * 1024; $size = intval(preg_replace("#\s.*$#", "", Util::exec_cmdv($cmdv)), 10) * 1024;
} else { } else {

View file

@ -11,8 +11,10 @@ class Custom {
private function read_custom_file($path, $name, &$content, &$type) { private function read_custom_file($path, $name, &$content, &$type) {
$file_prefix = $this->app->get_setup()->get("FILE_PREFIX");
foreach (Custom::$extensions as $ext) { foreach (Custom::$extensions as $ext) {
$file = "$path/" . FILE_PREFIX . ".$name.$ext"; $file = "${path}/${file_prefix}.${name}.${ext}";
if (is_readable($file)) { if (is_readable($file)) {
$content = file_get_contents($file); $content = file_get_contents($file);
$type = $ext; $type = $ext;
@ -30,6 +32,7 @@ class Custom {
]; ];
} }
$root_path = $this->app->get_setup()->get("FILE_PREFIX");
$path = $this->app->to_path($href); $path = $this->app->to_path($href);
$header = null; $header = null;
@ -49,7 +52,7 @@ class Custom {
$this->read_custom_file($path, "footers", $footer, $footer_type); $this->read_custom_file($path, "footers", $footer, $footer_type);
} }
if ($path === ROOT_PATH) { if ($path === $root_path) {
break; break;
} }
$parent_path = Util::normalize_path(dirname($path)); $parent_path = Util::normalize_path(dirname($path));

View file

@ -13,9 +13,10 @@ class Thumb {
public function __construct($app) { public function __construct($app) {
$this->setup = $app->get_setup();
$this->app = $app; $this->app = $app;
$this->thumbs_path = CACHE_PATH . "/" . Thumb::$THUMB_CACHE; $this->thumbs_path = $this->setup->get("CACHE_PATH") . "/" . Thumb::$THUMB_CACHE;
$this->thumbs_href = CACHE_HREF . Thumb::$THUMB_CACHE; $this->thumbs_href = $this->setup->get("CACHE_HREF") . Thumb::$THUMB_CACHE;
if (!is_dir($this->thumbs_path)) { if (!is_dir($this->thumbs_path)) {
@mkdir($this->thumbs_path, 0755, true); @mkdir($this->thumbs_path, 0755, true);
@ -26,7 +27,7 @@ class Thumb {
public function thumb($type, $source_href, $width, $height) { public function thumb($type, $source_href, $width, $height) {
$source_path = $this->app->to_path($source_href); $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; return null;
} }
@ -34,12 +35,12 @@ class Thumb {
if ($type === "img") { if ($type === "img") {
$capture_path = $source_path; $capture_path = $source_path;
} else if ($type === "mov") { } else if ($type === "mov") {
if (HAS_CMD_AVCONV) { if ($this->setup->get("HAS_CMD_AVCONV")) {
$capture_path = $this->capture(Thumb::$AVCONV_CMDV, $source_path); $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); $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); $capture_path = $this->capture(Thumb::$CONVERT_CMDV, $source_path);
} }
@ -62,7 +63,7 @@ class Thumb {
$image = new Image(); $image = new Image();
$et = false; $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); $et = @exif_thumbnail($source_path);
} }
if($et !== false) { if($et !== false) {

View file

@ -1,14 +0,0 @@
<?php
define("MIN_PHP_VERSION", "5.4.0");
define("HAS_MIN_PHP_VERSION", version_compare(PHP_VERSION, MIN_PHP_VERSION) >= 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;
}

View file

@ -1,8 +1,15 @@
<?php <?php
$basepath = preg_replace("#[\\\\/]+#", "/", dirname(__FILE__)); define('MIN_PHP_VERSION', '5.4.0');
require_once($basepath . "/inc/version-check.php"); if (version_compare(PHP_VERSION, MIN_PHP_VERSION) < 0) {
require_once($basepath . "/inc/class-bootstrap.php"); header('Content-type: application/json;charset=utf-8');
echo '{"err":"ERR_PHP","msg":"PHP ' . MIN_PHP_VERSION . '+ required","ver":"' . PHP_VERSION . '"}';
exit;
}
(new Bootstrap($basepath))->run(); $basepath = preg_replace('#[\\\\/]+#', '/', dirname(__FILE__));
require_once($basepath . '/inc/class-bootstrap.php');
$bootstrap = new Bootstrap($basepath);
$bootstrap->run();