More PHP refactorings. 'cs' lang update.

This commit is contained in:
Lars Jung 2012-10-06 18:25:59 +02:00
parent 42cfe55ef3
commit bd65025a4c
5 changed files with 113 additions and 123 deletions

View file

@ -1,16 +1,19 @@
{ {
"lang": "čeština", "lang": "čeština",
"details": "podrobnosti", "details": "Podrobnosti",
"icons": "ikony", "list": "Dlaždice",
"grid": "Seznam",
"icons": "Velké ikony",
"name": "Název", "name": "Název",
"lastModified": "Upraveno", "lastModified": "Datum změny",
"size": "Velikost", "size": "Velikost",
"parentDirectory": "Nadřazený adresář", "parentDirectory": "Nadřazený adresář",
"empty": "prázdný", "empty": "Prázdná složka",
"folders": "složek", "folders": "složek",
"files": "souborů", "files": "souborů",
"download": "stáhnout", "download": "Stáhnout",
"noMatch": "žádná shoda", "noMatch": "Žádná shoda",
"dateFormat": "DD.MM.YYYY HH:mm", "dateFormat": "DD.MM.YYYY HH:mm",
"filter": "filtr" "filter": "Filtr",
"delete": "Odstranit"
} }

View file

@ -1,5 +1,5 @@
- var appHref = "/_{{pkg.name}}/client" - var href = "/_{{pkg.name}}/"
doctype 5 doctype 5
//if lt IE 9 //if lt IE 9
@ -11,14 +11,14 @@ doctype 5
head head
meta( charset="utf-8" ) meta( charset="utf-8" )
meta( http-equiv="X-UA-Compatible", content="IE=edge,chrome=1" ) meta( http-equiv="X-UA-Compatible", content="IE=edge,chrome=1" )
title Directory index · styled with {{pkg.name}} {{pkg.version}} title index · styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}})
meta( name="description", content="Directory 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" ) meta( name="viewport", content="width=device-width" )
link( rel="shortcut icon", href="#{appHref}/images/app-16x16.ico" ) link( rel="shortcut icon", href="#{href}client/images/app-16x16.ico" )
link( rel="apple-touch-icon", type="image/png", href="#{appHref}/images/app-48x48.png" ) 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="//fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic,700italic|Ubuntu:400,700,400italic,700italic" )
link( rel="stylesheet", href="#{appHref}/css/styles.css" ) link( rel="stylesheet", href="#{href}client/css/styles.css" )
script( src="#{appHref}/js/scripts.js" ) script( src="#{href}client/js/scripts.js" )
|<body id="h5ai-main"> |<body id="h5ai-main">

View file

@ -80,7 +80,9 @@ class App {
$parts = explode("/", $abs_path); $parts = explode("/", $abs_path);
$encoded_parts = array(); $encoded_parts = array();
foreach ($parts as $part) { 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); return normalize_path($this->root_abs_href . implode("/", $encoded_parts), $trailing_slash);
@ -147,7 +149,7 @@ class App {
return 200; return 200;
} }
} }
return "h5ai"; return App::$MAGIC_SEQUENCE;
} }
@ -189,27 +191,30 @@ class App {
public function get_entries($abs_href, $what) { 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) { // add content of subfolders
foreach ($folder->getContent() as $entry) { if ($what >= 2 && $folder !== null) {
$entry->getContent(); foreach ($folder->get_content($cache) as $entry) {
$entry->get_content($cache);
} }
$folder = $folder->getParent(); $folder = $folder->get_parent($cache);
} }
while ($what > 0 && $folder !== null) { // add content of this folder and all parent folders
$folder->getContent(); while ($what >= 1 && $folder !== null) {
$folder = $folder->getParent(); $folder->get_content($cache);
} $folder = $folder->get_parent($cache);
Entry::sort();
$entries = array();
foreach (Entry::get_cache() as $entry) {
$entries[] = $entry->toJsonObject(true);
} }
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"); date_default_timezone_set("UTC");
function _cmp_no_js_fallback($entry1, $entry2) { // function _cmp_no_js_fallback($entry1, $entry2) {
if ($entry1->isFolder && !$entry2->isFolder) { // if ($entry1->isFolder && !$entry2->isFolder) {
return -1; // return -1;
} // }
if (!$entry1->isFolder && $entry2->isFolder) { // if (!$entry1->isFolder && $entry2->isFolder) {
return 1; // 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); $cache = array();
$entries = $folder->getContent(); $folder = Entry::get($this, $this->abs_path, $cache);
uasort($entries, "_cmp_no_js_fallback"); $entries = $folder->get_content($cache);
uasort($entries, array("Entry", "cmp"));
$html = "<table>"; $html = "<table>";
$html .= "<tr><th></th><th><span>Name</span></th><th><span>Last modified</span></th><th><span>Size</span></th></tr>"; $html .= "<tr><th></th><th><span>Name</span></th><th><span>Last modified</span></th><th><span>Size</span></th></tr>";
if ($folder->parent) { if ($folder->get_parent($cache)) {
$html .= "<tr><td></td><td><a href=\"..\">Parent Directory</a></td><td></td><td></td></tr>"; $html .= "<tr><td></td><td><a href=\"..\">Parent Directory</a></td><td></td><td></td></tr>";
} }
foreach ($entries as $entry) { foreach ($entries as $entry) {
$html .= "<tr>"; $html .= "<tr>";
$html .= "<td></td>"; $html .= "<td>" . ($entry->is_folder ? "[D]" : "[F]") . "</td>";
$html .= "<td><a href=\"" . $entry->absHref . "\">" . basename($entry->absPath) . ($entry->isFolder ? "/" : "") . "</a></td>"; $html .= "<td><a href=\"" . $entry->abs_href . "\">" . basename($entry->abs_path) . ($entry->is_folder ? "/" : "") . "</a></td>";
$html .= "<td>" . date("Y-m-d H:i", $entry->date) . "</td>"; $html .= "<td>" . date("Y-m-d H:i", $entry->date) . "</td>";
$html .= "<td>" . ($entry->size !== null ? intval($entry->size / 1000) . " KB" : "" ) . "</td>"; $html .= "<td>" . ($entry->size !== null ? intval($entry->size / 1000) . " KB" : "" ) . "</td>";
$html .= "</tr>"; $html .= "</tr>";

View file

@ -4,119 +4,105 @@ class Entry {
private static $FOLDER_SIZE_CMD = "du -sb \"[DIR]\""; private static $FOLDER_SIZE_CMD = "du -sb \"[DIR]\"";
public static function cmp($entry1, $entry2) {
private static $cache = array(); return strcasecmp($entry1->abs_path, $entry2->abs_path);
public static function get_cache() {
return Entry::$cache;
} }
public static function get($app, $abs_path, &$cache) {
public static function get($h5ai, $absPath, $absHref) { if (!starts_with($abs_path, $app->get_root_abs_path())) {
error_log("ILLEGAL REQUEST: " . $abs_path . ", " . $app->get_root_abs_path());
if (!starts_with($absHref, $h5ai->get_root_abs_href())) {
error_log("ILLEGAL REQUEST: " . $absHref . ", " . $absPath . ", " . $h5ai->get_root_abs_href());
return null; return null;
} }
if (array_key_exists($absHref, Entry::$cache)) { if (is_array($cache) && array_key_exists($abs_path, $cache)) {
return Entry::$cache[$absHref]; 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() { public $app,
$abs_path, $abs_href,
function cmp($entry1, $entry2) { $date, $size,
$is_folder,
return strcasecmp($entry1->absHref, $entry2->absHref); $is_content_fetched;
}
uasort(Entry::$cache, "cmp");
}
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) { if ($this->is_folder) {
$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) {
$this->size = null; $this->size = null;
$options = $h5ai->get_options(); $options = $app->get_options();
if ($options["foldersize"]["enabled"]) { 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); $this->size = intval(preg_replace("/\s.*$/", "", `$cmd`), 10);
} }
} else { } else {
$this->size = filesize($this->absPath); $this->size = filesize($this->abs_path);
} }
$this->parent = null; $this->is_content_fetched = false;
$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;
} }
public function toJsonObject($withStatus) { public function to_json_object() {
$obj = array( $obj = array(
"absHref" => $this->absHref, "absHref" => $this->abs_href,
"time" => ($this->date * 1000), "time" => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript)
"size" => $this->size "size" => $this->size
); );
if ($withStatus && $this->isFolder) { if ($this->is_folder) {
$obj["status"] = $this->h5ai->get_http_code($this->absHref); $obj["status"] = $this->app->get_http_code($this->abs_href);
$obj["content"] = $this->isContentFetched; $obj["content"] = $this->is_content_fetched;
} }
return $obj; 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(); $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; return $content;
} }
$files = $this->h5ai->read_dir($this->absPath); $files = $this->app->read_dir($this->abs_path);
foreach ($files as $file) { foreach ($files as $file) {
$entry = Entry::get($this->h5ai, $this->absPath . "/" . $file, $this->absHref . rawurlencode($file)); $entry = Entry::get($this->app, $this->abs_path . "/" . $file, $cache);
$content[$entry->absPath] = $entry; $content[$entry->abs_path] = $entry;
} }
$this->isContentFetched = true; $this->is_content_fetched = true;
return $content; return $content;
} }

View file

@ -1,14 +1,9 @@
|<?php |<?php require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/init.php"); ?>
| header("Content-type: text/html;{{pkg.name}}={{pkg.version}}");
| require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/init.php");
| $app_abs_href = $APP->get_app_abs_href();
| $is_head_request = stripos($_SERVER["REQUEST_METHOD"], "HEAD");
|?>
- var appHref = "<?php echo $app_abs_href; ?>client" - var href = "<?php echo $APP->get_app_abs_href(); ?>"
- var json = "<?php if (!$is_head_request) { echo $APP->get_generic_json(); }?>" - var json = "<?php echo $APP->get_generic_json(); ?>"
- var fallback = "<?php if (!$is_head_request) { echo $APP->get_no_js_fallback(); }?>" - var fallback = "<?php echo $APP->get_no_js_fallback(); ?>"
- var config = "<?php if (!$is_head_request) { echo $APP->get_custom_config(); }?>" - var config = "<?php echo $APP->get_custom_config(); ?>"
doctype 5 doctype 5
//if lt IE 9 //if lt IE 9
@ -20,14 +15,14 @@ html.no-js( lang="en" )
head head
meta( charset="utf-8" ) meta( charset="utf-8" )
meta( http-equiv="X-UA-Compatible", content="IE=edge,chrome=1" ) meta( http-equiv="X-UA-Compatible", content="IE=edge,chrome=1" )
title Directory index · styled with {{pkg.name}} {{pkg.version}} title index · styled with {{pkg.name}} {{pkg.version}} ({{pkg.url}})
meta( name="description", content="Directory 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" ) meta( name="viewport", content="width=device-width" )
link( rel="shortcut icon", href!="#{appHref}/images/app-16x16.ico" ) link( rel="shortcut icon", href!="#{href}client/images/app-16x16.ico" )
link( rel="apple-touch-icon", type="image/png", href!="#{appHref}/images/app-48x48.png" ) 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="//fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic,700italic|Ubuntu:400,700,400italic,700italic" )
link( rel="stylesheet", href!="#{appHref}/css/styles.css" ) link( rel="stylesheet", href!="#{href}client/css/styles.css" )
script( src!="#{appHref}/js/scripts.js", data-config!="#{config}" ) script( src!="#{href}client/js/scripts.js", data-config!="#{config}" )
body#h5ai-main body#h5ai-main