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

@ -1,46 +1,53 @@
modulejs.define('core/server', ['$', '_', 'config', 'core/location'], function ($, _, config, location) {
var server = _.extend({}, config.server, {
var server = {
request: function (data, callback) {
backend: config.setup.BACKEND,
api: config.setup.API === true,
name: config.setup.SERVER_NAME,
version: config.setup.SERVER_VERSION,
if (server.api) {
$.ajax({
url: location.getAbsHref(),
data: data,
type: 'POST',
dataType: 'json',
success: function (json) {
request: function (data, callback) {
callback(json);
},
error: function () {
if (server.api) {
$.ajax({
url: location.getAbsHref(),
data: data,
type: 'POST',
dataType: 'json',
success: function (json) {
callback();
}
});
} else {
callback();
callback(json);
},
error: function () {
callback();
}
});
} else {
callback();
}
},
formRequest: function (data) {
if (server.api) {
var $form = $('<form method="post" style="display:none;"/>')
.attr('action', location.getAbsHref());
_.each(data, function (val, key) {
$('<input type="hidden"/>')
.attr('name', key)
.attr('value', val)
.appendTo($form);
});
$form.appendTo('body').submit().remove();
}
}
},
formRequest: function (data) {
var $form = $('<form method="post" style="display:none;"/>')
.attr('action', location.getAbsHref());
_.each(data, function (val, key) {
$('<input type="hidden"/>')
.attr('name', key)
.attr('value', val)
.appendTo($form);
});
$form.appendTo('body').submit().remove();
}
});
};
return server;
});

View File

@ -5,9 +5,9 @@ modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/e
enabled: false
}, allsettings.custom),
onLocationChanged = function () {
onLocationChanged = function (item) {
server.request({action: 'get', custom: true}, function (response) {
server.request({action: 'get', custom: true, customHref: item.absHref}, function (response) {
var h, f;
if (response) {

View File

@ -1,7 +1,22 @@
modulejs.define('info', ['$'], function ($) {
modulejs.define('info', ['$', 'config'], function ($, config) {
var setCheckResult = function (el, result) {
var map = function (setup) {
return {
'php_version': setup['HAS_PHP_VERSION'],
'cache_dir': setup['HAS_WRITABLE_CACHE'],
'image_thumbs': setup['HAS_PHP_JPG'],
'exif_thumbs': setup['HAS_PHP_EXIF'],
'movie_thumbs': setup['HAS_CMD_FFMPEG'] || setup['HAS_CMD_AVCONV'],
'pdf_thumbs': setup['HAS_CMD_CONVERT'],
'shell_tar': setup['HAS_CMD_TAR'],
'shell_zip': setup['HAS_CMD_ZIP'],
'folder_sizes': setup['HAS_CMD_DU']
};
},
setValue = function (el, result) {
var $result = $(el).find('.result');
@ -14,17 +29,16 @@ modulejs.define('info', ['$'], function ($) {
init = function () {
$.getJSON('server/php/index.php', {action: 'get', checks: true}, function (json) {
var setup = config.setup,
values = map(setup);
if (json) {
$('.idx-file .value').text(json.checks['path_index']);
$('.test').each(function () {
$('.test').each(function () {
setCheckResult(this, json.checks[$(this).data('id')]);
});
$('.test.php .result').text(json.checks['php_version']);
}
setValue(this, values[$(this).data('id')]);
});
$('.idx-file .value').text(setup['INDEX_URL']);
$('.test.php .result').text(setup['PHP_VERSION']);
};
init();

View File

@ -31,27 +31,29 @@
// @include "inc/**/*.js"
var $ = jQuery,
mode = $('script[src$="scripts.js"]').data('mode');
mode = $('script[src$="scripts.js"]').data('mode'),
url = '.',
module = 'main';
if ($('html').hasClass('no-browser')) {
} else if (mode === 'info') {
$(function () { modulejs.require('info'); });
} else {
$.ajax({
url: '.',
data: {action: 'get', options: true, types: true, langs: true, server: true},
type: 'POST',
dataType: 'json',
success: function (config) {
modulejs.define('config', config);
$(function () { modulejs.require('main'); });
}
});
return;
}
if (mode === 'info') {
url = 'server/php/index.php';
module = 'info';
}
$.ajax({
url: url,
data: {action: 'get', setup: true, options: true, types: true, langs: true},
type: 'POST',
dataType: 'json',
success: function (config) {
modulejs.define('config', config);
$(function () { modulejs.require(module); });
}
});
}());

View File

@ -8,7 +8,7 @@ html.no-js.browser( lang="en" )
head
meta( charset="utf-8" )
meta( http-equiv="X-UA-Compatible", content="IE=edge,chrome=1" )
title {{pkg.name}} {{pkg.version}} server details
title {{pkg.name}} {{pkg.version}} Server Setup
meta( name="description", content="{{pkg.name}} server details" )
meta( name="viewport", content="width=device-width" )
link( rel="shortcut icon", href="client/images/app-16x16.ico" )
@ -27,28 +27,28 @@ html.no-js.browser( lang="en" )
a( href="http://larsjung.de/h5ai/" ) {{pkg.name}}
span.build-version version {{pkg.version}}
span.build-stamp {{stamp}}
span.idx-file Index File:
span.idx-file Index:
code.value
h2 Server Details
h2 Server Setup
ul#tests
li.test.php( data-id="php_version_supported" )
li.test.php( data-id="php_version" )
span.label PHP version
span.result ?
div.info PHP version &gt;= 5.3.0
li.test( data-id="path_cache_writable" )
li.test( data-id="cache_dir" )
span.label Cache directory
span.result ?
div.info Web server has write access
li.test( data-id="php_jpg" )
li.test( data-id="image_thumbs" )
span.label Image thumbs
span.result ?
div.info PHP GD extension with JPEG support available
li.test( data-id="php_exif" )
li.test( data-id="exif_thumbs" )
span.label Use EXIF thumbs
span.result ?
div.info PHP EXIF extension available
li.test( data-id="cmd_ffmpeg_or_avconv" )
li.test( data-id="movie_thumbs" )
span.label Movie thumbs
span.result ?
div.info
@ -57,28 +57,28 @@ html.no-js.browser( lang="en" )
| or
code avconv
| available
li.test( data-id="cmd_convert" )
li.test( data-id="pdf_thumbs" )
span.label PDF thumbs
span.result ?
div.info
| Command line program
code convert
| available
li.test( data-id="cmd_tar" )
li.test( data-id="shell_tar" )
span.label Shell tar
span.result ?
div.info
| Command line program
code tar
| available
li.test( data-id="cmd_zip" )
li.test( data-id="shell_zip" )
span.label Shell zip
span.result ?
div.info
| Command line program
code zip
| available
li.test( data-id="cmd_du" )
li.test( data-id="folder_sizes" )
span.label Folder sizes
span.result ?
div.info

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

View File

@ -8,9 +8,43 @@ function normalize_path($path, $trailing_slash = false) {
function normalized_require_once($lib) {
require_once(normalize_path(dirname(__FILE__) . "/inc/" . $lib, false));
require_once(normalize_path(dirname(__FILE__) . "/inc/${lib}.php", false));
}
normalized_require_once("main.php");
normalized_require_once("util");
normalized_require_once("setup");
normalized_require_once("class-api");
normalized_require_once("class-app");
normalized_require_once("class-archive");
normalized_require_once("class-item");
normalized_require_once("class-thumb");
time_log(" 0");
setup();
time_log(" 1");
$app = new App();
// time_log(" 2");
// err_log('setup', $app->get_setup());
time_log(" 3");
if (has_request_param("action")) {
header("Content-type: application/json;charset=utf-8");
time_log("a1");
$api = new Api($app);
time_log("a2");
$api->apply();
} else {
header("Content-type: text/html;charset=utf-8");
time_log("i1");
define("FALLBACK", $app->get_fallback());
time_log("i2");
normalized_require_once("page");
}
?>