summaryrefslogtreecommitdiffstats
path: root/application/controllers/Api.php
blob: 4f32ad427f2eed7c4d7767d8163f766a566e55f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
 * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * 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() {
		try {
			$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\UserInputException("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)) {
				throw new \exceptions\UserInputException("api/invalid-endpoint", "Invalid endpoint requested");
			}

			if (!preg_match("/^[a-zA-Z-_]+$/", $function)) {
				throw new \exceptions\UserInputException("api/invalid-endpoint", "Invalid endpoint 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\UserInputException("api/version-not-supported", "Requested API version is not supported");
			}

			if (!class_exists($class)) {
				throw new \exceptions\UserInputException("api/unknown-endpoint", "Unknown endpoint requested");
			}

			$c= new $class;
			if (!method_exists($c, $function)) {
				throw new \exceptions\UserInputException("api/unknown-endpoint", "Unknown endpoint requested");
			}
			return $this->send_json_reply($c->$function());
		} catch (\exceptions\PublicApiException $e) {
			return $this->send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data());
		} catch (\Exception $e) {
			\libraries\ExceptionHandler::log_exception($e);
			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));
	}

}