-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunctions.cjs
411 lines (411 loc) · 15.2 KB
/
functions.cjs
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const { sign, verify } = require("jsonwebtoken");
const path = require('path');
const moment = require('moment');
const mongo = require('./mongo.cjs');
const fs = require('fs');
const getSize = require('get-folder-size');
const formidable = require('formidable');
let nav = ["chat", "player", "shooter", "notes", "webcam_face_detection", "hibo", "money", "clock"];
exports.clock = (req, res) => {
let now = moment();
res.render('clock');
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' money');
};
exports.save = function (req, res) {
var record = req.query;
record.user = req.session.user;
if (record.page !== "") {
req.session.page = record.page;
}
else {
delete req.session.page;
}
if (record.value !== "") {
if (record.repeat == "1") {
delete record.month;
delete record.year;
mongo.saveMove(record, function (resp) {
if (!resp) {
console.log('Nãosalvo');
}
});
}
else {
delete record.months;
delete record.years;
delete record.startm;
delete record.starty;
mongo.saveMove(record, function (resp) {
if (!resp) {
console.log('Nãosalvo2');
}
});
}
}
res.redirect('/money');
};
exports.six = function (req, res) {
delete req.session.page;
res.redirect('/money');
};
exports.money = (req, res) => {
let now = moment();
res.render('money', { title: 'Money', user: req.session.user, page: req.session.page });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' money');
};
exports.rag = (req, res) => {
let now = moment();
res.render('rag', { title: 'Ragnatuba', user: req.session.user });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' rag');
};
exports.tuba_player_privacy = (req, res) => {
let now = moment();
res.render('tuba_player_privacy', { title: 'tuba_player_privacy', user: req.session.user });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' tuba_player_privacy');
};
exports.privacy = (req, res) => {
let now = moment();
res.render('privacy', { title: 'Privacy', user: req.session.user });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' privacy');
};
exports.cookies = (req, res) => {
let now = moment();
res.render('cookies', { title: 'Cookies', user: req.session.user });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' cookies');
};
exports.talking = (req, res) => {
let now = moment();
res.render('talking', { title: 'Talking', user: req.session.user });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' talking');
};
exports.default = (req, res) => {
let now = moment();
res.render('default', { title: 'Default', user: req.session.user });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' default');
};
exports.home = (req, res) => {
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user };
if (req.session.verified || req.session.user) {
let data = { title: 'Dashboard' };
data.nav = nav;
data.user = req.session.user;
res.render('dashboard', data);
visit.page = "dashboard";
} else {
let data = { title: 'Home' }
visit.page = "home"
res.render('home', data)
}
mongo.saveRecord.bind(req.db)('visits', visit);
};
exports.profile = (req, res) => {
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user, page: "profile" };
let data = { title: 'Profile' };
data.token = sign({ username: req.session.user, email: req.session.email }, process.env.JWT_KEY);
data.user = req.session.user;
mongo.getUserInfo.bind(req.db)(req.session.user, (err, resp) => {
if (err) {
console.log(err);
}
else {
data.userinfo = resp;
res.render("profile", data);
mongo.saveVisit.bind(req.db)(visit);
}
});
};
exports.login = (req, res) => {
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user };
mongo.existUser.bind(req.db)(req.body.username, exist => {
if (exist) {
mongo.auth.bind(req.db)(req.body.username, req.body.password, (success) => {
if (success) {
req.session.user = req.body.username;
req.session.email = exist.email;
req.session.permission = exist.permission || 1;
req.session.verified = true;
if (req.body.url == "/login")
res.redirect("home");
else
res.redirect(req.body.url);
}
else {
res.render('home', { title: 'Home', msg: 'Wrong password' });
}
});
}
else {
res.render('home', { title: 'Home', msg: 'User don\'t exists' });
}
});
visit.page = "login";
mongo.saveRecord.bind(req.db)('visits', visit);
};
exports.logout = (req, res) => {
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user, page: "about" };
req.session.destroy();
res.redirect('/home');
mongo.saveRecord.bind(req.db)('visits', visit);
};
exports.signup = (req, res) => {
let now = moment();
mongo.existUser.bind(req.db)(req.body.username, (exist) => {
if (exist) {
res.render('home', { title: 'home', msg: 'User already exists' });
}
else {
console.log(req.body.username);
mongo.addUser.bind(req.db)(req.body.username, req.body.password, req.body.email, (success) => {
if (success) {
req.session.user = req.body.username;
req.session.email = req.body.email;
res.redirect('/auth?id=' + success.ops[0]._id);
}
else {
res.render('home', { title: 'home', msg: 'User not registered' });
}
});
}
});
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' sigup');
};
exports.auth = (req, res) => {
if (req.query.id) {
mongo.existId.bind(req.db)(req.query.id, record => {
if (record) {
req.session.verified = true;
record.verified = true;
mongo.updateRecord.bind(req.db)('users', { _id: record._id }, record, resp => {
if (resp) {
let dir = __dirname + '/public/users/' + record.username;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
res.redirect('/');
}
});
}
});
}
else {
mongo.setEmail.bind(req.db)({ user: req.session.user, email: req.query.email }, resp => {
if (resp) {
//otherEmail = true;
req.session.email = req.query.email;
mongo.existUser.bind(req.db)(req.session.user, exist => {
if (exist)
res.redirect('/auth?id=' + exist._id);
});
}
});
}
};
exports.dashboard = (req, res) => {
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user };
if (req.session.verified || req.session.user) {
let data = { title: 'Dashboard' };
data.user = req.session.user;
data.nav = nav;
res.render('dashboard', data);
visit.page = "dashboard";
} else {
let data = { title: 'Home' }
visit.page = "home"
res.render('home', data)
}
mongo.saveRecord.bind(req.db)('visits', visit)
};
exports.player = (req, res) => {
let dir = path.join(__dirname, '/public/users/', (req.params.user ? req.params.user : req.session.user));
let date = new Date();
let data = { title: 'Player' };
data.token = 'none';
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user, page: "player" };
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
getSize(dir, (err, folder_size) => {
if (err)
console.log(err);
else
fs.readdir(dir, (err, files) => {
if (err)
throw err;
data.musics = files;
data.size = (folder_size / 1024 / 1024).toFixed(2);
//data.user = req.session.user
if (req.params.user)
data = { ...data, user: req.params.user, owner: false };
else
data = { ...data, user: req.session.user, owner: true };
data.permission = req.session.permission || 1;
data.token = sign({ username: req.session.user, email: req.session.email, permission: data.permission }, process.env.JWT_KEY);
res.render('player', data);
mongo.saveRecord.bind(req.db)('visits', visit);
});
});
};
exports.notes = (req, res) => {
let data = { title: 'Notes' };
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user, page: "notes" };
data.user = req.session.user, data.nav = nav;
mongo.takeNotes.bind(req.db)(req.session.user, (err, docs) => {
if (!docs) {
res.render('notes', data);
}
else {
if (docs) {
data.notes = docs;
res.render('notes', data);
}
else {
res.render('notes', data);
}
}
});
mongo.saveRecord.bind(req.db)('visits', visit);
};
exports.chat = (req, res) => {
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.session.user, page: "chat" };
let data = { ip: req.ip, title: 'Chat' };
if (req.session.user != null) {
data.user = req.session.user;
}
else if (req.query.user) {
data.user = 'u' + Math.floor(Math.random() * 100000000);
}
if (req.params.room) {
data.room = req.params.room;
}
res.render('chat', data);
//mongo.saveRecord.apply( {db:req.db}, ['visits', visit])
//mongo.saveRecord.call({db:req.db},'visits', visit)
mongo.saveRecord.bind(req.db)('visits', visit);
};
exports.shooter = (req, res) => {
let now = moment();
res.render('shooter', { title: 'Shooter', user: req.session.user });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' shooter');
};
// API CODE
exports.songs = async (req, res) => {
let date = new Date();
let visit = { ip: req.ip, date: date.getTime(), user: req.me.username, page: "songs" };
let dir = __dirname + '/public/users/' + req.me.username;
//let songs =
if (!fs.existsSync(dir)) {
res.status(404).send(`User ${req.me.username} has no songs stored in the server!`);
}
else {
fs.readdir(dir, (err, files) => {
if (err)
res.send(`Error: ${err}`);
else {
const shufle = b => {
let a = [...b]; for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
} return a;
};
res.json(shufle(files));
mongo.saveVisit.bind(req.db)(visit);
}
});
}
};
exports.jwt = (req, res) => {
let now = moment();
mongo.auth.bind(req.db)(req.body.username, req.body.password, user => {
if (user) {
const token = sign({ ...user }, process.env.JWT_KEY);
res.header("auth-token", token).json({ ok: true, token: token, data: user });
}
else {
res.json({ ok: false, msg: "Check your user or password" });
}
});
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' jwt');
};
exports.join = (req, res) => {
let now = moment();
mongo.existUser.bind(req.db)(req.body.username, (exist) => {
if (exist) {
res.json({ ok: false, msg: 'User already exists' });
}
else {
console.log(req.body.username);
mongo.addUser.bind(req.db)(req.body.username, req.body.password, req.body.email, (success) => {
if (success) {
const data = { username: req.body.username, email: req.body.email };
const token = sign(data, process.env.JWT_KEY);
res.header("auth-token", token).json({ ok: true, token: token, data: data });
}
else {
res.json({ ok: false, msg: 'Error occurred, user not registered' });
}
});
}
});
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' join');
};
exports.audio = async (req, res) => {
let now = moment();
const data = verify(req.header('token'), process.env.JWT_KEY);
if (data.username === req.params.user) {
const p = path.join(__dirname, 'public/users', req.params.user);
if (!fs.existsSync(p))
fs.mkdirSync(p);
const size = () => new Promise((reso, reje) => getSize(p, (err, folder_size) => {
if (err)
console.log(err);
else
reso((folder_size / 1024 / 1024 / 1024).toFixed(2));
}));
let dirSize = await size();
if (dirSize > (data.permission || 1)) {
res.json({ ok: false, msg: `You've reached your limit of ${data.permission || 1}GB.` });
}
else {
const formi = formidable({ keepExtensions: true, uploadDir: p });
formi.parse(req, (err, fields, files) => {
if (err)
res.json({ ok: false, error: err });
let songs = [];
for (let key of Object.keys(files)) {
let oldn = files[key].path;
let newn = oldn.substr(0, oldn.lastIndexOf('/') + 1) + files[key].name;
fs.renameSync(oldn, newn);
songs.push(files[key].name);
}
res.json({ ok: true, song: songs.length === 1 ? songs[0] : songs });
});
}
}
else
res.json({ ok: false, msg: "You must to be authenticated to upload" });
console.log(req.ip + " " + now.format('DD/MM/YYYY HH:mm:ss') + ' audio');
};
exports.cp = async (req, res) => {
let now = moment();
const data = verify(req.body.token, process.env.JWT_KEY);
if (data.username === req.body.user) {
let np = req.body.password.trim();
if (np != "") {
let worked = await mongo.setPassword.bind(req.db)(req.body.user, np);
res.json({ success: worked, msg: 'Password set!' });
}
else
res.json({ success: false, msg: 'Password cannot be empty!' });
}
else
res.json({ success: false, msg: 'You should be the same user!' });
};