From b67c22f33ba9a6b803350ea99b8e4e16a8dd98cc Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Mon, 12 May 2014 00:39:27 +0200 Subject: [PATCH] Refactoring PHP init code. --- src/_h5ai/client/css/inc/h5ai-info.less | 4 +- src/_h5ai/client/js/inc/info.js | 4 +- src/_h5ai/index.html.jade | 20 ++--- src/_h5ai/server/php/inc/App.php | 97 ++++++++++++++++--------- src/_h5ai/server/php/index.php | 22 +----- 5 files changed, 79 insertions(+), 68 deletions(-) diff --git a/src/_h5ai/client/css/inc/h5ai-info.less b/src/_h5ai/client/css/inc/h5ai-info.less index cafce35e..d33a2f9d 100644 --- a/src/_h5ai/client/css/inc/h5ai-info.less +++ b/src/_h5ai/client/css/inc/h5ai-info.less @@ -71,7 +71,7 @@ body#h5ai-info { .test { .label { display: inline-block; - width: 350px; + width: 370px; } .result { display: inline-block; @@ -91,7 +91,7 @@ body#h5ai-info { margin: 4px 0 12px 12px; font-size: 0.7em; color: #aaa; - width: 350px; + width: 370px; line-height: 1.2em; } } diff --git a/src/_h5ai/client/js/inc/info.js b/src/_h5ai/client/js/inc/info.js index 8097b7f8..e127ff57 100644 --- a/src/_h5ai/client/js/inc/info.js +++ b/src/_h5ai/client/js/inc/info.js @@ -17,12 +17,12 @@ modulejs.define('info', ['$'], function ($) { $.getJSON('server/php/index.php', {action: 'get', checks: true}, function (json) { if (json) { - $('.idx-file .value').text(json.checks['idx']); + $('.idx-file .value').text(json.checks['path_index']); $('.test').each(function () { setCheckResult(this, json.checks[$(this).data('id')]); }); - $('.test.php .result').text(json.checks['phpversion']); + $('.test.php .result').text(json.checks['php_version']); } }); }; diff --git a/src/_h5ai/index.html.jade b/src/_h5ai/index.html.jade index 9e7f2409..8bf0e8cb 100644 --- a/src/_h5ai/index.html.jade +++ b/src/_h5ai/index.html.jade @@ -32,23 +32,23 @@ html.no-js.browser( lang="en" ) h2 Server Details ul#tests - li.test.php( data-id="php" ) + li.test.php( data-id="php_version_supported" ) span.label PHP version span.result ? - div.info PHP version >= 5.2.1 - li.test( data-id="cache" ) + div.info PHP version >= 5.3.0 + li.test( data-id="path_cache_writable" ) span.label Cache directory span.result ? div.info Web server has write access - li.test( data-id="thumbs" ) + li.test( data-id="php_jpg" ) span.label Image thumbs span.result ? div.info PHP GD extension with JPEG support available - li.test( data-id="exif" ) + li.test( data-id="php_exif" ) span.label Use EXIF thumbs span.result ? div.info PHP EXIF extension available - li.test( data-id="moviethumbs" ) + li.test( data-id="cmd_ffmpeg_or_avconv" ) 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="convert" ) + li.test( data-id="cmd_convert" ) span.label PDF thumbs span.result ? div.info | Command line program code convert | available - li.test( data-id="tar" ) + li.test( data-id="cmd_tar" ) span.label Shell tar span.result ? div.info | Command line program code tar | available - li.test( data-id="zip" ) + li.test( data-id="cmd_zip" ) span.label Shell zip span.result ? div.info | Command line program code zip | available - li.test( data-id="du" ) + li.test( data-id="cmd_du" ) span.label Folder sizes span.result ? div.info diff --git a/src/_h5ai/server/php/inc/App.php b/src/_h5ai/server/php/inc/App.php index 040feda0..63166f34 100644 --- a/src/_h5ai/server/php/inc/App.php +++ b/src/_h5ai/server/php/inc/App.php @@ -5,17 +5,48 @@ class App { public static $MAGIC_SEQUENCE = "={{pkg.name}}="; public static $FILE_PREFIX = "_{{pkg.name}}"; + private static $MIN_PHP_VERSION = "5.3.0"; private static $RE_DELIMITER = "|"; private static $CACHE_DIR = "cache"; - private $app_abs_path, $root_abs_path, + private $is_win_os, $is_supported_php, + $server_name, $server_version, + $app_abs_path, $root_abs_path, $app_abs_href, $root_abs_href, $abs_href, $abs_path, $options; - public function __construct($app_abs_path, $app_abs_href, $abs_href) { + public static function from_env() { + + $server_name = null; + $server_version = null; + if (preg_match("#^(.*?)/(.*?)(?: |$)#", strtolower(getenv("SERVER_SOFTWARE")), $matches)) { + $server_name = $matches[1]; + $server_version = $matches[2]; + } + + $script_name = getenv("SCRIPT_NAME"); + if ($server_name === "lighttpd") { + $script_name = preg_replace("#^.*//#", "/", $script_name); + } + $app_abs_href = dirname(dirname(dirname($script_name))); + + $url_parts = parse_url(getenv("REQUEST_URI")); + $abs_href = $url_parts["path"]; + + return new App($server_name, $server_version, APP_ABS_PATH, $app_abs_href, $abs_href); + } + + + public function __construct($server_name, $server_version, $app_abs_path, $app_abs_href, $abs_href) { + + $this->is_win_os = strtolower(substr(PHP_OS, 0, 3)) === "win"; + $this->is_supported_php = version_compare(PHP_VERSION, App::$MIN_PHP_VERSION) >= 0; + + $this->server_name = strtolower($server_name); + $this->server_version = strtolower($server_version); $this->app_abs_path = normalize_path($app_abs_path); $this->root_abs_path = normalize_path(dirname($this->app_abs_path)); @@ -293,41 +324,40 @@ class App { public function get_server_checks() { - $php = version_compare(PHP_VERSION, "5.2.1") >= 0; + $results = array(); + + $results["path_index"] = $this->app_abs_href . "server/php/index.php"; + $results["php_version"] = PHP_VERSION; + $results["php_version_supported"] = $this->is_supported_php; + $results["php_exif"] = function_exists("exif_thumbnail"); + $results["path_cache_writable"] = @is_writable($this->get_cache_abs_path()); + $gd = false; if (function_exists("gd_info")) { $gdinfo = gd_info(); $gd = array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"]; } - $exif = function_exists("exif_thumbnail"); - $cache = @is_writable($this->get_cache_abs_path()); - if (strtoupper(substr(PHP_OS, 0, 3)) === "WIN") { - $cmd = "which"; - } else { - $cmd = "command -v"; - } - $tar = @preg_match("/tar(.exe)?$/i", exec_cmd($cmd . " tar")) > 0; - $zip = @preg_match("/zip(.exe)?$/i", exec_cmd($cmd . " zip")) > 0; - $convert = @preg_match("/convert(.exe)?$/i", exec_cmd($cmd . " convert")) > 0; - $ffmpeg = @preg_match("/ffmpeg(.exe)?$/i", exec_cmd($cmd . " ffmpeg")) > 0; - $avconv = @preg_match("/avconv(.exe)?$/i", exec_cmd($cmd . " avconv")) > 0; - $du = @preg_match("/du(.exe)?$/i", exec_cmd($cmd . " du")) > 0; + $results["php_jpg"] = $gd; - return array( - "idx" => $this->app_abs_href . "server/php/index.php", - "phpversion" => PHP_VERSION, - "php" => $php, - "cache" => $cache, - "thumbs" => $gd, - "exif" => $exif, - "tar" => $tar, - "zip" => $zip, - "convert" => $convert, - "ffmpeg" => $ffmpeg, - "avconv" => $avconv, - "moviethumbs" => $ffmpeg || $avconv, - "du" => $du - ); + $check_cmd = $this->is_win_os ? "which" : "command -v"; + foreach (array("tar", "zip", "convert", "ffmpeg", "avconv", "du") as $c) { + $results["cmd_" . $c] = @preg_match("#" . $c . "(.exe)?$#i", exec_cmd($check_cmd . " " . $c)) > 0; + } + $results["cmd_ffmpeg_or_avconv"] = $results["cmd_ffmpeg"] || $results["cmd_avconv"]; + + $results["is_win_os"] = $this->is_win_os; + $results["is_supported_php"] = $this->is_supported_php; + $results["server_name"] = $this->server_name; + $results["server_version"] = $this->server_version; + $results["app_abs_path"] = $this->app_abs_path; + $results["root_abs_path"] = $this->root_abs_path; + $results["app_abs_href"] = $this->app_abs_href; + $results["root_abs_href"] = $this->root_abs_href; + $results["abs_href"] = $this->abs_href; + $results["abs_path"] = $this->abs_path; + $results["options"] = $this->options; + + return $results; } @@ -335,9 +365,8 @@ class App { return array( "backend" => "php", - "api" => true, - "name" => strtolower(preg_replace("/\\/.*$/", "", getenv("SERVER_SOFTWARE"))), - "version" => strtolower(preg_replace("/^.*\\//", "", preg_replace("/\\s.*$/", "", getenv("SERVER_SOFTWARE")))) + "name" => $this->server_name, + "version" => $this->server_version ); } diff --git a/src/_h5ai/server/php/index.php b/src/_h5ai/server/php/index.php index 967d49b1..78b177c7 100644 --- a/src/_h5ai/server/php/index.php +++ b/src/_h5ai/server/php/index.php @@ -10,11 +10,6 @@ function normalize_path($path, $trailing_slash = false) { } define("APP_ABS_PATH", normalize_path(dirname(dirname(dirname(__FILE__))))); -// define("APP_ABS_HREF", normalize_path(dirname(dirname(dirname(getenv("SCRIPT_NAME")))), true)); -define("APP_ABS_HREF", normalize_path(dirname(dirname(dirname(preg_replace('#^.*//#', '/', getenv("SCRIPT_NAME"))))), true)); // fixes lighttpd issues -$url_parts = parse_url(getenv("REQUEST_URI")); -define("ABS_HREF", normalize_path($url_parts["path"]), true); - function normalized_require_once($lib) { @@ -22,25 +17,12 @@ function normalized_require_once($lib) { } -/* Fast exit on version check */ - -if (array_key_exists("version", $_REQUEST)) { - - echo json_encode(array("code" => 0, "version" => "{{pkg.version}}", "href" => APP_ABS_HREF)); - exit; -} - - -/* Load Libs */ +/* Init */ normalized_require_once("/server/php/inc/util.php"); normalized_require_once("/server/php/inc/App.php"); normalized_require_once("/server/php/inc/Item.php"); - - -/* Init */ - -$app = new App(APP_ABS_PATH, APP_ABS_HREF, ABS_HREF); +$app = App::from_env(); /* Run */