Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
302 lines (300 loc) · 15.6 KB
/
Copy pathapi.js
File metadata and controls
302 lines (300 loc) · 15.6 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const backend = require('./backend.js');
const config = require("./config.js");
const chalk = require('chalk');
const moment = require('moment-timezone');
module.exports = async function(app, con) {
app.get('/api', async function(req, res) {
return res.redirect(`https://docs.hyperz.net/c/products/boardsapi`)
});
app.get('/api/get/:section/:data', async function(req, res) {
res.set('Access-Control-Allow-Origin', '*');
if(!req.params.section) {
let json_ = {
"pass": false,
"reason": "No section provided within URL request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
req.params.section = req.params.section.toLowerCase();
if(!req?.headers) {
let json_ = {
"pass": false,
"reason": "No headers provided within the request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
if(!req?.headers?.authorization) {
let json_ = {
"pass": false,
"reason": "No authorization header was provided within the request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
req.headers.authorization = await backend.sanitize(req.headers.authorization);
await con.query(`SELECT * FROM apikeys WHERE apikey="${req.headers.authorization}"`, async (err, row) => {
if(err) throw err;
if(row[0]) {
let section;
switch(req.params.section) {
case 'forum':
section = 'forum';
break;
case 'topic':
section = 'topic';
break;
case 'user':
section = 'user';
break;
case 'statistics':
section = 'statistics';
break;
default:
section = 'none';
};
if(section == 'none') {
let json_ = {
"pass": false,
"reason": "Section provided within URL request was invalid."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
} else if(section == 'statistics') {
await con.query(`SELECT * FROM users`, async (err, row) => {
if(err) throw err;
let users = row.length || 0;
await con.query(`SELECT * FROM topics`, async (err, row) => {
if(err) throw err;
let topics = row.length || 0;
await con.query(`SELECT * FROM categories`, async (err, row) => {
if(err) throw err;
let categories = row.length || 0;
await con.query(`SELECT * FROM comments`, async (err, row) => {
if(err) throw err;
let comments = row.length || 0;
await con.query(`SELECT * FROM bannedusers`, async (err, row) => {
if(err) throw err;
let bannedusers = row.length || 0;
await con.query(`SELECT * FROM staff`, async (err, row) => {
if(err) throw err;
let staff = row.length || 0;
await con.query(`SELECT * FROM auditlogs`, async (err, row) => {
if(err) throw err;
let auditlogs = row.length || 0;
await con.query(`SELECT * FROM events`, async (err, row) => {
if(err) throw err;
let events = row.length || 0;
await con.query(`SELECT * FROM socials`, async (err, row) => {
if(err) throw err;
let socials = row.length || 0;
await con.query(`SELECT * FROM gallery`, async (err, row) => {
if(err) throw err;
let gallery = row.length || 0;
await con.query(`SELECT * FROM custompages`, async (err, row) => {
if(err) throw err;
let custompages = row.length || 0;
let json_ = {
"pass": true,
"code": 200,
"stats": {
"users": users,
"topics": topics,
"staff": staff,
"socials": socials,
"auditlogs": auditlogs,
"events": events,
"gallery": gallery,
"custompages": custompages,
"bannedusers": bannedusers,
"categories": categories,
"comments": comments
}
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
});
});
});
});
});
});
});
});
});
});
});
} else if(section == 'forum') {
if(!req.params.data) {
let json_ = {
"pass": false,
"reason": "No data was provided within URL parameters."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
req.params.data = await backend.sanitize(req.params.data);
await con.query(`SELECT * FROM categories WHERE uniqueid="${req.params.data}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) {
let json_ = {
"pass": true,
"code": 200,
"forum": {}
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
} else {
let json_ = {
"pass": true,
"code": 200,
"forum": row[0]
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
});
} else if(section == 'topic') {
if(!req.params.data) {
let json_ = {
"pass": false,
"reason": "No data was provided within URL parameters."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
req.params.data = await backend.sanitize(req.params.data);
await con.query(`SELECT * FROM topics WHERE link="${req.params.data}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) {
let json_ = {
"pass": true,
"code": 200,
"topic": {}
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
} else {
let json_ = {
"pass": true,
"code": 200,
"topic": row[0]
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
});
} else if(section == 'user') {
if(!req.params.data) {
let json_ = {
"pass": false,
"reason": "No data was provided within URL parameters."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
req.params.data = await backend.sanitize(req.params.data);
await con.query(`SELECT * FROM staff WHERE userid="${req.params.data}"`, async (err, row) => {
if(err) throw err;
let staff;
if(row[0]) {
staff = true;
} else {
staff = false;
};
await con.query(`SELECT * FROM bannedusers WHERE userid="${req.params.data}"`, async (err, row) => {
if(err) throw err;
let isBanned;
if(row[0]) {
isBanned = true;
} else {
isBanned = false;
};
await con.query(`SELECT * FROM users WHERE userid="${req.params.data}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) {
let json_ = {
"pass": true,
"code": 200,
"user": {},
"isBanned": isBanned,
"isStaff": staff
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
} else {
let json_ = {
"pass": true,
"code": 200,
"user": row[0],
"isBanned": isBanned,
"isStaff": staff
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
});
});
});
};
} else {
let json_ = {
"pass": false,
"reason": "Invalid authorization token provided within the request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
});
});
app.post('/api/post/:section', async function(req, res) {
res.set('Access-Control-Allow-Origin', '*');
if(!req.params.section) {
let json_ = {
"pass": false,
"reason": "No section provided within URL request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
req.params.section = req.params.section.toLowerCase();
if(!req?.headers) {
let json_ = {
"pass": false,
"reason": "No headers provided within the request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
if(!req?.headers?.authorization) {
let json_ = {
"pass": false,
"reason": "No authorization header was provided within the request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
req.headers.authorization = await backend.sanitize(req.headers.authorization);
await con.query(`SELECT * FROM apikeys WHERE apikey="${req.headers.authorization}"`, async (err, row) => {
if(err) throw err;
if(row[0]) {
let section;
switch(req.params.section) {
case 'auditlogs':
section = 'auditlogs';
break;
default:
section = 'none';
};
if(section == 'none') {
let json_ = {
"pass": false,
"reason": "Section provided within URL request was invalid."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
} else if(section == 'auditlogs') {
if(typeof req.body.userbased != 'boolean') req.body.userbased = false;
if(typeof req.body.punishment != 'boolean') req.body.punishment = false;
await backend.createAudit(req.body.userbased, req.body.punishment, `${req.body.userid || ''}`, `${req.body.content || 'Unspecified'}`);
let json_ = {
"pass": true,
"code": 200
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
} else {
let json_ = {
"pass": false,
"reason": "Invalid authorization token provided within the request."
};
return res.type('json').send(JSON.stringify(json_, null, 4) + '\n');
};
});
});
setTimeout(() => {
console.log(`${chalk.blue(`[API Manager]`)} Community Boards API is now initialized.`);
}, 4000);
};