More refactorings.

This commit is contained in:
Lars Jung 2014-05-18 00:10:38 +02:00
parent c40fac67d0
commit fc9f846cb5
13 changed files with 248 additions and 212 deletions

View file

@ -22,6 +22,12 @@ class Api {
$response = array();
if (has_request_param("setup")) {
use_request_param("setup");
$response["setup"] = $this->app->get_setup();
}
if (has_request_param("options")) {
use_request_param("options");
@ -48,32 +54,20 @@ class Api {
$response["l10n"] = $this->app->get_l10n($iso_codes);
}
if (has_request_param("checks")) {
use_request_param("checks");
$response["checks"] = $this->app->get_server_checks();
}
if (has_request_param("server")) {
use_request_param("server");
$response["server"] = $this->app->get_server_details();
}
if (has_request_param("custom")) {
use_request_param("custom");
$abs_href = use_request_param("customHref", null);
$response["custom"] = $this->app->get_customizations($abs_href);
$url = use_request_param("customHref");
$response["custom"] = $this->app->get_customizations($url);
}
if (has_request_param("items")) {
use_request_param("items");
$abs_href = use_request_param("itemsHref", null);
$what = use_request_param("itemsWhat", null);
$url = use_request_param("itemsHref");
$what = use_request_param("itemsWhat");
$what = is_numeric($what) ? intval($what, 10) : 1;
$response["items"] = $this->app->get_items($abs_href, $what);
$response["items"] = $this->app->get_items($url, $what);
}
if (count($_REQUEST)) {
@ -90,7 +84,7 @@ class Api {
json_fail(1, "thumbnails disabled");
}
normalized_require_once("Thumb.php");
normalized_require_once("class-thumb");
if (!Thumb::is_supported()) {
json_fail(2, "thumbnails not supported");
}
@ -119,7 +113,7 @@ class Api {
$type = use_request_param("type");
$hrefs = use_request_param("hrefs");
normalized_require_once("Archive.php");
normalized_require_once("class-archive");
$archive = new Archive($this->app);
$hrefs = explode("|:|", trim($hrefs));

View file

@ -20,6 +20,14 @@ class App {
}
public function get_setup() {
$consts = get_defined_constants(true)['user'];
$consts['PHP_VERSION'] = PHP_VERSION;
return $consts;
}
public function to_url($path, $trailing_slash = true) {
$rel_path = substr($path, strlen(ROOT_PATH));
@ -232,56 +240,6 @@ class App {
}
public function get_server_checks() {
$results = array();
$results["path_index"] = INDEX_URL;
$results["path_cache_writable"] = @is_writable(CACHE_PATH);
$results["php_version"] = PHP_VERSION;
$results["php_version_supported"] = IS_PHP_VERSION_SUPPORTED;
$results["php_exif"] = function_exists("exif_thumbnail");
$php_jpg = false;
if (function_exists("gd_info")) {
$infos = gd_info();
$php_jpg = array_key_exists("JPG Support", $infos) && $infos["JPG Support"] || array_key_exists("JPEG Support", $infos) && $infos["JPEG Support"];
}
$results["php_jpg"] = $php_jpg;
$check_cmd = IS_WIN_OS ? "which" : "command -v";
foreach (array("tar", "zip", "convert", "ffmpeg", "avconv", "du") as $cmd) {
$results["cmd_" . $cmd] = @preg_match("#" . $cmd . "(.exe)?$#i", exec_cmd($check_cmd . " " . $cmd)) > 0;
}
$results["cmd_ffmpeg_or_avconv"] = $results["cmd_ffmpeg"] || $results["cmd_avconv"];
$results["is_win_os"] = IS_WIN_OS;
$results["is_supported_php"] = IS_PHP_VERSION_SUPPORTED;
$results["server_name"] = SERVER_NAME;
$results["server_version"] = SERVER_VERSION;
$results["app_url"] = APP_URL;
$results["app_path"] = APP_PATH;
$results["root_url"] = ROOT_URL;
$results["root_path"] = ROOT_PATH;
$results["current_url"] = CURRENT_URL;
$results["current_path"] = CURRENT_PATH;
$results["options"] = $this->options;
return $results;
}
public function get_server_details() {
return array(
"backend" => "php",
"api" => true,
"name" => SERVER_NAME,
"version" => SERVER_VERSION
);
}
public function get_customizations($url) {
if (!$this->options["custom"]["enabled"]) {

View file

@ -2,8 +2,6 @@
class Item {
private static $FOLDER_SIZE_CMD = "du -sk [DIR]";
public static function cmp($item1, $item2) {
if ($item1->is_folder && !$item2->is_folder) {
@ -57,8 +55,8 @@ class Item {
$this->size = null;
$options = $app->get_options();
if ($options["foldersize"]["enabled"]) {
$cmd = str_replace("[DIR]", escapeshellarg($this->path), Item::$FOLDER_SIZE_CMD);
$this->size = intval(preg_replace("#\s.*$#", "", exec_cmd($cmd)), 10) * 1024;
$cmdv = array("du", "-sk", $this->path);
$this->size = intval(preg_replace("#\s.*$#", "", exec_cmdv($cmdv)), 10) * 1024;
}
} else {
$this->size = @filesize($this->path);

View file

@ -93,6 +93,14 @@ class Thumb {
$capture_path = $this->thumbs_path . "/capture-" . sha1($source_path) . ".jpg";
if (!file_exists($capture_path) || filemtime($source_path) >= filemtime($capture_path)) {
// if ($type === "mov") {
// $cmdv = array("ffmpeg", "-ss", "0:01:00", "-i", $source_path, "-an", "-vframes", "1", $capture_path);
// $cmdv = array("avconv", "-ss", "0:01:00", "-i", $source_path, "-an", "-vframes", "1", $capture_path);
// } else if ($type === "doc") {
// $cmdv = array("convert", "-strip", $source_path, $capture_path);
// }
$cmd = str_replace("[SOURCE]", escapeshellarg($source_path), $cmd);
$cmd = str_replace("[TARGET]", escapeshellarg($capture_path), $cmd);
exec_cmd($cmd);

View file

@ -1,26 +1,33 @@
<?php
function refl($id, $obj) {
$s = "<pre class='refl'>";
$s .= $id . " " . var_export($obj, true);
$s .= "</pre>\n";
echo($s);
}
function setup() {
function init() {
// MISC
putenv("LANG=en_US.UTF-8");
setlocale(LC_CTYPE, "en_US.UTF-8");
date_default_timezone_set("UTC");
define("BACKEND", "PHP");
define("API", true);
define("MAGIC_SEQUENCE", "={{pkg.name}}=");
define("FILE_PREFIX", "_{{pkg.name}}");
// PHP
define("MIN_PHP_VERSION", "5.3.0");
define("IS_PHP_VERSION_SUPPORTED", version_compare(PHP_VERSION, MIN_PHP_VERSION) >= 0);
define("HAS_PHP_VERSION", version_compare(PHP_VERSION, MIN_PHP_VERSION) >= 0);
define("HAS_PHP_EXIF", function_exists("exif_thumbnail"));
$has_php_jpg = false;
if (function_exists("gd_info")) {
$infos = gd_info();
$has_php_jpg = array_key_exists("JPG Support", $infos) && $infos["JPG Support"] || array_key_exists("JPEG Support", $infos) && $infos["JPEG Support"];
}
define("HAS_PHP_JPG", $has_php_jpg);
define("IS_WIN_OS", strtolower(substr(PHP_OS, 0, 3)) === "win");
// SERVER
$server_name = null;
$server_version = null;
$server_software = getenv("SERVER_SOFTWARE");
@ -30,7 +37,10 @@ function init() {
}
define("SERVER_NAME", $server_name);
define("SERVER_VERSION", $server_version);
define("HAS_WIN_OS", strtolower(substr(PHP_OS, 0, 3)) === "win");
// PATHS
$script_name = getenv("SCRIPT_NAME");
if (SERVER_NAME === "lighttpd") {
$script_name = preg_replace("#^.*//#", "/", $script_name);
@ -52,58 +62,18 @@ function init() {
define("CURRENT_URL", $cur_url);
define("CURRENT_PATH", $cur_path);
define("MAGIC_SEQUENCE", "={{pkg.name}}=");
define("FILE_PREFIX", "_{{pkg.name}}");
define("INDEX_URL", normalize_path(APP_URL . "server/php/index.php", false));
define("CACHE_URL", normalize_path(APP_URL . "cache", true));
define("CACHE_PATH", normalize_path(APP_PATH . "/cache", false));
// refl('DEFINED', array(
// "PHP_VERSION " => PHP_VERSION,
// "PHP_OS " => PHP_OS,
// "MIN_PHP_VERSION " => MIN_PHP_VERSION,
// "IS_PHP_VERSION_SUPPORTED " => IS_PHP_VERSION_SUPPORTED,
// "IS_WIN_OS " => IS_WIN_OS,
// "SERVER_NAME " => SERVER_NAME,
// "SERVER_VERSION " => SERVER_VERSION,
// "APP_URL " => APP_URL,
// "APP_PATH " => APP_PATH,
// "ROOT_URL " => ROOT_URL,
// "ROOT_PATH " => ROOT_PATH,
// "CURRENT_URL " => CURRENT_URL,
// "CURRENT_PATH " => CURRENT_PATH,
// "MAGIC_SEQUENCE " => MAGIC_SEQUENCE,
// "FILE_PREFIX " => FILE_PREFIX,
// "INDEX_URL " => INDEX_URL,
// "CACHE_URL " => CACHE_URL,
// "CACHE_PATH " => CACHE_PATH
// ));
// exit();
}
init();
normalized_require_once("util.php");
normalized_require_once("App.php");
normalized_require_once("Item.php");
$app = new App();
define("HAS_WRITABLE_CACHE", @is_writable(CACHE_PATH));
if (has_request_param("action")) {
header("Content-type: application/json;charset=utf-8");
normalized_require_once("Api.php");
$api = new Api($app);
$api->apply();
} else {
header("Content-type: text/html;charset=utf-8");
define("FALLBACK", $app->get_fallback());
normalized_require_once("page.php");
// EXTERNAL COMMANDS
foreach (array("tar", "zip", "convert", "ffmpeg", "avconv", "du") as $cmd) {
$cmdv = HAS_WIN_OS ? array("which", $cmd) : array("command", "-v", $cmd);
define("HAS_CMD_" . strtoupper($cmd), @preg_match("#" . $cmd . "(.exe)?$#i", exec_cmdv($cmdv)) > 0);
}
}
?>

View file

@ -1,5 +1,6 @@
<?php
function json_exit($obj = array()) {
$obj["code"] = 0;
@ -7,6 +8,7 @@ function json_exit($obj = array()) {
exit;
}
function json_fail($code, $msg = "", $cond = true) {
if ($cond) {
@ -15,12 +17,15 @@ function json_fail($code, $msg = "", $cond = true) {
}
}
function has_request_param($key) {
return array_key_exists($key, $_REQUEST);
}
define("NO_DEFAULT", "__NO_DEFAULT_VALUE__");
function use_request_param($key, $default = NO_DEFAULT) {
if (!array_key_exists($key, $_REQUEST)) {
@ -33,16 +38,19 @@ function use_request_param($key, $default = NO_DEFAULT) {
return $value;
}
function starts_with($sequence, $head) {
return substr($sequence, 0, strlen($head)) === $head;
}
function ends_with($sequence, $tail) {
return substr($sequence, -strlen($tail)) === $tail;
}
function load_commented_json($file) {
if (!file_exists($file)) {
@ -57,6 +65,7 @@ function load_commented_json($file) {
return json_decode($str, true);
}
function exec_cmd($cmd) {
$lines = array();
@ -65,6 +74,7 @@ function exec_cmd($cmd) {
return implode("\n", $lines);
}
function passthru_cmd($cmd) {
$rc = null;
@ -72,4 +82,45 @@ function passthru_cmd($cmd) {
return $rc;
}
function exec_cmdv($cmdv) {
if (!is_array($cmdv)) {
$cmdv = func_get_args();
}
$cmd = implode(" ", array_map("escapeshellarg", $cmdv));
return exec_cmd($cmd);
}
// debug tools
function err_log($message, $obj = null) {
error_log($message . ": " . var_export($obj, true));
}
function scr_log($message, $obj = null) {
echo("<pre>" . $message . ": " . var_export($obj, true) . "</pre>\n");
}
global $__TIMER_START, $__TIMER_LAST;
$__TIMER_START = microtime(true);
$__TIMER_LAST = $__TIMER_START;
function time_log($message) {
global $__TIMER_START, $__TIMER_LAST;
$now = microtime(true);
if ($__TIMER_START === $__TIMER_LAST) {
error_log("-----------------------------");
function timer_shutdown() { time_log('ex'); }
register_shutdown_function('timer_shutdown');
}
error_log($message . " DT " . number_format($now - $__TIMER_LAST, 5) . " TT " . number_format($now - $__TIMER_START, 5));
$__TIMER_LAST = $now;
}
?>