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 = {};
|
|
|
|
|
|
|
|
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-05-26 01:01:32 +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-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();
|
|
|
|
if(typeof arg == "string") {
|
|
|
|
arg = arg.replace(/([\'])/g, "\\$1");
|
|
|
|
if(idx == 0 || template[idx-1] != "`") {
|
|
|
|
arg = "'" + arg + "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(arg === null || arg === undefined) {
|
|
|
|
arg = "NULL";
|
|
|
|
}
|
|
|
|
var first = template.substring(0, idx);
|
|
|
|
template = first + template.substring(idx).replace("?", arg);
|
|
|
|
last = idx + (arg+"").length;
|
2013-04-27 16:48:36 +00:00
|
|
|
}
|
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,",
|
|
|
|
"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-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-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
|
|
|
|
var hash = hashlib.sha256(Date.now() + 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-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;
|