CyTube/database.js

445 lines
14 KiB
JavaScript
Raw Normal View History

2013-03-24 02:28:20 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
2013-03-24 02:28:20 +00:00
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:
2013-03-24 02:28:20 +00:00
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2013-03-24 02:28:20 +00:00
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.
*/
var mysql = require("mysql-libmysqlclient");
var Config = require("./config.js");
2013-03-27 19:28:51 +00:00
var Logger = require("./logger.js");
2013-04-02 19:39:56 +00:00
var Rank = require("./rank.js");
2013-04-03 17:47:41 +00:00
var Media = require("./media.js").Media;
//var Server = require("./server.js");
2013-03-06 22:02:40 +00:00
var initialized = false;
2013-04-02 19:39:56 +00:00
exports.getConnection = function() {
2013-03-06 22:02:40 +00:00
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-04-02 19:39:56 +00:00
Logger.errlog.log("database.getConnection: DB connection failed");
return false;
}
2013-04-02 19:39:56 +00:00
return db;
}
function sqlEscape(data) {
if(data == null || data == undefined)
return "NULL";
else if(typeof data == "number")
return data;
else if(typeof data == "object")
return "(object)";
else if(typeof data == "string") {
return data.replace("'", "\\'");
}
}
2013-05-04 18:01:04 +00:00
exports.sqlEscape = sqlEscape;
2013-04-02 19:39:56 +00:00
exports.init = function() {
if(initialized)
return;
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.init: DB conection failed");
return;
}
2013-03-06 22:02:40 +00:00
var query = "CREATE TABLE IF NOT EXISTS `channels` \
2013-03-31 23:38:20 +00:00
(`id` INT NOT NULL AUTO_INCREMENT, \
2013-03-06 22:02:40 +00:00
`name` VARCHAR(255) NOT NULL, \
PRIMARY KEY (`id`)) \
ENGINE = MyISAM;";
var results = db.querySync(query);
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("database.init: channel table init failed!");
2013-03-06 22:02:40 +00:00
return false;
}
var query = "CREATE TABLE IF NOT EXISTS `registrations` \
2013-03-31 23:35:35 +00:00
(`id` INT NOT NULL AUTO_INCREMENT, \
2013-03-06 22:02:40 +00:00
`uname` VARCHAR(20) NOT NULL, \
`pw` VARCHAR(64) NOT NULL, \
`global_rank` INT NOT NULL, \
2013-05-03 21:27:12 +00:00
`session_hash` VARCHAR(64) NOT NULL, \
`expire` BIGINT NOT NULL, \
`profile_image` VARCHAR( 255 ) NOT NULL , \
`profile_text` TEXT NOT NULL, \
2013-03-06 22:02:40 +00:00
PRIMARY KEY (`id`)) \
ENGINE = MyISAM;";
var results = db.querySync(query);
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("database.init: registration table init failed!");
2013-03-06 22:02:40 +00:00
return false;
}
2013-04-28 06:30:18 +00:00
var query = "CREATE TABLE IF NOT EXISTS `global_bans` \
(`ip` VARCHAR( 15 ) NOT NULL ,\
`note` VARCHAR( 255 ) NOT NULL ,\
PRIMARY KEY (`ip`)\
) ENGINE = MYISAM";
var results = db.querySync(query);
if(!results) {
Logger.errlog.log("database.init: global ban table init failed!");
return false;
}
2013-03-06 22:02:40 +00:00
initialized = true;
2013-03-24 03:01:37 +00:00
db.closeSync();
2013-03-06 22:02:40 +00:00
return true;
}
2013-04-28 06:30:18 +00:00
var gbanTime = 0;
var gbans = {};
exports.checkGlobalBan = function(ip) {
// Check database at most once per 5 minutes
if(new Date().getTime() > gbanTime + 300000) {
exports.refreshGlobalBans();
2013-04-28 06:30:18 +00:00
}
2013-04-29 22:06:39 +00:00
var parts = ip.split(".");
var slash16 = parts[0] + "." + parts[1];
var slash24 = slash16 + "." + parts[2];
return (ip in gbans || slash16 in gbans || slash24 in gbans);
2013-04-28 06:30:18 +00:00
}
exports.refreshGlobalBans = function() {
var db = exports.getConnection();
if(!db) {
return false;
}
// Check if channel exists
var query = "SELECT * FROM global_bans WHERE 1";
var results = db.querySync(query);
if(!results) {
Logger.errlog.log("loadGlobalBans: query failed");
return false;
}
var rows = results.fetchAllSync();
gbans = {};
for(var i = 0; i < rows.length; i++) {
gbans[rows[i].ip] = rows[i].note;
}
db.closeSync();
gbanTime = new Date().getTime();
return gbans;
}
exports.addGlobalBan = function(ip, reason) {
var db = exports.getConnection();
if(!db) {
return false;
}
var query = "INSERT INTO global_bans VALUES ('{1}', '{2}')"
.replace("{1}", sqlEscape(ip))
.replace("{2}", sqlEscape(reason));
var result = db.querySync(query);
db.closeSync();
return result;
}
exports.liftGlobalBan = function(ip) {
var db = exports.getConnection();
if(!db) {
return false;
}
var query = "DELETE FROM global_bans WHERE ip='{}'"
.replace("{}", sqlEscape(ip))
var result = db.querySync(query);
db.closeSync();
return result;
}
2013-04-02 19:39:56 +00:00
exports.loadChannel = function(chan) {
var db = exports.getConnection();
if(!db) {
chan.logger.log("!!! Could not connect to database");
return;
}
// Check if channel exists
var query = "SELECT * FROM channels WHERE name='{}'"
.replace("{}", chan.name);
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
if(!results) {
2013-04-02 19:39:56 +00:00
Logger.errlog.log("Channel.loadMysql: Channel query failed");
return;
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
var rows = results.fetchAllSync();
if(rows.length == 0) {
Logger.syslog.log("Channel " + chan.name + " is unregistered.");
return;
}
2013-04-03 17:47:41 +00:00
else if(rows[0].name != chan.name) {
chan.name = rows[0].name;
2013-04-03 17:47:41 +00:00
}
2013-04-02 19:39:56 +00:00
chan.registered = true;
2013-03-06 22:02:40 +00:00
2013-04-02 19:39:56 +00:00
// Load library
2013-05-11 19:21:14 +00:00
var query = "SELECT * FROM `chan_{}_library`"
.replace("{}", sqlEscape(chan.name));
2013-04-02 19:39:56 +00:00
var results = db.querySync(query);
if(!results) {
Logger.errlog.log("Channel.loadMysql: failed to load library for " + chan.name);
return;
}
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
chan.library[rows[i].id] = new Media(rows[i].id, rows[i].title, rows[i].seconds, rows[i].type);
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
// Load bans
2013-05-11 19:21:14 +00:00
var query = "SELECT * FROM `chan_{}_bans`"
.replace("{}", sqlEscape(chan.name));
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
if(!results) {
2013-04-02 19:39:56 +00:00
Logger.errlog.log("Channel.loadMysql: failed to load banlist for " + chan.name);
return;
}
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
2013-05-21 16:17:01 +00:00
// Name ban
if(rows[i].ip == "*") {
chan.namebans[rows[i].name] = rows[i].banner;
}
else {
chan.ipbans[rows[i].ip] = [rows[i].name, rows[i].banner];
}
2013-04-02 19:39:56 +00:00
}
chan.logger.log("*** Loaded channel from database");
Logger.syslog.log("Loaded channel " + chan.name + " from database");
db.closeSync();
}
exports.registerChannel = function(chan) {
var db = exports.getConnection();
if(!db) {
chan.logger.log("!!! Could not connect to database");
2013-03-06 22:02:40 +00:00
return false;
}
2013-04-02 19:39:56 +00:00
// Create library table
var query= "CREATE TABLE `chan_{}_library` \
(`id` VARCHAR(255) NOT NULL, \
`title` VARCHAR(255) NOT NULL, \
`seconds` INT NOT NULL, \
`playtime` VARCHAR(8) NOT NULL, \
`type` VARCHAR(2) NOT NULL, \
PRIMARY KEY (`id`)) \
ENGINE = MyISAM;"
.replace("{}", chan.name);
var results = db.querySync(query);
2013-03-06 22:02:40 +00:00
2013-04-02 19:39:56 +00:00
// Create rank table
var query = "CREATE TABLE `chan_{}_ranks` (\
`name` VARCHAR( 32 ) NOT NULL ,\
`rank` INT NOT NULL ,\
UNIQUE (\
`name`\
)\
) ENGINE = MYISAM"
.replace("{}", chan.name);
results = db.querySync(query) || results;
// Create ban table
var query = "CREATE TABLE `chan_{}_bans` (\
`ip` VARCHAR( 15 ) NOT NULL ,\
`name` VARCHAR( 32 ) NOT NULL ,\
`banner` VARCHAR( 32 ) NOT NULL ,\
PRIMARY KEY (\
`ip`\
)\
) ENGINE = MYISAM"
.replace("{}", chan.name);
results = db.querySync(query) || results;
// Insert into global channel table
var query = "INSERT INTO channels (`id`, `name`) VALUES (NULL, '{}')"
.replace("{}", sqlEscape(chan.name));
2013-04-02 19:39:56 +00:00
results = db.querySync(query) || results;
db.closeSync();
return results;
}
2013-05-13 19:41:29 +00:00
exports.unregisterChannel = function(channame) {
var db = exports.getConnection();
if(!db) {
return false;
}
var query = "DROP TABLE `chan_{}_bans`, `chan_{}_ranks`, `chan_{}_library`"
.replace(/\{\}/g, sqlEscape(channame));
var results = db.querySync(query);
if(!results) {
return false;
}
query = "DELETE FROM channels WHERE name='{}'"
.replace("{}", sqlEscape(channame));
results = db.querySync(query);
db.closeSync();
return results;
}
2013-04-02 19:39:56 +00:00
exports.lookupChannelRank = function(channame, username) {
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.lookupChannelRank: DB connection failed");
return Rank.Guest;
}
2013-05-11 19:21:14 +00:00
var query = "SELECT * FROM `chan_{1}_ranks` WHERE name='{2}'"
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(username));
2013-04-02 19:39:56 +00:00
var results = db.querySync(query);
2013-04-04 20:41:41 +00:00
if(!results) {
2013-04-02 19:39:56 +00:00
return Rank.Guest;
2013-04-04 20:41:41 +00:00
}
2013-04-02 19:39:56 +00:00
var rows = results.fetchAllSync();
if(rows.length == 0) {
return Rank.Guest;
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
db.closeSync();
return rows[0].rank;
}
exports.saveChannelRank = function(channame, user) {
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.saveChannelRank: DB connection failed");
2013-03-06 22:02:40 +00:00
return false;
2013-04-02 19:39:56 +00:00
}
2013-05-11 19:21:14 +00:00
var query = "UPDATE `chan_{1}_ranks` SET rank='{2}' WHERE name='{3}'"
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(user.rank))
.replace("{3}", sqlEscape(user.name));
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
2013-04-02 19:39:56 +00:00
// Gonna have to insert a new one, bugger
if(!results.fetchAllSync) {
2013-05-11 19:21:14 +00:00
var query = "INSERT INTO `chan_{1}_ranks` (`name`, `rank`) VALUES ('{2}', '{3}')"
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(user.name))
.replace("{3}", sqlEscape(user.rank));
2013-04-02 19:39:56 +00:00
results = db.querySync(query);
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
db.closeSync();
return results;
}
2013-03-06 22:02:40 +00:00
2013-04-02 19:39:56 +00:00
exports.cacheMedia = function(channame, media) {
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.cacheMedia: DB connection failed");
return false;
2013-03-06 22:02:40 +00:00
}
2013-05-11 19:21:14 +00:00
var query = "INSERT INTO `chan_{1}_library` VALUES ('{2}', '{3}', {4}, '{5}', '{6}')"
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(media.id))
.replace("{3}", sqlEscape(media.title))
.replace("{4}", sqlEscape(media.seconds))
.replace("{5}", sqlEscape(media.duration))
.replace("{6}", sqlEscape(media.type));
var results = db.querySync(query);
db.closeSync();
return results;
}
exports.uncacheMedia = function(channame, id) {
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.uncacheMedia: DB connection failed");
return false;
}
2013-05-11 19:21:14 +00:00
var query = "DELETE FROM `chan_{1}_library` WHERE id='{2}'"
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(id))
2013-04-02 19:39:56 +00:00
var results = db.querySync(query);
db.closeSync();
return results;
}
2013-04-03 03:56:44 +00:00
2013-04-03 17:47:41 +00:00
exports.addChannelBan = function(channame, actor, receiver) {
2013-04-03 03:56:44 +00:00
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("exports.addChannelBan: DB connection failed");
return false;
}
2013-05-11 19:21:14 +00:00
var query = "INSERT INTO `chan_{1}_bans` (`ip`, `name`, `banner`) VALUES ('{2}', '{3}', '{4}')"
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(receiver.ip))
.replace("{3}", sqlEscape(receiver.name))
.replace("{4}", sqlEscape(actor.name));
2013-04-03 03:56:44 +00:00
results = db.querySync(query);
db.closeSync();
return results;
}
2013-04-03 17:47:41 +00:00
exports.removeChannelBan = function(channame, ip) {
2013-04-03 03:56:44 +00:00
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("exports.removeChannelBan: DB connection failed");
return false;
}
2013-05-11 19:21:14 +00:00
var query = "DELETE FROM `chan_{1}_bans` WHERE `ip` = '{2}'"
2013-04-28 04:10:48 +00:00
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(ip));
2013-04-03 03:56:44 +00:00
results = db.querySync(query);
db.closeSync();
return results;
}
2013-05-21 16:17:01 +00:00
exports.removeNameBan = function(channame, name) {
var db = exports.getConnection();
if(!db) {
return false;
}
var query = "DELETE FROM `chan_{1}_bans` WHERE `ip` = '*' AND `name`='{2}'"
.replace("{1}", sqlEscape(channame))
.replace("{2}", sqlEscape(name));
results = db.querySync(query);
db.closeSync();
return results;
}
exports.getChannelRanks = function(channame) {
var db = exports.getConnection();
if(!db) {
return false;
}
2013-05-11 19:21:14 +00:00
var query = "SELECT * FROM `chan_{}_ranks` WHERE 1"
.replace("{}", sqlEscape(channame));
var results = db.querySync(query);
if(results) {
var rows = results.fetchAllSync();
db.closeSync();
return rows;
}
else {
return [];
}
}
exports.setProfile = function(name, data) {
var db = exports.getConnection();
if(!db) {
return false;
}
var query = "UPDATE registrations SET profile_image='{1}',profile_text='{2}' WHERE uname='{3}'"
.replace("{1}", sqlEscape(data.image))
.replace("{2}", sqlEscape(data.text))
.replace("{3}", sqlEscape(name));
var results = db.querySync(query);
db.closeSync();
return results;
}