/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


"use strict";

exports.userExists = function(dbConnection, username, callback, table)
{
	if (!table) table = 'uniusers';
	dbConnection.query(
		// Seems unsafe? It is.
		// But escaper doesn't know that table name and column value are different things.
		'SELECT count(*) AS result FROM `'+table+'` WHERE user = ?',
		[username],
		function (error, result){
			if (!!error)
			{
				callback(error, undefined);
			}
			else
			{
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.idExists = function(dbConnection, id, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `id`= ?',
		[id],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.userExists = function(dbConnection, user, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `user` = ?',
		[user],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.mailExists = function(dbConnection, mail, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `mail` = ?',
		[mail],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.sessionExists = function(dbConnection, sess, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `sessid` = ?',
		[sess],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.sessionActive = function(dbConnection, sess, callback) {
	dbConnection.query(
		'SELECT `sessexpire` > NOW() FROM `uniusers` WHERE `sessid` = ?',
		[sess],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};
@@ -1479,320 +1479,8 @@
 %7D;%0A%0A
-exports.userExists = function(dbConnection, user, callback) %7B%0A%09dbConnection.query(%0A%09%09'SELECT count(*) FROM %60uniusers%60 WHERE %60user%60 = ?',%0A%09%09%5Buser%5D,%0A%09%09function (error, result) %7B%0A%09%09%09if (!!error) %7B%0A%09%09%09%09callback(error, undefined);%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09callback(undefined, (result.rows%5B0%5D.result %3E 0));%0A%09%09%09%7D%0A%09%09%7D%0A%09);%0A%7D;%0A%0A
 expo
@@ -2411,28 +2411,29 @@
 esult %3E 0));%0A%09%09%09%7D%0A%09%09%7D%0A%09);%0A%7D;
+%0A

Fix build broken by b6c8c27f4449664fb1497fea84e07a0e92b08f94.