| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | var express = require('express'), | 
					
						
							| 
									
										
										
										
											2013-04-22 15:17:41 -04:00
										 |  |  | 	WebServer = express(), | 
					
						
							|  |  |  | 	server = require('http').createServer(WebServer), | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 	RedisStore = require('connect-redis')(express), | 
					
						
							|  |  |  | 	path = require('path'), | 
					
						
							| 
									
										
										
										
											2013-05-01 12:54:04 -04:00
										 |  |  |     config = require('../config.js'), | 
					
						
							|  |  |  |     redis = require('redis'), | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 	redisServer = redis.createClient(config.redis.port, config.redis.host, config.redis.options), | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-04 18:20:22 -04:00
										 |  |  | 	user = require('./user.js'), | 
					
						
							| 
									
										
										
										
											2013-05-14 13:04:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 	categories = require('./categories.js'), | 
					
						
							|  |  |  | 	posts = require('./posts.js'), | 
					
						
							|  |  |  | 	topics = require('./topics.js'), | 
					
						
							| 
									
										
										
										
											2013-05-04 18:20:22 -04:00
										 |  |  | 	utils = require('./utils.js'), | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 	fs = require('fs'), | 
					
						
							| 
									
										
										
										
											2013-05-09 03:33:53 +00:00
										 |  |  | 	admin = require('./routes/admin.js'), | 
					
						
							| 
									
										
										
										
											2013-05-14 13:04:12 -04:00
										 |  |  | 	userRoute = require('./routes/user.js'), | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 	auth = require('./routes/authentication.js'); | 
					
						
							| 
									
										
										
										
											2013-05-02 15:57:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(app) { | 
					
						
							|  |  |  | 	var templates = global.templates; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 	// Middlewares
 | 
					
						
							|  |  |  | 	app.use(express.favicon());	// 2 args: string path and object options (i.e. expire time etc)
 | 
					
						
							| 
									
										
										
										
											2013-05-02 09:40:44 -04:00
										 |  |  | 	app.use(require('less-middleware')({ src: path.join(__dirname, '../', '/public') })); | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 	app.use(express.static(path.join(__dirname, '../', 'public'))); | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 	app.use(express.bodyParser());	// Puts POST vars in request.body
 | 
					
						
							|  |  |  | 	app.use(express.cookieParser());	// If you want to parse cookies (res.cookies)
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 	app.use(express.compress()); | 
					
						
							| 
									
										
										
										
											2013-04-25 11:15:03 -04:00
										 |  |  | 	app.use(express.session({ | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		store: new RedisStore({ | 
					
						
							| 
									
										
										
										
											2013-05-01 12:54:04 -04:00
										 |  |  | 			client: redisServer, | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 			ttl: 60*60*24*14 | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2013-05-01 16:27:57 -04:00
										 |  |  | 		secret: config.secret, | 
					
						
							| 
									
										
										
										
											2013-04-25 11:15:03 -04:00
										 |  |  | 		key: 'express.sid' | 
					
						
							|  |  |  | 	})); | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	auth.initialize(app); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 	app.use(function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 		// Don't bother with session handling for API requests
 | 
					
						
							| 
									
										
										
										
											2013-04-28 22:26:27 -04:00
										 |  |  | 		if (/^\/api\//.test(req.url)) return next(); | 
					
						
							| 
									
										
										
										
											2013-04-28 21:15:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 		if (req.user && req.user.uid) { | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 			user.session_ping(req.sessionID, req.user.uid); | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 		// (Re-)register the session as active
 | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 		user.active.register(req.sessionID); | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		next(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-05-07 10:56:30 -04:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 	auth.create_routes(app); | 
					
						
							|  |  |  | 	admin.create_routes(app); | 
					
						
							| 
									
										
										
										
											2013-05-14 13:04:12 -04:00
										 |  |  | 	userRoute.create_routes(app); | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 09:13:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 	app.create_route = function(url, tpl) { // to remove
 | 
					
						
							| 
									
										
										
										
											2013-05-07 21:29:28 +00:00
										 |  |  | 		return '<script>templates.ready(function(){ajaxify.go("' + url + '", null, "' + tpl + '");});</script>'; | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-07 21:29:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 21:19:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 22:19:54 +00:00
										 |  |  | 	// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
 | 
					
						
							|  |  |  | 	(function() { | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 		var routes = ['', 'login', 'register', 'account', 'latest', 'popular', 'active', '403']; | 
					
						
							| 
									
										
										
										
											2013-04-25 21:55:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 22:19:54 +00:00
										 |  |  | 		for (var i=0, ii=routes.length; i<ii; i++) { | 
					
						
							|  |  |  | 			(function(route) { | 
					
						
							| 
									
										
										
										
											2013-05-06 14:27:15 -04:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-05-01 22:19:54 +00:00
										 |  |  | 				app.get('/' + route, function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-05-06 14:27:15 -04:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					if ((route === 'login' || route ==='register') && (req.user && req.user.uid > 0)) { | 
					
						
							|  |  |  | 						res.redirect('/account'); | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 					res.send(templates['header'] + app.create_route(route) + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-05-01 22:19:54 +00:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}(routes[i])); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}()); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-03 21:19:28 +00:00
										 |  |  | 	// Complex Routes
 | 
					
						
							|  |  |  | 	app.get('/topic/:topic_id/:slug?', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-05-07 19:22:38 +00:00
										 |  |  | 		var topic_url = req.params.topic_id + (req.params.slug ? '/' + req.params.slug : ''); | 
					
						
							|  |  |  | 		res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("topic/' + topic_url + '");});</script>' + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-05-03 21:19:28 +00:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-04-25 21:55:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-06 22:05:42 +00:00
										 |  |  | 	app.get('/category/:category_id/:slug?', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-05-07 19:22:38 +00:00
										 |  |  | 		var category_url = req.params.category_id + (req.params.slug ? '/' + req.params.slug : ''); | 
					
						
							|  |  |  | 		res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("category/' + category_url + '");});</script>' + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-05-06 22:05:42 +00:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-05 13:05:05 -04:00
										 |  |  | 	app.get('/confirm/:code', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-05-07 19:22:38 +00:00
										 |  |  | 		res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("confirm/' + req.params.code + '");});</script>' + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-05-05 13:05:05 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-03 21:19:28 +00:00
										 |  |  | 	// These functions are called via ajax once the initial page is loaded to populate templates with data
 | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 	function api_method(req, res) {		 | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 		switch(req.params.method) { | 
					
						
							|  |  |  | 			case 'home' : | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 					categories.get(function(data) { | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-05-02 16:36:26 +00:00
										 |  |  | 			case 'login' : | 
					
						
							|  |  |  | 					var data = {}, | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 						login_strategies = auth.get_login_strategies(), | 
					
						
							| 
									
										
										
										
											2013-05-02 16:36:26 +00:00
										 |  |  | 						num_strategies = login_strategies.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (num_strategies == 0) { | 
					
						
							|  |  |  | 						data = { | 
					
						
							|  |  |  | 							'login_window:spansize': 'span12', | 
					
						
							|  |  |  | 							'alternate_logins:display': 'none' | 
					
						
							|  |  |  | 						};	 | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						data = { | 
					
						
							|  |  |  | 							'login_window:spansize': 'span6', | 
					
						
							|  |  |  | 							'alternate_logins:display': 'block' | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						for (var i=0, ii=num_strategies; i<ii; i++) { | 
					
						
							| 
									
										
										
										
											2013-05-02 14:47:00 -04:00
										 |  |  | 							data[login_strategies[i] + ':display'] = 'active'; | 
					
						
							| 
									
										
										
										
											2013-05-02 16:36:26 +00:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-05-02 19:48:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 14:16:33 -04:00
										 |  |  | 					res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case 'register' : | 
					
						
							|  |  |  | 					var data = {}, | 
					
						
							|  |  |  | 						login_strategies = auth.get_login_strategies(), | 
					
						
							|  |  |  | 						num_strategies = login_strategies.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (num_strategies == 0) { | 
					
						
							|  |  |  | 						data = { | 
					
						
							|  |  |  | 							'register_window:spansize': 'span12', | 
					
						
							|  |  |  | 							'alternate_logins:display': 'none' | 
					
						
							|  |  |  | 						};	 | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						data = { | 
					
						
							|  |  |  | 							'register_window:spansize': 'span6', | 
					
						
							|  |  |  | 							'alternate_logins:display': 'block' | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						for (var i=0, ii=num_strategies; i<ii; i++) { | 
					
						
							|  |  |  | 							data[login_strategies[i] + ':display'] = 'active'; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 16:36:26 +00:00
										 |  |  | 					res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 			case 'topic' : | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 					posts.get(function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							| 
									
										
										
										
											2013-05-04 06:01:06 +00:00
										 |  |  | 					}, req.params.id, (req.user) ? req.user.uid : 0); | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-05-06 22:05:42 +00:00
										 |  |  | 			case 'category' : | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 					topics.get(function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-06 22:05:42 +00:00
										 |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							| 
									
										
										
										
											2013-05-09 07:27:55 +00:00
										 |  |  | 					}, req.params.id, (req.user) ? req.user.uid : 0); | 
					
						
							| 
									
										
										
										
											2013-05-06 22:05:42 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-05-07 16:52:35 +00:00
										 |  |  | 			case 'latest' : | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 					topics.get(function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-07 16:52:35 +00:00
										 |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case 'popular' : | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 					topics.get(function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-07 16:52:35 +00:00
										 |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case 'active' : | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 					topics.get(function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-07 16:52:35 +00:00
										 |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-05-07 16:12:26 +00:00
										 |  |  | 			case 'users' :  | 
					
						
							| 
									
										
										
										
											2013-05-07 19:18:13 -04:00
										 |  |  | 					if (!req.params.section && !req.params.id) { | 
					
						
							|  |  |  | 						get_users_fn(req, res, function(userData) { | 
					
						
							|  |  |  | 							res.send(JSON.stringify(userData)); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					else if (String(req.params.section).toLowerCase() === 'edit') { | 
					
						
							| 
									
										
										
										
											2013-05-07 21:29:28 +00:00
										 |  |  | 						get_account_fn(req, res, function(userData) { | 
					
						
							|  |  |  | 							res.send(JSON.stringify(userData)); | 
					
						
							| 
									
										
										
										
											2013-05-08 14:49:33 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-07 21:17:22 +00:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						get_account_fn(req, res, function(userData) { | 
					
						
							|  |  |  | 							res.send(JSON.stringify(userData)); | 
					
						
							|  |  |  | 						});						 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-05-07 19:18:13 -04:00
										 |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-06 03:41:22 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-05-05 13:05:05 -04:00
										 |  |  | 			case 'confirm': | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 					user.email.confirm(req.params.id, function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-05 13:05:05 -04:00
										 |  |  | 						if (data.status === 'ok') { | 
					
						
							|  |  |  | 							res.send(JSON.stringify({ | 
					
						
							|  |  |  | 								'alert-class': 'alert-success', | 
					
						
							|  |  |  | 								title: 'Email Confirmed', | 
					
						
							|  |  |  | 								text: 'Thank you for vaidating your email. Your account is now fully activated.' | 
					
						
							|  |  |  | 							})); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							res.send(JSON.stringify({ | 
					
						
							|  |  |  | 								'alert-class': 'alert-error', | 
					
						
							|  |  |  | 								title: 'An error occurred...', | 
					
						
							|  |  |  | 								text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.' | 
					
						
							|  |  |  | 							})); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 			default : | 
					
						
							|  |  |  | 				res.send('{}'); | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-07 21:17:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 	app.get('/api/:method', api_method); | 
					
						
							|  |  |  | 	app.get('/api/:method/:id', api_method); | 
					
						
							| 
									
										
										
										
											2013-05-07 21:17:22 +00:00
										 |  |  | 	// ok fine MUST ADD RECURSION style. I'll look for a better fix in future but unblocking baris for this:
 | 
					
						
							|  |  |  | 	app.get('/api/:method/:id/:section?', api_method); | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 	app.get('/api/:method/:id*', api_method); | 
					
						
							| 
									
										
										
										
											2013-05-07 21:17:22 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | // TODO move user related logic into another file vvvvvvvvvvvvvvvvvvvv
 | 
					
						
							| 
									
										
										
										
											2013-05-02 10:15:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 	app.post('/pictureupload', function(req, res) { | 
					
						
							|  |  |  |     	 | 
					
						
							|  |  |  | 		if(!req.user) | 
					
						
							|  |  |  | 			return res.redirect('/403'); | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-13 13:51:25 -04:00
										 |  |  | 		if(req.files.userPhoto.size > 131072) { | 
					
						
							| 
									
										
										
										
											2013-05-13 12:14:10 -04:00
										 |  |  | 			res.send({ | 
					
						
							| 
									
										
										
										
											2013-05-13 13:51:25 -04:00
										 |  |  | 				error: 'Images must be smaller than 128kb!' | 
					
						
							| 
									
										
										
										
											2013-05-13 12:14:10 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-13 14:26:08 -04:00
										 |  |  | 		user.getUserField(req.user.uid, 'uploadedpicture', function(oldpicture) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if(!oldpicture) { | 
					
						
							|  |  |  | 				uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-05-13 14:26:08 -04:00
										 |  |  | 			var index = oldpicture.lastIndexOf('/'); | 
					
						
							|  |  |  | 			var filename = oldpicture.substr(index+1); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			var absolutePath = global.configuration['ROOT_DIRECTORY'] + config.upload_path + filename; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			fs.unlink(absolutePath, function(err) { | 
					
						
							|  |  |  | 				if(err) {				 | 
					
						
							|  |  |  | 					console.log(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res); | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	function uploadUserPicture(uid, filename, tempPath, res) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 12:45:40 -04:00
										 |  |  | 		if(!filename){ | 
					
						
							|  |  |  | 			res.send({ | 
					
						
							|  |  |  |                 error: 'Error uploading file! Error : Invalid file name!' | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-13 15:16:54 -04:00
										 |  |  | 		filename = uid + '-' + filename; | 
					
						
							| 
									
										
										
										
											2013-05-13 13:30:33 -04:00
										 |  |  | 		var uploadPath = config.upload_path + filename; | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-13 14:26:08 -04:00
										 |  |  | 		console.log('trying to upload to : '+ global.configuration['ROOT_DIRECTORY'] + uploadPath); | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 		fs.rename( | 
					
						
							|  |  |  | 			tempPath, | 
					
						
							| 
									
										
										
										
											2013-05-13 13:30:33 -04:00
										 |  |  | 			global.configuration['ROOT_DIRECTORY'] + uploadPath, | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 			function(error) { | 
					
						
							|  |  |  | 	            if(error) { | 
					
						
							| 
									
										
										
										
											2013-05-13 14:26:08 -04:00
										 |  |  | 	            	console.log(error); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 					res.send({ | 
					
						
							| 
									
										
										
										
											2013-05-13 13:51:25 -04:00
										 |  |  | 	                    error: 'Error uploading file!' | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 	                return; | 
					
						
							|  |  |  | 	            } | 
					
						
							|  |  |  | 	 			 | 
					
						
							| 
									
										
										
										
											2013-05-13 13:30:33 -04:00
										 |  |  | 	 			var imageUrl = config.upload_url + filename; | 
					
						
							| 
									
										
										
										
											2013-05-11 21:55:14 -04:00
										 |  |  | 	 			 | 
					
						
							|  |  |  | 	            res.send({ | 
					
						
							|  |  |  | 					path: imageUrl | 
					
						
							|  |  |  | 	            }); | 
					
						
							|  |  |  | 	             | 
					
						
							|  |  |  | 	            user.setUserField(uid, 'uploadedpicture', imageUrl); | 
					
						
							|  |  |  | 	            user.setUserField(uid, 'picture', imageUrl); | 
					
						
							|  |  |  | 	             | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  |     	); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	app.post('/changeuserpicture', function(req, res){ | 
					
						
							|  |  |  | 		if(!req.user) | 
					
						
							|  |  |  | 			return res.redirect('/403'); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if(req.user.uid != req.body.uid) | 
					
						
							|  |  |  | 			return res.redirect('/'); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		var type = req.body.type; | 
					
						
							|  |  |  | 		if(type == 'gravatar') {	 | 
					
						
							|  |  |  | 			user.getUserField(req.user.uid, 'gravatarpicture', function(gravatar){ | 
					
						
							|  |  |  | 				user.setUserField(req.user.uid, 'picture', gravatar); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if(type == 'uploaded') { | 
					
						
							|  |  |  | 			user.getUserField(req.user.uid, 'uploadedpicture', function(uploadedpicture){ | 
					
						
							|  |  |  | 				user.setUserField(req.user.uid, 'picture', uploadedpicture); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		res.send({}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-08 13:43:56 -04:00
										 |  |  | 	app.post('/edituser', function(req, res){ | 
					
						
							| 
									
										
										
										
											2013-05-08 16:00:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-08 14:49:33 -04:00
										 |  |  | 		if(!req.user) | 
					
						
							|  |  |  | 			return res.redirect('/403'); | 
					
						
							| 
									
										
										
										
											2013-05-08 14:54:07 -04:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-08 16:00:37 -04:00
										 |  |  | 		if(req.user.uid != req.body.uid) | 
					
						
							| 
									
										
										
										
											2013-05-08 14:49:33 -04:00
										 |  |  | 			return res.redirect('/'); | 
					
						
							| 
									
										
										
										
											2013-05-08 13:43:56 -04:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-09 13:53:58 -04:00
										 |  |  | 		user.updateProfile(req.user.uid, req.body); | 
					
						
							| 
									
										
										
										
											2013-05-08 13:43:56 -04:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		res.redirect('/'); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-06 03:41:22 +00:00
										 |  |  | 	//to baris, move this into account.js or sth later - just moved this out here for you to utilize client side tpl parsing
 | 
					
						
							|  |  |  | 	//I didn't want to change too much so you should probably sort out the params etc
 | 
					
						
							|  |  |  | 	function get_account_fn(req, res, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-07 16:35:29 -04:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		var username = req.params.id; | 
					
						
							| 
									
										
										
										
											2013-05-14 13:04:12 -04:00
										 |  |  | 		console.log("derp"); | 
					
						
							| 
									
										
										
										
											2013-05-07 16:35:29 -04:00
										 |  |  | 		user.get_uid_by_username(username, function(uid) { | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-08 11:48:21 -04:00
										 |  |  | 			user.getUserData(uid, function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-07 16:35:29 -04:00
										 |  |  | 				if(data) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					data.joindate = utils.relativeTime(data.joindate); | 
					
						
							| 
									
										
										
										
											2013-05-13 17:40:14 -04:00
										 |  |  | 					data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear(); | 
					
						
							| 
									
										
										
										
											2013-05-14 13:04:12 -04:00
										 |  |  | 					console.log(data.age); | 
					
						
							|  |  |  | 					if(data.age === null) | 
					
						
							|  |  |  | 						data.age = 0; | 
					
						
							| 
									
										
										
										
											2013-05-08 11:48:21 -04:00
										 |  |  | 					data.uid = uid; | 
					
						
							| 
									
										
										
										
											2013-05-08 14:58:40 -04:00
										 |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-14 11:32:37 -04:00
										 |  |  | 					data.yourid = (req.user)?req.user.uid : 0; | 
					
						
							|  |  |  | 					data.theirid = uid; | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					callback(data); | 
					
						
							| 
									
										
										
										
											2013-05-07 16:35:29 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					callback({user:{}}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-05-04 18:20:22 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-07 19:18:13 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	function get_users_fn(req, res, callback) { | 
					
						
							|  |  |  | 		user.getUserList(function(data){ | 
					
						
							|  |  |  | 			callback({users:data}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-06 03:41:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-05 18:40:04 -04:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-02 09:13:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-07 16:49:18 -04:00
										 |  |  | 	app.get('/users/:uid/edit', function(req, res){ | 
					
						
							| 
									
										
										
										
											2013-05-07 13:55:02 -04:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-08 14:26:29 -04:00
										 |  |  | 		if(!req.user) | 
					
						
							|  |  |  | 			return res.redirect('/403'); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		user.getUserField(req.user.uid, 'username', function(username) { | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 			if(req.params.uid && username === req.params.uid) | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | 				res.send(templates['header'] + app.create_route('users/'+req.params.uid+'/edit','accountedit') + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-05-08 14:26:29 -04:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				return res.redirect('/403'); | 
					
						
							|  |  |  | 		});	 | 
					
						
							| 
									
										
										
										
											2013-05-07 13:55:02 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-05-07 16:35:29 -04:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-06 10:05:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 10:20:25 -04:00
										 |  |  | 	app.get('/test', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-05-14 12:13:29 -04:00
										 |  |  | 		posts.getRawContent(11, function(post) { | 
					
						
							| 
									
										
										
										
											2013-05-09 10:20:25 -04:00
										 |  |  | 			res.send(JSON.stringify(post)); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 06:26:32 +00:00
										 |  |  | // TODO move user related logic into another file ^^^^^^^^^^^^^^^^^^^^^^^
 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | }(WebServer)); | 
					
						
							| 
									
										
										
										
											2013-04-22 15:17:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | server.listen(config.port); | 
					
						
							| 
									
										
										
										
											2013-04-22 15:23:02 -04:00
										 |  |  | global.server = server; |