Refactor PHP, 5.3 => 5.4.

This commit is contained in:
Lars Jung 2015-05-05 23:55:35 +02:00
parent 1d7c2f3b80
commit 059e166402
8 changed files with 55 additions and 53 deletions

View file

@ -1,5 +1,8 @@
# Changelog
* now requires PHP 5.4.0+
* lots of code cleanup and refactorings
* changes API
* fixes some styles in IE10
* resets IE edge mode
* fixes preview bottom bar for small screen widths

View file

@ -14,7 +14,7 @@ class Api {
public function apply() {
$action = Util::get_request_param("action");
$supported = array("login", "logout", "get", "download");
$supported = ["login", "logout", "get", "download"];
Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported));
$methodname = "on_${action}";
@ -26,22 +26,22 @@ class Api {
$pass = Util::get_request_param("pass");
$_SESSION[AS_ADMIN_SESSION_KEY] = sha1($pass) === PASSHASH;
Util::json_exit(array("asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]));
Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]);
}
private function on_logout() {
$_SESSION[AS_ADMIN_SESSION_KEY] = false;
Util::json_exit(array("asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]));
Util::json_exit(["asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]]);
}
private function on_get() {
$response = array();
$response = [];
foreach (array("setup", "options", "types", "theme", "langs") as $name) {
foreach (["setup", "options", "types", "theme", "langs"] as $name) {
if (Util::get_boolean_request_param($name, false)) {
$methodname = "get_${name}";

View file

@ -3,8 +3,8 @@
class App {
private static $RE_DELIMITER = "|";
private static $ICON_EXTS = array("svg", "png", "jpg");
private static $CUSTOM_EXTS = array("html", "md");
private static $ICON_EXTS = ["svg", "png", "jpg"];
private static $CUSTOM_EXTS = ["html", "md"];
private $options;
@ -30,17 +30,17 @@ class App {
public function get_setup() {
$keys = array(
$keys = [
"APP_HREF",
"ROOT_HREF",
"VERSION",
"AS_ADMIN",
"HAS_CUSTOM_PASSHASH"
);
];
if (AS_ADMIN) {
$keys = array_merge($keys, array(
$keys = array_merge($keys, [
"PHP_VERSION",
"MIN_PHP_VERSION",
"HAS_MIN_PHP_VERSION",
@ -61,10 +61,10 @@ class App {
"HAS_CMD_FFMPEG",
"HAS_CMD_AVCONV",
"HAS_CMD_DU"
));
]);
}
$setup = array();
$setup = [];
foreach ($keys as $key) {
$setup[$key] = constant($key);
}
@ -83,7 +83,7 @@ class App {
$theme = $this->get_option("view.theme", "-NONE-");
$theme_path = APP_PATH . "/client/images/themes/${theme}";
$icons = array();
$icons = [];
if (is_dir($theme_path)) {
if ($dir = opendir($theme_path)) {
@ -105,7 +105,7 @@ class App {
$rel_path = substr($path, strlen(ROOT_PATH));
$parts = explode("/", $rel_path);
$encoded_parts = array();
$encoded_parts = [];
foreach ($parts as $part) {
if ($part != "") {
$encoded_parts[] = rawurlencode($part);
@ -130,7 +130,7 @@ class App {
return true;
}
foreach ($this->get_option("view.hidden", array()) as $re) {
foreach ($this->get_option("view.hidden", []) as $re) {
$re = App::$RE_DELIMITER . str_replace(App::$RE_DELIMITER, '\\' . App::$RE_DELIMITER, $re) . App::$RE_DELIMITER;
if (preg_match($re, $name)) {
return true;
@ -143,7 +143,7 @@ class App {
public function read_dir($path) {
$names = array();
$names = [];
if (is_dir($path)) {
foreach (scandir($path) as $name) {
if (
@ -176,7 +176,7 @@ class App {
return false;
}
foreach ($this->get_option("view.unmanaged", array()) as $name) {
foreach ($this->get_option("view.unmanaged", []) as $name) {
if (file_exists($path . "/" . $name)) {
return false;
}
@ -199,10 +199,10 @@ class App {
public function get_items($href, $what) {
if (!$this->is_managed_href($href)) {
return array();
return [];
}
$cache = array();
$cache = [];
$folder = Item::get($this, $this->to_path($href), $cache);
// add content of subfolders
@ -219,8 +219,8 @@ class App {
$folder = $folder->get_parent($cache);
}
uasort($cache, array("Item", "cmp"));
$result = array();
uasort($cache, ["Item", "cmp"]);
$result = [];
foreach ($cache as $p => $item) {
$result[] = $item->to_json_object();
}
@ -249,10 +249,10 @@ class App {
$path = $this->get_current_path();
}
$cache = array();
$cache = [];
$folder = Item::get($this, $path, $cache);
$items = $folder->get_content($cache);
uasort($items, array("Item", "cmp"));
uasort($items, ["Item", "cmp"]);
$html = "<table>";
@ -291,7 +291,7 @@ class App {
public function get_langs() {
$langs = array();
$langs = [];
$l10n_path = APP_PATH . "/conf/l10n";
if (is_dir($l10n_path)) {
if ($dir = opendir($l10n_path)) {
@ -311,7 +311,7 @@ class App {
public function get_l10n($iso_codes) {
$results = array();
$results = [];
foreach ($iso_codes as $iso_code) {
$file = APP_PATH . "/conf/l10n/" . $iso_code . ".json";
@ -339,10 +339,10 @@ class App {
public function get_customizations($href) {
if (!$this->get_option("custom.enabled", false)) {
return array(
"header" => array("content" => null, "type" => null),
"footer" => array("content" => null, "type" => null)
);
return [
"header" => ["content" => null, "type" => null],
"footer" => ["content" => null, "type" => null]
];
}
$path = $this->to_path($href);
@ -374,10 +374,10 @@ class App {
$path = $parent_path;
}
return array(
"header" => array("content" => $header, "type" => $header_type),
"footer" => array("content" => $footer, "type" => $footer_type)
);
return [
"header" => ["content" => $header, "type" => $header_type],
"footer" => ["content" => $footer, "type" => $footer_type]
];
}

View file

@ -22,8 +22,8 @@ class Archive {
return 500;
}
$this->dirs = array();
$this->files = array();
$this->dirs = [];
$this->files = [];
$this->add_hrefs($hrefs);
@ -67,7 +67,7 @@ class Archive {
private function php_tar($dirs, $files) {
$filesizes = array();
$filesizes = [];
$total_size = 512 * count($dirs);
foreach (array_keys($files) as $real_file) {

View file

@ -70,7 +70,7 @@ class Bootstrap {
}
define("SERVER_NAME", $server_name);
define("SERVER_VERSION", $server_version);
define("HAS_SERVER", in_array($server_name, array("apache", "lighttpd", "nginx", "cherokee")));
define("HAS_SERVER", in_array($server_name, ["apache", "lighttpd", "nginx", "cherokee"]));
}
@ -118,7 +118,7 @@ class Bootstrap {
$cmd = "which";
}
foreach (array("tar", "zip", "convert", "ffmpeg", "avconv", "du") as $c) {
foreach (["tar", "zip", "convert", "ffmpeg", "avconv", "du"] as $c) {
$cmds[$c] = ($cmd !== false) && Util::exec_0($cmd . " " . $c);
}

View file

@ -54,11 +54,11 @@ class Item {
public function to_json_object() {
$obj = array(
$obj = [
"href" => $this->href,
"time" => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript)
"size" => $this->size
);
];
if ($this->is_folder) {
$obj["managed"] = $this->app->is_managed_href($this->href);
@ -81,7 +81,7 @@ class Item {
public function get_content(&$cache) {
$items = array();
$items = [];
if (!$this->app->is_managed_href($this->href)) {
return $items;

View file

@ -2,9 +2,9 @@
class Thumb {
private static $FFMPEG_CMDV = array("ffmpeg", "-ss", "0:00:10", "-i", "[SRC]", "-an", "-vframes", "1", "[DEST]");
private static $AVCONV_CMDV = array("avconv", "-ss", "0:00:10", "-i", "[SRC]", "-an", "-vframes", "1", "[DEST]");
private static $CONVERT_CMDV = array("convert", "-density", "200", "-quality", "100", "-sharpen", "0x1.0", "-strip", "[SRC][0]", "[DEST]");
private static $FFMPEG_CMDV = ["ffmpeg", "-ss", "0:00:10", "-i", "[SRC]", "-an", "-vframes", "1", "[DEST]"];
private static $AVCONV_CMDV = ["avconv", "-ss", "0:00:10", "-i", "[SRC]", "-an", "-vframes", "1", "[DEST]"];
private static $CONVERT_CMDV = ["convert", "-density", "200", "-quality", "100", "-sharpen", "0x1.0", "-strip", "[SRC][0]", "[DEST]"];
private static $THUMB_CACHE = "thumbs";
@ -240,7 +240,7 @@ class Image {
$this->source = imagerotate($this->source, $angle, 0);
if ( $angle === 90 || $angle === 270 ) {
list($this->width, $this->height) = array($this->height, $this->width);
list($this->width, $this->height) = [$this->height, $this->width];
}
}

View file

@ -16,7 +16,7 @@ class Util {
}
public static function json_exit($obj = array()) {
public static function json_exit($obj = []) {
header("Content-type: application/json;charset=utf-8");
echo json_encode($obj);
@ -27,7 +27,7 @@ class Util {
public static function json_fail($err, $msg = "", $cond = true) {
if ($cond) {
Util::json_exit(array("err" => $err, "msg" => $msg));
Util::json_exit(["err" => $err, "msg" => $msg]);
}
}
@ -50,7 +50,6 @@ class Util {
public static function is_post_request() {
// Logger::log("POSTED", $_POST);
return (strtolower($_SERVER["REQUEST_METHOD"]) === "post");
}
@ -87,7 +86,7 @@ class Util {
public static function load_commented_json($path) {
if (!file_exists($path)) {
return array();
return [];
}
$content = file_get_contents($path);
@ -121,7 +120,7 @@ class Util {
}
$cmd = implode(" ", array_map("escapeshellarg", $cmdv));
$lines = array();
$lines = [];
$rc = null;
exec($cmd, $lines, $rc);
return implode("\n", $lines);
@ -130,7 +129,7 @@ class Util {
public static function exec_0($cmd) {
$lines = array();
$lines = [];
$rc = null;
try {
@exec($cmd, $lines, $rc);
@ -140,7 +139,7 @@ class Util {
}
private static $size_cache = array();
private static $size_cache = [];
public static function filesize($app, $path) {
@ -184,7 +183,7 @@ class Util {
if ($app->get_option("foldersize.enabled", false)) {
if (HAS_CMD_DU && $app->get_option("foldersize.type", null) === "shell-du") {
$cmdv = array("du", "-sk", $path);
$cmdv = ["du", "-sk", $path];
$size = intval(preg_replace("#\s.*$#", "", Util::exec_cmdv($cmdv)), 10) * 1024;
} else {
$size = 0;