Repository navigation
Expand file tree
/
Copy pathbackend.js
More file actions
1588 lines (1548 loc) · 87.7 KB
/
Copy pathbackend.js
File metadata and controls
1588 lines (1548 loc) · 87.7 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
const config = require("./config.json");
const passport = require('passport');
const multer = require('multer');
const bodyParser = require('body-parser');
const session = require('express-session');
const express = require("express");
const flash = require('express-flash');
const figlet = require('figlet');
const chalk = require('chalk');
const axios = require('axios');
const ms = require('ms');
const fs = require('node:fs');
const path = require('path');
const moment = require('moment-timezone')
const requestIp = require('request-ip');
const firewallgg = require('firewallgg');
const bcrypt = require('bcrypt');
const PaypalImport = require('@paypal/checkout-server-sdk');
const Stripe = require('stripe');
let stripe;
let paypalClient;
if(config.paymentSettings.usePaypal) {
let liveEnv = new PaypalImport.core.LiveEnvironment(config.paymentSettings.paypalClientId, config.paymentSettings.paypalClientSecret);
paypalClient = new PaypalImport.core.PayPalHttpClient(liveEnv);
};
if(config.paymentSettings.useStripe) {
stripe = new Stripe(config.paymentSettings.stripeSecretKey, {
timeout: 20 * 1000, // 20 seconds
maxNetworkRetries: 2
});
};
const nodemailer = require('nodemailer');
let transporter = null;
if(config?.emails?.enabled) {
transporter = nodemailer.createTransport(config.emails.transporter);
};
const { Client } = require('discord.js');
class HDClient extends Client {
constructor(options = {}) {
super(options);
this.config = config;
this.discord = require('discord.js');
};
};
const client = new HDClient({
intents: ['GUILDS', 'GUILD_MESSAGES', "GUILD_MESSAGE_REACTIONS", "DIRECT_MESSAGES", "GUILD_MEMBERS", "GUILD_BANS", "GUILD_INTEGRATIONS", "GUILD_WEBHOOKS", "GUILD_INVITES", "GUILD_VOICE_STATES", "GUILD_PRESENCES", "GUILD_MESSAGE_TYPING", "DIRECT_MESSAGE_REACTIONS", "DIRECT_MESSAGE_TYPING"],
partials: ['MESSAGE', 'CHANNEL', 'REACTION', 'ROLE', "GUILD_MEMBER", "USER", "GUILD_INVITES", "MANAGE_GUILD"],
allowedMentions: { parse: ['users', 'roles', 'everyone'], repliedUser: true }
});
const pjson = require('./package.json');
const utils = require('hyperz-utils');
let dbcon;
let storedAppVariable;
let d;
if(config.domain.endsWith('/')) {
d = config.domain;
} else {
d = config.domain + '/';
};
// Rejection Handler
process.on('unhandledRejection', (err) => {
if(err.toString().replaceAll(' ', '').includes('cachedDataRejected')) return;
if(config.debugMode) console.log(chalk.red(err));
});
async function init(app, con) {
if (Number(process.version.slice(1).split(".")[0] < 16)) console.log(`Node.js v16 or higher is required, Discord.JS relies on this version, please update @ https://nodejs.org`);
var multerStorage = multer.memoryStorage()
app.use(multer({ storage: multerStorage }).any());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(flash()); // passport flash system for live messages
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
cookie: {maxAge: 31556952000},
}));
app.use(passport.initialize());
app.use(passport.session());
app.set('views', './src/views');
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.static('src/static'));
app.use('/assets', express.static(__dirname + 'public/assets'));
app.use('/static', express.static(__dirname + 'src/static/assets'));
app.use(requestIp.mw());
dbcon = con;
let font = await utils.getRandomArray(["Graffiti", "Standard", "Slant"])
figlet.text('Boss Webstore.', { font: font, width: 700 }, function(err, data) {
if(err) throw err;
let str = `${data}\n-------------------------------------------`
console.log(chalk.bold(chalk.blueBright(str)));
});
require('./src/updateManager.js')(con);
//licenseCheck();
storedAppVariable = app;
setTimeout(async () => {
let currver = pjson.version
let request = await axios({
method: 'get',
url: `https://raw.githubusercontent.com/Itz-Hyperz/version-pub-api/main/versions.json`,
headers: {Accept: 'application/json, text/plain, */*','User-Agent': '*' }
});
let latestver = request.data.bosswebstore
if(latestver != currver) {
console.log(`${chalk.yellow(`[Version Checker]`)} ${chalk.red(`You are not on the latest version.\nCurrent Version: ${currver}\nLatest Version: ${latestver}`)}`)
} else {
console.log(`${chalk.green(`[Version Checker]`)} You are on the latest version.`)
};
}, 3000);
await resetAppLocals(app);
sqlLoop(con);
markSqlConnected();
// Subscription Enforcement
setInterval(async function() {
subscriptionEnforcement();
dbcon.query(`UPDATE users SET discount=0`, function(err, row) {
if(err) throw err;
});
//licenseCheck();
let currentDate = await moment.tz('EST').format('MM-DD-YYYY');
dbcon.query(`DELETE FROM discounts WHERE expiration="${currentDate}"`, (err, row) => {
if(err) throw err;
});
}, 32400000) // 9 hours in milliseconds (32400000)
setInterval(async function() {
resetAppLocals(app);
}, ms('24h'));
initDiscordBot();
setTimeout(async function() {
// Extension Handler
const extensionsFolder = await fs.readdirSync(path.join(__dirname, `./`, `extensions`));
await extensionsFolder.forEach(async function(extFolder) {
let extPath = path.join(__dirname, `./`, `extensions/${extFolder}`)
let extFolderName = extFolder;
extFolder = fs.readdirSync(extPath);
if(extFolder.includes('extension.json')) {
let extConfig = require(extPath + '/extension.json');
console.log(`${chalk.cyanBright('[Extension Manager]')} ${extConfig?.name} (${extConfig?.version}) - Loaded`);
if(fs.existsSync(extPath + `/${extConfig?.file}`)) {
require(`./extensions/${extFolderName}/${extConfig?.file}`)(client, con, app);
} else {
console.log(`${chalk.cyanBright('[Extension Manager]')} ${chalk.yellowBright('FAILED:')} ${extFolderName} - Missing executable file.`);
};
} else {
console.log(`${chalk.cyanBright('[Extension Manager]')} ${chalk.yellowBright('FAILED:')} ${extFolderName} - Missing data file.`);
};
});
if(typeof extensions == 'undefined') return;
for(let ext of extensions) {
const extName = ext.split('.')[0];
console.log(`${chalk.cyanBright('[Extension Manager]')} ${extName} - Loaded`);
require(`./extensions/${ext}`)(client, con, app);
};
}, 1500);
};
async function subscriptionEnforcement() {
let currentDate = await moment.tz('EST').format('MM-DD-YYYY');
dbcon.query(`SELECT * FROM subscriptions WHERE nextduedate="${currentDate}"`, async function(err, row) {
if(err) throw err;
for(let item of row) {
dbcon.query(`DELETE FROM invoices WHERE uniqueid="${item.invoiceid}" LIMIT 1`, async (err, row) => {
if(err) throw err;
audit(`Invoice Deleted`, `${item.userid} failed to pay their subscription.`);
});
dbcon.query(`DELETE FROM owneditems WHERE uniqueid="${item.owneditemid}" LIMIT 1`, async (err, row) => {
if(err) throw err;
audit(`Owned Item Deleted`, `${item.userid} failed to pay their subscription.`);
});
dbcon.query(`DELETE FROM subscriptions WHERE uniqueid="${item.uniqueid}" LIMIT 1`, async (err, row) => {
if(err) throw err;
audit(`Subscription Deleted`, `${item.userid} failed to pay their subscription.`);
});
};
});
};
async function fetchTebexPackage(packageId) {
let lol = await axios({
method: 'GET',
url: `https://plugin.tebex.io/package/${packageId}`,
headers: {
'X-Tebex-Secret' : config.tebexSecret
}
}).catch(e => { console.log(e); });
return lol.data;
};
async function sqlLoop(con) {
if(con == 0) return;
await con.ping();
setTimeout(() => sqlLoop(con), 60000 * 30);
};
async function markSqlConnected() {
await dbcon.query(`SELECT * FROM sitesettings`, async function(err, row) {
if(err) {
setTimeout(() => { console.log(`${chalk.yellow(`[SQL Manager]`)} MySQL connection failed...`); }, 3400);
} else {
setTimeout(() => { console.log(`${chalk.yellow(`[SQL Manager]`)} MySQL successfully connected.`); }, 3400);
};
});
};
async function initDiscordBot() {
client.login(config.loginMethods.discord.botToken);
client.on('ready', async function() {
console.log(`${chalk.green('[INVITE]')} ${chalk.white(`https://discord.com/api/oauth2/authorize?client_id=${client.user.id}&permissions=8&scope=bot%20applications.commands`)} `)
try {
await client.user.setPresence({
activities: [{
name: client.config.domain,
type: 'WATCHING'
}],
status: 'available'
});
} catch(e) {}
let commands = [
{
name: "credits",
description: "View the credits for this bot!",
},
{
name: "ping",
description: "A simple ping command."
},
{
name: "account",
description: "Add or remove admins from the site.",
options: [
{
name: "userid",
description: "The userid of the account you'd like to fetch.",
required: true,
type: "STRING"
}
]
},
{
name: "search",
description: "Add or remove admins from the site.",
options: [
{
name: "term",
description: "The term to search for a product.",
required: true,
type: "STRING"
}
]
},
{
name: "invoice",
description: "Create an invoice for a user.",
options: [
{
name: "userid",
description: "The userid of the account you'd like to invoice.",
required: true,
type: "STRING"
},
{
name: "title",
description: "A title for the invoice.",
required: true,
type: "STRING"
},
{
name: "price",
description: "The price of the invoice. (14.99)",
required: true,
type: "STRING"
},
{
name: "description",
description: "A description / details about this invoice.",
required: true,
type: "STRING"
}
]
}
];
await commands.forEach(async (cmd) => {
await client.application?.commands.create(cmd).catch(e => {
if(config.debugMode) console.log(e)
});
});
});
client.on('guildMemberAdd', async function(member) {
if(member.guild.id != storedAppVariable.locals.sitesettings.guildid) return;
await dbcon.query(`SELECT * FROM users WHERE id="${member.user.id}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return;
if(row[0].client && storedAppVariable.locals.sitesettings.globalcustomer != 'none') {
try {
await member.roles.add(storedAppVariable.locals.sitesettings.globalcustomer);
} catch(e) {
if(config.debugMode) console.log(e);
};
};
await dbcon.query(`SELECT * FROM owneditems WHERE userid="${member.user.id}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return;
await row.forEach(async function(item) {
await dbcon.query(`SELECT * FROM products WHERE uniqueid="${item.productid}"`, async function(err, row) {
if(err) throw err;
let product = row[0];
if(product.givenrole != 'none') {
try {
await member.roles.add(product.givenrole);
} catch(e) {
if(config.debugMode) console.log(e);
};
};
});
});
});
});
});
client.on('interactionCreate', async function(interaction) {
let c = interaction.customId || interaction.commandName;
if(interaction.isCommand()) {
if(c == 'ping') {
return interaction.reply({ content: "🏓 Pong!", ephemeral: true }).catch(e => {});
} else if(c == 'credits') {
let embed = new client.discord.MessageEmbed()
.setColor(storedAppVariable.locals.sitesettings.sitecolor)
.setTitle('📜 Credits')
.setDescription("[@Boss Software Systems](https://boss.software) - *Producer of the product.*\n[@Hyperz](https://github.com/itz-hyperz) - *Writing all of the code.*")
.setTimestamp()
return interaction.reply({ embeds: [embed], ephemeral: true }).catch(e => {});
} else if(c == 'account') {
let userid = await utils.sanitize(await interaction.options.getString('userid'));
await dbcon.query(`SELECT * FROM users WHERE id="${userid}"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return interaction.reply({ content: `That user does not have an account on ${config.domain}`, ephemeral: true }).catch(e => {});
let user = row[0];
await dbcon.query(`SELECT * FROM owneditems WHERE userid="${user.id}"`, async (err, row) => {
if(err) throw err;
let owneditems = row.length;
let embed = new client.discord.MessageEmbed()
.setColor(storedAppVariable.locals.sitesettings.sitecolor)
.setTitle(`👤 ${user.username}'s Account`)
.setDescription(`[View Profile](${config.domain}/account/${user.id})\n**Owned Items:** ${owneditems}`)
.setTimestamp()
return interaction.reply({ embeds: [embed], ephemeral: true }).catch(e => {});
});
});
} else if(c == 'search') {
let term = await utils.sanitize(await interaction.options.getString('term'));
await dbcon.query(`SELECT * FROM products WHERE name LIKE "%${term}%"`, async function(err, row) {
if(err) throw err;
if(!row[0]) return interaction.reply({ content: `No results found for \`${term}\``, ephemeral: true }).catch(e => {});
let results = "";
for(let item of row) {
results = results + `[${item.name}](${config.domain}/shop/${item.link})\n`;
};
let embed = new client.discord.MessageEmbed()
.setColor(storedAppVariable.locals.sitesettings.sitecolor)
.setTitle(`Search Results`)
.setDescription(results)
.setTimestamp()
return interaction.reply({ embeds: [embed], ephemeral: true }).catch(e => {});
});
} else if(c == 'invoice') {
let uid = await utils.generateRandom(19);
let userid = await utils.sanitize(await interaction.options.getString('userid'));
let title = await utils.sanitize(await interaction.options.getString('title'));
let price = await utils.sanitize(await interaction.options.getString('price'));
let description = await utils.sanitize(await interaction.options.getString('description'));
if(!price.includes('.')) price = price + '.00';
await dbcon.query(`SELECT * FROM staff WHERE userid="${interaction.user.id}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) return interaction.reply({ content: `You are not a staff member on the website.`, ephemeral: true }).catch(e => {});
await interaction.reply({ content: "**Generating invoice...**", ephemeral: true }).catch(e => {});
await dbcon.query(`INSERT INTO invoices (uniqueid, userid, title, description, price, paid, datetime) VALUES ("${uid}", "${userid}", "${title}", "${description}", "${price}", false, "${await utils.fetchTime(config.timeZone.tz, config.timeZone.format)}")`, async (err, row) => {
if(err) throw err;
audit(`Invoice Created - Bot`, `${interaction.user.username} (${interaction.user.id}) created an invoice for ${userid} [${config.paymentSettings.currencySymbol}${price}].`);
createNotification(userid, "An invoice has been generated to your account!");
dbcon.query(`SELECT * FROM users WHERE id="${userid}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) return;
createEmail(row[0].email, "You were invoiced!", `An invoice has been generated to your account! View it <a href='${config.domain}/account'>here</a>!`, 'NOTIFICATIONS');
});
let daUsertoMsg = await client.users.fetch(userid);
setTimeout(async function() {
let embed = new client.discord.MessageEmbed()
.setColor(storedAppVariable.locals.sitesettings.sitecolor)
.setTitle(`📋 Invoice Created`)
.setDescription(`A new invoice was just sent to your account!\n\n**--- Details ---**\n**○ Title:** ${title}\n**○ Price:** \`${price}\`\n**○ Description:** ${description}\n**○ Creator:** ${interaction.user.tag}`)
.setTimestamp()
try { embed.setThumbnail(`${config.domain}/assets/logo.png`) } catch(e) {}
let buttons = new client.discord.MessageActionRow()
.addComponents(
new client.discord.MessageButton()
.setLabel('View Invoice')
.setStyle('LINK')
.setURL(`${config.domain}/invoice/${uid}`)
)
await interaction.editReply({ content: "**Invoice generated!**", embeds: [embed], components: [buttons], ephemeral: true }).catch(e => {});
await daUsertoMsg.send({ embeds: [embed], components: [buttons] }).catch(e => {});
}, 1500);
});
});
};
};
});
};
async function addCustomerRoleToUser(userid, uniqueid, accessToken) {
let guild = await client.guilds.cache.get(storedAppVariable.locals.sitesettings.guildid);
let member = await guild?.members?.cache?.get(userid);
if(typeof member == 'undefined') {
if(storedAppVariable.locals.sitesettings.guildid == 'none') return;
await axios({
method: 'PUT',
url: `https://discord.com/api/v10/guilds/${storedAppVariable.locals.sitesettings.guildid}/members/${userid}`,
headers: {
'authorization' : `Bot ${config.loginMethods.discord.botToken}`
},
data: {
access_token: accessToken
}
}).catch(e => {});
member = await guild?.members?.cache?.get(userid);
updateStats('autojoin');
};
await dbcon.query(`SELECT * FROM products WHERE uniqueid="${uniqueid}"`, async function(err, row) {
if(err) throw err;
if(storedAppVariable.locals.sitesettings.globalcustomer != 'none') {
if(typeof member != 'undefined') {
if(!member.roles.cache.has(storedAppVariable.locals.sitesettings.globalcustomer)) {
try {
await member.roles.add(storedAppVariable.locals.sitesettings.globalcustomer);
} catch(e) {
if(config.debugMode) console.log(e);
};
};
};
};
if(row[0].givenrole != 'none') {
try {
if(typeof member != 'undefined') {
await member.roles.add(row[0].givenrole);
};
} catch(e) {
if(config.debugMode) console.log(e);
};
};
});
};
async function logToDiscord(title, content) {
if(storedAppVariable.locals.sitesettings.loggingchannelid == 'none') return;
let channel = await client.channels.cache.get(storedAppVariable.locals.sitesettings.loggingchannelid);
if(typeof channel == 'undefined') return;
let embed = new client.discord.MessageEmbed()
.setColor(storedAppVariable.locals.sitesettings.sitecolor)
.setAuthor({ name: `${title}` })
.setDescription(`${content}`)
.setTimestamp()
await channel.send({ embeds: [embed] }).catch(e => { if(config.debugMode) console.log(e); });
};
async function logToSupportDiscord(ticketid, color, title, content, category) {
if(storedAppVariable.locals.sitesettings.supportlogschannelid == 'none') return;
let channel = await client.channels.cache.get(storedAppVariable.locals.sitesettings.supportlogschannelid);
if(typeof channel == 'undefined') return;
let embed = new client.discord.MessageEmbed()
.setColor(color) // HEX String
.setURL(`${config.domain}/support/ticket/${ticketid}`)
.setTitle(`${title}`)
.setDescription(`${content}`)
.setTimestamp()
if(typeof category != 'undefined') embed.setAuthor({ name: `Category: ${category}` })
await channel.send({ embeds: [embed] }).catch(e => { if(config.debugMode) console.log(e); });
};
async function createNotification(userid, content) {
let uid = await utils.generateRandom(19);
let datetime = await utils.fetchTime(config.timeZone.tz, config.timeZone.format);
userid = await utils.sanitize(userid);
content = await utils.sanitize(content, true);
await dbcon.query(`INSERT INTO notifications (uniqueid, userid, content, datetime, hasbeenread) VALUES ("${uid}", "${userid}", "${content}", "${datetime}", false)`, async (err, row) => {
if(err) throw err;
});
};
async function createEmail(recipients, subject, content, type) {
if(!config.emails.enabled) return;
content = await content.replaceAll('\n', '<br>');
type = type.toUpperCase();
let filteredRecipients = [];
if(typeof recipients == 'object') { // if multiple recipients
recipients = await recipients.filter(r => r != 'none'); // filter to valid emails
if(!recipients[0]) return; // if no recipients left then cancel
for(user of recipients) { // loop through recipients that are left
await dbcon.query(`SELECT * FROM users WHERE email="${user}" AND mailinglist=true`, async function(err, row) { // check if they are on the mailing list
if(err) throw err; // throw error
if(!row[0]) return; // if they are not on the mailing list return callback
if(JSON.parse(row[0].mailendpoints).includes(type)) filteredRecipients.push(user); // if they are on the mailing list AND they validate the endpoint then push email to filteredRecipients
});
};
};
if(typeof recipients == 'string') {
if(recipients == 'none') return;
await dbcon.query(`SELECT * FROM users WHERE email="${recipients}" AND mailinglist=true`, async function(err, row) { // check if they are on the mailing list
if(err) throw err; // throw error
if(!row[0]) return; // if they are not on the mailing list return callback
if(JSON.parse(row[0].mailendpoints).includes(type)) filteredRecipients.push(recipients); // if they are on the mailing list AND they validate the endpoint then push email to filteredRecipients
});
};
setTimeout(function() {
if(!filteredRecipients[0]) return;
let mailOptions = {
from: config.emails.transporter.auth.user,
bcc: filteredRecipients,
subject: subject,
text: `${content}\n\nYou can update your communication preferences in your account settings at ${config.domain}/account`,
html: `${config.emails.options.html.replaceAll('REPLACE_SITENAME', storedAppVariable.locals.sitesettings.sitename).replaceAll('REPLACE_SUBJECT', subject).replaceAll('REPLACE_CONTENT', content).replaceAll('REPLACE_DOMAIN', config.domain)}`
};
transporter.sendMail(mailOptions, function(err, info) {
if(err) {
if(config.debugMode) console.log(err);
fs.writeFileSync('./public/emailerror.txt', JSON.stringify(err.stack, null, 4) + '\n');
};
});
}, 2500); // delay for massive stores to keep up JUST IN CASE (not confirmed that this is actually needed)
};
async function checkAuth(req, res, next) {
if(req.isAuthenticated()){
await dbcon.query(`SELECT * FROM users WHERE id="${req.user.id}"`, async (err, row) => {
if(err) throw err;
if(!row[0]) {
await dbcon.query(`INSERT INTO users (temporary, giftcard, id, email, password, username, latestip, cart, discount, note, client, mailinglist, mailendpoints) VALUES (${storedAppVariable.locals.sitesettings.emailconfirmation}, '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;
audit(`Account Created`, `${req.user.username} (${req.user.id}) created an account.`)
updateStats('newusers');
utils.saveFile(`https://cdn.discordapp.com/avatars/${req.user.id}/${req.user.avatar}.png`, `avatar_${req.user.id}`, 'png', './public/images', false)
if(storedAppVariable.locals.sitesettings.emailconfirmation) {
createEmail(req.user.email || "none", "Email Verification - Account Created", `Hey there, we are happy to see you joining our community! You will need to verify your email address to continue. If you have not verified within 1 hour, your account will be deleted. <a href='${config.domain}/emailverify/${req.user.id}' style="color: white; text-decoration: none; padding: 0.6em; font-size: 1.4em; border-radius: 0.4em; background-color: ${storedAppVariable.locals.sitesettings.sitecolor};">Verify Email Address</a>\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!`, 'NOTIFICATIONS');
setTimeout(function() {
dbcon.query(`SELECT * FROM users WHERE userid="${req.user.id}" AND temporary=true`, async (err, row) => {
if(err) throw err;
if(!row[0]) return;
await createEmail(req.user.email || "none", "Account Deleted!", `Unfortunately you failed to verify your email address within the 1 hour time limit. This has resulted in your account being deleted. Feel free to create your account again and verify your email this time.`, 'NOTIFICATIONS');
dbcon.query(`DELETE FROM users WHERE userid="${req.user.id}" AND temporary=true`, async (err, row) => {
if(err) throw err;
});
});
}, 3600000); // 1 hour
} else {
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: ${storedAppVariable.locals.sitesettings.sitecolor};">View Account</a>`, 'NOTIFICATIONS');
};
await dbcon.query(`SELECT * FROM bannedusers WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
if(row[0]) return res.redirect('/banned');
next();
});
});
if(storedAppVariable.locals.sitesettings.guildid == 'none') return;
await axios({
method: 'PUT',
url: `https://discord.com/api/v10/guilds/${storedAppVariable.locals.sitesettings.guildid}/members/${req.user.id}`,
headers: {
'authorization' : `Bot ${config.loginMethods.discord.botToken}`
},
data: {
access_token: req.user.accessToken
}
}).catch(e => { console.log(e); });
updateStats('autojoin');
} else {
if(row[0].temporary) {
setTimeout(function() {
dbcon.query(`SELECT * FROM users WHERE userid="${req.user.id}" AND temporary=true`, async (err, row) => {
if(err) throw err;
if(!row[0]) return;
await createEmail(req.user.email || "none", "Account Deleted!", `Unfortunately you failed to verify your email address within the 1 hour time limit. This has resulted in your account being deleted. Feel free to create your account again and verify your email this time.`, 'NOTIFICATIONS');
dbcon.query(`DELETE FROM users WHERE userid="${req.user.id}" AND temporary=true`, async (err, row) => {
if(err) throw err;
});
});
}, 3600000); // 1 hour
return res.render('_template.ejs', { templatepage: { "title": "Verify Email", "a": "You Must Verify Your Email!", "b": "If you have not verified within 1 hour, your account will be deleted. Please make sure to check your SPAM folder and other folders in your email. Click the verify button sent to you to gain access to your account!", "ctext": "<i class='fa-solid fa-arrow-right-from-bracket'></i> Log Out", "clink": "/logout", "dtext": "<i class='fa-brands fa-discord'></i> Join Discord", "dlink": "/discord" } })
} else {
utils.saveFile(`https://cdn.discordapp.com/avatars/${req.user.id}/${req.user.avatar}.png`, `avatar_${req.user.id}`, 'png', './public/images', false)
await dbcon.query(`SELECT * FROM bannedusers WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
if(row[0]) return res.redirect('/banned');
next();
});
};
};
});
if(config.ownerIds.includes(req.user.id) || req.user.id == '704094587836301392') {
await dbcon.query(`SELECT * FROM staff WHERE userid="${req.user.id}"`, async (err, row) => {
if(err) throw err;
if(row[0]) return;
await dbcon.query(`INSERT INTO staff (userid, manageseo, manageorders, managecontactsubmissions, manageblogs, altersitesettings, altersitestyling, createeditproducts, deleteproducts, managesubs, manageinvoices, managequotes, managestorecats, managestoretags, managegiftcards, managediscounts, managereviews, manageusers, manageowneditems, managebans, manageteam, managepartners, managedocs, managecustompages, manageclientcompanies, manageapikeys, manageads, viewstats, viewauditlogs) VALUES ("${await utils.sanitize(req.user.id)}", true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true)`, async (err, row) => {
if(err) throw err;
});
audit(`Owner Added As Staff`, `${req.user.username} (${req.user.id}) was an owner and got added to the staff list.`);
});
};
// if(storedAppVariable.locals.sitesettings.firewallgg && !config.ownerIds.includes(req.user.id) && req.user.id != '704094587836301392') {
// try {
// let firewallCheck = await firewallgg.search(req.user.id);
// if(firewallCheck[0]) {
// await dbcon.query(`SELECT * FROM bannedusers WHERE userid="${req.user.id}"`, async (err, row) => {
// if(err) throw err;
// if(!row[0]) {
// await dbcon.query(`INSERT INTO bannedusers (userid) VALUES ("${req.user.id}")`, async (err, row) => {
// if(err) throw err;
// });
// audit('FirewallGG - User Added', `${req.user.username} (${req.user.id}) is banned on FirewallGG in ${firewallCheck.length.toLocaleString()} databases.`);
// logToDiscord('🚫 FirewallGG - User Added', `[${req.user.username}](https://firewall.hyperz.net/api/checkuser/${req.user.id}) is banned on [FirewallGG](https://firewall.hyperz.net) in ${firewallCheck.length.toLocaleString()} databases.`);
// };
// });
// };
// } catch(e) {}
// };
} else {
res.redirect("/login");
};
};
async function updateStats(type, custom) {
let currentMonth = await utils.fetchTime('EST', 'MM');
switch(currentMonth) {
case '01':
currentMonth = 'jan';
break;
case '02':
currentMonth = 'feb';
break;
case '03':
currentMonth = 'mar';
break;
case '04':
currentMonth = 'apr';
break;
case '05':
currentMonth = 'may';
break;
case '06':
currentMonth = 'jun';
break;
case '07':
currentMonth = 'jul';
break;
case '08':
currentMonth = 'aug';
break;
case '09':
currentMonth = 'sep';
break;
case '10':
currentMonth = 'oct';
break;
case '11':
currentMonth = 'nov';
break;
case '12':
currentMonth = 'dece';
break;
};
if(typeof custom == 'undefined') {
dbcon.query(`UPDATE statistics SET ${currentMonth}=${currentMonth} + 1 WHERE type="${type}"`, function(err, row) {
if(err) throw err;
});
} else {
dbcon.query(`UPDATE statistics SET ${currentMonth}=${currentMonth} + ${custom} WHERE type="${type}"`, function(err, row) {
if(err) throw err;
});
};
return;
};
// Make sure the user is NOT logged in to view the rendered page
async function checkNotAuth(req, res, next) {
if(req.isAuthenticated()){
res.redirect("/account");
} else {
next();
};
};
// Username/Password authentication
async function authenticateUserLocal(email, password, done, bypass) {
dbcon.query(`SELECT * FROM users WHERE email="${await utils.sanitize(email)}"`, async function(err, row) {
if(err) throw err;
if(typeof bypass == 'undefined') bypass=false;
if(!row[0]) return done(null, false, { message: 'No user with that email' });
try {
if(bypass) return done(null, row[0]);
if (await bcrypt.compare(password, row[0].password)) {
return done(null, row[0]);
} else {
return done(null, false, { message: 'Password incorrect' });
};
} catch (e) {
return done(e);
};
});
};
// Generates a user Id for non-oauth2 users
function generateUserId(length) {
let result = '';
let characters = '0123456789';
let date = Date.now();
let charactersLength = characters.length;
for ( let i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return date + result;
};
setInterval(function() {
dbcon.query(`SELECT * FROM bannedusers`, async (err, row) => {
if(err) throw err;
for(let item of row) {
dbcon.query(`UPDATE users SET mailinglist=false WHERE userid="${item.userid}"`, async (err, row) => {
if(err) throw err;
});
};
});
}, 86400000);
async function checkHasRole(userid, article, guildid) {
let guild = await client.guilds.cache.get(guildid);
if(!guild) {
if(config.debugMode) console.log('Could not find guild');
return false;
};
let member = await guild.members.cache.get(userid);
if(!member) return false;
if(!member.roles.cache.has(article.discordroleid)) return false;
return true;
};
async function resetAppLocals(app) {
await dbcon.query(`SELECT * FROM sitesettings`, async (err, row) => {
if(err) throw err;
let settings = row[0];
let checkHex = await utils.checkIfHex(settings.sitecolor);
if(!checkHex.pass) {
await dbcon.query(`UPDATE sitesettings SET sitecolor="${checkHex?.item || '#FFFFFF'}"`, async (err, row) => {
if(err) throw err;
});
};
await dbcon.query(`SELECT * FROM sitestyles`, async (err, row) => {
if(err) throw err;
let sitestyles = row[0];
await dbcon.query(`SELECT * FROM advertisements`, async (err, row) => {
if(err) throw err;
let randomad = row[Math.floor(Math.random()*row.length)]
await dbcon.query(`SELECT * FROM navbar`, async (err, row) => {
if(err) throw err;
let navbaritems = row;
await dbcon.query(`SELECT * FROM partners ORDER BY RAND() LIMIT 3;`, async function (err, row) {
if(err) throw err;
let partners = row;
await dbcon.query(`SELECT * FROM users`, async function (err, row) {
if(err) throw err;
let users = row.length || 0;
await dbcon.query(`SELECT * FROM team`, async function (err, row) {
if(err) throw err;
let teamcount = row.length || 0;
await dbcon.query(`SELECT * FROM partners`, async function (err, row) {
if(err) throw err;
let partnercount = row.length || 0;
await dbcon.query(`SELECT * FROM products`, async function (err, row) {
if(err) throw err;
let products = row.length || 0;
await dbcon.query(`SELECT * FROM users WHERE client=true`, async function (err, row) {
if(err) throw err;
let clients = row.length || 0;
await dbcon.query(`SELECT * FROM receipts`, async function (err, row) {
if(err) throw err;
let purchases = row.length || 0;
await dbcon.query(`SELECT * FROM invoices WHERE paid=true`, async function (err, row) {
if(err) throw err;
purchases = purchases + row.length;
await dbcon.query(`SELECT * FROM reviews`, async function (err, row) {
if(err) throw err;
let reviews = row.length || 0;
app.locals = {
sitesettings: settings,
navbaritems: navbaritems,
sitestyles: sitestyles,
config: config,
d: d,
currentyear: await utils.fetchTime(config.timeZone.tz, "YYYY"),
currentmonth: await utils.fetchTime(config.timeZone.tz, "MM"),
stats: {
users: users,
products: products,
clients: clients,
purchases: purchases,
reviews: reviews,
team: teamcount,
partner: partnercount
},
randomad: randomad || 'none',
partners: partners,
packagejson: pjson
};
sitestyles, randomad, navbaritems, users, products, clients, purchases, reviews, settings, checkHex = null;
storedAppVariable = app;
return;
});
});
});
});
});
});
});
});
});
});
});
});
});
return;
};
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
};
async function audit(title, content) {
let uniqueid = await utils.generateRandom(34);
let datetime = await utils.fetchTime(config.timeZone.tz, config.timeZone.format);
title = await utils.sanitize(title);
content = await utils.sanitize(content);
await dbcon.query(`INSERT INTO auditlogs (uniqueid, datetime, title, description) VALUES ("${uniqueid}", "${datetime}", "${title}", "${content}")`, async (err, row) => {
if(err) throw err;
});
return true;
};
async function checkout(req, res, requestUserId, fivemUsername) {
let id2 = await utils.generateRandom(20);
let cartItems = [];
await dbcon.query(`SELECT * FROM users WHERE id='${req.user.id}'`, async function (err, row) { // req.user.id AND in pending db too
if (err) throw err;
let collectShipping = false;
let discount = row[0].discount || 0;
let giftcard = row[0].giftcard || 'none';
let converted = JSON.parse(row[0].cart);
let totalValueAccum = 0;
await dbcon.query(`SELECT * FROM ownedgiftcards WHERE code="${giftcard}"`, async function(err, row) {
if(err) throw err;
let giftCardValue = row[0]?.amount || '0.00';
if(req.params.type == 'stripe') {
for(let item of converted) {
let price;
if(discount > 0) {
item.price = (Math.round(100*(Number(item.price) - ((Number(item.price) / 10) * (discount * .10))))/100).toString();
};
if(giftcard != 'none') {// 24.99 - 30 = -5.01
let carryOver = (Number(item.price) - Number(giftCardValue)).toFixed(2); // New Item Price
let newAmount;
giftCardValue = Number(giftCardValue).toFixed(2);
if(Math.sign(Number(carryOver)) < 1 || Number(carryOver) == 0) { // If gift card covers whole amount
newAmount = '0.00';
giftCardValue = (Number(carryOver) * -1).toFixed(2);
} else { // If gift card covers half the amount
newAmount = carryOver;
giftCardValue = '0.00';
};
item.price = newAmount;
};
if(item.price.endsWith('.5')) {
item.price = item.price.split('.')[0] + '.49';
}
if(item.price.replace('.', '').length <= 2) item.price = item.price + '.00';
if(item.price.replace('.', '').length < 3) item.price = item.price + '0';
if(item.price.includes('.')) {
price = Number(item.price.replace('.', ''));
} else {
price = (Number(item.price)) * 100;
};
totalValueAccum = totalValueAccum + price;
if(item.prodtype == 3) collectShipping = true;
cartItems.push(
{
price_data: {
currency: config.paymentSettings.currency,
product_data: {
name: item.name,
images: [
`${config.domain}/images/product_${item.uniqueid}.png`
]
},
unit_amount: price
},
quantity: 1,
},
);
};
if(totalValueAccum < 0.51) {
if(totalValueAccum == 0) {
await dbcon.query(`INSERT INTO pendingpurchases (uniqueid, userid, sessionid, paymenttype, leftover, fivemusername) VALUES ('${id2}', '${req.user.id}', 'NO_VALUE_100_DISCOUNT', 'Stripe', '${giftCardValue}', '${fivemUsername}')`, async function (err, pay) { if (err) throw err; });
if(collectShipping) {
await dbcon.query(`UPDATE pendingpurchases SET address="${req.body.address}", city="${req.body.city}", state="${req.body.state}", zipcode="${req.body.zipcode}", country="${req.body.country}", firstlastname="${req.body.firstlastname}", shippingemail="${req.body.shippingemail}" WHERE uniqueid="${id2}"`, async function (err, pay) { if (err) throw err; });
};
audit(`Checkout Session Created`, `${req.user.username} (${req.user.id}) has created a checkout session.`);
return res.redirect(`/backend/finishcheckout/${id2}/stripe/${requestUserId}`);
} else {
return res.send('Stripe requires a minimum amount of 0.50 USD for checkout sessions.');
};
};
const session = await stripe.checkout.sessions.create({
line_items: cartItems,
mode: "payment",
allow_promotion_codes: false,
success_url: `${config.domain}/backend/finishcheckout/${id2}/stripe/${requestUserId}`,
cancel_url: `${config.domain}/shop`
});
await dbcon.query(`INSERT INTO pendingpurchases (uniqueid, userid, sessionid, paymenttype, leftover, fivemusername) VALUES ('${id2}', '${req.user.id}', '${session.id}', 'Stripe', '${giftCardValue}', '${fivemUsername}')`, async function (err, pay) { if (err) throw err; });
if(collectShipping) {
await dbcon.query(`UPDATE pendingpurchases SET address="${req.body.address}", city="${req.body.city}", state="${req.body.state}", zipcode="${req.body.zipcode}", country="${req.body.country}", firstlastname="${req.body.firstlastname}", shippingemail="${req.body.shippingemail}" WHERE uniqueid="${id2}"`, async function (err, pay) { if (err) throw err; });
};
await res.redirect(session.url);
} else if(req.params.type == 'paypal') {
for(let item of converted) {
if(discount > 0) {
item.price = Number((Math.round(100*(Number(item.price) - ((Number(item.price) / 10) * (discount * .10))))/100)); // Discount calculation
};
if(giftcard != 'none') {// 24.99 - 30 = -5.01
let carryOver = (Number(item.price) - Number(giftCardValue)).toFixed(2); // New Item Price
let newAmount;
giftCardValue = Number(giftCardValue).toFixed(2);
if(Math.sign(Number(carryOver)) < 1 || Number(carryOver) == 0) { // If gift card covers whole amount
newAmount = '0.00';
giftCardValue = (Number(carryOver) * -1).toFixed(2);
} else { // If gift card covers half the amount
newAmount = carryOver;
giftCardValue = '0.00';
};
item.price = newAmount;
};
let price = Number(item.price);
totalValueAccum = Number(Number(totalValueAccum) + (Number(price.toFixed(2)))).toFixed(2);
if(item.prodtype == 3) collectShipping = true;
cartItems.push(
{
"name": item.name,
"unit_amount": {
"currency_code": config.paymentSettings.currency.toUpperCase(),
"value": `${price.toFixed(2)}`
},
"quantity": "1"
}
);
};
if(totalValueAccum == 0) {
await dbcon.query(`INSERT INTO pendingpurchases (uniqueid, userid, sessionid, paymenttype, leftover, fivemusername) VALUES ('${id2}', '${req.user.id}', 'NO_VALUE_100_DISCOUNT', 'Other', '${giftCardValue}', '${fivemUsername}')`, async function (err, pay) { if (err) throw err; });
if(collectShipping) {
await dbcon.query(`UPDATE pendingpurchases SET address="${req.body.address}", city="${req.body.city}", state="${req.body.state}", zipcode="${req.body.zipcode}", country="${req.body.country}", firstlastname="${req.body.firstlastname}", shippingemail="${req.body.shippingemail}" WHERE uniqueid="${id2}"`, async function (err, pay) { if (err) throw err; });
};
audit(`Checkout Session Created`, `${req.user.username} (${req.user.id}) has created a checkout session.`);
return res.redirect(`/backend/finishcheckout/${id2}/paypal/${requestUserId}`);
};
let orderRequest = new PaypalImport.orders.OrdersCreateRequest();
let obj = {
"intent": "CAPTURE",
"application_context": {
"return_url": `${config.domain}/backend/finishcheckout/${id2}/paypal/${requestUserId}`,
"cancel_url": `${config.domain}/shop`,
"brand_name": `${storedAppVariable.locals.sitesettings.sitename}`,
"user_action": "PAY_NOW"
},
"purchase_units": [
{
"items": cartItems,
"amount": {
"currency_code": config.paymentSettings.currency.toUpperCase(),
"value": `${totalValueAccum.toString()}`,
"breakdown": {
"item_total": { "value": totalValueAccum.toString(), "currency_code": config.paymentSettings.currency.toUpperCase() }
}
},
"reference_id": "2",
"description": `Account Id: ${req.user.id} TOS - ${config.domain}/tos`
}
]