10 #include "stg/user_ips.h"
11 #include "stg/user_conf.h"
12 #include "stg/user_stat.h"
13 #include "stg/blowfish.h"
14 #include "stg/plugin_creator.h"
15 #include "stg/logger.h"
16 #include "mysql_store.h"
18 #define adm_enc_passwd "cjeifY8m3"
24 const int pt_mega = 1024 * 1024;
25 const std::string badSyms = "'`";
26 const char repSym = '\"';
27 const int RepitTimes = 3;
30 int GetInt(const std::string & str, T * val, T defaultVal = T())
34 *val = static_cast<T>(strtoll(str.c_str(), &res, 10));
38 *val = defaultVal; //Error!
45 int GetDouble(const std::string & str, double * val, double defaultVal)
49 *val = strtod(str.c_str(), &res);
53 *val = defaultVal; //Error!
60 int GetTime(const std::string & str, time_t * val, time_t defaultVal)
64 *val = strtol(str.c_str(), &res, 10);
68 *val = defaultVal; //Error!
75 //-----------------------------------------------------------------------------
76 std::string ReplaceStr(std::string source, const std::string & symlist, const char chgsym)
78 std::string::size_type pos=0;
80 while( (pos = source.find_first_of(symlist,pos)) != std::string::npos)
81 source.replace(pos, 1,1, chgsym);
86 int GetULongLongInt(const std::string & str, uint64_t * val, uint64_t defaultVal)
90 *val = strtoull(str.c_str(), &res, 10);
94 *val = defaultVal; //Error!
101 PLUGIN_CREATOR<MYSQL_STORE> msc;
104 extern "C" STORE * GetStore();
105 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
110 return msc.GetPlugin();
112 //-----------------------------------------------------------------------------
113 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
122 //-----------------------------------------------------------------------------
123 int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<PARAM_VALUE> & moduleParams,
124 const std::string & name, std::string & result)
128 std::vector<PARAM_VALUE>::const_iterator pvi;
129 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
130 if (pvi == moduleParams.end())
132 errorStr = "Parameter \'" + name + "\' not found.";
136 result = pvi->value[0];
140 //-----------------------------------------------------------------------------
141 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
143 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
144 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
146 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
147 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
149 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
150 ParseParam(s.moduleParams, "dbname", dbName) < 0)
152 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
153 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
158 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
161 MYSQL_STORE::MYSQL_STORE()
163 version("mysql_store v.0.67"),
167 logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
170 //-----------------------------------------------------------------------------
171 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
175 if( (ret = mysql_query(sock,sQuery)) )
177 for(int i=0; i<RepitTimes; i++)
179 if( (ret = mysql_query(sock,sQuery)) )
180 ;//need to send error result
188 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
191 int MYSQL_STORE::ParseSettings()
193 int ret = storeSettings.ParseSettings(settings);
197 errorStr = storeSettings.GetStrError();
200 if(storeSettings.GetDBPassword().length() == 0)
202 errorStr = "Database password must be not empty. Please read Manual.";
206 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
207 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
210 errorStr = "Couldn't connect to mysql engine! With error:\n";
211 errorStr += mysql_error(&mysql);
217 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
219 std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
221 if(MysqlQuery(res.c_str(),sock))
223 errorStr = "Couldn't create database! With error:\n";
224 errorStr += mysql_error(sock);
230 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
232 errorStr = "Couldn't select database! With error:\n";
233 errorStr += mysql_error(sock);
237 ret = CheckAllTables(sock);
242 ret = CheckAllTables(sock);
246 logger("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
254 //-----------------------------------------------------------------------------
255 bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
259 if (!(result=mysql_list_tables(sock,str.c_str() )))
261 errorStr = "Couldn't get tables list With error:\n";
262 errorStr += mysql_error(sock);
267 my_ulonglong num_rows = mysql_num_rows(result);
270 mysql_free_result(result);
272 return num_rows == 1;
274 //-----------------------------------------------------------------------------
275 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
277 //info-------------------------------------------------------------------------
278 if(!IsTablePresent("info",sock))
280 sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
282 if(MysqlQuery(qbuf,sock))
284 errorStr = "Couldn't create info table With error:\n";
285 errorStr += mysql_error(sock);
290 sprintf(qbuf,"INSERT INTO info SET version=0");
292 if(MysqlQuery(qbuf,sock))
294 errorStr = "Couldn't write default version. With error:\n";
295 errorStr += mysql_error(sock);
303 std::vector<std::string> info;
304 if (GetAllParams(&info, "info", "version"))
311 GetInt(info.front(), &schemaVersion, 0);
314 //admins-----------------------------------------------------------------------
315 if(!IsTablePresent("admins",sock))
317 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
318 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
319 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
320 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
321 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
323 if(MysqlQuery(qbuf,sock))
325 errorStr = "Couldn't create admin table list With error:\n";
326 errorStr += mysql_error(sock);
331 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
332 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
333 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
335 if(MysqlQuery(qbuf,sock))
337 errorStr = "Couldn't create default admin. With error:\n";
338 errorStr += mysql_error(sock);
344 //tariffs-----------------------------------------------------------------------
345 std::string param, res;
346 if(!IsTablePresent("tariffs",sock))
348 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
350 for (int i = 0; i < DIR_NUM; i++)
352 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
355 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
358 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
361 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
364 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
367 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
370 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
373 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
377 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
378 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
379 "period VARCHAR(32) NOT NULL DEFAULT 'month')";
381 if(MysqlQuery(res.c_str(),sock))
383 errorStr = "Couldn't create tariffs table list With error:\n";
384 errorStr += mysql_error(sock);
389 res = "INSERT INTO tariffs SET name='tariff',";
391 for (int i = 0; i < DIR_NUM; i++)
393 strprintf(¶m, " NoDiscount%d=1,", i);
396 strprintf(¶m, " Threshold%d=0,", i);
399 strprintf(¶m, " Time%d='0:0-0:0',", i);
404 strprintf(¶m, " SinglePrice%d=0,", i);
410 strprintf(¶m, " PriceDayA%d=0.0,", i);
415 strprintf(¶m, " PriceDayB%d=0.0,", i);
421 strprintf(¶m, " PriceNightA%d=0.0,", i);
426 strprintf(¶m, " PriceNightB%d=0.0,", i);
431 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
432 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
433 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'";
435 if(MysqlQuery(res.c_str(),sock))
437 errorStr = "Couldn't create default tariff. With error:\n";
438 errorStr += mysql_error(sock);
443 sprintf(qbuf,"UPDATE info SET version=1");
445 if(MysqlQuery(qbuf,sock))
447 errorStr = "Couldn't write default version. With error:\n";
448 errorStr += mysql_error(sock);
455 //users-----------------------------------------------------------------------
456 if(!IsTablePresent("users",sock))
458 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
459 "Passive INT(3) DEFAULT 0,Down INT(3) DEFAULT 0,DisabledDetailStat INT(3) DEFAULT 0,AlwaysOnline INT(3) DEFAULT 0,Tariff VARCHAR(40) NOT NULL DEFAULT '',"\
460 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
461 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
462 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
464 for (int i = 0; i < USERDATA_NUM; i++)
466 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
470 param = " CreditExpire INT(11) DEFAULT 0,";
473 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
476 for (int i = 0; i < DIR_NUM; i++)
478 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
481 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
485 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
486 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
487 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
488 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
491 if(MysqlQuery(res.c_str(),sock))
493 errorStr = "Couldn't create users table list With error:\n";
494 errorStr += mysql_error(sock);
495 errorStr += "\n\n" + res;
500 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
501 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
502 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
503 "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
505 for (int i = 0; i < USERDATA_NUM; i++)
507 strprintf(¶m, " Userdata%d='',", i);
511 for (int i = 0; i < DIR_NUM; i++)
513 strprintf(¶m, " D%d=0,", i);
516 strprintf(¶m, " U%d=0,", i);
520 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
521 "LastCashAddTime=0, PassiveTime=0";
523 if(MysqlQuery(res.c_str(),sock))
525 errorStr = "Couldn't create default user. With error:\n";
526 errorStr += mysql_error(sock);
532 //logs-----------------------------------------------------------------------
533 if(!IsTablePresent("logs"))
535 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
539 errorStr = "Couldn't create admin table list With error:\n";
540 errorStr += mysql_error(sock);
545 //messages---------------------------------------------------------------------
546 if(!IsTablePresent("messages",sock))
548 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
549 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
550 "stgRepeat INT, repeatPeriod INT, text TEXT)");
552 if(MysqlQuery(qbuf,sock))
554 errorStr = "Couldn't create messages table. With error:\n";
555 errorStr += mysql_error(sock);
561 //month_stat-------------------------------------------------------------------
562 if(!IsTablePresent("stat",sock))
564 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
566 for (int i = 0; i < DIR_NUM; i++)
568 strprintf(¶m, " U%d BIGINT,", i);
571 strprintf(¶m, " D%d BIGINT,", i);
575 res += " cash DOUBLE, INDEX (login))";
577 if(MysqlQuery(res.c_str(),sock))
579 errorStr = "Couldn't create stat table. With error:\n";
580 errorStr += mysql_error(sock);
588 //-----------------------------------------------------------------------------
589 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
591 if (schemaVersion < 1)
593 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
595 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
596 errorStr += mysql_error(sock);
600 if (MysqlQuery("UPDATE info SET version = 1", sock))
602 errorStr = "Couldn't update DB schema version to 1. With error:\n";
603 errorStr += mysql_error(sock);
608 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
612 //-----------------------------------------------------------------------------
614 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList,
615 const std::string & table, const std::string & name) const
624 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
626 if(MysqlGetQuery(qbuf,sock))
628 errorStr = "Couldn't GetAllParams Query for: ";
629 errorStr += name + " - " + table + "\n";
630 errorStr += mysql_error(sock);
635 if (!(res=mysql_store_result(sock)))
637 errorStr = "Couldn't GetAllParams Results for: ";
638 errorStr += name + " - " + table + "\n";
639 errorStr += mysql_error(sock);
643 num = mysql_num_rows(res);
645 for(i = 0; i < num; i++)
647 row = mysql_fetch_row(res);
648 ParamList->push_back(row[0]);
651 mysql_free_result(res);
657 //-----------------------------------------------------------------------------
658 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
660 if(GetAllParams(usersList, "users", "login"))
665 //-----------------------------------------------------------------------------
666 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
668 if(GetAllParams(adminsList, "admins", "login"))
673 //-----------------------------------------------------------------------------
674 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
676 if(GetAllParams(tariffsList, "tariffs", "name"))
681 //-----------------------------------------------------------------------------
682 int MYSQL_STORE::AddUser(const std::string & login) const
684 std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
686 for (int i = 0; i < USERDATA_NUM; i++)
687 query += ",Userdata" + x2str(i) + "=''";
689 if(MysqlSetQuery(query.c_str()))
691 errorStr = "Couldn't add user:\n";
692 //errorStr += mysql_error(sock);
698 //-----------------------------------------------------------------------------
699 int MYSQL_STORE::DelUser(const std::string & login) const
701 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
703 if(MysqlSetQuery(qbuf))
705 errorStr = "Couldn't delete user:\n";
706 //errorStr += mysql_error(sock);
712 //-----------------------------------------------------------------------------
713 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
720 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
721 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
722 RealName, StgGroup, Credit, TariffChange, ";
724 for (int i = 0; i < USERDATA_NUM; i++)
726 sprintf(qbuf, "Userdata%d, ", i);
730 query += "CreditExpire, IP FROM users WHERE login='";
731 query += login + "' LIMIT 1";
733 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
735 if(MysqlGetQuery(query.c_str(),sock))
737 errorStr = "Couldn't restore Tariff(on query):\n";
738 errorStr += mysql_error(sock);
743 if (!(res=mysql_store_result(sock)))
745 errorStr = "Couldn't restore Tariff(on getting result):\n";
746 errorStr += mysql_error(sock);
751 if (mysql_num_rows(res) != 1)
753 errorStr = "User not found";
758 row = mysql_fetch_row(res);
760 conf->password = row[1];
762 if (conf->password.empty())
764 mysql_free_result(res);
765 errorStr = "User \'" + login + "\' password is blank.";
770 if (GetInt(row[2],&conf->passive) != 0)
772 mysql_free_result(res);
773 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
778 if (GetInt(row[3], &conf->disabled) != 0)
780 mysql_free_result(res);
781 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
786 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
788 mysql_free_result(res);
789 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
794 if (GetInt(row[5], &conf->alwaysOnline) != 0)
796 mysql_free_result(res);
797 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
802 conf->tariffName = row[6];
804 if (conf->tariffName.empty())
806 mysql_free_result(res);
807 errorStr = "User \'" + login + "\' tariff is blank.";
812 conf->address = row[7];
813 conf->phone = row[8];
814 conf->email = row[9];
815 conf->note = row[10];
816 conf->realName = row[11];
817 conf->group = row[12];
819 if (GetDouble(row[13], &conf->credit, 0) != 0)
821 mysql_free_result(res);
822 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
827 conf->nextTariff = row[14];
829 for (int i = 0; i < USERDATA_NUM; i++)
831 conf->userdata[i] = row[15+i];
834 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
836 std::string ipStr = row[16+USERDATA_NUM];
842 catch (const std::string & s)
844 mysql_free_result(res);
845 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
851 mysql_free_result(res);
856 //-----------------------------------------------------------------------------
857 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
867 for (int i = 0; i < DIR_NUM; i++)
869 sprintf(qbuf, "D%d, U%d, ", i, i);
873 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
874 FROM users WHERE login = '";
875 query += login + "'";
877 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
879 if(MysqlGetQuery(query.c_str() ,sock))
881 errorStr = "Couldn't restore UserStat(on query):\n";
882 errorStr += mysql_error(sock);
887 if (!(res=mysql_store_result(sock)))
889 errorStr = "Couldn't restore UserStat(on getting result):\n";
890 errorStr += mysql_error(sock);
895 row = mysql_fetch_row(res);
897 unsigned int startPos=0;
901 for (int i = 0; i < DIR_NUM; i++)
904 sprintf(s, "D%d", i);
905 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
907 mysql_free_result(res);
908 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
912 stat->monthDown[i] = traff;
914 sprintf(s, "U%d", i);
915 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
917 mysql_free_result(res);
918 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
922 stat->monthUp[i] = traff;
925 startPos += (2*DIR_NUM);
927 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
929 mysql_free_result(res);
930 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
935 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
937 mysql_free_result(res);
938 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
943 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
945 mysql_free_result(res);
946 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
951 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
953 mysql_free_result(res);
954 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
959 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
961 mysql_free_result(res);
962 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
967 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
969 mysql_free_result(res);
970 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
975 mysql_free_result(res);
979 //-----------------------------------------------------------------------------
980 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
985 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
986 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
987 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
988 conf.password.c_str(),
991 conf.disabledDetailStat,
993 conf.tariffName.c_str(),
994 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
995 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
996 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
997 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
998 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
999 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
1001 conf.nextTariff.c_str()
1004 for (int i = 0; i < USERDATA_NUM; i++)
1006 strprintf(¶m, " Userdata%d='%s',", i,
1007 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1011 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1014 std::ostringstream ipStr;
1017 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1020 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1023 if(MysqlSetQuery(res.c_str()))
1025 errorStr = "Couldn't save user conf:\n";
1026 //errorStr += mysql_error(sock);
1032 //-----------------------------------------------------------------------------
1033 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
1038 res = "UPDATE users SET";
1040 for (int i = 0; i < DIR_NUM; i++)
1042 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1045 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1049 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1050 " PassiveTime=%d, LastActivityTime=%d",
1054 stat.lastCashAddTime,
1056 stat.lastActivityTime
1060 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1063 if(MysqlSetQuery(res.c_str()))
1065 errorStr = "Couldn't save user stat:\n";
1066 // errorStr += mysql_error(sock);
1072 //-----------------------------------------------------------------------------
1073 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
1075 std::string res, tempStr;
1084 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1085 if (!(sock=MysqlConnect())){
1086 errorStr = "Couldn't connect to Server";
1089 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1091 errorStr = "Couldn't get table " + tempStr + ":\n";
1092 errorStr += mysql_error(sock);
1097 my_ulonglong num_rows = mysql_num_rows(result);
1099 mysql_free_result(result);
1103 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1104 lt->tm_mon+1, lt->tm_year+1900);
1106 if(MysqlQuery(qbuf,sock))
1108 errorStr = "Couldn't create WriteDetailedStat table:\n";
1109 errorStr += mysql_error(sock);
1115 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1119 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1120 lt->tm_mon+1, lt->tm_year+1900,
1121 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1123 if(MysqlQuery(send.c_str(),sock))
1125 errorStr = "Couldn't write log string:\n";
1126 errorStr += mysql_error(sock);
1134 //-----------------------------------------------------------------------------
1135 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
1136 const std::string & admLogin,
1138 const std::string & paramName,
1139 const std::string & oldValue,
1140 const std::string & newValue,
1141 const std::string & message) const
1143 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1144 + paramName + "\' parameter changed from \'" + oldValue +
1145 "\' to \'" + newValue + "\'. " + message;
1147 return WriteLogString(userLogMsg, login);
1149 //-----------------------------------------------------------------------------
1150 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1152 std::string logStr = "Connect, " + inet_ntostring(ip);
1153 return WriteLogString(logStr, login);
1155 //-----------------------------------------------------------------------------
1156 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
1157 const DIR_TRAFF & up,
1158 const DIR_TRAFF & down,
1159 const DIR_TRAFF & sessionUp,
1160 const DIR_TRAFF & sessionDown,
1163 const std::string & /*reason*/) const
1165 std::string logStr = "Disconnect, ";
1166 std::ostringstream sssu;
1167 std::ostringstream sssd;
1168 std::ostringstream ssmu;
1169 std::ostringstream ssmd;
1170 std::ostringstream sscash;
1176 sssd << sessionDown;
1180 logStr += " session upload: \'";
1181 logStr += sssu.str();
1182 logStr += "\' session download: \'";
1183 logStr += sssd.str();
1184 logStr += "\' month upload: \'";
1185 logStr += ssmu.str();
1186 logStr += "\' month download: \'";
1187 logStr += ssmd.str();
1188 logStr += "\' cash: \'";
1189 logStr += sscash.str();
1192 return WriteLogString(logStr, login);
1194 //-----------------------------------------------------------------------------
1195 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1196 const std::string & login) const
1198 std::string param, res;
1200 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1201 login.c_str(), month+1, year+1900);
1203 for (int i = 0; i < DIR_NUM; i++)
1205 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1208 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1212 strprintf(¶m, " cash=%f", stat.cash);
1215 if(MysqlSetQuery(res.c_str()))
1217 errorStr = "Couldn't SaveMonthStat:\n";
1218 //errorStr += mysql_error(sock);
1224 //-----------------------------------------------------------------------------*/
1225 int MYSQL_STORE::AddAdmin(const std::string & login) const
1227 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1229 if(MysqlSetQuery(qbuf))
1231 errorStr = "Couldn't add admin:\n";
1232 //errorStr += mysql_error(sock);
1238 //-----------------------------------------------------------------------------*/
1239 int MYSQL_STORE::DelAdmin(const std::string & login) const
1241 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1243 if(MysqlSetQuery(qbuf))
1245 errorStr = "Couldn't delete admin:\n";
1246 //errorStr += mysql_error(sock);
1252 //-----------------------------------------------------------------------------*/
1253 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1255 char passwordE[2 * ADM_PASSWD_LEN + 2];
1256 char pass[ADM_PASSWD_LEN + 1];
1257 char adminPass[ADM_PASSWD_LEN + 1];
1259 memset(pass, 0, sizeof(pass));
1260 memset(adminPass, 0, sizeof(adminPass));
1263 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1265 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1266 adminPass[ADM_PASSWD_LEN - 1] = 0;
1268 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1270 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1273 pass[ADM_PASSWD_LEN - 1] = 0;
1274 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1276 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1277 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1278 "WHERE login='%s' LIMIT 1",
1290 if(MysqlSetQuery(qbuf))
1292 errorStr = "Couldn't save admin:\n";
1293 //errorStr += mysql_error(sock);
1299 //-----------------------------------------------------------------------------
1300 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1302 char pass[ADM_PASSWD_LEN + 1];
1303 char password[ADM_PASSWD_LEN + 1];
1304 char passwordE[2*ADM_PASSWD_LEN + 2];
1307 memset(password, 0, sizeof(password));
1313 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1315 if(MysqlGetQuery(qbuf,sock))
1317 errorStr = "Couldn't restore admin:\n";
1318 errorStr += mysql_error(sock);
1323 if (!(res=mysql_store_result(sock)))
1325 errorStr = "Couldn't restore admin:\n";
1326 errorStr += mysql_error(sock);
1331 if ( mysql_num_rows(res) == 0)
1333 mysql_free_result(res);
1334 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1339 row = mysql_fetch_row(res);
1345 mysql_free_result(res);
1346 errorStr = "Error in parameter password";
1351 memset(passwordE, 0, sizeof(passwordE));
1352 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1354 memset(pass, 0, sizeof(pass));
1356 if (passwordE[0] != 0)
1358 Decode21(pass, passwordE);
1359 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1361 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1363 DecodeString(password + 8*i, pass + 8*i, &ctx);
1371 ac->password = password;
1375 if (GetInt(row[2], &a) == 0)
1376 ac->priv.userConf = a;
1379 mysql_free_result(res);
1380 errorStr = "Error in parameter ChgConf";
1385 if (GetInt(row[3], &a) == 0)
1386 ac->priv.userPasswd = a;
1389 mysql_free_result(res);
1390 errorStr = "Error in parameter ChgPassword";
1395 if (GetInt(row[4], &a) == 0)
1396 ac->priv.userStat = a;
1399 mysql_free_result(res);
1400 errorStr = "Error in parameter ChgStat";
1405 if (GetInt(row[5], &a) == 0)
1406 ac->priv.userCash = a;
1409 mysql_free_result(res);
1410 errorStr = "Error in parameter ChgCash";
1415 if (GetInt(row[6], &a) == 0)
1416 ac->priv.userAddDel = a;
1419 mysql_free_result(res);
1420 errorStr = "Error in parameter UsrAddDel";
1425 if (GetInt(row[7], &a) == 0)
1426 ac->priv.tariffChg = a;
1429 mysql_free_result(res);
1430 errorStr = "Error in parameter ChgTariff";
1435 if (GetInt(row[8], &a) == 0)
1436 ac->priv.adminChg = a;
1439 mysql_free_result(res);
1440 errorStr = "Error in parameter ChgAdmin";
1445 mysql_free_result(res);
1449 //-----------------------------------------------------------------------------
1450 int MYSQL_STORE::AddTariff(const std::string & name) const
1452 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1454 if(MysqlSetQuery(qbuf))
1456 errorStr = "Couldn't add tariff:\n";
1457 // errorStr += mysql_error(sock);
1463 //-----------------------------------------------------------------------------
1464 int MYSQL_STORE::DelTariff(const std::string & name) const
1466 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1468 if(MysqlSetQuery(qbuf))
1470 errorStr = "Couldn't delete tariff: ";
1471 // errorStr += mysql_error(sock);
1477 //-----------------------------------------------------------------------------
1478 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1483 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1485 if(MysqlGetQuery(qbuf,sock))
1487 errorStr = "Couldn't restore Tariff:\n";
1488 errorStr += mysql_error(sock);
1493 if (!(res=mysql_store_result(sock)))
1495 errorStr = "Couldn't restore Tariff:\n";
1496 errorStr += mysql_error(sock);
1502 td->tariffConf.name = tariffName;
1504 row = mysql_fetch_row(res);
1507 for (int i = 0; i<DIR_NUM; i++)
1509 strprintf(¶m, "Time%d", i);
1511 if (str.length() == 0)
1513 mysql_free_result(res);
1514 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1519 ParseTariffTimeStr(str.c_str(),
1520 td->dirPrice[i].hDay,
1521 td->dirPrice[i].mDay,
1522 td->dirPrice[i].hNight,
1523 td->dirPrice[i].mNight);
1525 strprintf(¶m, "PriceDayA%d", i);
1526 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1528 mysql_free_result(res);
1529 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1533 td->dirPrice[i].priceDayA /= (1024*1024);
1535 strprintf(¶m, "PriceDayB%d", i);
1536 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1538 mysql_free_result(res);
1539 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1543 td->dirPrice[i].priceDayB /= (1024*1024);
1545 strprintf(¶m, "PriceNightA%d", i);
1546 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1548 mysql_free_result(res);
1549 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1553 td->dirPrice[i].priceNightA /= (1024*1024);
1555 strprintf(¶m, "PriceNightB%d", i);
1556 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1558 mysql_free_result(res);
1559 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1563 td->dirPrice[i].priceNightB /= (1024*1024);
1565 strprintf(¶m, "Threshold%d", i);
1566 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
1568 mysql_free_result(res);
1569 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1574 strprintf(¶m, "SinglePrice%d", i);
1575 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
1577 mysql_free_result(res);
1578 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1583 strprintf(¶m, "NoDiscount%d", i);
1584 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
1586 mysql_free_result(res);
1587 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1593 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1595 mysql_free_result(res);
1596 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1601 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1603 mysql_free_result(res);
1604 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1609 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1611 mysql_free_result(res);
1612 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1617 str = row[4+8*DIR_NUM];
1618 param = "TraffType";
1620 if (str.length() == 0)
1622 mysql_free_result(res);
1623 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1628 if (!strcasecmp(str.c_str(), "up"))
1629 td->tariffConf.traffType = TRAFF_UP;
1631 if (!strcasecmp(str.c_str(), "down"))
1632 td->tariffConf.traffType = TRAFF_DOWN;
1634 if (!strcasecmp(str.c_str(), "up+down"))
1635 td->tariffConf.traffType = TRAFF_UP_DOWN;
1637 if (!strcasecmp(str.c_str(), "max"))
1638 td->tariffConf.traffType = TRAFF_MAX;
1641 mysql_free_result(res);
1642 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1647 if (schemaVersion > 0)
1649 str = row[5+8*DIR_NUM];
1652 if (str.length() == 0)
1654 mysql_free_result(res);
1655 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1660 td->tariffConf.period = TARIFF::StringToPeriod(str);
1664 td->tariffConf.period = TARIFF::MONTH;
1667 mysql_free_result(res);
1671 //-----------------------------------------------------------------------------
1672 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1676 std::string res="UPDATE tariffs SET";
1678 for (int i = 0; i < DIR_NUM; i++)
1680 strprintf(¶m, " PriceDayA%d=%f,", i,
1681 td.dirPrice[i].priceDayA * pt_mega);
1684 strprintf(¶m, " PriceDayB%d=%f,", i,
1685 td.dirPrice[i].priceDayB * pt_mega);
1688 strprintf(¶m, " PriceNightA%d=%f,", i,
1689 td.dirPrice[i].priceNightA * pt_mega);
1692 strprintf(¶m, " PriceNightB%d=%f,", i,
1693 td.dirPrice[i].priceNightB * pt_mega);
1696 strprintf(¶m, " Threshold%d=%d,", i,
1697 td.dirPrice[i].threshold);
1701 strprintf(¶m, " Time%d", i);
1703 strprintf(&s, "%0d:%0d-%0d:%0d",
1704 td.dirPrice[i].hDay,
1705 td.dirPrice[i].mDay,
1706 td.dirPrice[i].hNight,
1707 td.dirPrice[i].mNight);
1709 res += (param + "='" + s + "',");
1711 strprintf(¶m, " NoDiscount%d=%d,", i,
1712 td.dirPrice[i].noDiscount);
1715 strprintf(¶m, " SinglePrice%d=%d,", i,
1716 td.dirPrice[i].singlePrice);
1720 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1723 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1726 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1729 switch (td.tariffConf.traffType)
1732 res += " TraffType='up'";
1735 res += " TraffType='down'";
1738 res += " TraffType='up+down'";
1741 res += " TraffType='max'";
1745 if (schemaVersion > 0)
1746 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1748 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1751 if(MysqlSetQuery(res.c_str()))
1753 errorStr = "Couldn't save tariff:\n";
1754 //errorStr += mysql_error(sock);
1760 //-----------------------------------------------------------------------------
1761 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1763 const std::string & login) const
1765 std::string res, stTime, endTime, tempStr;
1772 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1780 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1782 if (!(sock=MysqlConnect())){
1787 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1789 errorStr = "Couldn't get table " + tempStr + ":\n";
1790 errorStr += mysql_error(sock);
1795 my_ulonglong num_rows = mysql_num_rows(result);
1797 mysql_free_result(result);
1801 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1802 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1803 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1804 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1805 lt->tm_mon+1, lt->tm_year+1900);
1807 if(MysqlQuery(qbuf,sock))
1809 errorStr = "Couldn't create WriteDetailedStat table:\n";
1810 errorStr += mysql_error(sock);
1819 lt1 = localtime(&lastStat);
1828 lt2 = localtime(&t);
1834 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1835 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1837 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1838 "day=%d,startTime='%s',endTime='%s',",
1839 lt->tm_mon+1, lt->tm_year+1900,
1846 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1847 stIter = statTree.begin();
1849 while (stIter != statTree.end())
1851 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1852 inet_ntostring(stIter->first.ip).c_str(),
1854 stIter->second.down,
1859 if( MysqlQuery((res+tempStr).c_str(),sock) )
1861 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1862 errorStr += mysql_error(sock);
1867 result=mysql_store_result(sock);
1869 mysql_free_result(result);
1876 //-----------------------------------------------------------------------------
1877 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1881 gettimeofday(&tv, NULL);
1883 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1885 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1887 static_cast<long long>(msg->header.id)
1890 if(MysqlSetQuery(qbuf))
1892 errorStr = "Couldn't add message:\n";
1893 //errorStr += mysql_error(sock);
1897 return EditMessage(*msg, login);
1899 //-----------------------------------------------------------------------------
1900 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1904 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1905 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1906 "WHERE login='%s' AND id=%lld LIMIT 1",
1908 msg.header.lastSendTime,
1909 msg.header.creationTime,
1910 msg.header.showTime,
1912 msg.header.repeatPeriod,
1913 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1918 if(MysqlSetQuery(res.c_str()))
1920 errorStr = "Couldn't edit message:\n";
1921 //errorStr += mysql_error(sock);
1927 //-----------------------------------------------------------------------------
1928 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1934 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1935 login.c_str(), static_cast<unsigned long long>(id));
1937 if(MysqlGetQuery(qbuf,sock))
1939 errorStr = "Couldn't GetMessage:\n";
1940 errorStr += mysql_error(sock);
1945 if (!(res=mysql_store_result(sock)))
1947 errorStr = "Couldn't GetMessage:\n";
1948 errorStr += mysql_error(sock);
1953 row = mysql_fetch_row(res);
1955 if(row[2]&&str2x(row[2], msg->header.type))
1957 mysql_free_result(res);
1958 errorStr = "Invalid value in message header for user: " + login;
1963 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1965 mysql_free_result(res);
1966 errorStr = "Invalid value in message header for user: " + login;
1971 if(row[4] && str2x(row[4], msg->header.creationTime))
1973 mysql_free_result(res);
1974 errorStr = "Invalid value in message header for user: " + login;
1979 if(row[5] && str2x(row[5], msg->header.showTime))
1981 mysql_free_result(res);
1982 errorStr = "Invalid value in message header for user: " + login;
1987 if(row[6] && str2x(row[6], msg->header.repeat))
1989 mysql_free_result(res);
1990 errorStr = "Invalid value in message header for user: " + login;
1995 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1997 mysql_free_result(res);
1998 errorStr = "Invalid value in message header for user: " + login;
2003 msg->header.id = id;
2006 mysql_free_result(res);
2010 //-----------------------------------------------------------------------------
2011 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
2013 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
2014 login.c_str(), static_cast<long long>(id));
2016 if(MysqlSetQuery(qbuf))
2018 errorStr = "Couldn't delete Message:\n";
2019 //errorStr += mysql_error(sock);
2025 //-----------------------------------------------------------------------------
2026 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
2031 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
2033 if(MysqlGetQuery(qbuf,sock))
2035 errorStr = "Couldn't GetMessageHdrs:\n";
2036 errorStr += mysql_error(sock);
2041 if (!(res=mysql_store_result(sock)))
2043 errorStr = "Couldn't GetMessageHdrs:\n";
2044 errorStr += mysql_error(sock);
2050 my_ulonglong num_rows = mysql_num_rows(res);
2053 for (i = 0; i < num_rows; i++)
2055 row = mysql_fetch_row(res);
2056 if (str2x(row[1], id))
2061 if(str2x(row[2], hdr.type))
2065 if(str2x(row[3], hdr.lastSendTime))
2069 if(str2x(row[4], hdr.creationTime))
2073 if(str2x(row[5], hdr.showTime))
2077 if(str2x(row[6], hdr.repeat))
2081 if(str2x(row[7], hdr.repeatPeriod))
2085 hdrsList->push_back(hdr);
2088 mysql_free_result(res);
2092 //-----------------------------------------------------------------------------
2094 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2097 int ret=MysqlGetQuery(Query,sock);
2101 //-----------------------------------------------------------------------------
2102 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2103 if (!(sock=MysqlConnect())) {
2106 return MysqlQuery(Query,sock);
2108 //-----------------------------------------------------------------------------
2109 MYSQL * MYSQL_STORE::MysqlConnect() const {
2111 if ( !(sock=mysql_init(NULL)) ){
2112 errorStr= "mysql init susck\n";
2115 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2116 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2119 errorStr = "Couldn't connect to mysql engine! With error:\n";
2120 errorStr += mysql_error(sock);
2124 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2125 errorStr = "Database lost !\n";
2131 //-----------------------------------------------------------------------------