diff --git a/src/_h5ai/client/js/inc/core/server.js b/src/_h5ai/client/js/inc/core/server.js index 528c943c..ba215526 100644 --- a/src/_h5ai/client/js/inc/core/server.js +++ b/src/_h5ai/client/js/inc/core/server.js @@ -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 = $('
') + .attr('action', location.getAbsHref()); + + _.each(data, function (val, key) { + + $('') + .attr('name', key) + .attr('value', val) + .appendTo($form); + }); + + $form.appendTo('body').submit().remove(); + } } - }, - - formRequest: function (data) { - - var $form = $('') - .attr('action', location.getAbsHref()); - - _.each(data, function (val, key) { - - $('') - .attr('name', key) - .attr('value', val) - .appendTo($form); - }); - - $form.appendTo('body').submit().remove(); - } - }); + }; return server; }); diff --git a/src/_h5ai/client/js/inc/ext/custom.js b/src/_h5ai/client/js/inc/ext/custom.js index e2cc27a2..613165e5 100644 --- a/src/_h5ai/client/js/inc/ext/custom.js +++ b/src/_h5ai/client/js/inc/ext/custom.js @@ -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) { diff --git a/src/_h5ai/client/js/inc/info.js b/src/_h5ai/client/js/inc/info.js index e127ff57..12e1c91f 100644 --- a/src/_h5ai/client/js/inc/info.js +++ b/src/_h5ai/client/js/inc/info.js @@ -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(); diff --git a/src/_h5ai/client/js/scripts.js b/src/_h5ai/client/js/scripts.js index 7b454a7c..24fa44dd 100644 --- a/src/_h5ai/client/js/scripts.js +++ b/src/_h5ai/client/js/scripts.js @@ -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); }); + } + }); + }()); diff --git a/src/_h5ai/index.html.jade b/src/_h5ai/index.html.jade index 8bf0e8cb..ee42865e 100644 --- a/src/_h5ai/index.html.jade +++ b/src/_h5ai/index.html.jade @@ -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 >= 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 diff --git a/src/_h5ai/server/php/inc/Api.php b/src/_h5ai/server/php/inc/class-api.php similarity index 87% rename from src/_h5ai/server/php/inc/Api.php rename to src/_h5ai/server/php/inc/class-api.php index b9f855b4..914ff0a4 100644 --- a/src/_h5ai/server/php/inc/Api.php +++ b/src/_h5ai/server/php/inc/class-api.php @@ -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)); diff --git a/src/_h5ai/server/php/inc/App.php b/src/_h5ai/server/php/inc/class-app.php similarity index 79% rename from src/_h5ai/server/php/inc/App.php rename to src/_h5ai/server/php/inc/class-app.php index b6af8c74..3d02ce31 100644 --- a/src/_h5ai/server/php/inc/App.php +++ b/src/_h5ai/server/php/inc/class-app.php @@ -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"]) { diff --git a/src/_h5ai/server/php/inc/Archive.php b/src/_h5ai/server/php/inc/class-archive.php similarity index 100% rename from src/_h5ai/server/php/inc/Archive.php rename to src/_h5ai/server/php/inc/class-archive.php diff --git a/src/_h5ai/server/php/inc/Item.php b/src/_h5ai/server/php/inc/class-item.php similarity index 90% rename from src/_h5ai/server/php/inc/Item.php rename to src/_h5ai/server/php/inc/class-item.php index 531508c7..94d437da 100644 --- a/src/_h5ai/server/php/inc/Item.php +++ b/src/_h5ai/server/php/inc/class-item.php @@ -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); diff --git a/src/_h5ai/server/php/inc/Thumb.php b/src/_h5ai/server/php/inc/class-thumb.php similarity index 95% rename from src/_h5ai/server/php/inc/Thumb.php rename to src/_h5ai/server/php/inc/class-thumb.php index 5def0a58..2412046f 100644 --- a/src/_h5ai/server/php/inc/Thumb.php +++ b/src/_h5ai/server/php/inc/class-thumb.php @@ -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); diff --git a/src/_h5ai/server/php/inc/main.php b/src/_h5ai/server/php/inc/setup.php similarity index 51% rename from src/_h5ai/server/php/inc/main.php rename to src/_h5ai/server/php/inc/setup.php index bc3a8d02..b1107745 100644 --- a/src/_h5ai/server/php/inc/main.php +++ b/src/_h5ai/server/php/inc/setup.php @@ -1,26 +1,33 @@ "; - $s .= $id . " " . var_export($obj, true); - $s .= "\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); + } } ?> \ No newline at end of file diff --git a/src/_h5ai/server/php/inc/util.php b/src/_h5ai/server/php/inc/util.php index a47cc5ca..65b6c4b2 100644 --- a/src/_h5ai/server/php/inc/util.php +++ b/src/_h5ai/server/php/inc/util.php @@ -1,5 +1,6 @@ " . $message . ": " . var_export($obj, true) . "\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; +} + ?> \ No newline at end of file diff --git a/src/_h5ai/server/php/index.php b/src/_h5ai/server/php/index.php index 3bc97245..7ead7493 100644 --- a/src/_h5ai/server/php/index.php +++ b/src/_h5ai/server/php/index.php @@ -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"); +} ?> \ No newline at end of file