summaryrefslogtreecommitdiffstats
path: root/application/controllers
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2013-08-14 17:06:07 +0200
committerFlorian Pritz <bluewind@xinu.at>2013-09-02 22:02:27 +0200
commit84ce2c6ce0eb1b4f2f32c4ae0d7e08f3571f5018 (patch)
tree895a059bdc6d82a462a055764e761cdd16656a63 /application/controllers
parentf8417cd3aa92f49cbe98188cd6fca2ec50da9613 (diff)
Provide json output for api functions
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/controllers')
-rw-r--r--application/controllers/file.php64
-rw-r--r--application/controllers/user.php8
2 files changed, 43 insertions, 29 deletions
diff --git a/application/controllers/file.php b/application/controllers/file.php
index ef2e87084..c336db92b 100644
--- a/application/controllers/file.php
+++ b/application/controllers/file.php
@@ -9,6 +9,12 @@
class File extends MY_Controller {
+ protected $json_enabled_functions = array(
+ "upload_history",
+ "do_upload",
+ "do_delete",
+ );
+
function __construct()
{
parent::__construct();
@@ -280,6 +286,10 @@ class File extends MY_Controller {
}
}
+ if (request_type() == "json") {
+ return send_json_reply($this->data["urls"]);
+ }
+
if (is_cli_client()) {
$redirect = false;
}
@@ -456,7 +466,7 @@ class File extends MY_Controller {
ORDER BY date $order
", array($user))->result_array();
- if ($this->input->get("json") !== false) {
+ if (request_type() == "json") {
return send_json_reply($query);
}
@@ -499,11 +509,11 @@ class File extends MY_Controller {
$ids = $this->input->post("ids");
$errors = array();
- $msgs = array();
+ $deleted = array();
$deleted_count = 0;
$total_count = 0;
- if (!$ids) {
+ if (!$ids || !is_array($ids)) {
show_error("No IDs specified");
}
@@ -511,20 +521,34 @@ class File extends MY_Controller {
$total_count++;
if (!$this->mfile->id_exists($id)) {
- $errors[] = "'$id' didn't exist anymore.";
+ $errors[] = array(
+ "id" => $id,
+ "reason" => "doesn't exist",
+ );
continue;
}
if ($this->mfile->delete_id($id)) {
- $msgs[] = "'$id' has been removed.";
+ $deleted[] = $id;
$deleted_count++;
} else {
- $errors[] = "'$id' couldn't be deleted.";
+ $errors[] = array(
+ "id" => $id,
+ "reason" => "unknown error",
+ );
}
}
+ if (request_type() == "json") {
+ return send_json_reply(array(
+ "errors" => $errors,
+ "deleted" => $deleted,
+ "total_count" => $total_count,
+ "deleted_count" => $deleted_count,
+ ));
+ }
+
$this->data["errors"] = $errors;
- $this->data["msgs"] = $msgs;
$this->data["deleted_count"] = $deleted_count;
$this->data["total_count"] = $total_count;
@@ -571,20 +595,11 @@ class File extends MY_Controller {
$filename = "stdin";
if (!$content) {
- $this->output->set_status_header(400);
- $this->data["msg"] = "Nothing was pasted, content is empty.";
- $this->load->view('header', $this->data);
- $this->load->view($this->var->view_dir.'/upload_error', $this->data);
- $this->load->view('footer');
- return;
+ show_error("Nothing was pasted, content is empty.", 400);
}
if ($filesize > $this->config->item('upload_max_size')) {
- $this->output->set_status_header(413);
- $this->load->view('header', $this->data);
- $this->load->view($this->var->view_dir.'/too_big');
- $this->load->view('footer');
- return;
+ show_error("Error while uploading: File too big", 413);
}
$limits = $this->muser->get_upload_id_limits();
@@ -624,8 +639,6 @@ class File extends MY_Controller {
foreach ($files as $key => $file) {
// getNormalizedFILES() removes any file with error == 4
if ($file['error'] !== UPLOAD_ERR_OK) {
- $this->output->set_status_header(400);
-
// ERR_OK only for completeness, condition above ignores it
$errors = array(
UPLOAD_ERR_OK => "There is no error, the file uploaded with success",
@@ -646,19 +659,12 @@ class File extends MY_Controller {
$this->data["msg"] = "Unknown error code: ".$file['error'].". Please report a bug.";
}
- $this->load->view('header', $this->data);
- $this->load->view($this->var->view_dir.'/upload_error', $this->data);
- $this->load->view('footer');
- return;
+ show_error("Error while uploading: ".$this->data["msg"], 400);
}
$filesize = filesize($file['tmp_name']);
if ($filesize > $this->config->item('upload_max_size')) {
- $this->output->set_status_header(413);
- $this->load->view('header', $this->data);
- $this->load->view($this->var->view_dir.'/too_big');
- $this->load->view('footer');
- return;
+ show_error("Error while uploading: File too big", 413);
}
}
diff --git a/application/controllers/user.php b/application/controllers/user.php
index 498a626d7..56f571d6a 100644
--- a/application/controllers/user.php
+++ b/application/controllers/user.php
@@ -8,6 +8,10 @@
*/
class User extends MY_Controller {
+ protected $json_enabled_functions = array(
+ "apikeys",
+ );
+
function __construct()
{
@@ -127,6 +131,10 @@ class User extends MY_Controller {
WHERE `user` = ? order by created desc
", array($userid))->result_array();
+ if (request_type() == "json") {
+ return send_json_reply($query);
+ }
+
$this->data["query"] = $query;
$this->load->view('header', $this->data);