diff --git a/src/_h5ai/private/php/class-bootstrap.php b/src/_h5ai/private/php/class-bootstrap.php index d47a8275..67df539e 100644 --- a/src/_h5ai/private/php/class-bootstrap.php +++ b/src/_h5ai/private/php/class-bootstrap.php @@ -1,11 +1,9 @@ context = $context; $this->request = $context->get_request(); $this->setup = $context->get_setup(); } public function apply() { - $action = $this->request->query('action'); $supported = ['download', 'get', 'login', 'logout']; Util::json_fail(Util::ERR_UNSUPPORTED, 'unsupported action', !in_array($action, $supported)); @@ -24,7 +21,6 @@ class Api { } private function on_download() { - Util::json_fail(Util::ERR_DISABLED, 'download disabled', !$this->context->query_option('download.enabled', false)); $as = $this->request->query('as'); @@ -45,7 +41,6 @@ class Api { } private function on_get() { - $response = []; foreach (['langs', 'options', 'types'] as $name) { @@ -57,58 +52,46 @@ class Api { } if ($this->request->query_boolean('setup', false)) { - $response['setup'] = $this->setup->to_jsono($this->context->is_admin()); } if ($this->request->query_boolean('theme', false)) { - $theme = new Theme($this->context); $response['theme'] = $theme->get_icons(); } if ($this->request->query('items', false)) { - $href = $this->request->query('items.href'); $what = $this->request->query_numeric('items.what'); - $response['items'] = $this->context->get_items($href, $what); } if ($this->request->query('custom', false)) { - Util::json_fail(Util::ERR_DISABLED, 'custom disabled', !$this->context->query_option('custom.enabled', false)); $href = $this->request->query('custom'); - $custom = new Custom($this->context); $response['custom'] = $custom->get_customizations($href); } if ($this->request->query('l10n', false)) { - Util::json_fail(Util::ERR_DISABLED, 'l10n disabled', !$this->context->query_option('l10n.enabled', false)); $iso_codes = $this->request->query_array('l10n'); - $iso_codes = array_filter($iso_codes); $response['l10n'] = $this->context->get_l10n($iso_codes); } if ($this->request->query('search', false)) { - Util::json_fail(Util::ERR_DISABLED, 'search disabled', !$this->context->query_option('search.enabled', false)); $href = $this->request->query('search.href'); $pattern = $this->request->query('search.pattern'); - $search = new Search($this->context); $response['search'] = $search->get_items($href, $pattern); } if ($this->request->query('thumbs', false)) { - Util::json_fail(Util::ERR_DISABLED, 'thumbnails disabled', !$this->context->query_option('thumbnails.enabled', false)); Util::json_fail(Util::ERR_UNSUPPORTED, 'thumbnails not supported', !$this->setup->get('HAS_PHP_JPEG')); $thumbs = $this->request->query_array('thumbs'); - $response['thumbs'] = $this->context->get_thumbs($thumbs); } @@ -116,13 +99,11 @@ class Api { } private function on_login() { - $pass = $this->request->query('pass'); Util::json_exit(['asAdmin' => $this->context->login_admin($pass)]); } private function on_logout() { - Util::json_exit(['asAdmin' => $this->context->logout_admin()]); } } diff --git a/src/_h5ai/private/php/core/class-context.php b/src/_h5ai/private/php/core/class-context.php index a83216a6..18fe487a 100644 --- a/src/_h5ai/private/php/core/class-context.php +++ b/src/_h5ai/private/php/core/class-context.php @@ -1,7 +1,6 @@ session = $session; $this->request = $request; $this->setup = $setup; @@ -25,64 +23,52 @@ class Context { } public function get_session() { - return $this->session; } 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_types() { - return Json::load($this->setup->get('CONF_PATH') . '/types.json'); } public function login_admin($pass) { - $this->session->set(Context::$AS_ADMIN_SESSION_KEY, strcasecmp(hash('sha512', $pass), $this->passhash) === 0); return $this->session->get(Context::$AS_ADMIN_SESSION_KEY); } public function logout_admin() { - $this->session->set(Context::$AS_ADMIN_SESSION_KEY, false); return $this->session->get(Context::$AS_ADMIN_SESSION_KEY); } public function is_admin() { - return $this->session->get(Context::$AS_ADMIN_SESSION_KEY); } public function is_api_request() { - return strtolower($this->setup->get('REQUEST_METHOD')) === 'post'; } public function is_info_request() { - return Util::starts_with($this->setup->get('REQUEST_HREF') . '/', $this->setup->get('PUBLIC_HREF')); } public function to_href($path, $trailing_slash = true) { - $rel_path = substr($path, strlen($this->setup->get('ROOT_PATH'))); $parts = explode('/', $rel_path); $encoded_parts = []; @@ -96,13 +82,11 @@ class Context { } public function to_path($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 if ($name === '.' || $name === '..') { return true; @@ -119,7 +103,6 @@ class Context { } public function read_dir($path) { - $names = []; if (is_dir($path)) { foreach (scandir($path) as $name) { @@ -137,12 +120,10 @@ class Context { } 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; } @@ -175,7 +156,6 @@ class Context { } public function get_current_path() { - $current_href = Util::normalize_path($this->setup->get('REQUEST_HREF'), true); $current_path = $this->to_path($current_href); @@ -187,7 +167,6 @@ class Context { } public function get_items($href, $what) { - if (!$this->is_managed_href($href)) { return []; } @@ -219,7 +198,6 @@ class Context { } public function get_langs() { - $langs = []; $l10n_path = $this->setup->get('CONF_PATH') . '/l10n'; if (is_dir($l10n_path)) { @@ -238,7 +216,6 @@ class Context { } public function get_l10n($iso_codes) { - $results = []; foreach ($iso_codes as $iso_code) { @@ -251,7 +228,6 @@ class Context { } public function get_thumbs($requests) { - $hrefs = []; foreach ($requests as $req) { @@ -263,7 +239,6 @@ class Context { } private function prefix_x_head_href($href) { - if (preg_match('@^(https?://|/)@i', $href)) { return $href; } @@ -272,7 +247,6 @@ class Context { } private function get_fonts_html() { - $fonts = $this->query_option('view.fonts', []); $fonts_mono = $this->query_option('view.fontsMono', []); @@ -292,7 +266,6 @@ class Context { } public function get_x_head_html() { - $scripts = $this->query_option('resources.scripts', []); $styles = $this->query_option('resources.styles', []); diff --git a/src/_h5ai/private/php/core/class-fallback.php b/src/_h5ai/private/php/core/class-fallback.php index 812f3d9d..7bd317fa 100644 --- a/src/_h5ai/private/php/core/class-fallback.php +++ b/src/_h5ai/private/php/core/class-fallback.php @@ -1,16 +1,13 @@ context = $context; } public function get_html($path = null) { - if (!$path) { $path = $this->context->get_current_path(); } diff --git a/src/_h5ai/private/php/core/class-filesize.php b/src/_h5ai/private/php/core/class-filesize.php index 6307902d..e9860a45 100644 --- a/src/_h5ai/private/php/core/class-filesize.php +++ b/src/_h5ai/private/php/core/class-filesize.php @@ -1,15 +1,12 @@ exec($cmdv); @@ -78,18 +71,15 @@ class Filesize { } public function du_dir($path) { - return $this->du_paths($this->read_dir($path)); } public function du_path($path) { - $sizes = $this->du_paths([$path]); return $sizes[$path]; } public function add($path) { - $size = 0; foreach ($this->read_dir($path) as $p) { $size += $this->filesize($p); diff --git a/src/_h5ai/private/php/core/class-item.php b/src/_h5ai/private/php/core/class-item.php index ca482427..2b9ce348 100644 --- a/src/_h5ai/private/php/core/class-item.php +++ b/src/_h5ai/private/php/core/class-item.php @@ -1,9 +1,7 @@ is_folder && !$item2->is_folder) { return -1; } @@ -15,7 +13,6 @@ class Item { } public static function get($context, $path, &$cache) { - if (!Util::starts_with($path, $context->get_setup()->get('ROOT_PATH'))) { return null; } @@ -41,7 +38,6 @@ class Item { public $is_content_fetched; private function __construct($context, $path) { - $this->context = $context; $this->path = Util::normalize_path($path, false); @@ -53,7 +49,6 @@ class Item { } public function to_json_object() { - $obj = [ 'href' => $this->href, 'time' => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript) @@ -69,7 +64,6 @@ 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, $this->context->get_setup()->get('ROOT_PATH'))) { return Item::get($this->context, $parent_path, $cache); @@ -78,7 +72,6 @@ class Item { } public function get_content(&$cache) { - $items = []; if (!$this->context->is_managed_href($this->href)) { diff --git a/src/_h5ai/private/php/core/class-json.php b/src/_h5ai/private/php/core/class-json.php index 06626f57..461f116c 100644 --- a/src/_h5ai/private/php/core/class-json.php +++ b/src/_h5ai/private/php/core/class-json.php @@ -1,12 +1,10 @@ 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) { @@ -22,20 +19,17 @@ class Request { } 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/private/php/core/class-session.php b/src/_h5ai/private/php/core/class-session.php index 4ad036a2..630d78f9 100644 --- a/src/_h5ai/private/php/core/class-session.php +++ b/src/_h5ai/private/php/core/class-session.php @@ -1,24 +1,19 @@ store = &$store; } public function set($key, $value) { - $key = Session::$KEY_PREFIX . $key; $this->store[$key] = $value; } public function get($key, $default = null) { - $key = Session::$KEY_PREFIX . $key; return array_key_exists($key, $this->store) ? $this->store[$key] : $default; } diff --git a/src/_h5ai/private/php/core/class-setup.php b/src/_h5ai/private/php/core/class-setup.php index 8743dee9..d0ae1693 100644 --- a/src/_h5ai/private/php/core/class-setup.php +++ b/src/_h5ai/private/php/core/class-setup.php @@ -1,12 +1,10 @@ store = []; $this->refresh = $refresh; @@ -19,7 +17,6 @@ class Setup { } private function set($key, $value) { - if (array_key_exists($key, $this->store)) { Logger::log('setup key already taken', [ 'key' => $key, @@ -40,7 +37,6 @@ class Setup { } public function get($key) { - if (!array_key_exists($key, $this->store)) { Logger::log('setup key not found', ['key' => $key]); exit; @@ -50,7 +46,6 @@ class Setup { } private function add_globals_and_envs() { - $this->set('PHP_VERSION', PHP_VERSION); $this->set('MIN_PHP_VERSION', MIN_PHP_VERSION); @@ -61,7 +56,6 @@ class Setup { } private function add_php_checks() { - $this->set('HAS_PHP_EXIF', function_exists('exif_thumbnail')); $has_php_jpeg = false; @@ -73,14 +67,12 @@ class Setup { } private function add_app_metadata() { - $this->set('NAME', 'h5ai'); $this->set('VERSION', H5AI_VERSION); $this->set('FILE_PREFIX', '_h5ai'); } private function add_server_metadata_and_check() { - $server_software = $this->get('SERVER_SOFTWARE'); $server_name = null; $server_version = null; @@ -96,7 +88,6 @@ class Setup { } private function add_paths() { - $script_name = $this->get('SCRIPT_NAME'); if ($this->get('SERVER_NAME') === 'lighttpd') { $script_name = preg_replace('#^.*?//#', '/', $script_name); @@ -123,7 +114,6 @@ class Setup { } private function add_sys_cmd_checks() { - $cmds_cache_path = Util::normalize_path($this->get('CACHE_PRV_PATH') . '/cmds.json', false); $cmds = Json::load($cmds_cache_path); @@ -150,7 +140,6 @@ class Setup { } public function to_jsono($as_admin = false) { - $keys = [ 'PUBLIC_HREF', 'ROOT_HREF' diff --git a/src/_h5ai/private/php/core/class-theme.php b/src/_h5ai/private/php/core/class-theme.php index f6549f89..8fcaddd9 100644 --- a/src/_h5ai/private/php/core/class-theme.php +++ b/src/_h5ai/private/php/core/class-theme.php @@ -1,18 +1,14 @@ context = $context; } public function get_icons() { - $public_path = $this->context->get_setup()->get('PUBLIC_PATH'); $theme = $this->context->query_option('view.theme', '-NONE-'); $theme_path = $public_path . '/images/themes/' . $theme; diff --git a/src/_h5ai/private/php/core/class-util.php b/src/_h5ai/private/php/core/class-util.php index fbc6a9e1..a1c2d36e 100644 --- a/src/_h5ai/private/php/core/class-util.php +++ b/src/_h5ai/private/php/core/class-util.php @@ -1,7 +1,6 @@ $err, 'msg' => $msg]); } } public static function array_query($array, $keypath = '', $default = Util::NO_DEFAULT) { - $value = $array; $keys = array_filter(explode('.', $keypath)); @@ -46,30 +41,25 @@ class Util { } public static function starts_with($sequence, $head) { - return substr($sequence, 0, strlen($head)) === $head; } public static function ends_with($sequence, $tail) { - $len = strlen($tail); return $len === 0 ? true : substr($sequence, -$len) === $tail; } public static function wrap_pattern($pattern) { - return Util::RE_DELIMITER . str_replace(Util::RE_DELIMITER, '\\' . Util::RE_DELIMITER, $pattern) . Util::RE_DELIMITER; } public static function passthru_cmd($cmd) { - $rc = null; passthru($cmd, $rc); return $rc; } public static function exec_cmdv($cmdv) { - if (!is_array($cmdv)) { $cmdv = func_get_args(); } @@ -82,7 +72,6 @@ class Util { } public static function exec_0($cmd) { - $lines = []; $rc = null; try { @@ -95,7 +84,6 @@ class Util { private static $size_cache = []; public static function filesize($context, $path) { - if (array_key_exists($path, Util::$size_cache)) { return Util::$size_cache[$path]; } diff --git a/src/_h5ai/private/php/ext/class-archive.php b/src/_h5ai/private/php/ext/class-archive.php index b23b351a..e143e070 100644 --- a/src/_h5ai/private/php/ext/class-archive.php +++ b/src/_h5ai/private/php/ext/class-archive.php @@ -1,7 +1,6 @@ context = $context; } public function output($type, $base_href, $hrefs) { - $this->base_path = $this->context->to_path($base_href); if (!$this->context->is_managed_path($this->base_path)) { return false; @@ -39,22 +36,16 @@ class Archive { } if ($type === 'php-tar') { - return $this->php_tar($this->dirs, $this->files); - } else if ($type === 'shell-tar') { - return $this->shell_cmd(Archive::$TAR_PASSTHRU_CMD); - } else if ($type === 'shell-zip') { - return $this->shell_cmd(Archive::$ZIP_PASSTHRU_CMD); } return false; } private function shell_cmd($cmd) { - $cmd = str_replace('[ROOTDIR]', escapeshellarg($this->base_path), $cmd); $cmd = str_replace('[DIRS]', count($this->dirs) ? implode(' ', array_map('escapeshellarg', $this->dirs)) : '', $cmd); $cmd = str_replace('[FILES]', count($this->files) ? implode(' ', array_map('escapeshellarg', $this->files)) : '', $cmd); @@ -67,11 +58,9 @@ class Archive { } private function php_tar($dirs, $files) { - $filesizes = []; $total_size = 512 * count($dirs); foreach (array_keys($files) as $real_file) { - $size = filesize($real_file); $filesizes[$real_file] = $size; @@ -84,11 +73,10 @@ class Archive { header('Content-Length: ' . $total_size); foreach ($dirs as $real_dir => $archived_dir) { - echo $this->php_tar_header($archived_dir, 0, @filemtime($real_dir . DIRECTORY_SEPARATOR . "."), 5); } - foreach ($files as $real_file => $archived_file) { + foreach ($files as $real_file => $archived_file) { $size = $filesizes[$real_file]; echo $this->php_tar_header($archived_file, $size, @filemtime($real_file), 0); @@ -103,7 +91,6 @@ class Archive { } private function php_tar_header($filename, $size, $mtime, $type) { - $name = substr(basename($filename), -99); $prefix = substr(Util::normalize_path(dirname($filename)), -154); if ($prefix === '.') { @@ -136,7 +123,6 @@ class Archive { } private function print_file($file) { - // Send file content in segments to not hit PHP's memory limit (default: 128M) if ($fd = fopen($file, 'rb')) { while (!feof($fd)) { @@ -149,9 +135,7 @@ class Archive { } private function add_hrefs($hrefs) { - foreach ($hrefs as $href) { - if (trim($href) === '') { continue; } @@ -174,20 +158,17 @@ class Archive { } private function add_file($real_file, $archived_file) { - if (is_readable($real_file)) { $this->files[$real_file] = $archived_file; } } private function add_dir($real_dir, $archived_dir) { - if ($this->context->is_managed_path($real_dir)) { $this->dirs[$real_dir] = $archived_dir; $files = $this->context->read_dir($real_dir); foreach ($files as $file) { - $real_file = $real_dir . '/' . $file; $archived_file = $archived_dir . '/' . $file; diff --git a/src/_h5ai/private/php/ext/class-custom.php b/src/_h5ai/private/php/ext/class-custom.php index 0588b11d..ab1628ed 100644 --- a/src/_h5ai/private/php/ext/class-custom.php +++ b/src/_h5ai/private/php/ext/class-custom.php @@ -1,18 +1,14 @@ context = $context; } private function read_custom_file($path, $name, &$content, &$type) { - $file_prefix = $this->context->get_setup()->get('FILE_PREFIX'); foreach (Custom::$EXTENSIONS as $ext) { @@ -26,7 +22,6 @@ class Custom { } public function get_customizations($href) { - if (!$this->context->query_option('custom.enabled', false)) { return [ 'header' => ['content' => null, 'type' => null], @@ -46,14 +41,12 @@ class Custom { $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; } diff --git a/src/_h5ai/private/php/ext/class-search.php b/src/_h5ai/private/php/ext/class-search.php index 8d58eeb9..0477ac05 100644 --- a/src/_h5ai/private/php/ext/class-search.php +++ b/src/_h5ai/private/php/ext/class-search.php @@ -1,16 +1,13 @@ context = $context; } public function get_paths($root, $pattern = null) { - $paths = []; if ($pattern && $this->context->is_managed_path($root)) { $re = Util::wrap_pattern($pattern); @@ -29,12 +26,10 @@ class Search { } public function get_items($href, $pattern = null) { - $cache = []; $root = $this->context->to_path($href); $paths = $this->get_paths($root, $pattern); $items = array_map(function ($path) { - return Item::get($this->context, $path, $cache)->to_json_object(); }, $paths); return $items; diff --git a/src/_h5ai/private/php/ext/class-thumb.php b/src/_h5ai/private/php/ext/class-thumb.php index 71c3bbf7..f4504a0a 100644 --- a/src/_h5ai/private/php/ext/class-thumb.php +++ b/src/_h5ai/private/php/ext/class-thumb.php @@ -1,7 +1,6 @@ context = $context; $this->setup = $context->get_setup(); $this->thumbs_path = $this->setup->get('CACHE_PUB_PATH') . '/' . Thumb::$THUMB_CACHE; @@ -26,7 +24,6 @@ class Thumb { } public function thumb($type, $source_href, $width, $height) { - $source_path = $this->context->to_path($source_href); if (!file_exists($source_path) || Util::starts_with($source_path, $this->setup->get('CACHE_PUB_PATH'))) { return null; @@ -53,7 +50,6 @@ class Thumb { } private function thumb_href($source_path, $width, $height) { - if (!file_exists($source_path)) { return null; } @@ -63,7 +59,6 @@ class Thumb { $thumb_href = $this->thumbs_href . '/' . $name; if (!file_exists($thumb_path) || filemtime($source_path) >= filemtime($thumb_path)) { - $image = new Image(); $et = false; @@ -86,7 +81,6 @@ class Thumb { } private function capture($cmdv, $source_path) { - if (!file_exists($source_path)) { return null; } @@ -94,7 +88,6 @@ class Thumb { $capture_path = $this->thumbs_path . '/capture-' . sha1($source_path) . '.jpg'; if (!file_exists($capture_path) || filemtime($source_path) >= filemtime($capture_path)) { - foreach ($cmdv as &$arg) { $arg = str_replace('[SRC]', $source_path, $arg); $arg = str_replace('[DEST]', $capture_path, $arg); @@ -108,7 +101,6 @@ class Thumb { } class Image { - private $source_file; private $source; private $width; @@ -117,7 +109,6 @@ class Image { private $dest; public function __construct($filename = null) { - $this->source_file = null; $this->source = null; $this->width = null; @@ -130,13 +121,11 @@ class Image { } public function __destruct() { - $this->release_source(); $this->release_dest(); } public function set_source($filename) { - $this->release_source(); $this->release_dest(); @@ -160,7 +149,6 @@ class Image { } public function save_dest_jpeg($filename, $quality = 80) { - if (!is_null($this->dest)) { @imagejpeg($this->dest, $filename, $quality); @chmod($filename, 0775); @@ -168,7 +156,6 @@ class Image { } public function release_dest() { - if (!is_null($this->dest)) { @imagedestroy($this->dest); $this->dest = null; @@ -176,7 +163,6 @@ class Image { } public function release_source() { - if (!is_null($this->source)) { @imagedestroy($this->source); $this->source_file = null; @@ -188,7 +174,6 @@ class Image { } public function thumb($width, $height) { - if (is_null($this->source)) { return; } @@ -233,7 +218,6 @@ class Image { } public function rotate($angle) { - if (is_null($this->source) || ($angle !== 90 && $angle !== 180 && $angle !== 270)) { return; } @@ -245,7 +229,6 @@ class Image { } public function normalize_exif_orientation($exif_source_file = null) { - if (is_null($this->source) || !function_exists('exif_read_data')) { return; }