This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMistAPI.php
115 lines (94 loc) · 3.27 KB
/
MistAPI.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/*
MistAPI ver. 0.1beta
--------------------
Extend this class to create your API.
*/
abstract class MistAPI{
private $endpoint = '';
private $method = '';
private $args = Array();
/**
* MistAPI constructor.
* @param $request
* @param array $allow
* @throws Exception
*/
public function __construct($request, $allow = ['*'])
{
$this->CORS($allow);
$this->args = explode('/', rtrim($request, '/'));
array_shift($this->args);
$this->endpoint = array_shift($this->args);
$this->method = $_SERVER['REQUEST_METHOD'];
if ($this->method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
$this->method = 'DELETE';
} else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
$this->method = 'PUT';
} else {
throw new Exception("Unexpected Header");
}
}
switch($this->method) {
case 'DELETE':
case 'POST':
$this->request = $this->_cleanInputs($_POST);
break;
case 'GET':
$this->request = $this->_cleanInputs($_GET);
break;
case 'PUT':
$this->request = $this->_cleanInputs($_GET);
$this->file = file_get_contents("php://input");
break;
case 'OPTIONS':
header("Allow: " . implode(", ", $allow));
break;
default:
$this->_response(["error" => "invalid-method", "method" => $this->method], 405);
break;
}
}
private function CORS($allow) {
header("Access-Control-Allow-Headers: Accept, Content-Type, Origin");
header("Access-Control-Allow-Methods: " . implode(", ", $allow));
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
}
public function ProcessAPI() {
if ($this->method == "OPTIONS") return $this->_response();
$res = false;
$func = strtolower($this->method) . '_' . $this->endpoint;
if (method_exists($this, $func)) $res = $this->{$func}($this->args);
if ($res)
return $this->_response($res);
else
return $this->_response(["error" => "not-found", "endpoint" => $this->endpoint], 404);
}
private function _response($data = false, $status = 200) {
header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
if ($data) return json_encode($data);
else return "";
}
private function _cleanInputs($data) {
$clean_input = Array();
if (is_array($data)) {
foreach ($data as $k => $v) {
$clean_input[$k] = $this->_cleanInputs($v);
}
} else {
$clean_input = trim(strip_tags($data));
}
return $clean_input;
}
private function _requestStatus($code) {
$status = array(
200 => 'OK',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
);
return ($status[$code])?$status[$code]:$status[500];
}
}