From bd65025a4c4c194c2ff3003a45e5b6f2e90f69e4 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sat, 6 Oct 2012 18:25:59 +0200 Subject: [PATCH] More PHP refactorings. 'cs' lang update. --- src/_h5ai/conf/l10n/cs.json | 17 ++-- src/_h5ai/server/aai/header.html.jade | 14 ++-- src/_h5ai/server/php/inc/App.php | 70 +++++++++-------- src/_h5ai/server/php/inc/Entry.php | 108 +++++++++++--------------- src/_h5ai/server/php/index.php.jade | 27 +++---- 5 files changed, 113 insertions(+), 123 deletions(-) diff --git a/src/_h5ai/conf/l10n/cs.json b/src/_h5ai/conf/l10n/cs.json index 6162436c..464cf755 100644 --- a/src/_h5ai/conf/l10n/cs.json +++ b/src/_h5ai/conf/l10n/cs.json @@ -1,16 +1,19 @@ { "lang": "čeština", - "details": "podrobnosti", - "icons": "ikony", + "details": "Podrobnosti", + "list": "Dlaždice", + "grid": "Seznam", + "icons": "Velké ikony", "name": "Název", - "lastModified": "Upraveno", + "lastModified": "Datum změny", "size": "Velikost", "parentDirectory": "Nadřazený adresář", - "empty": "prázdný", + "empty": "Prázdná složka", "folders": "složek", "files": "souborů", - "download": "stáhnout", - "noMatch": "žádná shoda", + "download": "Stáhnout", + "noMatch": "Žádná shoda", "dateFormat": "DD.MM.YYYY HH:mm", - "filter": "filtr" + "filter": "Filtr", + "delete": "Odstranit" } \ No newline at end of file diff --git a/src/_h5ai/server/aai/header.html.jade b/src/_h5ai/server/aai/header.html.jade index 6561fa21..f8835134 100644 --- a/src/_h5ai/server/aai/header.html.jade +++ b/src/_h5ai/server/aai/header.html.jade @@ -1,5 +1,5 @@ -- var appHref = "/_{{pkg.name}}/client" +- var href = "/_{{pkg.name}}/" doctype 5 //if lt IE 9 @@ -11,14 +11,14 @@ doctype 5 head meta( charset="utf-8" ) meta( http-equiv="X-UA-Compatible", content="IE=edge,chrome=1" ) - title Directory index · styled with {{pkg.name}} {{pkg.version}} - meta( name="description", content="Directory index styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}})" ) + title index · styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}}) + meta( name="description", content="index styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}})" ) meta( name="viewport", content="width=device-width" ) - link( rel="shortcut icon", href="#{appHref}/images/app-16x16.ico" ) - link( rel="apple-touch-icon", type="image/png", href="#{appHref}/images/app-48x48.png" ) + link( rel="shortcut icon", href="#{href}client/images/app-16x16.ico" ) + link( rel="apple-touch-icon", type="image/png", href="#{href}client/images/app-48x48.png" ) link( rel="stylesheet", href="//fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic,700italic|Ubuntu:400,700,400italic,700italic" ) - link( rel="stylesheet", href="#{appHref}/css/styles.css" ) - script( src="#{appHref}/js/scripts.js" ) + link( rel="stylesheet", href="#{href}client/css/styles.css" ) + script( src="#{href}client/js/scripts.js" ) | diff --git a/src/_h5ai/server/php/inc/App.php b/src/_h5ai/server/php/inc/App.php index 7c3356ae..be2f21db 100644 --- a/src/_h5ai/server/php/inc/App.php +++ b/src/_h5ai/server/php/inc/App.php @@ -80,7 +80,9 @@ class App { $parts = explode("/", $abs_path); $encoded_parts = array(); foreach ($parts as $part) { - $encoded_parts[] = rawurlencode($part); + if ($part) { + $encoded_parts[] = rawurlencode($part); + } } return normalize_path($this->root_abs_href . implode("/", $encoded_parts), $trailing_slash); @@ -147,7 +149,7 @@ class App { return 200; } } - return "h5ai"; + return App::$MAGIC_SEQUENCE; } @@ -189,27 +191,30 @@ class App { public function get_entries($abs_href, $what) { - $folder = Entry::get($this, $this->get_abs_path($abs_href), $abs_href); + $cache = array(); + $folder = Entry::get($this, $this->get_abs_path($abs_href), $cache); - if ($what > 1 && $folder !== null) { - foreach ($folder->getContent() as $entry) { - $entry->getContent(); + // add content of subfolders + if ($what >= 2 && $folder !== null) { + foreach ($folder->get_content($cache) as $entry) { + $entry->get_content($cache); } - $folder = $folder->getParent(); + $folder = $folder->get_parent($cache); } - while ($what > 0 && $folder !== null) { - $folder->getContent(); - $folder = $folder->getParent(); - } - Entry::sort(); - - $entries = array(); - foreach (Entry::get_cache() as $entry) { - $entries[] = $entry->toJsonObject(true); + // add content of this folder and all parent folders + while ($what >= 1 && $folder !== null) { + $folder->get_content($cache); + $folder = $folder->get_parent($cache); } - return $entries; + uasort($cache, array("Entry", "cmp")); + $result = array(); + foreach ($cache as $p => $entry) { + $result[] = $entry->to_json_object(); + } + + return $result; } @@ -217,31 +222,32 @@ class App { date_default_timezone_set("UTC"); - function _cmp_no_js_fallback($entry1, $entry2) { + // function _cmp_no_js_fallback($entry1, $entry2) { - if ($entry1->isFolder && !$entry2->isFolder) { - return -1; - } - if (!$entry1->isFolder && $entry2->isFolder) { - return 1; - } + // if ($entry1->isFolder && !$entry2->isFolder) { + // return -1; + // } + // if (!$entry1->isFolder && $entry2->isFolder) { + // return 1; + // } - return strcasecmp($entry1->absHref, $entry2->absHref); - } + // return strcasecmp($entry1->abs_href, $entry2->abs_href); + // } - $folder = Entry::get($this, $this->abs_path, $this->abs_href); - $entries = $folder->getContent(); - uasort($entries, "_cmp_no_js_fallback"); + $cache = array(); + $folder = Entry::get($this, $this->abs_path, $cache); + $entries = $folder->get_content($cache); + uasort($entries, array("Entry", "cmp")); $html = ""; $html .= ""; - if ($folder->parent) { + if ($folder->get_parent($cache)) { $html .= ""; } foreach ($entries as $entry) { $html .= ""; - $html .= ""; - $html .= ""; + $html .= ""; + $html .= ""; $html .= ""; $html .= ""; $html .= ""; diff --git a/src/_h5ai/server/php/inc/Entry.php b/src/_h5ai/server/php/inc/Entry.php index 5c50ec4a..bd36afc1 100644 --- a/src/_h5ai/server/php/inc/Entry.php +++ b/src/_h5ai/server/php/inc/Entry.php @@ -4,119 +4,105 @@ class Entry { private static $FOLDER_SIZE_CMD = "du -sb \"[DIR]\""; + public static function cmp($entry1, $entry2) { - private static $cache = array(); - - - public static function get_cache() { - - return Entry::$cache; + return strcasecmp($entry1->abs_path, $entry2->abs_path); } + public static function get($app, $abs_path, &$cache) { - public static function get($h5ai, $absPath, $absHref) { - - if (!starts_with($absHref, $h5ai->get_root_abs_href())) { - error_log("ILLEGAL REQUEST: " . $absHref . ", " . $absPath . ", " . $h5ai->get_root_abs_href()); + if (!starts_with($abs_path, $app->get_root_abs_path())) { + error_log("ILLEGAL REQUEST: " . $abs_path . ", " . $app->get_root_abs_path()); return null; } - if (array_key_exists($absHref, Entry::$cache)) { - return Entry::$cache[$absHref]; + if (is_array($cache) && array_key_exists($abs_path, $cache)) { + return $cache[$abs_path]; } - return new Entry($h5ai, $absPath, $absHref); + $entry = new Entry($app, $abs_path); + + if (is_array($cache)) { + $cache[$abs_path] = $entry; + } + return $entry; } - public static function sort() { - - function cmp($entry1, $entry2) { - - return strcasecmp($entry1->absHref, $entry2->absHref); - } - - uasort(Entry::$cache, "cmp"); - } + public $app, + $abs_path, $abs_href, + $date, $size, + $is_folder, + $is_content_fetched; + private function __construct($app, $abs_path) { + $this->app = $app; - public $h5ai, $absPath, $absHref, $date, $size, $isFolder, $parent, $isContentFetched; + $this->abs_path = normalize_path($abs_path); + $this->is_folder = is_dir($this->abs_path); + $this->abs_href = $this->app->get_abs_href($abs_path, $this->is_folder); + $this->date = filemtime($this->abs_path); - private function __construct($h5ai, $absPath, $absHref) { - - $this->h5ai = $h5ai; - - $this->absPath = normalize_path($absPath); - - $this->isFolder = is_dir($this->absPath); - $this->absHref = normalize_path($absHref, $this->isFolder); - - $this->date = filemtime($this->absPath); - - if ($this->isFolder) { + if ($this->is_folder) { $this->size = null; - $options = $h5ai->get_options(); + $options = $app->get_options(); if ($options["foldersize"]["enabled"]) { - $cmd = str_replace("[DIR]", $this->absPath, Entry::$FOLDER_SIZE_CMD); + $cmd = str_replace("[DIR]", $this->abs_path, Entry::$FOLDER_SIZE_CMD); $this->size = intval(preg_replace("/\s.*$/", "", `$cmd`), 10); } } else { - $this->size = filesize($this->absPath); + $this->size = filesize($this->abs_path); } - $this->parent = null; - $parentAbsHref = normalize_path(dirname($this->absHref), true); - if ($this->absHref !== "/" && starts_with($parentAbsHref, $h5ai->get_root_abs_href())) { - $this->parent = Entry::get($this->h5ai, normalize_path(dirname($this->absPath)), $parentAbsHref); - } - - $this->isContentFetched = false; - - Entry::$cache[$this->absHref] = $this; + $this->is_content_fetched = false; } - public function toJsonObject($withStatus) { + public function to_json_object() { $obj = array( - "absHref" => $this->absHref, - "time" => ($this->date * 1000), + "absHref" => $this->abs_href, + "time" => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript) "size" => $this->size ); - if ($withStatus && $this->isFolder) { - $obj["status"] = $this->h5ai->get_http_code($this->absHref); - $obj["content"] = $this->isContentFetched; + if ($this->is_folder) { + $obj["status"] = $this->app->get_http_code($this->abs_href); + $obj["content"] = $this->is_content_fetched; } return $obj; } - public function getParent() { + public function get_parent(&$cache) { - return $this->parent; + $parentAbsPath = normalize_path(dirname($this->abs_path)); + if (starts_with($parentAbsPath, $this->app->get_root_abs_path())) { + return Entry::get($this->app, $parentAbsPath, $cache); + } + return null; } - public function getContent() { + public function get_content(&$cache) { $content = array(); - if ($this->h5ai->get_http_code($this->absHref) !== "h5ai") { + if ($this->app->get_http_code($this->abs_href) !== App::$MAGIC_SEQUENCE) { return $content; } - $files = $this->h5ai->read_dir($this->absPath); + $files = $this->app->read_dir($this->abs_path); foreach ($files as $file) { - $entry = Entry::get($this->h5ai, $this->absPath . "/" . $file, $this->absHref . rawurlencode($file)); - $content[$entry->absPath] = $entry; + $entry = Entry::get($this->app, $this->abs_path . "/" . $file, $cache); + $content[$entry->abs_path] = $entry; } - $this->isContentFetched = true; + $this->is_content_fetched = true; return $content; } diff --git a/src/_h5ai/server/php/index.php.jade b/src/_h5ai/server/php/index.php.jade index e8166069..a8759ad5 100644 --- a/src/_h5ai/server/php/index.php.jade +++ b/src/_h5ai/server/php/index.php.jade @@ -1,14 +1,9 @@ -|get_app_abs_href(); -| $is_head_request = stripos($_SERVER["REQUEST_METHOD"], "HEAD"); -|?> +| -- var appHref = "client" -- var json = "get_generic_json(); }?>" -- var fallback = "get_no_js_fallback(); }?>" -- var config = "get_custom_config(); }?>" +- var href = "get_app_abs_href(); ?>" +- var json = "get_generic_json(); ?>" +- var fallback = "get_no_js_fallback(); ?>" +- var config = "get_custom_config(); ?>" doctype 5 //if lt IE 9 @@ -20,14 +15,14 @@ html.no-js( lang="en" ) head meta( charset="utf-8" ) meta( http-equiv="X-UA-Compatible", content="IE=edge,chrome=1" ) - title Directory index · styled with {{pkg.name}} {{pkg.version}} - meta( name="description", content="Directory index styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}})" ) + title index · styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}}) + meta( name="description", content="index styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}})" ) meta( name="viewport", content="width=device-width" ) - link( rel="shortcut icon", href!="#{appHref}/images/app-16x16.ico" ) - link( rel="apple-touch-icon", type="image/png", href!="#{appHref}/images/app-48x48.png" ) + link( rel="shortcut icon", href!="#{href}client/images/app-16x16.ico" ) + link( rel="apple-touch-icon", type="image/png", href!="#{href}client/images/app-48x48.png" ) link( rel="stylesheet", href="//fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic,700italic|Ubuntu:400,700,400italic,700italic" ) - link( rel="stylesheet", href!="#{appHref}/css/styles.css" ) - script( src!="#{appHref}/js/scripts.js", data-config!="#{config}" ) + link( rel="stylesheet", href!="#{href}client/css/styles.css" ) + script( src!="#{href}client/js/scripts.js", data-config!="#{config}" ) body#h5ai-main
NameLast modifiedSize
Parent Directory
absHref . "\">" . basename($entry->absPath) . ($entry->isFolder ? "/" : "") . "" . ($entry->is_folder ? "[D]" : "[F]") . "abs_href . "\">" . basename($entry->abs_path) . ($entry->is_folder ? "/" : "") . "" . date("Y-m-d H:i", $entry->date) . "" . ($entry->size !== null ? intval($entry->size / 1000) . " KB" : "" ) . "