From 1e11b804abc993c1c67b7713ed2679b4dfb005dc Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Mon, 11 May 2015 12:40:33 +0200 Subject: [PATCH] Refactor PHP. Switch to explicit string literals where possible. --- src/_h5ai/server/php/inc/core/class-app.php | 58 +++++++++---------- .../server/php/inc/core/class-fallback.php | 14 ++--- src/_h5ai/server/php/inc/core/class-item.php | 16 ++--- .../server/php/inc/core/class-logger.php | 8 +-- .../server/php/inc/core/class-request.php | 14 ++--- 5 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/_h5ai/server/php/inc/core/class-app.php b/src/_h5ai/server/php/inc/core/class-app.php index e007fc1f..d5166539 100644 --- a/src/_h5ai/server/php/inc/core/class-app.php +++ b/src/_h5ai/server/php/inc/core/class-app.php @@ -10,7 +10,7 @@ class App { $this->request = $request; $this->setup = $setup; - $this->options = Util::load_commented_json($this->setup->get("APP_PATH") . "/conf/options.json"); + $this->options = Util::load_commented_json($this->setup->get('APP_PATH') . '/conf/options.json'); } public function get_request() { @@ -28,28 +28,28 @@ class App { 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); } public function get_types() { - return Util::load_commented_json($this->setup->get("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"); + $key = $this->setup->get('AS_ADMIN_SESSION_KEY'); + $hash = $this->setup->get('PASSHASH'); - $_SESSION[$key] = strcasecmp(hash("sha512", $pass), $hash) === 0; + $_SESSION[$key] = strcasecmp(hash('sha512', $pass), $hash) === 0; return $_SESSION[$key]; } public function logout_admin() { - $key = $this->setup->get("AS_ADMIN_SESSION_KEY"); + $key = $this->setup->get('AS_ADMIN_SESSION_KEY'); $_SESSION[$key] = false; return $_SESSION[$key]; @@ -57,32 +57,32 @@ class App { public function to_href($path, $trailing_slash = true) { - $rel_path = substr($path, strlen($this->setup->get("ROOT_PATH"))); - $parts = explode("/", $rel_path); + $rel_path = substr($path, strlen($this->setup->get('ROOT_PATH'))); + $parts = explode('/', $rel_path); $encoded_parts = []; foreach ($parts as $part) { - if ($part != "") { + if ($part != '') { $encoded_parts[] = rawurlencode($part); } } - return Util::normalize_path($this->setup->get("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($this->setup->get("ROOT_HREF"))); - return Util::normalize_path($this->setup->get("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 - if ($name === "." || $name === "..") { + if ($name === '.' || $name === '..') { return true; } - foreach ($this->query_option("view.hidden", []) as $re) { + foreach ($this->query_option('view.hidden', []) as $re) { $re = Util::wrap_pattern($re); if (preg_match($re, $name)) { return true; @@ -100,7 +100,7 @@ class App { if ( $this->is_hidden($name) || $this->is_hidden($this->to_href($path) . $name) - || (!is_readable($path .'/'. $name) && $this->query_option("view.hideIf403", false)) + || (!is_readable($path . '/' . $name) && $this->query_option('view.hideIf403', false)) ) { continue; } @@ -121,18 +121,18 @@ class App { return false; } - if ($path === $this->setup->get("APP_PATH") || strpos($path, $this->setup->get("APP_PATH") . '/') === 0) { + if ($path === $this->setup->get('APP_PATH') || strpos($path, $this->setup->get('APP_PATH') . '/') === 0) { return false; } - foreach ($this->query_option("view.unmanaged", []) as $name) { - if (file_exists($path . "/" . $name)) { + foreach ($this->query_option('view.unmanaged', []) as $name) { + if (file_exists($path . '/' . $name)) { return false; } } - while ($path !== $this->setup->get("ROOT_PATH")) { - if (@is_dir($path . "/_h5ai/server")) { + while ($path !== $this->setup->get('ROOT_PATH')) { + if (@is_dir($path . '/_h5ai/server')) { return false; } $parent_path = Util::normalize_path(dirname($path)); @@ -167,7 +167,7 @@ class App { $folder = $folder->get_parent($cache); } - uasort($cache, ["Item", "cmp"]); + uasort($cache, ['Item', 'cmp']); $result = []; foreach ($cache as $p => $item) { $result[] = $item->to_json_object(); @@ -179,13 +179,13 @@ class App { public function get_langs() { $langs = []; - $l10n_path = $this->setup->get("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) { - if (Util::ends_with($file, ".json")) { - $translations = Util::load_commented_json($l10n_path . "/" . $file); - $langs[basename($file, ".json")] = $translations["lang"]; + if (Util::ends_with($file, '.json')) { + $translations = Util::load_commented_json($l10n_path . '/' . $file); + $langs[basename($file, '.json')] = $translations['lang']; } } closedir($dir); @@ -200,9 +200,9 @@ class App { $results = []; foreach ($iso_codes as $iso_code) { - $file = $this->setup->get("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; + $results[$iso_code]['isoCode'] = $iso_code; } return $results; @@ -214,7 +214,7 @@ class App { foreach ($requests as $req) { $thumb = new Thumb($this); - $hrefs[] = $thumb->thumb($req["type"], $req["href"], $req["width"], $req["height"]); + $hrefs[] = $thumb->thumb($req['type'], $req['href'], $req['width'], $req['height']); } return $hrefs; diff --git a/src/_h5ai/server/php/inc/core/class-fallback.php b/src/_h5ai/server/php/inc/core/class-fallback.php index cdd30355..d59924eb 100644 --- a/src/_h5ai/server/php/inc/core/class-fallback.php +++ b/src/_h5ai/server/php/inc/core/class-fallback.php @@ -9,8 +9,8 @@ class Fallback { private function get_current_path() { - $uri_parts = parse_url(getenv("REQUEST_URI")); - $current_href = Util::normalize_path($uri_parts["path"], true); + $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)) { @@ -26,12 +26,12 @@ class Fallback { $path = $this->get_current_path(); } - $app_href = $this->app->get_setup()->get("APP_HREF"); + $app_href = $this->app->get_setup()->get('APP_HREF'); $cache = []; $folder = Item::get($this->app, $path, $cache); $items = $folder->get_content($cache); - uasort($items, ["Item", "cmp"]); + uasort($items, ['Item', 'cmp']); $html = ""; @@ -52,13 +52,13 @@ class Fallback { } foreach ($items as $item) { - $type = $item->is_folder ? "folder" : "file"; + $type = $item->is_folder ? 'folder' : 'file'; $html .= ""; $html .= ""; $html .= ""; - $html .= ""; - $html .= ""; + $html .= ""; + $html .= ""; $html .= ""; } diff --git a/src/_h5ai/server/php/inc/core/class-item.php b/src/_h5ai/server/php/inc/core/class-item.php index b1d87963..25eca2f8 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, $app->get_setup()->get("ROOT_PATH"))) { + if (!Util::starts_with($path, $app->get_setup()->get('ROOT_PATH'))) { return null; } @@ -55,14 +55,14 @@ class Item { public function to_json_object() { $obj = [ - "href" => $this->href, - "time" => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript) - "size" => $this->size + 'href' => $this->href, + 'time' => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript) + 'size' => $this->size ]; if ($this->is_folder) { - $obj["managed"] = $this->app->is_managed_href($this->href); - $obj["fetched"] = $this->is_content_fetched; + $obj['managed'] = $this->app->is_managed_href($this->href); + $obj['fetched'] = $this->is_content_fetched; } return $obj; @@ -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, $this->app->get_setup()->get("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; @@ -89,7 +89,7 @@ class Item { $files = $this->app->read_dir($this->path); foreach ($files as $file) { - $item = Item::get($this->app, $this->path . "/" . $file, $cache); + $item = Item::get($this->app, $this->path . '/' . $file, $cache); $items[$item->path] = $item; } diff --git a/src/_h5ai/server/php/inc/core/class-logger.php b/src/_h5ai/server/php/inc/core/class-logger.php index bbf8e1d6..062aacd3 100644 --- a/src/_h5ai/server/php/inc/core/class-logger.php +++ b/src/_h5ai/server/php/inc/core/class-logger.php @@ -9,8 +9,8 @@ class Logger { self::$start = self::time(); self::$prev = self::$start; - register_shutdown_function(function () { Logger::log("shutdown"); }); - Logger::log("--------------------------------"); + register_shutdown_function(function () { Logger::log('shutdown'); }); + Logger::log('--------------------------------'); } private static function time() { @@ -21,9 +21,9 @@ class Logger { public static function log($message=null, $obj=null) { $now = self::time(); - $message = number_format($now - self::$start, 3) . " " . number_format($now - self::$prev, 3) . " " . $message; + $message = number_format($now - self::$start, 3) . ' ' . number_format($now - self::$prev, 3) . ' ' . $message; - @error_log($message . " " . var_export($obj, true)); + @error_log($message . ' ' . var_export($obj, true)); self::$prev = $now; } diff --git a/src/_h5ai/server/php/inc/core/class-request.php b/src/_h5ai/server/php/inc/core/class-request.php index ba5cae9b..d8d74f7f 100644 --- a/src/_h5ai/server/php/inc/core/class-request.php +++ b/src/_h5ai/server/php/inc/core/class-request.php @@ -9,35 +9,35 @@ class Request { $this->params = $params; } - public function query($keypath = "", $default = Util::NO_DEFAULT) { + 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); + 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) { + 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) { + 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)); + 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) { + 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)); + Util::json_fail(Util::ERR_ILLIGAL_PARAM, 'parameter \'' . $keypath . '\' is no array', !is_array($value)); return $value; } }
${type}" . basename($item->path) . "" . date("Y-m-d H:i", $item->date) . "" . ($item->size !== null ? intval($item->size / 1000) . " KB" : "" ) . "" . date('Y-m-d H:i', $item->date) . "" . ($item->size !== null ? intval($item->size / 1000) . ' KB' : '' ) . "