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

@ -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;
}