forked from dangrossman/Bookmarkly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
556 lines (434 loc) · 19.9 KB
/
server.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
/* ==============================================================
Include required packages
=============================================================== */
var express = require('express'),
formidable = require('formidable'),
fs = require('fs'),
crypto = require('crypto'),
Client = require('mysql').Client,
parser = require('uglify-js').parser,
uglifyer = require('uglify-js').uglify;
var RedisStore = require('connect-redis')(express);
/* ==============================================================
Configuration
=============================================================== */
//used for session and password hashes
var salt = '20sdkfjk23';
var client = new Client();
client.host = 'hostname';
client.user = 'username';
client.password = 'password';
client.database = 'bookmarks';
var app = express.createServer();
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: salt, store: new RedisStore, cookie: { maxAge: 3600000 * 24 * 30 } }));
app.use(express.methodOverride());
app.use(express.logger({ format: ':method :url' }));
delete express.bodyParser.parse['multipart/form-data'];
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/js', express.static(__dirname + '/public/js'));
app.use('/images', express.static(__dirname + '/public/images'))
app.use(express.favicon(__dirname + '/public/favicon.ico'));
/* ==============================================================
Bundle + minify scripts & templates before starting server
=============================================================== */
function bundle() {
var scripts = [
'jquery.min',
'json2',
'underscore-min',
'handlebars.min',
'backbone-min',
'jquery.masonry.min',
'jquery.tagsinput.min',
'bootstrap-modal',
'jquery-ui.min',
'models/Bookmark',
'models/BookmarksCollection',
'models/Tag',
'models/TagsCollection',
'views/PublicView',
'views/AccountView',
'views/EditView',
'views/BookmarkView',
'views/BookmarksView',
'views/TagView',
'views/TagsView',
'views/AppView',
'routers/BookmarklyRouter',
'App'
];
var templates = ['account', 'bookmark', 'edit', 'footer', 'header', 'index', 'pub', 'tag', 'bookmarks'];
var bundle = '';
scripts.forEach(function(file) {
bundle += "\n/**\n* " + file + ".js\n*/\n\n" + fs.readFileSync(__dirname + '/public/js/' + file + '.js') + "\n\n";
});
var ast = parser.parse(bundle);
ast = uglifyer.ast_mangle(ast);
ast = uglifyer.ast_squeeze(ast);
bundle = uglifyer.gen_code(ast)
fs.writeFileSync(__dirname + '/public/js/bundle.js', bundle, 'utf8');
bundle = "Templates = {};\n";
templates.forEach(function(file) {
var html = fs.readFileSync(__dirname + '/public/templates/' + file + '.html', 'utf8');
html = html.replace(/(\r\n|\n|\r)/gm, ' ').replace(/\s+/gm, ' ').replace(/'/gm, "\\'");
bundle += "Templates." + file + " = '" + html + "';\n";
});
fs.writeFileSync(__dirname + '/public/js/templates.js', bundle, 'utf8');
}
/* ==============================================================
Launch the server
=============================================================== */
bundle();
app.listen(3000);
/* ==============================================================
Serve the site skeleton HTML to start the app
=============================================================== */
app.get('*', function(req, res, next) {
//Regenerates the JS/template file
if (req.url.indexOf('/bundle') == 0) bundle();
//Don't process requests for API endpoints
if (req.url.indexOf('/json') == 0 ) return next();
var init = "$(document).ready(function() { App.initialize(); });";
if (typeof req.session.user != 'undefined') {
init = "$(document).ready(function() { App.user = " + JSON.stringify(req.session.user) + "; App.initialize(); });";
}
fs.readFile(__dirname + '/public/templates/index.html', 'utf8', function(error, content) {
if (error) console.log(error);
content = content.replace("{{init}}", init);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
});
});
/* ==============================================================
API endpoints for the front-end AJAX requests
=============================================================== */
//Register a new user
app.post('/json/register', function(req, res) {
var username = req.body.username;
var password = md5(req.body.username + req.body.password + salt);
var email = req.body.email;
var query = "INSERT INTO users (username, email, password, created_at, last_login) VALUES (?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)";
client.query(query, [username, email, password], function(err, info) {
if (err) {
console.log(err);
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.write(JSON.stringify({ error: 'There is already an account with that e-mail or username' }), 'utf8');
res.end('\n');
} else {
user = { id: info.insertId, username: username, email: email };
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.write(JSON.stringify(user), 'utf8');
req.session.user_id = user.id;
req.session.user = user;
res.end('\n');
}
});
});
//Log in an existing user, starting a session
app.post('/json/login', function(req, res) {
var username = req.body.username;
var password = md5(req.body.username + req.body.password + salt);
var query = "SELECT id, username, email FROM users WHERE username = ? AND password = ?";
client.query(query, [username, password], function(err, results, fields) {
if (err) console.log(err);
if (results && results.length == 1) {
req.session.user_id = results[0].id;
req.session.user = results[0];
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.write(JSON.stringify(results[0]), 'utf8');
res.end('\n');
client.query('UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id = ?', [req.session.user_id]);
} else {
req.session.destroy();
res.writeHead(401, { 'Content-Type': 'text/html' });
res.end();
}
});
});
//Log out the current user
app.post('/json/logout', function(req, res) {
req.session.destroy();
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end();
});
//Update a user's profile
app.put('/json/user', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var query = "UPDATE users SET username = ?, password = md5(concat(?, ?, ?)), email = ? WHERE id = ?";
client.query(query, [req.body.username, req.body.username, req.body.password, salt, req.body.email, req.session.user_id], function(err) {
if (err) console.log(err);
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-type': 'application/json' });
res.write(JSON.stringify({ id: req.session.user_id, username: req.body.username, email: req.body.email }), 'utf-8');
res.end('\n');
});
});
//Retrieve a user's tags
app.get('/json/tag', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var tags = Array();
var query = "SELECT tag, COUNT(*) AS `count` FROM tags INNER JOIN bookmarks ON bookmarks.id = tags.bookmark_id WHERE user_id = ? GROUP BY tag ORDER BY COUNT(*) DESC";
client.query(query, [req.session.user_id], function(err, results, fields) {
if (err) console.log(err);
for (var i = 0; i < results.length; i++) {
tags.push(results[i]);
}
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-type': 'application/json' });
res.write(JSON.stringify(tags), 'utf-8');
res.end('\n');
});
});
//Autocomplete for tagging, returns tags matching input
app.get('/json/autocomplete', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var tags = Array();
client.query("SELECT DISTINCT tag FROM tags INNER JOIN bookmarks ON bookmarks.id = tags.bookmark_id WHERE user_id = ? AND tag LIKE ?", [req.session.user_id, req.query['term'] + '%'], function(err, results, fields) {
if (err) console.log(err);
for (var i = 0; i < results.length; i++) {
tags.push(results[i].tag);
}
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-type': 'application/json' });
res.write(JSON.stringify(tags), 'utf-8');
res.end('\n');
});
console.log('autocomplete # ' + req.url + ' # ' + req.query['term']);
});
//Return a user's bookmarks
app.get('/json/bookmark', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var bookmarks = Array();
var params = [];
var query = '';
if (req.query['tag']) {
params = [req.session.user_id, req.query['tag']];
query = "SELECT id, url, title, description, UNIX_TIMESTAMP(created_at) AS `timestamp`, GROUP_CONCAT(DISTINCT tag ORDER BY tag ASC SEPARATOR ',') AS tags FROM bookmarks LEFT OUTER JOIN tags ON bookmarks.id = tags.bookmark_id WHERE user_id = ? AND id IN (SELECT bookmark_id FROM tags WHERE tag = ?) GROUP BY id ORDER BY created_at DESC";
} else if (req.query['search']) {
params = [req.session.user_id, '%' + req.query['search'] + '%', '%' + req.query['search'] + '%', '%' + req.query['search'] + '%'];
query = "SELECT id, url, title, description, UNIX_TIMESTAMP(created_at) AS `timestamp`, GROUP_CONCAT(DISTINCT tag ORDER BY tag ASC SEPARATOR ',') AS tags FROM bookmarks LEFT OUTER JOIN tags ON bookmarks.id = tags.bookmark_id WHERE user_id = ? AND ((title LIKE ? OR description LIKE ?) OR id IN (SELECT bookmark_id FROM tags WHERE tag LIKE ?))GROUP BY id ORDER BY created_at DESC";
} else {
params = [req.session.user_id];
var offset = 0;
if (typeof req.query['offset'] != 'undefined')
offset = req.query['offset'];
query = "SELECT id, url, title, description, UNIX_TIMESTAMP(created_at) AS `timestamp`, GROUP_CONCAT(DISTINCT tag ORDER BY tag ASC SEPARATOR ',') AS tags FROM bookmarks LEFT OUTER JOIN tags ON bookmarks.id = tags.bookmark_id WHERE user_id = ? GROUP BY id ORDER BY created_at DESC LIMIT " + offset + ",50";
}
client.query(query, params, function(err, results, fields) {
if (err) console.log(err);
for (var i = 0; i < results.length; i++) {
if (results[i].tags && results[i].tags.indexOf(',') !== -1) {
results[i].tags = results[i].tags.split(',');
} else if (results[i].tags && results[i].tags != '') {
var tags = Array();
tags.push(results[i].tags);
results[i].tags = tags;
} else {
results[i].tags = Array();
}
bookmarks.push(results[i]);
}
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-type': 'application/json' });
res.write(JSON.stringify(bookmarks), 'utf-8');
res.end('\n');
}
);
});
//Update a bookmark
app.put('/json/bookmark/:id', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var bookmark = req.body;
var params = [bookmark.url, bookmark.title, bookmark.description, bookmark.id];
client.query('UPDATE bookmarks SET url = ?, title = ?, description = ? WHERE id = ?', params, function(err) {
if (err) console.log(err);
});
client.query('DELETE FROM tags WHERE bookmark_id = ?', [bookmark.id], function(err) {
if (err) console.log(err);
});
for (var i = 0; i < bookmark.tags.length; i++) {
client.query('INSERT INTO tags (bookmark_id, tag) VALUES (?, ?)', [bookmark.id, bookmark.tags[i]], function(err) {
if (err) console.log(err);
});
}
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-type': 'application/json' });
res.write(JSON.stringify(bookmark), 'utf-8');
res.end('\n');
});
//Create a new bookmark
app.post('/json/bookmark/:id?', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var bookmark = req.body;
var params = [req.session.user_id, bookmark.url, bookmark.title, bookmark.description];
client.query('INSERT INTO bookmarks (user_id, url, title, description, created_at) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)', params, function(err, info) {
if (err) console.log(err);
bookmark.id = info.insertId;
for (var i = 0; i < bookmark.tags.length; i++) {
client.query('INSERT INTO tags (bookmark_id, tag) VALUES (?, ?)', [info.insertId, bookmark.tags[i]], function(err) {
if (err) console.log(err);
});
}
res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
res.writeHead(200, { 'Content-type': 'application/json' });
res.write(JSON.stringify(bookmark), 'utf-8');
res.end('\n');
});
});
//Delete a bookmark
app.del('/json/bookmark/:id', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var params = [req.params.id, req.session.user_id];
client.query('DELETE FROM bookmarks WHERE id = ? AND user_id = ?', params, function(err) {
if (err) {
console.log(err);
} else {
params = [req.params.id];
client.query('DELETE FROM tags WHERE bookmark_id = ?', params, function(err) {
if (err) console.log(err);
});
}
});
res.writeHead(200, { 'Content-type': 'application/json' });
res.end('\n');
});
//Import bookmarks from an HTML file
app.post('/json/import', function(req, res) {
if (typeof req.session.user_id == 'undefined') {
res.writeHead(401, { 'Content-type': 'text/html' });
res.end();
return;
}
var form = new formidable.IncomingForm();
form.uploadDir = __dirname;
form.addListener('file', function(name, file) {
fs.readFile(file.path, 'utf8', function(error, content) {
importFrom(content, req, res);
fs.unlink(file.path);
});
});
form.parse(req, function(err, fields, files) {
if (err) {
console.log(err);
res.end();
}
});
});
/* ==============================================================
Convenience functions
=============================================================== */
function md5(str) {
return crypto
.createHash('md5')
.update(str)
.digest('hex');
};
function importFrom(html, req, res) {
var regex = /<a([^>]*)>([^<]*)<\/a>/gmi;
//Extract anchor tags from the import file
var list = [];
while (true) {
matches = regex.exec(html);
if (matches !== null) {
list.push([matches[1], matches[2]]);
} else {
break;
}
}
//Stick the attributes from the anchor tag onto an object
var links = [];
list.forEach(function(link) {
regex = /(\w+?)=["']{0,1}(.*?)["']{0,1}\s+/g
var obj = { title: link[1] };
while (true) {
matches = regex.exec(' ' + link[0] + ' ');
if (matches !== null) {
obj[matches[1].toLowerCase()] = matches[2];
} else {
break;
}
}
var old = obj.tags;
if (typeof obj.tags == 'undefined' || obj.tags == '') {
obj.tags = [];
} else if (obj.tags.indexOf(',') === -1) {
obj.tags = [obj.tags];
} else {
obj.tags = obj.tags.replace(/"([^,]*),([^"]*)"/gi,"$1 $2").split(',');
}
if ((obj.href.indexOf('http://') === 0 || obj.href.indexOf('https://') === 0) && obj.title.length > 0) {
links.push(obj);
}
});
console.log("Importing " + links.length + "links");
importQueue = 0;
links.forEach(function(link) {
importQueue++;
link.tags.forEach(function(tag) {
importQueue++;
});
});
//Insert the bookmarks and their tags into the database
links.forEach(function(link) {
if (typeof link['add_date'] == 'undefined' || parseInt(link['add_date']) == 0) {
link['add_date'] = 'CURRENT_TIMESTAMP';
} else {
link['add_date'] = "FROM_UNIXTIME('" + link['add_date'] + "')";
}
console.log(link['add_date']);
if (typeof link['private'] == 'undefined') {
link['private'] = 0;
}
var params = [req.session.user_id, link['href'], link['title'], link['private']];
client.query('INSERT INTO bookmarks (user_id, url, title, private, created_at) VALUES (?, ?, ?, ?, ' + link['add_date'] + ')', params, function(err, info) {
if (err) console.log(err);
importQueue--;
link.tags.forEach(function(tag) {
tag = tag.replace('.', ' ').replace('-', ' ');
client.query('INSERT INTO tags (bookmark_id, tag) VALUES (?, ?)', [info.insertId, tag], function(err) {
if (err) console.log(err);
importQueue--;
if (importQueue == 0) {
res.writeHead(302, { 'Location': '/bookmarks' });
res.end();
}
});
});
if (importQueue == 0) {
res.writeHead(302, { 'Location': '/bookmarks' });
res.end();
}
});
});
};