[2] | 1 | /* |
---|
[44] | 2 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
[2] | 3 | * |
---|
[44] | 4 | * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/> |
---|
| 5 | * |
---|
[2] | 6 | * This program is free software; you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[44] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
[2] | 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program; if not, write to the Free Software |
---|
[44] | 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
[2] | 19 | */ |
---|
| 20 | |
---|
| 21 | #include "AccountMgr.h" |
---|
| 22 | #include "Database/DatabaseEnv.h" |
---|
| 23 | #include "ObjectMgr.h" |
---|
| 24 | #include "Player.h" |
---|
| 25 | #include "Policies/SingletonImp.h" |
---|
| 26 | #include "Util.h" |
---|
| 27 | |
---|
| 28 | #ifdef DO_POSTGRESQL |
---|
| 29 | extern DatabasePostgre loginDatabase; |
---|
| 30 | #else |
---|
| 31 | extern DatabaseMysql loginDatabase; |
---|
| 32 | #endif |
---|
| 33 | |
---|
| 34 | INSTANTIATE_SINGLETON_1(AccountMgr); |
---|
| 35 | |
---|
| 36 | AccountMgr::AccountMgr() |
---|
| 37 | {} |
---|
| 38 | |
---|
| 39 | AccountMgr::~AccountMgr() |
---|
| 40 | {} |
---|
| 41 | |
---|
| 42 | AccountOpResult AccountMgr::CreateAccount(std::string username, std::string password) |
---|
| 43 | { |
---|
| 44 | if(utf8length(username) > MAX_ACCOUNT_STR) |
---|
| 45 | return AOR_NAME_TOO_LONG; // username's too long |
---|
| 46 | |
---|
| 47 | normilizeString(username); |
---|
| 48 | normilizeString(password); |
---|
| 49 | |
---|
| 50 | loginDatabase.escape_string(username); |
---|
| 51 | loginDatabase.escape_string(password); |
---|
| 52 | |
---|
| 53 | QueryResult *result = loginDatabase.PQuery("SELECT 1 FROM account WHERE username = '%s'", username.c_str()); |
---|
| 54 | if(result) |
---|
| 55 | { |
---|
| 56 | delete result; |
---|
| 57 | return AOR_NAME_ALREDY_EXIST; // username does already exist |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | if(!loginDatabase.PExecute("INSERT INTO account(username,sha_pass_hash,joindate) VALUES('%s',SHA1(CONCAT('%s',':','%s')),NOW())", username.c_str(), username.c_str(), password.c_str())) |
---|
| 61 | return AOR_DB_INTERNAL_ERROR; // unexpected error |
---|
[22] | 62 | loginDatabase.Execute("INSERT INTO realmcharacters (realmid, acctid, numchars) SELECT realmlist.id, account.id, 0 FROM realmlist,account LEFT JOIN realmcharacters ON acctid=account.id WHERE acctid IS NULL"); |
---|
[2] | 63 | |
---|
| 64 | return AOR_OK; // everything's fine |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | AccountOpResult AccountMgr::DeleteAccount(uint32 accid) |
---|
| 68 | { |
---|
| 69 | QueryResult *result = loginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accid); |
---|
| 70 | if(!result) |
---|
| 71 | return AOR_NAME_NOT_EXIST; // account doesn't exist |
---|
| 72 | delete result; |
---|
| 73 | |
---|
| 74 | result = CharacterDatabase.PQuery("SELECT guid FROM characters WHERE account='%d'",accid); |
---|
| 75 | if (result) |
---|
| 76 | { |
---|
| 77 | do |
---|
| 78 | { |
---|
| 79 | Field *fields = result->Fetch(); |
---|
| 80 | uint32 guidlo = fields[0].GetUInt32(); |
---|
| 81 | uint64 guid = MAKE_NEW_GUID(guidlo, 0, HIGHGUID_PLAYER); |
---|
| 82 | |
---|
| 83 | // kick if player currently |
---|
| 84 | if(Player* p = objmgr.GetPlayer(guid)) |
---|
| 85 | { |
---|
| 86 | WorldSession* s = p->GetSession(); |
---|
| 87 | s->KickPlayer(); // mark session to remove at next session list update |
---|
| 88 | s->LogoutPlayer(false); // logout player without waiting next session list update |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | Player::DeleteFromDB(guid, accid, false); // no need to update realm characters |
---|
| 92 | } while (result->NextRow()); |
---|
| 93 | |
---|
| 94 | delete result; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | // table realm specific but common for all characters of account for realm |
---|
| 98 | CharacterDatabase.PExecute("DELETE FROM character_tutorial WHERE account = '%u'",accid); |
---|
| 99 | |
---|
| 100 | loginDatabase.BeginTransaction(); |
---|
| 101 | |
---|
| 102 | bool res = |
---|
| 103 | loginDatabase.PExecute("DELETE FROM account WHERE id='%d'", accid) && |
---|
| 104 | loginDatabase.PExecute("DELETE FROM realmcharacters WHERE acctid='%d'", accid); |
---|
| 105 | |
---|
| 106 | loginDatabase.CommitTransaction(); |
---|
| 107 | |
---|
| 108 | if(!res) |
---|
| 109 | return AOR_DB_INTERNAL_ERROR; // unexpected error; |
---|
| 110 | |
---|
| 111 | return AOR_OK; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | AccountOpResult AccountMgr::ChangeUsername(uint32 accid, std::string new_uname, std::string new_passwd) |
---|
| 115 | { |
---|
| 116 | QueryResult *result = loginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accid); |
---|
| 117 | if(!result) |
---|
| 118 | return AOR_NAME_NOT_EXIST; // account doesn't exist |
---|
| 119 | delete result; |
---|
| 120 | |
---|
| 121 | if(utf8length(new_uname) > MAX_ACCOUNT_STR) |
---|
| 122 | return AOR_NAME_TOO_LONG; |
---|
| 123 | |
---|
| 124 | if(utf8length(new_passwd) > MAX_ACCOUNT_STR) |
---|
| 125 | return AOR_PASS_TOO_LONG; |
---|
| 126 | |
---|
| 127 | normilizeString(new_uname); |
---|
| 128 | normilizeString(new_passwd); |
---|
| 129 | |
---|
| 130 | loginDatabase.escape_string(new_uname); |
---|
| 131 | loginDatabase.escape_string(new_passwd); |
---|
| 132 | if(!loginDatabase.PExecute("UPDATE account SET username='%s',sha_pass_hash=SHA1(CONCAT('%s',':','%s')) WHERE id='%d'", new_uname.c_str(), new_uname.c_str(), new_passwd.c_str(), accid)) |
---|
| 133 | return AOR_DB_INTERNAL_ERROR; // unexpected error |
---|
| 134 | |
---|
| 135 | return AOR_OK; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | AccountOpResult AccountMgr::ChangePassword(uint32 accid, std::string new_passwd) |
---|
| 139 | { |
---|
| 140 | QueryResult *result = loginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accid); |
---|
| 141 | if(!result) |
---|
| 142 | return AOR_NAME_NOT_EXIST; // account doesn't exist |
---|
| 143 | delete result; |
---|
| 144 | |
---|
| 145 | if (utf8length(new_passwd) > MAX_ACCOUNT_STR) |
---|
| 146 | return AOR_PASS_TOO_LONG; |
---|
| 147 | |
---|
| 148 | normilizeString(new_passwd); |
---|
| 149 | |
---|
| 150 | loginDatabase.escape_string(new_passwd); |
---|
| 151 | if(!loginDatabase.PExecute("UPDATE account SET sha_pass_hash=SHA1(CONCAT(username,':','%s')) WHERE id='%d'", new_passwd.c_str(), accid)) |
---|
| 152 | return AOR_DB_INTERNAL_ERROR; // unexpected error |
---|
| 153 | |
---|
| 154 | return AOR_OK; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | uint32 AccountMgr::GetId(std::string username) |
---|
| 158 | { |
---|
| 159 | loginDatabase.escape_string(username); |
---|
| 160 | QueryResult *result = loginDatabase.PQuery("SELECT id FROM account WHERE username = '%s'", username.c_str()); |
---|
| 161 | if(!result) |
---|
| 162 | return 0; |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | uint32 id = (*result)[0].GetUInt32(); |
---|
| 166 | delete result; |
---|
| 167 | return id; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | bool AccountMgr::CheckPassword(uint32 accid, std::string passwd) |
---|
| 172 | { |
---|
| 173 | normilizeString(passwd); |
---|
| 174 | loginDatabase.escape_string(passwd); |
---|
| 175 | |
---|
| 176 | QueryResult *result = loginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d' AND sha_pass_hash=SHA1(CONCAT(username,':','%s'))", accid, passwd.c_str()); |
---|
| 177 | if (result) |
---|
| 178 | { |
---|
| 179 | delete result; |
---|
| 180 | return true; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return false; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | bool AccountMgr::normilizeString(std::string& utf8str) |
---|
| 187 | { |
---|
| 188 | wchar_t wstr_buf[MAX_ACCOUNT_STR+1]; |
---|
| 189 | |
---|
| 190 | size_t wstr_len = MAX_ACCOUNT_STR; |
---|
| 191 | if(!Utf8toWStr(utf8str,wstr_buf,wstr_len)) |
---|
| 192 | return false; |
---|
| 193 | |
---|
| 194 | std::transform( &wstr_buf[0], wstr_buf+wstr_len, &wstr_buf[0], wcharToUpperOnlyLatin ); |
---|
| 195 | |
---|
| 196 | return WStrToUtf8(wstr_buf,wstr_len,utf8str); |
---|
| 197 | } |
---|