mirror of
				https://github.com/KevinMidboe/zoff.git
				synced 2025-10-29 18:00:23 +00:00 
			
		
		
		
	Merge pull request #511 from zoff-music/refactor/prettifier
Prettified som more files and fixed some logging of missing files so …
This commit is contained in:
		| @@ -1,21 +1,20 @@ | |||||||
| var cluster = require('cluster'), | var cluster = require("cluster"), | ||||||
| net = require('net'), |   net = require("net"), | ||||||
| path = require('path'), |   path = require("path"), | ||||||
| //publicPath = path.join(__dirname, 'public'), |   //publicPath = path.join(__dirname, 'public'), | ||||||
| http = require('http'), |   http = require("http"), | ||||||
| port = 8080, |   port = 8080, | ||||||
| //farmhash = require('farmhash'), |   //farmhash = require('farmhash'), | ||||||
| uniqid = require('uniqid'), |   uniqid = require("uniqid"), | ||||||
| num_processes = require('os').cpus().length; |   num_processes = require("os").cpus().length; | ||||||
|  |  | ||||||
| publicPath = path.join(__dirname, 'public'); | publicPath = path.join(__dirname, "public"); | ||||||
| pathThumbnails = __dirname; | pathThumbnails = __dirname; | ||||||
|  |  | ||||||
|  |  | ||||||
| try { | try { | ||||||
|   var redis = require("redis"); |   var redis = require("redis"); | ||||||
|     var client = redis.createClient({host: "localhost", port: 6379}); |   var client = redis.createClient({ host: "localhost", port: 6379 }); | ||||||
|     client.on("error", function (err) { |   client.on("error", function(err) { | ||||||
|     console.log("Couldn't connect to redis-server, assuming non-clustered run"); |     console.log("Couldn't connect to redis-server, assuming non-clustered run"); | ||||||
|     num_processes = 1; |     num_processes = 1; | ||||||
|     startSingle(false, false); |     startSingle(false, false); | ||||||
| @@ -25,7 +24,7 @@ try { | |||||||
|     startClustered(true); |     startClustered(true); | ||||||
|     client.quit(); |     client.quit(); | ||||||
|   }); |   }); | ||||||
| } catch(e) { | } catch (e) { | ||||||
|   console.log("Couldn't connect to redis-server, assuming non-clustered run"); |   console.log("Couldn't connect to redis-server, assuming non-clustered run"); | ||||||
|   num_processes = 1; |   num_processes = 1; | ||||||
|   startSingle(false, false); |   startSingle(false, false); | ||||||
| @@ -37,12 +36,12 @@ function startClustered(redis_enabled) { | |||||||
|     var workers = []; |     var workers = []; | ||||||
|     var spawn = function(i) { |     var spawn = function(i) { | ||||||
|       workers[i] = cluster.fork(); |       workers[i] = cluster.fork(); | ||||||
|             workers[i].on('exit', function(code, signal) { |       workers[i].on("exit", function(code, signal) { | ||||||
|                 if(code == 1) { |         if (code == 1) { | ||||||
|           process.exit(1); |           process.exit(1); | ||||||
|           return; |           return; | ||||||
|         } |         } | ||||||
|                 console.log('respawning worker', i); |         console.log("respawning worker", i); | ||||||
|         spawn(i); |         spawn(i); | ||||||
|       }); |       }); | ||||||
|     }; |     }; | ||||||
| @@ -53,21 +52,24 @@ function startClustered(redis_enabled) { | |||||||
|  |  | ||||||
|     var worker_index = function(ip, len) { |     var worker_index = function(ip, len) { | ||||||
|       //console.log(ip); |       //console.log(ip); | ||||||
|             var s = ''; |       var s = ""; | ||||||
|             if(ip == undefined) ip = uniqid.time(); |       if (ip == undefined) ip = uniqid.time(); | ||||||
|       for (var i = 0, _len = ip.length; i < _len; i++) { |       for (var i = 0, _len = ip.length; i < _len; i++) { | ||||||
|                 if(!isNaN(ip[i])) { |         if (!isNaN(ip[i])) { | ||||||
|           s += ip[i]; |           s += ip[i]; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|             return Number(s)%len; |       return Number(s) % len; | ||||||
|       //eturn farmhash.fingerprint32(ip) % len; |       //eturn farmhash.fingerprint32(ip) % len; | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|         var server = net.createServer({ pauseOnConnect: true }, function(connection, a) { |     var server = net | ||||||
|             var worker = workers[worker_index(connection.remoteAddress, num_processes)]; |       .createServer({ pauseOnConnect: true }, function(connection, a) { | ||||||
|             worker.send('sticky-session:connection', connection); |         var worker = | ||||||
|         }).listen(port); |           workers[worker_index(connection.remoteAddress, num_processes)]; | ||||||
|  |         worker.send("sticky-session:connection", connection); | ||||||
|  |       }) | ||||||
|  |       .listen(port); | ||||||
|   } else { |   } else { | ||||||
|     startSingle(true, redis_enabled); |     startSingle(true, redis_enabled); | ||||||
|   } |   } | ||||||
| @@ -75,10 +77,13 @@ function startClustered(redis_enabled) { | |||||||
|  |  | ||||||
| function startSingle(clustered, redis_enabled) { | function startSingle(clustered, redis_enabled) { | ||||||
|   var server; |   var server; | ||||||
|     var client = require('./apps/client.js'); |   var client = require("./apps/client.js"); | ||||||
|   try { |   try { | ||||||
|         var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js')); |     var cert_config = require(path.join( | ||||||
|         var fs = require('fs'); |       path.join(__dirname, "config"), | ||||||
|  |       "cert_config.js" | ||||||
|  |     )); | ||||||
|  |     var fs = require("fs"); | ||||||
|     var privateKey = fs.readFileSync(cert_config.privateKey).toString(); |     var privateKey = fs.readFileSync(cert_config.privateKey).toString(); | ||||||
|     var certificate = fs.readFileSync(cert_config.certificate).toString(); |     var certificate = fs.readFileSync(cert_config.certificate).toString(); | ||||||
|     var ca = fs.readFileSync(cert_config.ca).toString(); |     var ca = fs.readFileSync(cert_config.ca).toString(); | ||||||
| @@ -87,14 +92,14 @@ function startSingle(clustered, redis_enabled) { | |||||||
|       cert: certificate, |       cert: certificate, | ||||||
|       ca: ca |       ca: ca | ||||||
|     }; |     }; | ||||||
|         var https = require('https'); |     var https = require("https"); | ||||||
|     server = https.Server(credentials, routingFunction); |     server = https.Server(credentials, routingFunction); | ||||||
|     } catch(err){ |   } catch (err) { | ||||||
|     console.log("Starting without https (probably on localhost)"); |     console.log("Starting without https (probably on localhost)"); | ||||||
|     server = http.createServer(routingFunction); |     server = http.createServer(routingFunction); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|     if(clustered) { |   if (clustered) { | ||||||
|     server.listen(onListen); |     server.listen(onListen); | ||||||
|   } else { |   } else { | ||||||
|     server.listen(port, onListen); |     server.listen(port, onListen); | ||||||
| @@ -102,21 +107,21 @@ function startSingle(clustered, redis_enabled) { | |||||||
|  |  | ||||||
|   var socketIO = client.socketIO; |   var socketIO = client.socketIO; | ||||||
|  |  | ||||||
|     if(redis_enabled) { |   if (redis_enabled) { | ||||||
|         var redis = require('socket.io-redis'); |     var redis = require("socket.io-redis"); | ||||||
|     try { |     try { | ||||||
|             socketIO.adapter(redis({ host: 'localhost', port: 6379 })); |       socketIO.adapter(redis({ host: "localhost", port: 6379 })); | ||||||
|         } catch(e) { |     } catch (e) { | ||||||
|       console.log("No redis-server to connect to.."); |       console.log("No redis-server to connect to.."); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   socketIO.listen(server); |   socketIO.listen(server); | ||||||
|  |  | ||||||
|     process.on('message', function(message, connection) { |   process.on("message", function(message, connection) { | ||||||
|         if (message !== 'sticky-session:connection') { |     if (message !== "sticky-session:connection") { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|         server.emit('connection', connection); |     server.emit("connection", connection); | ||||||
|     connection.resume(); |     connection.resume(); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
| @@ -126,21 +131,25 @@ function onListen() { | |||||||
| } | } | ||||||
|  |  | ||||||
| function routingFunction(req, res, next) { | function routingFunction(req, res, next) { | ||||||
|     var client = require('./apps/client.js'); |   var client = require("./apps/client.js"); | ||||||
|     var admin = require('./apps/admin.js'); |   var admin = require("./apps/admin.js"); | ||||||
|   try { |   try { | ||||||
|         var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; |     var url = req.headers["x-forwarded-host"] | ||||||
|         var subdomain = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'].split(".") : req.headers.host.split(":")[0].split("."); |       ? req.headers["x-forwarded-host"] | ||||||
|  |       : req.headers.host.split(":")[0]; | ||||||
|  |     var subdomain = req.headers["x-forwarded-host"] | ||||||
|  |       ? req.headers["x-forwarded-host"].split(".") | ||||||
|  |       : req.headers.host.split(":")[0].split("."); | ||||||
|  |  | ||||||
|         if(subdomain.length > 1 && subdomain[0] == "admin") { |     if (subdomain.length > 1 && subdomain[0] == "admin") { | ||||||
|       admin(req, res, next); |       admin(req, res, next); | ||||||
|     } else { |     } else { | ||||||
|       client(req, res, next); |       client(req, res, next); | ||||||
|     } |     } | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     console.log("Bad request for " + req.headers.host + req.url, e); |     console.log("Bad request for " + req.headers.host + req.url, e); | ||||||
|     res.statusCode = 500; |     res.statusCode = 500; | ||||||
|         res.write('Bad request'); //write a response to the client |     res.write("Bad request"); //write a response to the client | ||||||
|     res.end(); //end the response |     res.end(); //end the response | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,40 +1,39 @@ | |||||||
| var express = require('express'); | var express = require("express"); | ||||||
| var app = express(); | var app = express(); | ||||||
|  |  | ||||||
| const path = require('path'); | const path = require("path"); | ||||||
| const publicPath = path.join(__dirname + "", '../public'); | const publicPath = path.join(__dirname + "", "../public"); | ||||||
| var exphbs = require('express-handlebars'); | var exphbs = require("express-handlebars"); | ||||||
| var hbs = exphbs.create({ | var hbs = exphbs.create({ | ||||||
|     defaultLayout: publicPath + '/layouts/admin/main', |   defaultLayout: publicPath + "/layouts/admin/main", | ||||||
|     layoutsDir: publicPath + '/layouts', |   layoutsDir: publicPath + "/layouts", | ||||||
|     partialsDir: publicPath + '/partials' |   partialsDir: publicPath + "/partials" | ||||||
| }); | }); | ||||||
|  |  | ||||||
| var passport = require('passport'); | var passport = require("passport"); | ||||||
| var mpromise = require('mpromise'); | var mpromise = require("mpromise"); | ||||||
| var LocalStrategy = require('passport-local').Strategy; | var LocalStrategy = require("passport-local").Strategy; | ||||||
| var mongoose = require('mongoose'); | var mongoose = require("mongoose"); | ||||||
| var mongo_db_cred = require(pathThumbnails + '/config/mongo_config.js'); | var mongo_db_cred = require(pathThumbnails + "/config/mongo_config.js"); | ||||||
| var mongojs = require('mongojs'); | var mongojs = require("mongojs"); | ||||||
| var db = mongojs(mongo_db_cred.config); | var db = mongojs(mongo_db_cred.config); | ||||||
| var token_db = mongojs("tokens"); | var token_db = mongojs("tokens"); | ||||||
| var bodyParser = require('body-parser'); | var bodyParser = require("body-parser"); | ||||||
| var session = require('express-session'); | var session = require("express-session"); | ||||||
| var MongoStore = require('connect-mongo')(session); | var MongoStore = require("connect-mongo")(session); | ||||||
| var api = require(pathThumbnails + '/routing/admin/api.js'); | var api = require(pathThumbnails + "/routing/admin/api.js"); | ||||||
|  |  | ||||||
| var compression = require('compression'); | var compression = require("compression"); | ||||||
| var User = require(pathThumbnails + '/models/user.js'); | var User = require(pathThumbnails + "/models/user.js"); | ||||||
| var url = 'mongodb://' + mongo_db_cred.host + '/' + mongo_db_cred.users; | var url = "mongodb://" + mongo_db_cred.host + "/" + mongo_db_cred.users; | ||||||
| mongoose.connect(url); | mongoose.connect(url); | ||||||
|  |  | ||||||
|  | app.engine("handlebars", hbs.engine); | ||||||
|  | app.set("view engine", "handlebars"); | ||||||
|  | app.use(compression({ filter: shouldCompress })); | ||||||
|  |  | ||||||
| app.engine('handlebars', hbs.engine); | function shouldCompress(req, res) { | ||||||
| app.set('view engine', 'handlebars'); |   if (req.headers["x-no-compression"]) { | ||||||
| app.use(compression({filter: shouldCompress})) |  | ||||||
|  |  | ||||||
| function shouldCompress (req, res) { |  | ||||||
|     if (req.headers['x-no-compression']) { |  | ||||||
|     // don't compress responses with this request header |     // don't compress responses with this request header | ||||||
|     return false; |     return false; | ||||||
|   } |   } | ||||||
| @@ -42,14 +41,15 @@ function shouldCompress (req, res) { | |||||||
|   // fallback to standard filter function |   // fallback to standard filter function | ||||||
|   return compression.filter(req, res); |   return compression.filter(req, res); | ||||||
| } | } | ||||||
| app.set('trust proxy', '127.0.0.1'); | app.set("trust proxy", "127.0.0.1"); | ||||||
|  |  | ||||||
| var bodyParser = require('body-parser'); | var bodyParser = require("body-parser"); | ||||||
| var cookieParser = require("cookie-parser"); | var cookieParser = require("cookie-parser"); | ||||||
| var referrerPolicy = require('referrer-policy'); | var referrerPolicy = require("referrer-policy"); | ||||||
| var helmet = require('helmet'); | var helmet = require("helmet"); | ||||||
| var featurePolicy = require('feature-policy'); | var featurePolicy = require("feature-policy"); | ||||||
| app.use(featurePolicy({ | app.use( | ||||||
|  |   featurePolicy({ | ||||||
|     features: { |     features: { | ||||||
|       fullscreen: ["*"], |       fullscreen: ["*"], | ||||||
|       //vibrate: ["'none'"], |       //vibrate: ["'none'"], | ||||||
| @@ -57,31 +57,38 @@ app.use(featurePolicy({ | |||||||
|       microphone: ["'none'"], |       microphone: ["'none'"], | ||||||
|       camera: ["'none'"], |       camera: ["'none'"], | ||||||
|       speaker: ["*"], |       speaker: ["*"], | ||||||
|         syncXhr: ["'self'"], |       syncXhr: ["'self'"] | ||||||
|       //notifications: ["'self'"] |       //notifications: ["'self'"] | ||||||
|     } |     } | ||||||
| })); |   }) | ||||||
| app.use(helmet({ | ); | ||||||
|     frameguard: false, | app.use( | ||||||
| })); |   helmet({ | ||||||
| app.use(referrerPolicy({ policy: 'origin-when-cross-origin' })); |     frameguard: false | ||||||
| app.enable('view cache'); |   }) | ||||||
| app.set('views', publicPath); | ); | ||||||
| app.use( bodyParser.json() );       // to support JSON-encoded bodies | app.use(referrerPolicy({ policy: "origin-when-cross-origin" })); | ||||||
| app.use(bodyParser.urlencoded({ | app.enable("view cache"); | ||||||
|  | app.set("views", publicPath); | ||||||
|  | app.use(bodyParser.json()); // to support JSON-encoded bodies | ||||||
|  | app.use( | ||||||
|  |   bodyParser.urlencoded({ | ||||||
|     extended: true |     extended: true | ||||||
| })); |   }) | ||||||
| app.use(session({ | ); | ||||||
|  | app.use( | ||||||
|  |   session({ | ||||||
|     secret: mongo_db_cred.secret, |     secret: mongo_db_cred.secret, | ||||||
|     resave: true, |     resave: true, | ||||||
|     saveUninitialized: true, |     saveUninitialized: true, | ||||||
|     store: new MongoStore({ |     store: new MongoStore({ | ||||||
|       url: url, |       url: url, | ||||||
|       useNewUrlParser: true, |       useNewUrlParser: true, | ||||||
|         collection: 'sessions', |       collection: "sessions", | ||||||
|       ttl: mongo_db_cred.expire |       ttl: mongo_db_cred.expire | ||||||
|     }) |     }) | ||||||
| })); // session secret |   }) | ||||||
|  | ); // session secret | ||||||
| app.use(passport.initialize()); | app.use(passport.initialize()); | ||||||
| app.use(passport.session()); // persistent login sessions | app.use(passport.session()); // persistent login sessions | ||||||
|  |  | ||||||
| @@ -91,8 +98,6 @@ passport.serializeUser(function(user, done) { | |||||||
|   done(null, user.id); |   done(null, user.id); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| // used to deserialize the user | // used to deserialize the user | ||||||
| passport.deserializeUser(function(id, done) { | passport.deserializeUser(function(id, done) { | ||||||
|   User.findById(id, function(err, user) { |   User.findById(id, function(err, user) { | ||||||
| @@ -100,33 +105,37 @@ passport.deserializeUser(function(id, done) { | |||||||
|   }); |   }); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| passport.use('local-signup', new LocalStrategy({ | passport.use( | ||||||
|  |   "local-signup", | ||||||
|  |   new LocalStrategy( | ||||||
|  |     { | ||||||
|       // by default, local strategy uses username and password, we will override with username |       // by default, local strategy uses username and password, we will override with username | ||||||
|     usernameField : 'username', |       usernameField: "username", | ||||||
|     passwordField : 'password', |       passwordField: "password", | ||||||
|     passReqToCallback : true // allows us to pass back the entire request to the callback |       passReqToCallback: true // allows us to pass back the entire request to the callback | ||||||
| }, |     }, | ||||||
| function(req, username, password, done) { |     function(req, username, password, done) { | ||||||
|       // asynchronous |       // asynchronous | ||||||
|       // User.findOne wont fire unless data is sent back |       // User.findOne wont fire unless data is sent back | ||||||
|       process.nextTick(function() { |       process.nextTick(function() { | ||||||
|  |  | ||||||
|         // find a user whose username is the same as the forms username |         // find a user whose username is the same as the forms username | ||||||
|         // we are checking to see if the user trying to login already exists |         // we are checking to see if the user trying to login already exists | ||||||
|         var token = req.body.token; |         var token = req.body.token; | ||||||
|         token_db.collection("tokens").find({token: token}, function(err, docs){ |         token_db | ||||||
|             if(docs.length == 1){ |           .collection("tokens") | ||||||
|                 token_db.collection("tokens").remove({token: token}, function(err, docs){ |           .find({ token: token }, function(err, docs) { | ||||||
|                     User.findOne({ 'username' :  username }, function(err, user) { |             if (docs.length == 1) { | ||||||
|  |               token_db | ||||||
|  |                 .collection("tokens") | ||||||
|  |                 .remove({ token: token }, function(err, docs) { | ||||||
|  |                   User.findOne({ username: username }, function(err, user) { | ||||||
|                     // if there are any errors, return the error |                     // if there are any errors, return the error | ||||||
|                         if (err) |                     if (err) return done(err); | ||||||
|                         return done(err); |  | ||||||
|  |  | ||||||
|                     // check to see if theres already a user with that username |                     // check to see if theres already a user with that username | ||||||
|                     if (user) { |                     if (user) { | ||||||
|                       return done(null, false); |                       return done(null, false); | ||||||
|                     } else { |                     } else { | ||||||
|  |  | ||||||
|                       // if there is no user with that username |                       // if there is no user with that username | ||||||
|                       // create the user |                       // create the user | ||||||
|                       var newUser = new User(); |                       var newUser = new User(); | ||||||
| @@ -137,12 +146,10 @@ function(req, username, password, done) { | |||||||
|  |  | ||||||
|                       // save the user |                       // save the user | ||||||
|                       newUser.save(function(err) { |                       newUser.save(function(err) { | ||||||
|                                 if (err) |                         if (err) throw err; | ||||||
|                                 throw err; |  | ||||||
|                         return done(null, newUser); |                         return done(null, newUser); | ||||||
|                       }); |                       }); | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                   }); |                   }); | ||||||
|                 }); |                 }); | ||||||
|             } else { |             } else { | ||||||
| @@ -150,103 +157,112 @@ function(req, username, password, done) { | |||||||
|             } |             } | ||||||
|           }); |           }); | ||||||
|       }); |       }); | ||||||
|  |     } | ||||||
|  |   ) | ||||||
|  | ); | ||||||
|  |  | ||||||
| })); | passport.use( | ||||||
|  |   "local-login", | ||||||
| passport.use('local-login', new LocalStrategy({ |   new LocalStrategy( | ||||||
|  |     { | ||||||
|       // by default, local strategy uses username and password, we will override with email |       // by default, local strategy uses username and password, we will override with email | ||||||
|     usernameField : 'username', |       usernameField: "username", | ||||||
|     passwordField : 'password', |       passwordField: "password", | ||||||
|     passReqToCallback : true // allows us to pass back the entire request to the callback |       passReqToCallback: true // allows us to pass back the entire request to the callback | ||||||
| },    function(req, username, password, done) { // callback with email and password from our form |     }, | ||||||
|  |     function(req, username, password, done) { | ||||||
|  |       // callback with email and password from our form | ||||||
|  |  | ||||||
|       // find a user whose email is the same as the forms email |       // find a user whose email is the same as the forms email | ||||||
|       // we are checking to see if the user trying to login already exists |       // we are checking to see if the user trying to login already exists | ||||||
|     User.findOne({ 'username' :  username }, function(err, user) { |       User.findOne({ username: username }, function(err, user) { | ||||||
|         // if there are any errors, return the error before anything else |         // if there are any errors, return the error before anything else | ||||||
|         if (err) |         if (err) return done(err); | ||||||
|         return done(err); |  | ||||||
|  |  | ||||||
|         // if no user is found, return the message |         // if no user is found, return the message | ||||||
|         if (!user) |         if (!user) return done(null, false); // req.flash is the way to set flashdata using connect-flash | ||||||
|         return done(null, false); // req.flash is the way to set flashdata using connect-flash |  | ||||||
|  |  | ||||||
|         // if the user is found but the password is wrong |         // if the user is found but the password is wrong | ||||||
|         if (!user.validPassword(password)) |         if (!user.validPassword(password)) return done(null, false); // create the loginMessage and save it to session as flashdata | ||||||
|         return done(null, false); // create the loginMessage and save it to session as flashdata |  | ||||||
|  |  | ||||||
|         // all is well, return successful user |         // all is well, return successful user | ||||||
|  |  | ||||||
|         return done(null, user); |         return done(null, user); | ||||||
|       }); |       }); | ||||||
|  |     } | ||||||
|  |   ) | ||||||
|  | ); | ||||||
|  |  | ||||||
| })); | app.post( | ||||||
|  |   "/signup", | ||||||
|  |   passport.authenticate("local-signup", { | ||||||
|  |     successRedirect: "/", // redirect to the secure profile section | ||||||
|  |     failureRedirect: "/signup", // redirect back to the signup page if there is an error | ||||||
|  |     failureFlash: true // allow flash messages | ||||||
|  |   }) | ||||||
|  | ); | ||||||
|  |  | ||||||
| app.post('/signup', passport.authenticate('local-signup', { | app.post( | ||||||
|     successRedirect : '/', // redirect to the secure profile section |   "/login", | ||||||
|     failureRedirect : '/signup', // redirect back to the signup page if there is an error |   passport.authenticate("local-login", { | ||||||
|     failureFlash : true // allow flash messages |     successRedirect: "/", // redirect to the secure profile section | ||||||
| })); |     failureRedirect: "/login#failed", // redirect back to the signup page if there is an error | ||||||
|  |     failureFlash: true // allow flash messages | ||||||
|  |   }) | ||||||
|  | ); | ||||||
|  |  | ||||||
| app.post('/login', passport.authenticate('local-login', { | app.use("/login", isLoggedInTryingToLogIn, function(req, res) { | ||||||
|     successRedirect : '/', // redirect to the secure profile section |  | ||||||
|     failureRedirect : '/login#failed', // redirect back to the signup page if there is an error |  | ||||||
|     failureFlash : true // allow flash messages |  | ||||||
| })); |  | ||||||
|  |  | ||||||
| app.use('/login', isLoggedInTryingToLogIn, function(req, res) { |  | ||||||
|   var data = { |   var data = { | ||||||
|     where_get: "not_authenticated" |     where_get: "not_authenticated" | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|     res.render('layouts/admin/not_authenticated', data); |   res.render("layouts/admin/not_authenticated", data); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/signup', isLoggedInTryingToLogIn, function(req, res) { | app.use("/signup", isLoggedInTryingToLogIn, function(req, res) { | ||||||
|   var data = { |   var data = { | ||||||
|     where_get: "not_authenticated" |     where_get: "not_authenticated" | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|     res.render('layouts/admin/not_authenticated', data); |   res.render("layouts/admin/not_authenticated", data); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/', api); | app.use("/", api); | ||||||
|  |  | ||||||
| app.use('/logout', function(req, res) { | app.use("/logout", function(req, res) { | ||||||
|   req.logout(); |   req.logout(); | ||||||
|     res.redirect('/login'); |   res.redirect("/login"); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/assets/admin/authenticated', function(req, res, next) { | app.use("/assets/admin/authenticated", function(req, res, next) { | ||||||
|     if(!req.isAuthenticated()) { |   if (!req.isAuthenticated()) { | ||||||
|     res.sendStatus(403); |     res.sendStatus(403); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   return next(); |   return next(); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/assets', express.static(publicPath + '/assets')); | app.use("/assets", express.static(publicPath + "/assets")); | ||||||
|  |  | ||||||
| app.use('/', isLoggedIn, function(req, res) { | app.use("/", isLoggedIn, function(req, res) { | ||||||
|   var data = { |   var data = { | ||||||
|     where_get: "authenticated", |     where_get: "authenticated", | ||||||
|         year: new Date().getYear()+1900, |     year: new Date().getYear() + 1900 | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|     res.render('layouts/admin/authenticated', data); |   res.render("layouts/admin/authenticated", data); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| function isLoggedInTryingToLogIn(req, res, next){ | function isLoggedInTryingToLogIn(req, res, next) { | ||||||
|     if(!req.isAuthenticated()){ |   if (!req.isAuthenticated()) { | ||||||
|     return next(); |     return next(); | ||||||
|   } |   } | ||||||
|   res.redirect("/"); |   res.redirect("/"); | ||||||
| } | } | ||||||
|  |  | ||||||
| function isLoggedIn(req, res, next) { | function isLoggedIn(req, res, next) { | ||||||
|     if (req.isAuthenticated()) |   if (req.isAuthenticated()) return next(); | ||||||
|     return next(); |   res.redirect("/login"); | ||||||
|     res.redirect('/login'); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| //app.listen(default_port); | //app.listen(default_port); | ||||||
|   | |||||||
| @@ -1,9 +1,12 @@ | |||||||
| VERSION = require(pathThumbnails + '/VERSION.js'); | VERSION = require(pathThumbnails + "/VERSION.js"); | ||||||
| var secure = false; | var secure = false; | ||||||
| var path = require('path'); | var path = require("path"); | ||||||
| try { | try { | ||||||
|     var cert_config = require(path.join(path.join(__dirname, '../config/'), 'cert_config.js')); |   var cert_config = require(path.join( | ||||||
|     var fs = require('fs'); |     path.join(__dirname, "../config/"), | ||||||
|  |     "cert_config.js" | ||||||
|  |   )); | ||||||
|  |   var fs = require("fs"); | ||||||
|   var privateKey = fs.readFileSync(cert_config.privateKey).toString(); |   var privateKey = fs.readFileSync(cert_config.privateKey).toString(); | ||||||
|   var certificate = fs.readFileSync(cert_config.certificate).toString(); |   var certificate = fs.readFileSync(cert_config.certificate).toString(); | ||||||
|   var ca = fs.readFileSync(cert_config.ca).toString(); |   var ca = fs.readFileSync(cert_config.ca).toString(); | ||||||
| @@ -13,40 +16,39 @@ try { | |||||||
|     ca: ca |     ca: ca | ||||||
|   }; |   }; | ||||||
|   secure = true; |   secure = true; | ||||||
| } catch(err){} | } catch (err) {} | ||||||
|  |  | ||||||
| var add = ""; | var add = ""; | ||||||
| var express = require('express'); | var express = require("express"); | ||||||
| var app = express(); | var app = express(); | ||||||
| var compression = require('compression'); | var compression = require("compression"); | ||||||
| var exphbs = require('express-handlebars'); | var exphbs = require("express-handlebars"); | ||||||
| var cors = require('cors'); | var cors = require("cors"); | ||||||
| var Functions = require(pathThumbnails + '/handlers/functions.js'); | var Functions = require(pathThumbnails + "/handlers/functions.js"); | ||||||
|  |  | ||||||
| var hbs = exphbs.create({ | var hbs = exphbs.create({ | ||||||
|     defaultLayout: publicPath + '/layouts/client/main', |   defaultLayout: publicPath + "/layouts/client/main", | ||||||
|     layoutsDir: publicPath + '/layouts/client', |   layoutsDir: publicPath + "/layouts/client", | ||||||
|     partialsDir: publicPath + '/partials', |   partialsDir: publicPath + "/partials", | ||||||
|   helpers: { |   helpers: { | ||||||
|     if_equal: function(a, b, opts) { |     if_equal: function(a, b, opts) { | ||||||
|       if (a == b) { |       if (a == b) { | ||||||
|                 return opts.fn(this) |         return opts.fn(this); | ||||||
|       } else { |       } else { | ||||||
|                 return opts.inverse(this) |         return opts.inverse(this); | ||||||
|       } |       } | ||||||
|     }, |     }, | ||||||
|     decodeString: function(s) { |     decodeString: function(s) { | ||||||
|             if(s == undefined) return s; |       if (s == undefined) return s; | ||||||
|       return Functions.decodeChannelName(s); |       return Functions.decodeChannelName(s); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
| var uniqid = require('uniqid'); | var uniqid = require("uniqid"); | ||||||
| app.use(compression({filter: shouldCompress})) | app.use(compression({ filter: shouldCompress })); | ||||||
|  |  | ||||||
| function shouldCompress (req, res) { | function shouldCompress(req, res) { | ||||||
|     if (req.headers['x-no-compression']) { |   if (req.headers["x-no-compression"]) { | ||||||
|     // don't compress responses with this request header |     // don't compress responses with this request header | ||||||
|     return false; |     return false; | ||||||
|   } |   } | ||||||
| @@ -55,18 +57,19 @@ function shouldCompress (req, res) { | |||||||
|   return compression.filter(req, res); |   return compression.filter(req, res); | ||||||
| } | } | ||||||
|  |  | ||||||
| app.engine('handlebars', hbs.engine); | app.engine("handlebars", hbs.engine); | ||||||
| app.set('view engine', 'handlebars'); | app.set("view engine", "handlebars"); | ||||||
| app.enable('view cache'); | app.enable("view cache"); | ||||||
| app.set('views', publicPath); | app.set("views", publicPath); | ||||||
| app.set('trust proxy', '127.0.0.1'); | app.set("trust proxy", "127.0.0.1"); | ||||||
|  |  | ||||||
| var bodyParser = require('body-parser'); | var bodyParser = require("body-parser"); | ||||||
| var cookieParser = require("cookie-parser"); | var cookieParser = require("cookie-parser"); | ||||||
| var referrerPolicy = require('referrer-policy'); | var referrerPolicy = require("referrer-policy"); | ||||||
| var helmet = require('helmet'); | var helmet = require("helmet"); | ||||||
| var featurePolicy = require('feature-policy'); | var featurePolicy = require("feature-policy"); | ||||||
| app.use(featurePolicy({ | app.use( | ||||||
|  |   featurePolicy({ | ||||||
|     features: { |     features: { | ||||||
|       fullscreen: ["*"], |       fullscreen: ["*"], | ||||||
|       //vibrate: ["'none'"], |       //vibrate: ["'none'"], | ||||||
| @@ -74,112 +77,131 @@ app.use(featurePolicy({ | |||||||
|       microphone: ["'none'"], |       microphone: ["'none'"], | ||||||
|       camera: ["'none'"], |       camera: ["'none'"], | ||||||
|       speaker: ["*"], |       speaker: ["*"], | ||||||
|         syncXhr: ["'self'"], |       syncXhr: ["'self'"] | ||||||
|       //notifications: ["'self'"] |       //notifications: ["'self'"] | ||||||
|     } |     } | ||||||
| })); |   }) | ||||||
| app.use(helmet({ | ); | ||||||
|     frameguard: false, | app.use( | ||||||
| })); |   helmet({ | ||||||
| app.use(referrerPolicy({ policy: 'origin-when-cross-origin' })); |     frameguard: false | ||||||
| app.use( bodyParser.json() );       // to support JSON-encoded bodies |   }) | ||||||
| app.use(bodyParser.urlencoded({     // to support URL-encoded bodies | ); | ||||||
|  | app.use(referrerPolicy({ policy: "origin-when-cross-origin" })); | ||||||
|  | app.use(bodyParser.json()); // to support JSON-encoded bodies | ||||||
|  | app.use( | ||||||
|  |   bodyParser.urlencoded({ | ||||||
|  |     // to support URL-encoded bodies | ||||||
|     extended: true |     extended: true | ||||||
| })); |   }) | ||||||
|  | ); | ||||||
| app.use(cookieParser()); | app.use(cookieParser()); | ||||||
| //app.set('json spaces', 2); | //app.set('json spaces', 2); | ||||||
|  |  | ||||||
| io = require('socket.io')({ | io = require("socket.io")({ | ||||||
|     pingTimeout: 25000, |   pingTimeout: 25000 | ||||||
|   //path: '/zoff', |   //path: '/zoff', | ||||||
|   //"origins": ("https://zoff.me:443*,https://zoff.me:8080*,zoff.me:8080*,https://remote.zoff.me:443*,https://remote.zoff.me:8080*,https://fb.zoff.me:443*,https://fb.zoff.me:8080*,https://admin.zoff.me:443*,https://admin.zoff.me:8080*, http://localhost:8080*")}); |   //"origins": ("https://zoff.me:443*,https://zoff.me:8080*,zoff.me:8080*,https://remote.zoff.me:443*,https://remote.zoff.me:8080*,https://fb.zoff.me:443*,https://fb.zoff.me:8080*,https://admin.zoff.me:443*,https://admin.zoff.me:8080*, http://localhost:8080*")}); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| var socketIO = require(pathThumbnails +'/handlers/io.js'); | var socketIO = require(pathThumbnails + "/handlers/io.js"); | ||||||
| socketIO(); | socketIO(); | ||||||
|  |  | ||||||
| app.socketIO = io; | app.socketIO = io; | ||||||
|  |  | ||||||
|  |  | ||||||
| /* Globally needed "libraries" and files */ | /* Globally needed "libraries" and files */ | ||||||
| var router = require(pathThumbnails + '/routing/client/router.js'); | var router = require(pathThumbnails + "/routing/client/router.js"); | ||||||
| var api_file = require(pathThumbnails + '/routing/client/api.js'); | var api_file = require(pathThumbnails + "/routing/client/api.js"); | ||||||
| var api = api_file.router; | var api = api_file.router; | ||||||
| api_file.sIO = app.socketIO; | api_file.sIO = app.socketIO; | ||||||
| var ico_router = require(pathThumbnails + '/routing/client/icons_routing.js'); | var ico_router = require(pathThumbnails + "/routing/client/icons_routing.js"); | ||||||
|  |  | ||||||
| app.get('/robots.txt', function (req, res) { | app.get("/robots.txt", function(req, res) { | ||||||
|     res.type('text/plain'); |   res.type("text/plain"); | ||||||
|   res.send("User-agent: *\nAllow: /$\nDisallow: /"); |   res.send("User-agent: *\nAllow: /$\nDisallow: /"); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use(function (req, res, next) { | app.use(function(req, res, next) { | ||||||
|   var cookie = req.cookies._uI; |   var cookie = req.cookies._uI; | ||||||
|     var skipElements = ["/_embed", "/assets/manifest.json", "/apple-touch-icon.png"]; |   var skipElements = [ | ||||||
|     if(skipElements.indexOf(req.originalUrl) > -1) { |     "/_embed", | ||||||
|  |     "/assets/manifest.json", | ||||||
|  |     "/apple-touch-icon.png" | ||||||
|  |   ]; | ||||||
|  |   if (skipElements.indexOf(req.originalUrl) > -1) { | ||||||
|     res.header("Access-Control-Allow-Origin", "*"); |     res.header("Access-Control-Allow-Origin", "*"); | ||||||
|         res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); |     res.header( | ||||||
|  |       "Access-Control-Allow-Headers", | ||||||
|  |       "Origin, X-Requested-With, Content-Type, Accept" | ||||||
|  |     ); | ||||||
|     next(); |     next(); | ||||||
|   } else { |   } else { | ||||||
|         if(req.originalUrl.split("/").length > 3) { |     if (req.originalUrl.split("/").length > 3) { | ||||||
|       res.header("Access-Control-Allow-Origin", "*"); |       res.header("Access-Control-Allow-Origin", "*"); | ||||||
|             res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); |       res.header( | ||||||
|  |         "Access-Control-Allow-Headers", | ||||||
|  |         "Origin, X-Requested-With, Content-Type, Accept" | ||||||
|  |       ); | ||||||
|       next(); |       next(); | ||||||
|     } else { |     } else { | ||||||
|       if (cookie === undefined) { |       if (cookie === undefined) { | ||||||
|         try { |         try { | ||||||
|           //console.error((new Date), "originalUrl", req.originalUrl); |           //console.error((new Date), "originalUrl", req.originalUrl); | ||||||
|           //console.error((new Date), "couldn't fetch cookie for some reason, maybe no cookie exists?", req.get('origin'), "couldn't fetch cookie for some reason, maybe no cookie exists?"); |           //console.error((new Date), "couldn't fetch cookie for some reason, maybe no cookie exists?", req.get('origin'), "couldn't fetch cookie for some reason, maybe no cookie exists?"); | ||||||
|  |         } catch (e) { | ||||||
|                 } catch(e) { |  | ||||||
|           //console.error((new Date), "couldn't fetch origin"); |           //console.error((new Date), "couldn't fetch origin"); | ||||||
|         } |         } | ||||||
|                 var user_name = Functions.hash_pass(Functions.rndName(uniqid.time(), 15)); |         var user_name = Functions.hash_pass( | ||||||
|                 res.cookie('_uI', user_name, { |           Functions.rndName(uniqid.time(), 15) | ||||||
|  |         ); | ||||||
|  |         res.cookie("_uI", user_name, { | ||||||
|           maxAge: 365 * 10000 * 3600000, |           maxAge: 365 * 10000 * 3600000, | ||||||
|           httpOnly: true, |           httpOnly: true, | ||||||
|                     secure: secure, |           secure: secure | ||||||
|           //sameSite: true, |           //sameSite: true, | ||||||
|         }); |         }); | ||||||
|       } else { |       } else { | ||||||
|         //process.stderr.write((new Date), "couldn't fetch cookie for some reason, maybe no cookie exists?", req, "couldn't fetch cookie for some reason, maybe no cookie exists?"); |         //process.stderr.write((new Date), "couldn't fetch cookie for some reason, maybe no cookie exists?", req, "couldn't fetch cookie for some reason, maybe no cookie exists?"); | ||||||
|                 res.cookie('_uI', cookie, { |         res.cookie("_uI", cookie, { | ||||||
|           maxAge: 365 * 10000 * 3600000, |           maxAge: 365 * 10000 * 3600000, | ||||||
|           httpOnly: true, |           httpOnly: true, | ||||||
|                     secure: secure, |           secure: secure | ||||||
|           //sameSite: true, |           //sameSite: true, | ||||||
|         }); |         }); | ||||||
|       } |       } | ||||||
|       res.header("Access-Control-Allow-Origin", "*"); |       res.header("Access-Control-Allow-Origin", "*"); | ||||||
|             res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); |       res.header( | ||||||
|  |         "Access-Control-Allow-Headers", | ||||||
|  |         "Origin, X-Requested-With, Content-Type, Accept" | ||||||
|  |       ); | ||||||
|       next(); |       next(); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/service-worker.js', function(req, res) { | app.use("/service-worker.js", function(req, res) { | ||||||
|     res.sendFile(publicPath + '/service-worker.js'); |   res.sendFile(publicPath + "/service-worker.js"); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/', ico_router); | app.use("/", ico_router); | ||||||
| app.use('/', api); | app.use("/", api); | ||||||
| app.use('/', cors(), router); | app.use("/", cors(), router); | ||||||
|  |  | ||||||
| app.use('/assets/js', function(req, res, next) { | app.use("/assets/js", function(req, res, next) { | ||||||
|   res.sendStatus(403); |   res.sendStatus(403); | ||||||
|   return; |   return; | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/assets/admin', function(req, res, next) { | app.use("/assets/admin", function(req, res, next) { | ||||||
|   res.sendStatus(403); |   res.sendStatus(403); | ||||||
|   return; |   return; | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/assets', express.static(publicPath + '/assets')); | app.use("/assets", express.static(publicPath + "/assets")); | ||||||
|  |  | ||||||
| app.use(function (req, res, next) { | app.use(function(req, res, next) { | ||||||
|   res.status(404); |   res.status(404); | ||||||
|   res.redirect("/404"); |   res.redirect("/404"); | ||||||
| }) | }); | ||||||
|  |  | ||||||
| module.exports = app; | module.exports = app; | ||||||
|   | |||||||
| @@ -1,8 +1,8 @@ | |||||||
| var api_key = { | var api_key = { | ||||||
|     "youtube": "xxxx", |   youtube: "xxxx", | ||||||
|     "soundcloud": "xx", |   soundcloud: "xx" // This can be excluded if you don't have a soundcloud key | ||||||
| }; | }; | ||||||
|  |  | ||||||
| try { | try { | ||||||
|   module.exports = api_key; |   module.exports = api_key; | ||||||
| } catch(e) {} | } catch (e) {} | ||||||
|   | |||||||
| @@ -1,42 +1,45 @@ | |||||||
| var toShowConfig = { | var toShowConfig = { | ||||||
|     "addsongs": true, |   addsongs: true, | ||||||
|     "adminpass": 1, |   adminpass: 1, | ||||||
|     "allvideos": 1, |   allvideos: 1, | ||||||
|     "frontpage": 1, |   frontpage: 1, | ||||||
|     "longsongs": 1, |   longsongs: 1, | ||||||
|     "removeplay": 1, |   removeplay: 1, | ||||||
|     "shuffle": 1, |   shuffle: 1, | ||||||
|     "skip": 1, |   skip: 1, | ||||||
|     "startTime": 1, |   startTime: 1, | ||||||
|     "userpass": 1, |   userpass: 1, | ||||||
|     "vote": 1, |   vote: 1, | ||||||
|     "toggleChat": { $ifNull: [ "$toggleChat", true ] }, |   toggleChat: { $ifNull: ["$toggleChat", true] }, | ||||||
|     "strictSkip": { $ifNull: [ "$strictSkip", false ] }, |   strictSkip: { $ifNull: ["$strictSkip", false] }, | ||||||
|     "strictSkipNumber": { $ifNull: [ "$strictSkipNumber", 10 ] }, |   strictSkipNumber: { $ifNull: ["$strictSkipNumber", 10] }, | ||||||
|     "description": { $ifNull: [ "$description", "" ] }, |   description: { $ifNull: ["$description", ""] }, | ||||||
|     "thumbnail": { $ifNull: [ "$thumbnail", "" ] }, |   thumbnail: { $ifNull: ["$thumbnail", ""] }, | ||||||
|     "rules": { $ifNull: [ "$rules", "" ] }, |   rules: { $ifNull: ["$rules", ""] }, | ||||||
|     "_id": 0 |   _id: 0 | ||||||
| }; | }; | ||||||
|  |  | ||||||
| var project_object = { | var project_object = { | ||||||
|     "_id": 0, |   _id: 0, | ||||||
|     "id": 1, |   id: 1, | ||||||
|     "added": 1, |   added: 1, | ||||||
|     "now_playing": 1, |   now_playing: 1, | ||||||
|     "title": 1, |   title: 1, | ||||||
|     "votes": 1, |   votes: 1, | ||||||
|     "start": 1, |   start: 1, | ||||||
|     "duration": 1, |   duration: 1, | ||||||
|     "end": 1, |   end: 1, | ||||||
|     "type": 1, |   type: 1, | ||||||
|     "source": { $ifNull: [ "$source", "youtube" ] }, |   source: { $ifNull: ["$source", "youtube"] }, | ||||||
|     "thumbnail": { |   thumbnail: { | ||||||
|         $ifNull: [ "$thumbnail", { |     $ifNull: [ | ||||||
|             $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] |       "$thumbnail", | ||||||
|         } ] |       { | ||||||
|  |         $concat: ["https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] | ||||||
|  |       } | ||||||
|  |     ] | ||||||
|   }, |   }, | ||||||
|     "tags": { $ifNull: [ "$tags", [] ] }, |   tags: { $ifNull: ["$tags", []] } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| var toShowChannel = { | var toShowChannel = { | ||||||
| @@ -53,7 +56,7 @@ var toShowChannel = { | |||||||
|   now_playing: 1, |   now_playing: 1, | ||||||
|   type: 1, |   type: 1, | ||||||
|   source: 1, |   source: 1, | ||||||
|     thumbnail: 1, |   thumbnail: 1 | ||||||
| }; | }; | ||||||
|  |  | ||||||
| module.exports.project_object = project_object; | module.exports.project_object = project_object; | ||||||
|   | |||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -1,39 +1,84 @@ | |||||||
| var path = require('path'); | var path = require("path"); | ||||||
| try { | try { | ||||||
|     var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js')); |   var mongo_config = require(path.join( | ||||||
| } catch(e) { |     path.join(__dirname, "../config/"), | ||||||
|     console.log("Error - missing file"); |     "mongo_config.js" | ||||||
|     console.log("Seems you forgot to create the file mongo_config.js in /server/config/. Have a look at mongo_config.example.js."); |   )); | ||||||
|  | } catch (e) { | ||||||
|  |   console.log( | ||||||
|  |     "(!) Missing file - /config/mongo_config.js. Have a look at /config/mongo_config.example.js.  The server won't run without this existing." | ||||||
|  |   ); | ||||||
|   process.exit(1); |   process.exit(1); | ||||||
| } | } | ||||||
| var mongojs = require('mongojs'); | var mongojs = require("mongojs"); | ||||||
| var db = mongojs('mongodb://' + mongo_config.host + '/' + mongo_config.config); | var db = mongojs("mongodb://" + mongo_config.host + "/" + mongo_config.config); | ||||||
| var connected_db = mongojs('mongodb://' + mongo_config.host + '/user_credentials'); | var connected_db = mongojs( | ||||||
|  |   "mongodb://" + mongo_config.host + "/user_credentials" | ||||||
|  | ); | ||||||
| var ObjectId = mongojs.ObjectId; | var ObjectId = mongojs.ObjectId; | ||||||
|  |  | ||||||
| db.collection("chat_logs").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 600 }, function(){}); | db.collection("chat_logs").createIndex( | ||||||
| db.collection("timeout_api").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 120 }, function(){}); |   { createdAt: 1 }, | ||||||
| db.collection("api_links").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 86400 }, function(){}); |   { expireAfterSeconds: 600 }, | ||||||
| db.on('connected', function(err) { |   function() {} | ||||||
|  | ); | ||||||
|  | db.collection("timeout_api").createIndex( | ||||||
|  |   { createdAt: 1 }, | ||||||
|  |   { expireAfterSeconds: 120 }, | ||||||
|  |   function() {} | ||||||
|  | ); | ||||||
|  | db.collection("api_links").createIndex( | ||||||
|  |   { createdAt: 1 }, | ||||||
|  |   { expireAfterSeconds: 86400 }, | ||||||
|  |   function() {} | ||||||
|  | ); | ||||||
|  | db.on("connected", function(err) { | ||||||
|   console.log("connected"); |   console.log("connected"); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| db.on('error',function(err) { | db.on("error", function(err) { | ||||||
|   console.log("\n" + new Date().toString() + "\n Database error: ", err); |   console.log("\n" + new Date().toString() + "\n Database error: ", err); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | db.on("error", function(err) { | ||||||
| db.on('error',function(err) { |  | ||||||
|   console.log("\n" + new Date().toString() + "\n Database error: ", err); |   console.log("\n" + new Date().toString() + "\n Database error: ", err); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| /* Resetting usernames, and connected users */ | /* Resetting usernames, and connected users */ | ||||||
| db.collection("unique_ids").update({"_id": "unique_ids"}, {$set: {unique_ids: []}}, {multi: true, upsert: true}, function(err, docs){}); | db.collection("unique_ids").update( | ||||||
| db.collection("user_names").remove({"guid": {$exists: true}}, {multi: true, upsert: true}, function(err, docs){}); |   { _id: "unique_ids" }, | ||||||
| db.collection("user_names").update({"_id": "all_names"}, {$set: {names: []}}, {multi: true, upsert: true}, function(err, docs){}); |   { $set: { unique_ids: [] } }, | ||||||
| db.collection("connected_users").update({users: {$exists: true}}, {$set: {users: []}}, {multi: true, upsert: true}, function(err, docs){}); |   { multi: true, upsert: true }, | ||||||
| db.collection("connected_users").update({"_id": "total_users"}, {$set: {total_users: []}}, {multi: true, upsert: true}, function(err, docs) {}); |   function(err, docs) {} | ||||||
| db.collection("frontpage_lists").update({viewers: {$ne: 0}}, {$set: {"viewers": 0}}, {multi: true, upsert: true}, function(err, docs) {}); | ); | ||||||
|  | db.collection("user_names").remove( | ||||||
|  |   { guid: { $exists: true } }, | ||||||
|  |   { multi: true, upsert: true }, | ||||||
|  |   function(err, docs) {} | ||||||
|  | ); | ||||||
|  | db.collection("user_names").update( | ||||||
|  |   { _id: "all_names" }, | ||||||
|  |   { $set: { names: [] } }, | ||||||
|  |   { multi: true, upsert: true }, | ||||||
|  |   function(err, docs) {} | ||||||
|  | ); | ||||||
|  | db.collection("connected_users").update( | ||||||
|  |   { users: { $exists: true } }, | ||||||
|  |   { $set: { users: [] } }, | ||||||
|  |   { multi: true, upsert: true }, | ||||||
|  |   function(err, docs) {} | ||||||
|  | ); | ||||||
|  | db.collection("connected_users").update( | ||||||
|  |   { _id: "total_users" }, | ||||||
|  |   { $set: { total_users: [] } }, | ||||||
|  |   { multi: true, upsert: true }, | ||||||
|  |   function(err, docs) {} | ||||||
|  | ); | ||||||
|  | db.collection("frontpage_lists").update( | ||||||
|  |   { viewers: { $ne: 0 } }, | ||||||
|  |   { $set: { viewers: 0 } }, | ||||||
|  |   { multi: true, upsert: true }, | ||||||
|  |   function(err, docs) {} | ||||||
|  | ); | ||||||
|  |  | ||||||
| module.exports = db; | module.exports = db; | ||||||
|   | |||||||
| @@ -1,103 +1,142 @@ | |||||||
| var Functions = require(pathThumbnails + '/handlers/functions.js'); | var Functions = require(pathThumbnails + "/handlers/functions.js"); | ||||||
| var db = require(pathThumbnails + '/handlers/db.js'); | var db = require(pathThumbnails + "/handlers/db.js"); | ||||||
| function frontpage_lists(msg, socket) { | function frontpage_lists(msg, socket) { | ||||||
|     if(msg == undefined || !msg.hasOwnProperty('version') || msg.version != VERSION || msg.version == undefined) { |   if ( | ||||||
|  |     msg == undefined || | ||||||
|  |     !msg.hasOwnProperty("version") || | ||||||
|  |     msg.version != VERSION || | ||||||
|  |     msg.version == undefined | ||||||
|  |   ) { | ||||||
|     var result = { |     var result = { | ||||||
|       version: { |       version: { | ||||||
|         expected: VERSION, |         expected: VERSION, | ||||||
|                 got: msg.hasOwnProperty("version") ? msg.version : undefined, |         got: msg.hasOwnProperty("version") ? msg.version : undefined | ||||||
|       } |       } | ||||||
|     }; |     }; | ||||||
|         socket.emit('update_required', result); |     socket.emit("update_required", result); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|     db.collection("frontpage_lists").find({frontpage:true}, function(err, docs){ |   db.collection("frontpage_lists").find({ frontpage: true }, function( | ||||||
|         db.collection("connected_users").find({"_id": "total_users"}, function(err, tot){ |     err, | ||||||
|             socket.compress(true).emit("playlists", {channels: docs, viewers: tot[0].total_users.length}); |     docs | ||||||
|  |   ) { | ||||||
|  |     db.collection("connected_users").find({ _id: "total_users" }, function( | ||||||
|  |       err, | ||||||
|  |       tot | ||||||
|  |     ) { | ||||||
|  |       socket | ||||||
|  |         .compress(true) | ||||||
|  |         .emit("playlists", { | ||||||
|  |           channels: docs, | ||||||
|  |           viewers: tot[0].total_users.length | ||||||
|  |         }); | ||||||
|     }); |     }); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_frontpage_lists(callback) { | function get_frontpage_lists(callback) { | ||||||
|   var project_object = { |   var project_object = { | ||||||
|         "_id": 1, |     _id: 1, | ||||||
|         "count": 1, |     count: 1, | ||||||
|         "frontpage": 1, |     frontpage: 1, | ||||||
|         "id": 1, |     id: 1, | ||||||
|         "title": 1, |     title: 1, | ||||||
|         "viewers": 1, |     viewers: 1, | ||||||
|         "accessed": 1, |     accessed: 1, | ||||||
|         "pinned": { $ifNull: [ "$pinned", 0 ] }, |     pinned: { $ifNull: ["$pinned", 0] }, | ||||||
|         "description": { |     description: { | ||||||
|             $ifNull: [ {$cond: { |       $ifNull: [ | ||||||
|                 "if": { |         { | ||||||
|                     "$or": [ |           $cond: { | ||||||
|                         { "$eq": [ "$description", ""] }, |             if: { | ||||||
|                         { "$eq": [ "$description", null] }, |               $or: [ | ||||||
|                         { "$eq": [ "$description", undefined] } |                 { $eq: ["$description", ""] }, | ||||||
|  |                 { $eq: ["$description", null] }, | ||||||
|  |                 { $eq: ["$description", undefined] } | ||||||
|               ] |               ] | ||||||
|             }, |             }, | ||||||
|             then: "This list has no description", |             then: "This list has no description", | ||||||
|             else: "$description" |             else: "$description" | ||||||
|             }}, "This list has no description"] |           } | ||||||
|  |  | ||||||
|         }, |         }, | ||||||
|         "thumbnail": { |         "This list has no description" | ||||||
|             $ifNull: [ {$cond: { |       ] | ||||||
|                 "if": { |     }, | ||||||
|                     "$or": [ |     thumbnail: { | ||||||
|                         { "$eq": [ "$thumbnail", ""] }, |       $ifNull: [ | ||||||
|                         { "$eq": [ "$thumbnail", null] }, |         { | ||||||
|                         { "$eq": [ "$thumbnail", undefined] } |           $cond: { | ||||||
|  |             if: { | ||||||
|  |               $or: [ | ||||||
|  |                 { $eq: ["$thumbnail", ""] }, | ||||||
|  |                 { $eq: ["$thumbnail", null] }, | ||||||
|  |                 { $eq: ["$thumbnail", undefined] } | ||||||
|               ] |               ] | ||||||
|             }, |             }, | ||||||
|             then: { |             then: { | ||||||
|                     $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] |               $concat: ["https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] | ||||||
|             }, |             }, | ||||||
|             else: "$thumbnail" |             else: "$thumbnail" | ||||||
|             }}, { $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"]}] |           } | ||||||
|  |         }, | ||||||
|  |         { $concat: ["https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] } | ||||||
|  |       ] | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
|     db.collection("frontpage_lists").aggregate([ |   db.collection("frontpage_lists").aggregate( | ||||||
|  |     [ | ||||||
|       { |       { | ||||||
|             "$match": { |         $match: { | ||||||
|           frontpage: true, |           frontpage: true, | ||||||
|                 count: {$gt: 3}, |           count: { $gt: 3 } | ||||||
|         } |         } | ||||||
|       }, |       }, | ||||||
|       { |       { | ||||||
|             "$project": project_object |         $project: project_object | ||||||
|       }, |       }, | ||||||
|       { |       { | ||||||
|             "$sort" : { |         $sort: { | ||||||
|                 "pinned": -1, |           pinned: -1, | ||||||
|                 "viewers": -1, |           viewers: -1, | ||||||
|                 "accessed": -1, |           accessed: -1, | ||||||
|                 "count": -1, |           count: -1, | ||||||
|                 "title": 1 |           title: 1 | ||||||
|         } |         } | ||||||
|         }, |       } | ||||||
|     ], callback); |     ], | ||||||
|  |     callback | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function update_frontpage(coll, id, title, thumbnail, source, callback) { | function update_frontpage(coll, id, title, thumbnail, source, callback) { | ||||||
|   //coll = coll.replace(/ /g,''); |   //coll = coll.replace(/ /g,''); | ||||||
|     db.collection("frontpage_lists").find({_id: coll}, function(e, doc) { |   db.collection("frontpage_lists").find({ _id: coll }, function(e, doc) { | ||||||
|     var updateObject = { |     var updateObject = { | ||||||
|       id: id, |       id: id, | ||||||
|       title: title, |       title: title, | ||||||
|       accessed: Functions.get_time() |       accessed: Functions.get_time() | ||||||
|     }; |     }; | ||||||
| if(doc.length > 0 && ((doc[0].thumbnail != "" && doc[0].thumbnail != undefined && (doc[0].thumbnail.indexOf("https://i1.sndcdn.com") > -1 || doc[0].thumbnail.indexOf("https://w1.sndcdn.com") > -1 || doc[0].thumbnail.indexOf("https://img.youtube.com") > -1)) || (doc[0].thumbnail == "" || doc[0].thumbnail == undefined))) { |     if ( | ||||||
|  |       doc.length > 0 && | ||||||
|  |       ((doc[0].thumbnail != "" && | ||||||
|  |         doc[0].thumbnail != undefined && | ||||||
|  |         (doc[0].thumbnail.indexOf("https://i1.sndcdn.com") > -1 || | ||||||
|  |           doc[0].thumbnail.indexOf("https://w1.sndcdn.com") > -1 || | ||||||
|  |           doc[0].thumbnail.indexOf("https://img.youtube.com") > -1)) || | ||||||
|  |         (doc[0].thumbnail == "" || doc[0].thumbnail == undefined)) | ||||||
|  |     ) { | ||||||
|       updateObject.thumbnail = thumbnail; |       updateObject.thumbnail = thumbnail; | ||||||
|             if(thumbnail == undefined) updateObject.thumbnail = ""; |       if (thumbnail == undefined) updateObject.thumbnail = ""; | ||||||
|     } |     } | ||||||
|         db.collection("frontpage_lists").update({_id: coll}, {$set: updateObject |     db.collection("frontpage_lists").update( | ||||||
|         },{upsert: true}, function(err, returnDocs){ |       { _id: coll }, | ||||||
|             if(typeof(callback) == "function") callback(); |       { $set: updateObject }, | ||||||
|         }); |       { upsert: true }, | ||||||
|  |       function(err, returnDocs) { | ||||||
|  |         if (typeof callback == "function") callback(); | ||||||
|  |       } | ||||||
|  |     ); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,20 +1,26 @@ | |||||||
| var path = require('path'); | var path = require("path"); | ||||||
| try { | try { | ||||||
|     var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js')); |   var mongo_config = require(path.join( | ||||||
| } catch(e) { |     path.join(__dirname, "../config/"), | ||||||
|     console.log("Error - missing file"); |     "mongo_config.js" | ||||||
|     console.log("Seems you forgot to create the file mongo_config.js in /server/config/. Have a look at mongo_config.example.js."); |   )); | ||||||
|  | } catch (e) { | ||||||
|  |   console.log( | ||||||
|  |     "(!) Missing file - /config/mongo_config.js. Have a look at /config/mongo_config.example.js.  The server won't run without this existing." | ||||||
|  |   ); | ||||||
|   process.exit(1); |   process.exit(1); | ||||||
| } | } | ||||||
| var mongojs = require('mongojs'); | var mongojs = require("mongojs"); | ||||||
| var connected_db = mongojs('mongodb://' + mongo_config.host + '/user_credentials'); | var connected_db = mongojs( | ||||||
| var crypto = require('crypto'); |   "mongodb://" + mongo_config.host + "/user_credentials" | ||||||
| var db = require(pathThumbnails + '/handlers/db.js'); | ); | ||||||
| var uniqid = require('uniqid'); | var crypto = require("crypto"); | ||||||
| var Filter = require('bad-words'); | var db = require(pathThumbnails + "/handlers/db.js"); | ||||||
| var filter = new Filter({ placeHolder: 'x'}); | var uniqid = require("uniqid"); | ||||||
|  | var Filter = require("bad-words"); | ||||||
|  | var filter = new Filter({ placeHolder: "x" }); | ||||||
|  |  | ||||||
| var Chat = require(pathThumbnails + '/handlers/chat.js'); | var Chat = require(pathThumbnails + "/handlers/chat.js"); | ||||||
|  |  | ||||||
| function encodeChannelName(str) { | function encodeChannelName(str) { | ||||||
|   var _fn = encodeURIComponent; |   var _fn = encodeURIComponent; | ||||||
| @@ -36,34 +42,55 @@ function decodeChannelName(str) { | |||||||
| } | } | ||||||
|  |  | ||||||
| function remove_unique_id(short_id) { | function remove_unique_id(short_id) { | ||||||
|     db.collection("unique_ids").update({"_id": "unique_ids"}, {$pull: {unique_ids: short_id}}, function(err, docs) {}); |   db.collection("unique_ids").update( | ||||||
|  |     { _id: "unique_ids" }, | ||||||
|  |     { $pull: { unique_ids: short_id } }, | ||||||
|  |     function(err, docs) {} | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function remove_name_from_db(guid, channel) { | function remove_name_from_db(guid, channel) { | ||||||
|   // Use temporary, with caution. Can bottleneck in large quantity of users. |   // Use temporary, with caution. Can bottleneck in large quantity of users. | ||||||
|   // |   // | ||||||
|   // Find a way of indexing users in lists in a clever way, to avoid the search here |   // Find a way of indexing users in lists in a clever way, to avoid the search here | ||||||
|     db.collection("connected_users").find({"_id": "total_users"}, function(err, all_users) { |   db.collection("connected_users").find({ _id: "total_users" }, function( | ||||||
|         var hasOne = all_users[0].total_users.some(function(v){ return v.indexOf(guid)>=0 }); |     err, | ||||||
|         if(!hasOne) { |     all_users | ||||||
|             db.collection("user_names").find({"guid": guid}, function(err, user){ |   ) { | ||||||
|                 if(user.length == 1){ |     var hasOne = all_users[0].total_users.some(function(v) { | ||||||
|                     db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: user[0].name}}, function(err, updated) { |       return v.indexOf(guid) >= 0; | ||||||
|                         db.collection("user_names").remove({"guid": guid}, function(err, removed) {	}); |  | ||||||
|     }); |     }); | ||||||
|  |     if (!hasOne) { | ||||||
|  |       db.collection("user_names").find({ guid: guid }, function(err, user) { | ||||||
|  |         if (user.length == 1) { | ||||||
|  |           db.collection("user_names").update( | ||||||
|  |             { _id: "all_names" }, | ||||||
|  |             { $pull: { names: user[0].name } }, | ||||||
|  |             function(err, updated) { | ||||||
|  |               db.collection("user_names").remove({ guid: guid }, function( | ||||||
|  |                 err, | ||||||
|  |                 removed | ||||||
|  |               ) {}); | ||||||
|  |             } | ||||||
|  |           ); | ||||||
|         } |         } | ||||||
|       }); |       }); | ||||||
|     } else { |     } else { | ||||||
|             if(channel == undefined || channel == "") return; |       if (channel == undefined || channel == "") return; | ||||||
|             db.collection("user_names").update({"guid": guid}, {$pull: {channels: channel}}, function(err, docs) { |       db.collection("user_names").update( | ||||||
|  |         { guid: guid }, | ||||||
|  |         { $pull: { channels: channel } }, | ||||||
|  |         function(err, docs) { | ||||||
|           //console.log("Pulled user from current channel"); |           //console.log("Pulled user from current channel"); | ||||||
|             }); |         } | ||||||
|  |       ); | ||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
| function isUrl(str) { | function isUrl(str) { | ||||||
|     var pattern = new RegExp("\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" + |   var pattern = new RegExp( | ||||||
|  |     "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" + | ||||||
|       "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" + |       "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" + | ||||||
|       "|mil|biz|info|mobi|name|aero|jobs|museum" + |       "|mil|biz|info|mobi|name|aero|jobs|museum" + | ||||||
|       "|travel|[a-z]{2}))(:[\\d]{1,5})?" + |       "|travel|[a-z]{2}))(:[\\d]{1,5})?" + | ||||||
| @@ -72,8 +99,9 @@ function isUrl(str) { | |||||||
|       "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" + |       "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" + | ||||||
|       "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" + |       "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" + | ||||||
|       "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" + |       "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" + | ||||||
|     "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b"); |       "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b" | ||||||
|     if(!pattern.test(str)) { |   ); | ||||||
|  |   if (!pattern.test(str)) { | ||||||
|     return false; |     return false; | ||||||
|   } else { |   } else { | ||||||
|     return true; |     return true; | ||||||
| @@ -86,32 +114,39 @@ function getSession(socket) { | |||||||
|         var cookie = require("cookie"); |         var cookie = require("cookie"); | ||||||
|         var parsedCookies = cookie.parse(socket.handshake.headers.cookie); |         var parsedCookies = cookie.parse(socket.handshake.headers.cookie); | ||||||
|         return parsedCookies["_uI"];*/ |         return parsedCookies["_uI"];*/ | ||||||
|         if(socket.cookie_id == undefined) throw "Undefined error"; |     if (socket.cookie_id == undefined) throw "Undefined error"; | ||||||
|     return socket.cookie_id; |     return socket.cookie_id; | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     // Returning "sessiong"-based on place of connection |     // Returning "sessiong"-based on place of connection | ||||||
|         return hash_pass(socket.handshake.headers["user-agent"] + socket.handshake.address + socket.handshake.headers["accept-language"]); |     return hash_pass( | ||||||
|  |       socket.handshake.headers["user-agent"] + | ||||||
|  |         socket.handshake.address + | ||||||
|  |         socket.handshake.headers["accept-language"] | ||||||
|  |     ); | ||||||
|     //return "empty"; |     //return "empty"; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function remove_from_array(array, element){ | function remove_from_array(array, element) { | ||||||
|     if(contains(array, element)){ |   if (contains(array, element)) { | ||||||
|     var index = array.indexOf(element); |     var index = array.indexOf(element); | ||||||
|         if(index != -1) |     if (index != -1) array.splice(index, 1); | ||||||
|         array.splice(index, 1); |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function generate_channel_name(res) { | function generate_channel_name(res) { | ||||||
|   var trying_id = uniqid.time().toLowerCase(); |   var trying_id = uniqid.time().toLowerCase(); | ||||||
|     db.collection("frontpage_lists").find({frontpage: {$exists: true }, "_id": trying_id }, {"_id": 1}, function(err, docs){ |   db.collection("frontpage_lists").find( | ||||||
|         if(docs.length == 0) { |     { frontpage: { $exists: true }, _id: trying_id }, | ||||||
|  |     { _id: 1 }, | ||||||
|  |     function(err, docs) { | ||||||
|  |       if (docs.length == 0) { | ||||||
|         res.send(trying_id); |         res.send(trying_id); | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|       generate_channel_name(res); |       generate_channel_name(res); | ||||||
|     }); |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_short_id(socket) { | function get_short_id(socket) { | ||||||
| @@ -121,73 +156,151 @@ function get_short_id(socket) { | |||||||
|   socket.emit("id", new_short_id); |   socket.emit("id", new_short_id); | ||||||
| } | } | ||||||
|  |  | ||||||
| function check_inlist(coll, guid, socket, offline, callback, double_check) | function check_inlist(coll, guid, socket, offline, callback, double_check) { | ||||||
| { |   if (coll == undefined) { | ||||||
|     if(coll == undefined) { |     if (typeof callback == "function") callback(); | ||||||
|         if(typeof(callback) == "function") callback(); |  | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   //coll = coll.replace(/ /g,''); |   //coll = coll.replace(/ /g,''); | ||||||
|     if(!offline && coll != undefined){ |   if (!offline && coll != undefined) { | ||||||
|         db.collection("connected_users").update({"_id": coll}, {$addToSet:{users: guid}}, {upsert: true}, function(err, updated) { |     db.collection("connected_users").update( | ||||||
|             if(updated.nModified > 0 || updated.upserted != undefined) { |       { _id: coll }, | ||||||
|                 db.collection("connected_users").find({"_id": coll}, function(err, new_doc) { |       { $addToSet: { users: guid } }, | ||||||
|                     db.collection("frontpage_lists").update({"_id": coll}, {$set: {"viewers": new_doc[0].users.length}}, function(){ |       { upsert: true }, | ||||||
|                         if(new_doc[0].users == undefined || new_doc[0].users.length == undefined) { |       function(err, updated) { | ||||||
|  |         if (updated.nModified > 0 || updated.upserted != undefined) { | ||||||
|  |           db.collection("connected_users").find({ _id: coll }, function( | ||||||
|  |             err, | ||||||
|  |             new_doc | ||||||
|  |           ) { | ||||||
|  |             db.collection("frontpage_lists").update( | ||||||
|  |               { _id: coll }, | ||||||
|  |               { $set: { viewers: new_doc[0].users.length } }, | ||||||
|  |               function() { | ||||||
|  |                 if ( | ||||||
|  |                   new_doc[0].users == undefined || | ||||||
|  |                   new_doc[0].users.length == undefined | ||||||
|  |                 ) { | ||||||
|                   io.to(coll).emit("viewers", 1); |                   io.to(coll).emit("viewers", 1); | ||||||
|                 } else { |                 } else { | ||||||
|                   io.to(coll).emit("viewers", new_doc[0].users.length); |                   io.to(coll).emit("viewers", new_doc[0].users.length); | ||||||
|                 } |                 } | ||||||
|                         Chat.namechange({initial: true, first:true, channel: coll}, guid, socket, false, function(enabled) { |                 Chat.namechange( | ||||||
|                             db.collection("user_names").find({"guid": guid}, function(err, docs) { |                   { initial: true, first: true, channel: coll }, | ||||||
|                                 if(docs.length == 1) { |                   guid, | ||||||
|  |                   socket, | ||||||
|  |                   false, | ||||||
|  |                   function(enabled) { | ||||||
|  |                     db.collection("user_names").find({ guid: guid }, function( | ||||||
|  |                       err, | ||||||
|  |                       docs | ||||||
|  |                     ) { | ||||||
|  |                       if (docs.length == 1) { | ||||||
|                         var icon = ""; |                         var icon = ""; | ||||||
|                                     if(docs[0].icon != undefined) icon = docs[0].icon; |                         if (docs[0].icon != undefined) icon = docs[0].icon; | ||||||
|                                     db.collection("user_names").update({"guid": guid}, {$addToSet:{channels: coll}}, function(err, doc){}); |                         db.collection("user_names").update( | ||||||
|                                     if(enabled) { |                           { guid: guid }, | ||||||
|                                         socket.broadcast.to(coll).emit('chat', {from: docs[0].name, icon: icon, msg: " joined"}); |                           { $addToSet: { channels: coll } }, | ||||||
|  |                           function(err, doc) {} | ||||||
|  |                         ); | ||||||
|  |                         if (enabled) { | ||||||
|  |                           socket.broadcast.to(coll).emit("chat", { | ||||||
|  |                             from: docs[0].name, | ||||||
|  |                             icon: icon, | ||||||
|  |                             msg: " joined" | ||||||
|  |                           }); | ||||||
|                         } |                         } | ||||||
|                                 } else if(docs.length == 0) { |                       } else if (docs.length == 0) { | ||||||
|                         //console.log("User doesn't have a name for some reason."); |                         //console.log("User doesn't have a name for some reason."); | ||||||
|                         //console.log("guid", guid); |                         //console.log("guid", guid); | ||||||
|                         //console.log("channel", coll); |                         //console.log("channel", coll); | ||||||
|                         //console.log("Trying to get a chat-name"); |                         //console.log("Trying to get a chat-name"); | ||||||
|                                     Chat.get_name(guid, {announce: false, socket: socket, channel: coll}); |                         Chat.get_name(guid, { | ||||||
|  |                           announce: false, | ||||||
|  |                           socket: socket, | ||||||
|  |                           channel: coll | ||||||
|  |                         }); | ||||||
|                       } |                       } | ||||||
|                     }); |                     }); | ||||||
|                             db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){ |                     db.collection("connected_users").update( | ||||||
|                                 if(callback != undefined && typeof(callback) == "function") callback(); |                       { _id: "total_users" }, | ||||||
|                             }); |                       { $addToSet: { total_users: guid + coll } }, | ||||||
|                         }); |                       function(err, docs) { | ||||||
|                     }); |                         if ( | ||||||
|  |                           callback != undefined && | ||||||
|  |                           typeof callback == "function" | ||||||
|  |                         ) | ||||||
|  |                           callback(); | ||||||
|  |                       } | ||||||
|  |                     ); | ||||||
|  |                   } | ||||||
|  |                 ); | ||||||
|  |               } | ||||||
|  |             ); | ||||||
|           }); |           }); | ||||||
|         } else { |         } else { | ||||||
|                 db.collection("connected_users").find({"_id": coll}, function(err, new_doc) { |           db.collection("connected_users").find({ _id: coll }, function( | ||||||
|  |             err, | ||||||
|  |             new_doc | ||||||
|  |           ) { | ||||||
|             io.to(coll).emit("viewers", new_doc[0].users.length); |             io.to(coll).emit("viewers", new_doc[0].users.length); | ||||||
|           }); |           }); | ||||||
|                 if(callback != undefined && typeof(callback) == "function") callback(); |           if (callback != undefined && typeof callback == "function") | ||||||
|  |             callback(); | ||||||
|         } |         } | ||||||
|         }); |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|         if(offline) { |     if (offline) { | ||||||
|             db.collection("connected_users").update({"_id": "offline_users"}, {$addToSet: {users: guid}}, function(err, docs){}); |       db.collection("connected_users").update( | ||||||
|  |         { _id: "offline_users" }, | ||||||
|  |         { $addToSet: { users: guid } }, | ||||||
|  |         function(err, docs) {} | ||||||
|  |       ); | ||||||
|     } else { |     } else { | ||||||
|             db.collection("connected_users").update({"_id": coll}, {$addToSet: {users: guid}}, function(err, docs){}); |       db.collection("connected_users").update( | ||||||
|  |         { _id: coll }, | ||||||
|  |         { $addToSet: { users: guid } }, | ||||||
|  |         function(err, docs) {} | ||||||
|  |       ); | ||||||
|     } |     } | ||||||
|     // |     // | ||||||
|         if(coll != undefined && coll != "") { |     if (coll != undefined && coll != "") { | ||||||
|             db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs) {}); |       db.collection("connected_users").update( | ||||||
|  |         { _id: "total_users" }, | ||||||
|  |         { $addToSet: { total_users: guid + coll } }, | ||||||
|  |         function(err, docs) {} | ||||||
|  |       ); | ||||||
|     } |     } | ||||||
|         if(callback != undefined && typeof(callback) == "function") callback(); |     if (callback != undefined && typeof callback == "function") callback(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function rndName(seed, len) { | function rndName(seed, len) { | ||||||
|     var vowels = ['a', 'e', 'i', 'o', 'u']; |   var vowels = ["a", "e", "i", "o", "u"]; | ||||||
|     consts =  ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y']; |   consts = [ | ||||||
|  |     "b", | ||||||
|  |     "c", | ||||||
|  |     "d", | ||||||
|  |     "f", | ||||||
|  |     "g", | ||||||
|  |     "h", | ||||||
|  |     "j", | ||||||
|  |     "k", | ||||||
|  |     "l", | ||||||
|  |     "m", | ||||||
|  |     "n", | ||||||
|  |     "p", | ||||||
|  |     "r", | ||||||
|  |     "s", | ||||||
|  |     "t", | ||||||
|  |     "v", | ||||||
|  |     "w", | ||||||
|  |     "x", | ||||||
|  |     "y" | ||||||
|  |   ]; | ||||||
|   len = Math.floor(len); |   len = Math.floor(len); | ||||||
|     word = ''; |   word = ""; | ||||||
|   is_vowel = false; |   is_vowel = false; | ||||||
|   var arr; |   var arr; | ||||||
|   try { |   try { | ||||||
| @@ -195,37 +308,36 @@ function rndName(seed, len) { | |||||||
|       if (is_vowel) arr = vowels; |       if (is_vowel) arr = vowels; | ||||||
|       else arr = consts; |       else arr = consts; | ||||||
|       is_vowel = !is_vowel; |       is_vowel = !is_vowel; | ||||||
|             word += arr[(seed[i%seed.length].charCodeAt()+i) % (arr.length-1)]; |       word += arr[(seed[i % seed.length].charCodeAt() + i) % (arr.length - 1)]; | ||||||
|     } |     } | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     return rndName(uniqid.time().toLowerCase(), len); |     return rndName(uniqid.time().toLowerCase(), len); | ||||||
|   } |   } | ||||||
|   return word; |   return word; | ||||||
| } | } | ||||||
|  |  | ||||||
| function removeEmojis (string) { | function removeEmojis(string) { | ||||||
|   //https://stackoverflow.com/a/41164278/4266467 |   //https://stackoverflow.com/a/41164278/4266467 | ||||||
|   var regex = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g; |   var regex = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g; | ||||||
|     return string.replace(regex, ''); |   return string.replace(regex, ""); | ||||||
| } | } | ||||||
|  |  | ||||||
| function decrypt_string(pw){ | function decrypt_string(pw) { | ||||||
|   try { |   try { | ||||||
|         return Buffer.from(pw, 'base64').toString('ascii') |     return Buffer.from(pw, "base64").toString("ascii"); | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     return ""; |     return ""; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_time() | function get_time() { | ||||||
| { |  | ||||||
|   var d = new Date(); |   var d = new Date(); | ||||||
|   var time = Math.floor(d.getTime() / 1000); |   var time = Math.floor(d.getTime() / 1000); | ||||||
|   return time; |   return time; | ||||||
| } | } | ||||||
|  |  | ||||||
| function contains(a, obj) { | function contains(a, obj) { | ||||||
|     try{ |   try { | ||||||
|     var i = a.length; |     var i = a.length; | ||||||
|     while (i--) { |     while (i--) { | ||||||
|       if (a[i] === obj) { |       if (a[i] === obj) { | ||||||
| @@ -233,44 +345,63 @@ function contains(a, obj) { | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     return false; |     return false; | ||||||
|     }catch(e){ |   } catch (e) { | ||||||
|     return false; |     return false; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function hash_pass(adminpass, hex) { | function hash_pass(adminpass, hex) { | ||||||
|     if(adminpass == undefined || adminpass == "") return ""; |   if (adminpass == undefined || adminpass == "") return ""; | ||||||
|     if(hex) return crypto.createHash('sha256').update(adminpass).digest('hex'); |   if (hex) | ||||||
|     return crypto.createHash('sha256').update(adminpass).digest('base64'); |     return crypto | ||||||
|  |       .createHash("sha256") | ||||||
|  |       .update(adminpass) | ||||||
|  |       .digest("hex"); | ||||||
|  |   return crypto | ||||||
|  |     .createHash("sha256") | ||||||
|  |     .update(adminpass) | ||||||
|  |     .digest("base64"); | ||||||
| } | } | ||||||
|  |  | ||||||
| function setSessionAdminPass(id, adminpass, list, callback) { | function setSessionAdminPass(id, adminpass, list, callback) { | ||||||
|   try { |   try { | ||||||
|         if(id == "empty" || id == undefined) { |     if (id == "empty" || id == undefined) { | ||||||
|       callback(); |       callback(); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         connected_db.collection(id).update({_id: list}, {$set: {adminpass: hash_pass(decrypt_string(adminpass), true)}}, {upsert: true}, function(e, d){ |     connected_db | ||||||
|  |       .collection(id) | ||||||
|  |       .update( | ||||||
|  |         { _id: list }, | ||||||
|  |         { $set: { adminpass: hash_pass(decrypt_string(adminpass), true) } }, | ||||||
|  |         { upsert: true }, | ||||||
|  |         function(e, d) { | ||||||
|           callback(); |           callback(); | ||||||
|           return; |           return; | ||||||
|         }); |  | ||||||
|     } catch(e) { |  | ||||||
|  |  | ||||||
|         } |         } | ||||||
|  |       ); | ||||||
|  |   } catch (e) {} | ||||||
| } | } | ||||||
|  |  | ||||||
| function setSessionChatPass(id, name, pass, callback) { | function setSessionChatPass(id, name, pass, callback) { | ||||||
|   try { |   try { | ||||||
|         if(id == "empty" || id == undefined) { |     if (id == "empty" || id == undefined) { | ||||||
|       callback(); |       callback(); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|         connected_db.collection(id).update({_id: "_chat_"}, {$set: {password: pass, name: name}}, {upsert: true}, function(e) { |     connected_db | ||||||
|  |       .collection(id) | ||||||
|  |       .update( | ||||||
|  |         { _id: "_chat_" }, | ||||||
|  |         { $set: { password: pass, name: name } }, | ||||||
|  |         { upsert: true }, | ||||||
|  |         function(e) { | ||||||
|           callback(); |           callback(); | ||||||
|           return; |           return; | ||||||
|         }) |         } | ||||||
|     } catch(e) { |       ); | ||||||
|  |   } catch (e) { | ||||||
|     callback(); |     callback(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| @@ -278,119 +409,141 @@ function setSessionChatPass(id, name, pass, callback) { | |||||||
|  |  | ||||||
| function getSessionChatPass(id, callback) { | function getSessionChatPass(id, callback) { | ||||||
|   try { |   try { | ||||||
|         if(id == "empty" || id == undefined) { |     if (id == "empty" || id == undefined) { | ||||||
|       callback("", "", false); |       callback("", "", false); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         connected_db.collection(id).find({_id: "_chat_"}, function(e, d) { |     connected_db.collection(id).find({ _id: "_chat_" }, function(e, d) { | ||||||
|             if(d.length > 0) { |       if (d.length > 0) { | ||||||
|         var name = ""; |         var name = ""; | ||||||
|         var pass = ""; |         var pass = ""; | ||||||
|                 if(d[0].name != undefined) name = d[0].name; |         if (d[0].name != undefined) name = d[0].name; | ||||||
|                 if(d[0].password != undefined) pass = d[0].password; |         if (d[0].password != undefined) pass = d[0].password; | ||||||
|         callback(name, pass); |         callback(name, pass); | ||||||
|         return; |         return; | ||||||
|       } else { |       } else { | ||||||
|         callback("", "", false); |         callback("", "", false); | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|         }) |     }); | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     callback(); |     callback(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| function setChromecastHost(id, other_id, list, callback) { | function setChromecastHost(id, other_id, list, callback) { | ||||||
|   try { |   try { | ||||||
|         if(id == "empty" || id == undefined || other_id == "empty" || other_id == undefined) { |     if ( | ||||||
|  |       id == "empty" || | ||||||
|  |       id == undefined || | ||||||
|  |       other_id == "empty" || | ||||||
|  |       other_id == undefined | ||||||
|  |     ) { | ||||||
|       callback(); |       callback(); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|         connected_db.collection(id).update({_id: list}, {"chromecast": true, id: other_id}, {upsert: true}, function(e, docs) { |     connected_db | ||||||
|  |       .collection(id) | ||||||
|  |       .update( | ||||||
|  |         { _id: list }, | ||||||
|  |         { chromecast: true, id: other_id }, | ||||||
|  |         { upsert: true }, | ||||||
|  |         function(e, docs) { | ||||||
|           callback(true); |           callback(true); | ||||||
|           return; |           return; | ||||||
|         }); |         } | ||||||
|     } catch(e) { |       ); | ||||||
|  |   } catch (e) { | ||||||
|     callback(false); |     callback(false); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function setSessionUserPass(id, userpass, list, callback) { | function setSessionUserPass(id, userpass, list, callback) { | ||||||
|   try { |   try { | ||||||
|         if(id == "empty" || id == undefined || userpass == undefined) { |     if (id == "empty" || id == undefined || userpass == undefined) { | ||||||
|       callback(); |       callback(); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         connected_db.collection(id).update({_id: list}, {$set: {userpass: userpass}}, {upsert: true}, function(e, d){ |     connected_db | ||||||
|  |       .collection(id) | ||||||
|  |       .update( | ||||||
|  |         { _id: list }, | ||||||
|  |         { $set: { userpass: userpass } }, | ||||||
|  |         { upsert: true }, | ||||||
|  |         function(e, d) { | ||||||
|           callback(); |           callback(); | ||||||
|           return; |           return; | ||||||
|         }); |         } | ||||||
|     } catch(e) { |       ); | ||||||
|  |   } catch (e) { | ||||||
|     callback(); |     callback(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function getSessionAdminUser(id, list, callback) { | function getSessionAdminUser(id, list, callback) { | ||||||
|   try { |   try { | ||||||
|         if(id == "empty" || id == undefined) { |     if (id == "empty" || id == undefined) { | ||||||
|       callback("", "", false); |       callback("", "", false); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|         connected_db.collection(id).find({_id: list}, function(e, d) { |     connected_db.collection(id).find({ _id: list }, function(e, d) { | ||||||
|       var userpass = ""; |       var userpass = ""; | ||||||
|       var adminpass = ""; |       var adminpass = ""; | ||||||
|             if(d.length > 0) { |       if (d.length > 0) { | ||||||
|                 if(d[0].hasOwnProperty("chromecast") && d[0].chromecast) { |         if (d[0].hasOwnProperty("chromecast") && d[0].chromecast) { | ||||||
|           getSessionAdminUser(d[0].id, list, callback); |           getSessionAdminUser(d[0].id, list, callback); | ||||||
|         } else { |         } else { | ||||||
|                     if(d[0].userpass != undefined) userpass = d[0].userpass; |           if (d[0].userpass != undefined) userpass = d[0].userpass; | ||||||
|                     if(d[0].adminpass != undefined) adminpass = d[0].adminpass; |           if (d[0].adminpass != undefined) adminpass = d[0].adminpass; | ||||||
|           callback(userpass, adminpass, true); |           callback(userpass, adminpass, true); | ||||||
|         } |         } | ||||||
|       } else { |       } else { | ||||||
|         callback(userpass, adminpass, true); |         callback(userpass, adminpass, true); | ||||||
|       } |       } | ||||||
|         }) |     }); | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     callback("", "", false); |     callback("", "", false); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function removeSessionChatPass(id, callback) { | function removeSessionChatPass(id, callback) { | ||||||
|     if(id == "empty" || id == undefined) { |   if (id == "empty" || id == undefined) { | ||||||
|     callback(); |     callback(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|     connected_db.collection(id).remove({_id: "_chat_"}, function() { |   connected_db.collection(id).remove({ _id: "_chat_" }, function() { | ||||||
|     callback(); |     callback(); | ||||||
|     return; |     return; | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| function removeSessionAdminPass(id, channel, callback) { | function removeSessionAdminPass(id, channel, callback) { | ||||||
|     if(id == "empty" || id == undefined) { |   if (id == "empty" || id == undefined) { | ||||||
|     callback(); |     callback(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|     connected_db.collection(id).update({_id: channel}, {$set: {"adminpass": ""}}, function() { |   connected_db | ||||||
|  |     .collection(id) | ||||||
|  |     .update({ _id: channel }, { $set: { adminpass: "" } }, function() { | ||||||
|       callback(); |       callback(); | ||||||
|       return; |       return; | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
| function remove_from_chat_channel(coll, guid) { | function remove_from_chat_channel(coll, guid) { | ||||||
|     db.collection("user_names").update({"guid": guid}, {$pull: {channels: coll}}, function(err, docs) { |   db.collection("user_names").update( | ||||||
|     }); |     { guid: guid }, | ||||||
|  |     { $pull: { channels: coll } }, | ||||||
|  |     function(err, docs) {} | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function left_channel(coll, guid, short_id, in_list, socket, change, caller) { | function left_channel(coll, guid, short_id, in_list, socket, change, caller) { | ||||||
|     if(!coll) { |   if (!coll) { | ||||||
|         if(!change) { |     if (!change) { | ||||||
|       remove_name_from_db(guid, coll); |       remove_name_from_db(guid, coll); | ||||||
|     } else { |     } else { | ||||||
|       remove_from_chat_channel(coll, guid); |       remove_from_chat_channel(coll, guid); | ||||||
| @@ -398,67 +551,111 @@ function left_channel(coll, guid, short_id, in_list, socket, change, caller) { | |||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   //coll = coll.replace(/ /g,''); |   //coll = coll.replace(/ /g,''); | ||||||
|     db.collection("connected_users").update({"_id": coll}, {$pull: {users: guid}}, function(err, updated) { |   db.collection("connected_users").update( | ||||||
|         if(updated.nModified > 0) { |     { _id: coll }, | ||||||
|             db.collection("connected_users").update({"_id": "total_users"}, {$pull: {total_users: guid + coll}}, function(err, updated){}); |     { $pull: { users: guid } }, | ||||||
|             db.collection("connected_users").find({"_id": coll}, function(err, new_doc){ |     function(err, updated) { | ||||||
|                 db.collection("frontpage_lists").update({"_id": coll, viewers: {$gt: 0}}, {$inc: {viewers: -1}}, function(err, doc) { |       if (updated.nModified > 0) { | ||||||
|                     db.collection("user_names").find({"guid": guid}, function(err, docs) { |         db.collection("connected_users").update( | ||||||
|                         if(docs.length == 1) { |           { _id: "total_users" }, | ||||||
|  |           { $pull: { total_users: guid + coll } }, | ||||||
|  |           function(err, updated) {} | ||||||
|  |         ); | ||||||
|  |         db.collection("connected_users").find({ _id: coll }, function( | ||||||
|  |           err, | ||||||
|  |           new_doc | ||||||
|  |         ) { | ||||||
|  |           db.collection("frontpage_lists").update( | ||||||
|  |             { _id: coll, viewers: { $gt: 0 } }, | ||||||
|  |             { $inc: { viewers: -1 } }, | ||||||
|  |             function(err, doc) { | ||||||
|  |               db.collection("user_names").find({ guid: guid }, function( | ||||||
|  |                 err, | ||||||
|  |                 docs | ||||||
|  |               ) { | ||||||
|  |                 if (docs.length == 1) { | ||||||
|                   var icon = ""; |                   var icon = ""; | ||||||
|                             if(docs[0].icon != undefined) icon = docs[0].icon; |                   if (docs[0].icon != undefined) icon = docs[0].icon; | ||||||
|                             io.to(coll).emit('chat', {from: docs[0].name, icon: icon, msg: " left"}); |                   io.to(coll).emit("chat", { | ||||||
|  |                     from: docs[0].name, | ||||||
|  |                     icon: icon, | ||||||
|  |                     msg: " left" | ||||||
|  |                   }); | ||||||
|                 } |                 } | ||||||
|               }); |               }); | ||||||
|               io.to(coll).emit("viewers", new_doc[0].users.length); |               io.to(coll).emit("viewers", new_doc[0].users.length); | ||||||
|               socket.leave(coll); |               socket.leave(coll); | ||||||
|                     if(!change) { |               if (!change) { | ||||||
|                 remove_name_from_db(guid, coll); |                 remove_name_from_db(guid, coll); | ||||||
|               } else { |               } else { | ||||||
|                 remove_from_chat_channel(coll, guid); |                 remove_from_chat_channel(coll, guid); | ||||||
|               } |               } | ||||||
|  |             } | ||||||
|  |           ); | ||||||
|         }); |         }); | ||||||
|             }); |  | ||||||
|  |  | ||||||
|       } else { |       } else { | ||||||
|             db.collection("connected_users").update({"_id": "offline_users"}, {$pull: {users: guid}}, function(err, updated){ |         db.collection("connected_users").update( | ||||||
|  |           { _id: "offline_users" }, | ||||||
|  |           { $pull: { users: guid } }, | ||||||
|  |           function(err, updated) { | ||||||
|             //if(updated.nModified > 0) { |             //if(updated.nModified > 0) { | ||||||
|                     db.collection("connected_users").update({"_id": "total_users"}, {$pull: {total_users: guid + coll}}, function(err, updated){}); |             db.collection("connected_users").update( | ||||||
|                     if(!change) { |               { _id: "total_users" }, | ||||||
|  |               { $pull: { total_users: guid + coll } }, | ||||||
|  |               function(err, updated) {} | ||||||
|  |             ); | ||||||
|  |             if (!change) { | ||||||
|               remove_name_from_db(guid, coll); |               remove_name_from_db(guid, coll); | ||||||
|             } else { |             } else { | ||||||
|               remove_from_chat_channel(coll, guid); |               remove_from_chat_channel(coll, guid); | ||||||
|             } |             } | ||||||
|             //} |             //} | ||||||
|             }); |  | ||||||
|  |  | ||||||
|           } |           } | ||||||
|     }); |         ); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   ); | ||||||
|   remove_unique_id(short_id); |   remove_unique_id(short_id); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function checkTimeout( | ||||||
| function checkTimeout(type, timeout, channel, guid, conf_pass, this_pass, socket, callback, error_message, error_callback){ |   type, | ||||||
|     if(conf_pass != "" && conf_pass == this_pass) { |   timeout, | ||||||
|  |   channel, | ||||||
|  |   guid, | ||||||
|  |   conf_pass, | ||||||
|  |   this_pass, | ||||||
|  |   socket, | ||||||
|  |   callback, | ||||||
|  |   error_message, | ||||||
|  |   error_callback | ||||||
|  | ) { | ||||||
|  |   if (conf_pass != "" && conf_pass == this_pass) { | ||||||
|     callback(); |     callback(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|     db.collection("timeout_api").find({ |   db.collection("timeout_api").find( | ||||||
|  |     { | ||||||
|       type: type, |       type: type, | ||||||
|         guid: guid, |       guid: guid | ||||||
|     }, function(err, docs) { |     }, | ||||||
|         if(docs.length > 0) { |     function(err, docs) { | ||||||
|  |       if (docs.length > 0) { | ||||||
|         var date = new Date(docs[0].createdAt); |         var date = new Date(docs[0].createdAt); | ||||||
|         date.setSeconds(date.getSeconds() + timeout); |         date.setSeconds(date.getSeconds() + timeout); | ||||||
|         var now = new Date(); |         var now = new Date(); | ||||||
|  |  | ||||||
|         var retry_in = (date.getTime() - now.getTime()) / 1000; |         var retry_in = (date.getTime() - now.getTime()) / 1000; | ||||||
|             if(retry_in > 0) { |         if (retry_in > 0) { | ||||||
|                 if(typeof(error_callback) == "function") { |           if (typeof error_callback == "function") { | ||||||
|             error_callback(); |             error_callback(); | ||||||
|                 } else if(error_message) { |           } else if (error_message) { | ||||||
|                     var sOrNot = Math.ceil(retry_in) > 1 || Math.ceil(retry_in) == 0 ? "s" : ""; |             var sOrNot = | ||||||
|                     socket.emit("toast", error_message + Math.ceil(retry_in) + " second" + sOrNot + "."); |               Math.ceil(retry_in) > 1 || Math.ceil(retry_in) == 0 ? "s" : ""; | ||||||
|  |             socket.emit( | ||||||
|  |               "toast", | ||||||
|  |               error_message + Math.ceil(retry_in) + " second" + sOrNot + "." | ||||||
|  |             ); | ||||||
|           } else { |           } else { | ||||||
|             socket.emit("toast", "wait_longer"); |             socket.emit("toast", "wait_longer"); | ||||||
|           } |           } | ||||||
| @@ -466,17 +663,23 @@ function checkTimeout(type, timeout, channel, guid, conf_pass, this_pass, socket | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|       var now_date = new Date(); |       var now_date = new Date(); | ||||||
|         db.collection("timeout_api").update({type: type, guid: guid}, { |       db.collection("timeout_api").update( | ||||||
|  |         { type: type, guid: guid }, | ||||||
|  |         { | ||||||
|           $set: { |           $set: { | ||||||
|                 "createdAt": now_date, |             createdAt: now_date, | ||||||
|             type: type, |             type: type, | ||||||
|                 guid: guid, |             guid: guid | ||||||
|  |           } | ||||||
|         }, |         }, | ||||||
|         }, {upsert: true}, function(err, docs) { |         { upsert: true }, | ||||||
|  |         function(err, docs) { | ||||||
|           callback(); |           callback(); | ||||||
|           return; |           return; | ||||||
|         }); |         } | ||||||
|     }); |       ); | ||||||
|  |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports.checkTimeout = checkTimeout; | module.exports.checkTimeout = checkTimeout; | ||||||
|   | |||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -1,81 +1,114 @@ | |||||||
| var Functions = require(pathThumbnails + '/handlers/functions.js'); | var Functions = require(pathThumbnails + "/handlers/functions.js"); | ||||||
| var crypto = require('crypto'); | var crypto = require("crypto"); | ||||||
| var Filter = require('bad-words'); | var Filter = require("bad-words"); | ||||||
| var filter = new Filter({ placeHolder: 'x'}); | var filter = new Filter({ placeHolder: "x" }); | ||||||
| /*var filter = { | /*var filter = { | ||||||
|     clean: function(str) { |     clean: function(str) { | ||||||
|         return str; |         return str; | ||||||
|     } |     } | ||||||
| }*/ | }*/ | ||||||
| var projects = require(pathThumbnails + "/handlers/aggregates.js"); | var projects = require(pathThumbnails + "/handlers/aggregates.js"); | ||||||
| var db = require(pathThumbnails + '/handlers/db.js'); | var db = require(pathThumbnails + "/handlers/db.js"); | ||||||
|  |  | ||||||
| function password(inp, coll, guid, offline, socket) { | function password(inp, coll, guid, offline, socket) { | ||||||
|   var sessionId = Functions.getSession(socket); |   var sessionId = Functions.getSession(socket); | ||||||
|     if(sessionId == "") sessionId = "empty"; |   if (sessionId == "") sessionId = "empty"; | ||||||
|     if(inp !== undefined && inp !== null && inp !== "") |   if (inp !== undefined && inp !== null && inp !== "") { | ||||||
|     { |     if ( | ||||||
|         if(!inp.hasOwnProperty("password") || !inp.hasOwnProperty("channel") || |       !inp.hasOwnProperty("password") || | ||||||
|          typeof(inp.password) != "string" || typeof(inp.channel) != "string") { |       !inp.hasOwnProperty("channel") || | ||||||
|  |       typeof inp.password != "string" || | ||||||
|  |       typeof inp.channel != "string" | ||||||
|  |     ) { | ||||||
|       var result = { |       var result = { | ||||||
|         channel: { |         channel: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                      got: inp.hasOwnProperty("channel") ? typeof(inp.channel) : undefined, |           got: inp.hasOwnProperty("channel") ? typeof inp.channel : undefined | ||||||
|         }, |         }, | ||||||
|         password: { |         password: { | ||||||
|           expected: "password", |           expected: "password", | ||||||
|                      got: inp.hasOwnProperty("password") ? typeof(inp.password) : undefined, |           got: inp.hasOwnProperty("password") ? typeof inp.password : undefined | ||||||
|                  }, |         } | ||||||
|       }; |       }; | ||||||
|              socket.emit('update_required', result); |       socket.emit("update_required", result); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     pw = inp.password; |     pw = inp.password; | ||||||
|     try { |     try { | ||||||
|       coll = inp.channel; |       coll = inp.channel; | ||||||
|             if(coll.length == 0) return; |       if (coll.length == 0) return; | ||||||
|       coll = Functions.removeEmojis(coll).toLowerCase(); |       coll = Functions.removeEmojis(coll).toLowerCase(); | ||||||
|       //coll = coll.replace(/_/g, ""); |       //coll = coll.replace(/_/g, ""); | ||||||
|  |  | ||||||
|       //coll = filter.clean(coll); |       //coll = filter.clean(coll); | ||||||
|         } catch(e) { |     } catch (e) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     //coll = coll.replace(/ /g,''); |     //coll = coll.replace(/ /g,''); | ||||||
|     uncrypted = pw; |     uncrypted = pw; | ||||||
|     pw = Functions.hash_pass(Functions.decrypt_string(pw), true); |     pw = Functions.hash_pass(Functions.decrypt_string(pw), true); | ||||||
|     Functions.check_inlist(coll, guid, socket, offline, undefined, "place 8"); |     Functions.check_inlist(coll, guid, socket, offline, undefined, "place 8"); | ||||||
|         Functions.getSessionAdminUser(sessionId, coll, function(userpass, adminpass) { |     Functions.getSessionAdminUser(sessionId, coll, function( | ||||||
|  |       userpass, | ||||||
|  |       adminpass | ||||||
|  |     ) { | ||||||
|       adminpass = Functions.hash_pass(adminpass); |       adminpass = Functions.hash_pass(adminpass); | ||||||
|             db.collection(coll + "_settings").find(function(err, docs){ |       db.collection(coll + "_settings").find(function(err, docs) { | ||||||
|                 if(docs !== null && docs.length !== 0) |         if (docs !== null && docs.length !== 0) { | ||||||
|                 { |           if ( | ||||||
|                     if(docs[0].adminpass === "" || docs[0].adminpass == Functions.hash_pass(pw)) |             docs[0].adminpass === "" || | ||||||
|                     { |             docs[0].adminpass == Functions.hash_pass(pw) | ||||||
|                         Functions.setSessionAdminPass(sessionId, inp.password, coll, function() { |           ) { | ||||||
|                             db.collection(coll + "_settings").update({ id: "config" }, {$set:{adminpass:Functions.hash_pass(pw)}}, function(err, docs){ |             Functions.setSessionAdminPass( | ||||||
|                                 if(adminpass != pw && adminpass != "") { |               sessionId, | ||||||
|  |               inp.password, | ||||||
|  |               coll, | ||||||
|  |               function() { | ||||||
|  |                 db.collection(coll + "_settings").update( | ||||||
|  |                   { id: "config" }, | ||||||
|  |                   { $set: { adminpass: Functions.hash_pass(pw) } }, | ||||||
|  |                   function(err, docs) { | ||||||
|  |                     if (adminpass != pw && adminpass != "") { | ||||||
|                       socket.emit("toast", "changedpass"); |                       socket.emit("toast", "changedpass"); | ||||||
|                     } else { |                     } else { | ||||||
|                       socket.emit("toast", "correctpass"); |                       socket.emit("toast", "correctpass"); | ||||||
|                     } |                     } | ||||||
|                     socket.emit("pw", true); |                     socket.emit("pw", true); | ||||||
|                             }); |                   } | ||||||
|                         }); |                 ); | ||||||
|                     } else if(docs[0].adminpass === "" || docs[0].adminpass == adminpass) { |               } | ||||||
|                         Functions.setSessionAdminPass(sessionId, inp.password, coll, function() { |             ); | ||||||
|                             db.collection(coll + "_settings").update({ id: "config" }, {$set:{adminpass:Functions.hash_pass(pw)}}, function(err, docs){ |           } else if ( | ||||||
|                                 if(adminpass != pw) { |             docs[0].adminpass === "" || | ||||||
|  |             docs[0].adminpass == adminpass | ||||||
|  |           ) { | ||||||
|  |             Functions.setSessionAdminPass( | ||||||
|  |               sessionId, | ||||||
|  |               inp.password, | ||||||
|  |               coll, | ||||||
|  |               function() { | ||||||
|  |                 db.collection(coll + "_settings").update( | ||||||
|  |                   { id: "config" }, | ||||||
|  |                   { $set: { adminpass: Functions.hash_pass(pw) } }, | ||||||
|  |                   function(err, docs) { | ||||||
|  |                     if (adminpass != pw) { | ||||||
|                       socket.emit("toast", "changedpass"); |                       socket.emit("toast", "changedpass"); | ||||||
|                     } |                     } | ||||||
|                     socket.emit("pw", true); |                     socket.emit("pw", true); | ||||||
|                             }); |                   } | ||||||
|                         }); |                 ); | ||||||
|  |               } | ||||||
|  |             ); | ||||||
|           } else { |           } else { | ||||||
|                         Functions.setSessionAdminPass(Functions.getSession(socket), "", coll, function() { |             Functions.setSessionAdminPass( | ||||||
|  |               Functions.getSession(socket), | ||||||
|  |               "", | ||||||
|  |               coll, | ||||||
|  |               function() { | ||||||
|                 socket.emit("toast", "wrongpass"); |                 socket.emit("toast", "wrongpass"); | ||||||
|                 socket.emit("pw", false); |                 socket.emit("pw", false); | ||||||
|                         }); |               } | ||||||
|  |             ); | ||||||
|           } |           } | ||||||
|         } |         } | ||||||
|       }); |       }); | ||||||
| @@ -84,88 +117,119 @@ function password(inp, coll, guid, offline, socket) { | |||||||
|     var result = { |     var result = { | ||||||
|       inp: { |       inp: { | ||||||
|         expected: "string", |         expected: "string", | ||||||
|                 got: typeof(inpt) |         got: typeof inpt | ||||||
|             }, |       } | ||||||
|     }; |     }; | ||||||
|         socket.emit('update_required', result); |     socket.emit("update_required", result); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function conf_function(params, coll, guid, offline, socket) { | function conf_function(params, coll, guid, offline, socket) { | ||||||
|     if(params !== undefined && params !== null && params !== "") |   if (params !== undefined && params !== null && params !== "") { | ||||||
|     { |     if (coll !== undefined) { | ||||||
|         if(coll !== undefined) { |  | ||||||
|       try { |       try { | ||||||
|                 coll = params.channel;//.replace(/ /g,''); |         coll = params.channel; //.replace(/ /g,''); | ||||||
|                 if(coll.length == 0) return; |         if (coll.length == 0) return; | ||||||
|         coll = Functions.removeEmojis(coll).toLowerCase(); |         coll = Functions.removeEmojis(coll).toLowerCase(); | ||||||
|         //coll = coll.replace(/_/g, ""); |         //coll = coll.replace(/_/g, ""); | ||||||
|  |  | ||||||
|         //coll = filter.clean(coll); |         //coll = filter.clean(coll); | ||||||
|             } catch(e) { |       } catch (e) { | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         if(coll == "" || coll == undefined || coll == null) { |     if (coll == "" || coll == undefined || coll == null) { | ||||||
|       socket.emit("update_required"); |       socket.emit("update_required"); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     Functions.check_inlist(coll, guid, socket, offline, undefined, "place 9"); |     Functions.check_inlist(coll, guid, socket, offline, undefined, "place 9"); | ||||||
|  |  | ||||||
|         Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass, gotten) { |     Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( | ||||||
|             if(gotten) { |       userpass, | ||||||
|  |       adminpass, | ||||||
|  |       gotten | ||||||
|  |     ) { | ||||||
|  |       if (gotten) { | ||||||
|         params.adminpass = adminpass; |         params.adminpass = adminpass; | ||||||
|                 if(!params.userpass_changed) params.userpass = userpass; |         if (!params.userpass_changed) params.userpass = userpass; | ||||||
|       } |       } | ||||||
|             if(!params.hasOwnProperty('voting') || !params.hasOwnProperty('addsongs') || |       if ( | ||||||
|                 !params.hasOwnProperty('longsongs') || !params.hasOwnProperty('frontpage') || |         !params.hasOwnProperty("voting") || | ||||||
|                 !params.hasOwnProperty('allvideos') || !params.hasOwnProperty('removeplay') || |         !params.hasOwnProperty("addsongs") || | ||||||
|                 !params.hasOwnProperty('adminpass') || !params.hasOwnProperty('skipping') || |         !params.hasOwnProperty("longsongs") || | ||||||
|                 !params.hasOwnProperty('shuffling') || !params.hasOwnProperty('channel') || |         !params.hasOwnProperty("frontpage") || | ||||||
|                 typeof(params.userpass) != "string" || typeof(params.adminpass) != "string" || |         !params.hasOwnProperty("allvideos") || | ||||||
|                 typeof(params.voting) != "boolean" || typeof(params.addsongs) != "boolean" || |         !params.hasOwnProperty("removeplay") || | ||||||
|                 typeof(params.longsongs) != "boolean" || typeof(params.frontpage) != "boolean" || |         !params.hasOwnProperty("adminpass") || | ||||||
|                 typeof(params.allvideos) != "boolean" || typeof(params.removeplay) != "boolean" || |         !params.hasOwnProperty("skipping") || | ||||||
|                 typeof(params.skipping) != "boolean" || typeof(params.shuffling) != "boolean" || |         !params.hasOwnProperty("shuffling") || | ||||||
|                 typeof(params.userpass_changed) != "boolean") { |         !params.hasOwnProperty("channel") || | ||||||
|  |         typeof params.userpass != "string" || | ||||||
|  |         typeof params.adminpass != "string" || | ||||||
|  |         typeof params.voting != "boolean" || | ||||||
|  |         typeof params.addsongs != "boolean" || | ||||||
|  |         typeof params.longsongs != "boolean" || | ||||||
|  |         typeof params.frontpage != "boolean" || | ||||||
|  |         typeof params.allvideos != "boolean" || | ||||||
|  |         typeof params.removeplay != "boolean" || | ||||||
|  |         typeof params.skipping != "boolean" || | ||||||
|  |         typeof params.shuffling != "boolean" || | ||||||
|  |         typeof params.userpass_changed != "boolean" | ||||||
|  |       ) { | ||||||
|         var result = { |         var result = { | ||||||
|           adminpass: { |           adminpass: { | ||||||
|             expected: "string", |             expected: "string", | ||||||
|                             got: params.hasOwnProperty("adminpass") ? typeof(params.adminpass) : undefined, |             got: params.hasOwnProperty("adminpass") | ||||||
|  |               ? typeof params.adminpass | ||||||
|  |               : undefined | ||||||
|           }, |           }, | ||||||
|           userpass: { |           userpass: { | ||||||
|             expected: "string", |             expected: "string", | ||||||
|                             got: params.hasOwnProperty("userpass") ? typeof(params.userpass) : undefined, |             got: params.hasOwnProperty("userpass") | ||||||
|  |               ? typeof params.userpass | ||||||
|  |               : undefined | ||||||
|           }, |           }, | ||||||
|           vote: { |           vote: { | ||||||
|             expected: "boolean", |             expected: "boolean", | ||||||
|                             got: params.hasOwnProperty("vote") ? typeof(params.vote) : undefined, |             got: params.hasOwnProperty("vote") ? typeof params.vote : undefined | ||||||
|           }, |           }, | ||||||
|           addsongs: { |           addsongs: { | ||||||
|             expected: "boolean", |             expected: "boolean", | ||||||
|                             got: params.hasOwnProperty("addsongs") ? typeof(params.addsongs) : undefined, |             got: params.hasOwnProperty("addsongs") | ||||||
|  |               ? typeof params.addsongs | ||||||
|  |               : undefined | ||||||
|           }, |           }, | ||||||
|           longsongs: { |           longsongs: { | ||||||
|             expected: "boolean", |             expected: "boolean", | ||||||
|                             got: params.hasOwnProperty("longsongs") ? typeof(params.longsongs) : undefined, |             got: params.hasOwnProperty("longsongs") | ||||||
|  |               ? typeof params.longsongs | ||||||
|  |               : undefined | ||||||
|           }, |           }, | ||||||
|           frontpage: { |           frontpage: { | ||||||
|             expected: "boolean", |             expected: "boolean", | ||||||
|                             got: params.hasOwnProperty("frontpage") ? typeof(params.frontpage) : undefined, |             got: params.hasOwnProperty("frontpage") | ||||||
|  |               ? typeof params.frontpage | ||||||
|  |               : undefined | ||||||
|           }, |           }, | ||||||
|           skipping: { |           skipping: { | ||||||
|             expected: "boolean", |             expected: "boolean", | ||||||
|                             got: params.hasOwnProperty("skipping") ? typeof(params.skipping) : undefined, |             got: params.hasOwnProperty("skipping") | ||||||
|  |               ? typeof params.skipping | ||||||
|  |               : undefined | ||||||
|           }, |           }, | ||||||
|           shuffling: { |           shuffling: { | ||||||
|             expected: "boolean", |             expected: "boolean", | ||||||
|                             got: params.hasOwnProperty("shuffling") ? typeof(params.shuffling) : undefined, |             got: params.hasOwnProperty("shuffling") | ||||||
|  |               ? typeof params.shuffling | ||||||
|  |               : undefined | ||||||
|           }, |           }, | ||||||
|           userpass_changed: { |           userpass_changed: { | ||||||
|             expected: "boolean", |             expected: "boolean", | ||||||
|                             got: params.hasOwnProperty("userpass_changed") ? typeof(params.userpass_changed) : undefined, |             got: params.hasOwnProperty("userpass_changed") | ||||||
|  |               ? typeof params.userpass_changed | ||||||
|  |               : undefined | ||||||
|           } |           } | ||||||
|         }; |         }; | ||||||
|         socket.emit("update_required", result); |         socket.emit("update_required", result); | ||||||
| @@ -183,85 +247,137 @@ function conf_function(params, coll, guid, offline, socket) { | |||||||
|  |  | ||||||
|       var userpass = Functions.decrypt_string(params.userpass); |       var userpass = Functions.decrypt_string(params.userpass); | ||||||
|  |  | ||||||
|             if((!params.userpass_changed && frontpage) || (params.userpass_changed && userpass == "")) { |       if ( | ||||||
|  |         (!params.userpass_changed && frontpage) || | ||||||
|  |         (params.userpass_changed && userpass == "") | ||||||
|  |       ) { | ||||||
|         userpass = ""; |         userpass = ""; | ||||||
|             } else if(params.userpass_changed && userpass != "") { |       } else if (params.userpass_changed && userpass != "") { | ||||||
|         frontpage = false; |         frontpage = false; | ||||||
|       } |       } | ||||||
|       var description = ""; |       var description = ""; | ||||||
|       var hash; |       var hash; | ||||||
|             if(params.description) description = params.description; |       if (params.description) description = params.description; | ||||||
|             if(adminpass !== "" && !gotten) { |       if (adminpass !== "" && !gotten) { | ||||||
|                 hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(adminpass), true)); |         hash = Functions.hash_pass( | ||||||
|             } else if(adminpass !== "" && gotten) { |           Functions.hash_pass(Functions.decrypt_string(adminpass), true) | ||||||
|  |         ); | ||||||
|  |       } else if (adminpass !== "" && gotten) { | ||||||
|         hash = Functions.hash_pass(adminpass); |         hash = Functions.hash_pass(adminpass); | ||||||
|       } else { |       } else { | ||||||
|         hash = adminpass; |         hash = adminpass; | ||||||
|       } |       } | ||||||
|             if(userpass != "") { |       if (userpass != "") { | ||||||
|                 if(!params.userpass_changed && gotten) { |         if (!params.userpass_changed && gotten) { | ||||||
|  |  | ||||||
|         } else { |         } else { | ||||||
|                     userpass = crypto.createHash('sha256').update(userpass).digest("base64"); |           userpass = crypto | ||||||
|  |             .createHash("sha256") | ||||||
|  |             .update(userpass) | ||||||
|  |             .digest("base64"); | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|             db.collection(coll + "_settings").find({id: "config"}, function(err, docs){ |       db.collection(coll + "_settings").find({ id: "config" }, function( | ||||||
|                 if(docs !== null && docs.length !== 0 && (docs[0].adminpass === "" || docs[0].adminpass == hash)) { |         err, | ||||||
|  |         docs | ||||||
|  |       ) { | ||||||
|  |         if ( | ||||||
|  |           docs !== null && | ||||||
|  |           docs.length !== 0 && | ||||||
|  |           (docs[0].adminpass === "" || docs[0].adminpass == hash) | ||||||
|  |         ) { | ||||||
|           var obj = { |           var obj = { | ||||||
|                         addsongs:addsongs, |             addsongs: addsongs, | ||||||
|                         allvideos:allvideos, |             allvideos: allvideos, | ||||||
|                         frontpage:frontpage, |             frontpage: frontpage, | ||||||
|                         skip:skipping, |             skip: skipping, | ||||||
|                         vote:voting, |             vote: voting, | ||||||
|                         removeplay:removeplay, |             removeplay: removeplay, | ||||||
|                         shuffle:shuffling, |             shuffle: shuffling, | ||||||
|                         longsongs:longsongs, |             longsongs: longsongs, | ||||||
|                         adminpass:hash, |             adminpass: hash, | ||||||
|                         desc: description, |             desc: description | ||||||
|           }; |           }; | ||||||
|                     if(params.hasOwnProperty("toggleChat") && docs[0].adminpass != "" && docs[0].adminpass != undefined && docs[0].adminpass == hash) { |           if ( | ||||||
|  |             params.hasOwnProperty("toggleChat") && | ||||||
|  |             docs[0].adminpass != "" && | ||||||
|  |             docs[0].adminpass != undefined && | ||||||
|  |             docs[0].adminpass == hash | ||||||
|  |           ) { | ||||||
|             obj.toggleChat = params.toggleChat; |             obj.toggleChat = params.toggleChat; | ||||||
|           } |           } | ||||||
|                     if(params.hasOwnProperty("strictSkip") && docs[0].adminpass != "" && docs[0].adminpass != undefined && docs[0].adminpass == hash) { |           if ( | ||||||
|  |             params.hasOwnProperty("strictSkip") && | ||||||
|  |             docs[0].adminpass != "" && | ||||||
|  |             docs[0].adminpass != undefined && | ||||||
|  |             docs[0].adminpass == hash | ||||||
|  |           ) { | ||||||
|             obj.strictSkip = params.strictSkip; |             obj.strictSkip = params.strictSkip; | ||||||
|           } |           } | ||||||
|                     if(params.hasOwnProperty("strictSkipNumber") && docs[0].adminpass != "" && docs[0].adminpass != undefined && docs[0].adminpass == hash) { |           if ( | ||||||
|  |             params.hasOwnProperty("strictSkipNumber") && | ||||||
|  |             docs[0].adminpass != "" && | ||||||
|  |             docs[0].adminpass != undefined && | ||||||
|  |             docs[0].adminpass == hash | ||||||
|  |           ) { | ||||||
|             try { |             try { | ||||||
|               obj.strictSkipNumber = parseInt(params.strictSkipNumber); |               obj.strictSkipNumber = parseInt(params.strictSkipNumber); | ||||||
|                         } catch(e) {} |             } catch (e) {} | ||||||
|           } |           } | ||||||
|                     if(params.userpass_changed) { |           if (params.userpass_changed) { | ||||||
|             obj["userpass"] = userpass; |             obj["userpass"] = userpass; | ||||||
|           } else if (frontpage) { |           } else if (frontpage) { | ||||||
|             obj["userpass"] = ""; |             obj["userpass"] = ""; | ||||||
|           } |           } | ||||||
|                     db.collection(coll + "_settings").update({ id: "config" }, { |           db.collection(coll + "_settings").update( | ||||||
|                         $set:obj |             { id: "config" }, | ||||||
|                     }, function(err, docs){ |  | ||||||
|                         Functions.setSessionUserPass(Functions.getSession(socket), obj["userpass"], coll, function() { |  | ||||||
|                             db.collection(coll + "_settings").aggregate([ |  | ||||||
|             { |             { | ||||||
|                                     "$match": { |               $set: obj | ||||||
|  |             }, | ||||||
|  |             function(err, docs) { | ||||||
|  |               Functions.setSessionUserPass( | ||||||
|  |                 Functions.getSession(socket), | ||||||
|  |                 obj["userpass"], | ||||||
|  |                 coll, | ||||||
|  |                 function() { | ||||||
|  |                   db.collection(coll + "_settings").aggregate( | ||||||
|  |                     [ | ||||||
|  |                       { | ||||||
|  |                         $match: { | ||||||
|                           id: "config" |                           id: "config" | ||||||
|                         } |                         } | ||||||
|                       }, |                       }, | ||||||
|                       { |                       { | ||||||
|                                     "$project": projects.toShowConfig |                         $project: projects.toShowConfig | ||||||
|                                 }, |                       } | ||||||
|                             ], function(err, docs){ |                     ], | ||||||
|                                 if(docs[0].adminpass !== "") docs[0].adminpass = true; |                     function(err, docs) { | ||||||
|                                 if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; |                       if (docs[0].adminpass !== "") docs[0].adminpass = true; | ||||||
|  |                       if ( | ||||||
|  |                         docs[0].hasOwnProperty("userpass") && | ||||||
|  |                         docs[0].userpass != "" | ||||||
|  |                       ) | ||||||
|  |                         docs[0].userpass = true; | ||||||
|                       else docs[0].userpass = false; |                       else docs[0].userpass = false; | ||||||
|                       io.to(coll).emit("conf", docs); |                       io.to(coll).emit("conf", docs); | ||||||
|                       socket.emit("toast", "savedsettings"); |                       socket.emit("toast", "savedsettings"); | ||||||
|  |  | ||||||
|                                 db.collection("frontpage_lists").update({_id: coll}, {$set:{ |                       db.collection("frontpage_lists").update( | ||||||
|                                     frontpage:frontpage, accessed: Functions.get_time()} |                         { _id: coll }, | ||||||
|  |                         { | ||||||
|  |                           $set: { | ||||||
|  |                             frontpage: frontpage, | ||||||
|  |                             accessed: Functions.get_time() | ||||||
|  |                           } | ||||||
|                         }, |                         }, | ||||||
|                                 {upsert:true}, function(err, docs){}); |                         { upsert: true }, | ||||||
|                             }); |                         function(err, docs) {} | ||||||
|                         }); |                       ); | ||||||
|                     }); |                     } | ||||||
|  |                   ); | ||||||
|  |                 } | ||||||
|  |               ); | ||||||
|  |             } | ||||||
|  |           ); | ||||||
|         } else { |         } else { | ||||||
|           socket.emit("toast", "wrongpass"); |           socket.emit("toast", "wrongpass"); | ||||||
|         } |         } | ||||||
| @@ -271,12 +387,11 @@ function conf_function(params, coll, guid, offline, socket) { | |||||||
|     var result = { |     var result = { | ||||||
|       params: { |       params: { | ||||||
|         expected: "object", |         expected: "object", | ||||||
|                 got: typeof(params), |         got: typeof params | ||||||
|       } |       } | ||||||
|  |     }; | ||||||
|  |     socket.emit("update_required", result); | ||||||
|   } |   } | ||||||
|         socket.emit('update_required', result); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports.password = password; | module.exports.password = password; | ||||||
|   | |||||||
| @@ -1,10 +1,10 @@ | |||||||
| var path = require('path'); | var path = require("path"); | ||||||
|  |  | ||||||
| function requested_change(type, string, channel) { | function requested_change(type, string, channel) { | ||||||
|   try { |   try { | ||||||
|     //channel = channel.replace(/ /g,''); |     //channel = channel.replace(/ /g,''); | ||||||
|         var nodemailer = require('nodemailer'); |     var nodemailer = require("nodemailer"); | ||||||
|         var mailconfig = require(path.join(__dirname, '../config/mailconfig.js')); |     var mailconfig = require(path.join(__dirname, "../config/mailconfig.js")); | ||||||
|  |  | ||||||
|     let transporter = nodemailer.createTransport(mailconfig); |     let transporter = nodemailer.createTransport(mailconfig); | ||||||
|  |  | ||||||
| @@ -12,15 +12,22 @@ function requested_change(type, string, channel) { | |||||||
|       if (error) { |       if (error) { | ||||||
|         return; |         return; | ||||||
|       } else { |       } else { | ||||||
|                 var message = "A " + type + " change was requested on <b>" + channel + "</b><br><br>New supposed value is: <br><br><b>" + string + "</b><br><br><br> \ |         var message = | ||||||
|  |           "A " + | ||||||
|  |           type + | ||||||
|  |           " change was requested on <b>" + | ||||||
|  |           channel + | ||||||
|  |           "</b><br><br>New supposed value is: <br><br><b>" + | ||||||
|  |           string + | ||||||
|  |           "</b><br><br><br> \ | ||||||
|                 Go to <a href='https://admin.zoff.me/'>https://admin.zoff.me/</a> to accept or decline the request."; |                 Go to <a href='https://admin.zoff.me/'>https://admin.zoff.me/</a> to accept or decline the request."; | ||||||
|         var msg = { |         var msg = { | ||||||
|           from: mailconfig.from, |           from: mailconfig.from, | ||||||
|           to: mailconfig.notify_mail, |           to: mailconfig.notify_mail, | ||||||
|                     subject: 'ZOFF: Requested new ' + type, |           subject: "ZOFF: Requested new " + type, | ||||||
|           text: message, |           text: message, | ||||||
|                     html: message, |           html: message | ||||||
|                 } |         }; | ||||||
|         transporter.sendMail(msg, (error, info) => { |         transporter.sendMail(msg, (error, info) => { | ||||||
|           if (error) { |           if (error) { | ||||||
|             transporter.close(); |             transporter.close(); | ||||||
| @@ -30,9 +37,11 @@ function requested_change(type, string, channel) { | |||||||
|         }); |         }); | ||||||
|       } |       } | ||||||
|     }); |     }); | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     console.log("Mail is not configured and wont work"); |     console.log("Mail is not configured and wont work"); | ||||||
|         console.log("Seems you forgot to create a mailconfig.js in /server/config/. Have a look at the mailconfig.example.js."); |     console.log( | ||||||
|  |       "Seems you forgot to create a mailconfig.js in /server/config/. Have a look at the mailconfig.example.js." | ||||||
|  |     ); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,139 +1,180 @@ | |||||||
| var path = require('path'); | var path = require("path"); | ||||||
| var time_regex = /P((([0-9]*\.?[0-9]*)Y)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)W)?(([0-9]*\.?[0-9]*)D)?)?(T(([0-9]*\.?[0-9]*)H)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)S)?)?/; | var time_regex = /P((([0-9]*\.?[0-9]*)Y)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)W)?(([0-9]*\.?[0-9]*)D)?)?(T(([0-9]*\.?[0-9]*)H)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)S)?)?/; | ||||||
| try { | try { | ||||||
|     var keys = require(path.join(__dirname, '../config/api_key.js')); |   var keys = require(path.join(__dirname, "../config/api_key.js")); | ||||||
|   var key = keys.youtube; |   var key = keys.youtube; | ||||||
|   var soundcloudKey = keys.soundcloud; |   var soundcloudKey = keys.soundcloud; | ||||||
| } catch(e) { | } catch (e) { | ||||||
|     console.log("Error - missing file"); |   console.log( | ||||||
|     console.log("Seems you forgot to create the file api_key.js in /server/config/. Have a look at api_key.example.js."); |     "(!) Missing file - /config/api_key.js Have a look at /config/api_key.example.js. The server won't run without this existing." | ||||||
|  |   ); | ||||||
|   process.exit(1); |   process.exit(1); | ||||||
| } | } | ||||||
| var request = require('request'); | var request = require("request"); | ||||||
| var db = require(pathThumbnails + '/handlers/db.js'); | var db = require(pathThumbnails + "/handlers/db.js"); | ||||||
| var countryCodes = ["US", "NO", "SE", "DK", "CA", "EU", "UK"]; | var countryCodes = ["US", "NO", "SE", "DK", "CA", "EU", "UK"]; | ||||||
|  |  | ||||||
| function check_if_error_or_blocked(id, channel, errored, callback) { | function check_if_error_or_blocked(id, channel, errored, callback) { | ||||||
|     if(!errored) { |   if (!errored) { | ||||||
|     callback(false); |     callback(false); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|     db.collection(channel).find({id: id, now_playing: true}, function(err, song) { |   db.collection(channel).find({ id: id, now_playing: true }, function( | ||||||
|         if(song.length == 0) { |     err, | ||||||
|  |     song | ||||||
|  |   ) { | ||||||
|  |     if (song.length == 0) { | ||||||
|       callback(false); |       callback(false); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     var song_info = song[0]; |     var song_info = song[0]; | ||||||
|         if(song_info.source != "soundcloud") { |     if (song_info.source != "soundcloud") { | ||||||
|             request({ |       request( | ||||||
|  |         { | ||||||
|           type: "GET", |           type: "GET", | ||||||
|                 url: "https://www.googleapis.com/youtube/v3/videos?part=id,status,contentDetails&key="+key+"&id=" + song_info.id, |           url: | ||||||
|             }, function(error, response, body) { |             "https://www.googleapis.com/youtube/v3/videos?part=id,status,contentDetails&key=" + | ||||||
|  |             key + | ||||||
|  |             "&id=" + | ||||||
|  |             song_info.id | ||||||
|  |         }, | ||||||
|  |         function(error, response, body) { | ||||||
|           try { |           try { | ||||||
|             var resp = JSON.parse(body); |             var resp = JSON.parse(body); | ||||||
|                     if(resp.pageInfo.totalResults == 0) { |             if (resp.pageInfo.totalResults == 0) { | ||||||
|               callback(true); |               callback(true); | ||||||
|               return; |               return; | ||||||
|                     } else if(!resp.items[0].status.embeddable) { |             } else if (!resp.items[0].status.embeddable) { | ||||||
|               callback(true); |               callback(true); | ||||||
|               return; |               return; | ||||||
|                     } else if(resp.items[0].contentDetails.hasOwnProperty("licensedContent") && |             } else if ( | ||||||
|                               resp.items[0].contentDetails.licensedContent) { |               resp.items[0].contentDetails.hasOwnProperty("licensedContent") && | ||||||
|  |               resp.items[0].contentDetails.licensedContent | ||||||
|  |             ) { | ||||||
|               callback(true); |               callback(true); | ||||||
|               return; |               return; | ||||||
|                     } else if(resp.items[0].contentDetails.hasOwnProperty("regionRestriction") && |             } else if ( | ||||||
|                         resp.items[0].contentDetails.regionRestriction.hasOwnProperty("blocked") && |               resp.items[0].contentDetails.hasOwnProperty( | ||||||
|                         resp.items[0].contentDetails.regionRestriction.blocked.length > 0) { |                 "regionRestriction" | ||||||
|                             var any = resp.items[0].contentDetails.blocked.some(function(element) { |               ) && | ||||||
|  |               resp.items[0].contentDetails.regionRestriction.hasOwnProperty( | ||||||
|  |                 "blocked" | ||||||
|  |               ) && | ||||||
|  |               resp.items[0].contentDetails.regionRestriction.blocked.length > 0 | ||||||
|  |             ) { | ||||||
|  |               var any = resp.items[0].contentDetails.blocked.some(function( | ||||||
|  |                 element | ||||||
|  |               ) { | ||||||
|                 return countryCodes.indexOf(element) > -1; |                 return countryCodes.indexOf(element) > -1; | ||||||
|               }); |               }); | ||||||
|                             if(any) { |               if (any) { | ||||||
|                 callback(true); |                 callback(true); | ||||||
|                 return; |                 return; | ||||||
|               } |               } | ||||||
|             } |             } | ||||||
|             callback(false); |             callback(false); | ||||||
|             return; |             return; | ||||||
|                 } catch(e){ |           } catch (e) { | ||||||
|             callback(true); |             callback(true); | ||||||
|             return; |             return; | ||||||
|           } |           } | ||||||
|             }); |         } | ||||||
|  |       ); | ||||||
|     } else { |     } else { | ||||||
|             request({ |       request( | ||||||
|  |         { | ||||||
|           type: "GET", |           type: "GET", | ||||||
|                 url: "http://api.soundcloud.com/tracks/" + song_info.id + "?client_id=" + soundcloudKey, |           url: | ||||||
|             }, function(error, response, body) { |             "http://api.soundcloud.com/tracks/" + | ||||||
|  |             song_info.id + | ||||||
|  |             "?client_id=" + | ||||||
|  |             soundcloudKey | ||||||
|  |         }, | ||||||
|  |         function(error, response, body) { | ||||||
|           try { |           try { | ||||||
|             var resp = JSON.parse(body); |             var resp = JSON.parse(body); | ||||||
|                     if(resp.sharing != "public" || resp.embeddable_by != "all") { |             if (resp.sharing != "public" || resp.embeddable_by != "all") { | ||||||
|               callback(true); |               callback(true); | ||||||
|               return; |               return; | ||||||
|             } |             } | ||||||
|             callback(false); |             callback(false); | ||||||
|             return; |             return; | ||||||
|                 } catch(e){ |           } catch (e) { | ||||||
|             callback(true); |             callback(true); | ||||||
|             return; |             return; | ||||||
|           } |           } | ||||||
|             }); |         } | ||||||
|  |       ); | ||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
| function filterFunction(el) { | function filterFunction(el) { | ||||||
|     return el != null && |   return el != null && el != "" && el != undefined && el.trim() != ""; | ||||||
|     el != "" && |  | ||||||
|     el != undefined && |  | ||||||
|     el.trim() != '' |  | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_genres_soundcloud(song, channel) { | function get_genres_soundcloud(song, channel) { | ||||||
|     request("http://api.soundcloud.com/tracks/" + song.id + "?client_id=" + soundcloudKey, function(err, response, body) { |   request( | ||||||
|         if(err) { |     "http://api.soundcloud.com/tracks/" + | ||||||
|  |       song.id + | ||||||
|  |       "?client_id=" + | ||||||
|  |       soundcloudKey, | ||||||
|  |     function(err, response, body) { | ||||||
|  |       if (err) { | ||||||
|         console.log("error start", err, song, "error end"); |         console.log("error start", err, song, "error end"); | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|       try { |       try { | ||||||
|         var object = JSON.parse(body); |         var object = JSON.parse(body); | ||||||
|             if(!object.hasOwnProperty("genre") || !object.hasOwnProperty("tag_list")) return; |         if ( | ||||||
|  |           !object.hasOwnProperty("genre") || | ||||||
|  |           !object.hasOwnProperty("tag_list") | ||||||
|  |         ) | ||||||
|  |           return; | ||||||
|         var genre = object.genre + ","; |         var genre = object.genre + ","; | ||||||
|             genre = genre.toLowerCase().split(",").concat(object.tag_list.toLowerCase().split('"')); |         genre = genre | ||||||
|  |           .toLowerCase() | ||||||
|  |           .split(",") | ||||||
|  |           .concat(object.tag_list.toLowerCase().split('"')); | ||||||
|         genre = genre.filter(filterFunction); |         genre = genre.filter(filterFunction); | ||||||
|  |  | ||||||
|             db.collection(channel).update({"id": song.id}, { |         db.collection(channel).update( | ||||||
|  |           { id: song.id }, | ||||||
|  |           { | ||||||
|             $set: { |             $set: { | ||||||
|                     "tags": genre |               tags: genre | ||||||
|             } |             } | ||||||
|             }, function(e,d) { |           }, | ||||||
|  |           function(e, d) {} | ||||||
|             }); |         ); | ||||||
|         } catch(e) { |       } catch (e) { | ||||||
|         console.log("errored 2", e); |         console.log("errored 2", e); | ||||||
|       } |       } | ||||||
|     }); |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_genres_list(list, channel) { | function get_genres_list(list, channel) { | ||||||
|   var youtube_array = ""; |   var youtube_array = ""; | ||||||
|   var i = 0; |   var i = 0; | ||||||
|   try { |   try { | ||||||
|         for(var i = 0; i < list.length; i++) { |     for (var i = 0; i < list.length; i++) { | ||||||
|  |       if (!list[i].hasOwnProperty("id")) continue; | ||||||
|             if(!list[i].hasOwnProperty("id")) continue; |       if (list[i].source == undefined || list[i].source == "youtube") { | ||||||
|             if(list[i].source == undefined || list[i].source == "youtube") { |  | ||||||
|         youtube_array += list[i].id + ","; |         youtube_array += list[i].id + ","; | ||||||
|             } |       } else if ( | ||||||
|             else if(list[i].source != undefined && list[i].source == "soundcloud") { |         list[i].source != undefined && | ||||||
|  |         list[i].source == "soundcloud" | ||||||
|  |       ) { | ||||||
|         get_genres_soundcloud(list[i], channel); |         get_genres_soundcloud(list[i], channel); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|         if(youtube_array.length > 0) { |     if (youtube_array.length > 0) { | ||||||
|             if(youtube_array > 49) { |       if (youtube_array > 49) { | ||||||
|         var subList = []; |         var subList = []; | ||||||
|                 for(var i = 0; i < youtube_array.length; i++) { |         for (var i = 0; i < youtube_array.length; i++) { | ||||||
|           subList.push(youtube_array[i]); |           subList.push(youtube_array[i]); | ||||||
|                     if(subList.length > 49) { |           if (subList.length > 49) { | ||||||
|             get_genres_youtube(subList.join(","), channel); |             get_genres_youtube(subList.join(","), channel); | ||||||
|             subList = []; |             subList = []; | ||||||
|           } |           } | ||||||
| @@ -144,306 +185,419 @@ function get_genres_list(list, channel) { | |||||||
|         get_genres_youtube(youtube_array, channel); |         get_genres_youtube(youtube_array, channel); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     console.log("errored", e); |     console.log("errored", e); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| function start_soundcloud_get(arr, channel, callback) { | function start_soundcloud_get(arr, channel, callback) { | ||||||
|   get_genres_soundcloud_recursive(arr, channel, 0, callback); |   get_genres_soundcloud_recursive(arr, channel, 0, callback); | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_genres_soundcloud_recursive(arr, channel, i, callback) { | function get_genres_soundcloud_recursive(arr, channel, i, callback) { | ||||||
|     if(i >= arr.length) { |   if (i >= arr.length) { | ||||||
|         if(typeof(callback) == "function") callback(); |     if (typeof callback == "function") callback(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   var song = arr[i]; |   var song = arr[i]; | ||||||
|     request("http://api.soundcloud.com/tracks/" + song.id + "?client_id=" + soundcloudKey, function(err, response, body) { |   request( | ||||||
|         if(err) { |     "http://api.soundcloud.com/tracks/" + | ||||||
|  |       song.id + | ||||||
|  |       "?client_id=" + | ||||||
|  |       soundcloudKey, | ||||||
|  |     function(err, response, body) { | ||||||
|  |       if (err) { | ||||||
|         console.log("error start", err, song, "error end"); |         console.log("error start", err, song, "error end"); | ||||||
|         get_genres_soundcloud_recursive(arr, channel, i + 1, callback); |         get_genres_soundcloud_recursive(arr, channel, i + 1, callback); | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|       try { |       try { | ||||||
|         var object = JSON.parse(body); |         var object = JSON.parse(body); | ||||||
|             if(!object.hasOwnProperty("genre") || !object.hasOwnProperty("tag_list")) { |         if ( | ||||||
|  |           !object.hasOwnProperty("genre") || | ||||||
|  |           !object.hasOwnProperty("tag_list") | ||||||
|  |         ) { | ||||||
|           get_genres_soundcloud_recursive(arr, channel, i + 1, callback); |           get_genres_soundcloud_recursive(arr, channel, i + 1, callback); | ||||||
|           return; |           return; | ||||||
|         } |         } | ||||||
|         var genre = object.genre + ","; |         var genre = object.genre + ","; | ||||||
|             genre = genre.toLowerCase().split(",").concat(object.tag_list.toLowerCase().split('"')); |         genre = genre | ||||||
|  |           .toLowerCase() | ||||||
|  |           .split(",") | ||||||
|  |           .concat(object.tag_list.toLowerCase().split('"')); | ||||||
|         genre = genre.filter(filterFunction); |         genre = genre.filter(filterFunction); | ||||||
|  |  | ||||||
|             db.collection(channel).update({"id": song.id}, { |         db.collection(channel).update( | ||||||
|  |           { id: song.id }, | ||||||
|  |           { | ||||||
|             $set: { |             $set: { | ||||||
|                     "tags": genre |               tags: genre | ||||||
|             } |             } | ||||||
|             }, function(e,d) { |           }, | ||||||
|  |           function(e, d) { | ||||||
|             get_genres_soundcloud_recursive(arr, channel, i + 1, callback); |             get_genres_soundcloud_recursive(arr, channel, i + 1, callback); | ||||||
|             }); |           } | ||||||
|         } catch(e) { |         ); | ||||||
|  |       } catch (e) { | ||||||
|         console.log("errored 2", e); |         console.log("errored 2", e); | ||||||
|         get_genres_soundcloud_recursive(arr, channel, i + 1, callback); |         get_genres_soundcloud_recursive(arr, channel, i + 1, callback); | ||||||
|       } |       } | ||||||
|     }); |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_genres_list_recursive(list, channel, callback) { | function get_genres_list_recursive(list, channel, callback) { | ||||||
|   var youtube_array = []; |   var youtube_array = []; | ||||||
|   var soundcloud_array = []; |   var soundcloud_array = []; | ||||||
|     for(var i = 0; i < list.length; i++) { |   for (var i = 0; i < list.length; i++) { | ||||||
|         if(!list[i].hasOwnProperty("id")) continue; |     if (!list[i].hasOwnProperty("id")) continue; | ||||||
|         if(list[i].source == undefined || list[i].source == "youtube") { |     if (list[i].source == undefined || list[i].source == "youtube") { | ||||||
|       youtube_array.push(list[i]); |       youtube_array.push(list[i]); | ||||||
|         } |     } else if (list[i].source != undefined && list[i].source == "soundcloud") { | ||||||
|         else if(list[i].source != undefined && list[i].source == "soundcloud") { |  | ||||||
|       soundcloud_array.push(list[i]); |       soundcloud_array.push(list[i]); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   start_youtube_get(youtube_array, channel, function() { |   start_youtube_get(youtube_array, channel, function() { | ||||||
|     start_soundcloud_get(soundcloud_array, channel, function() { |     start_soundcloud_get(soundcloud_array, channel, function() { | ||||||
|             if(typeof(callback) == "function") callback(); |       if (typeof callback == "function") callback(); | ||||||
|         }) |     }); | ||||||
|     }) |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
| function start_youtube_get(arr, channel, callback) { | function start_youtube_get(arr, channel, callback) { | ||||||
|     get_genres_youtube_recursive(arr, channel, 0, callback) |   get_genres_youtube_recursive(arr, channel, 0, callback); | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_genres_youtube_recursive(arr, channel, i, callback) { | function get_genres_youtube_recursive(arr, channel, i, callback) { | ||||||
|     if(i >= arr.length) { |   if (i >= arr.length) { | ||||||
|         if(typeof(callback) == "function") callback(); |     if (typeof callback == "function") callback(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   var ids = []; |   var ids = []; | ||||||
|     for(var y = i; y < arr.length; y++) { |   for (var y = i; y < arr.length; y++) { | ||||||
|         if(ids.length >= 48) { |     if (ids.length >= 48) { | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     ids.push(arr[y].id); |     ids.push(arr[y].id); | ||||||
|   } |   } | ||||||
|     request({ |   request( | ||||||
|  |     { | ||||||
|       type: "GET", |       type: "GET", | ||||||
|         url: "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key="+key+"&id=" + ids.join(","), |       url: | ||||||
|     }, function(error, response, body) { |         "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + | ||||||
|         if(error) { |         key + | ||||||
|  |         "&id=" + | ||||||
|  |         ids.join(",") | ||||||
|  |     }, | ||||||
|  |     function(error, response, body) { | ||||||
|  |       if (error) { | ||||||
|         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); |         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|       var resp = JSON.parse(body); |       var resp = JSON.parse(body); | ||||||
|         if(!resp.hasOwnProperty("items")) { |       if (!resp.hasOwnProperty("items")) { | ||||||
|         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); |         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|         if(resp.items.length > 0) { |       if (resp.items.length > 0) { | ||||||
|             for(var z = 0; z < resp.items.length; z++) { |         for (var z = 0; z < resp.items.length; z++) { | ||||||
|                 if(!resp.items[z].hasOwnProperty("topicDetails")) continue; |           if (!resp.items[z].hasOwnProperty("topicDetails")) continue; | ||||||
|           var genre = resp.items[z].topicDetails.topicCategories; |           var genre = resp.items[z].topicDetails.topicCategories; | ||||||
|           genre = genre.join(","); |           genre = genre.join(","); | ||||||
|                 genre = genre.replace(new RegExp("https://en.wikipedia.org/wiki/", "g"), ""); |           genre = genre.replace( | ||||||
|                 genre = genre.replace(/_/g, " ").toLowerCase().split(","); |             new RegExp("https://en.wikipedia.org/wiki/", "g"), | ||||||
|  |             "" | ||||||
|  |           ); | ||||||
|  |           genre = genre | ||||||
|  |             .replace(/_/g, " ") | ||||||
|  |             .toLowerCase() | ||||||
|  |             .split(","); | ||||||
|           genre = genre.filter(filterFunction); |           genre = genre.filter(filterFunction); | ||||||
|           //console.log(resp.items[i].id + " - ", genre); |           //console.log(resp.items[i].id + " - ", genre); | ||||||
|                 db.collection(channel).update({"id": resp.items[z].id}, { |           db.collection(channel).update( | ||||||
|  |             { id: resp.items[z].id }, | ||||||
|  |             { | ||||||
|               $set: { |               $set: { | ||||||
|                         "tags": genre |                 tags: genre | ||||||
|               } |               } | ||||||
|                 }, function(e, d) { |             }, | ||||||
|                 }); |             function(e, d) {} | ||||||
|  |           ); | ||||||
|         } |         } | ||||||
|         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); |         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); | ||||||
|       } else { |       } else { | ||||||
|         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); |         get_genres_youtube_recursive(arr, channel, i + ids.length, callback); | ||||||
|       } |       } | ||||||
|     }); |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| function get_genres_youtube(ids, channel) { | function get_genres_youtube(ids, channel) { | ||||||
|     request({ |   request( | ||||||
|  |     { | ||||||
|       type: "GET", |       type: "GET", | ||||||
|         url: "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key="+key+"&id=" + ids, |       url: | ||||||
|     }, function(error, response, body) { |         "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + | ||||||
|         if(error) { |         key + | ||||||
|  |         "&id=" + | ||||||
|  |         ids | ||||||
|  |     }, | ||||||
|  |     function(error, response, body) { | ||||||
|  |       if (error) { | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|       var resp = JSON.parse(body); |       var resp = JSON.parse(body); | ||||||
|         if(!resp.hasOwnProperty("items")) { |       if (!resp.hasOwnProperty("items")) { | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|         if(resp.items.length > 0) { |       if (resp.items.length > 0) { | ||||||
|             for(var i = 0; i < resp.items.length; i++) { |         for (var i = 0; i < resp.items.length; i++) { | ||||||
|                 if(!resp.items[i].hasOwnProperty("topicDetails")) continue; |           if (!resp.items[i].hasOwnProperty("topicDetails")) continue; | ||||||
|           var genre = resp.items[i].topicDetails.topicCategories; |           var genre = resp.items[i].topicDetails.topicCategories; | ||||||
|           genre = genre.join(","); |           genre = genre.join(","); | ||||||
|                 genre = genre.replace(new RegExp("https://en.wikipedia.org/wiki/", "g"), ""); |           genre = genre.replace( | ||||||
|                 genre = genre.replace(/_/g, " ").toLowerCase().split(","); |             new RegExp("https://en.wikipedia.org/wiki/", "g"), | ||||||
|  |             "" | ||||||
|  |           ); | ||||||
|  |           genre = genre | ||||||
|  |             .replace(/_/g, " ") | ||||||
|  |             .toLowerCase() | ||||||
|  |             .split(","); | ||||||
|           genre = genre.filter(filterFunction); |           genre = genre.filter(filterFunction); | ||||||
|           //console.log(resp.items[i].id + " - ", genre); |           //console.log(resp.items[i].id + " - ", genre); | ||||||
|                 db.collection(channel).update({"id": resp.items[i].id}, { |           db.collection(channel).update( | ||||||
|  |             { id: resp.items[i].id }, | ||||||
|  |             { | ||||||
|               $set: { |               $set: { | ||||||
|                         "tags": genre |                 tags: genre | ||||||
|               } |               } | ||||||
|                 }, function(e, d) {}); |             }, | ||||||
|  |             function(e, d) {} | ||||||
|  |           ); | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     }); |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function get_correct_info(song_generated, channel, broadcast, callback) { | function get_correct_info(song_generated, channel, broadcast, callback) { | ||||||
|   //channel = channel.replace(/ /g,''); |   //channel = channel.replace(/ /g,''); | ||||||
|     request({ |   request( | ||||||
|  |     { | ||||||
|       type: "GET", |       type: "GET", | ||||||
|         url: "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key="+key+"&id=" + song_generated.id, |       url: | ||||||
|  |         "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + | ||||||
|     }, function(error, response, body) { |         key + | ||||||
|  |         "&id=" + | ||||||
|  |         song_generated.id | ||||||
|  |     }, | ||||||
|  |     function(error, response, body) { | ||||||
|       try { |       try { | ||||||
|         var resp = JSON.parse(body); |         var resp = JSON.parse(body); | ||||||
|             if(resp.items.length == 1) { |         if (resp.items.length == 1) { | ||||||
|                 var duration = parseInt(durationToSeconds(resp.items[0].contentDetails.duration)); |           var duration = parseInt( | ||||||
|  |             durationToSeconds(resp.items[0].contentDetails.duration) | ||||||
|  |           ); | ||||||
|           var title = resp.items[0].snippet.localized.title; |           var title = resp.items[0].snippet.localized.title; | ||||||
|           var genre = resp.items[0].topicDetails.topicCategories; |           var genre = resp.items[0].topicDetails.topicCategories; | ||||||
|           genre = genre.join(","); |           genre = genre.join(","); | ||||||
|                 genre = genre.replace(new RegExp("https://en.wikipedia.org/wiki/", "g"), ""); |           genre = genre.replace( | ||||||
|                 genre = genre.replace(/_/g, " ").toLowerCase().split(","); |             new RegExp("https://en.wikipedia.org/wiki/", "g"), | ||||||
|  |             "" | ||||||
|  |           ); | ||||||
|  |           genre = genre | ||||||
|  |             .replace(/_/g, " ") | ||||||
|  |             .toLowerCase() | ||||||
|  |             .split(","); | ||||||
|           genre = genre.filter(filterFunction); |           genre = genre.filter(filterFunction); | ||||||
|           //console.log(genre + " - ", song_generated.id); |           //console.log(genre + " - ", song_generated.id); | ||||||
|                 if(title != song_generated.title || duration < parseInt(song_generated.duration)) { |           if ( | ||||||
|                     if(title != song_generated.title) { |             title != song_generated.title || | ||||||
|  |             duration < parseInt(song_generated.duration) | ||||||
|  |           ) { | ||||||
|  |             if (title != song_generated.title) { | ||||||
|               song_generated.title = title; |               song_generated.title = title; | ||||||
|             } |             } | ||||||
|                     if(duration < parseInt(song_generated.duration)) { |             if (duration < parseInt(song_generated.duration)) { | ||||||
|               song_generated.duration = duration; |               song_generated.duration = duration; | ||||||
|               song_generated.start = 0; |               song_generated.start = 0; | ||||||
|               song_generated.end = duration; |               song_generated.end = duration; | ||||||
|             } |             } | ||||||
|                     db.collection(channel).update({"id": song_generated.id}, { |             db.collection(channel).update( | ||||||
|  |               { id: song_generated.id }, | ||||||
|  |               { | ||||||
|                 $set: { |                 $set: { | ||||||
|                             "duration": song_generated.duration, |                   duration: song_generated.duration, | ||||||
|                             "start": song_generated.start, |                   start: song_generated.start, | ||||||
|                             "end": song_generated.end, |                   end: song_generated.end, | ||||||
|                             "title": song_generated.title, |                   title: song_generated.title, | ||||||
|                             "tags": genre |                   tags: genre | ||||||
|                 } |                 } | ||||||
|                     }, function(err, docs) { |               }, | ||||||
|                         if(broadcast && docs.nModified == 1) { |               function(err, docs) { | ||||||
|  |                 if (broadcast && docs.nModified == 1) { | ||||||
|                   song_generated.new_id = song_generated.id; |                   song_generated.new_id = song_generated.id; | ||||||
|                   //if(song_generated.type == "video") |                   //if(song_generated.type == "video") | ||||||
|                             if(typeof(callback) == "function") { |                   if (typeof callback == "function") { | ||||||
|                     callback(song_generated, true); |                     callback(song_generated, true); | ||||||
|                   } else { |                   } else { | ||||||
|                                 io.to(channel).emit("channel", {type: "changed_values", value: song_generated}); |                     io.to(channel).emit("channel", { | ||||||
|                             } |                       type: "changed_values", | ||||||
|                         } else { |                       value: song_generated | ||||||
|                             if(typeof(callback) == "function") { |  | ||||||
|                                 callback(song_generated, true); |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                     }); |                     }); | ||||||
|  |                   } | ||||||
|                 } else { |                 } else { | ||||||
|                     db.collection(channel).update({"id": song_generated.id}, { |                   if (typeof callback == "function") { | ||||||
|  |                     callback(song_generated, true); | ||||||
|  |                   } | ||||||
|  |                 } | ||||||
|  |               } | ||||||
|  |             ); | ||||||
|  |           } else { | ||||||
|  |             db.collection(channel).update( | ||||||
|  |               { id: song_generated.id }, | ||||||
|  |               { | ||||||
|                 $set: { |                 $set: { | ||||||
|                             "tags": genre |                   tags: genre | ||||||
|                 } |                 } | ||||||
|                     }, function(e,d) { |               }, | ||||||
|                         if(typeof(callback) == "function") { |               function(e, d) { | ||||||
|  |                 if (typeof callback == "function") { | ||||||
|                   callback(song_generated, true); |                   callback(song_generated, true); | ||||||
|                 } |                 } | ||||||
|                     }); |               } | ||||||
|  |             ); | ||||||
|           } |           } | ||||||
|         } else { |         } else { | ||||||
|                 findSimilar(song_generated, channel, broadcast, callback) |           findSimilar(song_generated, channel, broadcast, callback); | ||||||
|         } |         } | ||||||
|         } catch(e){ |       } catch (e) { | ||||||
|             if(typeof(callback) == "function") { |         if (typeof callback == "function") { | ||||||
|           callback({}, false); |           callback({}, false); | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     }); |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function check_error_video(msg, channel) { | function check_error_video(msg, channel) { | ||||||
|     if(!msg.hasOwnProperty("id") || !msg.hasOwnProperty("title") || |   if ( | ||||||
|     typeof(msg.id) != "string" || typeof(msg.title) != "string") { |     !msg.hasOwnProperty("id") || | ||||||
|  |     !msg.hasOwnProperty("title") || | ||||||
|  |     typeof msg.id != "string" || | ||||||
|  |     typeof msg.title != "string" | ||||||
|  |   ) { | ||||||
|     var result = { |     var result = { | ||||||
|       id: { |       id: { | ||||||
|         expected: "string", |         expected: "string", | ||||||
|                 got: msg.hasOwnProperty("id") ? typeof(msg.id) : undefined, |         got: msg.hasOwnProperty("id") ? typeof msg.id : undefined | ||||||
|       }, |       }, | ||||||
|       title: { |       title: { | ||||||
|         expected: "string", |         expected: "string", | ||||||
|                 got: msg.hasOwnProperty("title") ? typeof(msg.title) : undefined, |         got: msg.hasOwnProperty("title") ? typeof msg.title : undefined | ||||||
|             }, |       } | ||||||
|     }; |     }; | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|     if(msg.source == "soundcloud") return; |   if (msg.source == "soundcloud") return; | ||||||
|   //channel = channel.replace(/ /g,''); |   //channel = channel.replace(/ /g,''); | ||||||
|     request({ |   request( | ||||||
|  |     { | ||||||
|       type: "GET", |       type: "GET", | ||||||
|         url: "https://www.googleapis.com/youtube/v3/videos?part=id&key="+key+"&id=" + msg.id, |       url: | ||||||
|  |         "https://www.googleapis.com/youtube/v3/videos?part=id&key=" + | ||||||
|     }, function(error, response, body) { |         key + | ||||||
|  |         "&id=" + | ||||||
|  |         msg.id | ||||||
|  |     }, | ||||||
|  |     function(error, response, body) { | ||||||
|       try { |       try { | ||||||
|         var resp = JSON.parse(body); |         var resp = JSON.parse(body); | ||||||
|             if(resp.pageInfo.totalResults == 0) { |         if (resp.pageInfo.totalResults == 0) { | ||||||
|           findSimilar(msg, channel, true, undefined); |           findSimilar(msg, channel, true, undefined); | ||||||
|         } |         } | ||||||
|         } catch(e){ |       } catch (e) { | ||||||
|         console.log(msg.id, key, e, body); |         console.log(msg.id, key, e, body); | ||||||
|       } |       } | ||||||
|     }); |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function findSimilar(msg, channel, broadcast, callback) { | function findSimilar(msg, channel, broadcast, callback) { | ||||||
|   //channel = channel.replace(/ /g,''); |   //channel = channel.replace(/ /g,''); | ||||||
|     var yt_url = "https://www.googleapis.com/youtube/v3/search?key="+key+"&videoEmbeddable=true&part=id&type=video&order=viewCount&safeSearch=none&maxResults=5&q=" + encodeURIComponent(msg.title); |   var yt_url = | ||||||
|     request({ |     "https://www.googleapis.com/youtube/v3/search?key=" + | ||||||
|  |     key + | ||||||
|  |     "&videoEmbeddable=true&part=id&type=video&order=viewCount&safeSearch=none&maxResults=5&q=" + | ||||||
|  |     encodeURIComponent(msg.title); | ||||||
|  |   request( | ||||||
|  |     { | ||||||
|       method: "GET", |       method: "GET", | ||||||
|         url: yt_url, |       url: yt_url | ||||||
|     }, function(error, response, body){ |     }, | ||||||
|  |     function(error, response, body) { | ||||||
|       var resp = JSON.parse(body); |       var resp = JSON.parse(body); | ||||||
|         if(resp.items.length > 0) { |       if (resp.items.length > 0) { | ||||||
|             var vid_url = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key="+key+"&id="; |         var vid_url = | ||||||
|             for(var i = 0; i < resp.items.length; i++) { |           "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key=" + | ||||||
|  |           key + | ||||||
|  |           "&id="; | ||||||
|  |         for (var i = 0; i < resp.items.length; i++) { | ||||||
|           vid_url += resp.items[i].id.videoId + ","; |           vid_url += resp.items[i].id.videoId + ","; | ||||||
|         } |         } | ||||||
|             request({ |         request( | ||||||
|  |           { | ||||||
|             type: "GET", |             type: "GET", | ||||||
|             url: vid_url |             url: vid_url | ||||||
|             }, function(error, response, body) { |           }, | ||||||
|  |           function(error, response, body) { | ||||||
|             var resp = JSON.parse(body); |             var resp = JSON.parse(body); | ||||||
|             var found = false; |             var found = false; | ||||||
|             var element = {}; |             var element = {}; | ||||||
|                 for(var i = 0; i < resp.items.length; i++) { |             for (var i = 0; i < resp.items.length; i++) { | ||||||
|                     if(similarity(resp.items[i].snippet.localized.title, msg.title) > 0.75) { |               if ( | ||||||
|  |                 similarity(resp.items[i].snippet.localized.title, msg.title) > | ||||||
|  |                 0.75 | ||||||
|  |               ) { | ||||||
|                 found = true; |                 found = true; | ||||||
|                 element = { |                 element = { | ||||||
|                   title: resp.items[i].snippet.localized.title, |                   title: resp.items[i].snippet.localized.title, | ||||||
|                             duration: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), |                   duration: parseInt( | ||||||
|  |                     durationToSeconds(resp.items[i].contentDetails.duration) | ||||||
|  |                   ), | ||||||
|                   id: resp.items[i].id, |                   id: resp.items[i].id, | ||||||
|                   start: 0, |                   start: 0, | ||||||
|                             end: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), |                   end: parseInt( | ||||||
|                         } |                     durationToSeconds(resp.items[i].contentDetails.duration) | ||||||
|  |                   ) | ||||||
|  |                 }; | ||||||
|                 break; |                 break; | ||||||
|               } |               } | ||||||
|             } |             } | ||||||
|                 if(found) { |             if (found) { | ||||||
|                     db.collection(channel).update({"id": msg.id}, { |               db.collection(channel).update( | ||||||
|  |                 { id: msg.id }, | ||||||
|  |                 { | ||||||
|                   $set: element |                   $set: element | ||||||
|                     }, function(err, docs) { |                 }, | ||||||
|                         if(docs && docs.hasOwnProperty("nModified") && docs.nModified == 1 && broadcast) { |                 function(err, docs) { | ||||||
|  |                   if ( | ||||||
|  |                     docs && | ||||||
|  |                     docs.hasOwnProperty("nModified") && | ||||||
|  |                     docs.nModified == 1 && | ||||||
|  |                     broadcast | ||||||
|  |                   ) { | ||||||
|                     element.new_id = element.id; |                     element.new_id = element.id; | ||||||
|                     element.id = msg.id; |                     element.id = msg.id; | ||||||
|                             if(!callback) { |                     if (!callback) { | ||||||
|                                 io.to(channel).emit("channel", {type: "changed_values", value: element}); |                       io.to(channel).emit("channel", { | ||||||
|  |                         type: "changed_values", | ||||||
|  |                         value: element | ||||||
|  |                       }); | ||||||
|                     } |                     } | ||||||
|                   } |                   } | ||||||
|                         if(typeof(callback) == "function") { |                   if (typeof callback == "function") { | ||||||
|                     msg.title = element.title; |                     msg.title = element.title; | ||||||
|                     msg.id = element.id; |                     msg.id = element.id; | ||||||
|                     msg.duration = element.duration; |                     msg.duration = element.duration; | ||||||
| @@ -451,13 +605,16 @@ function findSimilar(msg, channel, broadcast, callback) { | |||||||
|                     msg.end = element.end; |                     msg.end = element.end; | ||||||
|                     callback(msg, true); |                     callback(msg, true); | ||||||
|                   } |                   } | ||||||
|                     }); |                 } | ||||||
|                 } else if(typeof(callback) == "function") { |               ); | ||||||
|  |             } else if (typeof callback == "function") { | ||||||
|               callback({}, false); |               callback({}, false); | ||||||
|             } |             } | ||||||
|             }); |  | ||||||
|           } |           } | ||||||
|     }); |         ); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function similarity(s1, s2) { | function similarity(s1, s2) { | ||||||
| @@ -471,7 +628,9 @@ function similarity(s1, s2) { | |||||||
|   if (longerLength == 0) { |   if (longerLength == 0) { | ||||||
|     return 1.0; |     return 1.0; | ||||||
|   } |   } | ||||||
|     return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength); |   return ( | ||||||
|  |     (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength) | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function editDistance(s1, s2) { | function editDistance(s1, s2) { | ||||||
| @@ -482,31 +641,28 @@ function editDistance(s1, s2) { | |||||||
|   for (var i = 0; i <= s1.length; i++) { |   for (var i = 0; i <= s1.length; i++) { | ||||||
|     var lastValue = i; |     var lastValue = i; | ||||||
|     for (var j = 0; j <= s2.length; j++) { |     for (var j = 0; j <= s2.length; j++) { | ||||||
|             if (i == 0) |       if (i == 0) costs[j] = j; | ||||||
|             costs[j] = j; |  | ||||||
|       else { |       else { | ||||||
|         if (j > 0) { |         if (j > 0) { | ||||||
|           var newValue = costs[j - 1]; |           var newValue = costs[j - 1]; | ||||||
|           if (s1.charAt(i - 1) != s2.charAt(j - 1)) |           if (s1.charAt(i - 1) != s2.charAt(j - 1)) | ||||||
|                     newValue = Math.min(Math.min(newValue, lastValue), |             newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1; | ||||||
|                     costs[j]) + 1; |  | ||||||
|           costs[j - 1] = lastValue; |           costs[j - 1] = lastValue; | ||||||
|           lastValue = newValue; |           lastValue = newValue; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|         if (i > 0) |     if (i > 0) costs[s2.length] = lastValue; | ||||||
|         costs[s2.length] = lastValue; |  | ||||||
|   } |   } | ||||||
|   return costs[s2.length]; |   return costs[s2.length]; | ||||||
| } | } | ||||||
|  |  | ||||||
| function durationToSeconds(duration) { | function durationToSeconds(duration) { | ||||||
|   var matches = duration.match(time_regex); |   var matches = duration.match(time_regex); | ||||||
|     hours= parseInt(matches[12])||0; |   hours = parseInt(matches[12]) || 0; | ||||||
|     minutes= parseInt(matches[14])||0; |   minutes = parseInt(matches[14]) || 0; | ||||||
|     seconds= parseInt(matches[16])||0; |   seconds = parseInt(matches[16]) || 0; | ||||||
|     return hours*60*60+minutes*60+seconds; |   return hours * 60 * 60 + minutes * 60 + seconds; | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports.check_if_error_or_blocked = check_if_error_or_blocked; | module.exports.check_if_error_or_blocked = check_if_error_or_blocked; | ||||||
|   | |||||||
| @@ -1,58 +1,97 @@ | |||||||
| var Functions = require(pathThumbnails + '/handlers/functions.js'); | var Functions = require(pathThumbnails + "/handlers/functions.js"); | ||||||
| var Notifications = require(pathThumbnails + '/handlers/notifications.js'); | var Notifications = require(pathThumbnails + "/handlers/notifications.js"); | ||||||
| var crypto = require('crypto'); | var crypto = require("crypto"); | ||||||
| var db = require(pathThumbnails + '/handlers/db.js'); | var db = require(pathThumbnails + "/handlers/db.js"); | ||||||
|  |  | ||||||
| function thumbnail(msg, coll, guid, offline, socket) { | function thumbnail(msg, coll, guid, offline, socket) { | ||||||
|     if(msg.thumbnail != undefined && msg.channel && msg.channel != undefined && Functions.isUrl(msg.thumbnail)){ |   if ( | ||||||
|         if(typeof(msg.channel) != "string" || typeof(msg.thumbnail) != "string") |     msg.thumbnail != undefined && | ||||||
|              { |     msg.channel && | ||||||
|  |     msg.channel != undefined && | ||||||
|  |     Functions.isUrl(msg.thumbnail) | ||||||
|  |   ) { | ||||||
|  |     if (typeof msg.channel != "string" || typeof msg.thumbnail != "string") { | ||||||
|       var result = { |       var result = { | ||||||
|         channel: { |         channel: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("channel") ? typeof(msg.channel) : undefined, |           got: msg.hasOwnProperty("channel") ? typeof msg.channel : undefined | ||||||
|         }, |         }, | ||||||
|         pass: { |         pass: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("pass") ? typeof(msg.pass) : undefined, |           got: msg.hasOwnProperty("pass") ? typeof msg.pass : undefined | ||||||
|         }, |         }, | ||||||
|         thumbnail: { |         thumbnail: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("thumbnail") ? typeof(msg.thumbnail) : undefined, |           got: msg.hasOwnProperty("thumbnail") | ||||||
|  |             ? typeof msg.thumbnail | ||||||
|  |             : undefined | ||||||
|         }, |         }, | ||||||
|         adminpass: { |         adminpass: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("adminpass") ? typeof(msg.adminpass) : undefined, |           got: msg.hasOwnProperty("adminpass") | ||||||
|                     }, |             ? typeof msg.adminpass | ||||||
|  |             : undefined | ||||||
|  |         } | ||||||
|       }; |       }; | ||||||
|       socket.emit("update_required", result); |       socket.emit("update_required", result); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     //coll = coll.replace(/ /g,''); |     //coll = coll.replace(/ /g,''); | ||||||
|         Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass) { |     Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( | ||||||
|             if(userpass != "" || msg.pass == undefined) { |       userpass, | ||||||
|  |       adminpass | ||||||
|  |     ) { | ||||||
|  |       if (userpass != "" || msg.pass == undefined) { | ||||||
|         msg.pass = userpass; |         msg.pass = userpass; | ||||||
|             } else if(msg.hasOwnProperty("pass")){ |       } else if (msg.hasOwnProperty("pass")) { | ||||||
|                 msg.pass = crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64"); |         msg.pass = crypto | ||||||
|  |           .createHash("sha256") | ||||||
|  |           .update(Functions.decrypt_string(msg.pass)) | ||||||
|  |           .digest("base64"); | ||||||
|       } |       } | ||||||
|             if(adminpass != "" || msg.adminpass == undefined) { |       if (adminpass != "" || msg.adminpass == undefined) { | ||||||
|         msg.adminpass = Functions.hash_pass(adminpass); |         msg.adminpass = Functions.hash_pass(adminpass); | ||||||
|       } else { |       } else { | ||||||
|                 msg.adminpass = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(msg.adminpass),true)); |         msg.adminpass = Functions.hash_pass( | ||||||
|  |           Functions.hash_pass(Functions.decrypt_string(msg.adminpass), true) | ||||||
|  |         ); | ||||||
|       } |       } | ||||||
|             if(msg.thumbnail != "") { |       if (msg.thumbnail != "") { | ||||||
|         msg.thumbnail = msg.thumbnail.replace(/^https?\:\/\//i, ""); |         msg.thumbnail = msg.thumbnail.replace(/^https?\:\/\//i, ""); | ||||||
|                 if(msg.thumbnail.substring(0,2) != "//") msg.thumbnail = "//" + msg.thumbnail; |         if (msg.thumbnail.substring(0, 2) != "//") | ||||||
|  |           msg.thumbnail = "//" + msg.thumbnail; | ||||||
|       } |       } | ||||||
|       var channel = msg.channel.toLowerCase(); |       var channel = msg.channel.toLowerCase(); | ||||||
|       var hash = msg.adminpass; |       var hash = msg.adminpass; | ||||||
|             db.collection(channel + "_settings").find({id: "config"}, function(err, docs){ |       db.collection(channel + "_settings").find({ id: "config" }, function( | ||||||
|                 if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == msg.pass))) { |         err, | ||||||
|                     if(docs !== null && docs.length !== 0 && docs[0].adminpass !== "" && docs[0].adminpass == hash){ |         docs | ||||||
|                         db.collection("suggested_thumbnails").update({channel: channel}, {$set:{thumbnail: msg.thumbnail}}, {upsert:true}, function(err, docs){ |       ) { | ||||||
|                             Notifications.requested_change("thumbnail", msg.thumbnail, channel); |         if ( | ||||||
|  |           docs.length > 0 && | ||||||
|  |           (docs[0].userpass == undefined || | ||||||
|  |             docs[0].userpass == "" || | ||||||
|  |             (msg.hasOwnProperty("pass") && docs[0].userpass == msg.pass)) | ||||||
|  |         ) { | ||||||
|  |           if ( | ||||||
|  |             docs !== null && | ||||||
|  |             docs.length !== 0 && | ||||||
|  |             docs[0].adminpass !== "" && | ||||||
|  |             docs[0].adminpass == hash | ||||||
|  |           ) { | ||||||
|  |             db.collection("suggested_thumbnails").update( | ||||||
|  |               { channel: channel }, | ||||||
|  |               { $set: { thumbnail: msg.thumbnail } }, | ||||||
|  |               { upsert: true }, | ||||||
|  |               function(err, docs) { | ||||||
|  |                 Notifications.requested_change( | ||||||
|  |                   "thumbnail", | ||||||
|  |                   msg.thumbnail, | ||||||
|  |                   channel | ||||||
|  |                 ); | ||||||
|                 socket.emit("toast", "suggested_thumbnail"); |                 socket.emit("toast", "suggested_thumbnail"); | ||||||
|                         }); |               } | ||||||
|  |             ); | ||||||
|           } |           } | ||||||
|         } else { |         } else { | ||||||
|           socket.emit("auth_required"); |           socket.emit("auth_required"); | ||||||
| @@ -65,50 +104,85 @@ function thumbnail(msg, coll, guid, offline, socket) { | |||||||
| } | } | ||||||
|  |  | ||||||
| function description(msg, coll, guid, offline, socket) { | function description(msg, coll, guid, offline, socket) { | ||||||
|     if(msg.description && msg.channel && msg.description.length < 100){ |   if (msg.description && msg.channel && msg.description.length < 100) { | ||||||
|         if(typeof(msg.channel) != "string" || typeof(msg.description) != "string") { |     if (typeof msg.channel != "string" || typeof msg.description != "string") { | ||||||
|       var result = { |       var result = { | ||||||
|         channel: { |         channel: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("channel") ? typeof(msg.channel) : undefined, |           got: msg.hasOwnProperty("channel") ? typeof msg.channel : undefined | ||||||
|         }, |         }, | ||||||
|         pass: { |         pass: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("pass") ? typeof(msg.pass) : undefined, |           got: msg.hasOwnProperty("pass") ? typeof msg.pass : undefined | ||||||
|         }, |         }, | ||||||
|         description: { |         description: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("description") ? typeof(msg.description) : undefined, |           got: msg.hasOwnProperty("description") | ||||||
|  |             ? typeof msg.description | ||||||
|  |             : undefined | ||||||
|         }, |         }, | ||||||
|         adminpass: { |         adminpass: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("adminpass") ? typeof(msg.adminpass) : undefined, |           got: msg.hasOwnProperty("adminpass") | ||||||
|                     }, |             ? typeof msg.adminpass | ||||||
|  |             : undefined | ||||||
|  |         } | ||||||
|       }; |       }; | ||||||
|       socket.emit("update_required", result); |       socket.emit("update_required", result); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     //coll = coll.replace(/ /g,''); |     //coll = coll.replace(/ /g,''); | ||||||
|         Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass, gotten) { |     Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( | ||||||
|             if(userpass != "" || msg.pass == undefined) { |       userpass, | ||||||
|  |       adminpass, | ||||||
|  |       gotten | ||||||
|  |     ) { | ||||||
|  |       if (userpass != "" || msg.pass == undefined) { | ||||||
|         msg.pass = userpass; |         msg.pass = userpass; | ||||||
|             } else if(msg.hasOwnProperty("pass")) { |       } else if (msg.hasOwnProperty("pass")) { | ||||||
|                 msg.pass = crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64"); |         msg.pass = crypto | ||||||
|  |           .createHash("sha256") | ||||||
|  |           .update(Functions.decrypt_string(msg.pass)) | ||||||
|  |           .digest("base64"); | ||||||
|       } |       } | ||||||
|             if(adminpass != "" || msg.adminpass == undefined) { |       if (adminpass != "" || msg.adminpass == undefined) { | ||||||
|         msg.adminpass = Functions.hash_pass(adminpass); |         msg.adminpass = Functions.hash_pass(adminpass); | ||||||
|       } else { |       } else { | ||||||
|                 msg.adminpass = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(msg.adminpass), true)); |         msg.adminpass = Functions.hash_pass( | ||||||
|  |           Functions.hash_pass(Functions.decrypt_string(msg.adminpass), true) | ||||||
|  |         ); | ||||||
|       } |       } | ||||||
|       var channel = msg.channel.toLowerCase(); |       var channel = msg.channel.toLowerCase(); | ||||||
|       var hash = msg.adminpass; |       var hash = msg.adminpass; | ||||||
|             db.collection(channel + "_settings").find({id: "config"}, function(err, docs){ |       db.collection(channel + "_settings").find({ id: "config" }, function( | ||||||
|                 if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == msg.pass))) { |         err, | ||||||
|                     if(docs !== null && docs.length !== 0 && docs[0].adminpass !== "" && docs[0].adminpass == hash){ |         docs | ||||||
|                         db.collection("suggested_descriptions").update({channel: channel}, {$set:{description: msg.description}}, {upsert:true}, function(err, docs){ |       ) { | ||||||
|                             Notifications.requested_change("description", msg.description, channel); |         if ( | ||||||
|  |           docs.length > 0 && | ||||||
|  |           (docs[0].userpass == undefined || | ||||||
|  |             docs[0].userpass == "" || | ||||||
|  |             (msg.hasOwnProperty("pass") && docs[0].userpass == msg.pass)) | ||||||
|  |         ) { | ||||||
|  |           if ( | ||||||
|  |             docs !== null && | ||||||
|  |             docs.length !== 0 && | ||||||
|  |             docs[0].adminpass !== "" && | ||||||
|  |             docs[0].adminpass == hash | ||||||
|  |           ) { | ||||||
|  |             db.collection("suggested_descriptions").update( | ||||||
|  |               { channel: channel }, | ||||||
|  |               { $set: { description: msg.description } }, | ||||||
|  |               { upsert: true }, | ||||||
|  |               function(err, docs) { | ||||||
|  |                 Notifications.requested_change( | ||||||
|  |                   "description", | ||||||
|  |                   msg.description, | ||||||
|  |                   channel | ||||||
|  |                 ); | ||||||
|                 socket.emit("toast", "suggested_description"); |                 socket.emit("toast", "suggested_description"); | ||||||
|                         }); |               } | ||||||
|  |             ); | ||||||
|           } |           } | ||||||
|         } else { |         } else { | ||||||
|           socket.emit("auth_required"); |           socket.emit("auth_required"); | ||||||
| @@ -120,52 +194,80 @@ function description(msg, coll, guid, offline, socket) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| function rules(msg, coll, guid, offline, socket) { | function rules(msg, coll, guid, offline, socket) { | ||||||
|     if(msg.rules && msg.channel && msg.rules.length < 250){ |   if (msg.rules && msg.channel && msg.rules.length < 250) { | ||||||
|         if(typeof(msg.channel) != "string" || typeof(msg.rules) != "string") { |     if (typeof msg.channel != "string" || typeof msg.rules != "string") { | ||||||
|       var result = { |       var result = { | ||||||
|         channel: { |         channel: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("channel") ? typeof(msg.channel) : undefined, |           got: msg.hasOwnProperty("channel") ? typeof msg.channel : undefined | ||||||
|         }, |         }, | ||||||
|         pass: { |         pass: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("pass") ? typeof(msg.pass) : undefined, |           got: msg.hasOwnProperty("pass") ? typeof msg.pass : undefined | ||||||
|         }, |         }, | ||||||
|         rules: { |         rules: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("rules") ? typeof(msg.rules) : undefined, |           got: msg.hasOwnProperty("rules") ? typeof msg.rules : undefined | ||||||
|         }, |         }, | ||||||
|         adminpass: { |         adminpass: { | ||||||
|           expected: "string", |           expected: "string", | ||||||
|                         got: msg.hasOwnProperty("adminpass") ? typeof(msg.adminpass) : undefined, |           got: msg.hasOwnProperty("adminpass") | ||||||
|                     }, |             ? typeof msg.adminpass | ||||||
|  |             : undefined | ||||||
|  |         } | ||||||
|       }; |       }; | ||||||
|       socket.emit("update_required", result); |       socket.emit("update_required", result); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     //coll = coll.replace(/ /g,''); |     //coll = coll.replace(/ /g,''); | ||||||
|         Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass, gotten) { |     Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( | ||||||
|             if(userpass != "" || msg.pass == undefined) { |       userpass, | ||||||
|  |       adminpass, | ||||||
|  |       gotten | ||||||
|  |     ) { | ||||||
|  |       if (userpass != "" || msg.pass == undefined) { | ||||||
|         msg.pass = userpass; |         msg.pass = userpass; | ||||||
|             } else if(msg.hasOwnProperty("pass")) { |       } else if (msg.hasOwnProperty("pass")) { | ||||||
|                 msg.pass = crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64"); |         msg.pass = crypto | ||||||
|  |           .createHash("sha256") | ||||||
|  |           .update(Functions.decrypt_string(msg.pass)) | ||||||
|  |           .digest("base64"); | ||||||
|       } |       } | ||||||
|             if(adminpass != "" || msg.adminpass == undefined) { |       if (adminpass != "" || msg.adminpass == undefined) { | ||||||
|         msg.adminpass = Functions.hash_pass(adminpass); |         msg.adminpass = Functions.hash_pass(adminpass); | ||||||
|       } else { |       } else { | ||||||
|                 msg.adminpass = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(msg.adminpass), true)); |         msg.adminpass = Functions.hash_pass( | ||||||
|  |           Functions.hash_pass(Functions.decrypt_string(msg.adminpass), true) | ||||||
|  |         ); | ||||||
|       } |       } | ||||||
|       var channel = msg.channel.toLowerCase(); |       var channel = msg.channel.toLowerCase(); | ||||||
|       var hash = msg.adminpass; |       var hash = msg.adminpass; | ||||||
|             db.collection(channel + "_settings").find({id: "config"}, function(err, docs){ |       db.collection(channel + "_settings").find({ id: "config" }, function( | ||||||
|                 if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == msg.pass))) { |         err, | ||||||
|                     if(docs !== null && docs.length !== 0 && docs[0].adminpass !== "" && docs[0].adminpass == hash){ |         docs | ||||||
|                         db.collection("suggested_rules").update({channel: channel}, {$set:{rules: msg.rules}}, {upsert:true}, function(err, docs){ |       ) { | ||||||
|  |         if ( | ||||||
|  |           docs.length > 0 && | ||||||
|  |           (docs[0].userpass == undefined || | ||||||
|  |             docs[0].userpass == "" || | ||||||
|  |             (msg.hasOwnProperty("pass") && docs[0].userpass == msg.pass)) | ||||||
|  |         ) { | ||||||
|  |           if ( | ||||||
|  |             docs !== null && | ||||||
|  |             docs.length !== 0 && | ||||||
|  |             docs[0].adminpass !== "" && | ||||||
|  |             docs[0].adminpass == hash | ||||||
|  |           ) { | ||||||
|  |             db.collection("suggested_rules").update( | ||||||
|  |               { channel: channel }, | ||||||
|  |               { $set: { rules: msg.rules } }, | ||||||
|  |               { upsert: true }, | ||||||
|  |               function(err, docs) { | ||||||
|                 Notifications.requested_change("rules", msg.rules, channel); |                 Notifications.requested_change("rules", msg.rules, channel); | ||||||
|                 socket.emit("toast", "suggested_rules"); |                 socket.emit("toast", "suggested_rules"); | ||||||
|                         }); |               } | ||||||
|  |             ); | ||||||
|           } |           } | ||||||
|         } else { |         } else { | ||||||
|           socket.emit("auth_required"); |           socket.emit("auth_required"); | ||||||
| @@ -177,7 +279,6 @@ function rules(msg, coll, guid, offline, socket) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| module.exports.thumbnail = thumbnail; | module.exports.thumbnail = thumbnail; | ||||||
| module.exports.description = description; | module.exports.description = description; | ||||||
| module.exports.rules = rules; | module.exports.rules = rules; | ||||||
|   | |||||||
| @@ -1,23 +1,35 @@ | |||||||
| var express = require('express'); | var express = require("express"); | ||||||
| var router = express.Router(); | var router = express.Router(); | ||||||
| var path = require('path'); | var path = require("path"); | ||||||
| var mongo_db_cred = require(path.join(__dirname, '../../config/mongo_config.js')); | try { | ||||||
| var mongojs = require('mongojs'); |   var mongo_db_cred = require(path.join( | ||||||
|  |     __dirname, | ||||||
|  |     "../../config/mongo_config.js" | ||||||
|  |   )); | ||||||
|  | } catch (e) { | ||||||
|  |   console.log( | ||||||
|  |     "(!) Missing file - /config/mongo_config.js. Have a look at /config/mongo_config.example.js.  The server won't run without this existing." | ||||||
|  |   ); | ||||||
|  |   process.exit(1); | ||||||
|  | } | ||||||
|  | var mongojs = require("mongojs"); | ||||||
| var db = mongojs(mongo_db_cred.config); | var db = mongojs(mongo_db_cred.config); | ||||||
| var token_db = mongojs("tokens"); | var token_db = mongojs("tokens"); | ||||||
| var uniqid = require('uniqid'); | var uniqid = require("uniqid"); | ||||||
| var crypto = require('crypto'); | var crypto = require("crypto"); | ||||||
| var ObjectId = mongojs.ObjectId; | var ObjectId = mongojs.ObjectId; | ||||||
| var sIO = require(path.join(__dirname, '../../apps/client.js')).socketIO; | var sIO = require(path.join(__dirname, "../../apps/client.js")).socketIO; | ||||||
| var projects = require(pathThumbnails + "/handlers/aggregates.js"); | var projects = require(pathThumbnails + "/handlers/aggregates.js"); | ||||||
|  |  | ||||||
| router.use(function(req, res, next) { | router.use(function(req, res, next) { | ||||||
|   next(); // make sure we go to the next routes and don't stop here |   next(); // make sure we go to the next routes and don't stop here | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/lists').get(function(req, res){ | router.route("/api/lists").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|       db.collection("frontpage_lists").find().sort({count: -1},function(err, docs){ |     db.collection("frontpage_lists") | ||||||
|  |       .find() | ||||||
|  |       .sort({ count: -1 }, function(err, docs) { | ||||||
|         res.json(docs); |         res.json(docs); | ||||||
|       }); |       }); | ||||||
|   } else { |   } else { | ||||||
| @@ -25,9 +37,9 @@ router.route('/api/lists').get(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/thumbnails').get(function(req, res){ | router.route("/api/thumbnails").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|       db.collection("suggested_thumbnails").find(function(err, docs){ |     db.collection("suggested_thumbnails").find(function(err, docs) { | ||||||
|       res.json(docs); |       res.json(docs); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
| @@ -35,9 +47,9 @@ router.route('/api/thumbnails').get(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/descriptions').get(function(req, res){ | router.route("/api/descriptions").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|       db.collection("suggested_descriptions").find(function(err, docs){ |     db.collection("suggested_descriptions").find(function(err, docs) { | ||||||
|       res.json(docs); |       res.json(docs); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
| @@ -45,9 +57,9 @@ router.route('/api/descriptions').get(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/rules').get(function(req, res){ | router.route("/api/rules").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|       db.collection("suggested_rules").find(function(err, docs){ |     db.collection("suggested_rules").find(function(err, docs) { | ||||||
|       res.json(docs); |       res.json(docs); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
| @@ -55,43 +67,69 @@ router.route('/api/rules').get(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/approve_thumbnail').post(function(req, res){ | router.route("/api/approve_thumbnail").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("suggested_thumbnails").find({channel: channel}, function(err, docs){ |     db.collection("suggested_thumbnails").find({ channel: channel }, function( | ||||||
|  |       err, | ||||||
|  |       docs | ||||||
|  |     ) { | ||||||
|       var thumbnail = docs[0].thumbnail; |       var thumbnail = docs[0].thumbnail; | ||||||
|          db.collection("frontpage_lists").update({_id: channel}, {$set:{thumbnail: thumbnail}}, {upsert: true}, function(err, docs){ |       db.collection("frontpage_lists").update( | ||||||
|             db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{thumbnail: thumbnail}}, {upsert: true}, function(err, docs){ |         { _id: channel }, | ||||||
|                db.collection("suggested_thumbnails").remove({channel: channel}, function(err, docs){ |         { $set: { thumbnail: thumbnail } }, | ||||||
|                   db.collection(channel + "_settings").aggregate([ |         { upsert: true }, | ||||||
|  |         function(err, docs) { | ||||||
|  |           db.collection(channel + "_settings").update( | ||||||
|  |             { views: { $exists: true } }, | ||||||
|  |             { $set: { thumbnail: thumbnail } }, | ||||||
|  |             { upsert: true }, | ||||||
|  |             function(err, docs) { | ||||||
|  |               db.collection("suggested_thumbnails").remove( | ||||||
|  |                 { channel: channel }, | ||||||
|  |                 function(err, docs) { | ||||||
|  |                   db.collection(channel + "_settings").aggregate( | ||||||
|  |                     [ | ||||||
|                       { |                       { | ||||||
|                             "$match": { |                         $match: { | ||||||
|                           id: "config" |                           id: "config" | ||||||
|                         } |                         } | ||||||
|                       }, |                       }, | ||||||
|                       { |                       { | ||||||
|                             "$project": projects.toShowConfig |                         $project: projects.toShowConfig | ||||||
|                         }, |                       } | ||||||
|                     ], function(err, docs) { |                     ], | ||||||
|                      if(docs[0].adminpass !== "") docs[0].adminpass = true; |                     function(err, docs) { | ||||||
|                      if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; |                       if (docs[0].adminpass !== "") docs[0].adminpass = true; | ||||||
|  |                       if ( | ||||||
|  |                         docs[0].hasOwnProperty("userpass") && | ||||||
|  |                         docs[0].userpass != "" | ||||||
|  |                       ) | ||||||
|  |                         docs[0].userpass = true; | ||||||
|                       else docs[0].userpass = false; |                       else docs[0].userpass = false; | ||||||
|                       sIO.to(channel).emit("conf", docs); |                       sIO.to(channel).emit("conf", docs); | ||||||
|                       res.send(true); |                       res.send(true); | ||||||
|                   }); |                     } | ||||||
|                }); |                   ); | ||||||
|             }); |                 } | ||||||
|          }); |               ); | ||||||
|  |             } | ||||||
|  |           ); | ||||||
|  |         } | ||||||
|  |       ); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/deny_thumbnail').post(function(req, res){ | router.route("/api/deny_thumbnail").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("suggested_thumbnails").remove({channel: channel},function(err, docs){ |     db.collection("suggested_thumbnails").remove({ channel: channel }, function( | ||||||
|  |       err, | ||||||
|  |       docs | ||||||
|  |     ) { | ||||||
|       res.send(true); |       res.send(true); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
| @@ -99,42 +137,62 @@ router.route('/api/deny_thumbnail').post(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | router.route("/api/approve_rules").post(function(req, res) { | ||||||
| router.route('/api/approve_rules').post(function(req, res){ |   if (req.isAuthenticated()) { | ||||||
|    if(req.isAuthenticated()){ |  | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("suggested_rules").find({channel: channel}, function(err, docs){ |     db.collection("suggested_rules").find({ channel: channel }, function( | ||||||
|  |       err, | ||||||
|  |       docs | ||||||
|  |     ) { | ||||||
|       var rules = docs[0].rules; |       var rules = docs[0].rules; | ||||||
|          db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{rules: rules}}, {upsert: true}, function(err, docs){ |       db.collection(channel + "_settings").update( | ||||||
|             db.collection("suggested_rules").remove({channel: channel}, function(err, docs){ |         { views: { $exists: true } }, | ||||||
|                db.collection(channel + "_settings").aggregate([ |         { $set: { rules: rules } }, | ||||||
|  |         { upsert: true }, | ||||||
|  |         function(err, docs) { | ||||||
|  |           db.collection("suggested_rules").remove( | ||||||
|  |             { channel: channel }, | ||||||
|  |             function(err, docs) { | ||||||
|  |               db.collection(channel + "_settings").aggregate( | ||||||
|  |                 [ | ||||||
|                   { |                   { | ||||||
|                          "$match": { |                     $match: { | ||||||
|                       id: "config" |                       id: "config" | ||||||
|                     } |                     } | ||||||
|                   }, |                   }, | ||||||
|                   { |                   { | ||||||
|                          "$project": projects.toShowConfig |                     $project: projects.toShowConfig | ||||||
|                      }, |                   } | ||||||
|                  ], function(err, docs) { |                 ], | ||||||
|                    if(docs[0].adminpass !== "") docs[0].adminpass = true; |                 function(err, docs) { | ||||||
|                    if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; |                   if (docs[0].adminpass !== "") docs[0].adminpass = true; | ||||||
|  |                   if ( | ||||||
|  |                     docs[0].hasOwnProperty("userpass") && | ||||||
|  |                     docs[0].userpass != "" | ||||||
|  |                   ) | ||||||
|  |                     docs[0].userpass = true; | ||||||
|                   else docs[0].userpass = false; |                   else docs[0].userpass = false; | ||||||
|                   sIO.to(channel).emit("conf", docs); |                   sIO.to(channel).emit("conf", docs); | ||||||
|                   res.send(true); |                   res.send(true); | ||||||
|                 }); |                 } | ||||||
|             }); |               ); | ||||||
|          }); |             } | ||||||
|  |           ); | ||||||
|  |         } | ||||||
|  |       ); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/deny_rules').post(function(req, res){ | router.route("/api/deny_rules").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("suggested_rules").remove({channel: channel},function(err, docs){ |     db.collection("suggested_rules").remove({ channel: channel }, function( | ||||||
|  |       err, | ||||||
|  |       docs | ||||||
|  |     ) { | ||||||
|       res.send(true); |       res.send(true); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
| @@ -142,160 +200,235 @@ router.route('/api/deny_rules').post(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/remove_rules').post(function(req, res){ | router.route("/api/remove_rules").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{rules: ""}}, function(err, docs){ |     db.collection(channel + "_settings").update( | ||||||
|          db.collection(channel + "_settings").aggregate([ |       { views: { $exists: true } }, | ||||||
|  |       { $set: { rules: "" } }, | ||||||
|  |       function(err, docs) { | ||||||
|  |         db.collection(channel + "_settings").aggregate( | ||||||
|  |           [ | ||||||
|             { |             { | ||||||
|                   "$match": { |               $match: { | ||||||
|                 id: "config" |                 id: "config" | ||||||
|               } |               } | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
|                   "$project": projects.toShowConfig |               $project: projects.toShowConfig | ||||||
|               }, |             } | ||||||
|           ], function(err, docs) { |           ], | ||||||
|             if(docs[0].adminpass !== "") docs[0].adminpass = true; |           function(err, docs) { | ||||||
|             if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; |             if (docs[0].adminpass !== "") docs[0].adminpass = true; | ||||||
|  |             if (docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") | ||||||
|  |               docs[0].userpass = true; | ||||||
|             else docs[0].userpass = false; |             else docs[0].userpass = false; | ||||||
|             sIO.to(channel).emit("conf", docs); |             sIO.to(channel).emit("conf", docs); | ||||||
|             res.send(true); |             res.send(true); | ||||||
|         }); |           } | ||||||
|       }); |         ); | ||||||
|  |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/approve_description').post(function(req, res){ | router.route("/api/approve_description").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("suggested_descriptions").find({channel: channel}, function(err, docs){ |     db.collection("suggested_descriptions").find({ channel: channel }, function( | ||||||
|  |       err, | ||||||
|  |       docs | ||||||
|  |     ) { | ||||||
|       var description = docs[0].description; |       var description = docs[0].description; | ||||||
|          db.collection("frontpage_lists").update({_id: channel}, {$set:{description: description}}, {upsert: true}, function(err, docs){ |       db.collection("frontpage_lists").update( | ||||||
|            db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{description: description}}, function(err, docs){ |         { _id: channel }, | ||||||
|              db.collection("suggested_descriptions").remove({channel: channel}, function(err, docs){ |         { $set: { description: description } }, | ||||||
|                 db.collection(channel + "_settings").aggregate([ |         { upsert: true }, | ||||||
|  |         function(err, docs) { | ||||||
|  |           db.collection(channel + "_settings").update( | ||||||
|  |             { views: { $exists: true } }, | ||||||
|  |             { $set: { description: description } }, | ||||||
|  |             function(err, docs) { | ||||||
|  |               db.collection("suggested_descriptions").remove( | ||||||
|  |                 { channel: channel }, | ||||||
|  |                 function(err, docs) { | ||||||
|  |                   db.collection(channel + "_settings").aggregate( | ||||||
|  |                     [ | ||||||
|                       { |                       { | ||||||
|                           "$match": { |                         $match: { | ||||||
|                           id: "config" |                           id: "config" | ||||||
|                         } |                         } | ||||||
|                       }, |                       }, | ||||||
|                       { |                       { | ||||||
|                           "$project": projects.toShowConfig |                         $project: projects.toShowConfig | ||||||
|                       }, |                       } | ||||||
|                   ], function(err, docs) { |                     ], | ||||||
|                    if(docs[0].adminpass !== "") docs[0].adminpass = true; |                     function(err, docs) { | ||||||
|                    if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; |                       if (docs[0].adminpass !== "") docs[0].adminpass = true; | ||||||
|  |                       if ( | ||||||
|  |                         docs[0].hasOwnProperty("userpass") && | ||||||
|  |                         docs[0].userpass != "" | ||||||
|  |                       ) | ||||||
|  |                         docs[0].userpass = true; | ||||||
|                       else docs[0].userpass = false; |                       else docs[0].userpass = false; | ||||||
|                       sIO.to(channel).emit("conf", docs); |                       sIO.to(channel).emit("conf", docs); | ||||||
|                       res.send(true); |                       res.send(true); | ||||||
|                 }); |                     } | ||||||
|               }); |                   ); | ||||||
|             }); |                 } | ||||||
|           }); |               ); | ||||||
|  |             } | ||||||
|  |           ); | ||||||
|  |         } | ||||||
|  |       ); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/deny_description').post(function(req, res){ | router.route("/api/deny_description").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("suggested_descriptions").remove({channel: channel}, 1,function(err, docs){ |     db.collection("suggested_descriptions").remove( | ||||||
|  |       { channel: channel }, | ||||||
|  |       1, | ||||||
|  |       function(err, docs) { | ||||||
|         res.send(true); |         res.send(true); | ||||||
|       }); |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/remove_thumbnail').post(function(req, res){ | router.route("/api/remove_thumbnail").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("frontpage_lists").update({_id: channel}, {$set:{thumbnail: ""}}, function(err, docs){ |     db.collection("frontpage_lists").update( | ||||||
|          db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{thumbnail: ""}}, function(err, docs){ |       { _id: channel }, | ||||||
|             db.collection(channel + "_settings").aggregate([ |       { $set: { thumbnail: "" } }, | ||||||
|  |       function(err, docs) { | ||||||
|  |         db.collection(channel + "_settings").update( | ||||||
|  |           { views: { $exists: true } }, | ||||||
|  |           { $set: { thumbnail: "" } }, | ||||||
|  |           function(err, docs) { | ||||||
|  |             db.collection(channel + "_settings").aggregate( | ||||||
|  |               [ | ||||||
|                 { |                 { | ||||||
|                       "$match": { |                   $match: { | ||||||
|                     id: "config" |                     id: "config" | ||||||
|                   } |                   } | ||||||
|                 }, |                 }, | ||||||
|                 { |                 { | ||||||
|                       "$project": projects.toShowConfig |                   $project: projects.toShowConfig | ||||||
|                   }, |                 } | ||||||
|               ], function(err, docs) { |               ], | ||||||
|                 if(docs[0].adminpass !== "") docs[0].adminpass = true; |               function(err, docs) { | ||||||
|                 if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; |                 if (docs[0].adminpass !== "") docs[0].adminpass = true; | ||||||
|  |                 if ( | ||||||
|  |                   docs[0].hasOwnProperty("userpass") && | ||||||
|  |                   docs[0].userpass != "" | ||||||
|  |                 ) | ||||||
|  |                   docs[0].userpass = true; | ||||||
|                 else docs[0].userpass = false; |                 else docs[0].userpass = false; | ||||||
|                 sIO.to(channel).emit("conf", docs); |                 sIO.to(channel).emit("conf", docs); | ||||||
|                 res.send(true); |                 res.send(true); | ||||||
|              }); |               } | ||||||
|          }); |             ); | ||||||
|       }); |           } | ||||||
|  |         ); | ||||||
|  |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/remove_description').post(function(req, res){ | router.route("/api/remove_description").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var channel = req.body.channel; |     var channel = req.body.channel; | ||||||
|       db.collection("frontpage_lists").update({_id: channel}, {$set:{description: ""}}, function(err, docs){ |     db.collection("frontpage_lists").update( | ||||||
|          db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{description: ""}}, function(err, docs){ |       { _id: channel }, | ||||||
|             db.collection(channel + "_settings").aggregate([ |       { $set: { description: "" } }, | ||||||
|  |       function(err, docs) { | ||||||
|  |         db.collection(channel + "_settings").update( | ||||||
|  |           { views: { $exists: true } }, | ||||||
|  |           { $set: { description: "" } }, | ||||||
|  |           function(err, docs) { | ||||||
|  |             db.collection(channel + "_settings").aggregate( | ||||||
|  |               [ | ||||||
|                 { |                 { | ||||||
|                       "$match": { |                   $match: { | ||||||
|                     id: "config" |                     id: "config" | ||||||
|                   } |                   } | ||||||
|                 }, |                 }, | ||||||
|                 { |                 { | ||||||
|                       "$project": projects.toShowConfig |                   $project: projects.toShowConfig | ||||||
|                   }, |                 } | ||||||
|               ], function(err, docs) { |               ], | ||||||
|                 if(docs[0].adminpass !== "") docs[0].adminpass = true; |               function(err, docs) { | ||||||
|                 if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; |                 if (docs[0].adminpass !== "") docs[0].adminpass = true; | ||||||
|  |                 if ( | ||||||
|  |                   docs[0].hasOwnProperty("userpass") && | ||||||
|  |                   docs[0].userpass != "" | ||||||
|  |                 ) | ||||||
|  |                   docs[0].userpass = true; | ||||||
|                 else docs[0].userpass = false; |                 else docs[0].userpass = false; | ||||||
|                 sIO.to(channel).emit("conf", docs); |                 sIO.to(channel).emit("conf", docs); | ||||||
|                 res.send(true); |                 res.send(true); | ||||||
|              }); |               } | ||||||
|          }); |             ); | ||||||
|       }); |           } | ||||||
|  |         ); | ||||||
|  |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/names').get(function(req, res) { | router.route("/api/names").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|       db.collection("registered_users").find({_id: {$exists: true}}, {_id: 1, icon: 1}, function(err, docs) { |     db.collection("registered_users").find( | ||||||
|  |       { _id: { $exists: true } }, | ||||||
|  |       { _id: 1, icon: 1 }, | ||||||
|  |       function(err, docs) { | ||||||
|         res.json(docs); |         res.json(docs); | ||||||
|       }) |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/names').post(function(req, res) { | router.route("/api/names").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()) { |   if (req.isAuthenticated()) { | ||||||
|     var icon = req.body.icon; |     var icon = req.body.icon; | ||||||
|     var name = req.body.name; |     var name = req.body.name; | ||||||
|       db.collection("registered_users").update({_id: name}, {$set: {icon: icon}}, function(err, docs) { |     db.collection("registered_users").update( | ||||||
|          if(err) res.send(false); |       { _id: name }, | ||||||
|  |       { $set: { icon: icon } }, | ||||||
|  |       function(err, docs) { | ||||||
|  |         if (err) res.send(false); | ||||||
|         else res.send(true); |         else res.send(true); | ||||||
|       }); |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/names').delete(function(req, res) { | router.route("/api/names").delete(function(req, res) { | ||||||
|     if(req.isAuthenticated()) { |   if (req.isAuthenticated()) { | ||||||
|     var name = req.body.name; |     var name = req.body.name; | ||||||
|        db.collection("registered_users").remove({_id: name}, function(err, docs) { |     db.collection("registered_users").remove({ _id: name }, function( | ||||||
|           if(err) res.send(false); |       err, | ||||||
|  |       docs | ||||||
|  |     ) { | ||||||
|  |       if (err) res.send(false); | ||||||
|       else res.send(true); |       else res.send(true); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
| @@ -303,26 +436,30 @@ router.route('/api/names').delete(function(req, res) { | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/token').get(function(req, res){ | router.route("/api/token").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|       token_db.collection("tokens").find(function(err, docs){ |     token_db.collection("tokens").find(function(err, docs) { | ||||||
|          if(docs.length == 1){ |       if (docs.length == 1) { | ||||||
|             res.json({token: docs[0].token}); |         res.json({ token: docs[0].token }); | ||||||
|       } else { |       } else { | ||||||
|             var id = new Buffer(makeid()).toString('base64'); |         var id = new Buffer(makeid()).toString("base64"); | ||||||
|             token_db.collection("tokens").insert({token: id}, function(err, docs){ |         token_db | ||||||
|                res.json({token: id}); |           .collection("tokens") | ||||||
|  |           .insert({ token: id }, function(err, docs) { | ||||||
|  |             res.json({ token: id }); | ||||||
|           }); |           }); | ||||||
|       } |       } | ||||||
|       }) |     }); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/api_token').get(function(req, res) { | router.route("/api/api_token").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()) { |   if (req.isAuthenticated()) { | ||||||
|       token_db.collection("api_token").find({token: {$exists: true}}, function(err, all) { |     token_db | ||||||
|  |       .collection("api_token") | ||||||
|  |       .find({ token: { $exists: true } }, function(err, all) { | ||||||
|         res.json(all); |         res.json(all); | ||||||
|       }); |       }); | ||||||
|   } else { |   } else { | ||||||
| @@ -330,44 +467,56 @@ router.route('/api/api_token').get(function(req, res) { | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/api_token').delete(function(req, res){ | router.route("/api/api_token").delete(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var id = req.body.id; |     var id = req.body.id; | ||||||
|       token_db.collection("api_token").remove({_id: ObjectId(id)}, function(err, success) { |     token_db | ||||||
|          if(err) { |       .collection("api_token") | ||||||
|  |       .remove({ _id: ObjectId(id) }, function(err, success) { | ||||||
|  |         if (err) { | ||||||
|           res.send("failed"); |           res.send("failed"); | ||||||
|           return; |           return; | ||||||
|         } |         } | ||||||
|         res.send("success"); |         res.send("success"); | ||||||
|       }) |       }); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/api_token').put(function(req, res){ | router.route("/api/api_token").put(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var id = req.body.id; |     var id = req.body.id; | ||||||
|     var limit = req.body.limit; |     var limit = req.body.limit; | ||||||
|       if(limit < 0) { |     if (limit < 0) { | ||||||
|       res.sendStatus(500); |       res.sendStatus(500); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|       token_db.collection("api_token").update({_id: ObjectId(id)}, {$set: {limit: limit}}, function(err, success) { |     token_db | ||||||
|          if(err) { |       .collection("api_token") | ||||||
|  |       .update({ _id: ObjectId(id) }, { $set: { limit: limit } }, function( | ||||||
|  |         err, | ||||||
|  |         success | ||||||
|  |       ) { | ||||||
|  |         if (err) { | ||||||
|           res.sendStatus(500); |           res.sendStatus(500); | ||||||
|           return; |           return; | ||||||
|         } |         } | ||||||
|         res.sendStatus(200); |         res.sendStatus(200); | ||||||
|       }) |       }); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/api_token').post(function(req, res){ | router.route("/api/api_token").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var name = req.body.name; |     var name = req.body.name; | ||||||
|       var id = crypto.createHash('sha256').update(uniqid()).digest('base64'); |     var id = crypto | ||||||
|       token_db.collection("api_token").insert({name: name, token: id, usage: 0}, function(err, docs){ |       .createHash("sha256") | ||||||
|          token_db.collection("api_token").find({token: id}, function(err, d) { |       .update(uniqid()) | ||||||
|             res.json({token: id, _id: d[0]._id}); |       .digest("base64"); | ||||||
|  |     token_db | ||||||
|  |       .collection("api_token") | ||||||
|  |       .insert({ name: name, token: id, usage: 0 }, function(err, docs) { | ||||||
|  |         token_db.collection("api_token").find({ token: id }, function(err, d) { | ||||||
|  |           res.json({ token: id, _id: d[0]._id }); | ||||||
|         }); |         }); | ||||||
|       }); |       }); | ||||||
|   } else { |   } else { | ||||||
| @@ -375,14 +524,17 @@ router.route('/api/api_token').post(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/delete').post(function(req, res){ | router.route("/api/delete").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var list = req.body._id; |     var list = req.body._id; | ||||||
|       db.collection(list).drop(function(err, docs){ |     db.collection(list).drop(function(err, docs) { | ||||||
|          db.collection(list + "_settings").drop(function(err, docs){ |       db.collection(list + "_settings").drop(function(err, docs) { | ||||||
|             db.collection("frontpage_lists").remove({_id: list}, function(err, docs){ |         db.collection("frontpage_lists").remove({ _id: list }, function( | ||||||
|  |           err, | ||||||
|  |           docs | ||||||
|  |         ) { | ||||||
|           res.send(true); |           res.send(true); | ||||||
|             }) |         }); | ||||||
|       }); |       }); | ||||||
|     }); |     }); | ||||||
|   } else { |   } else { | ||||||
| @@ -390,67 +542,84 @@ router.route('/api/delete').post(function(req, res){ | |||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/remove_token').get(function(req, res){ | router.route("/api/remove_token").get(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|       token_db.collection("tokens").find(function(err, docs){ |     token_db.collection("tokens").find(function(err, docs) { | ||||||
|          if(docs.length == 1){ |       if (docs.length == 1) { | ||||||
|             token_db.collection("tokens").remove({token: docs[0].token}, function(err, docs){ |         token_db | ||||||
|  |           .collection("tokens") | ||||||
|  |           .remove({ token: docs[0].token }, function(err, docs) { | ||||||
|             res.send(true); |             res.send(true); | ||||||
|             }) |           }); | ||||||
|       } else { |       } else { | ||||||
|         res.send(false); |         res.send(false); | ||||||
|       } |       } | ||||||
|       }) |     }); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/pinned').post(function(req, res){ | router.route("/api/pinned").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var to_pin = req.body._id; |     var to_pin = req.body._id; | ||||||
|       db.collection("frontpage_lists").update({pinned:1}, {$set:{pinned:0}}, function(err, resp){ |     db.collection("frontpage_lists").update( | ||||||
|         	db.collection("frontpage_lists").update({_id:to_pin}, {$set:{pinned:1}}, function(err, resp){ |       { pinned: 1 }, | ||||||
|  |       { $set: { pinned: 0 } }, | ||||||
|  |       function(err, resp) { | ||||||
|  |         db.collection("frontpage_lists").update( | ||||||
|  |           { _id: to_pin }, | ||||||
|  |           { $set: { pinned: 1 } }, | ||||||
|  |           function(err, resp) { | ||||||
|             res.send(true); |             res.send(true); | ||||||
|         	}); |           } | ||||||
|         }); |         ); | ||||||
|  |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/admin').post(function(req, res){ | router.route("/api/admin").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var to_remove = req.body._id; |     var to_remove = req.body._id; | ||||||
|       db.collection(to_remove + "_settings").update({views: {$exists: true}}, {$set:{adminpass: ""}}, function(err, docs){ |     db.collection(to_remove + "_settings").update( | ||||||
|  |       { views: { $exists: true } }, | ||||||
|  |       { $set: { adminpass: "" } }, | ||||||
|  |       function(err, docs) { | ||||||
|         res.send(true); |         res.send(true); | ||||||
|       }); |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/userpass').post(function(req, res){ | router.route("/api/userpass").post(function(req, res) { | ||||||
|    if(req.isAuthenticated()){ |   if (req.isAuthenticated()) { | ||||||
|     var to_remove = req.body._id; |     var to_remove = req.body._id; | ||||||
|       db.collection(to_remove + "_settings").update({views: {$exists: true}}, {$set:{userpass: ""}}, function(err, docs){ |     db.collection(to_remove + "_settings").update( | ||||||
|  |       { views: { $exists: true } }, | ||||||
|  |       { $set: { userpass: "" } }, | ||||||
|  |       function(err, docs) { | ||||||
|         res.send(true); |         res.send(true); | ||||||
|       }); |       } | ||||||
|  |     ); | ||||||
|   } else { |   } else { | ||||||
|     res.send(false); |     res.send(false); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| function makeid() | function makeid() { | ||||||
| { |  | ||||||
|   var text = ""; |   var text = ""; | ||||||
|    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |   var possible = | ||||||
|  |     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||||||
|  |  | ||||||
|    for( var i=0; i < 20; i++ ) |   for (var i = 0; i < 20; i++) | ||||||
|     text += possible.charAt(Math.floor(Math.random() * possible.length)); |     text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||||||
|  |  | ||||||
|   return text; |   return text; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| module.exports = router; | module.exports = router; | ||||||
|   | |||||||
| @@ -13,7 +13,7 @@ try { | |||||||
| } catch (e) { | } catch (e) { | ||||||
|   allowed_key = ["***"]; |   allowed_key = ["***"]; | ||||||
|   console.log( |   console.log( | ||||||
|     "Allowed API-key for skipping songs from API has not been configured, so all keys are allowed by default (!). Have a look at config/allowed_api.example.js" |     "(!) Missing file - /config/allowed_api.js Have a look at /config/allowed_api.example.js." | ||||||
|   ); |   ); | ||||||
| } | } | ||||||
| var crypto = require("crypto"); | var crypto = require("crypto"); | ||||||
|   | |||||||
| @@ -1,24 +1,33 @@ | |||||||
| var express = require('express'); | var express = require("express"); | ||||||
| const path = require('path'); | const path = require("path"); | ||||||
| var router = express.Router(); | var router = express.Router(); | ||||||
| router.use(function(req, res, next) { | router.use(function(req, res, next) { | ||||||
|   next(); // make sure we go to the next routes and don't stop here |   next(); // make sure we go to the next routes and don't stop here | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/favicon.ico').get(function(req, res, next) { | router.route("/favicon.ico").get(function(req, res, next) { | ||||||
|     res.sendFile(path.join(pathThumbnails, '/public/assets/images/favicon.ico')); |   res.sendFile(path.join(pathThumbnails, "/public/assets/images/favicon.ico")); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/browserconfig.xml').get(function(req, res, next) { | router.route("/browserconfig.xml").get(function(req, res, next) { | ||||||
|     res.sendFile(path.join(pathThumbnails, '/public/assets/images/browserconfig.xml')); |   res.sendFile( | ||||||
|  |     path.join(pathThumbnails, "/public/assets/images/browserconfig.xml") | ||||||
|  |   ); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/apple-touch-icon.png').get(function(req, res, next) { | router.route("/apple-touch-icon.png").get(function(req, res, next) { | ||||||
|     res.sendFile(path.join(pathThumbnails, '/public/assets/images/apple-touch-icon.png')); |   res.sendFile( | ||||||
|  |     path.join(pathThumbnails, "/public/assets/images/apple-touch-icon.png") | ||||||
|  |   ); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/apple-touch-icon-precomposed.png').get(function(req, res, next) { | router.route("/apple-touch-icon-precomposed.png").get(function(req, res, next) { | ||||||
|     res.sendFile(path.join(pathThumbnails, '/public/assets/images/apple-touch-icon-precomposed.png')); |   res.sendFile( | ||||||
|  |     path.join( | ||||||
|  |       pathThumbnails, | ||||||
|  |       "/public/assets/images/apple-touch-icon-precomposed.png" | ||||||
|  |     ) | ||||||
|  |   ); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| module.exports = router; | module.exports = router; | ||||||
|   | |||||||
| @@ -1,69 +1,76 @@ | |||||||
| var express = require('express'); | var express = require("express"); | ||||||
| var router = express.Router(); | var router = express.Router(); | ||||||
| var path = require('path'); | var path = require("path"); | ||||||
| var year = new Date().getYear()+1900; | var year = new Date().getYear() + 1900; | ||||||
| var path = require('path'); | var path = require("path"); | ||||||
| var analytics = "xx"; | var analytics = "xx"; | ||||||
| var google = {}; | var google = {}; | ||||||
| var adsense = "xx"; | var adsense = "xx"; | ||||||
| var adds = false; | var adds = false; | ||||||
| var mongojs = require('mongojs'); | var mongojs = require("mongojs"); | ||||||
| var token_db = mongojs("tokens"); | var token_db = mongojs("tokens"); | ||||||
| var Functions = require(pathThumbnails + '/handlers/functions.js'); | var Functions = require(pathThumbnails + "/handlers/functions.js"); | ||||||
| var Frontpage = require(pathThumbnails + '/handlers/frontpage.js'); | var Frontpage = require(pathThumbnails + "/handlers/frontpage.js"); | ||||||
|  |  | ||||||
| var db = require(pathThumbnails + '/handlers/db.js'); | var db = require(pathThumbnails + "/handlers/db.js"); | ||||||
| //var db = require(pathThumbnails + '/handlers/db.js'); | //var db = require(pathThumbnails + '/handlers/db.js'); | ||||||
|  |  | ||||||
| try { | try { | ||||||
|     google = require(path.join(path.join(__dirname, '../../config/'), 'google.js')); |   google = require(path.join( | ||||||
|  |     path.join(__dirname, "../../config/"), | ||||||
|  |     "google.js" | ||||||
|  |   )); | ||||||
|   analytics = google.analytics; |   analytics = google.analytics; | ||||||
|   adsense = google.adsense; |   adsense = google.adsense; | ||||||
| } catch(e) { | } catch (e) { | ||||||
|   console.log("No analytics-id found"); |   console.log("No analytics-id found"); | ||||||
| } | } | ||||||
|  |  | ||||||
| try { | try { | ||||||
|     var Recaptcha = require('express-recaptcha'); |   var Recaptcha = require("express-recaptcha"); | ||||||
|     var recaptcha_config = require(path.join(path.join(__dirname, '../../config/'), 'recaptcha.js')); |   var recaptcha_config = require(path.join( | ||||||
|  |     path.join(__dirname, "../../config/"), | ||||||
|  |     "recaptcha.js" | ||||||
|  |   )); | ||||||
|   var RECAPTCHA_SITE_KEY = recaptcha_config.site; |   var RECAPTCHA_SITE_KEY = recaptcha_config.site; | ||||||
|   var RECAPTCHA_SECRET_KEY = recaptcha_config.key; |   var RECAPTCHA_SECRET_KEY = recaptcha_config.key; | ||||||
|   var recaptcha = new Recaptcha(RECAPTCHA_SITE_KEY, RECAPTCHA_SECRET_KEY); |   var recaptcha = new Recaptcha(RECAPTCHA_SITE_KEY, RECAPTCHA_SECRET_KEY); | ||||||
| } catch(e) { | } catch (e) { | ||||||
|     console.log("Error - missing file"); |   console.log( | ||||||
|     console.log("Seems you forgot to create the file recaptcha.js in /server/config/. Have a look at recaptcha.example.js."); |     "(!) Missing file - /config/recaptcha.js Have a look at /config/recaptcha.example.js." | ||||||
|  |   ); | ||||||
|   var recaptcha = { |   var recaptcha = { | ||||||
|     middleware: { |     middleware: { | ||||||
|       render: (req, res, next) => { |       render: (req, res, next) => { | ||||||
|                 res.recaptcha = "" |         res.recaptcha = ""; | ||||||
|                 next() |         next(); | ||||||
|             } |  | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |   }; | ||||||
| } | } | ||||||
|  |  | ||||||
| router.use(recaptcha.middleware.render, function(req, res, next) { | router.use(recaptcha.middleware.render, function(req, res, next) { | ||||||
|   next(); // make sure we go to the next routes and don't stop here |   next(); // make sure we go to the next routes and don't stop here | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/:channel_name').get(function(req, res, next){ | router.route("/:channel_name").get(function(req, res, next) { | ||||||
|   channel(req, res, next); |   channel(req, res, next); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/r/:base64data').get(function(req, res, next){ | router.route("/r/:base64data").get(function(req, res, next) { | ||||||
|     var channelToRedirect = Buffer.from(req.params.base64data, 'base64'); |   var channelToRedirect = Buffer.from(req.params.base64data, "base64"); | ||||||
|     res.redirect('/' + channelToRedirect); |   res.redirect("/" + channelToRedirect); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/').get(function(req, res, next){ | router.route("/").get(function(req, res, next) { | ||||||
|   root(req, res, next); |   root(req, res, next); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/').post(function(req, res, next){ | router.route("/").post(function(req, res, next) { | ||||||
|   root(req, res, next); |   root(req, res, next); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/embed').get(function(req, res, next) { | router.route("/api/embed").get(function(req, res, next) { | ||||||
|   var data = { |   var data = { | ||||||
|     year: year, |     year: year, | ||||||
|     type: "video", |     type: "video", | ||||||
| @@ -72,16 +79,16 @@ router.route('/api/embed').get(function(req, res, next) { | |||||||
|     analytics: analytics, |     analytics: analytics, | ||||||
|     stylesheet: "embed.css", |     stylesheet: "embed.css", | ||||||
|     embed: true, |     embed: true, | ||||||
|         og_image: "https://zoff.me/assets/images/small-square.jpg", |     og_image: "https://zoff.me/assets/images/small-square.jpg" | ||||||
|     } |   }; | ||||||
|     res.render('layouts/client/embed', data); |   res.render("layouts/client/embed", data); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/oauth').get(function(req, res, next) { | router.route("/api/oauth").get(function(req, res, next) { | ||||||
|     res.sendFile(path.join(pathThumbnails, '/public/assets/html/callback.html')); |   res.sendFile(path.join(pathThumbnails, "/public/assets/html/callback.html")); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/apply').get(function(req, res, next) { | router.route("/api/apply").get(function(req, res, next) { | ||||||
|   var data = { |   var data = { | ||||||
|     year: year, |     year: year, | ||||||
|     javascript_file: "token.min.js", |     javascript_file: "token.min.js", | ||||||
| @@ -95,17 +102,22 @@ router.route('/api/apply').get(function(req, res, next) { | |||||||
|     correct: false, |     correct: false, | ||||||
|     stylesheet: "style.css", |     stylesheet: "style.css", | ||||||
|     embed: false, |     embed: false, | ||||||
|         og_image: "https://zoff.me/assets/images/small-square.jpg", |     og_image: "https://zoff.me/assets/images/small-square.jpg" | ||||||
|     } |   }; | ||||||
|     res.render('layouts/client/token', data); |   res.render("layouts/client/token", data); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.route('/api/apply/:id').get(function(req, res) { | router.route("/api/apply/:id").get(function(req, res) { | ||||||
|   var id = req.params.id; |   var id = req.params.id; | ||||||
|     token_db.collection('api_links').find({id: id}, function(err, result) { |   token_db.collection("api_links").find({ id: id }, function(err, result) { | ||||||
|         if(result.length == 1) { |     if (result.length == 1) { | ||||||
|             token_db.collection('api_links').remove({id: id}, function(e,d) { |       token_db.collection("api_links").remove({ id: id }, function(e, d) { | ||||||
|                 token_db.collection('api_token').update({token: result[0].token}, {$set: {active: true}}, function(e,d) { |         token_db | ||||||
|  |           .collection("api_token") | ||||||
|  |           .update( | ||||||
|  |             { token: result[0].token }, | ||||||
|  |             { $set: { active: true } }, | ||||||
|  |             function(e, d) { | ||||||
|               var data = { |               var data = { | ||||||
|                 year: year, |                 year: year, | ||||||
|                 javascript_file: "token.min.js", |                 javascript_file: "token.min.js", | ||||||
| @@ -119,10 +131,11 @@ router.route('/api/apply/:id').get(function(req, res) { | |||||||
|                 correct: true, |                 correct: true, | ||||||
|                 stylesheet: "style.css", |                 stylesheet: "style.css", | ||||||
|                 embed: false, |                 embed: false, | ||||||
|                         og_image: "https://zoff.me/assets/images/small-square.jpg", |                 og_image: "https://zoff.me/assets/images/small-square.jpg" | ||||||
|  |               }; | ||||||
|  |               res.render("layouts/client/token", data); | ||||||
|             } |             } | ||||||
|                     res.render('layouts/client/token', data); |           ); | ||||||
|                 }); |  | ||||||
|       }); |       }); | ||||||
|     } else { |     } else { | ||||||
|       var data = { |       var data = { | ||||||
| @@ -133,28 +146,31 @@ router.route('/api/apply/:id').get(function(req, res) { | |||||||
|         adsense: adsense, |         adsense: adsense, | ||||||
|         adds: adds, |         adds: adds, | ||||||
|         activated: false, |         activated: false, | ||||||
|                 token:"", |         token: "", | ||||||
|         type: "website", |         type: "website", | ||||||
|         correct: false, |         correct: false, | ||||||
|         stylesheet: "style.css", |         stylesheet: "style.css", | ||||||
|         embed: false, |         embed: false, | ||||||
|                 og_image: "https://zoff.me/assets/images/small-square.jpg", |         og_image: "https://zoff.me/assets/images/small-square.jpg" | ||||||
|             } |       }; | ||||||
|             res.render('layouts/client/token', data); |       res.render("layouts/client/token", data); | ||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  |  | ||||||
| function root(req, res, next) { | function root(req, res, next) { | ||||||
|     try{ |   try { | ||||||
|         var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; |     var url = req.headers["x-forwarded-host"] | ||||||
|         var subdomain = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'].split(".") : req.headers.host.split(":")[0].split("."); |       ? req.headers["x-forwarded-host"] | ||||||
|  |       : req.headers.host.split(":")[0]; | ||||||
|  |     var subdomain = req.headers["x-forwarded-host"] | ||||||
|  |       ? req.headers["x-forwarded-host"].split(".") | ||||||
|  |       : req.headers.host.split(":")[0].split("."); | ||||||
|     /*if(url != "zoff.me" && url != "admin.localhost" && url != "admin.zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { |     /*if(url != "zoff.me" && url != "admin.localhost" && url != "admin.zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { | ||||||
|             res.redirect("https://zoff.me"); |             res.redirect("https://zoff.me"); | ||||||
|             return; |             return; | ||||||
|         }*/ |         }*/ | ||||||
|         if(subdomain[0] == "remote") { |     if (subdomain[0] == "remote") { | ||||||
|       var data = { |       var data = { | ||||||
|         year: year, |         year: year, | ||||||
|         javascript_file: "remote.min.js", |         javascript_file: "remote.min.js", | ||||||
| @@ -166,10 +182,10 @@ function root(req, res, next) { | |||||||
|         stylesheet: "style.css", |         stylesheet: "style.css", | ||||||
|         embed: false, |         embed: false, | ||||||
|         client: false, |         client: false, | ||||||
|                 og_image: "https://zoff.me/assets/images/small-square.jpg", |         og_image: "https://zoff.me/assets/images/small-square.jpg" | ||||||
|             } |       }; | ||||||
|             res.render('layouts/client/remote', data); |       res.render("layouts/client/remote", data); | ||||||
|         } else if(subdomain[0] == "www") { |     } else if (subdomain[0] == "www") { | ||||||
|       res.redirect("https://zoff.me"); |       res.redirect("https://zoff.me"); | ||||||
|     } else { |     } else { | ||||||
|       var data = { |       var data = { | ||||||
| @@ -184,14 +200,17 @@ function root(req, res, next) { | |||||||
|         embed: false, |         embed: false, | ||||||
|         client: false, |         client: false, | ||||||
|         og_image: "https://zoff.me/assets/images/small-square.jpg", |         og_image: "https://zoff.me/assets/images/small-square.jpg", | ||||||
|                 channels: [], |         channels: [] | ||||||
|             } |       }; | ||||||
|             if(subdomain[0] == "client") { |       if (subdomain[0] == "client") { | ||||||
|         data.client = true; |         data.client = true; | ||||||
|       } |       } | ||||||
|             Frontpage.get_frontpage_lists(function(err, docs){ |       Frontpage.get_frontpage_lists(function(err, docs) { | ||||||
|                 db.collection("connected_users").find({"_id": "total_users"}, function(err, tot) { |         db.collection("connected_users").find({ _id: "total_users" }, function( | ||||||
|                     if(docs.length > 0) { |           err, | ||||||
|  |           tot | ||||||
|  |         ) { | ||||||
|  |           if (docs.length > 0) { | ||||||
|             data.channels_exist = true; |             data.channels_exist = true; | ||||||
|             data.channels = docs.slice(0, 12); |             data.channels = docs.slice(0, 12); | ||||||
|             data.channel_list = JSON.stringify(docs); |             data.channel_list = JSON.stringify(docs); | ||||||
| @@ -201,26 +220,29 @@ function root(req, res, next) { | |||||||
|             data.channel_list = []; |             data.channel_list = []; | ||||||
|           } |           } | ||||||
|           data.viewers = tot[0].total_users.length; |           data.viewers = tot[0].total_users.length; | ||||||
|                     res.render('layouts/client/frontpage', data); |           res.render("layouts/client/frontpage", data); | ||||||
|         }); |         }); | ||||||
|       }); |       }); | ||||||
|  |  | ||||||
|     } |     } | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     console.log(e); |     console.log(e); | ||||||
|     //res.redirect("https://zoff.me"); |     //res.redirect("https://zoff.me"); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| function channel(req, res, next) { | function channel(req, res, next) { | ||||||
|     try{ |   try { | ||||||
|         var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; |     var url = req.headers["x-forwarded-host"] | ||||||
|         var subdomain = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'].split(".") : req.headers.host.split(":")[0].split("."); |       ? req.headers["x-forwarded-host"] | ||||||
|  |       : req.headers.host.split(":")[0]; | ||||||
|  |     var subdomain = req.headers["x-forwarded-host"] | ||||||
|  |       ? req.headers["x-forwarded-host"].split(".") | ||||||
|  |       : req.headers.host.split(":")[0].split("."); | ||||||
|     /*if(url != "zoff.me" && url != "admin.localhost" && url != "admin.zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { |     /*if(url != "zoff.me" && url != "admin.localhost" && url != "admin.zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { | ||||||
|             res.redirect("https://zoff.me"); |             res.redirect("https://zoff.me"); | ||||||
|             return; |             return; | ||||||
|         }*/ |         }*/ | ||||||
|         if(subdomain[0] == "remote") { |     if (subdomain[0] == "remote") { | ||||||
|       var data = { |       var data = { | ||||||
|         year: year, |         year: year, | ||||||
|         javascript_file: "remote.min.js", |         javascript_file: "remote.min.js", | ||||||
| @@ -232,13 +254,13 @@ function channel(req, res, next) { | |||||||
|         stylesheet: "style.css", |         stylesheet: "style.css", | ||||||
|         embed: false, |         embed: false, | ||||||
|         client: false, |         client: false, | ||||||
|                 og_image: "https://zoff.me/assets/images/small-square.jpg", |         og_image: "https://zoff.me/assets/images/small-square.jpg" | ||||||
|             } |       }; | ||||||
|             res.render('layouts/client/remote', data); |       res.render("layouts/client/remote", data); | ||||||
|         } else if(subdomain.length >= 2 && subdomain[0] == "www") { |     } else if (subdomain.length >= 2 && subdomain[0] == "www") { | ||||||
|       res.redirect("https://zoff.me"); |       res.redirect("https://zoff.me"); | ||||||
|     } else { |     } else { | ||||||
|             if(req.params.channel_name == "o_callback") { |       if (req.params.channel_name == "o_callback") { | ||||||
|         res.redirect("/api/oauth"); |         res.redirect("/api/oauth"); | ||||||
|         //res.sendFile(path.join(pathThumbnails, '/public/assets/html/callback.html')); |         //res.sendFile(path.join(pathThumbnails, '/public/assets/html/callback.html')); | ||||||
|       } else { |       } else { | ||||||
| @@ -266,20 +288,20 @@ function channel(req, res, next) { | |||||||
|           type: "video", |           type: "video", | ||||||
|           stylesheet: "style.css", |           stylesheet: "style.css", | ||||||
|           embed: false, |           embed: false, | ||||||
|                         client:false, |           client: false, | ||||||
|           og_image: "https://zoff.me/assets/images/small-square.jpg" |           og_image: "https://zoff.me/assets/images/small-square.jpg" | ||||||
|                     } |         }; | ||||||
|                     if(subdomain[0] == "client") { |         if (subdomain[0] == "client") { | ||||||
|           data.client = true; |           data.client = true; | ||||||
|         } |         } | ||||||
|                     if(req.params.channel_name == "404") { |         if (req.params.channel_name == "404") { | ||||||
|           res.status(404); |           res.status(404); | ||||||
|         } |         } | ||||||
|                     res.render('layouts/client/channel', data); |         res.render("layouts/client/channel", data); | ||||||
|         //}); |         //}); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     } catch(e) { |   } catch (e) { | ||||||
|     res.redirect("https://zoff.me"); |     res.redirect("https://zoff.me"); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user