diff options
Diffstat (limited to 'application')
-rw-r--r-- | application/controllers/file.php | 64 | ||||
-rw-r--r-- | application/controllers/user.php | 8 | ||||
-rw-r--r-- | application/core/MY_Controller.php | 12 | ||||
-rwxr-xr-x | application/errors/error_general.php | 10 | ||||
-rw-r--r-- | application/helpers/filebin_helper.php | 19 | ||||
-rw-r--r-- | application/views/file/deleted.php | 9 | ||||
-rw-r--r-- | application/views/file/too_big.php | 3 | ||||
-rw-r--r-- | application/views/file/upload_error.php | 6 | ||||
-rw-r--r-- | application/views/file_plaintext/too_big.php | 2 | ||||
-rw-r--r-- | application/views/file_plaintext/upload_error.php | 2 |
10 files changed, 84 insertions, 51 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); diff --git a/application/core/MY_Controller.php b/application/core/MY_Controller.php index 278768ad2..3ee63424a 100644 --- a/application/core/MY_Controller.php +++ b/application/core/MY_Controller.php @@ -11,7 +11,7 @@ class MY_Controller extends CI_Controller { public $data = array(); public $var; - private $json_enabled_functions = array( + protected $json_enabled_functions = array( ); function __construct() @@ -31,6 +31,16 @@ class MY_Controller extends CI_Controller { mb_internal_encoding('UTF-8'); $this->load->helper(array('form', 'filebin')); + if (isset($_SERVER["HTTP_ACCEPT"])) { + if ($_SERVER["HTTP_ACCEPT"] == "application/json") { + request_type("json"); + } + } + + if (request_type() == "json" && ! in_array($this->uri->rsegment(2), $this->json_enabled_functions)) { + show_error("Function not JSON enabled"); + } + $this->data['title'] = "FileBin"; } } diff --git a/application/errors/error_general.php b/application/errors/error_general.php index da3999cbd..fc3d3f607 100755 --- a/application/errors/error_general.php +++ b/application/errors/error_general.php @@ -9,6 +9,16 @@ if (class_exists("CI_Controller") && !isset($GLOBALS["is_error_page"])) { $CI->load->helper("filebin"); $CI->load->helper("url"); + if (request_type() == "json") { + $array = array( + "status" => "error", + "message" => strip_tags($message), + ); + header('Content-type: application/json'); + echo json_encode($array); + exit(); + } + if (is_cli_client()) { $message = strip_tags($message); echo "$heading: $message\n"; diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php index 57f7bdc65..9b124506f 100644 --- a/application/helpers/filebin_helper.php +++ b/application/helpers/filebin_helper.php @@ -330,11 +330,26 @@ function user_logged_in() return $CI->muser->logged_in(); } -function send_json_reply($array) +function send_json_reply($array, $status = "success") { + $reply = array(); + $reply["status"] = $status; + $reply["data"] = $array; + $CI =& get_instance(); $CI->output->set_content_type('application/json'); - $CI->output->set_output(json_encode($array)); + $CI->output->set_output(json_encode($reply)); +} + +function request_type($set_type = null) +{ + static $type = null; + + if ($set_type !== null) { + $type = $set_type; + } + + return $type; } # vim: set noet: diff --git a/application/views/file/deleted.php b/application/views/file/deleted.php index ef02398d9..8a5818f2d 100644 --- a/application/views/file/deleted.php +++ b/application/views/file/deleted.php @@ -1,12 +1,9 @@ <div class="center"> <?php if (!empty($errors)) { echo "<p>"; - echo implode("<br />\n", $errors); - echo "</p>"; - } ?> - <?php if (!empty($msgs)) { - echo "<p>"; - echo implode("<br />\n", $msgs); + foreach ($errors as $error) { + echo "${error["id"]}: ${error["reason"]}<br>\n"; + } echo "</p>"; } ?> diff --git a/application/views/file/too_big.php b/application/views/file/too_big.php deleted file mode 100644 index 5b970a4c8..000000000 --- a/application/views/file/too_big.php +++ /dev/null @@ -1,3 +0,0 @@ -<div class="center"> - <p>Sorry, the file you uploaded is too big.</p> -</div> diff --git a/application/views/file/upload_error.php b/application/views/file/upload_error.php deleted file mode 100644 index 83968eec2..000000000 --- a/application/views/file/upload_error.php +++ /dev/null @@ -1,6 +0,0 @@ -<div class="center"> - <p> - An error occurred while uploading.<br /> - <?php echo $msg; ?> - </p> -</div> diff --git a/application/views/file_plaintext/too_big.php b/application/views/file_plaintext/too_big.php deleted file mode 100644 index d27a0295c..000000000 --- a/application/views/file_plaintext/too_big.php +++ /dev/null @@ -1,2 +0,0 @@ -Sorry, the file you uploaded is too big. - diff --git a/application/views/file_plaintext/upload_error.php b/application/views/file_plaintext/upload_error.php deleted file mode 100644 index c86c56911..000000000 --- a/application/views/file_plaintext/upload_error.php +++ /dev/null @@ -1,2 +0,0 @@ -An error occurred while uploading. <?php echo $msg; ?> - |