Refactoring PHP init code.

This commit is contained in:
Lars Jung 2014-05-12 00:39:27 +02:00
parent 5043a5188b
commit b67c22f33b
5 changed files with 79 additions and 68 deletions

View file

@ -71,7 +71,7 @@ body#h5ai-info {
.test { .test {
.label { .label {
display: inline-block; display: inline-block;
width: 350px; width: 370px;
} }
.result { .result {
display: inline-block; display: inline-block;
@ -91,7 +91,7 @@ body#h5ai-info {
margin: 4px 0 12px 12px; margin: 4px 0 12px 12px;
font-size: 0.7em; font-size: 0.7em;
color: #aaa; color: #aaa;
width: 350px; width: 370px;
line-height: 1.2em; line-height: 1.2em;
} }
} }

View file

@ -17,12 +17,12 @@ modulejs.define('info', ['$'], function ($) {
$.getJSON('server/php/index.php', {action: 'get', checks: true}, function (json) { $.getJSON('server/php/index.php', {action: 'get', checks: true}, function (json) {
if (json) { if (json) {
$('.idx-file .value').text(json.checks['idx']); $('.idx-file .value').text(json.checks['path_index']);
$('.test').each(function () { $('.test').each(function () {
setCheckResult(this, json.checks[$(this).data('id')]); setCheckResult(this, json.checks[$(this).data('id')]);
}); });
$('.test.php .result').text(json.checks['phpversion']); $('.test.php .result').text(json.checks['php_version']);
} }
}); });
}; };

View file

@ -32,23 +32,23 @@ html.no-js.browser( lang="en" )
h2 Server Details h2 Server Details
ul#tests ul#tests
li.test.php( data-id="php" ) li.test.php( data-id="php_version_supported" )
span.label PHP version span.label PHP version
span.result ? span.result ?
div.info PHP version >= 5.2.1 div.info PHP version >= 5.3.0
li.test( data-id="cache" ) li.test( data-id="path_cache_writable" )
span.label Cache directory span.label Cache directory
span.result ? span.result ?
div.info Web server has write access div.info Web server has write access
li.test( data-id="thumbs" ) li.test( data-id="php_jpg" )
span.label Image thumbs span.label Image thumbs
span.result ? span.result ?
div.info PHP GD extension with JPEG support available 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.label Use EXIF thumbs
span.result ? span.result ?
div.info PHP EXIF extension available div.info PHP EXIF extension available
li.test( data-id="moviethumbs" ) li.test( data-id="cmd_ffmpeg_or_avconv" )
span.label Movie thumbs span.label Movie thumbs
span.result ? span.result ?
div.info div.info
@ -57,28 +57,28 @@ html.no-js.browser( lang="en" )
| or | or
code avconv code avconv
| available | available
li.test( data-id="convert" ) li.test( data-id="cmd_convert" )
span.label PDF thumbs span.label PDF thumbs
span.result ? span.result ?
div.info div.info
| Command line program | Command line program
code convert code convert
| available | available
li.test( data-id="tar" ) li.test( data-id="cmd_tar" )
span.label Shell tar span.label Shell tar
span.result ? span.result ?
div.info div.info
| Command line program | Command line program
code tar code tar
| available | available
li.test( data-id="zip" ) li.test( data-id="cmd_zip" )
span.label Shell zip span.label Shell zip
span.result ? span.result ?
div.info div.info
| Command line program | Command line program
code zip code zip
| available | available
li.test( data-id="du" ) li.test( data-id="cmd_du" )
span.label Folder sizes span.label Folder sizes
span.result ? span.result ?
div.info div.info

View file

@ -5,17 +5,48 @@ class App {
public static $MAGIC_SEQUENCE = "={{pkg.name}}="; public static $MAGIC_SEQUENCE = "={{pkg.name}}=";
public static $FILE_PREFIX = "_{{pkg.name}}"; public static $FILE_PREFIX = "_{{pkg.name}}";
private static $MIN_PHP_VERSION = "5.3.0";
private static $RE_DELIMITER = "|"; private static $RE_DELIMITER = "|";
private static $CACHE_DIR = "cache"; 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, $app_abs_href, $root_abs_href,
$abs_href, $abs_path, $abs_href, $abs_path,
$options; $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->app_abs_path = normalize_path($app_abs_path);
$this->root_abs_path = normalize_path(dirname($this->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() { 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; $gd = false;
if (function_exists("gd_info")) { if (function_exists("gd_info")) {
$gdinfo = gd_info(); $gdinfo = gd_info();
$gd = array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"]; $gd = array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"];
} }
$exif = function_exists("exif_thumbnail"); $results["php_jpg"] = $gd;
$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;
return array( $check_cmd = $this->is_win_os ? "which" : "command -v";
"idx" => $this->app_abs_href . "server/php/index.php", foreach (array("tar", "zip", "convert", "ffmpeg", "avconv", "du") as $c) {
"phpversion" => PHP_VERSION, $results["cmd_" . $c] = @preg_match("#" . $c . "(.exe)?$#i", exec_cmd($check_cmd . " " . $c)) > 0;
"php" => $php, }
"cache" => $cache, $results["cmd_ffmpeg_or_avconv"] = $results["cmd_ffmpeg"] || $results["cmd_avconv"];
"thumbs" => $gd,
"exif" => $exif, $results["is_win_os"] = $this->is_win_os;
"tar" => $tar, $results["is_supported_php"] = $this->is_supported_php;
"zip" => $zip, $results["server_name"] = $this->server_name;
"convert" => $convert, $results["server_version"] = $this->server_version;
"ffmpeg" => $ffmpeg, $results["app_abs_path"] = $this->app_abs_path;
"avconv" => $avconv, $results["root_abs_path"] = $this->root_abs_path;
"moviethumbs" => $ffmpeg || $avconv, $results["app_abs_href"] = $this->app_abs_href;
"du" => $du $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( return array(
"backend" => "php", "backend" => "php",
"api" => true, "name" => $this->server_name,
"name" => strtolower(preg_replace("/\\/.*$/", "", getenv("SERVER_SOFTWARE"))), "version" => $this->server_version
"version" => strtolower(preg_replace("/^.*\\//", "", preg_replace("/\\s.*$/", "", getenv("SERVER_SOFTWARE"))))
); );
} }

View file

@ -10,11 +10,6 @@ function normalize_path($path, $trailing_slash = false) {
} }
define("APP_ABS_PATH", normalize_path(dirname(dirname(dirname(__FILE__))))); 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) { function normalized_require_once($lib) {
@ -22,25 +17,12 @@ function normalized_require_once($lib) {
} }
/* Fast exit on version check */ /* Init */
if (array_key_exists("version", $_REQUEST)) {
echo json_encode(array("code" => 0, "version" => "{{pkg.version}}", "href" => APP_ABS_HREF));
exit;
}
/* Load Libs */
normalized_require_once("/server/php/inc/util.php"); normalized_require_once("/server/php/inc/util.php");
normalized_require_once("/server/php/inc/App.php"); normalized_require_once("/server/php/inc/App.php");
normalized_require_once("/server/php/inc/Item.php"); normalized_require_once("/server/php/inc/Item.php");
$app = App::from_env();
/* Init */
$app = new App(APP_ABS_PATH, APP_ABS_HREF, ABS_HREF);
/* Run */ /* Run */