LoginDatabase.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. */
  6. #include "../common/debug.h"
  7. #include <iostream>
  8. #include <sstream>
  9. #include <iomanip>
  10. using namespace std;
  11. #ifdef WIN32
  12. #define WIN32_LEAN_AND_MEAN
  13. #include <windows.h>
  14. #define snprintf _snprintf
  15. #define strncasecmp _strnicmp
  16. #define strcasecmp _stricmp
  17. #else
  18. #include "../common/unix.h"
  19. #include <netinet/in.h>
  20. #endif
  21. #include "../common/Log.h"
  22. #include "../common/DatabaseNew.h"
  23. #include "LoginDatabase.h"
  24. #include "LoginAccount.h"
  25. #include "../common/MiscFunctions.h"
  26. #include "../common/packet_functions.h"
  27. #include "../common/packet_dump.h"
  28. #include "LWorld.h"
  29. extern LoginDatabase database;
  30. extern LWorldList world_list;
  31. bool LoginDatabase::ConnectNewDatabase() {
  32. return dbLogin.Connect();
  33. }
  34. void LoginDatabase::RemoveDeletedCharacterData()
  35. {
  36. dbLogin.Query("DELETE FROM login_char_colors WHERE login_characters_id IN (SELECT id FROM login_characters WHERE deleted = 1)");
  37. dbLogin.Query("DELETE FROM login_equipment WHERE login_characters_id IN (SELECT id FROM login_characters WHERE deleted = 1)");
  38. }
  39. void LoginDatabase::SetZoneInformation(int32 server_id, int32 zone_id, int32 version, PacketStruct* packet){
  40. if(packet){
  41. Query query;
  42. MYSQL_RES* result = 0;
  43. if ( version >= 1212 )
  44. result = query.RunQuery2(Q_SELECT, "SELECT name, description from ls_world_zones where server_id=%i and zone_id=%i", server_id, zone_id);
  45. MYSQL_ROW row;
  46. if(result && (row = mysql_fetch_row(result))) {
  47. if (row[0])
  48. packet->setMediumStringByName("zone", row[0]);
  49. else
  50. packet->setMediumStringByName("zone", " ");
  51. if(row[1])
  52. packet->setMediumStringByName("zonedesc", row[1]);
  53. else
  54. packet->setMediumStringByName("zonedesc", " ");
  55. }
  56. else{
  57. Query query2;
  58. MYSQL_RES* result2 = 0;
  59. if (version < 1212)
  60. result2 = query2.RunQuery2(Q_SELECT, "SELECT file, description from zones where id=%i", zone_id);
  61. else
  62. result2 = query2.RunQuery2(Q_SELECT, "SELECT name, description from zones where id=%i", zone_id);
  63. MYSQL_ROW row2;
  64. if(result2 && (row2 = mysql_fetch_row(result2))) {
  65. if (version != 546 && version < 1212)
  66. {
  67. if (row2[0])
  68. {
  69. int len = strlen(row2[0]);
  70. char* zoneName = new char[len + 2];
  71. strncpy(zoneName, row2[0], len);
  72. zoneName[len] = 0x2E;
  73. zoneName[len + 1] = 0x30;
  74. packet->setMediumStringByName("zone", zoneName);
  75. safe_delete_array(zoneName);
  76. }
  77. else
  78. packet->setMediumStringByName("zone", ".0");
  79. }
  80. else
  81. {
  82. if (row2[0])
  83. packet->setMediumStringByName("zone", row2[0]);
  84. else
  85. packet->setMediumStringByName("zone", " ");
  86. }
  87. if(row2[1])
  88. packet->setMediumStringByName("zonedesc", row2[1]);
  89. else
  90. packet->setMediumStringByName("zonedesc", " ");
  91. }
  92. }
  93. packet->setMediumStringByName("zonename2"," ");
  94. }
  95. }
  96. string LoginDatabase::GetZoneDescription(char* name){
  97. string ret;
  98. Query query;
  99. query.escaped_name = getEscapeString(name);
  100. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT description from zones where file=substring_index('%s', '.', 1)", query.escaped_name);
  101. MYSQL_ROW row;
  102. if((row = mysql_fetch_row(result))) {
  103. ret = string(row[0]);
  104. }
  105. return ret;
  106. }
  107. int32 LoginDatabase::GetLoginCharacterIDFromWorldCharID(int32 server_id, int32 char_id)
  108. {
  109. int32 ret;
  110. Query query;
  111. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id FROM login_characters WHERE server_id = %u AND char_id = %u AND deleted = 0 LIMIT 0,1", server_id, char_id);
  112. MYSQL_ROW row;
  113. if((row = mysql_fetch_row(result))) {
  114. ret = atoi(row[0]);
  115. }
  116. return ret;
  117. }
  118. void LoginDatabase::SetServerEquipmentAppearances(int32 server_id, map<int32, LoginEquipmentUpdate> equip_updates)
  119. {
  120. if(equip_updates.size() > 0)
  121. {
  122. LogWrite(LOGIN__DEBUG, 0, "Login", "Saving appearance info from world %u...", server_id);
  123. map<int32, LoginEquipmentUpdate>::iterator equip_itr;
  124. stringstream ss;
  125. ss << "replace into login_equipment (login_characters_id, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue, slot) values";
  126. int count=0;
  127. int32 char_id = 0;
  128. for(equip_itr = equip_updates.begin(); equip_itr != equip_updates.end(); equip_itr++)
  129. {
  130. char_id = GetLoginCharacterIDFromWorldCharID(server_id, (int32)equip_itr->second.world_char_id);
  131. if( char_id == 0 ) // invalid character/world match
  132. continue;
  133. LogWrite(LOGIN__DEBUG, 5, "Login", "--Processing character %u, slot %i", char_id, (int32)equip_itr->second.slot);
  134. if(count > 0)
  135. ss << ", ";
  136. ss << "(" << char_id << ", ";
  137. ss << (int32)equip_itr->second.equip_type << ", ";
  138. ss << (int32)equip_itr->second.red << ", ";
  139. ss << (int32)equip_itr->second.green << ", ";
  140. ss << (int32)equip_itr->second.blue << ", ";
  141. ss << (int32)equip_itr->second.highlight_red << ", ";
  142. ss << (int32)equip_itr->second.highlight_green << ", ";
  143. ss << (int32)equip_itr->second.highlight_blue << ", ";
  144. ss << (int32)equip_itr->second.slot << ")";
  145. count++;
  146. }
  147. if( !dbLogin.Query(ss.str().c_str()) )
  148. LogWrite(LOGIN__ERROR, 0, "Login", "Error saving login_equipment data");
  149. }
  150. }
  151. void LoginDatabase::SetServerZoneDescriptions(int32 server_id, map<int32, LoginZoneUpdate> zone_descriptions){
  152. if(zone_descriptions.size() > 0){
  153. map<int32, LoginZoneUpdate>::iterator zone_itr;
  154. string query_string = "replace into ls_world_zones (server_id, zone_id, name, description) values";
  155. int count=0;
  156. char server_id_str[12] = {0};
  157. sprintf(server_id_str, "%i", server_id);
  158. for(zone_itr = zone_descriptions.begin(); zone_itr != zone_descriptions.end(); zone_itr++, count++){
  159. char zone_id_str[12] = {0};
  160. sprintf(zone_id_str, "%i", zone_itr->first);
  161. if(count > 0)
  162. query_string.append(", ");
  163. query_string.append("(").append(server_id_str).append(",");
  164. query_string.append(zone_id_str).append(",");
  165. query_string.append("'").append(getSafeEscapeString(zone_itr->second.name.c_str()).c_str()).append("', '");
  166. query_string.append(getSafeEscapeString(zone_itr->second.description.c_str()).c_str()).append("')");
  167. }
  168. Query query;
  169. query.RunQuery2(query_string, Q_REPLACE);
  170. }
  171. }
  172. //this is really just for the version that doesn't send the server id in its play request
  173. int32 LoginDatabase::GetServer(int32 accountID, int32 charID, string name) {
  174. int32 id = 0;
  175. Query query;
  176. MYSQL_ROW row;
  177. query.escaped_name = getEscapeString(name.c_str());
  178. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT server_id from login_characters where account_id=%i and char_id=%i and name='%s'", accountID, charID, query.escaped_name);
  179. if (result && mysql_num_rows(result) == 1) {
  180. row = mysql_fetch_row(result);
  181. id = atoi(row[0]);
  182. }
  183. return id;
  184. }
  185. void LoginDatabase::LoadCharacters(LoginAccount* acct, int16 version){
  186. if(acct != NULL)
  187. acct->flushCharacters ( );
  188. Query query;
  189. Query query2;
  190. int32 id = 0;
  191. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT lc.char_id, lc.server_id, lc.name, lc.race, lc.class, lc.gender, lc.deity, lc.body_size, lc.body_age, lc.current_zone_id, lc.level, lc.soga_wing_type, lc.soga_chest_type, lc.soga_legs_type, lc.soga_hair_type, lc.legs_type, lc.chest_type, lc.wing_type, lc.hair_type, unix_timestamp(lc.created_date), unix_timestamp(lc.last_played), lc.id, lw.name, lc.facial_hair_type, lc.soga_facial_hair_type, lc.soga_model_type, lc.model_type from login_characters lc, login_worldservers lw where lw.id = lc.server_id and lc.account_id=%i and lc.deleted=0",acct->getLoginAccountID());
  192. if(result) {
  193. MYSQL_ROW row;
  194. MYSQL_ROW row2;
  195. MYSQL_ROW row3;
  196. while ((row = mysql_fetch_row(result))) {
  197. CharSelectProfile* player = new CharSelectProfile(version);
  198. id = atoul(row[0]);
  199. //for (int i = 0; i < 10; i++)
  200. // player->packet->setDataByName("hair_type", 0, i);
  201. //player->packet->setDataByName("test23", 413);
  202. //player->packet->setDataByName("test24", 414);
  203. player->packet->setDataByName("charid", id);
  204. player->packet->setDataByName("server_id", atoul(row[1]));
  205. player->packet->setMediumStringByName("name", row[2]);
  206. player->packet->setDataByName("race", atoi(row[3]));
  207. player->packet->setDataByName("class", atoi(row[4]));
  208. player->packet->setDataByName("gender", atoi(row[5]));
  209. player->packet->setDataByName("deity", atoi(row[6]));
  210. player->packet->setDataByName("body_size", atof(row[7]));
  211. player->packet->setDataByName("body_age", atof(row[8]));
  212. SetZoneInformation(atoi(row[1]), atoi(row[9]), version, player->packet);
  213. player->packet->setDataByName("level", atoi(row[10]));
  214. if(atoi(row[11]) > 0)
  215. player->packet->setDataByName("soga_wing_type", atoi(row[11]));
  216. else
  217. player->packet->setDataByName("soga_wing_type", atoi(row[17]));
  218. if(atoi(row[12]) > 0)
  219. player->packet->setDataByName("soga_chest_type", atoi(row[12]));
  220. else
  221. player->packet->setDataByName("soga_chest_type", atoi(row[16]));
  222. if(atoi(row[13]) > 0)
  223. player->packet->setDataByName("soga_legs_type", atoi(row[13]));
  224. else
  225. player->packet->setDataByName("soga_legs_type", atoi(row[15]));
  226. if(atoi(row[14]) > 0)
  227. player->packet->setDataByName("soga_hair_type", atoi(row[14]));
  228. else
  229. player->packet->setDataByName("soga_hair_type", atoi(row[18]));
  230. player->packet->setDataByName("legs_type", atoi(row[15]));
  231. player->packet->setDataByName("chest_type", atoi(row[16]));
  232. player->packet->setDataByName("wing_type", atoi(row[17]));
  233. player->packet->setDataByName("hair_type", atoi(row[18]));
  234. player->packet->setDataByName("created_date", atol(row[19]));
  235. if (row[20])
  236. player->packet->setDataByName("last_played", atol(row[20]));
  237. if(version == 546)
  238. player->packet->setDataByName("version", 11);
  239. else if(version >= 887)
  240. player->packet->setDataByName("version", 6);
  241. else
  242. player->packet->setDataByName("version", 5);
  243. player->packet->setDataByName("account_id", acct->getLoginAccountID());
  244. player->packet->setDataByName("account_id2", acct->getLoginAccountID());
  245. LoadAppearanceData(atol(row[21]), player->packet);
  246. if(row[22])
  247. player->packet->setMediumStringByName("server_name", row[22]);
  248. player->packet->setDataByName("hair_face_type", atoi(row[23]));
  249. if(atoi(row[24]) > 0)
  250. player->packet->setDataByName("soga_hair_face_type", atoi(row[24]));
  251. else
  252. player->packet->setDataByName("soga_hair_face_type", atoi(row[23]));
  253. if(atoi(row[25]) > 0)
  254. player->packet->setDataByName("soga_race_type", atoi(row[25]));
  255. else
  256. player->packet->setDataByName("soga_race_type", atoi(row[26]));
  257. player->packet->setDataByName("race_type", atoi(row[26]));
  258. player->packet->setDataByName("unknown3", 57);
  259. player->packet->setDataByName("unknown4", 56);
  260. player->packet->setDataByName("unknown6", 1, 1); //if not here will not display character
  261. player->packet->setDataByName("unknown8", 15);
  262. player->packet->setDataByName("unknown13", 212);
  263. player->packet->setColorByName("unknown14", 0xFF, 0xFF, 0xFF);
  264. uchar tmp[] = {0xFF, 0xFF, 0xFF, 0x61, 0x00, 0x2C, 0x04, 0xA5, 0x09, 0x02, 0x0F, 0x00, 0x00};
  265. for(size_t y=0;y<sizeof(tmp);y++)
  266. player->packet->setDataByName("unknown11", tmp[y], y);
  267. MYSQL_RES* result3 = query2.RunQuery2(Q_SELECT, "SELECT slot, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue from login_equipment where login_characters_id=%i order by slot",atoi(row[21]));
  268. if(result3){
  269. for(int i=0;(row3 = mysql_fetch_row(result3)) && i<24; i++){
  270. player->packet->setEquipmentByName("equip", atoi(row3[1]), atoi(row3[2]), atoi(row3[3]), atoi(row3[4]), atoi(row3[5]), atoi(row3[6]), atoi(row3[7]), atoi(row3[0]));
  271. }
  272. }
  273. player->packet->setDataByName("mount", 1377);
  274. player->packet->setDataByName("mount_color1", 57);
  275. /*
  276. enum NetAppearance::NetAppearanceFlags
  277. {
  278. NAF_INVISIBLE=1,
  279. NAF_SHOW_HOOD=2
  280. };
  281. */
  282. acct->addCharacter(player);
  283. }
  284. }
  285. else
  286. LogWrite(LOGIN__ERROR, 0, "Login", "Error in LoadCharacters query '%s': %s", query.GetQuery(), query.GetError());
  287. }
  288. void LoginDatabase::CheckCharacterTimeStamps(LoginAccount* acct){
  289. Query query;
  290. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT char_id, unix_timestamp from login_characters where account_id=%i",acct->getLoginAccountID());
  291. if(result && mysql_num_rows(result) > 0) {
  292. MYSQL_ROW row;
  293. ServerPacket* outpack = new ServerPacket(ServerOP_CharTimeStamp, sizeof(CharacterTimeStamp_Struct));
  294. CharacterTimeStamp_Struct* cts = (CharacterTimeStamp_Struct*) outpack->pBuffer;
  295. cts->account_id = acct->getLoginAccountID();
  296. int32 server_id = 0;
  297. LWorld* world_server = 0;
  298. while ((row = mysql_fetch_row(result))) {
  299. server_id = atoi(row[1]);
  300. if(server_id != 0)
  301. world_server = world_list.FindByAccount(server_id, World);
  302. if(world_server) // If the pointer is 0, the world server must be down, we can't do any updates...
  303. {
  304. cts->char_id = atoi(row[0]);
  305. cts->unix_timestamp = atoi(row[1]);
  306. world_server->SendPacket(outpack);
  307. //Reset for next character
  308. world_server = 0;
  309. server_id = 0;
  310. }
  311. }
  312. safe_delete(outpack);
  313. }
  314. }
  315. void LoginDatabase::SaveCharacterFloats(int32 char_id, char* type, float float1, float float2, float float3){
  316. Query query;
  317. string create_char = string("insert into login_char_colors (login_characters_id, type, red, green, blue, signed_value) values(%i,'%s',%i,%i,%i, 1)");
  318. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, (sint8)(float1*100), (sint8)(float2*100), (sint8)(float3*100));
  319. }
  320. void LoginDatabase::SaveCharacterColors(int32 char_id, char* type, EQ2_Color color){
  321. Query query;
  322. string create_char = string("insert into login_char_colors (login_characters_id, type, red, green, blue) values(%i,'%s',%i,%i,%i)");
  323. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, color.red, color.green, color.blue);
  324. }
  325. void LoginDatabase::LoadAppearanceData(int32 char_id, PacketStruct* char_select_packet){
  326. Query query;
  327. MYSQL_ROW row;
  328. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT type, signed_value, red, green, blue from login_char_colors where login_characters_id = %i",char_id);
  329. while((row = mysql_fetch_row(result))){
  330. if(atoi(row[1]) == 0)
  331. char_select_packet->setColorByName(row[0], atoi(row[2]), atoi(row[3]), atoi(row[4]));
  332. else{
  333. if (char_select_packet->GetVersion() <= 283)
  334. {
  335. char_select_packet->setDataByName(row[0], atoi(row[2]) * 2.5, 0);
  336. char_select_packet->setDataByName(row[0], atoi(row[3]) * 2.5, 1);
  337. char_select_packet->setDataByName(row[0], atoi(row[4]) * 2.5, 2);
  338. }
  339. else
  340. {
  341. char_select_packet->setDataByName(row[0], atoi(row[2]), 0);
  342. char_select_packet->setDataByName(row[0], atoi(row[3]), 1);
  343. char_select_packet->setDataByName(row[0], atoi(row[4]), 2);
  344. }
  345. }
  346. }
  347. }
  348. int16 LoginDatabase::GetAppearanceID(string name){
  349. int32 id = 0;
  350. Query query;
  351. MYSQL_ROW row;
  352. query.escaped_name = getEscapeString(name.c_str());
  353. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT appearance_id from appearances where name='%s'", query.escaped_name);
  354. if(result && mysql_num_rows(result) == 1){
  355. row = mysql_fetch_row(result);
  356. id = atoi(row[0]);
  357. }
  358. return id;
  359. }
  360. void LoginDatabase::DeactivateCharID(int32 server_id, int32 char_id, int32 exception_id){
  361. Query query;
  362. query.RunQuery2(Q_UPDATE, "update login_characters set deleted=1 where char_id=%u and server_id=%u and id!=%u",char_id,server_id,exception_id);
  363. }
  364. int32 LoginDatabase::SaveCharacter(PacketStruct* create, LoginAccount* acct, int32 world_charid, int32 client_version){
  365. int32 ret_id = 0;
  366. Query query;
  367. string create_char =
  368. string("Insert into login_characters (account_id, server_id, char_id, name, race, class, gender, deity, body_size, body_age, soga_wing_type, soga_chest_type, soga_legs_type, soga_hair_type, soga_facial_hair_type, legs_type, chest_type, wing_type, hair_type, facial_hair_type, soga_model_type, model_type)"
  369. " values(%i, %i, %i, '%s', %i, %i, %i, %i, %f, %f, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i)");
  370. query.RunQuery2(Q_INSERT, create_char.c_str(),
  371. acct->getLoginAccountID(),
  372. create->getType_int32_ByName("server_id"), world_charid,
  373. create->getType_EQ2_16BitString_ByName("name").data.c_str(),
  374. create->getType_int8_ByName("race"),
  375. create->getType_int8_ByName("class"),
  376. create->getType_int8_ByName("gender"),
  377. create->getType_int8_ByName("deity"),
  378. create->getType_float_ByName("body_size"),
  379. create->getType_float_ByName("body_age"),
  380. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_wing_file").data),
  381. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_chest_file").data),
  382. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_legs_file").data),
  383. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_hair_file").data),
  384. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_face_file").data),
  385. GetAppearanceID(create->getType_EQ2_16BitString_ByName("legs_file").data),
  386. GetAppearanceID(create->getType_EQ2_16BitString_ByName("chest_file").data),
  387. GetAppearanceID(create->getType_EQ2_16BitString_ByName("wing_file").data),
  388. GetAppearanceID(create->getType_EQ2_16BitString_ByName("hair_file").data),
  389. GetAppearanceID(create->getType_EQ2_16BitString_ByName("face_file").data),
  390. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_race_file").data),
  391. GetAppearanceID(create->getType_EQ2_16BitString_ByName("race_file").data));
  392. if(query.GetError() && strlen(query.GetError()) > 0){
  393. LogWrite(LOGIN__ERROR, 0, "Login", "Error in SaveCharacter query '%s': %s", query.GetQuery(), query.GetError());
  394. return 0;
  395. }
  396. int32 last_insert_id = query.GetLastInsertedID();
  397. //mark any remaining characters with same id as deleted (creates problems if world deleted their db and started assigning new char ids)
  398. DeactivateCharID(create->getType_int32_ByName("server_id"), world_charid, last_insert_id);
  399. int32 char_id = last_insert_id;
  400. if (client_version <= 546) {
  401. SaveCharacterFloats(char_id, "skin_color", create->getType_float_ByName("skin_color", 0), create->getType_float_ByName("skin_color", 1), create->getType_float_ByName("skin_color", 2));
  402. SaveCharacterFloats(char_id, "eye_color", create->getType_float_ByName("eye_color", 0), create->getType_float_ByName("eye_color", 1), create->getType_float_ByName("eye_color", 2));
  403. SaveCharacterFloats(char_id, "hair_color1", create->getType_float_ByName("hair_color1", 0), create->getType_float_ByName("hair_color1", 1), create->getType_float_ByName("hair_color1", 2));
  404. SaveCharacterFloats(char_id, "hair_color2", create->getType_float_ByName("hair_color2", 0), create->getType_float_ByName("hair_color2", 1), create->getType_float_ByName("hair_color2", 2));
  405. SaveCharacterFloats(char_id, "hair_highlight", create->getType_float_ByName("hair_highlight", 0), create->getType_float_ByName("hair_highlight", 1), create->getType_float_ByName("hair_highlight", 2));
  406. SaveCharacterFloats(char_id, "hair_type_color", create->getType_float_ByName("hair_type_color", 0), create->getType_float_ByName("hair_type_color", 1), create->getType_float_ByName("hair_type_color", 2));
  407. SaveCharacterFloats(char_id, "hair_type_highlight_color", create->getType_float_ByName("hair_type_highlight_color", 0), create->getType_float_ByName("hair_type_highlight_color", 1), create->getType_float_ByName("hair_type_highlight_color", 2));
  408. SaveCharacterFloats(char_id, "hair_type_color", create->getType_float_ByName("hair_type_color", 0), create->getType_float_ByName("hair_type_color", 1), create->getType_float_ByName("hair_type_color", 2));
  409. SaveCharacterFloats(char_id, "hair_type_highlight_color", create->getType_float_ByName("hair_type_highlight_color", 0), create->getType_float_ByName("hair_type_highlight_color", 1), create->getType_float_ByName("hair_type_highlight_color", 2));
  410. SaveCharacterFloats(char_id, "hair_face_color", create->getType_float_ByName("hair_face_color", 0), create->getType_float_ByName("hair_face_color", 1), create->getType_float_ByName("hair_face_color", 2));
  411. SaveCharacterFloats(char_id, "hair_face_highlight_color", create->getType_float_ByName("hair_face_highlight_color", 0), create->getType_float_ByName("hair_face_highlight_color", 1), create->getType_float_ByName("hair_face_highlight_color", 2));
  412. SaveCharacterFloats(char_id, "shirt_color", create->getType_float_ByName("shirt_color", 0), create->getType_float_ByName("shirt_color", 1), create->getType_float_ByName("shirt_color", 2));
  413. SaveCharacterFloats(char_id, "unknown_chest_color", create->getType_float_ByName("unknown_chest_color", 0), create->getType_float_ByName("unknown_chest_color", 1), create->getType_float_ByName("unknown_chest_color", 2));
  414. SaveCharacterFloats(char_id, "pants_color", create->getType_float_ByName("pants_color", 0), create->getType_float_ByName("pants_color", 1), create->getType_float_ByName("pants_color", 2));
  415. SaveCharacterFloats(char_id, "unknown_legs_color", create->getType_float_ByName("unknown_legs_color", 0), create->getType_float_ByName("unknown_legs_color", 1), create->getType_float_ByName("unknown_legs_color", 2));
  416. SaveCharacterFloats(char_id, "unknown9", create->getType_float_ByName("unknown9", 0), create->getType_float_ByName("unknown9", 1), create->getType_float_ByName("unknown9", 2));
  417. }
  418. else {
  419. SaveCharacterColors(char_id, "skin_color", create->getType_EQ2_Color_ByName("skin_color"));
  420. SaveCharacterColors(char_id, "model_color", create->getType_EQ2_Color_ByName("model_color"));
  421. SaveCharacterColors(char_id, "eye_color", create->getType_EQ2_Color_ByName("eye_color"));
  422. SaveCharacterColors(char_id, "hair_color1", create->getType_EQ2_Color_ByName("hair_color1"));
  423. SaveCharacterColors(char_id, "hair_color2", create->getType_EQ2_Color_ByName("hair_color2"));
  424. SaveCharacterColors(char_id, "hair_highlight", create->getType_EQ2_Color_ByName("hair_highlight"));
  425. SaveCharacterColors(char_id, "hair_type_color", create->getType_EQ2_Color_ByName("hair_type_color"));
  426. SaveCharacterColors(char_id, "hair_type_highlight_color", create->getType_EQ2_Color_ByName("hair_type_highlight_color"));
  427. SaveCharacterColors(char_id, "hair_face_color", create->getType_EQ2_Color_ByName("hair_face_color"));
  428. SaveCharacterColors(char_id, "hair_face_highlight_color", create->getType_EQ2_Color_ByName("hair_face_highlight_color"));
  429. SaveCharacterColors(char_id, "wing_color1", create->getType_EQ2_Color_ByName("wing_color1"));
  430. SaveCharacterColors(char_id, "wing_color2", create->getType_EQ2_Color_ByName("wing_color2"));
  431. SaveCharacterColors(char_id, "shirt_color", create->getType_EQ2_Color_ByName("shirt_color"));
  432. SaveCharacterColors(char_id, "unknown_chest_color", create->getType_EQ2_Color_ByName("unknown_chest_color"));
  433. SaveCharacterColors(char_id, "pants_color", create->getType_EQ2_Color_ByName("pants_color"));
  434. SaveCharacterColors(char_id, "unknown_legs_color", create->getType_EQ2_Color_ByName("unknown_legs_color"));
  435. SaveCharacterColors(char_id, "unknown9", create->getType_EQ2_Color_ByName("unknown9"));
  436. SaveCharacterColors(char_id, "soga_skin_color", create->getType_EQ2_Color_ByName("soga_skin_color"));
  437. SaveCharacterColors(char_id, "soga_model_color", create->getType_EQ2_Color_ByName("soga_model_color"));
  438. SaveCharacterColors(char_id, "soga_eye_color", create->getType_EQ2_Color_ByName("soga_eye_color"));
  439. SaveCharacterColors(char_id, "soga_hair_color1", create->getType_EQ2_Color_ByName("soga_hair_color1"));
  440. SaveCharacterColors(char_id, "soga_hair_color2", create->getType_EQ2_Color_ByName("soga_hair_color2"));
  441. SaveCharacterColors(char_id, "soga_hair_highlight", create->getType_EQ2_Color_ByName("soga_hair_highlight"));
  442. SaveCharacterColors(char_id, "soga_hair_type_color", create->getType_EQ2_Color_ByName("soga_hair_type_color"));
  443. SaveCharacterColors(char_id, "soga_hair_type_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_type_highlight_color"));
  444. SaveCharacterColors(char_id, "soga_hair_face_color", create->getType_EQ2_Color_ByName("soga_hair_face_color"));
  445. SaveCharacterColors(char_id, "soga_hair_face_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_face_highlight_color"));
  446. SaveCharacterColors(char_id, "soga_wing_color1", create->getType_EQ2_Color_ByName("soga_wing_color1"));
  447. SaveCharacterColors(char_id, "soga_wing_color2", create->getType_EQ2_Color_ByName("soga_wing_color2"));
  448. SaveCharacterColors(char_id, "soga_shirt_color", create->getType_EQ2_Color_ByName("soga_shirt_color"));
  449. SaveCharacterColors(char_id, "soga_unknown_chest_color", create->getType_EQ2_Color_ByName("soga_unknown_chest_color"));
  450. SaveCharacterColors(char_id, "soga_pants_color", create->getType_EQ2_Color_ByName("soga_pants_color"));
  451. SaveCharacterColors(char_id, "soga_unknown_legs_color", create->getType_EQ2_Color_ByName("soga_unknown_legs_color"));
  452. SaveCharacterColors(char_id, "soga_unknown13", create->getType_EQ2_Color_ByName("soga_unknown13"));
  453. SaveCharacterFloats(char_id, "soga_eye_type", create->getType_float_ByName("soga_eyes2", 0), create->getType_float_ByName("soga_eyes2", 1), create->getType_float_ByName("soga_eyes2", 2));
  454. SaveCharacterFloats(char_id, "soga_ear_type", create->getType_float_ByName("soga_ears", 0), create->getType_float_ByName("soga_ears", 1), create->getType_float_ByName("soga_ears", 2));
  455. SaveCharacterFloats(char_id, "soga_eye_brow_type", create->getType_float_ByName("soga_eye_brows", 0), create->getType_float_ByName("soga_eye_brows", 1), create->getType_float_ByName("soga_eye_brows", 2));
  456. SaveCharacterFloats(char_id, "soga_cheek_type", create->getType_float_ByName("soga_cheeks", 0), create->getType_float_ByName("soga_cheeks", 1), create->getType_float_ByName("soga_cheeks", 2));
  457. SaveCharacterFloats(char_id, "soga_lip_type", create->getType_float_ByName("soga_lips", 0), create->getType_float_ByName("soga_lips", 1), create->getType_float_ByName("soga_lips", 2));
  458. SaveCharacterFloats(char_id, "soga_chin_type", create->getType_float_ByName("soga_chin", 0), create->getType_float_ByName("soga_chin", 1), create->getType_float_ByName("soga_chin", 2));
  459. SaveCharacterFloats(char_id, "soga_nose_type", create->getType_float_ByName("soga_nose", 0), create->getType_float_ByName("soga_nose", 1), create->getType_float_ByName("soga_nose", 2));
  460. }
  461. SaveCharacterFloats(char_id, "eye_type", create->getType_float_ByName("eyes2", 0), create->getType_float_ByName("eyes2", 1), create->getType_float_ByName("eyes2", 2));
  462. SaveCharacterFloats(char_id, "ear_type", create->getType_float_ByName("ears", 0), create->getType_float_ByName("ears", 1), create->getType_float_ByName("ears", 2));
  463. SaveCharacterFloats(char_id, "eye_brow_type", create->getType_float_ByName("eye_brows", 0), create->getType_float_ByName("eye_brows", 1), create->getType_float_ByName("eye_brows", 2));
  464. SaveCharacterFloats(char_id, "cheek_type", create->getType_float_ByName("cheeks", 0), create->getType_float_ByName("cheeks", 1), create->getType_float_ByName("cheeks", 2));
  465. SaveCharacterFloats(char_id, "lip_type", create->getType_float_ByName("lips", 0), create->getType_float_ByName("lips", 1), create->getType_float_ByName("lips", 2));
  466. SaveCharacterFloats(char_id, "chin_type", create->getType_float_ByName("chin", 0), create->getType_float_ByName("chin", 1), create->getType_float_ByName("chin", 2));
  467. SaveCharacterFloats(char_id, "nose_type", create->getType_float_ByName("nose", 0), create->getType_float_ByName("nose", 1), create->getType_float_ByName("nose", 2));
  468. SaveCharacterFloats(char_id, "body_size", create->getType_float_ByName("body_size", 0), 0, 0);
  469. return ret_id;
  470. }
  471. bool LoginDatabase::DeleteCharacter(int32 account_id, int32 character_id, int32 server_id){
  472. Query query;
  473. string delete_char = string("delete from login_characters where char_id=%i and account_id=%i and server_id=%i");
  474. query.RunQuery2(Q_DELETE, delete_char.c_str(),character_id,account_id,server_id);
  475. if(!query.GetAffectedRows())
  476. {
  477. //No error just in case ppl try doing stupid stuff
  478. return false;
  479. }
  480. return true;
  481. }
  482. string LoginDatabase::GetCharacterName(int32 char_id, int32 server_id){
  483. Query query;
  484. MYSQL_ROW row;
  485. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name from login_characters where char_id=%lu and server_id=%lu and deleted = 0", char_id,server_id);
  486. if(result && mysql_num_rows(result) == 1){
  487. row = mysql_fetch_row(result);
  488. return string(row[0]);
  489. }
  490. return string("");
  491. }
  492. bool LoginDatabase::UpdateCharacterTimeStamp(int32 account_id, int32 character_id, int32 timestamp_update, int32 server_id){
  493. Query query;
  494. string update_charts = string("update login_characters set unix_timestamp=%lu where char_id=%lu and account_id=%lu and server_id=%lu");
  495. query.RunQuery2(Q_UPDATE, update_charts.c_str(),timestamp_update,character_id,account_id,server_id);
  496. if(!query.GetAffectedRows())
  497. {
  498. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterTimeStamp query '%s': %s", query.GetQuery(), query.GetError());
  499. return false;
  500. }
  501. return true;
  502. }
  503. bool LoginDatabase::UpdateCharacterLevel(int32 account_id, int32 character_id, int8 in_level, int32 server_id){
  504. Query query;
  505. string update_charts = string("update login_characters set level=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  506. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_level,character_id,account_id,server_id);
  507. if(!query.GetAffectedRows())
  508. {
  509. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterLevel query '%s': %s", query.GetQuery(), query.GetError());
  510. return false;
  511. }
  512. return true;
  513. }
  514. bool LoginDatabase::UpdateCharacterRace(int32 account_id, int32 character_id, int16 in_racetype, int8 in_race, int32 server_id){
  515. Query query;
  516. string update_charts = string("update login_characters set race_type=%i, race=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  517. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_racetype,in_race,character_id,account_id,server_id);
  518. if(!query.GetAffectedRows())
  519. {
  520. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterRace query '%s': %s", query.GetQuery(), query.GetError());
  521. return false;
  522. }
  523. return true;
  524. }
  525. bool LoginDatabase::UpdateCharacterZone(int32 account_id, int32 character_id, int32 zone_id, int32 server_id){
  526. Query query;
  527. string update_chars = string("update login_characters set current_zone_id=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  528. query.RunQuery2(Q_UPDATE, update_chars.c_str(), zone_id, character_id, account_id, server_id);
  529. if(!query.GetAffectedRows())
  530. {
  531. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterZone query '%s': %s", query.GetQuery(), query.GetError());
  532. return false;
  533. }
  534. return true;
  535. }
  536. bool LoginDatabase::UpdateCharacterClass(int32 account_id, int32 character_id, int8 in_class, int32 server_id){
  537. Query query;
  538. string update_charts = string("update login_characters set class=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  539. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_class,character_id,account_id,server_id);
  540. if(!query.GetAffectedRows())
  541. {
  542. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterClass query '%s': %s", query.GetQuery(), query.GetError());
  543. return false;
  544. }
  545. return true;
  546. }
  547. bool LoginDatabase::UpdateCharacterGender(int32 account_id, int32 character_id, int8 in_gender, int32 server_id){
  548. Query query;
  549. string update_charts = string("update login_characters set gender=%i where char_id=%lu and account_id=%lu and server_id=%lu");
  550. query.RunQuery2(Q_UPDATE, update_charts.c_str(),in_gender,character_id,account_id,server_id);
  551. if(!query.GetAffectedRows())
  552. {
  553. LogWrite(LOGIN__ERROR, 0, "Login", "Error in UpdateCharacterClass query '%s': %s", query.GetQuery(), query.GetError());
  554. return false;
  555. }
  556. return true;
  557. }
  558. LoginAccount* LoginDatabase::LoadAccount(const char* name, const char* password, bool attemptAccountCreation){
  559. LoginAccount* acct = NULL;
  560. Query query;
  561. query.escaped_name = getEscapeString(name);
  562. query.escaped_pass = getEscapeString(password);
  563. MYSQL_ROW row;
  564. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id from account where name='%s' and passwd=sha2('%s',512)", query.escaped_name, query.escaped_pass);
  565. if(result){
  566. if (mysql_num_rows(result) == 1){
  567. row = mysql_fetch_row(result);
  568. int32 id = atol(row[0]);
  569. acct = new LoginAccount(id, name, password);
  570. acct->setAuthenticated(true);
  571. }
  572. else if(mysql_num_rows(result) > 0)
  573. LogWrite(LOGIN__ERROR, 0, "Login", "Error in LoginAccount: more than one account returned for '%s'", name);
  574. else if (attemptAccountCreation && !database.GetAccountIDByName(name))
  575. {
  576. Query newquery;
  577. newquery.RunQuery2(Q_INSERT, "insert into account set name='%s',passwd=sha2('%s',512)", query.escaped_name, query.escaped_pass);
  578. // re-run the query for select only not account creation
  579. return LoadAccount(name, password, false);
  580. }
  581. }
  582. return acct;
  583. }
  584. int32 LoginDatabase::GetAccountIDByName(const char* name) {
  585. int32 id = 0;
  586. Query query;
  587. MYSQL_ROW row;
  588. query.escaped_name = getEscapeString(name);
  589. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id from account where name='%s'", query.escaped_name);
  590. if (result && mysql_num_rows(result) == 1) {
  591. row = mysql_fetch_row(result);
  592. id = atoi(row[0]);
  593. }
  594. return id;
  595. }
  596. int32 LoginDatabase::CheckServerAccount(char* name, char* passwd){
  597. int32 id = 0;
  598. Query query;
  599. MYSQL_ROW row;
  600. query.escaped_name = getEscapeString(name);
  601. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT lower(password), id from login_worldservers where account='%s' and disabled = 0", query.escaped_name);
  602. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer CheckServerAccount Account=%s\nSHA=%s", (char*)query.escaped_name, passwd);
  603. if(result && mysql_num_rows(result) == 1){
  604. row = mysql_fetch_row(result);
  605. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer CheckServerAccountResult Account=%s\nPassword=%s", (char*)query.escaped_name, (row && row[0]) ? row[0] : "(NULL)");
  606. if (memcmp(row[0], passwd, strnlen(row[0], 256)) == 0)
  607. {
  608. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer CheckServerAccountResultMatch Account=%s", (char*)query.escaped_name);
  609. id = atoi(row[1]);
  610. }
  611. }
  612. return id;
  613. }
  614. bool LoginDatabase::IsServerAccountDisabled(char* name){
  615. Query query;
  616. MYSQL_ROW row;
  617. query.escaped_name = getEscapeString(name);
  618. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id from login_worldservers where account='%s' and disabled = 1", query.escaped_name);
  619. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer IsServerAccountDisabled Account=%s", (char*)query.escaped_name);
  620. if(result && mysql_num_rows(result) > 0){
  621. row = mysql_fetch_row(result);
  622. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer IsServerAccountDisabled Match Account=%s", (char*)query.escaped_name);
  623. return true;
  624. }
  625. return false;
  626. }
  627. bool LoginDatabase::IsIPBanned(char* ipaddr){
  628. if(!ipaddr)
  629. return false;
  630. Query query;
  631. MYSQL_ROW row;
  632. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT ip from login_bannedips where '%s' LIKE CONCAT(ip ,'%%')", ipaddr);
  633. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer IsServerIPBanned IPPartial=%s", (char*)ipaddr);
  634. if(result && mysql_num_rows(result) > 0){
  635. row = mysql_fetch_row(result);
  636. LogWrite(LOGIN__INFO, 0, "Login", "WorldServer IsServerIPBanned Match IPBan=%s", row[0]);
  637. return true;
  638. }
  639. return false;
  640. }
  641. void LoginDatabase::GetServerAccounts(vector<LWorld*>* server_list){
  642. Query query;
  643. MYSQL_ROW row;
  644. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, account, name, admin_id from login_worldservers");
  645. while((row = mysql_fetch_row(result))){
  646. LWorld* world = new LWorld(atol(row[0]), row[1], row[2], atoi(row[3]));
  647. world->SetID(world->GetAccountID());
  648. server_list->push_back(world);
  649. }
  650. }
  651. void LoginDatabase::SaveClientLog(const char* type, const char* message, const char* player_name, int16 version){
  652. Query query;
  653. query.escaped_data1 = getEscapeString(message);
  654. query.escaped_name = getEscapeString(player_name);
  655. query.RunQuery2(Q_INSERT, "insert into log_messages (type, message, name, version) values('%s', '%s', '%s', %i)", type, query.escaped_data1, query.escaped_name, version);
  656. }
  657. bool LoginDatabase::VerifyDelete(int32 account_id, int32 character_id, const char* name){
  658. Query query;
  659. query.escaped_name = getEscapeString(name);
  660. query.RunQuery2(Q_UPDATE, "update login_characters set deleted = 1 where char_id=%i and account_id=%i and name='%s'", character_id, account_id, query.escaped_name);
  661. if(query.GetAffectedRows() == 1)
  662. return true;
  663. else
  664. return false;
  665. }
  666. char* LoginDatabase::GetServerAccountName(int32 id){
  667. Query query;
  668. MYSQL_ROW row;
  669. char* name = 0;
  670. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name from login_worldservers where id=%lu", id);
  671. if(result && mysql_num_rows(result) == 1){
  672. row = mysql_fetch_row(result);
  673. if(strlen(row[0]) > 0){
  674. name = new char[strlen(row[0])+1];
  675. strcpy(name, row[0]);
  676. }
  677. }
  678. return name;
  679. }
  680. int32 LoginDatabase::GetRaceID(char* name){
  681. int32 ret = 1487;
  682. Query query;
  683. MYSQL_ROW row;
  684. query.escaped_name = getEscapeString(name);
  685. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT race_type from login_races where name='%s'", query.escaped_name);
  686. if(result && mysql_num_rows(result) == 1){
  687. row = mysql_fetch_row(result);
  688. ret = atol(row[0]);
  689. }
  690. else if(!result || mysql_num_rows(result) == 0)
  691. UpdateRaceID(query.escaped_name);
  692. return ret;
  693. }
  694. void LoginDatabase::UpdateRaceID(char* name){
  695. Query query;
  696. query.RunQuery2(Q_UPDATE, "insert into login_races (name) values('%s')", name);
  697. }
  698. bool LoginDatabase::CheckVersion(char* in_version){
  699. Query query;
  700. query.escaped_data1 = getEscapeString(in_version);
  701. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id from login_versions where version='%s' or version='*'", query.escaped_data1);
  702. if(result && mysql_num_rows(result) > 0)
  703. return true;
  704. else
  705. return false;
  706. }
  707. void LoginDatabase::GetLatestTableVersions(LatestTableVersions* table_versions){
  708. Query query;
  709. MYSQL_ROW row;
  710. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name, max(version) from login_table_versions group by name order by id");
  711. if(result && mysql_num_rows(result) > 0){
  712. table_versions->SetTableSize(mysql_num_rows(result));
  713. }
  714. else // we need to return if theres no result, otherwise it will crash attempting to loop through rows
  715. return;
  716. while((row = mysql_fetch_row(result))){
  717. if(VerifyDataTable(row[0]))
  718. table_versions->AddTable(row[0], atoi(row[1]), GetDataVersion(row[0]));
  719. else
  720. table_versions->AddTable(row[0], atoi(row[1]), 0);
  721. }
  722. }
  723. bool LoginDatabase::VerifyDataTable(char* name){
  724. Query query;
  725. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT table_name from download_tables where table_name='%s'", name);
  726. if(result && mysql_num_rows(result) > 0)
  727. return true;
  728. return false;
  729. }
  730. string LoginDatabase::GetColumnNames(char* name){
  731. Query query;
  732. MYSQL_ROW row;
  733. string columns = "(";
  734. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "show columns from %s", name);
  735. if(result && mysql_num_rows(result) > 0){
  736. int16 i = 0;
  737. while((row = mysql_fetch_row(result))){
  738. if(strcmp(row[0], "table_data_version") != 0){
  739. if(i>0)
  740. columns.append(",");
  741. columns.append(row[0]);
  742. i++;
  743. }
  744. }
  745. }
  746. columns.append(") ");
  747. return columns;
  748. }
  749. TableDataQuery* LoginDatabase::GetTableDataQuery(int32 server_ip, char* name, int16 version){
  750. Query query;
  751. MYSQL_ROW row;
  752. query.escaped_name = getEscapeString(name);
  753. TableDataQuery* table_query = 0;
  754. MYSQL_RES* result = 0;
  755. string columns;
  756. if(VerifyDataTable(query.escaped_name)){
  757. result = query.RunQuery2(Q_SELECT, "SELECT * from %s where table_data_version > %i", query.escaped_name, version);
  758. columns = GetColumnNames(query.escaped_name);
  759. }
  760. if(result && mysql_num_rows(result) > 0){
  761. table_query = new TableDataQuery(query.escaped_name);
  762. table_query->num_queries = mysql_num_rows(result);
  763. table_query->columns_size = columns.length() + 1;
  764. table_query->columns = new char[table_query->columns_size + 1];
  765. table_query->version = GetDataVersion(query.escaped_name);
  766. strcpy(table_query->columns, (char*)columns.c_str());
  767. string query_data;
  768. MYSQL_FIELD* field;
  769. int* int_list = new int[mysql_num_fields(result)];
  770. int16 ndx = 0;
  771. while((field = mysql_fetch_field(result))){
  772. int_list[ndx] = IS_NUM(field->type);
  773. if(strcmp(field->name,"table_data_version") == 0)
  774. int_list[ndx] = 2;
  775. ndx++;
  776. }
  777. ndx = 0;
  778. while((row = mysql_fetch_row(result))){
  779. query_data = "";
  780. for(int i=0;i<mysql_num_fields(result);i++){
  781. if(int_list[i]<2){
  782. if(i>0)
  783. query_data.append(",");
  784. if(!int_list[i]){
  785. query_data.append("'").append(getEscapeString(row[i])).append("'");
  786. }
  787. else
  788. query_data.append(row[i]);
  789. }
  790. }
  791. TableData* new_query = new TableData;
  792. new_query->size = query_data.length() + 1;
  793. new_query->query = new char[query_data.length() + 1];
  794. strcpy(new_query->query, query_data.c_str());
  795. table_query->queries.push_back(new_query);
  796. ndx++;
  797. }
  798. safe_delete_array(int_list);
  799. }
  800. else{
  801. string query2 = string("The user tried to download the following table: ").append(query.escaped_name);
  802. SaveClientLog("Possible Hacking Attempt", (char*)query2.c_str(), "Hacking Data", server_ip);
  803. }
  804. return table_query;
  805. }
  806. TableQuery* LoginDatabase::GetLatestTableQuery(int32 server_ip, char* name, int16 version){
  807. Query query;
  808. MYSQL_ROW row;
  809. query.escaped_name = getEscapeString(name);
  810. TableQuery* table_query = 0;
  811. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT query, version from login_table_versions where name = '%s' and version>=%i order by version", query.escaped_name, version + 1);
  812. if(result && mysql_num_rows(result) > 0){
  813. int16 i = 0;
  814. table_query = new TableQuery;
  815. while((row = mysql_fetch_row(result))){
  816. char* rowdata = row[0];
  817. if(strstr(rowdata, ";")){
  818. char* token = strtok(rowdata,";");
  819. while(token){
  820. char* new_query = new char[strlen(token) + 1];
  821. strcpy(new_query, token);
  822. table_query->AddQuery(new_query);
  823. token = strtok(NULL, ";");
  824. }
  825. }
  826. else
  827. table_query->AddQuery(rowdata);
  828. table_query->latest_version = atoi(row[1]);
  829. }
  830. strcpy(table_query->tablename, name);
  831. table_query->your_version = version;
  832. }
  833. else{
  834. string query2 = string("The following was the DB Query: ").append(query.GetQuery());
  835. SaveClientLog("Possible Hacking Attempt", (char*)query2.c_str(), "Hacking Query", server_ip);
  836. }
  837. return table_query;
  838. }
  839. sint16 LoginDatabase::GetDataVersion(char* name){
  840. Query query;
  841. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(table_data_version) from %s", name);
  842. sint16 ret_version = 0;
  843. if(result && mysql_num_rows(result) > 0) {
  844. MYSQL_ROW row;
  845. row = mysql_fetch_row(result);
  846. if(row[0])
  847. ret_version = atoi(row[0]);
  848. }
  849. return ret_version;
  850. }
  851. void LoginDatabase::RemoveOldWorldServerStats(){
  852. Query query;
  853. query.RunQuery2(Q_DELETE, "delete from login_worldstats where (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(last_update)) > 86400");
  854. }
  855. void LoginDatabase::UpdateWorldServerStats( LWorld* world, sint32 status )
  856. {
  857. if( !world || world->GetAccountID() == 0 )
  858. return;
  859. Query query;
  860. query.RunQuery2(Q_INSERT, "insert into login_worldstats (world_id, world_status, current_players, current_zones, last_update) values(%lu, %i, %i, %i, NOW()) ON DUPLICATE KEY UPDATE current_players=%i,current_zones=%i,world_status=%i,last_update=NOW()",
  861. world->GetAccountID(), status, world->GetPlayerNum(), world->GetZoneNum(), world->GetPlayerNum(), world->GetZoneNum(), status);
  862. }
  863. bool LoginDatabase::ResetWorldServerStatsConnectedTime(LWorld* world){
  864. if(!world || world->GetAccountID() == 0)
  865. return false;
  866. Query query;
  867. string update_stats = string("update login_worldstats set connected_time=now() where world_id=%i and (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(last_update)) > 300");
  868. query.RunQuery2(Q_UPDATE, update_stats.c_str(),world->GetAccountID());
  869. return true;
  870. }
  871. void LoginDatabase::ResetWorldStats ( )
  872. {
  873. Query query;
  874. string update_stats = string("update login_worldstats set world_status=-4, current_players=0, current_zones=0");
  875. query.RunQuery2(update_stats.c_str(), Q_UPDATE);
  876. }
  877. void LoginDatabase::SaveBugReport(int32 world_id, char* category, char* subcategory, char* causes_crash, char* reproducible, char* summary, char* description, char* version, char* player, int32 account_id, char* spawn_name, int32 spawn_id, int32 zone_id){
  878. Query query;
  879. string bug_report = string("insert into bugs (world_id, category, subcategory, causes_crash, reproducible, summary, description, version, player, account_id, spawn_name, spawn_id, zone_id) values(%lu, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %lu, '%s', %lu, %lu)");
  880. query.RunQuery2(Q_INSERT, bug_report.c_str(), world_id, database.getSafeEscapeString(category).c_str(), database.getSafeEscapeString(subcategory).c_str(),
  881. database.getSafeEscapeString(causes_crash).c_str(), database.getSafeEscapeString(reproducible).c_str(), database.getSafeEscapeString(summary).c_str(),
  882. database.getSafeEscapeString(description).c_str(), database.getSafeEscapeString(version).c_str(), database.getSafeEscapeString(player).c_str(), account_id,
  883. database.getSafeEscapeString(spawn_name).c_str(), spawn_id, zone_id);
  884. FixBugReport();
  885. }
  886. void LoginDatabase::FixBugReport(){
  887. Query query;
  888. string bug_report = string("update bugs set description = REPLACE(description,SUBSTRING(description,INSTR(description,'%'), 3),char(CONV(SUBSTRING(description,INSTR(description,'%')+1, 2), 16, 10))), summary = REPLACE(summary,SUBSTRING(summary,INSTR(summary,'%'), 3),char(CONV(SUBSTRING(summary,INSTR(summary,'%')+1, 2), 16, 10)))");
  889. query.RunQuery2(bug_report.c_str(), Q_UPDATE);
  890. }
  891. void LoginDatabase::UpdateWorldIPAddress(int32 world_id, int32 address){
  892. struct in_addr in;
  893. in.s_addr = address;
  894. Query query;
  895. query.RunQuery2(Q_UPDATE, "update login_worldservers set ip_address='%s' where id=%lu", inet_ntoa(in), world_id);
  896. }
  897. void LoginDatabase::UpdateAccountIPAddress(int32 account_id, int32 address){
  898. struct in_addr in;
  899. in.s_addr = address;
  900. Query query;
  901. query.RunQuery2(Q_UPDATE, "update account set ip_address='%s' where id=%lu", inet_ntoa(in), account_id);
  902. }