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 # Changelog
* now requires PHP 5.4.0+
* lots of code cleanup and refactorings
* changes API
* fixes some styles in IE10 * fixes some styles in IE10
* resets IE edge mode * resets IE edge mode
* fixes preview bottom bar for small screen widths * fixes preview bottom bar for small screen widths

View File

@@ -14,7 +14,7 @@ class Api {
public function apply() { public function apply() {
$action = Util::get_request_param("action"); $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)); Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported));
$methodname = "on_${action}"; $methodname = "on_${action}";
@@ -26,22 +26,22 @@ class Api {
$pass = Util::get_request_param("pass"); $pass = Util::get_request_param("pass");
$_SESSION[AS_ADMIN_SESSION_KEY] = sha1($pass) === PASSHASH; $_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() { private function on_logout() {
$_SESSION[AS_ADMIN_SESSION_KEY] = false; $_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() { 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)) { if (Util::get_boolean_request_param($name, false)) {
$methodname = "get_${name}"; $methodname = "get_${name}";

View File

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

View File

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

View File

@@ -70,7 +70,7 @@ class Bootstrap {
} }
define("SERVER_NAME", $server_name); define("SERVER_NAME", $server_name);
define("SERVER_VERSION", $server_version); 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"; $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); $cmds[$c] = ($cmd !== false) && Util::exec_0($cmd . " " . $c);
} }

View File

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

View File

@@ -2,9 +2,9 @@
class Thumb { class Thumb {
private static $FFMPEG_CMDV = array("ffmpeg", "-ss", "0:00:10", "-i", "[SRC]", "-an", "-vframes", "1", "[DEST]"); private static $FFMPEG_CMDV = ["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 $AVCONV_CMDV = ["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 $CONVERT_CMDV = ["convert", "-density", "200", "-quality", "100", "-sharpen", "0x1.0", "-strip", "[SRC][0]", "[DEST]"];
private static $THUMB_CACHE = "thumbs"; private static $THUMB_CACHE = "thumbs";
@@ -240,7 +240,7 @@ class Image {
$this->source = imagerotate($this->source, $angle, 0); $this->source = imagerotate($this->source, $angle, 0);
if ( $angle === 90 || $angle === 270 ) { 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"); header("Content-type: application/json;charset=utf-8");
echo json_encode($obj); echo json_encode($obj);
@@ -27,7 +27,7 @@ class Util {
public static function json_fail($err, $msg = "", $cond = true) { public static function json_fail($err, $msg = "", $cond = true) {
if ($cond) { 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() { public static function is_post_request() {
// Logger::log("POSTED", $_POST);
return (strtolower($_SERVER["REQUEST_METHOD"]) === "post"); return (strtolower($_SERVER["REQUEST_METHOD"]) === "post");
} }
@@ -87,7 +86,7 @@ class Util {
public static function load_commented_json($path) { public static function load_commented_json($path) {
if (!file_exists($path)) { if (!file_exists($path)) {
return array(); return [];
} }
$content = file_get_contents($path); $content = file_get_contents($path);
@@ -121,7 +120,7 @@ class Util {
} }
$cmd = implode(" ", array_map("escapeshellarg", $cmdv)); $cmd = implode(" ", array_map("escapeshellarg", $cmdv));
$lines = array(); $lines = [];
$rc = null; $rc = null;
exec($cmd, $lines, $rc); exec($cmd, $lines, $rc);
return implode("\n", $lines); return implode("\n", $lines);
@@ -130,7 +129,7 @@ class Util {
public static function exec_0($cmd) { public static function exec_0($cmd) {
$lines = array(); $lines = [];
$rc = null; $rc = null;
try { try {
@exec($cmd, $lines, $rc); @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) { public static function filesize($app, $path) {
@@ -184,7 +183,7 @@ class Util {
if ($app->get_option("foldersize.enabled", false)) { if ($app->get_option("foldersize.enabled", false)) {
if (HAS_CMD_DU && $app->get_option("foldersize.type", null) === "shell-du") { 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; $size = intval(preg_replace("#\s.*$#", "", Util::exec_cmdv($cmdv)), 10) * 1024;
} else { } else {
$size = 0; $size = 0;