More PHP refactorings. Fixes text preview.

This commit is contained in:
Lars Jung 2012-10-06 14:29:24 +02:00
parent 838a346c29
commit 029872a212
8 changed files with 141 additions and 134 deletions

View File

@ -69,11 +69,11 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/resource',
},
loadSyntaxhighlighter = function (callback) {
loadScript(allsettings.h5aiAbsHref + 'js/syntaxhighlighter.js', 'SyntaxHighlighter', callback);
loadScript(allsettings.h5aiAbsHref + 'client/js/syntaxhighlighter.js', 'SyntaxHighlighter', callback);
},
loadMarkdown = function (callback) {
loadScript(allsettings.h5aiAbsHref + 'js/markdown.js', 'markdown', callback);
loadScript(allsettings.h5aiAbsHref + 'client/js/markdown.js', 'markdown', callback);
},
adjustSize = function () {

View File

@ -1,41 +1,11 @@
<?php
require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/H5ai.php");
$h5ai = new H5ai(__FILE__);
require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/init.php");
$h5ai = $APP;
$options = $h5ai->getOptions();
function json_exit($obj) {
$obj["code"] = 0;
echo json_encode($obj);
exit;
}
function json_fail($code, $msg = "", $cond = true) {
if ($cond) {
echo json_encode(array("code" => $code, "msg" => $msg));
exit;
}
}
function check_keys($keys) {
$values = array();
foreach ($keys as $key) {
json_fail(101, "parameter '$key' is missing", !array_key_exists($key, $_REQUEST));
$values[] = $_REQUEST[$key];
}
return $values;
}
function delete_tempfile($file) {
@unlink($file);
}
list($action) = check_keys(array("action"));
list($action) = use_request_params(array("action"));
if ($action === "getthumbsrc") {
@ -44,12 +14,12 @@ if ($action === "getthumbsrc") {
json_fail(1, "thumbnails disabled");
}
H5ai::req_once("/server/php/inc/Thumb.php");
normalized_require_once("/server/php/inc/Thumb.php");
if (!Thumb::is_supported()) {
json_fail(2, "thumbnails not supported");
}
list($type, $srcAbsHref, $mode, $width, $height) = check_keys(array("type", "href", "mode", "width", "height"));
list($type, $srcAbsHref, $mode, $width, $height) = use_request_params(array("type", "href", "mode", "width", "height"));
$thumb = new Thumb($h5ai);
$thumbHref = $thumb->thumb($type, $srcAbsHref, $mode, $width, $height);
@ -65,9 +35,9 @@ else if ($action === "archive") {
json_fail(1, "downloads disabled", !$options["download"]["enabled"]);
list($execution, $format, $hrefs) = check_keys(array("execution", "format", "hrefs"));
list($execution, $format, $hrefs) = use_request_params(array("execution", "format", "hrefs"));
H5ai::req_once("/server/php/inc/Archive.php");
normalized_require_once("/server/php/inc/Archive.php");
$archive = new Archive($h5ai);
$hrefs = explode(":", trim($hrefs));
@ -85,7 +55,7 @@ else if ($action === "getarchive") {
json_fail(1, "downloads disabled", !$options["download"]["enabled"]);
list($id, $as) = check_keys(array("id", "as"));
list($id, $as) = use_request_params(array("id", "as"));
json_fail(2, "file not found", !preg_match("/^package-/", $id));
$target = $h5ai->getCacheAbsPath() . "/" . $id;
@ -132,7 +102,7 @@ else if ($action === "getchecks") {
else if ($action === "getentries") {
list($href, $content) = check_keys(array("href", "content"));
list($href, $content) = use_request_params(array("href", "content"));
$content = intval($content, 10);
@ -142,7 +112,7 @@ else if ($action === "getentries") {
else if ($action === "upload") {
list($href) = check_keys(array("href"));
list($href) = use_request_params(array("href"));
json_fail(1, "wrong HTTP method", strtolower($_SERVER["REQUEST_METHOD"]) !== "post");
json_fail(2, "something went wrong", !array_key_exists("userfile", $_FILES));
@ -170,14 +140,14 @@ else if ($action === "delete") {
json_fail(1, "deletion disabled", !$options["delete"]["enabled"]);
list($hrefs) = check_keys(array("hrefs"));
list($hrefs) = use_request_params(array("hrefs"));
$hrefs = explode(":", trim($hrefs));
$errors = array();
foreach ($hrefs as $href) {
$d = H5ai::normalize_path(dirname($href), true);
$d = normalize_path(dirname($href), true);
$n = basename($href);
$code = $h5ai->getHttpCode($d);
@ -206,9 +176,9 @@ else if ($action === "rename") {
json_fail(1, "renaming disabled", !$options["rename"]["enabled"]);
list($href, $name) = check_keys(array("href", "name"));
list($href, $name) = use_request_params(array("href", "name"));
$d = H5ai::normalize_path(dirname($href), true);
$d = normalize_path(dirname($href), true);
$n = basename($href);
$code = $h5ai->getHttpCode($d);
@ -218,7 +188,7 @@ else if ($action === "rename") {
if ($code == "h5ai" && !$h5ai->is_ignored($n)) {
$absPath = $h5ai->getAbsPath($href);
$folder = H5ai::normalize_path(dirname($absPath));
$folder = normalize_path(dirname($absPath));
if (!rename($absPath, $folder . "/" . $name)) {
json_fail(2, "renaming failed");

View File

@ -1,75 +1,27 @@
<?php
define("H5AI_ABS_PATH", H5ai::normalize_path(dirname(dirname(dirname(dirname(__FILE__))))));
H5ai::req_once("/conf/config.php");
H5ai::req_once("/server/php/inc/Entry.php");
class H5ai {
public static final function normalize_path($path, $endWithSlash = false) {
$path = str_replace("\\", "/", $path);
return preg_match("#^(\w:)?/$#", $path) ? $path : (preg_replace('#/$#', '', $path) . ($endWithSlash ? "/" : ""));
}
public static final function req_once($lib) {
require_once(H5AI_ABS_PATH . $lib);
}
private static final function load_config($file) {
if (!file_exists($file)) {
return array();
}
$str = file_get_contents($file);
// remove comments to get pure json
$str = preg_replace("/\/\*.*?\*\/|\/\/.*?(\n|$)/s", "", $str);
return json_decode($str, true);
}
private static final function merge_config($a, $b) {
$result = array_merge(array(), $a);
foreach ($b as $key => $value) {
$result[$key] = array_merge($result[$key], $b[$key]);
}
return $result;
}
private static $H5AI_CONTENT_TYPE = "Content-Type: text/html;h5ai=";
private $h5aiAbsPath, $rootAbsPath,
$h5aiAbsHref, $rootAbsHref,
$absHref, $absPath,
$ignore_names, $ignore_patterns, $index_files,
$config, $options;
private $requested_from,
$h5aiAbsPath,
$rootAbsPath, $ignore_names, $ignore_patterns, $index_files,
$config, $options,
$rootAbsHref, $h5aiAbsHref,
$absHref, $absPath;
public function __construct($app_abs_path, $app_abs_href) {
$this->h5aiAbsPath = normalize_path($app_abs_path);
$this->rootAbsPath = normalize_path(dirname($app_abs_path));
public function __construct($requested_from) {
$this->h5aiAbsHref = normalize_path($app_abs_href, true);
$this->rootAbsHref = normalize_path(dirname($app_abs_href), true);
$this->requested_from = H5ai::normalize_path($requested_from);
$this->h5aiAbsPath = H5ai::normalize_path(H5AI_ABS_PATH);
$this->rootAbsPath = H5ai::normalize_path(dirname(H5AI_ABS_PATH));
$this->absHref = normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true);
$this->absPath = $this->getAbsPath($this->absHref);
global $H5AI_CONFIG;
$this->ignore_names = $H5AI_CONFIG["IGNORE"];
@ -77,16 +29,10 @@ class H5ai {
$this->index_files = $H5AI_CONFIG["INDEX_FILES"];
$this->config = array("options" => array(), "types" => array(), "langs" => array());
$this->config = H5ai::merge_config($this->config, H5ai::load_config($this->h5aiAbsPath . "/conf/config.json"));
$this->config = merge_config($this->config, load_commented_json($this->h5aiAbsPath . "/conf/config.json"));
$this->options = $this->config["options"];
$this->h5aiAbsHref = H5ai::normalize_path($this->options["h5aiAbsHref"], true);
$this->rootAbsHref = H5ai::normalize_path(dirname($this->options["h5aiAbsHref"]), true);
$this->absHref = H5ai::normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true);
$this->absPath = $this->getAbsPath($this->absHref);
$this->config = H5ai::merge_config($this->config, H5ai::load_config($this->absPath . "/_h5ai.config.json"));
$this->config = merge_config($this->config, load_commented_json($this->absPath . "/_h5ai.config.json"));
$this->options = $this->config["options"];
}
@ -147,7 +93,7 @@ class H5ai {
$encodedParts[] = rawurlencode($part);
}
return H5ai::normalize_path($this->rootAbsHref . implode("/", $encodedParts), $endWithSlash);
return normalize_path($this->rootAbsHref . implode("/", $encodedParts), $endWithSlash);
}
@ -159,7 +105,7 @@ class H5ai {
$absHref = substr($absHref, strlen($this->rootAbsHref));
return H5ai::normalize_path($this->rootAbsPath . "/" . rawurldecode($absHref));
return normalize_path($this->rootAbsPath . "/" . rawurldecode($absHref));
}
@ -285,7 +231,7 @@ class H5ai {
$footer = $this->fileExists($footer ? $this->absPath . "/" . $footer : null) ? $footer : null;
$json = array(
"id" => $this->requested_from === $this->h5aiAbsPath . "/server/php/index.php" ? "php" : "idx.php",
"id" => "php",
"serverName" => strtolower(preg_replace("/\\/.*$/", "", getenv("SERVER_SOFTWARE"))),
"serverVersion" => strtolower(preg_replace("/^.*\\//", "", preg_replace("/\\s.*$/", "", getenv("SERVER_SOFTWARE")))),
"customHeader" => $header,

View File

@ -71,7 +71,7 @@ class Archive {
foreach ($hrefs as $href) {
$d = H5ai::normalize_path(dirname($href), true);
$d = normalize_path(dirname($href), true);
$n = basename($href);
$code = $this->h5ai->getHttpCode($d);
@ -82,7 +82,7 @@ class Archive {
if ($code == "h5ai" && !$this->h5ai->is_ignored($n)) {
$realFile = $this->h5ai->getAbsPath($href);
$archivedFile = preg_replace("!^" . H5ai::normalize_path($this->h5ai->getRootAbsPath(), true) . "!", "", $realFile);
$archivedFile = preg_replace("!^" . normalize_path($this->h5ai->getRootAbsPath(), true) . "!", "", $realFile);
if (is_dir($realFile)) {
$this->add_dir($realFile, $archivedFile);

View File

@ -8,12 +8,6 @@ class Entry {
private static $cache = array();
private static function starts_with($sequence, $part) {
return (substr($sequence, 0, strlen($part)) === $part);
}
public static function get_cache() {
return Entry::$cache;
@ -22,7 +16,7 @@ class Entry {
public static function get($h5ai, $absPath, $absHref) {
if (!Entry::starts_with($absHref, $h5ai->getRootAbsHref())) {
if (!starts_with($absHref, $h5ai->getRootAbsHref())) {
error_log("ILLEGAL REQUEST: " . $absHref . ", " . $absPath . ", " . $h5ai->getRootAbsHref());
return null;
}
@ -55,10 +49,10 @@ class Entry {
$this->h5ai = $h5ai;
$this->absPath = H5ai::normalize_path($absPath);
$this->absPath = normalize_path($absPath);
$this->isFolder = is_dir($this->absPath);
$this->absHref = H5ai::normalize_path($absHref, $this->isFolder);
$this->absHref = normalize_path($absHref, $this->isFolder);
$this->date = filemtime($this->absPath);
@ -74,9 +68,9 @@ class Entry {
}
$this->parent = null;
$parentAbsHref = H5ai::normalize_path(dirname($this->absHref), true);
if ($this->absHref !== "/" && Entry::starts_with($parentAbsHref, $h5ai->getRootAbsHref())) {
$this->parent = Entry::get($this->h5ai, H5ai::normalize_path(dirname($this->absPath)), $parentAbsHref);
$parentAbsHref = normalize_path(dirname($this->absHref), true);
if ($this->absHref !== "/" && starts_with($parentAbsHref, $h5ai->getRootAbsHref())) {
$this->parent = Entry::get($this->h5ai, normalize_path(dirname($this->absPath)), $parentAbsHref);
}
$this->isContentFetched = false;

View File

@ -0,0 +1,24 @@
<?php
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(dirname(__FILE__))))));
define("APP_ABS_HREF", normalize_path(dirname(dirname(dirname(getenv("SCRIPT_NAME")))), true));
function normalized_require_once($lib) {
require_once(APP_ABS_PATH . $lib);
}
normalized_require_once("/conf/config.php");
normalized_require_once("/server/php/inc/App.php");
normalized_require_once("/server/php/inc/Entry.php");
normalized_require_once("/server/php/inc/util.php");
$APP = new H5ai(APP_ABS_PATH, APP_ABS_HREF);
?>

View File

@ -0,0 +1,73 @@
<?php
function json_exit($obj) {
$obj["code"] = 0;
echo json_encode($obj);
exit;
}
function json_fail($code, $msg = "", $cond = true) {
if ($cond) {
echo json_encode(array("code" => $code, "msg" => $msg));
exit;
}
}
function use_request_params($keys) {
if (!is_array($keys)) {
$keys = func_get_args();
}
$values = array();
foreach ($keys as $key) {
json_fail(101, "parameter '$key' is missing", !array_key_exists($key, $_REQUEST));
$values[] = $_REQUEST[$key];
unset($_REQUEST[$key]);
}
return $values;
}
function delete_tempfile($file) {
@unlink($file);
}
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)) {
return array();
}
$str = file_get_contents($file);
// remove comments to get pure json
$str = preg_replace("/\/\*.*?\*\/|\/\/.*?(\n|$)/s", "", $str);
return json_decode($str, true);
}
function merge_config($a, $b) {
$result = array_merge(array(), $a);
foreach ($b as $key => $value) {
$result[$key] = array_merge($result[$key], $b[$key]);
}
return $result;
}
?>

View File

@ -1,7 +1,7 @@
|<?php
| header("Content-type: text/html;{{pkg.name}}={{pkg.version}}");
| require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/H5ai.php");
| $h5ai = new H5ai(__FILE__);
| require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/init.php");
| $h5ai = $APP;
| $h5aiAbsHref = $h5ai->getH5aiAbsHref();
| $isHeadRequest = stripos($_SERVER["REQUEST_METHOD"], "HEAD");
|?>