/
control.js
executable file
·541 lines (530 loc) · 17.5 KB
/
control.js
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
var formidable = require('formidable');
var fs = require('fs');
var db = require('./database');
var errors = require('../routing/errors');
var crypt = require('./admin_crypt');
var processing = require('./post_processing');
var captcha = require('./captcha');
var stack = require('./stack');
var checking = require('./checking_board');
var messages = require('../../configs/messages');
var app_config = require('../../configs/app');
var fo = require('./file_operation');
//RegExp
var re_num = new RegExp('^[0-9]+$');
var re_board = new RegExp('^[a-z0-9]+$');
//reports handling
function report(req, res, next) {
var admin_id = res.modId;
var report_id = req.body.id;
var resolution = req.body.resolution;
db.reports.update({
respondent: admin_id,
resolution: resolution,
active: 0
}, {
where: {
id: report_id,
active: 1
}
}).then(function() {
res.redirect('/admin/reports');
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
};
//editing spam list
function spam(req, res, next) {
var board = req.body.board;
var word = req.body.word;
fo.read('app/data/spam.json').then(function(spam_list) {
if(!spam_list[board]) {
throw new Error('Board not defined');
}
var position = spam_list[board].indexOf(word);
if(position == '-1') {
spam_list[board].push(word);
}
else {
spam_list[board].splice(position, 1);
}
return fo.write('app/data/spam.json', spam_list);
}).then(function() {
res.redirect('/admin/spam');
}).catch(function(err) {
console.log(err);
errors.e500(req, res, next);
});
};
//ban and unban
function ban(req, res, next) {
var type = req.body.type;
var respondent = res.modId;
var time = req.body.time;
var reason = req.body.reason;
var board = req.body.board || null;
switch(type) {
case "1":
//ban to IP
var ip = req.body.ip;
db.bans.create({
ip: ip,
board: board,
reason: reason,
respondent: respondent,
time: time
}).then(function() {
res.redirect('/admin/bans');
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
break;
case "2":
//ban to post's ID
var post_id = req.body.post_id;
db[board + '_posts'].findById(post_id).then(function(post) {
if(post) {
db.bans.create({
ip: post.ip,
board: board,
reason: reason,
respondent: respondent,
time: time
});
res.redirect('/admin/bans');
}
else {
errors.e500(req, res, next);
}
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
break;
case "3":
//unban to ID
var unban_id = req.body.num;
db.bans.destroy({where: {
id: unban_id
}}).then(function() {
res.redirect('/admin/bans');
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
break;
default:
errors.e500(req, res, next);
break;
};
};
//operation with boards for admins
function boards(req, res, next) {
var board_name = req.body.name.toLowerCase();
var bumplimit = req.body.bump;
var pages = req.body.pages;
var threads = req.body.threads;
//board's data
var board_data = {};
board_data.name = req.body.public_name;
board_data.category = req.body.category;
board_data.default_username = req.body.username;
board_data.image_permit = Boolean(req.body.image);
board_data.trip_permit = Boolean(req.body.tripcode);
board_data.trip_required = Boolean(req.body.tripcode_require);
board_data.bumplimit = req.body.bump;
board_data.pages = req.body.pages;
board_data.thread_in_page = req.body.threads;
board_data.size = req.body.max_size;
board_data.hidden = Boolean(req.body.hidden);
board_data.image_require = Boolean(req.body.image_require);
board_data.roulette = Boolean(req.body.roulette);
//incorrect data type
if (!(
re_num.test(bumplimit)
&& re_num.test(req.body.pages)
&& re_num.test(req.body.bump)
&& re_num.test(req.body.max_size)
&& re_board.test(board_name))) {
errors.e500(req, res, next);
return null;
}
fo.read('app/data/boards.json').then(function(boards) {
if(!boards[board_name]) {
fs.mkdir('client/source/img/trd/' + board_name, function(err) {
console.log('Directory already exist');
});
}
boards[board_name] = board_data;
fo.write('app/data/boards.json', boards);
return fo.read('app/data/spam.json');
}).then(function(spam_list) {
if(!spam_list[board_name]) {
spam_list[board_name] = [];
fo.write('app/data/spam.json', spam_list);
}
res.redirect('/admin/boards');
}).catch(function(err) {
console.log(err);
errors.e500(req, res, next);
});
};
//operation with admins
function admin(req, res, next) {
if(req.body.id) {
db.admins.update({
active: 0
}, {
where: {
id: req.body.id
}
}).then(function() {
res.redirect('/admin/admins');
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
}
else {
var name = req.body.name;
var pass = req.body.pass;
var status = req.body.status;
var all_boards = req.body.all_boards;
var boards = req.body.boards;
db.admins.findAll({
where: {
name: name
}
}).then(function(result) {
if(result.length) {
res.redirect('/admin/admins#incorrect');
}
else {
var pass_enc = crypt.encrypt(pass);
var need_board;
if(all_boards) {
need_board = null;
}
else {
need_board = JSON.stringify(boards) || null;
}
return db.admins.create({
name: name,
pass: pass_enc,
status: status,
boards: need_board
});
}
}).then(function() {
res.redirect('/admin/admins');
}).catch(function(err) {
console.log(err);
errors.e500(req, res, next);
});
}
};
//creating post or thread
function posting(req, res, next) {
var form = new formidable.IncomingForm({
uploadDir: "tmp",
encoding: 'utf-8',
maxFieldsSize: 2 * 1024 * 1024
});
form.parse(req, function(err, fields, files) {
if(err) {
console.log(err);
res.end('6');
return Promise.reject('6');
}
var board = req.params.name;
var ip = req.ip;
var thread = req.params.num || null;
var image = files.image;
var require_trip;
if(image && image.size == 0) {
fs.unlink(image.path, function(err) {
if(err) console.log('Image not exist');
});
}
//captcha
var c_key = fields.c_key;
var c_value = fields.c_value;
//captcha check
if (!captcha.check_f(c_key, c_value)) {
res.end('1');
return Promise.reject('1');
}
if(req.cookies['pause_post_' + board] && thread) {
res.end('10');
return Promise.reject('10');
}
if(req.cookies['pause_thread_' + board] && !thread) {
res.end('10');
return Promise.reject('10');
}
var boards_data, name, title, text, sage, name_trip, img;
//loading data
fo.read('app/data/boards.json').then(function (boards) {
boards_data = boards;
//main data
name = fields.name || boards[board].default_username;
title = (fields.title || '').substr(0, 40);
text = processing.text(fields.text, boards[board].roulette);
sage = fields.sage || 0;
//processing
name_trip = processing.trip(name, boards[board].trip_permit);
//generating tripcode for special boards
if(boards[board].trip_required) {
require_trip = processing.require_trip(ip);
}
//require image for special boards
if(boards[board].image_require && image && !Boolean(image.size) && !thread) {
res.end('9');
return Promise.reject('9');
}
//checking spam
return processing.spam(text, board);
}).then(function () {
//checking ban
return processing.ban(ip, board);
}).then(function () {
//checking lock thread
if(thread) {
return processing.lock(board, thread)
}
else {
return Promise.resolve();
}
}).then(function () {
//image processing
if(image && image.size) {
return processing.image(image, board);
}
else {
return Promise.resolve();
}
}).then(function(image_ext) {
img = image_ext;
var post_data = {
title: title,
name: name_trip.name,
trip: require_trip || name_trip.trip,
text: text,
thread: thread,
ip: req.ip,
sage: sage,
admin: res.modId || null
};
return Promise.all([db.boards[board].create(post_data), processing.count(board, thread)]);
}).then(function (result) {
//create thread
if (!thread) {
fs.mkdir('client/source/img/trd/' + board + '/' + result[0].id, function(err) {
if(err) console.log(err);
});
stack.new(board, +result[0].id);
}
//create post
else if (!sage && thread && result[1] < boards_data[board].bumplimit) {
stack.bump(board, +thread);
}
//antispam cookie
var cookie_type, cookie_time;
if(thread) {
cookie_type = 'post';
cookie_time = app_config.seconds_post;
}
else {
cookie_type = 'thread';
cookie_time = app_config.seconds_thread;
}
res.cookie('pause_' + cookie_type + '_' + board, true, {maxAge: cookie_time * 1000});
res.end('0');
var need_thread = result[0].thread || result[0].id;
if(img) {
fs.rename(
image.path,
'client/source/img/trd/' + board + '/' + need_thread + '/' + result[0].id + '.' + img
, function(err) {
if (err) console.log('Error saving image');
});
db.boards[board].update({
image: img
}, {where: {
id: result[0].id
}})
}
}).catch(function (err) {
console.log(err);
res.end(err.toString());
});
});
};
//admin actions with threads
function actions(req, res, next) {
var type = req.body.type;
var thread = req.body.thread;
var board = req.body.board;
var status = res.modStatus;
var mod_id = res.modId;
db.admins.findById(mod_id).then(function(admin) {
//if not admin
if(!admin) {
errors.e403(req, res, next);
return;
}
//attach/detach thread
if(type == 1 && status >= 2 && checking(admin.boards, board)) {
stack.attachment(board, thread);
res.redirect('/' + board);
}
//close/open thread
else if(type == 2 && status >= 2 && checking(admin.boards, board)) {
db.boards[board].findOne({where: {
id: thread,
thread: null
}}).then(function(selected_thread) {
if(!selected_thread) {
errors.e500(req, res, next);
return;
}
var need_status;
selected_thread.close == 0 ? need_status = 1 : need_status = 0;
db.boards[board].update({close: need_status}, {where: {
id: thread
}});
res.redirect('/' + board);
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
}
//deleting thread
else if(type == 3 && status >= 2 && checking(admin.boards, board)) {
stack.deleting(board, thread);
res.redirect('/' + board);
}
//deleting post
else if(type == 4 && status >= 1 && checking(admin.boards, board)) {
db.boards[board].destroy({where: {
id: thread,
thread: {
$ne: null
}
}});
res.redirect('/' + board);
}
//ban author
else if(type == 5 && status >= 2 && checking(admin.boards, board)) {
var time = req.body.time || 7;
var reason = req.body.reason;
db.boards[board].findById(thread).then(function(post) {
if(!post) {
errors.e500(req, res, next);
return;
}
var ban_data = {
ip: post.ip,
board: board,
reason: reason,
respondent: mod_id,
time: time
};
db.bans.create(ban_data);
db.boards[board].update({
text: post.text + '<br><span class="message">' + messages.ban + '</span>'
}, {where: {
id: post.id
}});
res.redirect('/' + board);
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
}
//Deleting all author's posts (not thread)
else if(type == 6 && status >= 2 && checking(admin.boards, board)) {
db.boards[board].findById(thread).then(function(post) {
if(!post) {
errors.e500(req, res, next);
return;
}
db.boards[board].destroy({where: {
ip: post.ip,
thread: {
$ne: null
}
}});
res.redirect('/' + board);
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
}
//Deleting all author's posts and thread
else if(type == 7 && status >= 2 && checking(admin.boards, board)) {
var need_post;
db.boards[board].findById(thread).then(function(post) {
if(!post) {
errors.e500(req, res, next);
return;
}
need_post = post;
return db.boards[board].findAll({where: {
ip: post.ip,
thread: null
}});
}).then(function(threads) {
var deleting_threads = stack.thread_num(threads);
deleting_threads.forEach(function(item) {
stack.deleting(board, item);
});
db.boards[board].destroy({where: {
ip: need_post.ip,
thread: {
$ne: null
}
}});
res.redirect('/' + board);
}).catch(function(err) {
console.log(err);
errors.e500(req, res, next);
});
}
//Prevention author
else if(type == 8 && status >= 1 && checking(admin.boards, board)) {
db.boards[board].findById(thread).then(function(post) {
db.boards[board].update({
text: post.text + '<br><span class="message">' + messages.prevention + '</span>'
}, {
where: {
id: thread
}
});
});
res.redirect('/' + board);
}
//incorrect type or incorrect rights
else {
errors.e403(req, res, next);
}
}, function(err) {
console.log(err);
errors.e500(req, res, next);
});
};
//deleting admin cookie
function admin_exit(req, res, next) {
res.clearCookie('hanako_admin');
res.redirect('/');
};
exports.report = report;
exports.spam = spam;
exports.ban = ban;
exports.boards = boards;
exports.admin = admin;
exports.posting = posting;
exports.actions = actions;
exports.exit = admin_exit;