using System; using System.Text; using System.Collections.Generic; using Woodpecker.Data; using Woodpecker.Strings; namespace Woodpecker.Core { /// /// Provides alot of functions which do things. doh. /// public static class Access { #region Username checks, passwords etc /// /// Returns a boolean that indicates if a user with a certain username exists in the 'users' table of the database. /// /// The username to check. public static bool userExists(string Username) { return Database.checkExists("users", "username", Username); } /// /// Returns the ID of the error at a name check of a username/new pet. If no errors, then 0 is returned. /// /// Specifies if this namecheck is for a pet purchase. /// The name to check. public static int getNameCheckError(bool Pet, string Name) { if (Name.Length > 15) return 2; else { if (Pet == false && userExists(Name)) return 4; // Username already taken else return 0; // OK } } /// /// Returns a boolean that indicates if a string is valid as a password for a user. The string shouldn't be the same as the username, it should be 6-9 characters long and it should contain at least one number. /// /// The username for this password. /// The password to check. public static bool passwordOK(string Username, string Password) { return (Username != Password && (Password.Length > 5 && Password.Length < 10) && stringManager.hasNumbers(Password)); } /// /// Returns a boolean that indicates if an email address is valid. /// /// The email address to check. public static bool emailOK(string Email) { try { // mike.fails@script-o-matic.net :] string[] Parts = Email.Split('@'); return (Parts.Length == 2 && Parts[1].Contains(".")); } catch { return false; } } #endregion #region FUSE rights #region Declares /// /// Contains the accessRank objects. /// private static Dictionary _accessRanks = new Dictionary(); #endregion #region Methods /// /// Initializes the access ranks for users. /// public static void initAccessRanks() { Logging.logInfo("Initializing access ranks..."); for (byte i = 0; i <= 6; i++) _accessRanks.Add(i, new accessRank(i)); Logging.logInfo("Access ranks 1-6 initialized."); Logging.logInfo("Initializing subscription ranks..."); string[] Subscriptions = { "club_habbo" }; foreach (string Subscription in Subscriptions) _accessRanks.Add(Subscription, new accessRank(Subscription)); Logging.logInfo("Subscription rank 'club_habbo' initialized."); } /// /// Returns a boolean that indicates if a certain access rank contains a certain FUSE right. /// /// The ID of the main rank. /// The subscriptions that the user has. /// The FUSE right to check. public static bool accessRankHasFUSERight(byte rankID, string[] Subscriptions, string Right) { try { foreach (string acRight in _accessRanks[rankID].Rights) if (acRight == Right) return true; foreach (string Subscription in Subscriptions) foreach (string acRight in _accessRanks[Subscription].Rights) if (acRight == Right) return true; return false; } catch { return false; } } /// /// Returns all the FUSE rights for a certain user rank + subscription as a string. The rights are separated by \x02. /// /// The user rank of the user. /// The subscriptions that the user has. public static string getFUSERights(byte rankID, string[] Subscriptions) { StringBuilder Rights = new StringBuilder(); try { foreach (string Right in _accessRanks[rankID].Rights) Rights.Append(Right + Convert.ToChar(2)); foreach (string Subscription in Subscriptions) foreach (string Right in _accessRanks[Subscription].Rights) Rights.Append(Right + Convert.ToChar(2)); return Rights.ToString(); } catch { return Rights.ToString(); } } #endregion #region Private members /// /// Represents an access rank for users, for a user rank or a subscription. Contains the FUSE rights that this rank provides. /// private struct accessRank { /// /// The FUSE rights of this access rank. /// internal string[] Rights; /// /// Initializes a user rank. /// /// The ID of the user rank to initialize. internal accessRank(byte rankID) { this.Rights = Database.runReadColumnStrings("SELECT fuseright FROM woodpecker_fuse WHERE minrank <= " + rankID + "", 0); } /// /// Initializes a subscription rank. /// /// The subscription rank to initialize, eg, 'club_habbo'. internal accessRank(string Subscription) { this.Rights = Database.runReadColumnStrings("SELECT fuseright FROM woodpecker_subscriptions_fuse WHERE subscription = '" + Subscription + "'", 0); } } #endregion #endregion #region Financial /// /// Writes a transaction (eg, furniture purchase) by a user into the database, together with the current date + time. /// /// The database ID of the user that did the transaction. /// The type of the transaction, eg, 'stuff_store'. /// The activity of the transaction, eg, '-5' or '10'. public static void logTransaction(int userID, string Type, int Activity) { Database.runQuery("INSERT INTO users_transactions(userid,date,time,type,activity) VALUES ('" + userID + "','" + DateTime.Today.ToShortDateString() + "','" + DateTime.Now.ToShortTimeString() + "','" + Type + "','" + Activity + "')"); } #endregion #region Rooms /// /// Converts an access level of a guestroom to it's virtualRoom.accessFlag equivalent. /// /// The access level as an integer. //public static virtualRoom.accessFlags getRoomAccessFlag(int Level) //{ // return (virtualRoom.accessFlags)Level; //} /// /// Converts an access level of a guestroom to it's virtualRoom.accessFlag equivalent. /// /// The access level as a string. //public static virtualRoom.accessFlags getRoomAccessFlag(string Level) //{ // try { return (virtualRoom.accessFlags)int.Parse(Level); } // catch { return virtualRoom.accessFlags.open; } //} //public static int convertRoomAccessFlag(string accessFlag) //{ // try { return (int)Enum.Parse(typeof(virtualRoom.accessFlags), accessFlag, false); } // catch { return 0; } // } #endregion } }