From 36025a9ab64f0c99bb026529034cb53ca8ca77ba Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 24 Sep 2018 15:51:13 +0200 Subject: Move JSON code to API class Signed-off-by: Florian Pritz --- application/controllers/Api.php | 33 ++++++++++++++++++++++++++++++--- application/helpers/filebin_helper.php | 28 ---------------------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 9540f1ff7..1fa49cb46 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -55,12 +55,39 @@ class Api extends MY_Controller { if (!method_exists($c, $function)) { throw new \exceptions\PublicApiException("api/unknown-endpoint", "Unknown endpoint requested"); } - return send_json_reply($c->$function()); + return $this->send_json_reply($c->$function()); } catch (\exceptions\PublicApiException $e) { - return send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data()); + return $this->send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data()); } catch (\Exception $e) { \libraries\ExceptionHandler::log_exception($e); - return send_json_error_reply("internal-error", "An unhandled internal server error occured"); + return $this->send_json_error_reply("internal-error", "An unhandled internal server error occured"); } } + + private 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($reply)); + } + + private function send_json_error_reply($error_id, $message, $array = null, $status_code = 400) { + $reply = array(); + $reply["status"] = "error"; + $reply["error_id"] = $error_id; + $reply["message"] = $message; + + if ($array !== null) { + $reply["data"] = $array; + } + + $CI =& get_instance(); + $CI->output->set_status_header($status_code); + $CI->output->set_content_type('application/json'); + $CI->output->set_output(json_encode($reply)); + } + } diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php index 847d6d3ae..0fa986225 100644 --- a/application/helpers/filebin_helper.php +++ b/application/helpers/filebin_helper.php @@ -157,34 +157,6 @@ function auth_driver_function_implemented($function) return $result[$function]; } -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($reply)); -} - -function send_json_error_reply($error_id, $message, $array = null, $status_code = 400) -{ - $reply = array(); - $reply["status"] = "error"; - $reply["error_id"] = $error_id; - $reply["message"] = $message; - - if ($array !== null) { - $reply["data"] = $array; - } - - $CI =& get_instance(); - $CI->output->set_status_header($status_code); - $CI->output->set_content_type('application/json'); - $CI->output->set_output(json_encode($reply)); -} - function static_storage($key, $value = null) { static $storage = array(); -- cgit v1.2.3-24-g4f1b