Refactoring.

This commit is contained in:
Lars Jung 2014-05-14 14:27:38 +02:00
parent b67c22f33b
commit fc8a0589dd
6 changed files with 99 additions and 62 deletions

View file

@ -3,6 +3,9 @@
class Api {
private $app;
public function __construct($app) {
$this->app = $app;
@ -19,50 +22,50 @@ class Api {
$response = array();
if (array_key_exists("options", $_REQUEST)) {
if (has_request_param("options")) {
use_request_params("options");
$response["options"] = $this->app->get_options();
}
if (array_key_exists("types", $_REQUEST)) {
if (has_request_param("types")) {
use_request_params("types");
$response["types"] = $this->app->get_types();
}
if (array_key_exists("langs", $_REQUEST)) {
if (has_request_param("langs")) {
use_request_params("langs");
$response["langs"] = $this->app->get_l10n_list();
}
if (array_key_exists("l10n", $_REQUEST)) {
if (has_request_param("l10n")) {
list($iso_codes) = use_request_params("l10nCodes", "l10n");
$iso_codes = explode(":", $iso_codes);
$response["l10n"] = $this->app->get_l10n($iso_codes);
}
if (array_key_exists("checks", $_REQUEST)) {
if (has_request_param("checks")) {
use_request_params("checks");
$response["checks"] = $this->app->get_server_checks();
}
if (array_key_exists("server", $_REQUEST)) {
if (has_request_param("server")) {
use_request_params("server");
$response["server"] = $this->app->get_server_details();
}
if (array_key_exists("custom", $_REQUEST)) {
if (has_request_param("custom")) {
list($abs_href) = use_optional_request_params("customHref", "custom");
$response["custom"] = $this->app->get_customizations($abs_href);
}
if (array_key_exists("items", $_REQUEST)) {
if (has_request_param("items")) {
list($abs_href, $what) = use_optional_request_params("itemsHref", "itemsWhat", "items");
$what = is_numeric($what) ? intval($what, 10) : 1;
@ -83,7 +86,7 @@ class Api {
json_fail(1, "thumbnails disabled");
}
normalized_require_once("/server/php/inc/Thumb.php");
normalized_require_once("Thumb.php");
if (!Thumb::is_supported()) {
json_fail(2, "thumbnails not supported");
}
@ -106,7 +109,7 @@ class Api {
list($as, $type, $hrefs) = use_request_params(array("as", "type", "hrefs"));
normalized_require_once("/server/php/inc/Archive.php");
normalized_require_once("Archive.php");
$archive = new Archive($this->app);
$hrefs = explode("|:|", trim($hrefs));

View file

@ -27,6 +27,8 @@ class App {
$server_version = $matches[2];
}
$app_abs_path = normalize_path(dirname(dirname(dirname(dirname(__FILE__)))));
$script_name = getenv("SCRIPT_NAME");
if ($server_name === "lighttpd") {
$script_name = preg_replace("#^.*//#", "/", $script_name);
@ -36,7 +38,7 @@ class App {
$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);
return new App($server_name, $server_version, $app_abs_path, $app_abs_href, $abs_href);
}
@ -57,6 +59,10 @@ class App {
$this->abs_href = normalize_path($abs_href, true);
$this->abs_path = $this->get_abs_path($this->abs_href);
// echo("<pre>");
// var_dump($this);
// exit();
$this->options = load_commented_json($this->app_abs_path . "/conf/options.json");
}
@ -235,8 +241,6 @@ class App {
public function get_fallback() {
date_default_timezone_set("UTC");
$cache = array();
$folder = Item::get($this, $this->abs_path, $cache);
$items = $folder->get_content($cache);
@ -327,10 +331,10 @@ class App {
$results = array();
$results["path_index"] = $this->app_abs_href . "server/php/index.php";
$results["path_cache_writable"] = @is_writable($this->get_cache_abs_path());
$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")) {
@ -365,6 +369,7 @@ class App {
return array(
"backend" => "php",
"api" => true,
"name" => $this->server_name,
"version" => $this->server_version
);

View file

@ -6,19 +6,16 @@ class Thumb {
private static $AVCONV_CMD = "avconv -ss 0:01:00 -i [SOURCE] -an -vframes 1 [TARGET]";
private static $CONVERT_CMD = "convert -strip [SOURCE][0] [TARGET]";
private static $THUMB_CACHE = "thumbs";
private static $CAPTURE_CACHE = "captures";
public static final function is_supported() {
if (!function_exists("gd_info")) {
return false;
}
$gdinfo = gd_info();
return array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"];
return Image::is_supported();
}
private $app;
private $app, $thumbs_path, $thumbs_href;
public function __construct($app) {
@ -93,7 +90,7 @@ class Thumb {
return null;
}
$capture_abs_path = $this->app->get_cache_abs_path() . "/capture-" . sha1($source_abs_path) . ".jpg";
$capture_abs_path = $this->thumbs_path . "/capture-" . sha1($source_abs_path) . ".jpg";
if (!file_exists($capture_abs_path) || filemtime($source_abs_path) >= filemtime($capture_abs_path)) {
$cmd = str_replace("[SOURCE]", escapeshellarg($source_abs_path), $cmd);

View file

@ -0,0 +1,57 @@
<?php
putenv("LANG=en_US.UTF-8");
date_default_timezone_set("UTC");
normalized_require_once("util.php");
normalized_require_once("App.php");
normalized_require_once("Item.php");
function create_app() {
$server_name = null;
$server_version = null;
$server_software = getenv("SERVER_SOFTWARE");
if ($server_software && preg_match("#^(.*?)/(.*?)(?: |$)#", strtolower($server_software), $matches)) {
$server_name = $matches[1];
$server_version = $matches[2];
}
$app_abs_path = normalize_path(dirname(dirname(dirname(dirname(__FILE__)))));
$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);
}
$app = create_app();
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");
global $HREF, $FALLBACK;
$HREF = $app->get_app_abs_href();
$FALLBACK = $app->get_fallback();
normalized_require_once("page.php");
}
?>

View file

@ -1,5 +1,11 @@
<?php
function normalize_path($path, $trailing_slash = false) {
$path = str_replace("\\", "/", $path);
return preg_match("#^(\w:)?/$#", $path) ? $path : (rtrim($path, "/") . ($trailing_slash ? "/" : ""));
}
function json_exit($obj = array()) {
$obj["code"] = 0;
@ -15,6 +21,11 @@ function json_fail($code, $msg = "", $cond = true) {
}
}
function has_request_param($key) {
return array_key_exists($key, $_REQUEST);
}
function use_request_params($keys) {
if (!is_array($keys)) {

View file

@ -1,48 +1,12 @@
<?php
/* Bootstrap */
function normalize_path($path, $trailing_slash = false) {
$path = str_replace("\\", "/", $path);
return preg_match("#^(\w:)?/$#", $path) ? $path : (preg_replace('#/$#', '', $path) . ($trailing_slash ? "/" : ""));
}
define("APP_ABS_PATH", normalize_path(dirname(dirname(dirname(__FILE__)))));
function normalized_require_once($lib) {
require_once(APP_ABS_PATH . $lib);
$path = dirname(__FILE__) . "/inc/" . $lib;
$path = preg_replace("#\\+|/+#", "/", $path);
require_once($path);
}
/* 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");
$app = App::from_env();
/* Run */
if (array_key_exists("action", $_REQUEST)) {
header("Content-type: application/json;charset=utf-8");
normalized_require_once("/server/php/inc/Api.php");
$api = new Api($app);
$api->apply();
} else {
header("Content-type: text/html;charset=utf-8");
$HREF = $app->get_app_abs_href();
$FALLBACK = $app->get_fallback();
normalized_require_once("/server/php/inc/page.php");
}
normalized_require_once("main.php");
?>