forked from galenmaly/lighterpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·628 lines (540 loc) · 20.8 KB
/
app.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
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
var express = require('express');
var app = express();
app.enable('trust proxy');
var oneDay = 86400000;
var rootPath = __dirname + "/";
var fs = require("fs");
eval(fs.readFileSync(rootPath+'config.js')+'');
var collections = ["users", "libraries"];
var mongojs = require('mongojs');
var db = mongojs(databaseUrl, collections);
var connect = require('connect');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compression = require('compression');
var request = require("request");
var Mustache = require("mustache");
var extend = require('node.extend');
var nodemailer = require("nodemailer");
var sendmailTransport = require('nodemailer-sendmail-transport');
var transport = nodemailer.createTransport(sendmailTransport({}));
var formidable = require('formidable');
//I'm pretty sure there's a better way to do this:
eval(fs.readFileSync(rootPath+'public/sha3.js')+'');
eval(fs.readFileSync(rootPath+'public/pies.js')+'');
eval(fs.readFileSync(rootPath+'public/dataTypes.js')+'');
app.use(express.static(__dirname + '/public', { maxAge: oneDay }));
app.use(cookieParser());
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({
extended: true,
limit: '50mb'
}));
var templates = {},
indexTemplate,
embedJTemplate,
embedTemplate,
shareTemplate;
app.get('/', function(req, res) {
awesomeLog(req);
res.send(indexTemplate);
});
app.get('/r/:id', function(req, res) {
var id = req.params.id
awesomeLog(req);
if (!id) {
res.status(400).send("No list specified!");
return;
}
db.users.find({"library.lists.externalId": id}, function(err, users) {
if (err) {
res.status(500).send("An error occurred.");
return;
}
if (!users.length) {
res.status(400).send("Invalid list specified.");
return;
}
var library = new Library();
var list;
if (!users[0] || typeof(users[0].library) == "undefined") {
awesomeLog(req, "Undefined users[0].");
res.status(500).send("Unknown error.");
}
library.load(users[0].library);
for (var i in library.lists) {
if (library.lists[i].externalId && library.lists[i].externalId == id) {
library.defaultListId = library.lists[i].id;
list = library.lists[i];
break;
}
}
var chartData = escape(JSON.stringify(list.renderChart("total", false)));
var renderedCategories = library.render({itemTemplate: templates.t_itemShare, categoryTemplate: templates.t_categoryShare, showImages: library.showImages, unitSelectTemplate: templates.t_unitSelect});
var renderedTotals = library.renderTotals(templates.t_totals, templates.t_unitSelect, library.totalUnit);
var model = {listName: list.name, chartData: chartData, renderedCategories: renderedCategories, renderedTotals: renderedTotals, showImages: library.showImages};
model = extend(model, templates);
res.send(Mustache.render(shareTemplate, model));
});
});
app.get("/e/:id", function(req, res) {
var id = req.params.id
awesomeLog(req);
if (!id) {
res.statu(400).send("No list specified!");
return;
}
db.users.find({"library.lists.externalId": id}, function(err, users) {
if (err) {
res.status(500).send("An error occurred.");
return;
}
if (!users.length) {
res.status(400).send("Invalid list specified.");
return;
}
var library = new Library();
var list;
if (!users[0] || typeof(users[0].library) == "undefined") {
awesomeLog(req, "Undefined users[0].");
res.status(500).send("Unknown error.");
}
library.load(users[0].library);
for (var i in library.lists) {
if (library.lists[i].externalId && library.lists[i].externalId == id) {
library.defaultListId = library.lists[i].id;
list = library.lists[i];
break;
}
}
var chartData = escape(JSON.stringify(list.renderChart("total", false)));
var renderedCategories = library.render({itemTemplate: templates.t_itemShare, categoryTemplate: templates.t_categoryShare, showImages: library.showImages, unitSelectTemplate: templates.t_unitSelect});
var renderedTotals = library.renderTotals(templates.t_totals, templates.t_unitSelect);
var model = {externalId: id, listName: list.name, chartData: chartData, renderedCategories: renderedCategories, renderedTotals: renderedTotals};
model = extend(model, templates);
model.renderedTemplate = escape(Mustache.render(embedTemplate, model));
res.send(Mustache.render(embedJTemplate, model));
});
});
app.get("/csv/:id", function(req, res) {
var id = req.params.id
awesomeLog(req);
if (!id) {
res.status(400).send("No list specified!");
return;
}
db.users.find({"library.lists.externalId": id}, function(err, users) {
if (err) {
res.status(500).send("An error occurred.");
return;
}
if (!users.length) {
res.status(400).send("Invalid list specified.");
return;
}
var library = new Library();
var list;
if (!users[0] || typeof(users[0].library) == "undefined") {
awesomeLog(req, "Undefined users[0].");
res.status(500).send("Unknown error.");
}
library.load(users[0].library);
for (var i in library.lists) {
if (library.lists[i].externalId && library.lists[i].externalId == id) {
library.defaultListId = library.lists[i].id;
list = library.lists[i];
break;
}
}
var fullUnits = {oz: "ounce", lb: "pound", g: "gram", kg: "kilogram"};
var out = "Item Name,Category,desc,qty,weight,unit\n";
for (var i in list.categoryIds) {
var category = library.getCategoryById(list.categoryIds[i]);
for (var j in category.itemIds) {
var categoryItem = category.itemIds[j];
var item = library.getItemById(categoryItem.itemId);
var temp = [item.name, category.name, item.description, categoryItem.qty, ""+MgToWeight(item.weight, item.authorUnit), fullUnits[item.authorUnit]];
for (var k in temp) {
var field = temp[k];
if (k > 0) out += ",";
if (typeof(field) == "string") {
if (field.indexOf(",") > -1) out += "\"" + field.replace(/\"/g,"\"\"") + "\"";
else out += field;
} else out += field;
}
out += "\n";
}
}
var filename = list.name;
if (!filename) filename = id;
filename = filename.replace(/[^a-z0-9\-]/gi, '_');
res.setHeader("Content-Type", "text/csv");
res.setHeader("Content-Disposition", "attachment;filename="+filename+".csv")
res.send(out);
});
});
app.post("/register", function(req, res) {
var username = req.body.username;
var password = req.body.password;
var email = req.body.email;
if (!username || username.length < 1 || username.length > 24) {
res.status(400).send("Invalid username.");
awesomeLog(req, "invalid username");
return;
}
if (!password) {
res.status(400).send("Invalid password.");
awesomeLog(req, "invalid Password");
return;
}
if (!email) {
res.status(400).send("Invalid email.");
awesomeLog(req, "invalid Email");
return;
}
awesomeLog(req, username);
db.users.find({username: username}, function(err, users) {
if( err || users.length) {
res.status(400).send("User Exists.");
return;
}
require('crypto').randomBytes(48, function(ex, buf) {
var token = buf.toString('hex');
var newUser = {
username: username,
password: password,
email: email,
token: token,
library: JSON.parse(req.body.library)
}
db.users.save(newUser);
var out = {username: username, library: JSON.stringify(newUser.library)};
res.cookie("lp", token, { path: "/", maxAge: 365*24*60*1000 });
res.send(out);
});
});
});
/*app.get("/fixdb", function(req, res) {
db.users.find({}, function(err, users) {
if (!users.length) {
res.send(500, "fff");
return;
}
for (var i in users) {
var user = users[i];
user.library = JSON.parse(user.library);
db.users.save(user);
}
res.send("done");
});
});*/
/*
app.get("/fixdb", function(req, res) {
db.users.find({username: "galen"}, function(err, users) {
if (!users.length) {
res.send(500, "fff");
return;
}
for (var i in users) {
var user = users[i];
user.password = "";
db.users.save(user);
}
res.send("done");
});
});*/
app.post("/signin", function(req, res) {
authenticateUser(req, res, returnLibrary);
});
function returnLibrary(req, res, user) {
res.send({username: user.username, library: JSON.stringify(user.library)});
awesomeLog(req, user.username);
}
app.post("/saveLibrary", function(req, res) {
authenticateUser(req, res, saveLibrary);
});
function saveLibrary(req, res, user) {
try {
user.library = JSON.parse(req.body.data);
db.users.save(user);
res.send("success");
awesomeLog(req, user.username);
} catch(e) {
awesomeLog(req, user.username + " - " + e);
}
}
app.post("/externalId", function(req, res) {
authenticateUser(req, res, externalId);
});
app.post("/forgotPassword", function(req, res) {
awesomeLog(req);
var username = req.body.username;
if (!username || username.length < 1 || username.length > 24) {
res.status(400).send("Invalid username.");
awesomeLog(req, "Bad forgot password:" + username);
return;
}
db.users.find({username: username}, function(err, users) {
if( err ) {
res.status(500).send(":(");
awesomeLog(req, "Forgot password lookup error for:" + username)
return;
} else if ( !users.length ) {
res.status(400).send("error.");
awesomeLog(req, "Forgot password for unknown user:" + username)
return;
}
var user = users[0];
require('crypto').randomBytes(12, function(ex, buf) {
var newPassword = buf.toString('hex');
var hash = CryptoJS.SHA3(newPassword+username);
hash = hash.toString(CryptoJS.enc.Base64);
user.password = hash;
var email = user.email;
var message = "Hello " + username + ",\n Apparently you forgot your password. Here's your new one: \n\n Username: " + username + "\n Password: " + newPassword + "\n\n If you continue to have problems, please reply to this email with details.\n\n Thanks!";
var mailOptions = {
from: "LighterPack <[email protected]>",
to: email,
subject: "Your new LighterPack password",
text: message
}
awesomeLog(req, "Attempting to send new password to:" + email);
transport.sendMail(mailOptions, function(error, response){
if (error) {
awesomeLog(req, error);
} else {
db.users.save(user);
var out = {username: username};
res.send(out);
awesomeLog(req, "Message sent: " + response.message);
awesomeLog(req, "password changed for user:" + username);
}
});
});
});
});
app.post("/forgotUsername", function(req, res) {
awesomeLog(req);
var email = req.body.email;
if (!email || email.length < 1) {
res.status(400).send("Invalid email.");
awesomeLog(req, "Bad forgot username:" + email);
return;
}
db.users.find({email: email}, function(err, users) {
if( err ) {
res.status(500).send(":(");
awesomeLog(req, "Forgot email lookup error for:" + email)
return;
} else if ( !users.length ) {
res.status(400).send("error.");
awesomeLog(req, "Forgot email for unknown user:" + email)
return;
}
var user = users[0];
var username = user.username;
var message = "Hello " + username + ",\n Apparently you forgot your username. Here It is: \n\n Username: " + username + "\n\n If you continue to have problems, please reply to this email with details.\n\n Thanks!";
var mailOptions = {
from: "LighterPack <[email protected]>",
to: email,
subject: "Your LighterPack username",
text: message
}
awesomeLog(req, "Attempting to send username to:" + email);
transport.sendMail(mailOptions, function(error, response){
if (error) {
awesomeLog(req, error);
} else {
var out = {email: email};
res.send(out);
awesomeLog(req, "Message sent: " + response.message);
awesomeLog(req, "sent username message for user:" + username);
}
});
});
});
app.post("/account", function(req, res) {
authenticateUser(req, res, account);
});
function account(req, res, user) {
awesomeLog(req, user.name);
if (req.body.newPassword) {
user.password = req.body.newPassword;
awesomeLog(req, "Changing PW - " + user.username);
}
if (req.body.newEmail) {
user.email = req.body.newEmail;
awesomeLog(req, "Changing Email - " + user.username);
}
db.users.save(user);
res.send("success");
return;
};
function externalId(req, res, user) {
var filePath = rootPath+"extIds.txt";
fs.readFile(filePath, function(err, data) { // read file to memory
if (!err) {
data = data.toString(); // stringify buffer
var position = data.indexOf('\n'); // find position of new line element
if (position != -1) { // if new line element found
var myId = data.substr(0, position);
data = data.substr(position + 1); // subtract string based on first line length
fs.writeFile(filePath, data, function(err) { // write file
if (err) { // if error, report
awesomeLog(req, err);
}
});
res.send(myId);
awesomeLog(req, user.username + " - " + myId);
if (typeof user.externalIds == "undefined") user.externalIds = [myId];
else user.externalIds.push(myId);
db.users.save(user);
} else {
awesomeLog(req, 'External ID File: no lines found!!!111oneoneone');
}
} else {
awesomeLog(req, "ERROR OPENING EXTERNAL ID FILE");
awesomeLog(req, err);
}
});
}
app.post("/imageUpload", function(req, res) {
//authenticateUser(req, res, imageUpload);
awesomeLog(req);
imageUpload(req, res, {});
});
function imageUpload(req, res, user) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
if (err) {
awesomelog(req, "form parse error");
res.status(500).send("an error occurred");
return;
}
var path = files.image.path;
var imgurRequest = request.defaults({json: true});
fs.readFile(path, function(e, img_data) {
var temp = { uri: "https://api.imgur.com/3/image", headers: {"Authorization": "Client-ID " + imgurClientID}};
temp.body = img_data.toString("base64");
imgurRequest.post(temp, function(e, r, body) {
if (e) {
awesomeLog(req, "imgur post fail!");
awesomeLog(req, e);
awesomeLog(req, body);
res.send("upload failed :(");
} else if (!body) {
awesomeLog(req, "imgur post fail!!");
awesomeLog(req, e);
res.send("upload failed :((");
} else if (r.statusCode !== 200 || body.error) {
awesomeLog(req, "imgur post fail!!!");
awesomeLog(req, e);
awesomeLog(req, body);
res.send("upload failed :(((");
} else {
res.send(body);
}
});
});
});
}
function authenticateUser(req, res, callback) {
if (!req.cookies.lp && (!req.body.username || !req.body.password)) {
res.status(400).send("Please Authenticate");
return;
}
if (req.body.username) {
db.users.find({username: req.body.username, password: req.body.password}, function(err, users) {
if (err) {
res.status(500).send(":(");
awesomeLog(req, "Error on authenticateUser for:" + req.body.username + ", " + req.body.password )
return;
} else if (!users || !users.length) {
res.status(400).send("Invalid username and/or password.");
awesomeLog(req, "Bad password for: "+req.body.username);
return;
}
var user = users[0]
require("crypto").randomBytes(48, function(ex, buf) {
var token = buf.toString("hex");
user.token = token;
db.users.save(user);
res.cookie("lp", token, { path: "/", maxAge: 365*24*60*1000 });
callback(req, res, user);
});
});
} else {
db.users.find({token: req.cookies.lp}, function(err, users) {
if (err) {
res.status(500).send(":(");
awesomeLog(req, "Error on authenticateUser else for:" + req.body.username + ", " + req.body.password )
return;
} else if (!users || !users.length) {
awesomeLog(req, "bad cookie!");
res.status(400).send("Please log in again.");
return;
}
callback(req, res, users[0]);
});
}
}
function awesomeLog(req, data) {
if (!req) {
console.log("awesome log but no req? why!?");
return;
}
if (!data) data = "";
var d = new Date();
var time = d.toISOString();
var ua = req.get("user-agent");
console.log(time + " - " + req.ip + " - " + req.path + " - " + ua + " - " + data);
}
function init() {
fs.readdir(rootPath+"templates", function(err, files) {
files.filter(function(file) { return (file.substr(0,2) == "t_" && file.substr(-9) == '.mustache'); }).forEach(function(file) {
var fileShort = file.substr(0, file.length-9);
var data = fs.readFileSync(rootPath+"templates/"+file);
templates[fileShort] = data.toString();
});
fs.readFile(rootPath+"templates/index.mustache", function(err, data) {
if (!err) {
indexTemplate = data.toString();
indexTemplate = Mustache.render(indexTemplate, templates);
} else {
console.log("ERROR reading index.mustache");
}
});
fs.readFile(rootPath+"templates/share.mustache", function(err, data) {
if (!err) {
shareTemplate = data.toString();
shareTemplate = shareTemplate.replace(/\r?\n|\r/g, "");
} else {
console.log("ERROR reading share.mustache");
}
});
fs.readFile(rootPath+"templates/embed.mustache", function(err, data) {
if (!err) {
embedTemplate = data.toString();
embedTemplate = embedTemplate.replace(/\r?\n|\r/g, "");
} else {
console.log("ERROR reading embed.mustache");
}
});
fs.readFile(rootPath+"templates/embed.jmustache", function(err, data) {
if (!err) {
embedJTemplate = data.toString();
} else {
console.log("ERROR reading embed.jmustache");
}
});
//fs.writeFile(filePath, data, function(err) {
console.log("init complete.")
});
}
init();
app.listen(3000);
var d = new Date();
var time = d.toString().substr(0,24);
console.log("-------");
console.log(time);
console.log('Listening on port 3000');