Refactor PHP. Switch to explicit string literals where possible.

This commit is contained in:
Lars Jung 2015-05-11 12:40:33 +02:00
parent 0719eeeb90
commit 1e11b804ab
5 changed files with 55 additions and 55 deletions

View file

@ -10,7 +10,7 @@ class App {
$this->request = $request; $this->request = $request;
$this->setup = $setup; $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() { public function get_request() {
@ -28,28 +28,28 @@ class App {
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_types() { 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) { public function login_admin($pass) {
$key = $this->setup->get("AS_ADMIN_SESSION_KEY"); $key = $this->setup->get('AS_ADMIN_SESSION_KEY');
$hash = $this->setup->get("PASSHASH"); $hash = $this->setup->get('PASSHASH');
$_SESSION[$key] = strcasecmp(hash("sha512", $pass), $hash) === 0; $_SESSION[$key] = strcasecmp(hash('sha512', $pass), $hash) === 0;
return $_SESSION[$key]; return $_SESSION[$key];
} }
public function logout_admin() { public function logout_admin() {
$key = $this->setup->get("AS_ADMIN_SESSION_KEY"); $key = $this->setup->get('AS_ADMIN_SESSION_KEY');
$_SESSION[$key] = false; $_SESSION[$key] = false;
return $_SESSION[$key]; return $_SESSION[$key];
@ -57,32 +57,32 @@ class App {
public function to_href($path, $trailing_slash = true) { public function to_href($path, $trailing_slash = true) {
$rel_path = substr($path, strlen($this->setup->get("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) {
if ($part != "") { if ($part != '') {
$encoded_parts[] = rawurlencode($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) { public function to_path($href) {
$rel_href = substr($href, strlen($this->setup->get("ROOT_HREF"))); $rel_href = substr($href, strlen($this->setup->get('ROOT_HREF')));
return Util::normalize_path($this->setup->get("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
if ($name === "." || $name === "..") { if ($name === '.' || $name === '..') {
return true; return true;
} }
foreach ($this->query_option("view.hidden", []) as $re) { foreach ($this->query_option('view.hidden', []) as $re) {
$re = Util::wrap_pattern($re); $re = Util::wrap_pattern($re);
if (preg_match($re, $name)) { if (preg_match($re, $name)) {
return true; return true;
@ -100,7 +100,7 @@ class App {
if ( if (
$this->is_hidden($name) $this->is_hidden($name)
|| $this->is_hidden($this->to_href($path) . $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; continue;
} }
@ -121,18 +121,18 @@ class App {
return false; 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; return false;
} }
foreach ($this->query_option("view.unmanaged", []) as $name) { foreach ($this->query_option('view.unmanaged', []) as $name) {
if (file_exists($path . "/" . $name)) { if (file_exists($path . '/' . $name)) {
return false; return false;
} }
} }
while ($path !== $this->setup->get("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;
} }
$parent_path = Util::normalize_path(dirname($path)); $parent_path = Util::normalize_path(dirname($path));
@ -167,7 +167,7 @@ class App {
$folder = $folder->get_parent($cache); $folder = $folder->get_parent($cache);
} }
uasort($cache, ["Item", "cmp"]); uasort($cache, ['Item', 'cmp']);
$result = []; $result = [];
foreach ($cache as $p => $item) { foreach ($cache as $p => $item) {
$result[] = $item->to_json_object(); $result[] = $item->to_json_object();
@ -179,13 +179,13 @@ class App {
public function get_langs() { public function get_langs() {
$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 (is_dir($l10n_path)) {
if ($dir = opendir($l10n_path)) { if ($dir = opendir($l10n_path)) {
while (($file = readdir($dir)) !== false) { while (($file = readdir($dir)) !== false) {
if (Util::ends_with($file, ".json")) { if (Util::ends_with($file, '.json')) {
$translations = Util::load_commented_json($l10n_path . "/" . $file); $translations = Util::load_commented_json($l10n_path . '/' . $file);
$langs[basename($file, ".json")] = $translations["lang"]; $langs[basename($file, '.json')] = $translations['lang'];
} }
} }
closedir($dir); closedir($dir);
@ -200,9 +200,9 @@ class App {
$results = []; $results = [];
foreach ($iso_codes as $iso_code) { 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] = Util::load_commented_json($file);
$results[$iso_code]["isoCode"] = $iso_code; $results[$iso_code]['isoCode'] = $iso_code;
} }
return $results; return $results;
@ -214,7 +214,7 @@ class App {
foreach ($requests as $req) { foreach ($requests as $req) {
$thumb = new Thumb($this); $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; return $hrefs;

View file

@ -9,8 +9,8 @@ class Fallback {
private function get_current_path() { private function get_current_path() {
$uri_parts = parse_url(getenv("REQUEST_URI")); $uri_parts = parse_url(getenv('REQUEST_URI'));
$current_href = Util::normalize_path($uri_parts["path"], true); $current_href = Util::normalize_path($uri_parts['path'], true);
$current_path = $this->app->to_path($current_href); $current_path = $this->app->to_path($current_href);
if (!is_dir($current_path)) { if (!is_dir($current_path)) {
@ -26,12 +26,12 @@ class Fallback {
$path = $this->get_current_path(); $path = $this->get_current_path();
} }
$app_href = $this->app->get_setup()->get("APP_HREF"); $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);
uasort($items, ["Item", "cmp"]); uasort($items, ['Item', 'cmp']);
$html = "<table>"; $html = "<table>";
@ -52,13 +52,13 @@ class Fallback {
} }
foreach ($items as $item) { foreach ($items as $item) {
$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>";
$html .= "</tr>"; $html .= "</tr>";
} }

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, $app->get_setup()->get("ROOT_PATH"))) { if (!Util::starts_with($path, $app->get_setup()->get('ROOT_PATH'))) {
return null; return null;
} }
@ -55,14 +55,14 @@ class Item {
public function to_json_object() { public function to_json_object() {
$obj = [ $obj = [
"href" => $this->href, 'href' => $this->href,
"time" => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript) 'time' => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript)
"size" => $this->size 'size' => $this->size
]; ];
if ($this->is_folder) { if ($this->is_folder) {
$obj["managed"] = $this->app->is_managed_href($this->href); $obj['managed'] = $this->app->is_managed_href($this->href);
$obj["fetched"] = $this->is_content_fetched; $obj['fetched'] = $this->is_content_fetched;
} }
return $obj; return $obj;
@ -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, $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 Item::get($this->app, $parent_path, $cache);
} }
return null; return null;
@ -89,7 +89,7 @@ class Item {
$files = $this->app->read_dir($this->path); $files = $this->app->read_dir($this->path);
foreach ($files as $file) { 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; $items[$item->path] = $item;
} }

View file

@ -9,8 +9,8 @@ class Logger {
self::$start = self::time(); self::$start = self::time();
self::$prev = self::$start; self::$prev = self::$start;
register_shutdown_function(function () { Logger::log("shutdown"); }); register_shutdown_function(function () { Logger::log('shutdown'); });
Logger::log("--------------------------------"); Logger::log('--------------------------------');
} }
private static function time() { private static function time() {
@ -21,9 +21,9 @@ class Logger {
public static function log($message=null, $obj=null) { public static function log($message=null, $obj=null) {
$now = self::time(); $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; self::$prev = $now;
} }

View file

@ -9,35 +9,35 @@ class Request {
$this->params = $params; $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); $value = Util::array_query($this->params, $keypath, Util::NO_DEFAULT);
if ($value === 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 $default;
} }
return $value; 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); $value = $this->query($keypath, $default);
return filter_var($value, FILTER_VALIDATE_BOOLEAN); 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); $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); 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); $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; return $value;
} }
} }