summaryrefslogtreecommitdiffstats
path: root/application/controllers/api.php
blob: 7557c6c994105c196659c70382667b1f7f95ca89 (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
<?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() {
		$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");
		}
		return $c->$function();
	}
}