-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_handler.php
198 lines (164 loc) · 5.03 KB
/
api_handler.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
$output_format = "json";
header('Content-type: application/json');
if (array_key_exists('HTTP_AUTHORIZATION', $_SERVER)) {
$access_token = str_replace("Bearer ", "", $_SERVER['HTTP_AUTHORIZATION']);
}
if (!empty($access_token)) {
// Check who the access token belongs to
$token = db_execute("SELECT * FROM tokens WHERE access_token = ?", [$access_token]);
// if the token doesn't exist...
if (empty($token)) {
$invalid_token = true; // We won't tell this to the end-user immediately because I'd prefer to tell them about
// 404 first.
} else {
$token_owner = $token['owner_id'];
}
}
function check_authorisation($token=""): int
{
global $token_owner;
// Validate token
if (!validate_access_token($token) && "" != $token) {
return 0; // Unauthorised
}
// Check the type of token
$token_row = db_execute("SELECT * FROM tokens WHERE access_token = ?", [$token]);
if (null == $token_row) {
if (array_key_exists('auth', $_SESSION)) {
if ($_SESSION['auth']) {
$token_row = [
"type" => "dangerous"
];
$token_owner = $_SESSION['id'];
} else {
return 0;
}
} else {
return 0;
}
}
return match ($token_row['type']) {
"dangerous" => 1<<0 | 1<<1, // Everything
"basic" => 1<<1, // Basic
"oauth" => $token_row['permissions'],
default => 0,
};
}
// Misc (unauthorised)
function redirect_to_documentation(): void
{
header('Location: /docs/api');
}
// Health check
function api_health_check(): array
{
return ["message" => "Science compels us to explode the sun!", "time" => time(), "response_code" => 200];
}
// Potentially authenticated image endpoints
function get_avatar(): array
{
if (!array_key_exists('id', $query)) {
return [
'response_code' => 404,
'message' => 'ID not assigned/found'
];
}
$user_id = $query['id'];
return [];
}
// User (REQUIRES AUTHORISATION)
function api_user_info(): array
{
global $access_token, $token_owner;
// Authorisation levels:
// `display_name` = 1 (basic)
// `id` = 1 (basic)
// `email` = 1 (basic)
$level = check_authorisation($access_token);
$data = null;
if ($level & (1 << 0)) {
$data = db_execute("SELECT id, email, display_name FROM accounts WHERE id = ? LIMIT 1", [$token_owner]);
} else {
$data = db_execute("SELECT id, display_name FROM accounts WHERE id = ? LIMIT 1", [$token_owner]);
}
if (null != $data) {
return [
"response_code" => 200,
"data" => $data
];
}
http_response_code(401);
return [
"response_code" => 401,
"message" => "Unauthorized."
];
}
function api_settings(): array
{
// GET: Return all settings
// POST/PATCH: Update settings
global $access_token, $token_owner;
$level = check_authorisation($access_token);
if (!($level & (1 << 1))) { // account.settings
http_response_code(401);
return [
"response_code" => 401,
"message" => "Unauthorized."
];
}
if ($_SERVER['REQUEST_METHOD'] === "POST") {
// Now for the fucking worstest code ever
$settings_changed = json_decode(file_get_contents('php://input'), true);
if (isset($settings_changed['account'])) {
if (isset($settings_changed['account']['display_name'])) {
$display_name = db_execute('UPDATE accounts SET display_name = ? WHERE id = ?',
[$settings_changed['account']['display_name'], $token_owner]);
}
}
}
// Get account settings
$display_name = db_execute('SELECT display_name FROM accounts WHERE id = ?', [$token_owner])["display_name"];
return [
"response_code" => 200,
"settings" => [
"account" => [
"display_name" => $display_name,
]
]
];
}
$api_routes = [ // base url is base_url.'/api'
// "/path" => "function_name"
// Misc
"" => "redirect_to_documentation",
"/status" => "api_health_check",
// Account stuff
"/account/me" => "api_user_info",
// Settings
"/settings" => "api_settings",
// Get avatar
"/avatars/get" => "get_avatar"
];
$path = str_replace("/api", "", $path);
if (isset($api_routes[$path])) {
if (isset($invalid_token)) {
http_response_code(498);
echo (json_encode([
"response_code" => "498",
"message" => "Token expired or invalid."
]));
exit();
}
$response = $api_routes[$path]();
if (array_key_exists('response_code', $response)) {
http_response_code($response['response_code']);
}
echo json_encode($response);
} else {
http_response_code(404);
echo (json_encode([
"response_code" => "404",
"message" => "Route not found."
]));
}