Ejemplo n.º 1
0
void SecurityManager::loadSqlMembers() {

    QSqlQuery query;

    query.setForwardOnly(true);

    query.exec("select * from trainers limit 1");

    int count = query.record().count();

    if (count == 8) {
        /* Outdated database, we are going to add ban time */
        QSqlDatabase::database().transaction();

        query.exec("alter table trainers add column ban_expire_time int");
        query.exec("update trainers set ban_expire_time=0");
        //query.exec("create index ban_expire_time_index on trainers (ban_expire_time)");

        QSqlDatabase::database().commit();
    } else if (!query.next()) {
        if (SQLCreator::databaseType == SQLCreator::PostGreSQL) {
            /* The only way to have an auto increment field with PostGreSQL is to my knowledge using the serial type */
            query.exec("create table trainers (id serial, "
                       "name varchar(20), laston char(19), auth int, banned boolean,"
                       "salt varchar(7), hash varchar(32), ip varchar(39), ban_expire_time int, primary key(id), unique(name))");
        } else if (SQLCreator::databaseType == SQLCreator::MySQL) {
            query.exec("CREATE TABLE IF NOT EXISTS trainers (id int(11) NOT NULL auto_increment, "
                       "name varchar(20), laston char(19), auth int(11), banned bool, "
                       "salt varchar(7), hash varchar(32), ip varchar(39), "
                       "ban_expire_time int(11), PRIMARY KEY (id));");
        } else if (SQLCreator::databaseType == SQLCreator::SQLite){
            /* The only way to have an auto increment field with SQLite is to my knowledge having a 'integer primary key' field -- that exact quote */
            query.exec("create table trainers (id integer primary key autoincrement, name varchar(20) unique, "
                       "laston char(19), auth int, banned boolean, salt varchar(7), hash varchar(32), "
                       "ip varchar(39), ban_expire_time int);");
        } else {
            throw QString("Using a not supported database");
        }

        query.exec("create index tname_index on trainers (name)");
        query.exec("create index tip_index on trainers (ip)");

        QFile memberFile("serverdb/members.txt");
        if (memberFile.exists()) {
            Server::print("importing text db");

            if (!memberFile.open(QFile::ReadWrite)) {
                throw QObject::tr("Error: cannot open the file that contains the members ");
            }

            clock_t t = clock();

            query.prepare("insert into trainers(name, laston, auth,  banned, salt, hash, ip, ban_expire_time) values (:name, :laston, :auth,"
                          ":banned, :salt, :hash, :ip, :banexpire)");

            QSqlDatabase::database().transaction();
            int counter = 0;
            while (!memberFile.atEnd()) {
                if (query.lastError().isValid() && counter > 0) {
                    Server::print(QString("Error in last query (number %1): %2").arg(counter).arg(query.lastError().text()));
                    break;
                }

                ++counter;

                if (counter % 1000 == 0) {
                    Server::print(QString("Loaded %1 members so far...").arg(counter));
                }

                QByteArray arr = memberFile.readLine();
                QString s = QString::fromUtf8(arr.constData(), std::max(0,arr.length()-1)); //-1 to remove the \n

                QStringList ls = s.split('%');

                if (ls.size() >= 6 && isValid(ls[0])) {
                    query.bindValue(":name", ls[0].toLower());
                    query.bindValue(":laston",ls[1]);
                    query.bindValue(":auth", ls[2][0].toLatin1()-'0');
                    query.bindValue(":banned", ls[2][1] == '1');
                    /* Weirdly, i seem to have problems when updating something that has a salt containing \, probably postgresql driver,
                       so i remove them. */
                    if (!ls[3].contains('\\')) {
                        query.bindValue(":salt", ls[3].trimmed().toLatin1());
                        query.bindValue(":hash", ls[4].trimmed().toLatin1());
                    } else {
                        query.bindValue(":salt", "");
                        query.bindValue(":hash", "");
                    }
                    query.bindValue(":ip", ls[5].trimmed());
                    if (ls.size() >= 7) {
                        query.bindValue(":banexpire", ls[6]);
                    } else {
                        query.bindValue(":banexpire", 0);
                    }
                    query.exec();
                }
            }

            QSqlDatabase::database().commit();

            t = clock() - t;

            Server::print(QString::number(float(t)/CLOCKS_PER_SEC) + " secs");
            Server::print(query.lastError().text());
        }
    }

    /* Expire old temp bans */
    if (SQLCreator::databaseType == SQLCreator::MySQL) {
        query.prepare("update trainers set banned=0 where banned=1 and ban_expire_time < :now and ban_expire_time != 0");
    } else {
        query.prepare("update trainers set banned='false' where banned='true' and ban_expire_time < :now and ban_expire_time != 0");
    }
    query.bindValue(":now", QDateTime::currentDateTimeUtc().toTime_t());
    query.exec();
    QSqlDatabase::database().commit();

    /* Loading the ban list */

    if (SQLCreator::databaseType == SQLCreator::MySQL) {
        query.exec("select name, ip, ban_expire_time from trainers where banned=1");
    }
    else {
        query.exec("select name, ip, ban_expire_time from trainers where banned='true'");
    }

    while (query.next()) {
        bannedIPs.insert(query.value(1).toString(), query.value(2).toInt());
        bannedMembers.insert(query.value(0).toString().toLower(), std::make_pair(query.value(1).toString(), query.value(2).toInt()));
    }
}
Ejemplo n.º 2
0
/**
 * Main method for the Healthy Club system. Provides a menu to the user to decide
 * what they wish to do and allows them to enter their information. Makes calls
 * to different classes depending on what the user chooses.
 *
 * @author Isaac Whitfield
 * @version 28/02/2013
 */
int main() {
	WeightWatchers * pMain = new WeightWatchers();
	// Set the variables needed by the menu
	int selection; int i = 0; string firstName, lastName, gender;
	// Create a loop which is ended by the user
	while(pMain->getExitStatus() != "exit"){
		// Clear the screen and display opening information
		pMain->clearScreen();
		cout << "Welcome to the Healthy Club Command System.\n" << endl;
		cout << "Please type in your first name: "; cin >> firstName;
		cout << "Please type in your last name: "; cin >> lastName;
		cout << "Please type in your gender: "; cin >> gender;

		// Clear the screen
		pMain->clearScreen();
		// Open the members file
		ifstream memberFile("..\\resources\\members\\" + firstName + " " + lastName + ".txt");
		ofstream members;
		// If they already have a file, display this
		if(memberFile){
			cout << "Welcome back, " << firstName << "." << endl;
		} else {
			// If they haven't, create their new file
			ofstream createFile("..\\resources\\members\\" + firstName + " " + lastName + ".txt");
			// Also add them to the members list
			members.open("..\\resources\\members.txt", ios_base::app);
			members << firstName << " " << lastName << endl;
			// Close the stream and welcome them to the club
			members.close();
			cout << "Welcome to the club, " << firstName << "." << endl;
		}
		// Display the next screen
		cout << "Please type the number of the feature you wish to use:\n" << endl;
		cout << "1. Start the Client Server simulation" << endl;
		cout << "2. Calculate your body statistics\n" << endl;
		cout << "Your selection: "; cin >> selection;

		// Validates that the user has made a valid selection
		validateSelection:
		if(selection == 1){
			// Clear the screen and initialize the simulation
			pMain->clearScreen();
			pMain->runSimulation();
		} else if (selection == 2){
			// Clear the screen and continue to generate statistics
			pMain->clearScreen();
			pMain->calculateStatistics(firstName + " " + lastName, gender);
		} else if (selection == 0000){
			//--- Test function ---//
			//--- Test function ---//
		} else {
			// Show the error and ask for a valid selection
			if (i == 0) cout << endl, i++;
			cout << "Please make a valid selection: ";
			cin.clear(); while (cin.get() != '\n');
			cin >> selection;
			goto validateSelection;
		}
	}
	delete pMain;
}