From 811994423d368479764d6a48cdd8274a9ffb68d1 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Mon, 8 Oct 2012 01:16:02 +0200 Subject: [PATCH] Changes API. --- src/_h5ai/client/js/inc/core/server.js | 4 +- src/_h5ai/client/js/scripts.js | 3 +- src/_h5ai/server/php/api.php | 224 ------------------------ src/_h5ai/server/php/inc/Api.php | 233 +++++++++++++++++++++++++ src/_h5ai/server/php/inc/App.php | 21 +-- src/_h5ai/server/php/inc/init.php | 8 + src/_h5ai/server/php/index.php.jade | 1 + 7 files changed, 248 insertions(+), 246 deletions(-) delete mode 100644 src/_h5ai/server/php/api.php create mode 100644 src/_h5ai/server/php/inc/Api.php diff --git a/src/_h5ai/client/js/inc/core/server.js b/src/_h5ai/client/js/inc/core/server.js index 440e96ef..285b29e6 100644 --- a/src/_h5ai/client/js/inc/core/server.js +++ b/src/_h5ai/client/js/inc/core/server.js @@ -5,9 +5,11 @@ modulejs.define('core/server', ['$', '_', 'config'], function ($, _, config) { request: function (data, callback) { + data.api = true; + if (server.apiHref) { $.ajax({ - url: server.apiHref, + url: '.', data: data, type: 'POST', dataType: 'json', diff --git a/src/_h5ai/client/js/scripts.js b/src/_h5ai/client/js/scripts.js index 8b9afd68..50af566b 100644 --- a/src/_h5ai/client/js/scripts.js +++ b/src/_h5ai/client/js/scripts.js @@ -56,7 +56,8 @@ if (backend === 'php') { - $.getJSON(appHref + 'server/php/api.php', { + $.getJSON('.', { + api: true, action: 'get', options: true, types: true, diff --git a/src/_h5ai/server/php/api.php b/src/_h5ai/server/php/api.php deleted file mode 100644 index d5370edd..00000000 --- a/src/_h5ai/server/php/api.php +++ /dev/null @@ -1,224 +0,0 @@ -get_options(); - - -list($action) = use_request_params(array("action")); - - -if ($action === "get") { - - $response = array(); - - if (array_key_exists("options", $_REQUEST)) { - - use_request_params("options"); - $response["options"] = $APP->get_options(); - } - - if (array_key_exists("types", $_REQUEST)) { - - use_request_params("types"); - $response["types"] = $APP->get_types(); - } - - if (array_key_exists("langs", $_REQUEST)) { - - use_request_params("langs"); - $response["langs"] = $APP->get_l10n_list(); - } - - if (array_key_exists("l10n", $_REQUEST)) { - - list($iso_codes) = use_request_params("l10nCodes", "l10n"); - $iso_codes = explode(",", $iso_codes); - $response["l10n"] = $APP->get_l10n($iso_codes); - } - - if (array_key_exists("checks", $_REQUEST)) { - - use_request_params("checks"); - $response["checks"] = $APP->get_server_checks(); - } - - if (array_key_exists("server", $_REQUEST)) { - - use_request_params("server"); - $response["server"] = $APP->get_server_details(); - } - - if (array_key_exists("custom", $_REQUEST)) { - - list($abs_href) = use_request_params("customHref", "custom"); - $response["custom"] = $APP->get_customizations($abs_href); - } - - if (array_key_exists("entries", $_REQUEST)) { - - list($abs_href, $what) = use_request_params("entriesHref", "entriesWhat", "entries"); - $what = intval($what, 10); - $response["entries"] = $APP->get_entries($abs_href, $what); - } - - if (count($_REQUEST)) { - $response["unused"] = $_REQUEST; - } - - json_exit($response); -} - - -else if ($action === "getthumbsrc") { - - if (!$options["thumbnails"]["enabled"]) { - json_fail(1, "thumbnails disabled"); - } - - normalized_require_once("/server/php/inc/Thumb.php"); - if (!Thumb::is_supported()) { - json_fail(2, "thumbnails not supported"); - } - - list($type, $src_abs_href, $mode, $width, $height) = use_request_params(array("type", "href", "mode", "width", "height")); - - $thumb = new Thumb($APP); - $thumb_href = $thumb->thumb($type, $src_abs_href, $mode, $width, $height); - if ($thumb_href === null) { - json_fail(3, "thumbnail creation failed"); - } - - json_exit(array("absHref" => $thumb_href)); -} - - -else if ($action === "archive") { - - json_fail(1, "downloads disabled", !$options["download"]["enabled"]); - - list($execution, $format, $hrefs) = use_request_params(array("execution", "format", "hrefs")); - - normalized_require_once("/server/php/inc/Archive.php"); - $archive = new Archive($APP); - - $hrefs = explode(":", trim($hrefs)); - $target = $archive->create($execution, $format, $hrefs); - - if (!is_string($target)) { - json_fail($target, "package creation failed"); - } - - json_exit(array("id" => basename($target), "size" => filesize($target))); -} - - -else if ($action === "getarchive") { - - json_fail(1, "downloads disabled", !$options["download"]["enabled"]); - - list($id, $as) = use_request_params(array("id", "as")); - json_fail(2, "file not found", !preg_match("/^package-/", $id)); - - $target = $APP->get_cache_abs_path() . "/" . $id; - json_fail(3, "file not found", !file_exists($target)); - - header("Content-Type: application/octet-stream"); - header("Content-Length: " . filesize($target)); - header("Content-Disposition: attachment; filename=\"$as\""); - header("Connection: close"); - register_shutdown_function("delete_tempfile", $target); - readfile($target); -} - - -else if ($action === "upload") { - - 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)); - - $userfile = $_FILES["userfile"]; - - json_fail(3, "something went wrong [" . $userfile["error"] . "]", $userfile["error"] !== 0); - json_fail(4, "folders not supported", file_get_contents($userfile["tmp_name"]) === "null"); - - $upload_dir = $APP->get_abs_path($href); - $code = $APP->get_http_code($href); - - json_fail(5, "upload dir no h5ai folder or ignored", $code !== App::$MAGIC_SEQUENCE || $APP->is_ignored($upload_dir)); - - $dest = $upload_dir . "/" . utf8_encode($userfile["name"]); - - json_fail(6, "already exists", file_exists($dest)); - json_fail(7, "can't move uploaded file", !move_uploaded_file($userfile["tmp_name"], $dest)); - - json_exit(); -} - - -else if ($action === "delete") { - - json_fail(1, "deletion disabled", !$options["delete"]["enabled"]); - - list($hrefs) = use_request_params(array("hrefs")); - - $hrefs = explode(":", trim($hrefs)); - $errors = array(); - - foreach ($hrefs as $href) { - - $d = normalize_path(dirname($href), true); - $n = basename($href); - - $code = $APP->get_http_code($d); - - if ($code == App::$MAGIC_SEQUENCE && !$APP->is_ignored($n)) { - - $abs_path = $APP->get_abs_path($href); - - if (!unlink($abs_path)) { - $errors[] = $href; - } - } - } - - if (count($errors)) { - json_fail(2, "deletion failed for some"); - } else { - json_exit(); - } -} - - -else if ($action === "rename") { - - json_fail(1, "renaming disabled", !$options["rename"]["enabled"]); - - list($href, $name) = use_request_params(array("href", "name")); - - $d = normalize_path(dirname($href), true); - $n = basename($href); - - $code = $APP->get_http_code($d); - - if ($code == App::$MAGIC_SEQUENCE && !$APP->is_ignored($n)) { - - $abs_path = $APP->get_abs_path($href); - $folder = normalize_path(dirname($abs_path)); - - if (!rename($abs_path, $folder . "/" . $name)) { - json_fail(2, "renaming failed"); - } - } - - json_exit(); -} - - -else { - json_fail(100, "unsupported action"); -} - - -?> \ No newline at end of file diff --git a/src/_h5ai/server/php/inc/Api.php b/src/_h5ai/server/php/inc/Api.php new file mode 100644 index 00000000..610487de --- /dev/null +++ b/src/_h5ai/server/php/inc/Api.php @@ -0,0 +1,233 @@ +app = $app; + } + + + public function apply() { + + $options = $this->app->get_options(); + + list($action) = use_request_params(array("action")); + + if ($action === "get") { + + $response = array(); + + if (array_key_exists("options", $_REQUEST)) { + + use_request_params("options"); + $response["options"] = $this->app->get_options(); + } + + if (array_key_exists("types", $_REQUEST)) { + + use_request_params("types"); + $response["types"] = $this->app->get_types(); + } + + if (array_key_exists("langs", $_REQUEST)) { + + use_request_params("langs"); + $response["langs"] = $this->app->get_l10n_list(); + } + + if (array_key_exists("l10n", $_REQUEST)) { + + 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)) { + + use_request_params("checks"); + $response["checks"] = $this->app->get_server_checks(); + } + + if (array_key_exists("server", $_REQUEST)) { + + use_request_params("server"); + $response["server"] = $this->app->get_server_details(); + } + + if (array_key_exists("custom", $_REQUEST)) { + + list($abs_href) = use_request_params("customHref", "custom"); + $response["custom"] = $this->app->get_customizations($abs_href); + } + + if (array_key_exists("entries", $_REQUEST)) { + + list($abs_href, $what) = use_request_params("entriesHref", "entriesWhat", "entries"); + $what = intval($what, 10); + $response["entries"] = $this->app->get_entries($abs_href, $what); + } + + if (count($_REQUEST)) { + $response["unused"] = $_REQUEST; + } + + json_exit($response); + } + + + else if ($action === "getthumbsrc") { + + if (!$options["thumbnails"]["enabled"]) { + json_fail(1, "thumbnails disabled"); + } + + normalized_require_once("/server/php/inc/Thumb.php"); + if (!Thumb::is_supported()) { + json_fail(2, "thumbnails not supported"); + } + + list($type, $src_abs_href, $mode, $width, $height) = use_request_params(array("type", "href", "mode", "width", "height")); + + $thumb = new Thumb($this->app); + $thumb_href = $thumb->thumb($type, $src_abs_href, $mode, $width, $height); + if ($thumb_href === null) { + json_fail(3, "thumbnail creation failed"); + } + + json_exit(array("absHref" => $thumb_href)); + } + + + else if ($action === "archive") { + + json_fail(1, "downloads disabled", !$options["download"]["enabled"]); + + list($execution, $format, $hrefs) = use_request_params(array("execution", "format", "hrefs")); + + normalized_require_once("/server/php/inc/Archive.php"); + $archive = new Archive($this->app); + + $hrefs = explode(":", trim($hrefs)); + $target = $archive->create($execution, $format, $hrefs); + + if (!is_string($target)) { + json_fail($target, "package creation failed"); + } + + json_exit(array("id" => basename($target), "size" => filesize($target))); + } + + + else if ($action === "getarchive") { + + json_fail(1, "downloads disabled", !$options["download"]["enabled"]); + + list($id, $as) = use_request_params(array("id", "as")); + json_fail(2, "file not found", !preg_match("/^package-/", $id)); + + $target = $this->app->get_cache_abs_path() . "/" . $id; + json_fail(3, "file not found", !file_exists($target)); + + header("Content-Type: application/octet-stream"); + header("Content-Length: " . filesize($target)); + header("Content-Disposition: attachment; filename=\"$as\""); + header("Connection: close"); + register_shutdown_function("delete_tempfile", $target); + readfile($target); + } + + + else if ($action === "upload") { + + 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)); + + $userfile = $_FILES["userfile"]; + + json_fail(3, "something went wrong [" . $userfile["error"] . "]", $userfile["error"] !== 0); + json_fail(4, "folders not supported", file_get_contents($userfile["tmp_name"]) === "null"); + + $upload_dir = $this->app->get_abs_path($href); + $code = $this->app->get_http_code($href); + + json_fail(5, "upload dir no h5ai folder or ignored", $code !== App::$MAGIC_SEQUENCE || $this->app->is_ignored($upload_dir)); + + $dest = $upload_dir . "/" . utf8_encode($userfile["name"]); + + json_fail(6, "already exists", file_exists($dest)); + json_fail(7, "can't move uploaded file", !move_uploaded_file($userfile["tmp_name"], $dest)); + + json_exit(); + } + + + else if ($action === "delete") { + + json_fail(1, "deletion disabled", !$options["delete"]["enabled"]); + + list($hrefs) = use_request_params(array("hrefs")); + + $hrefs = explode(":", trim($hrefs)); + $errors = array(); + + foreach ($hrefs as $href) { + + $d = normalize_path(dirname($href), true); + $n = basename($href); + + $code = $this->app->get_http_code($d); + + if ($code == App::$MAGIC_SEQUENCE && !$this->app->is_ignored($n)) { + + $abs_path = $this->app->get_abs_path($href); + + if (!unlink($abs_path)) { + $errors[] = $href; + } + } + } + + if (count($errors)) { + json_fail(2, "deletion failed for some"); + } else { + json_exit(); + } + } + + + else if ($action === "rename") { + + json_fail(1, "renaming disabled", !$options["rename"]["enabled"]); + + list($href, $name) = use_request_params(array("href", "name")); + + $d = normalize_path(dirname($href), true); + $n = basename($href); + + $code = $this->app->get_http_code($d); + + if ($code == App::$MAGIC_SEQUENCE && !$this->app->is_ignored($n)) { + + $abs_path = $this->app->get_abs_path($href); + $folder = normalize_path(dirname($abs_path)); + + if (!rename($abs_path, $folder . "/" . $name)) { + json_fail(2, "renaming failed"); + } + } + + json_exit(); + } + + + else { + json_fail(100, "unsupported action"); + } + } +} + +?> \ No newline at end of file diff --git a/src/_h5ai/server/php/inc/App.php b/src/_h5ai/server/php/inc/App.php index a31e3a65..0ab82a94 100644 --- a/src/_h5ai/server/php/inc/App.php +++ b/src/_h5ai/server/php/inc/App.php @@ -150,28 +150,9 @@ class App { } - private function fileExists($file) { - - return is_string($file) && file_exists($file); - } - - public function get_generic_json() { - $entries = $this->get_entries($this->abs_href, 1); - - $header = $this->options["custom"]["header"]; - $footer = $this->options["custom"]["footer"]; - $header = $this->fileExists($header ? $this->abs_path . "/" . $header : null) ? $header : null; - $footer = $this->fileExists($footer ? $this->abs_path . "/" . $footer : null) ? $footer : null; - - $json = array( - "customHeader" => $header, - "customFooter" => $footer, - "entries" => $entries - ); - - return json_encode($json) . "\n"; + return json_encode(array("entries" => $this->get_entries($this->abs_href, 1))) . "\n"; } diff --git a/src/_h5ai/server/php/inc/init.php b/src/_h5ai/server/php/inc/init.php index 4f989b25..8a536b15 100644 --- a/src/_h5ai/server/php/inc/init.php +++ b/src/_h5ai/server/php/inc/init.php @@ -21,4 +21,12 @@ normalized_require_once("/server/php/inc/Entry.php"); $APP = new App(APP_ABS_PATH, APP_ABS_HREF, ABS_HREF); +if (array_key_exists("api", $_REQUEST)) { + + use_request_params("api"); + normalized_require_once("/server/php/inc/Api.php"); + $api = new Api($APP); + $api->apply(); +} + ?> \ No newline at end of file diff --git a/src/_h5ai/server/php/index.php.jade b/src/_h5ai/server/php/index.php.jade index d1e9a4f4..4b60f893 100644 --- a/src/_h5ai/server/php/index.php.jade +++ b/src/_h5ai/server/php/index.php.jade @@ -1,4 +1,5 @@ | +| - var href = "get_app_abs_href(); ?>" - var json = "get_generic_json(); ?>"