2013-06-04 22:26:17 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2013 Calvin Montgomery
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-03-24 17:23:48 +00:00
|
|
|
var mysql = require("mysql-libmysqlclient");
|
2013-05-26 01:01:32 +00:00
|
|
|
var Logger = require("./logger");
|
|
|
|
var Media = require("./media").Media;
|
2013-05-28 15:40:06 +00:00
|
|
|
var bcrypt = require("bcrypt");
|
2013-05-29 18:19:43 +00:00
|
|
|
var hashlib = require("node_hash");
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var db = false;
|
|
|
|
var SERVER = "";
|
|
|
|
var USER = "";
|
|
|
|
var DATABASE = "";
|
|
|
|
var PASSWORD = "";
|
|
|
|
var CONFIG = {};
|
2013-05-30 18:44:14 +00:00
|
|
|
var global_bans = {};
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
function setup(cfg) {
|
|
|
|
SERVER = cfg.MYSQL_SERVER;
|
|
|
|
USER = cfg.MYSQL_USER;
|
|
|
|
DATABASE = cfg.MYSQL_DB;
|
|
|
|
PASSWORD = cfg.MYSQL_PASSWORD;
|
|
|
|
CONFIG = cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConnection() {
|
|
|
|
if(db && db.connectedSync()) {
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(SERVER, USER, PASSWORD, DATABASE);
|
2013-03-28 23:51:08 +00:00
|
|
|
if(!db.connectedSync()) {
|
2013-06-19 21:54:27 +00:00
|
|
|
Logger.errlog.log("DB connection failed");
|
2013-03-28 23:51:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
if(CONFIG.DEBUG) {
|
|
|
|
db._querySync = db.querySync;
|
|
|
|
db.querySync = function(q) {
|
|
|
|
Logger.syslog.log("DEBUG: " + q);
|
|
|
|
return this._querySync(q);
|
|
|
|
}
|
|
|
|
}
|
2013-04-02 19:39:56 +00:00
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2013-06-03 22:37:30 +00:00
|
|
|
function sqlEscape(obj) {
|
|
|
|
if(obj === undefined || obj === null)
|
|
|
|
return "NULL";
|
|
|
|
|
|
|
|
if(typeof obj === "boolean")
|
|
|
|
return obj ? "true" : "false";
|
|
|
|
|
|
|
|
if(typeof obj === "number")
|
|
|
|
return obj + "";
|
|
|
|
|
|
|
|
if(typeof obj === "object")
|
|
|
|
return "'object'";
|
|
|
|
|
|
|
|
if(typeof obj === "string") {
|
|
|
|
obj = obj.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
|
|
|
|
switch(s) {
|
|
|
|
case "\0": return "\\0";
|
|
|
|
case "\n": return "\\n";
|
|
|
|
case "\r": return "\\r";
|
|
|
|
case "\b": return "\\b";
|
|
|
|
case "\t": return "\\t";
|
|
|
|
case "\x1a": return "\\Z";
|
|
|
|
default: return "\\" + s;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return "'" + obj + "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function createQuery(template, args) {
|
|
|
|
var last = -1;
|
|
|
|
while(template.indexOf("?", last) >= 0) {
|
|
|
|
var idx = template.indexOf("?", last);
|
|
|
|
var arg = args.shift();
|
2013-06-03 22:37:30 +00:00
|
|
|
arg = sqlEscape(arg);
|
2013-05-26 01:01:32 +00:00
|
|
|
var first = template.substring(0, idx);
|
|
|
|
template = first + template.substring(idx).replace("?", arg);
|
2013-06-03 22:37:30 +00:00
|
|
|
last = idx + arg.length;
|
2013-04-27 16:48:36 +00:00
|
|
|
}
|
2013-06-04 16:26:16 +00:00
|
|
|
template = template.replace(/`'/g, "`");
|
|
|
|
template = template.replace(/'`/g, "`");
|
2013-05-26 01:01:32 +00:00
|
|
|
return template;
|
2013-04-27 16:48:36 +00:00
|
|
|
}
|
2013-04-02 19:39:56 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function init() {
|
|
|
|
var db = getConnection();
|
2013-04-02 19:39:56 +00:00
|
|
|
if(!db) {
|
2013-03-06 22:02:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
// Create channel table
|
|
|
|
var query = ["CREATE TABLE IF NOT EXISTS `channels` (",
|
|
|
|
"`id` INT NOT NULL AUTO_INCREMENT,",
|
|
|
|
"`name` VARCHAR(255) NOT NULL,",
|
|
|
|
"PRIMARY KEY(`id`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
2013-03-06 22:02:40 +00:00
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to create channels table");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create registration table
|
|
|
|
query = ["CREATE TABLE IF NOT EXISTS `registrations` (",
|
|
|
|
"`id` INT NOT NULL AUTO_INCREMENT,",
|
|
|
|
"`uname` VARCHAR(20) NOT NULL,",
|
|
|
|
"`pw` VARCHAR(64) NOT NULL,",
|
|
|
|
"`global_rank` INT NOT NULL,",
|
|
|
|
"`session_hash` VARCHAR(64) NOT NULL,",
|
|
|
|
"`expire` BIGINT NOT NULL,",
|
|
|
|
"`profile_image` VARCHAR(255) NOT NULL,",
|
|
|
|
"`profile_text` TEXT NOT NULL,",
|
2013-05-30 18:52:00 +00:00
|
|
|
"`email` VARCHAR(255) NOT NULL,",
|
2013-05-26 01:01:32 +00:00
|
|
|
"PRIMARY KEY (`id`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to create registrations table");
|
2013-03-06 22:02:40 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
// Create global bans table
|
|
|
|
query = ["CREATE TABLE IF NOT EXISTS `global_bans` (",
|
|
|
|
"`ip` VARCHAR(15) NOT NULL,",
|
|
|
|
"`note` VARCHAR(255) NOT NULL,",
|
|
|
|
"PRIMARY KEY (`ip`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
2013-04-28 06:30:18 +00:00
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to create global ban table");
|
2013-04-28 06:30:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
refreshGlobalBans();
|
2013-05-28 15:40:06 +00:00
|
|
|
|
|
|
|
// Create password reset table
|
|
|
|
query = ["CREATE TABLE IF NOT EXISTS `password_reset` (",
|
|
|
|
"`ip` VARCHAR(15) NOT NULL,",
|
|
|
|
"`name` VARCHAR(20) NOT NULL,",
|
|
|
|
"`hash` VARCHAR(64) NOT NULL,",
|
|
|
|
"`email` VARCHAR(255) NOT NULL,",
|
|
|
|
"`expire` BIGINT NOT NULL,",
|
|
|
|
"PRIMARY KEY (`name`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to create password reset table");
|
|
|
|
}
|
2013-06-01 15:59:04 +00:00
|
|
|
|
|
|
|
// Create user playlist table
|
|
|
|
query = ["CREATE TABLE IF NOT EXISTS `user_playlists` (",
|
|
|
|
"`user` VARCHAR(20) NOT NULL,",
|
|
|
|
"`name` VARCHAR(255) NOT NULL,",
|
|
|
|
"`contents` MEDIUMTEXT NOT NULL,",
|
2013-06-02 17:54:58 +00:00
|
|
|
"`count` INT NOT NULL,",
|
|
|
|
"`time` INT NOT NULL,",
|
2013-06-01 15:59:04 +00:00
|
|
|
"PRIMARY KEY (`name`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to create playlist table");
|
|
|
|
}
|
2013-06-18 01:02:48 +00:00
|
|
|
|
|
|
|
// Create user aliases table
|
|
|
|
query = ["CREATE TABLE IF NOT EXISTS `aliases` (",
|
|
|
|
"`visit_id` INT NOT NULL AUTO_INCREMENT,",
|
|
|
|
"`ip` VARCHAR(15) NOT NULL,",
|
|
|
|
"`name` VARCHAR(20) NOT NULL,",
|
|
|
|
"`time` BIGINT NOT NULL,",
|
|
|
|
"PRIMARY KEY (`visit_id`), INDEX (`ip`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to create aliases table");
|
|
|
|
}
|
2013-03-06 22:02:40 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
/* REGION Global Bans */
|
|
|
|
|
|
|
|
function checkGlobalBan(ip) {
|
|
|
|
const re = /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
|
|
|
|
var s16 = ip.replace(re, "$1.$2");
|
|
|
|
var s24 = ip.replace(re, "$1.$2.$3");
|
|
|
|
return (ip in global_bans ||
|
|
|
|
s16 in global_bans ||
|
|
|
|
s24 in global_bans);
|
2013-04-28 06:30:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function refreshGlobalBans() {
|
|
|
|
var db = getConnection();
|
2013-05-03 17:52:14 +00:00
|
|
|
if(!db) {
|
2013-05-26 01:01:32 +00:00
|
|
|
return;
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = "SELECT * FROM `global_bans` WHERE 1";
|
2013-05-03 17:52:14 +00:00
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to load global bans");
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
else {
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
global_bans = {};
|
|
|
|
for(var i = 0; i < rows.length; i++) {
|
|
|
|
global_bans[rows[i].ip] = rows[i].note;
|
|
|
|
}
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
2013-05-27 19:43:37 +00:00
|
|
|
return global_bans;
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function globalBanIP(ip, reason) {
|
|
|
|
var db = getConnection();
|
2013-05-03 17:52:14 +00:00
|
|
|
if(!db) {
|
2013-05-26 01:01:32 +00:00
|
|
|
return;
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"INSERT INTO `global_bans` VALUES (?, ?)",
|
|
|
|
[ip, reason]
|
|
|
|
);
|
|
|
|
return db.querySync(query);
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function globalUnbanIP(ip) {
|
|
|
|
var db = getConnection();
|
2013-05-03 17:52:14 +00:00
|
|
|
if(!db) {
|
2013-05-26 01:01:32 +00:00
|
|
|
return;
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"DELETE FROM `global_bans` WHERE ip=?",
|
|
|
|
[ip]
|
|
|
|
);
|
|
|
|
|
|
|
|
return db.querySync(query);
|
2013-05-03 17:52:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
/* REGION Channel Registration/Loading */
|
|
|
|
|
|
|
|
function registerChannel(name) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!name.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
// Library table
|
|
|
|
var query = ["CREATE TABLE `?` (",
|
|
|
|
"`id` VARCHAR(255) NOT NULL,",
|
|
|
|
"`title` VARCHAR(255) NOT NULL,",
|
|
|
|
"`seconds` INT NOT NULL,",
|
|
|
|
"`type` VARCHAR(2) NOT NULL,",
|
|
|
|
"PRIMARY KEY (`id`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
query = createQuery(query, ["chan_" + name + "_library"]);
|
|
|
|
|
2013-03-06 22:02:40 +00:00
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to create table: chan_"+name+"_library");
|
|
|
|
return false;
|
2013-03-06 22:02:40 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
// Rank table
|
|
|
|
query = ["CREATE TABLE `?` (",
|
|
|
|
"`name` VARCHAR(32) NOT NULL,",
|
|
|
|
"`rank` INT NOT NULL,",
|
|
|
|
"UNIQUE (`name`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
query = createQuery(query, ["chan_" + name + "_ranks"]);
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to create table: chan_"+name+"_ranks");
|
|
|
|
return false;
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
// Ban table
|
|
|
|
query = ["CREATE TABLE `?` (",
|
|
|
|
"`ip` VARCHAR(15) NOT NULL,",
|
|
|
|
"`name` VARCHAR(32) NOT NULL,",
|
|
|
|
"`banner` VARCHAR(32) NOT NULL,",
|
|
|
|
"PRIMARY KEY (`ip`))",
|
|
|
|
"ENGINE = MyISAM;"].join("");
|
|
|
|
query = createQuery(query, ["chan_" + name + "_bans"]);
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to create table: chan_"+name+"_bans");
|
|
|
|
return false;
|
2013-04-03 17:47:41 +00:00
|
|
|
}
|
2013-03-06 22:02:40 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
// Insert into channel table
|
|
|
|
query = createQuery(
|
|
|
|
"INSERT INTO `channels` VALUES (NULL, ?)",
|
|
|
|
[name]
|
|
|
|
);
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
2013-04-02 19:39:56 +00:00
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to insert into channel table: " + name);
|
|
|
|
return false;
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadChannel(chan) {
|
2013-05-28 01:12:07 +00:00
|
|
|
if(!chan.name.match(/^[a-zA-Z0-9-_]+$/)) {
|
2013-05-27 19:43:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
2013-05-28 01:12:07 +00:00
|
|
|
return false;
|
2013-03-06 22:02:40 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
var query = createQuery(
|
|
|
|
"SELECT * FROM `channels` WHERE name=?",
|
|
|
|
[chan.name]
|
|
|
|
);
|
|
|
|
|
2013-03-06 22:02:40 +00:00
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to query channel table");
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
else {
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
if(rows.length == 0) {
|
|
|
|
// Unregistered
|
|
|
|
Logger.syslog.log("Channel " + chan.name + " is unregistered");
|
|
|
|
return;
|
2013-05-21 16:17:01 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
// Database is case insensitive
|
|
|
|
else if(rows[0].name != chan.name) {
|
|
|
|
chan.name = rows[0].name;
|
|
|
|
}
|
|
|
|
chan.registered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load channel library
|
|
|
|
query = createQuery(
|
|
|
|
"SELECT * FROM `?`",
|
|
|
|
["chan_" + chan.name + "_library"]
|
|
|
|
);
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to load channel library: " + chan.name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
for(var i = 0; i < rows.length; i++) {
|
|
|
|
var r = rows[i];
|
|
|
|
var m = new Media(r.id, r.title, r.seconds, r.type);
|
|
|
|
chan.library[r.id] = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load channel bans
|
|
|
|
query = createQuery(
|
|
|
|
"SELECT * FROM `?`",
|
|
|
|
["chan_" + chan.name + "_bans"]
|
|
|
|
);
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to load channel bans: " + chan.name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
for(var i = 0; i < rows.length; i++) {
|
|
|
|
var r = rows[i];
|
|
|
|
if(r.ip == "*") {
|
2013-05-27 19:15:22 +00:00
|
|
|
chan.namebans[r.name] = r.banner;
|
2013-05-26 01:01:32 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
chan.ipbans[r.ip] = [r.name, r.banner];
|
|
|
|
}
|
2013-05-21 16:17:01 +00:00
|
|
|
}
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chan.logger.log("*** Loaded channel from database");
|
|
|
|
Logger.syslog.log("Loaded channel " + chan.name + " from database");
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function deleteChannel(name) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!name.match(/^[a-zA-Z0-9-_]+$/)) {
|
2013-03-06 22:02:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-05-13 19:41:29 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
var query = "DROP TABLE `chan_?_bans`, `chan_?_ranks`, `chan_?_library`"
|
|
|
|
.replace(/\?/g, name);
|
2013-05-13 19:41:29 +00:00
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to delete channel tables for " + name);
|
2013-05-13 19:41:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
query = createQuery(
|
|
|
|
"DELETE FROM `channels` WHERE name=?",
|
|
|
|
[name]
|
|
|
|
);
|
|
|
|
|
2013-05-13 19:41:29 +00:00
|
|
|
results = db.querySync(query);
|
2013-05-26 01:01:32 +00:00
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to delete row from channel table: " + name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-05-13 19:41:29 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
/* REGION Channel data */
|
|
|
|
|
|
|
|
function getChannelRank(chan, name) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-04-02 19:39:56 +00:00
|
|
|
if(!db) {
|
2013-05-26 01:01:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query;
|
|
|
|
if(typeof name == "object") {
|
|
|
|
var n = "(?";
|
|
|
|
for(var i = 1; i < name.length; i++) {
|
|
|
|
n += ",?";
|
|
|
|
}
|
|
|
|
n += ")"
|
|
|
|
name.unshift("chan_" + chan + "_ranks");
|
|
|
|
query = createQuery(
|
|
|
|
"SELECT * FROM `?` WHERE name IN " + n,
|
|
|
|
name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
query = createQuery(
|
|
|
|
"SELECT * FROM `?` WHERE name=?",
|
|
|
|
["chan_"+chan+"_ranks", name]
|
|
|
|
);
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
2013-04-02 19:39:56 +00:00
|
|
|
var results = db.querySync(query);
|
2013-04-04 20:41:41 +00:00
|
|
|
if(!results) {
|
2013-05-26 01:01:32 +00:00
|
|
|
Logger.errlog.log("! Failed to lookup chan_"+chan+"_ranks");
|
|
|
|
return 0;
|
2013-04-04 20:41:41 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
2013-04-02 19:39:56 +00:00
|
|
|
var rows = results.fetchAllSync();
|
2013-05-26 01:01:32 +00:00
|
|
|
if(typeof name == "object") {
|
|
|
|
var ranks = [];
|
|
|
|
for(var i = 0; i < rows.length; i++) {
|
|
|
|
ranks.push(rows[i].rank);
|
|
|
|
}
|
|
|
|
while(ranks.length < rows.length) {
|
|
|
|
ranks.push(0);
|
|
|
|
}
|
|
|
|
return ranks;
|
|
|
|
}
|
2013-04-02 19:39:56 +00:00
|
|
|
if(rows.length == 0) {
|
2013-05-26 01:01:32 +00:00
|
|
|
return 0;
|
2013-03-06 22:02:40 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 19:39:56 +00:00
|
|
|
return rows[0].rank;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function setChannelRank(chan, name, rank) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-04-02 19:39:56 +00:00
|
|
|
if(!db) {
|
2013-03-06 22:02:40 +00:00
|
|
|
return false;
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
["INSERT INTO `?` ",
|
|
|
|
"(`name`, `rank`) ",
|
|
|
|
"VALUES ",
|
|
|
|
"(?, ?) ",
|
|
|
|
"ON DUPLICATE KEY UPDATE ",
|
|
|
|
"`rank`=?"].join(""),
|
|
|
|
["chan_"+chan+"_ranks", name, rank, rank]
|
|
|
|
);
|
|
|
|
|
|
|
|
return db.querySync(query);
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-03-06 22:02:40 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function listChannelRanks(chan) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
2013-05-26 01:01:32 +00:00
|
|
|
return [];
|
2013-03-06 22:02:40 +00:00
|
|
|
}
|
2013-05-27 19:43:37 +00:00
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
2013-05-27 19:17:29 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
var query = createQuery(
|
|
|
|
"SELECT * FROM `?` WHERE 1",
|
|
|
|
["chan_"+chan+"_ranks"]
|
|
|
|
);
|
|
|
|
|
2013-04-27 16:48:36 +00:00
|
|
|
var results = db.querySync(query);
|
2013-05-26 01:01:32 +00:00
|
|
|
if(!results) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return results.fetchAllSync();
|
2013-04-27 16:48:36 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function addToLibrary(chan, media) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-04-27 16:48:36 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
["INSERT INTO `?` ",
|
|
|
|
"(`id`, `title`, `seconds`, `type`) ",
|
|
|
|
"VALUES ",
|
|
|
|
"(?, ?, ?, ?)"].join(""),
|
|
|
|
["chan_"+chan+"_library", media.id, media.title, media.seconds, media.type]
|
|
|
|
);
|
|
|
|
|
|
|
|
return db.querySync(query);
|
2013-04-02 19:39:56 +00:00
|
|
|
}
|
2013-04-03 03:56:44 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function removeFromLibrary(chan, id) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-04-03 03:56:44 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"DELETE FROM `?` WHERE id=?",
|
|
|
|
["chan_"+chan+"_library", id]
|
|
|
|
);
|
|
|
|
|
|
|
|
return db.querySync(query);
|
2013-04-03 03:56:44 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function channelBan(chan, ip, name, banby) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-04-03 03:56:44 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
["INSERT INTO `?` ",
|
|
|
|
"(`ip`, `name`, `banner`) ",
|
|
|
|
"VALUES ",
|
|
|
|
"(?, ?, ?)"].join(""),
|
|
|
|
["chan_"+chan+"_bans", ip, name, banby]
|
|
|
|
);
|
|
|
|
|
|
|
|
return db.querySync(query);
|
2013-04-03 03:56:44 +00:00
|
|
|
}
|
2013-04-22 18:28:40 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function channelUnbanIP(chan, ip) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-05-21 16:17:01 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"DELETE FROM `?` WHERE `ip`=?",
|
|
|
|
["chan_"+chan+"_bans", ip]
|
|
|
|
);
|
|
|
|
|
|
|
|
return db.querySync(query);
|
2013-05-21 16:17:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function channelUnbanName(chan, name) {
|
2013-05-27 19:43:37 +00:00
|
|
|
if(!chan.match(/^[a-zA-Z0-9-_]+$/)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
var db = getConnection();
|
2013-04-22 18:28:40 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
var query = createQuery(
|
|
|
|
"DELETE FROM `?` WHERE `ip`='*' AND `name`=?",
|
|
|
|
["chan_"+chan+"_bans", name]
|
|
|
|
);
|
2013-04-22 18:28:40 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
return db.querySync(query);
|
2013-04-22 18:28:40 +00:00
|
|
|
}
|
2013-05-13 00:41:02 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
/* REGION Users */
|
|
|
|
|
2013-05-30 01:04:22 +00:00
|
|
|
function getProfile(name) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"SELECT profile_image,profile_text FROM registrations WHERE uname=?",
|
|
|
|
[name]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to retrieve user profile");
|
|
|
|
throw "Database failure. Contact an administrator.";
|
|
|
|
}
|
|
|
|
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
if(rows.length == 0) {
|
|
|
|
throw "User not found";
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
profile_image: rows[0].profile_image,
|
|
|
|
profile_text: rows[0].profile_text
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
function setProfile(name, data) {
|
|
|
|
var db = getConnection();
|
2013-05-13 00:41:02 +00:00
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
var query = createQuery(
|
|
|
|
["UPDATE `registrations` SET ",
|
|
|
|
"`profile_image`=?,",
|
|
|
|
"`profile_text`=? ",
|
|
|
|
"WHERE uname=?"].join(""),
|
|
|
|
[data.image, data.text, name]
|
|
|
|
);
|
2013-05-13 00:41:02 +00:00
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
return db.querySync(query);
|
2013-05-13 00:41:02 +00:00
|
|
|
}
|
2013-05-26 01:01:32 +00:00
|
|
|
|
2013-05-29 15:49:09 +00:00
|
|
|
function setUserEmail(name, email) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"UPDATE `registrations` SET `email`=? WHERE `uname`=?",
|
|
|
|
[email, name]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to set user email");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:12:24 +00:00
|
|
|
function genSalt() {
|
|
|
|
var chars = "abcdefgihjklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
+ "0123456789!@#$%^&*_+=~";
|
|
|
|
var salt = [];
|
|
|
|
for(var i = 0; i < 32; i++) {
|
|
|
|
salt.push(chars[parseInt(Math.random()*chars.length)]);
|
|
|
|
}
|
|
|
|
return salt.join('');
|
|
|
|
}
|
|
|
|
|
2013-05-28 15:40:06 +00:00
|
|
|
function generatePasswordReset(ip, name, email) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"SELECT `email` FROM `registrations` WHERE `uname`=?",
|
|
|
|
[name]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to retrieve user email");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
if(rows.length == 0) {
|
|
|
|
throw "Provided username does not exist";
|
|
|
|
}
|
|
|
|
if(rows[0].email != email) {
|
|
|
|
throw "Provided email does not match user's email";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation complete, now time to reset it
|
2013-06-21 00:12:24 +00:00
|
|
|
var hash = hashlib.sha256(genSalt() + name);
|
2013-05-29 18:19:43 +00:00
|
|
|
var exp = Date.now() + 24*60*60*1000;
|
2013-05-28 15:40:06 +00:00
|
|
|
query = createQuery(
|
|
|
|
["INSERT INTO `password_reset` (",
|
2013-05-29 18:19:43 +00:00
|
|
|
"`ip`, `name`, `hash`, `email`, `expire`",
|
2013-05-28 15:40:06 +00:00
|
|
|
") VALUES (",
|
|
|
|
"?, ?, ?, ?, ?",
|
2013-05-30 18:07:29 +00:00
|
|
|
") ON DUPLICATE KEY UPDATE `hash`=?,`expire`=?"].join(""),
|
|
|
|
[ip, name, hash, email, exp, hash, exp]
|
2013-05-28 15:40:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to insert password reset");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-30 01:04:22 +00:00
|
|
|
return hash;
|
2013-05-28 15:40:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 18:19:43 +00:00
|
|
|
function recoverPassword(hash) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"SELECT * FROM password_reset WHERE hash=?",
|
|
|
|
[hash]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to retrieve from password_reset");
|
|
|
|
throw "Database error. Contact an administrator";
|
|
|
|
}
|
|
|
|
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
if(rows.length == 0) {
|
|
|
|
throw "Invalid password reset link";
|
|
|
|
}
|
|
|
|
|
|
|
|
db.querySync(createQuery(
|
|
|
|
"DELETE FROM password_reset WHERE hash=?",
|
|
|
|
[hash]
|
|
|
|
));
|
|
|
|
|
|
|
|
if(Date.now() > rows[0].expire) {
|
|
|
|
throw "Link expired. Password resets are valid for 24 hours";
|
|
|
|
}
|
|
|
|
|
|
|
|
var pw;
|
|
|
|
if(!(pw = resetPassword(rows[0].name))) {
|
|
|
|
throw "Operation failed. Contact an administrator.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return [rows[0].name, pw];
|
|
|
|
}
|
|
|
|
|
2013-05-28 15:40:06 +00:00
|
|
|
function resetPassword(name) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pw = "";
|
|
|
|
for(var i = 0; i < 10; i++) {
|
|
|
|
pw += "abcdefghijklmnopqrstuvwxyz"[parseInt(Math.random() * 25)];
|
|
|
|
}
|
|
|
|
var hash = bcrypt.hashSync(pw, 10);
|
|
|
|
var query = createQuery(
|
|
|
|
"UPDATE `registrations` SET `pw`=? WHERE `uname`=?",
|
|
|
|
[hash, name]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pw;
|
|
|
|
}
|
|
|
|
|
2013-06-01 15:59:04 +00:00
|
|
|
/* REGION User Playlists */
|
|
|
|
function getUserPlaylists(user) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
[];
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
2013-06-02 17:54:58 +00:00
|
|
|
"SELECT name,count,time FROM user_playlists WHERE user=?",
|
2013-06-01 15:59:04 +00:00
|
|
|
[user]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to query user playlists");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return results.fetchAllSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadUserPlaylist(user, name) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
[];
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"SELECT contents FROM user_playlists WHERE user=? AND name=?",
|
|
|
|
[user, name]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to query playlists");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = results.fetchAllSync()[0];
|
|
|
|
var pl;
|
|
|
|
try {
|
|
|
|
pl = JSON.parse(row.contents);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
Logger.errlog.log("! Failed to load playlist "+user+"."+name);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveUserPlaylist(pl, user, name) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip out unnecessary data
|
|
|
|
var pl2 = [];
|
2013-06-02 17:54:58 +00:00
|
|
|
var time = 0;
|
2013-06-01 15:59:04 +00:00
|
|
|
for(var i = 0; i < pl.length; i++) {
|
|
|
|
var e = {
|
|
|
|
id: pl[i].id,
|
|
|
|
type: pl[i].type
|
|
|
|
};
|
2013-06-02 17:54:58 +00:00
|
|
|
time += pl[i].seconds;
|
2013-06-01 15:59:04 +00:00
|
|
|
pl2.push(e);
|
|
|
|
}
|
2013-06-02 17:54:58 +00:00
|
|
|
var count = pl2.length;
|
2013-06-01 15:59:04 +00:00
|
|
|
var plstr = JSON.stringify(pl2);
|
|
|
|
|
|
|
|
var query = createQuery(
|
2013-06-02 17:54:58 +00:00
|
|
|
"INSERT INTO user_playlists VALUES (?, ?, ?, ?, ?)" +
|
|
|
|
"ON DUPLICATE KEY UPDATE contents=?,count=?,time=?",
|
|
|
|
[user, name, plstr, count, time, plstr, count, time]
|
2013-06-01 15:59:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to insert into playlists");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-01 20:56:23 +00:00
|
|
|
function deleteUserPlaylist(user, name) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"DELETE FROM user_playlists WHERE user=? AND name=?",
|
|
|
|
[user, name]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to delete from user_playlists");
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2013-06-18 01:02:48 +00:00
|
|
|
/* User Aliases */
|
|
|
|
|
|
|
|
function recordVisit(ip, name) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var time = Date.now();
|
|
|
|
db.querySync(createQuery(
|
|
|
|
"DELETE FROM aliases WHERE ip=? AND name=?",
|
|
|
|
[ip, name]
|
|
|
|
));
|
|
|
|
var query = createQuery(
|
|
|
|
"INSERT INTO aliases VALUES (NULL, ?, ?, ?)",
|
|
|
|
[ip, name, time]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to record visit");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep most recent 5 records per IP
|
|
|
|
results = db.querySync(createQuery(
|
2013-06-20 02:40:23 +00:00
|
|
|
["DELETE FROM aliases WHERE ip=? AND visit_id NOT IN (",
|
2013-06-18 01:02:48 +00:00
|
|
|
"SELECT visit_id FROM (",
|
|
|
|
"SELECT visit_id,time FROM aliases WHERE ip=? ORDER BY time DESC LIMIT 5",
|
|
|
|
") foo",
|
|
|
|
");"].join(""),
|
2013-06-20 02:40:23 +00:00
|
|
|
[ip, ip]
|
2013-06-18 01:02:48 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:57:29 +00:00
|
|
|
function getAliases(ip) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"SELECT name FROM aliases WHERE ip=?",
|
|
|
|
[ip]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to retrieve aliases");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var names = [];
|
|
|
|
results.fetchAllSync().forEach(function(row) {
|
|
|
|
names.push(row.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ipForName(name) {
|
|
|
|
var db = getConnection();
|
|
|
|
if(!db) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = createQuery(
|
|
|
|
"SELECT ip FROM aliases WHERE name=?",
|
|
|
|
[name]
|
|
|
|
);
|
|
|
|
|
|
|
|
var results = db.querySync(query);
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("! Failed to retrieve IP for name");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var ips = [];
|
|
|
|
results.fetchAllSync().forEach(function(row) {
|
|
|
|
ips.push(row.ip);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ips;
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:01:32 +00:00
|
|
|
exports.setup = setup;
|
|
|
|
exports.getConnection = getConnection;
|
|
|
|
exports.createQuery = createQuery;
|
|
|
|
exports.init = init;
|
|
|
|
exports.checkGlobalBan = checkGlobalBan;
|
|
|
|
exports.refreshGlobalBans = refreshGlobalBans;
|
|
|
|
exports.globalBanIP = globalBanIP;
|
|
|
|
exports.globalUnbanIP = globalUnbanIP;
|
|
|
|
exports.registerChannel = registerChannel;
|
|
|
|
exports.loadChannel = loadChannel;
|
|
|
|
exports.deleteChannel = deleteChannel;
|
|
|
|
exports.getChannelRank = getChannelRank;
|
|
|
|
exports.setChannelRank = setChannelRank;
|
|
|
|
exports.listChannelRanks = listChannelRanks;
|
|
|
|
exports.addToLibrary = addToLibrary;
|
|
|
|
exports.removeFromLibrary = removeFromLibrary;
|
|
|
|
exports.channelBan = channelBan;
|
|
|
|
exports.channelUnbanIP = channelUnbanIP;
|
|
|
|
exports.channelUnbanName = channelUnbanName;
|
|
|
|
exports.setProfile = setProfile;
|
2013-05-30 01:04:22 +00:00
|
|
|
exports.getProfile = getProfile;
|
2013-05-29 15:49:09 +00:00
|
|
|
exports.setUserEmail = setUserEmail;
|
2013-05-28 15:40:06 +00:00
|
|
|
exports.generatePasswordReset = generatePasswordReset;
|
2013-05-29 18:19:43 +00:00
|
|
|
exports.recoverPassword = recoverPassword;
|
2013-05-28 15:40:06 +00:00
|
|
|
exports.resetPassword = resetPassword;
|
2013-06-01 15:59:04 +00:00
|
|
|
exports.getUserPlaylists = getUserPlaylists;
|
|
|
|
exports.loadUserPlaylist = loadUserPlaylist;
|
|
|
|
exports.saveUserPlaylist = saveUserPlaylist;
|
2013-06-01 20:56:23 +00:00
|
|
|
exports.deleteUserPlaylist = deleteUserPlaylist;
|
2013-06-18 01:02:48 +00:00
|
|
|
exports.recordVisit = recordVisit;
|
2013-06-18 03:57:29 +00:00
|
|
|
exports.getAliases = getAliases;
|
|
|
|
exports.ipForName = ipForName;
|