From d59962443687127ea1defc2f8ac41af1c2c02fe4 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 25 Oct 2014 13:55:08 +0200 Subject: first go at reworking; needs to be redesigned Signed-off-by: Florian Pritz --- application/controllers/api/api_controller.php | 15 +++++ application/controllers/api/v1.php | 83 ++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 application/controllers/api/api_controller.php create mode 100644 application/controllers/api/v1.php (limited to 'application/controllers/api') diff --git a/application/controllers/api/api_controller.php b/application/controllers/api/api_controller.php new file mode 100644 index 000000000..ca24dae59 --- /dev/null +++ b/application/controllers/api/api_controller.php @@ -0,0 +1,15 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace controllers\api; + +abstract class api_controller { + abstract static public function get_version(); +} + diff --git a/application/controllers/api/v1.php b/application/controllers/api/v1.php new file mode 100644 index 000000000..e6d3c56fe --- /dev/null +++ b/application/controllers/api/v1.php @@ -0,0 +1,83 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ +namespace controllers\api; + +class v1 extends api_controller { + protected $json_enabled_functions = array( + "upload", + "get_config", + "history", + ); + + static public function get_version() + { + return "1.0.1"; + } + + public function __construct() + { + parent::__construct(); + + $this->load->model('mfile'); + $this->load->model('mmultipaste'); + } + + public function upload() + { + $this->muser->require_access("basic"); + + $files = getNormalizedFILES(); + + if (empty($files)) { + show_error("No file was uploaded or unknown error occured."); + } + + $errors = service\files::verify_uploaded_files($files); + if (!empty($errors)) { + return send_json_reply($errors, "upload-error"); + } + + $limits = $this->muser->get_upload_id_limits(); + $urls = array(); + + foreach ($files as $file) { + $id = $this->mfile->new_id($limits[0], $limits[1]); + service\files::add_file($id, $file["tmp_name"], $file["name"]); + $ids[] = $id; + $urls[] = site_url($id).'/'; + } + + return send_json_reply(array( + "ids" => $ids, + "urls" => $urls, + )); + } + + public function get_config() + { + return send_json_reply(array( + "upload_max_size" => $this->config->item("upload_max_size"), + )); + } + + public function history() + { + $this->muser->require_access("apikey"); + $history = service\files::history($this->muser->get_userid()); + return send_json_reply($history); + } + + public function delete() + { + $this->muser->require_access("apikey"); + + + } +} +# vim: set noet: -- cgit v1.2.3-24-g4f1b From 349e9f6dc7da0c44ee80d0a73963c1c5cef87131 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 26 Oct 2014 21:39:58 +0100 Subject: misc Signed-off-by: Florian Pritz --- application/controllers/api/api_controller.php | 3 +- application/controllers/api/v1.php | 83 -------------------------- 2 files changed, 1 insertion(+), 85 deletions(-) delete mode 100644 application/controllers/api/v1.php (limited to 'application/controllers/api') diff --git a/application/controllers/api/api_controller.php b/application/controllers/api/api_controller.php index ca24dae59..2b9054b17 100644 --- a/application/controllers/api/api_controller.php +++ b/application/controllers/api/api_controller.php @@ -9,7 +9,6 @@ namespace controllers\api; -abstract class api_controller { - abstract static public function get_version(); +abstract class api_controller extends \CI_Controller { } diff --git a/application/controllers/api/v1.php b/application/controllers/api/v1.php deleted file mode 100644 index e6d3c56fe..000000000 --- a/application/controllers/api/v1.php +++ /dev/null @@ -1,83 +0,0 @@ - - * - * Licensed under AGPLv3 - * (see COPYING for full license text) - * - */ -namespace controllers\api; - -class v1 extends api_controller { - protected $json_enabled_functions = array( - "upload", - "get_config", - "history", - ); - - static public function get_version() - { - return "1.0.1"; - } - - public function __construct() - { - parent::__construct(); - - $this->load->model('mfile'); - $this->load->model('mmultipaste'); - } - - public function upload() - { - $this->muser->require_access("basic"); - - $files = getNormalizedFILES(); - - if (empty($files)) { - show_error("No file was uploaded or unknown error occured."); - } - - $errors = service\files::verify_uploaded_files($files); - if (!empty($errors)) { - return send_json_reply($errors, "upload-error"); - } - - $limits = $this->muser->get_upload_id_limits(); - $urls = array(); - - foreach ($files as $file) { - $id = $this->mfile->new_id($limits[0], $limits[1]); - service\files::add_file($id, $file["tmp_name"], $file["name"]); - $ids[] = $id; - $urls[] = site_url($id).'/'; - } - - return send_json_reply(array( - "ids" => $ids, - "urls" => $urls, - )); - } - - public function get_config() - { - return send_json_reply(array( - "upload_max_size" => $this->config->item("upload_max_size"), - )); - } - - public function history() - { - $this->muser->require_access("apikey"); - $history = service\files::history($this->muser->get_userid()); - return send_json_reply($history); - } - - public function delete() - { - $this->muser->require_access("apikey"); - - - } -} -# vim: set noet: -- cgit v1.2.3-24-g4f1b From 0c53ebac6e0328aea4551f5f1a97783f34c82866 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 2 Nov 2014 13:30:21 +0100 Subject: add missing files Signed-off-by: Florian Pritz --- application/controllers/api/v1/api_info.php | 16 +++++++ application/controllers/api/v1/file.php | 72 +++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 application/controllers/api/v1/api_info.php create mode 100644 application/controllers/api/v1/file.php (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/api_info.php b/application/controllers/api/v1/api_info.php new file mode 100644 index 000000000..3feaadfda --- /dev/null +++ b/application/controllers/api/v1/api_info.php @@ -0,0 +1,16 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ +namespace controllers\api\v1; + +class api_info extends \controllers\api\api_controller { + static public function get_version() + { + return "1.0.0"; + } +} diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php new file mode 100644 index 000000000..fc855f7f9 --- /dev/null +++ b/application/controllers/api/v1/file.php @@ -0,0 +1,72 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ +namespace controllers\api\v1; + +class file extends \controllers\api\api_controller { + public function __construct() + { + parent::__construct(); + + $this->load->model('mfile'); + $this->load->model('mmultipaste'); + } + + public function upload() + { + $this->muser->require_access("basic"); + + $files = getNormalizedFILES(); + + if (empty($files)) { + show_error("No file was uploaded or unknown error occured."); + } + + $errors = \service\files::verify_uploaded_files($files); + if (!empty($errors)) { + return send_json_reply($errors, "upload-error"); + } + + $limits = $this->muser->get_upload_id_limits(); + $urls = array(); + + foreach ($files as $file) { + $id = $this->mfile->new_id($limits[0], $limits[1]); + \service\files::add_file($id, $file["tmp_name"], $file["name"]); + $ids[] = $id; + $urls[] = site_url($id).'/'; + } + + return send_json_reply(array( + "ids" => $ids, + "urls" => $urls, + )); + } + + public function get_config() + { + return send_json_reply(array( + "upload_max_size" => $this->config->item("upload_max_size"), + )); + } + + public function history() + { + $this->muser->require_access("apikey"); + $history = \service\files::history($this->muser->get_userid()); + return send_json_reply($history); + } + + public function delete() + { + $this->muser->require_access("apikey"); + + + } +} +# vim: set noet: -- cgit v1.2.3-24-g4f1b From 434143c2b01c203bf9030669a14055872121b2c0 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Jan 2015 01:39:22 +0100 Subject: improve api errors Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index fc855f7f9..869d29ed1 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -29,7 +29,7 @@ class file extends \controllers\api\api_controller { $errors = \service\files::verify_uploaded_files($files); if (!empty($errors)) { - return send_json_reply($errors, "upload-error"); + return send_json_error_reply("file/upload-verify-failed", "Failed to verify uploaded file", $errors); } $limits = $this->muser->get_upload_id_limits(); -- cgit v1.2.3-24-g4f1b From 32e68c2dfff62cbdd82950b4b4e20a3c895dfb1f Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Jan 2015 01:39:40 +0100 Subject: add max_files_per_request to api/file/get_config Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 1 + 1 file changed, 1 insertion(+) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 869d29ed1..515286462 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -52,6 +52,7 @@ class file extends \controllers\api\api_controller { { return send_json_reply(array( "upload_max_size" => $this->config->item("upload_max_size"), + "max_files_per_request" => intval(ini_get("max_file_uploads")), )); } -- cgit v1.2.3-24-g4f1b From 8fd7c6c2ab80240ab1d163c9a4134822c7524144 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Jan 2015 01:40:07 +0100 Subject: add initial user api Signed-off-by: Florian Pritz --- application/controllers/api/v1/user.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 application/controllers/api/v1/user.php (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/user.php b/application/controllers/api/v1/user.php new file mode 100644 index 000000000..831fdb883 --- /dev/null +++ b/application/controllers/api/v1/user.php @@ -0,0 +1,24 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ +namespace controllers\api\v1; + +class user extends \controllers\api\api_controller { + public function __construct() + { + parent::__construct(); + + $this->load->model('muser'); + } + + public function apikeys() + { + $this->muser->require_access("full"); + return send_json_reply(\service\user::apikeys($this->muser->get_userid())); + } +} -- cgit v1.2.3-24-g4f1b From 0bed4fd5c9f67b60173df6638dc524d7b833c4e1 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Jan 2015 23:35:46 +0100 Subject: add some TODOs Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 3 ++- application/controllers/api/v1/user.php | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 515286462..56455c01e 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -50,6 +50,7 @@ class file extends \controllers\api\api_controller { public function get_config() { + // TODO: return more fields? return send_json_reply(array( "upload_max_size" => $this->config->item("upload_max_size"), "max_files_per_request" => intval(ini_get("max_file_uploads")), @@ -67,7 +68,7 @@ class file extends \controllers\api\api_controller { { $this->muser->require_access("apikey"); - + // TODO: implement } } # vim: set noet: diff --git a/application/controllers/api/v1/user.php b/application/controllers/api/v1/user.php index 831fdb883..4c2e5345d 100644 --- a/application/controllers/api/v1/user.php +++ b/application/controllers/api/v1/user.php @@ -21,4 +21,9 @@ class user extends \controllers\api\api_controller { $this->muser->require_access("full"); return send_json_reply(\service\user::apikeys($this->muser->get_userid())); } + + public function create_apikey() + { + // TODO: implement + } } -- cgit v1.2.3-24-g4f1b From 33efe571e3e7ebd607e92345c2e94e7fd8ae27f0 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 2 Feb 2015 19:45:11 +0100 Subject: Rework api error handling Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 56455c01e..c291ae879 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -24,12 +24,12 @@ class file extends \controllers\api\api_controller { $files = getNormalizedFILES(); if (empty($files)) { - show_error("No file was uploaded or unknown error occured."); + throw new \exceptions\PublicApiException("file/no-file", "No file was uploaded or unknown error occured."); } $errors = \service\files::verify_uploaded_files($files); if (!empty($errors)) { - return send_json_error_reply("file/upload-verify-failed", "Failed to verify uploaded file", $errors); + throw new \exceptions\PublicApiException("file/upload-verify-failed", "Failed to verify uploaded file", $errors); } $limits = $this->muser->get_upload_id_limits(); -- cgit v1.2.3-24-g4f1b From cb52a4cdc2daa45a61c728f5ec83603e6c6a71fa Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 00:23:12 +0100 Subject: Rework error handling in upload validator Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index c291ae879..82060e420 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -27,10 +27,7 @@ class file extends \controllers\api\api_controller { throw new \exceptions\PublicApiException("file/no-file", "No file was uploaded or unknown error occured."); } - $errors = \service\files::verify_uploaded_files($files); - if (!empty($errors)) { - throw new \exceptions\PublicApiException("file/upload-verify-failed", "Failed to verify uploaded file", $errors); - } + \service\files::verify_uploaded_files($files); $limits = $this->muser->get_upload_id_limits(); $urls = array(); -- cgit v1.2.3-24-g4f1b From e2c2740365b1f25beca1e174c8c5bda2950b7466 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 00:44:46 +0100 Subject: implement api/user/create_apikey Signed-off-by: Florian Pritz --- application/controllers/api/v1/user.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/user.php b/application/controllers/api/v1/user.php index 4c2e5345d..39c833d86 100644 --- a/application/controllers/api/v1/user.php +++ b/application/controllers/api/v1/user.php @@ -21,9 +21,19 @@ class user extends \controllers\api\api_controller { $this->muser->require_access("full"); return send_json_reply(\service\user::apikeys($this->muser->get_userid())); } - + public function create_apikey() { - // TODO: implement + $this->muser->require_access("full"); + $userid = $this->muser->get_userid(); + $comment = $this->input->post("comment"); + $comment = $comment === false ? "" : $comment; + $access_level = $this->input->post("access_level"); + + $key = \service\user::create_apikey($userid, $comment, $access_level); + + return send_json_reply(array( + "new_key" => $key, + )); } } -- cgit v1.2.3-24-g4f1b From 6816970229c6d0bd46ba46ecd70199c0687952da Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 11:12:01 +0100 Subject: api: handle json reply in api controller Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 10 +++++----- application/controllers/api/v1/user.php | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 82060e420..3aafd4732 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -39,26 +39,26 @@ class file extends \controllers\api\api_controller { $urls[] = site_url($id).'/'; } - return send_json_reply(array( + return array( "ids" => $ids, "urls" => $urls, - )); + ); } public function get_config() { // TODO: return more fields? - return send_json_reply(array( + return array( "upload_max_size" => $this->config->item("upload_max_size"), "max_files_per_request" => intval(ini_get("max_file_uploads")), - )); + ); } public function history() { $this->muser->require_access("apikey"); $history = \service\files::history($this->muser->get_userid()); - return send_json_reply($history); + return $history; } public function delete() diff --git a/application/controllers/api/v1/user.php b/application/controllers/api/v1/user.php index 39c833d86..e49b7c657 100644 --- a/application/controllers/api/v1/user.php +++ b/application/controllers/api/v1/user.php @@ -19,7 +19,7 @@ class user extends \controllers\api\api_controller { public function apikeys() { $this->muser->require_access("full"); - return send_json_reply(\service\user::apikeys($this->muser->get_userid())); + return \service\user::apikeys($this->muser->get_userid()); } public function create_apikey() @@ -32,8 +32,8 @@ class user extends \controllers\api\api_controller { $key = \service\user::create_apikey($userid, $comment, $access_level); - return send_json_reply(array( + return array( "new_key" => $key, - )); + ); } } -- cgit v1.2.3-24-g4f1b From a788fe55713e7c44068ee2dd8377b98037d9375f Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 11:38:03 +0100 Subject: api: implement file/delete Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 3aafd4732..0035d0a02 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -64,8 +64,8 @@ class file extends \controllers\api\api_controller { public function delete() { $this->muser->require_access("apikey"); - - // TODO: implement + $ids = $this->input->post("ids"); + return \service\files::delete($ids); } } # vim: set noet: -- cgit v1.2.3-24-g4f1b From 5816cbcad0e9c4cda4dc10b730a5a1ea2c4e419a Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 12:11:28 +0100 Subject: api: implement file/create_multipaste Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 0035d0a02..fc5565416 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -67,5 +67,15 @@ class file extends \controllers\api\api_controller { $ids = $this->input->post("ids"); return \service\files::delete($ids); } + + public function create_multipaste() + { + $this->muser->require_access("apikey"); + $ids = $this->input->post("ids"); + $userid = $this->muser->get_userid(); + $limits = $this->muser->get_upload_id_limits(); + + return \service\files::create_multipaste($ids, $userid, $limits); + } } # vim: set noet: -- cgit v1.2.3-24-g4f1b From b8facbbd7a9a29c6274c435932b9c810155e2460 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 14 Feb 2015 19:12:13 +0100 Subject: Fix typo in error message Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application/controllers/api') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index fc5565416..a10aaf63a 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -24,7 +24,7 @@ class file extends \controllers\api\api_controller { $files = getNormalizedFILES(); if (empty($files)) { - throw new \exceptions\PublicApiException("file/no-file", "No file was uploaded or unknown error occured."); + throw new \exceptions\PublicApiException("file/no-file", "No file was uploaded or unknown error occurred."); } \service\files::verify_uploaded_files($files); -- cgit v1.2.3-24-g4f1b