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()
117 //-----------------------------------------------------------------------------
118 int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<PARAM_VALUE> & moduleParams,
119 const std::string & name, std::string & result)
123 std::vector<PARAM_VALUE>::const_iterator pvi;
124 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
125 if (pvi == moduleParams.end() || pvi->value.empty())
127 errorStr = "Parameter \'" + name + "\' not found.";
131 result = pvi->value[0];
135 //-----------------------------------------------------------------------------
136 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
138 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
139 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
141 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
142 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
144 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
145 ParseParam(s.moduleParams, "dbname", dbName) < 0)
147 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
148 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
153 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 MYSQL_STORE::MYSQL_STORE()
157 : version("mysql_store v.0.67"),
159 logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
162 //-----------------------------------------------------------------------------
163 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
167 if( (ret = mysql_query(sock,sQuery)) )
169 for(int i=0; i<RepitTimes; i++)
171 if( (ret = mysql_query(sock,sQuery)) )
172 ;//need to send error result
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
183 int MYSQL_STORE::ParseSettings()
185 int ret = storeSettings.ParseSettings(settings);
189 errorStr = storeSettings.GetStrError();
192 if(storeSettings.GetDBPassword().length() == 0)
194 errorStr = "Database password must be not empty. Please read Manual.";
198 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
199 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
202 errorStr = "Couldn't connect to mysql engine! With error:\n";
203 errorStr += mysql_error(&mysql);
209 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
211 std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
213 if(MysqlQuery(res.c_str(),sock))
215 errorStr = "Couldn't create database! With error:\n";
216 errorStr += mysql_error(sock);
222 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
224 errorStr = "Couldn't select database! With error:\n";
225 errorStr += mysql_error(sock);
229 ret = CheckAllTables(sock);
234 ret = CheckAllTables(sock);
238 logger("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
246 //-----------------------------------------------------------------------------
247 bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
251 if (!(result=mysql_list_tables(sock,str.c_str() )))
253 errorStr = "Couldn't get tables list With error:\n";
254 errorStr += mysql_error(sock);
259 my_ulonglong num_rows = mysql_num_rows(result);
262 mysql_free_result(result);
264 return num_rows == 1;
266 //-----------------------------------------------------------------------------
267 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
269 //info-------------------------------------------------------------------------
270 if(!IsTablePresent("info",sock))
272 sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
274 if(MysqlQuery(qbuf,sock))
276 errorStr = "Couldn't create info table With error:\n";
277 errorStr += mysql_error(sock);
282 sprintf(qbuf,"INSERT INTO info SET version=0");
284 if(MysqlQuery(qbuf,sock))
286 errorStr = "Couldn't write default version. With error:\n";
287 errorStr += mysql_error(sock);
295 std::vector<std::string> info;
296 if (GetAllParams(&info, "info", "version"))
303 GetInt(info.front(), &schemaVersion, 0);
306 //admins-----------------------------------------------------------------------
307 if(!IsTablePresent("admins",sock))
309 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
310 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
311 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
312 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
313 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
315 if(MysqlQuery(qbuf,sock))
317 errorStr = "Couldn't create admin table list With error:\n";
318 errorStr += mysql_error(sock);
323 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
324 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
325 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
327 if(MysqlQuery(qbuf,sock))
329 errorStr = "Couldn't create default admin. With error:\n";
330 errorStr += mysql_error(sock);
336 //tariffs-----------------------------------------------------------------------
337 std::string param, res;
338 if(!IsTablePresent("tariffs",sock))
340 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
342 for (int i = 0; i < DIR_NUM; i++)
344 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
347 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
350 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
353 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
356 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
359 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
362 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
365 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
369 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
370 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
371 "period VARCHAR(32) NOT NULL DEFAULT 'month',"
372 "change_policy VARCHAR(32) NOT NULL DEFAULT 'allow')";
374 if(MysqlQuery(res.c_str(),sock))
376 errorStr = "Couldn't create tariffs table list With error:\n";
377 errorStr += mysql_error(sock);
382 res = "INSERT INTO tariffs SET name='tariff',";
384 for (int i = 0; i < DIR_NUM; i++)
386 strprintf(¶m, " NoDiscount%d=1,", i);
389 strprintf(¶m, " Threshold%d=0,", i);
392 strprintf(¶m, " Time%d='0:0-0:0',", i);
397 strprintf(¶m, " SinglePrice%d=0,", i);
403 strprintf(¶m, " PriceDayA%d=0.0,", i);
408 strprintf(¶m, " PriceDayB%d=0.0,", i);
414 strprintf(¶m, " PriceNightA%d=0.0,", i);
419 strprintf(¶m, " PriceNightB%d=0.0,", i);
424 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
425 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
426 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month',"\
427 "change_policy='allow'";
429 if(MysqlQuery(res.c_str(),sock))
431 errorStr = "Couldn't create default tariff. With error:\n";
432 errorStr += mysql_error(sock);
437 sprintf(qbuf,"UPDATE info SET version=1");
439 if(MysqlQuery(qbuf,sock))
441 errorStr = "Couldn't write default version. With error:\n";
442 errorStr += mysql_error(sock);
449 //users-----------------------------------------------------------------------
450 if(!IsTablePresent("users",sock))
452 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
453 "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 '',"\
454 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
455 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
456 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
458 for (int i = 0; i < USERDATA_NUM; i++)
460 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
464 param = " CreditExpire INT(11) DEFAULT 0,";
467 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
470 for (int i = 0; i < DIR_NUM; i++)
472 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
475 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
479 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
480 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
481 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
482 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
485 if(MysqlQuery(res.c_str(),sock))
487 errorStr = "Couldn't create users table list With error:\n";
488 errorStr += mysql_error(sock);
489 errorStr += "\n\n" + res;
494 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
495 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
496 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
497 "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
499 for (int i = 0; i < USERDATA_NUM; i++)
501 strprintf(¶m, " Userdata%d='',", i);
505 for (int i = 0; i < DIR_NUM; i++)
507 strprintf(¶m, " D%d=0,", i);
510 strprintf(¶m, " U%d=0,", i);
514 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
515 "LastCashAddTime=0, PassiveTime=0";
517 if(MysqlQuery(res.c_str(),sock))
519 errorStr = "Couldn't create default user. With error:\n";
520 errorStr += mysql_error(sock);
526 //logs-----------------------------------------------------------------------
527 if(!IsTablePresent("logs"))
529 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
533 errorStr = "Couldn't create admin table list With error:\n";
534 errorStr += mysql_error(sock);
539 //messages---------------------------------------------------------------------
540 if(!IsTablePresent("messages",sock))
542 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
543 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
544 "stgRepeat INT, repeatPeriod INT, text TEXT)");
546 if(MysqlQuery(qbuf,sock))
548 errorStr = "Couldn't create messages table. With error:\n";
549 errorStr += mysql_error(sock);
555 //month_stat-------------------------------------------------------------------
556 if(!IsTablePresent("stat",sock))
558 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
560 for (int i = 0; i < DIR_NUM; i++)
562 strprintf(¶m, " U%d BIGINT,", i);
565 strprintf(¶m, " D%d BIGINT,", i);
569 res += " cash DOUBLE, INDEX (login))";
571 if(MysqlQuery(res.c_str(),sock))
573 errorStr = "Couldn't create stat table. With error:\n";
574 errorStr += mysql_error(sock);
582 //-----------------------------------------------------------------------------
583 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
585 if (schemaVersion < 1)
587 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
589 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
590 errorStr += mysql_error(sock);
594 if (MysqlQuery("UPDATE info SET version = 1", sock))
596 errorStr = "Couldn't update DB schema version to 1. With error:\n";
597 errorStr += mysql_error(sock);
602 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
605 if (schemaVersion < 2)
607 if (MysqlQuery("ALTER TABLE tariffs ADD change_policy VARCHAR(32) NOT NULL DEFAULT 'allow'", sock))
609 errorStr = "Couldn't update tariffs table to version 2. With error:\n";
610 errorStr += mysql_error(sock);
614 if (MysqlQuery("UPDATE info SET version = 2", sock))
616 errorStr = "Couldn't update DB schema version to 2. With error:\n";
617 errorStr += mysql_error(sock);
622 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
626 //-----------------------------------------------------------------------------
628 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList,
629 const std::string & table, const std::string & name) const
638 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
640 if(MysqlGetQuery(qbuf,sock))
642 errorStr = "Couldn't GetAllParams Query for: ";
643 errorStr += name + " - " + table + "\n";
644 errorStr += mysql_error(sock);
649 if (!(res=mysql_store_result(sock)))
651 errorStr = "Couldn't GetAllParams Results for: ";
652 errorStr += name + " - " + table + "\n";
653 errorStr += mysql_error(sock);
657 num = mysql_num_rows(res);
659 for(i = 0; i < num; i++)
661 row = mysql_fetch_row(res);
662 ParamList->push_back(row[0]);
665 mysql_free_result(res);
671 //-----------------------------------------------------------------------------
672 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
674 if(GetAllParams(usersList, "users", "login"))
679 //-----------------------------------------------------------------------------
680 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
682 if(GetAllParams(adminsList, "admins", "login"))
687 //-----------------------------------------------------------------------------
688 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
690 if(GetAllParams(tariffsList, "tariffs", "name"))
695 //-----------------------------------------------------------------------------
696 int MYSQL_STORE::AddUser(const std::string & login) const
698 std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
700 for (int i = 0; i < USERDATA_NUM; i++)
701 query += ",Userdata" + x2str(i) + "=''";
703 if(MysqlSetQuery(query.c_str()))
705 errorStr = "Couldn't add user:\n";
706 //errorStr += mysql_error(sock);
712 //-----------------------------------------------------------------------------
713 int MYSQL_STORE::DelUser(const std::string & login) const
715 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
717 if(MysqlSetQuery(qbuf))
719 errorStr = "Couldn't delete user:\n";
720 //errorStr += mysql_error(sock);
726 //-----------------------------------------------------------------------------
727 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
734 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
735 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
736 RealName, StgGroup, Credit, TariffChange, ";
738 for (int i = 0; i < USERDATA_NUM; i++)
740 sprintf(qbuf, "Userdata%d, ", i);
744 query += "CreditExpire, IP FROM users WHERE login='";
745 query += login + "' LIMIT 1";
747 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
749 if(MysqlGetQuery(query.c_str(),sock))
751 errorStr = "Couldn't restore Tariff(on query):\n";
752 errorStr += mysql_error(sock);
757 if (!(res=mysql_store_result(sock)))
759 errorStr = "Couldn't restore Tariff(on getting result):\n";
760 errorStr += mysql_error(sock);
765 if (mysql_num_rows(res) != 1)
767 errorStr = "User not found";
772 row = mysql_fetch_row(res);
774 conf->password = row[1];
776 if (conf->password.empty())
778 mysql_free_result(res);
779 errorStr = "User \'" + login + "\' password is blank.";
784 if (GetInt(row[2],&conf->passive) != 0)
786 mysql_free_result(res);
787 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
792 if (GetInt(row[3], &conf->disabled) != 0)
794 mysql_free_result(res);
795 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
800 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
802 mysql_free_result(res);
803 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
808 if (GetInt(row[5], &conf->alwaysOnline) != 0)
810 mysql_free_result(res);
811 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
816 conf->tariffName = row[6];
818 if (conf->tariffName.empty())
820 mysql_free_result(res);
821 errorStr = "User \'" + login + "\' tariff is blank.";
826 conf->address = row[7];
827 conf->phone = row[8];
828 conf->email = row[9];
829 conf->note = row[10];
830 conf->realName = row[11];
831 conf->group = row[12];
833 if (GetDouble(row[13], &conf->credit, 0) != 0)
835 mysql_free_result(res);
836 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
841 conf->nextTariff = row[14];
843 for (int i = 0; i < USERDATA_NUM; i++)
845 conf->userdata[i] = row[15+i];
848 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
850 std::string ipStr = row[16+USERDATA_NUM];
856 catch (const std::string & s)
858 mysql_free_result(res);
859 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
865 mysql_free_result(res);
870 //-----------------------------------------------------------------------------
871 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
881 for (int i = 0; i < DIR_NUM; i++)
883 sprintf(qbuf, "D%d, U%d, ", i, i);
887 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
888 FROM users WHERE login = '";
889 query += login + "'";
891 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
893 if(MysqlGetQuery(query.c_str() ,sock))
895 errorStr = "Couldn't restore UserStat(on query):\n";
896 errorStr += mysql_error(sock);
901 if (!(res=mysql_store_result(sock)))
903 errorStr = "Couldn't restore UserStat(on getting result):\n";
904 errorStr += mysql_error(sock);
909 row = mysql_fetch_row(res);
911 unsigned int startPos=0;
915 for (int i = 0; i < DIR_NUM; i++)
918 sprintf(s, "D%d", i);
919 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
921 mysql_free_result(res);
922 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
926 stat->monthDown[i] = traff;
928 sprintf(s, "U%d", i);
929 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
931 mysql_free_result(res);
932 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
936 stat->monthUp[i] = traff;
939 startPos += (2*DIR_NUM);
941 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
943 mysql_free_result(res);
944 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
949 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
951 mysql_free_result(res);
952 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
957 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
959 mysql_free_result(res);
960 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
965 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
967 mysql_free_result(res);
968 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
973 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
975 mysql_free_result(res);
976 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
981 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
983 mysql_free_result(res);
984 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
989 mysql_free_result(res);
993 //-----------------------------------------------------------------------------
994 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
999 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
1000 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
1001 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
1002 conf.password.c_str(),
1005 conf.disabledDetailStat,
1007 conf.tariffName.c_str(),
1008 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
1009 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
1010 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
1011 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
1012 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
1013 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
1015 conf.nextTariff.c_str()
1018 for (int i = 0; i < USERDATA_NUM; i++)
1020 strprintf(¶m, " Userdata%d='%s',", i,
1021 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1025 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1028 std::ostringstream ipStr;
1031 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1034 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1037 if(MysqlSetQuery(res.c_str()))
1039 errorStr = "Couldn't save user conf:\n";
1040 //errorStr += mysql_error(sock);
1046 //-----------------------------------------------------------------------------
1047 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
1052 res = "UPDATE users SET";
1054 for (int i = 0; i < DIR_NUM; i++)
1056 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1059 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1063 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1064 " PassiveTime=%d, LastActivityTime=%d",
1068 stat.lastCashAddTime,
1070 stat.lastActivityTime
1074 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1077 if(MysqlSetQuery(res.c_str()))
1079 errorStr = "Couldn't save user stat:\n";
1080 // errorStr += mysql_error(sock);
1086 //-----------------------------------------------------------------------------
1087 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
1089 std::string res, tempStr;
1098 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1099 if (!(sock=MysqlConnect())){
1100 errorStr = "Couldn't connect to Server";
1103 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1105 errorStr = "Couldn't get table " + tempStr + ":\n";
1106 errorStr += mysql_error(sock);
1111 my_ulonglong num_rows = mysql_num_rows(result);
1113 mysql_free_result(result);
1117 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1118 lt->tm_mon+1, lt->tm_year+1900);
1120 if(MysqlQuery(qbuf,sock))
1122 errorStr = "Couldn't create WriteDetailedStat table:\n";
1123 errorStr += mysql_error(sock);
1129 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1133 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1134 lt->tm_mon+1, lt->tm_year+1900,
1135 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1137 if(MysqlQuery(send.c_str(),sock))
1139 errorStr = "Couldn't write log string:\n";
1140 errorStr += mysql_error(sock);
1148 //-----------------------------------------------------------------------------
1149 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
1150 const std::string & admLogin,
1152 const std::string & paramName,
1153 const std::string & oldValue,
1154 const std::string & newValue,
1155 const std::string & message) const
1157 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1158 + paramName + "\' parameter changed from \'" + oldValue +
1159 "\' to \'" + newValue + "\'. " + message;
1161 return WriteLogString(userLogMsg, login);
1163 //-----------------------------------------------------------------------------
1164 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1166 std::string logStr = "Connect, " + inet_ntostring(ip);
1167 return WriteLogString(logStr, login);
1169 //-----------------------------------------------------------------------------
1170 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
1171 const DIR_TRAFF & up,
1172 const DIR_TRAFF & down,
1173 const DIR_TRAFF & sessionUp,
1174 const DIR_TRAFF & sessionDown,
1177 const std::string & /*reason*/) const
1179 std::string logStr = "Disconnect, ";
1180 std::ostringstream sssu;
1181 std::ostringstream sssd;
1182 std::ostringstream ssmu;
1183 std::ostringstream ssmd;
1184 std::ostringstream sscash;
1190 sssd << sessionDown;
1194 logStr += " session upload: \'";
1195 logStr += sssu.str();
1196 logStr += "\' session download: \'";
1197 logStr += sssd.str();
1198 logStr += "\' month upload: \'";
1199 logStr += ssmu.str();
1200 logStr += "\' month download: \'";
1201 logStr += ssmd.str();
1202 logStr += "\' cash: \'";
1203 logStr += sscash.str();
1206 return WriteLogString(logStr, login);
1208 //-----------------------------------------------------------------------------
1209 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1210 const std::string & login) const
1212 std::string param, res;
1214 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1215 login.c_str(), month+1, year+1900);
1217 for (int i = 0; i < DIR_NUM; i++)
1219 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1222 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1226 strprintf(¶m, " cash=%f", stat.cash);
1229 if(MysqlSetQuery(res.c_str()))
1231 errorStr = "Couldn't SaveMonthStat:\n";
1232 //errorStr += mysql_error(sock);
1238 //-----------------------------------------------------------------------------*/
1239 int MYSQL_STORE::AddAdmin(const std::string & login) const
1241 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1243 if(MysqlSetQuery(qbuf))
1245 errorStr = "Couldn't add admin:\n";
1246 //errorStr += mysql_error(sock);
1252 //-----------------------------------------------------------------------------*/
1253 int MYSQL_STORE::DelAdmin(const std::string & login) const
1255 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1257 if(MysqlSetQuery(qbuf))
1259 errorStr = "Couldn't delete admin:\n";
1260 //errorStr += mysql_error(sock);
1266 //-----------------------------------------------------------------------------*/
1267 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1269 char passwordE[2 * ADM_PASSWD_LEN + 2];
1270 char pass[ADM_PASSWD_LEN + 1];
1271 char adminPass[ADM_PASSWD_LEN + 1];
1273 memset(pass, 0, sizeof(pass));
1274 memset(adminPass, 0, sizeof(adminPass));
1277 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1279 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1280 adminPass[ADM_PASSWD_LEN - 1] = 0;
1282 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1284 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1287 pass[ADM_PASSWD_LEN - 1] = 0;
1288 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1290 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1291 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1292 "WHERE login='%s' LIMIT 1",
1304 if(MysqlSetQuery(qbuf))
1306 errorStr = "Couldn't save admin:\n";
1307 //errorStr += mysql_error(sock);
1313 //-----------------------------------------------------------------------------
1314 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1316 char pass[ADM_PASSWD_LEN + 1];
1317 char password[ADM_PASSWD_LEN + 1];
1318 char passwordE[2*ADM_PASSWD_LEN + 2];
1321 memset(password, 0, sizeof(password));
1327 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1329 if(MysqlGetQuery(qbuf,sock))
1331 errorStr = "Couldn't restore admin:\n";
1332 errorStr += mysql_error(sock);
1337 if (!(res=mysql_store_result(sock)))
1339 errorStr = "Couldn't restore admin:\n";
1340 errorStr += mysql_error(sock);
1345 if ( mysql_num_rows(res) == 0)
1347 mysql_free_result(res);
1348 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1353 row = mysql_fetch_row(res);
1359 mysql_free_result(res);
1360 errorStr = "Error in parameter password";
1365 memset(passwordE, 0, sizeof(passwordE));
1366 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1368 memset(pass, 0, sizeof(pass));
1370 if (passwordE[0] != 0)
1372 Decode21(pass, passwordE);
1373 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1375 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1377 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1385 ac->password = password;
1389 if (GetInt(row[2], &a) == 0)
1390 ac->priv.userConf = a;
1393 mysql_free_result(res);
1394 errorStr = "Error in parameter ChgConf";
1399 if (GetInt(row[3], &a) == 0)
1400 ac->priv.userPasswd = a;
1403 mysql_free_result(res);
1404 errorStr = "Error in parameter ChgPassword";
1409 if (GetInt(row[4], &a) == 0)
1410 ac->priv.userStat = a;
1413 mysql_free_result(res);
1414 errorStr = "Error in parameter ChgStat";
1419 if (GetInt(row[5], &a) == 0)
1420 ac->priv.userCash = a;
1423 mysql_free_result(res);
1424 errorStr = "Error in parameter ChgCash";
1429 if (GetInt(row[6], &a) == 0)
1430 ac->priv.userAddDel = a;
1433 mysql_free_result(res);
1434 errorStr = "Error in parameter UsrAddDel";
1439 if (GetInt(row[7], &a) == 0)
1440 ac->priv.tariffChg = a;
1443 mysql_free_result(res);
1444 errorStr = "Error in parameter ChgTariff";
1449 if (GetInt(row[8], &a) == 0)
1450 ac->priv.adminChg = a;
1453 mysql_free_result(res);
1454 errorStr = "Error in parameter ChgAdmin";
1459 mysql_free_result(res);
1463 //-----------------------------------------------------------------------------
1464 int MYSQL_STORE::AddTariff(const std::string & name) const
1466 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1468 if(MysqlSetQuery(qbuf))
1470 errorStr = "Couldn't add tariff:\n";
1471 // errorStr += mysql_error(sock);
1477 //-----------------------------------------------------------------------------
1478 int MYSQL_STORE::DelTariff(const std::string & name) const
1480 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1482 if(MysqlSetQuery(qbuf))
1484 errorStr = "Couldn't delete tariff: ";
1485 // errorStr += mysql_error(sock);
1491 //-----------------------------------------------------------------------------
1492 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1497 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1499 if(MysqlGetQuery(qbuf,sock))
1501 errorStr = "Couldn't restore Tariff:\n";
1502 errorStr += mysql_error(sock);
1507 if (!(res=mysql_store_result(sock)))
1509 errorStr = "Couldn't restore Tariff:\n";
1510 errorStr += mysql_error(sock);
1516 td->tariffConf.name = tariffName;
1518 row = mysql_fetch_row(res);
1521 for (int i = 0; i<DIR_NUM; i++)
1523 strprintf(¶m, "Time%d", i);
1525 if (str.length() == 0)
1527 mysql_free_result(res);
1528 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1533 ParseTariffTimeStr(str.c_str(),
1534 td->dirPrice[i].hDay,
1535 td->dirPrice[i].mDay,
1536 td->dirPrice[i].hNight,
1537 td->dirPrice[i].mNight);
1539 strprintf(¶m, "PriceDayA%d", i);
1540 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1542 mysql_free_result(res);
1543 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1547 td->dirPrice[i].priceDayA /= (1024*1024);
1549 strprintf(¶m, "PriceDayB%d", i);
1550 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1552 mysql_free_result(res);
1553 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1557 td->dirPrice[i].priceDayB /= (1024*1024);
1559 strprintf(¶m, "PriceNightA%d", i);
1560 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1562 mysql_free_result(res);
1563 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1567 td->dirPrice[i].priceNightA /= (1024*1024);
1569 strprintf(¶m, "PriceNightB%d", i);
1570 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1572 mysql_free_result(res);
1573 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1577 td->dirPrice[i].priceNightB /= (1024*1024);
1579 strprintf(¶m, "Threshold%d", i);
1580 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
1582 mysql_free_result(res);
1583 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1588 strprintf(¶m, "SinglePrice%d", i);
1589 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
1591 mysql_free_result(res);
1592 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1597 strprintf(¶m, "NoDiscount%d", i);
1598 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
1600 mysql_free_result(res);
1601 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1607 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1609 mysql_free_result(res);
1610 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1615 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1617 mysql_free_result(res);
1618 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1623 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1625 mysql_free_result(res);
1626 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1631 str = row[4+8*DIR_NUM];
1632 param = "TraffType";
1634 if (str.length() == 0)
1636 mysql_free_result(res);
1637 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1642 td->tariffConf.traffType = TARIFF::StringToTraffType(str);
1644 if (schemaVersion > 0)
1646 str = row[5+8*DIR_NUM];
1649 if (str.length() == 0)
1651 mysql_free_result(res);
1652 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1657 td->tariffConf.period = TARIFF::StringToPeriod(str);
1661 td->tariffConf.period = TARIFF::MONTH;
1664 if (schemaVersion > 1)
1666 str = row[6+8*DIR_NUM];
1667 param = "ChangePolicy";
1669 if (str.length() == 0)
1671 mysql_free_result(res);
1672 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1677 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
1681 td->tariffConf.changePolicy = TARIFF::ALLOW;
1684 mysql_free_result(res);
1688 //-----------------------------------------------------------------------------
1689 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1693 std::string res="UPDATE tariffs SET";
1695 for (int i = 0; i < DIR_NUM; i++)
1697 strprintf(¶m, " PriceDayA%d=%f,", i,
1698 td.dirPrice[i].priceDayA * pt_mega);
1701 strprintf(¶m, " PriceDayB%d=%f,", i,
1702 td.dirPrice[i].priceDayB * pt_mega);
1705 strprintf(¶m, " PriceNightA%d=%f,", i,
1706 td.dirPrice[i].priceNightA * pt_mega);
1709 strprintf(¶m, " PriceNightB%d=%f,", i,
1710 td.dirPrice[i].priceNightB * pt_mega);
1713 strprintf(¶m, " Threshold%d=%d,", i,
1714 td.dirPrice[i].threshold);
1718 strprintf(¶m, " Time%d", i);
1720 strprintf(&s, "%0d:%0d-%0d:%0d",
1721 td.dirPrice[i].hDay,
1722 td.dirPrice[i].mDay,
1723 td.dirPrice[i].hNight,
1724 td.dirPrice[i].mNight);
1726 res += (param + "='" + s + "',");
1728 strprintf(¶m, " NoDiscount%d=%d,", i,
1729 td.dirPrice[i].noDiscount);
1732 strprintf(¶m, " SinglePrice%d=%d,", i,
1733 td.dirPrice[i].singlePrice);
1737 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1740 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1743 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1746 res += " TraffType='" + TARIFF::TraffTypeToString(td.tariffConf.traffType) + "'";
1748 if (schemaVersion > 0)
1749 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1751 if (schemaVersion > 1)
1752 res += ", change_policy='" + TARIFF::ChangePolicyToString(td.tariffConf.changePolicy) + "'";
1754 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1757 if(MysqlSetQuery(res.c_str()))
1759 errorStr = "Couldn't save tariff:\n";
1760 //errorStr += mysql_error(sock);
1766 //-----------------------------------------------------------------------------
1767 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1769 const std::string & login) const
1771 std::string res, stTime, endTime, tempStr;
1778 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1786 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1788 if (!(sock=MysqlConnect())){
1793 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1795 errorStr = "Couldn't get table " + tempStr + ":\n";
1796 errorStr += mysql_error(sock);
1801 my_ulonglong num_rows = mysql_num_rows(result);
1803 mysql_free_result(result);
1807 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1808 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1809 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1810 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1811 lt->tm_mon+1, lt->tm_year+1900);
1813 if(MysqlQuery(qbuf,sock))
1815 errorStr = "Couldn't create WriteDetailedStat table:\n";
1816 errorStr += mysql_error(sock);
1825 lt1 = localtime(&lastStat);
1834 lt2 = localtime(&t);
1840 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1841 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1843 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1844 "day=%d,startTime='%s',endTime='%s',",
1845 lt->tm_mon+1, lt->tm_year+1900,
1852 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1853 stIter = statTree.begin();
1855 while (stIter != statTree.end())
1857 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1858 inet_ntostring(stIter->first.ip).c_str(),
1860 stIter->second.down,
1865 if( MysqlQuery((res+tempStr).c_str(),sock) )
1867 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1868 errorStr += mysql_error(sock);
1873 result=mysql_store_result(sock);
1875 mysql_free_result(result);
1882 //-----------------------------------------------------------------------------
1883 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1887 gettimeofday(&tv, NULL);
1889 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1891 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1893 static_cast<long long>(msg->header.id)
1896 if(MysqlSetQuery(qbuf))
1898 errorStr = "Couldn't add message:\n";
1899 //errorStr += mysql_error(sock);
1903 return EditMessage(*msg, login);
1905 //-----------------------------------------------------------------------------
1906 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1910 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1911 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1912 "WHERE login='%s' AND id=%lld LIMIT 1",
1914 msg.header.lastSendTime,
1915 msg.header.creationTime,
1916 msg.header.showTime,
1918 msg.header.repeatPeriod,
1919 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1924 if(MysqlSetQuery(res.c_str()))
1926 errorStr = "Couldn't edit message:\n";
1927 //errorStr += mysql_error(sock);
1933 //-----------------------------------------------------------------------------
1934 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1940 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1941 login.c_str(), static_cast<unsigned long long>(id));
1943 if(MysqlGetQuery(qbuf,sock))
1945 errorStr = "Couldn't GetMessage:\n";
1946 errorStr += mysql_error(sock);
1951 if (!(res=mysql_store_result(sock)))
1953 errorStr = "Couldn't GetMessage:\n";
1954 errorStr += mysql_error(sock);
1959 row = mysql_fetch_row(res);
1961 if(row[2]&&str2x(row[2], msg->header.type))
1963 mysql_free_result(res);
1964 errorStr = "Invalid value in message header for user: " + login;
1969 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1971 mysql_free_result(res);
1972 errorStr = "Invalid value in message header for user: " + login;
1977 if(row[4] && str2x(row[4], msg->header.creationTime))
1979 mysql_free_result(res);
1980 errorStr = "Invalid value in message header for user: " + login;
1985 if(row[5] && str2x(row[5], msg->header.showTime))
1987 mysql_free_result(res);
1988 errorStr = "Invalid value in message header for user: " + login;
1993 if(row[6] && str2x(row[6], msg->header.repeat))
1995 mysql_free_result(res);
1996 errorStr = "Invalid value in message header for user: " + login;
2001 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
2003 mysql_free_result(res);
2004 errorStr = "Invalid value in message header for user: " + login;
2009 msg->header.id = id;
2012 mysql_free_result(res);
2016 //-----------------------------------------------------------------------------
2017 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
2019 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
2020 login.c_str(), static_cast<long long>(id));
2022 if(MysqlSetQuery(qbuf))
2024 errorStr = "Couldn't delete Message:\n";
2025 //errorStr += mysql_error(sock);
2031 //-----------------------------------------------------------------------------
2032 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
2037 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
2039 if(MysqlGetQuery(qbuf,sock))
2041 errorStr = "Couldn't GetMessageHdrs:\n";
2042 errorStr += mysql_error(sock);
2047 if (!(res=mysql_store_result(sock)))
2049 errorStr = "Couldn't GetMessageHdrs:\n";
2050 errorStr += mysql_error(sock);
2056 my_ulonglong num_rows = mysql_num_rows(res);
2059 for (i = 0; i < num_rows; i++)
2061 row = mysql_fetch_row(res);
2062 if (str2x(row[1], id))
2067 if(str2x(row[2], hdr.type))
2071 if(str2x(row[3], hdr.lastSendTime))
2075 if(str2x(row[4], hdr.creationTime))
2079 if(str2x(row[5], hdr.showTime))
2083 if(str2x(row[6], hdr.repeat))
2087 if(str2x(row[7], hdr.repeatPeriod))
2091 hdrsList->push_back(hdr);
2094 mysql_free_result(res);
2098 //-----------------------------------------------------------------------------
2100 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2103 int ret=MysqlGetQuery(Query,sock);
2107 //-----------------------------------------------------------------------------
2108 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2109 if (!(sock=MysqlConnect())) {
2112 return MysqlQuery(Query,sock);
2114 //-----------------------------------------------------------------------------
2115 MYSQL * MYSQL_STORE::MysqlConnect() const {
2117 if ( !(sock=mysql_init(NULL)) ){
2118 errorStr= "mysql init susck\n";
2121 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2122 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2125 errorStr = "Couldn't connect to mysql engine! With error:\n";
2126 errorStr += mysql_error(sock);
2130 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2131 errorStr = "Database lost !\n";
2137 //-----------------------------------------------------------------------------