From 318fefbbfde59db6a372142cfc9dab7b2a903d21 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sat, 6 Oct 2012 22:49:09 +0200 Subject: [PATCH] More refactorings. --- src/_h5ai/server/php/api.php | 61 ++++++------- src/_h5ai/server/php/inc/Archive.php | 71 +++++++-------- src/_h5ai/server/php/inc/Entry.php | 6 +- src/_h5ai/server/php/inc/Thumb.php | 130 +++++++++++++-------------- 4 files changed, 127 insertions(+), 141 deletions(-) diff --git a/src/_h5ai/server/php/api.php b/src/_h5ai/server/php/api.php index 0a8d9f38..40316c94 100644 --- a/src/_h5ai/server/php/api.php +++ b/src/_h5ai/server/php/api.php @@ -1,8 +1,7 @@ get_options(); +$options = $APP->get_options(); list($action) = use_request_params(array("action")); @@ -15,51 +14,51 @@ if ($action === "get") { if (array_key_exists("options", $_REQUEST)) { use_request_params("options"); - $response["options"] = $app->get_options(); + $response["options"] = $APP->get_options(); } if (array_key_exists("types", $_REQUEST)) { use_request_params("types"); - $response["types"] = $app->get_types(); + $response["types"] = $APP->get_types(); } if (array_key_exists("langs", $_REQUEST)) { use_request_params("langs"); - $response["langs"] = $app->get_l10n_list(); + $response["langs"] = $APP->get_l10n_list(); } if (array_key_exists("l10n", $_REQUEST)) { list($iso_codes) = use_request_params("l10nCodes", "l10n"); $iso_codes = explode(",", $iso_codes); - $response["l10n"] = $app->get_l10n($iso_codes); + $response["l10n"] = $APP->get_l10n($iso_codes); } if (array_key_exists("checks", $_REQUEST)) { use_request_params("checks"); - $response["checks"] = $app->get_server_checks(); + $response["checks"] = $APP->get_server_checks(); } if (array_key_exists("server", $_REQUEST)) { use_request_params("server"); - $response["server"] = $app->get_server_details(); + $response["server"] = $APP->get_server_details(); } if (array_key_exists("custom", $_REQUEST)) { list($abs_href) = use_request_params("customHref", "custom"); - $response["custom"] = $app->get_customizations($abs_href); + $response["custom"] = $APP->get_customizations($abs_href); } if (array_key_exists("entries", $_REQUEST)) { list($abs_href, $what) = use_request_params("entriesHref", "entriesWhat", "entries"); $what = intval($what, 10); - $response["entries"] = $app->get_entries($abs_href, $what); + $response["entries"] = $APP->get_entries($abs_href, $what); } if (count($_REQUEST)) { @@ -81,15 +80,15 @@ else if ($action === "getthumbsrc") { json_fail(2, "thumbnails not supported"); } - list($type, $srcAbsHref, $mode, $width, $height) = use_request_params(array("type", "href", "mode", "width", "height")); + list($type, $src_abs_href, $mode, $width, $height) = use_request_params(array("type", "href", "mode", "width", "height")); - $thumb = new Thumb($app); - $thumbHref = $thumb->thumb($type, $srcAbsHref, $mode, $width, $height); - if ($thumbHref === null) { + $thumb = new Thumb($APP); + $thumb_href = $thumb->thumb($type, $src_abs_href, $mode, $width, $height); + if ($thumb_href === null) { json_fail(3, "thumbnail creation failed"); } - json_exit(array("absHref" => $thumbHref)); + json_exit(array("absHref" => $thumb_href)); } @@ -100,7 +99,7 @@ else if ($action === "archive") { list($execution, $format, $hrefs) = use_request_params(array("execution", "format", "hrefs")); normalized_require_once("/server/php/inc/Archive.php"); - $archive = new Archive($app); + $archive = new Archive($APP); $hrefs = explode(":", trim($hrefs)); $target = $archive->create($execution, $format, $hrefs); @@ -120,7 +119,7 @@ else if ($action === "getarchive") { list($id, $as) = use_request_params(array("id", "as")); json_fail(2, "file not found", !preg_match("/^package-/", $id)); - $target = $app->get_cache_abs_path() . "/" . $id; + $target = $APP->get_cache_abs_path() . "/" . $id; json_fail(3, "file not found", !file_exists($target)); header("Content-Type: application/octet-stream"); @@ -144,10 +143,10 @@ else if ($action === "upload") { json_fail(3, "something went wrong [" . $userfile["error"] . "]", $userfile["error"] !== 0); json_fail(4, "folders not supported", file_get_contents($userfile["tmp_name"]) === "null"); - $upload_dir = $app->get_abs_path($href); - $code = $app->get_http_code($href); + $upload_dir = $APP->get_abs_path($href); + $code = $APP->get_http_code($href); - json_fail(5, "upload dir no h5ai folder or ignored", $code !== "h5ai" || $app->is_ignored($upload_dir)); + json_fail(5, "upload dir no h5ai folder or ignored", $code !== "h5ai" || $APP->is_ignored($upload_dir)); $dest = $upload_dir . "/" . utf8_encode($userfile["name"]); @@ -172,15 +171,13 @@ else if ($action === "delete") { $d = normalize_path(dirname($href), true); $n = basename($href); - $code = $app->get_http_code($d); - if ($code == 401) { - } + $code = $APP->get_http_code($d); - if ($code == "h5ai" && !$app->is_ignored($n)) { + if ($code == "h5ai" && !$APP->is_ignored($n)) { - $absPath = $app->get_abs_path($href); + $abs_path = $APP->get_abs_path($href); - if (!unlink($absPath)) { + if (!unlink($abs_path)) { $errors[] = $href; } } @@ -203,16 +200,14 @@ else if ($action === "rename") { $d = normalize_path(dirname($href), true); $n = basename($href); - $code = $app->get_http_code($d); - if ($code == 401) { - } + $code = $APP->get_http_code($d); - if ($code == "h5ai" && !$app->is_ignored($n)) { + if ($code == "h5ai" && !$APP->is_ignored($n)) { - $absPath = $app->get_abs_path($href); - $folder = normalize_path(dirname($absPath)); + $abs_path = $APP->get_abs_path($href); + $folder = normalize_path(dirname($abs_path)); - if (!rename($absPath, $folder . "/" . $name)) { + if (!rename($abs_path, $folder . "/" . $name)) { json_fail(2, "renaming failed"); } } diff --git a/src/_h5ai/server/php/inc/Archive.php b/src/_h5ai/server/php/inc/Archive.php index bdc1acb4..be6d2424 100644 --- a/src/_h5ai/server/php/inc/Archive.php +++ b/src/_h5ai/server/php/inc/Archive.php @@ -6,12 +6,12 @@ class Archive { private static $ZIP_CMD = "$(cd [ROOTDIR] && zip [TARGET] [FILES])"; - private $h5ai, $dirs, $files, $sc401; + private $app, $dirs, $files, $sc401; - public function __construct($h5ai) { + public function __construct($app) { - $this->h5ai = $h5ai; + $this->app = $app; } @@ -19,17 +19,14 @@ class Archive { $this->dirs = array(); $this->files = array(); - $this->sc401 = false; $this->add_hrefs($hrefs); - if ($this->sc401) { - return 401; - } else if (count($this->dirs) === 0 && count($this->files) === 0) { + if (count($this->dirs) === 0 && count($this->files) === 0) { return 404; } - $target = $this->h5ai->get_cache_abs_path() . "/package-" . sha1(microtime(true) . rand()) . "." . $format; + $target = $this->app->get_cache_abs_path() . "/package-" . sha1(microtime(true) . rand()) . "." . $format; try { if ($execution === "shell") { @@ -41,21 +38,21 @@ class Archive { } else { return null; } - $cmd = str_replace("[ROOTDIR]", "\"" . $this->h5ai->get_root_abs_path() . "\"", $cmd); + $cmd = str_replace("[ROOTDIR]", "\"" . $this->app->get_root_abs_path() . "\"", $cmd); $cmd = str_replace("[TARGET]", "\"" . $target . "\"", $cmd); $cmd = str_replace("[DIRS]", count($this->dirs) ? "\"" . implode("\" \"", array_values($this->dirs)) . "\"" : "", $cmd); $cmd = str_replace("[FILES]", count($this->files) ? "\"" . implode("\" \"", array_values($this->files)) . "\"" : "", $cmd); - `$cmd`; + shell_exec($cmd); } else if ($execution === "php") { $archive = new PharData($target); - foreach ($this->dirs as $archivedDir) { - $archive->addEmptyDir($archivedDir); + foreach ($this->dirs as $archived_dir) { + $archive->addEmptyDir($archived_dir); } - foreach ($this->files as $realFile => $archivedFile) { - $archive->addFile($realFile, $archivedFile); // very, very slow :/ + foreach ($this->files as $real_file => $archived_file) { + $archive->addFile($real_file, $archived_file); // very, very slow :/ } } @@ -74,54 +71,48 @@ class Archive { $d = normalize_path(dirname($href), true); $n = basename($href); - $code = $this->h5ai->get_http_code($d); - if ($code == 401) { - $this->sc401 = true; - } + $code = $this->app->get_http_code($d); - if ($code == "h5ai" && !$this->h5ai->is_ignored($n)) { + if ($code == App::$MAGIC_SEQUENCE && !$this->app->is_ignored($n)) { - $realFile = $this->h5ai->get_abs_path($href); - $archivedFile = preg_replace("!^" . normalize_path($this->h5ai->get_root_abs_path(), true) . "!", "", $realFile); + $real_file = $this->app->get_abs_path($href); + $archived_file = preg_replace("!^" . normalize_path($this->app->get_root_abs_path(), true) . "!", "", $real_file); - if (is_dir($realFile)) { - $this->add_dir($realFile, $archivedFile); + if (is_dir($real_file)) { + $this->add_dir($real_file, $archived_file); } else { - $this->add_file($realFile, $archivedFile); + $this->add_file($real_file, $archived_file); } } } } - private function add_file($realFile, $archivedFile) { + private function add_file($real_file, $archived_file) { - if (is_readable($realFile)) { - $this->files[$realFile] = $archivedFile; + if (is_readable($real_file)) { + $this->files[$real_file] = $archived_file; } } - private function add_dir($realDir, $archivedDir) { + private function add_dir($real_dir, $archived_dir) { - $code = $this->h5ai->get_http_code($this->h5ai->get_abs_href($realDir)); - if ($code == 401) { - $this->sc401 = true; - } + $code = $this->app->get_http_code($this->app->get_abs_href($real_dir)); - if ($code == "h5ai") { - $this->dirs[] = $archivedDir; + if ($code == App::$MAGIC_SEQUENCE) { + $this->dirs[] = $archived_dir; - $files = $this->h5ai->read_dir($realDir); + $files = $this->app->read_dir($real_dir); foreach ($files as $file) { - $realFile = $realDir . "/" . $file; - $archivedFile = $archivedDir . "/" . $file; + $real_file = $real_dir . "/" . $file; + $archived_file = $archived_dir . "/" . $file; - if (is_dir($realFile)) { - $this->add_dir($realFile, $archivedFile); + if (is_dir($real_file)) { + $this->add_dir($real_file, $archived_file); } else { - $this->add_file($realFile, $archivedFile); + $this->add_file($real_file, $archived_file); } } } diff --git a/src/_h5ai/server/php/inc/Entry.php b/src/_h5ai/server/php/inc/Entry.php index 77546a1e..305de5b3 100644 --- a/src/_h5ai/server/php/inc/Entry.php +++ b/src/_h5ai/server/php/inc/Entry.php @@ -87,9 +87,9 @@ class Entry { public function get_parent(&$cache) { - $parentAbsPath = normalize_path(dirname($this->abs_path)); - if (starts_with($parentAbsPath, $this->app->get_root_abs_path())) { - return Entry::get($this->app, $parentAbsPath, $cache); + $parent_abs_path = normalize_path(dirname($this->abs_path)); + if (starts_with($parent_abs_path, $this->app->get_root_abs_path())) { + return Entry::get($this->app, $parent_abs_path, $cache); } return null; } diff --git a/src/_h5ai/server/php/inc/Thumb.php b/src/_h5ai/server/php/inc/Thumb.php index f471f18f..08a04441 100644 --- a/src/_h5ai/server/php/inc/Thumb.php +++ b/src/_h5ai/server/php/inc/Thumb.php @@ -2,8 +2,8 @@ class Thumb { - private static $FFMPEG = "ffmpeg -i \"[SOURCE]\" -an -ss 3 -vframes 1 \"[TARGET]\""; - private static $CONVERT = "convert -strip \"[SOURCE][0]\" \"[TARGET]\""; + private static $FFMPEG_CMD = "ffmpeg -i \"[SOURCE]\" -an -ss 3 -vframes 1 \"[TARGET]\""; + private static $CONVERT_CMD = "convert -strip \"[SOURCE][0]\" \"[TARGET]\""; public static final function is_supported() { @@ -16,67 +16,67 @@ class Thumb { } - private $h5ai; + private $app; - public function __construct($h5ai) { + public function __construct($app) { - $this->h5ai = $h5ai; + $this->app = $app; } - public function thumb($type, $sourceAbsHref, $mode, $width, $height) { + public function thumb($type, $source_abs_href, $mode, $width, $height) { - $sourceAbsPath = $this->h5ai->get_abs_path($sourceAbsHref); + $source_abs_path = $this->app->get_abs_path($source_abs_href); if ($type === "img") { - $captureAbsPath = $sourceAbsPath; + $capture_abs_path = $source_abs_path; } else if ($type === "mov") { - $captureAbsPath = $this->capture(Thumb::$FFMPEG, $sourceAbsPath); + $capture_abs_path = $this->capture(Thumb::$FFMPEG_CMD, $source_abs_path); } else if ($type === "doc") { - $captureAbsPath = $this->capture(Thumb::$CONVERT, $sourceAbsPath); + $capture_abs_path = $this->capture(Thumb::$CONVERT_CMD, $source_abs_path); } - return $this->thumb_href($captureAbsPath, $mode, $width, $height); + return $this->thumb_href($capture_abs_path, $mode, $width, $height); } - private function thumb_href($sourceAbsPath, $mode, $width, $height) { + private function thumb_href($source_abs_path, $mode, $width, $height) { - if (!file_exists($sourceAbsPath)) { + if (!file_exists($source_abs_path)) { return null; } - $name = "thumb-" . sha1("$sourceAbsPath-$width-$height-$mode") . ".jpg"; - $thumbAbsPath = $this->h5ai->get_cache_abs_path() . "/" . $name; - $thumbAbsHref = $this->h5ai->get_cache_abs_href() . $name; + $name = "thumb-" . sha1("$source_abs_path-$width-$height-$mode") . ".jpg"; + $thumb_abs_path = $this->app->get_cache_abs_path() . "/" . $name; + $thumb_abs_href = $this->app->get_cache_abs_href() . $name; - if (!file_exists($thumbAbsPath) || filemtime($sourceAbsPath) >= filemtime($thumbAbsPath)) { + if (!file_exists($thumb_abs_path) || filemtime($source_abs_path) >= filemtime($thumb_abs_path)) { $image = new Image(); - $image->setSource($sourceAbsPath); + $image->set_source($source_abs_path); $image->thumb($mode, $width, $height); - $image->saveDestJpeg($thumbAbsPath, 80); + $image->save_dest_jpeg($thumb_abs_path, 80); } - return file_exists($thumbAbsPath) ? $thumbAbsHref : null; + return file_exists($thumb_abs_path) ? $thumb_abs_href : null; } - private function capture($cmd, $sourceAbsPath) { + private function capture($cmd, $source_abs_path) { - if (!file_exists($sourceAbsPath)) { + if (!file_exists($source_abs_path)) { return null; } - $captureAbsPath = $this->h5ai->get_cache_abs_path() . "/capture-" . sha1($sourceAbsPath) . ".jpg"; + $capture_abs_path = $this->app->get_cache_abs_path() . "/capture-" . sha1($source_abs_path) . ".jpg"; - if (!file_exists($captureAbsPath) || filemtime($sourceAbsPath) >= filemtime($captureAbsPath)) { - $cmd = str_replace("[SOURCE]", $sourceAbsPath, $cmd); - $cmd = str_replace("[TARGET]", $captureAbsPath, $cmd); + if (!file_exists($capture_abs_path) || filemtime($source_abs_path) >= filemtime($capture_abs_path)) { + $cmd = str_replace("[SOURCE]", $source_abs_path, $cmd); + $cmd = str_replace("[TARGET]", $capture_abs_path, $cmd); `$cmd`; } - return file_exists($captureAbsPath) ? $captureAbsPath : null; + return file_exists($capture_abs_path) ? $capture_abs_path : null; } } @@ -143,7 +143,7 @@ class Magic { class Image { - private $sourceFile, $source, $width, $height, $type, $dest; + private $source_file, $source, $width, $height, $type, $dest; public static final function is_supported() { @@ -159,7 +159,7 @@ class Image { public function __construct($filename = null) { - $this->sourceFile = null; + $this->source_file = null; $this->source = null; $this->width = null; $this->height = null; @@ -167,43 +167,43 @@ class Image { $this->dest = null; - $this->setSource($filename); + $this->set_source($filename); } public function __destruct() { - $this->releaseSource(); - $this->releaseDest(); + $this->release_source(); + $this->release_dest(); } - public function setSource($filename) { + public function set_source($filename) { - $this->releaseSource(); - $this->releaseDest(); + $this->release_source(); + $this->release_dest(); if (is_null($filename)) { return; } - $this->sourceFile = $filename; + $this->source_file = $filename; - list($this->width, $this->height, $this->type) = @getimagesize($this->sourceFile); + list($this->width, $this->height, $this->type) = @getimagesize($this->source_file); if (!$this->width || !$this->height) { - $this->sourceFile = null; + $this->source_file = null; $this->width = null; $this->height = null; $this->type = null; return; } - $this->source = imagecreatefromstring(file_get_contents($this->sourceFile)); + $this->source = imagecreatefromstring(file_get_contents($this->source_file)); } - public function saveDestJpeg($filename, $quality = 80) { + public function save_dest_jpeg($filename, $quality = 80) { if (!is_null($this->dest)) { @imagejpeg($this->dest, $filename, $quality); @@ -212,7 +212,7 @@ class Image { } - public function saveDestPng($filename, $quality = 9) { + public function save_dest_png($filename, $quality = 9) { if (!is_null($this->dest)) { @imagepng($this->dest, $filename, $quality); @@ -221,7 +221,7 @@ class Image { } - public function releaseDest() { + public function release_dest() { if (!is_null($this->dest)) { @imagedestroy($this->dest); @@ -230,11 +230,11 @@ class Image { } - public function releaseSource() { + public function release_source() { if (!is_null($this->source)) { @imagedestroy($this->source); - $this->sourceFile = null; + $this->source_file = null; $this->source = null; $this->width = null; $this->height = null; @@ -243,29 +243,29 @@ class Image { } - private function magic($destX, $destY, $srcX, $srcY, $destWidth, $destHeight, $srcWidth, $srcHeight, $canWidth = null, $canHeight = null, $color = null) { + private function magic($dest_x, $dest_y, $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height, $can_width = null, $can_height = null, $color = null) { if (is_null($this->source)) { return; } - if ($canWidth === 0) { - $canWidth = 1; + if ($can_width === 0) { + $can_width = 1; } - if ($canHeight === 0) { - $canHeight = 1; + if ($can_height === 0) { + $can_height = 1; } - if ($destWidth === 0) { - $destWidth = 1; + if ($dest_width === 0) { + $dest_width = 1; } - if ($destHeight === 0) { - $destHeight = 1; + if ($dest_height === 0) { + $dest_height = 1; } - if (!is_null($canWidth) && !is_null($canHeight)) { - $this->dest = imagecreatetruecolor($canWidth, $canHeight); + if (!is_null($can_width) && !is_null($can_height)) { + $this->dest = imagecreatetruecolor($can_width, $can_height); } else { - $this->dest = imagecreatetruecolor($destWidth, $destHeight); + $this->dest = imagecreatetruecolor($dest_width, $dest_height); } if (is_null($color)) { @@ -274,7 +274,7 @@ class Image { $icol = imagecolorallocate($this->dest, $color[0], $color[1], $color[2]); imagefill($this->dest, 0, 0, $icol); - imagecopyresampled($this->dest, $this->source, $destX, $destY, $srcX, $srcY, $destWidth, $destHeight, $srcWidth, $srcHeight); + imagecopyresampled($this->dest, $this->source, $dest_x, $dest_y, $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height); } @@ -284,18 +284,18 @@ class Image { $height = $width; } if ($mode === "square") { - $this->squareThumb($width); + $this->square_thumb($width); } elseif ($mode === "rational") { - $this->rationalThumb($width, $height); + $this->rational_thumb($width, $height); } elseif ($mode === "center") { - $this->centerThumb($width, $height, $color); + $this->center_thumb($width, $height, $color); } else { - $this->freeThumb($width, $height); + $this->free_thumb($width, $height); } } - public function squareThumb($width) { + public function square_thumb($width) { if (is_null($this->source)) { return; @@ -309,7 +309,7 @@ class Image { } - public function rationalThumb($width, $height) { + public function rational_thumb($width, $height) { if (is_null($this->source)) { return; @@ -333,7 +333,7 @@ class Image { } - public function centerThumb($width, $height, $color = null) { + public function center_thumb($width, $height, $color = null) { if (is_null($this->source)) { return; @@ -360,7 +360,7 @@ class Image { } - public function freeThumb($width, $height) { + public function free_thumb($width, $height) { if (is_null($this->source)) { return;