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.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 application/controllers/api.php (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php new file mode 100644 index 000000000..626e7b91a --- /dev/null +++ b/application/controllers/api.php @@ -0,0 +1,38 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +class Api extends MY_Controller { + + public function __construct() + { + parent::__construct(); + + $this->load->model('mfile'); + $this->load->model('mmultipaste'); + } + + public function route() { + $requested_version = $this->uri->segment(2); + $function = $this->uri->segment(3); + $major = intval(explode(".", $requested_version)[0]); + + $class = "controllers\\api\\v".$major; + + if (!class_exists($class) || version_compare($class::get_version(), $requested_version, "<")) { + return send_json_error_reply("Requested API version is not supported"); + } + + if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { + return send_json_error_reply("Invalid function requested"); + } + + $controller = new $class; + return $controller->$function(); + } +} -- 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.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index 626e7b91a..a7bd09f34 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -19,20 +19,34 @@ class Api extends MY_Controller { public function route() { $requested_version = $this->uri->segment(2); - $function = $this->uri->segment(3); + $controller = $this->uri->segment(3); + $function = $this->uri->segment(4); $major = intval(explode(".", $requested_version)[0]); - $class = "controllers\\api\\v".$major; - - if (!class_exists($class) || version_compare($class::get_version(), $requested_version, "<")) { - return send_json_error_reply("Requested API version is not supported"); + if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { + return send_json_error_reply("Invalid controller requested"); } if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { return send_json_error_reply("Invalid function requested"); } - $controller = new $class; - return $controller->$function(); + $namespace = "controllers\\api\\v".$major; + $class = $namespace."\\".$controller; + $class_info = $namespace."\\api_info"; + + if (!class_exists($class_info) || version_compare($class_info::get_version(), $requested_version, "<")) { + return send_json_error_reply("Requested API version is not supported"); + } + + if (!class_exists($class)) { + return send_json_error_reply("Unknown controller requested"); + } + + $c= new $class; + if (!method_exists($c, $function)) { + return send_json_error_reply("Unknown function requested"); + } + return $c->$function(); } } -- 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.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index a7bd09f34..7557c6c99 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -24,11 +24,11 @@ class Api extends MY_Controller { $major = intval(explode(".", $requested_version)[0]); if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { - return send_json_error_reply("Invalid controller requested"); + return send_json_error_reply("api/invalid-controller-value", "Invalid controller requested"); } if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { - return send_json_error_reply("Invalid function requested"); + return send_json_error_reply("api/invalid-function-value", "Invalid function requested"); } $namespace = "controllers\\api\\v".$major; @@ -36,16 +36,16 @@ class Api extends MY_Controller { $class_info = $namespace."\\api_info"; if (!class_exists($class_info) || version_compare($class_info::get_version(), $requested_version, "<")) { - return send_json_error_reply("Requested API version is not supported"); + return send_json_error_reply("api/version-not-supported", "Requested API version is not supported"); } if (!class_exists($class)) { - return send_json_error_reply("Unknown controller requested"); + return send_json_error_reply("api/unknown-controller", "Unknown controller requested"); } $c= new $class; if (!method_exists($c, $function)) { - return send_json_error_reply("Unknown function requested"); + return send_json_error_reply("api/unknown-function", "Unknown function requested"); } return $c->$function(); } -- 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.php | 62 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index 7557c6c99..490f59c2c 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -18,35 +18,39 @@ class Api extends MY_Controller { } public function route() { - $requested_version = $this->uri->segment(2); - $controller = $this->uri->segment(3); - $function = $this->uri->segment(4); - $major = intval(explode(".", $requested_version)[0]); - - if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { - return send_json_error_reply("api/invalid-controller-value", "Invalid controller requested"); - } - - if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { - return send_json_error_reply("api/invalid-function-value", "Invalid function requested"); - } - - $namespace = "controllers\\api\\v".$major; - $class = $namespace."\\".$controller; - $class_info = $namespace."\\api_info"; - - if (!class_exists($class_info) || version_compare($class_info::get_version(), $requested_version, "<")) { - return send_json_error_reply("api/version-not-supported", "Requested API version is not supported"); - } - - if (!class_exists($class)) { - return send_json_error_reply("api/unknown-controller", "Unknown controller requested"); - } - - $c= new $class; - if (!method_exists($c, $function)) { - return send_json_error_reply("api/unknown-function", "Unknown function requested"); + try { + $requested_version = $this->uri->segment(2); + $controller = $this->uri->segment(3); + $function = $this->uri->segment(4); + $major = intval(explode(".", $requested_version)[0]); + + if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { + throw new \exceptions\PublicApiException("api/invalid-controller-value", "Invalid controller requested"); + } + + if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { + throw new \exceptions\PublicApiException("api/invalid-function-value", "Invalid function requested"); + } + + $namespace = "controllers\\api\\v".$major; + $class = $namespace."\\".$controller; + $class_info = $namespace."\\api_info"; + + if (!class_exists($class_info) || version_compare($class_info::get_version(), $requested_version, "<")) { + throw new \exceptions\PublicApiException("api/version-not-supported", "Requested API version is not supported"); + } + + if (!class_exists($class)) { + throw new \exceptions\PublicApiException("api/unknown-controller", "Unknown controller requested"); + } + + $c= new $class; + if (!method_exists($c, $function)) { + throw new \exceptions\PublicApiException("api/unknown-function", "Unknown function requested"); + } + return $c->$function(); + } catch (\exceptions\PublicApiException $e) { + return send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data()); } - return $c->$function(); } } -- 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.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index 490f59c2c..dc31f47d2 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -48,7 +48,7 @@ class Api extends MY_Controller { if (!method_exists($c, $function)) { throw new \exceptions\PublicApiException("api/unknown-function", "Unknown function requested"); } - return $c->$function(); + return send_json_reply($c->$function()); } catch (\exceptions\PublicApiException $e) { return send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data()); } -- cgit v1.2.3-24-g4f1b From 9ea78213f8e505b5fde7372106adc1947d1f7de2 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 11:14:29 +0100 Subject: Improve general exception handling Signed-off-by: Florian Pritz --- application/controllers/api.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index dc31f47d2..3297f0614 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -51,6 +51,9 @@ class Api extends MY_Controller { return send_json_reply($c->$function()); } catch (\exceptions\PublicApiException $e) { return send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data()); + } catch (\Exception $e) { + _log_exception($e); + return send_json_error_reply("internal-error", "An unhandled internal server error occured"); } } } -- cgit v1.2.3-24-g4f1b From cb2df59b45d4cb35790472f76b06c59b22c6213b Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 10 Feb 2015 23:32:23 +0100 Subject: api: Require the version to start with v Makes the URL easier to understand (especially the v1 case). Signed-off-by: Florian Pritz --- application/controllers/api.php | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index 3297f0614..837f62e89 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -22,6 +22,13 @@ class Api extends MY_Controller { $requested_version = $this->uri->segment(2); $controller = $this->uri->segment(3); $function = $this->uri->segment(4); + + if (!preg_match("/^v([0-9]+)(.[0-9]+){0,2}$/", $requested_version)) { + throw new \exceptions\PublicApiException("api/invalid-version", "Invalid API version requested"); + } + + $requested_version = substr($requested_version, 1); + $major = intval(explode(".", $requested_version)[0]); if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { -- cgit v1.2.3-24-g4f1b From bfbbf4082779a7535cac2fb270fd928178ae7e70 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 14 Feb 2015 19:10:19 +0100 Subject: Unify exceptions for unknown/invalid endpoints Signed-off-by: Florian Pritz --- application/controllers/api.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index 837f62e89..644a726e7 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -32,11 +32,11 @@ class Api extends MY_Controller { $major = intval(explode(".", $requested_version)[0]); if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { - throw new \exceptions\PublicApiException("api/invalid-controller-value", "Invalid controller requested"); + throw new \exceptions\PublicApiException("api/invalid-endpoint", "Invalid endpoint requested"); } if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { - throw new \exceptions\PublicApiException("api/invalid-function-value", "Invalid function requested"); + throw new \exceptions\PublicApiException("api/invalid-endpoint", "Invalid endpoint requested"); } $namespace = "controllers\\api\\v".$major; @@ -48,12 +48,12 @@ class Api extends MY_Controller { } if (!class_exists($class)) { - throw new \exceptions\PublicApiException("api/unknown-controller", "Unknown controller requested"); + throw new \exceptions\PublicApiException("api/unknown-endpoint", "Unknown endpoint requested"); } $c= new $class; if (!method_exists($c, $function)) { - throw new \exceptions\PublicApiException("api/unknown-function", "Unknown function requested"); + throw new \exceptions\PublicApiException("api/unknown-endpoint", "Unknown endpoint requested"); } return send_json_reply($c->$function()); } catch (\exceptions\PublicApiException $e) { -- cgit v1.2.3-24-g4f1b