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);
606 //-----------------------------------------------------------------------------
608 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList,
609 const std::string & table, const std::string & name) const
618 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
620 if(MysqlGetQuery(qbuf,sock))
622 errorStr = "Couldn't GetAllParams Query for: ";
623 errorStr += name + " - " + table + "\n";
624 errorStr += mysql_error(sock);
629 if (!(res=mysql_store_result(sock)))
631 errorStr = "Couldn't GetAllParams Results for: ";
632 errorStr += name + " - " + table + "\n";
633 errorStr += mysql_error(sock);
637 num = mysql_num_rows(res);
639 for(i = 0; i < num; i++)
641 row = mysql_fetch_row(res);
642 ParamList->push_back(row[0]);
645 mysql_free_result(res);
651 //-----------------------------------------------------------------------------
652 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
654 if(GetAllParams(usersList, "users", "login"))
659 //-----------------------------------------------------------------------------
660 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
662 if(GetAllParams(adminsList, "admins", "login"))
667 //-----------------------------------------------------------------------------
668 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
670 if(GetAllParams(tariffsList, "tariffs", "name"))
675 //-----------------------------------------------------------------------------
676 int MYSQL_STORE::AddUser(const std::string & login) const
678 std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
680 for (int i = 0; i < USERDATA_NUM; i++)
681 query += ",Userdata" + x2str(i) + "=''";
683 if(MysqlSetQuery(query.c_str()))
685 errorStr = "Couldn't add user:\n";
686 //errorStr += mysql_error(sock);
692 //-----------------------------------------------------------------------------
693 int MYSQL_STORE::DelUser(const std::string & login) const
695 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
697 if(MysqlSetQuery(qbuf))
699 errorStr = "Couldn't delete user:\n";
700 //errorStr += mysql_error(sock);
706 //-----------------------------------------------------------------------------
707 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
714 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
715 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
716 RealName, StgGroup, Credit, TariffChange, ";
718 for (int i = 0; i < USERDATA_NUM; i++)
720 sprintf(qbuf, "Userdata%d, ", i);
724 query += "CreditExpire, IP FROM users WHERE login='";
725 query += login + "' LIMIT 1";
727 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
729 if(MysqlGetQuery(query.c_str(),sock))
731 errorStr = "Couldn't restore Tariff(on query):\n";
732 errorStr += mysql_error(sock);
737 if (!(res=mysql_store_result(sock)))
739 errorStr = "Couldn't restore Tariff(on getting result):\n";
740 errorStr += mysql_error(sock);
745 if (mysql_num_rows(res) != 1)
747 errorStr = "User not found";
752 row = mysql_fetch_row(res);
754 conf->password = row[1];
756 if (conf->password.empty())
758 mysql_free_result(res);
759 errorStr = "User \'" + login + "\' password is blank.";
764 if (GetInt(row[2],&conf->passive) != 0)
766 mysql_free_result(res);
767 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
772 if (GetInt(row[3], &conf->disabled) != 0)
774 mysql_free_result(res);
775 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
780 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
782 mysql_free_result(res);
783 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
788 if (GetInt(row[5], &conf->alwaysOnline) != 0)
790 mysql_free_result(res);
791 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
796 conf->tariffName = row[6];
798 if (conf->tariffName.empty())
800 mysql_free_result(res);
801 errorStr = "User \'" + login + "\' tariff is blank.";
806 conf->address = row[7];
807 conf->phone = row[8];
808 conf->email = row[9];
809 conf->note = row[10];
810 conf->realName = row[11];
811 conf->group = row[12];
813 if (GetDouble(row[13], &conf->credit, 0) != 0)
815 mysql_free_result(res);
816 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
821 conf->nextTariff = row[14];
823 for (int i = 0; i < USERDATA_NUM; i++)
825 conf->userdata[i] = row[15+i];
828 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
830 std::string ipStr = row[16+USERDATA_NUM];
836 catch (const std::string & s)
838 mysql_free_result(res);
839 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
845 mysql_free_result(res);
850 //-----------------------------------------------------------------------------
851 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
861 for (int i = 0; i < DIR_NUM; i++)
863 sprintf(qbuf, "D%d, U%d, ", i, i);
867 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
868 FROM users WHERE login = '";
869 query += login + "'";
871 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
873 if(MysqlGetQuery(query.c_str() ,sock))
875 errorStr = "Couldn't restore UserStat(on query):\n";
876 errorStr += mysql_error(sock);
881 if (!(res=mysql_store_result(sock)))
883 errorStr = "Couldn't restore UserStat(on getting result):\n";
884 errorStr += mysql_error(sock);
889 row = mysql_fetch_row(res);
891 unsigned int startPos=0;
895 for (int i = 0; i < DIR_NUM; i++)
898 sprintf(s, "D%d", i);
899 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
901 mysql_free_result(res);
902 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
906 stat->monthDown[i] = traff;
908 sprintf(s, "U%d", i);
909 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
911 mysql_free_result(res);
912 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
916 stat->monthUp[i] = traff;
919 startPos += (2*DIR_NUM);
921 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
923 mysql_free_result(res);
924 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
929 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
931 mysql_free_result(res);
932 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
937 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
939 mysql_free_result(res);
940 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
945 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
947 mysql_free_result(res);
948 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
953 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
955 mysql_free_result(res);
956 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
961 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
963 mysql_free_result(res);
964 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
969 mysql_free_result(res);
973 //-----------------------------------------------------------------------------
974 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
979 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
980 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
981 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
982 conf.password.c_str(),
985 conf.disabledDetailStat,
987 conf.tariffName.c_str(),
988 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
989 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
990 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
991 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
992 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
993 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
995 conf.nextTariff.c_str()
998 for (int i = 0; i < USERDATA_NUM; i++)
1000 strprintf(¶m, " Userdata%d='%s',", i,
1001 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1005 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1008 std::ostringstream ipStr;
1011 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1014 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1017 if(MysqlSetQuery(res.c_str()))
1019 errorStr = "Couldn't save user conf:\n";
1020 //errorStr += mysql_error(sock);
1026 //-----------------------------------------------------------------------------
1027 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
1032 res = "UPDATE users SET";
1034 for (int i = 0; i < DIR_NUM; i++)
1036 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1039 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1043 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1044 " PassiveTime=%d, LastActivityTime=%d",
1048 stat.lastCashAddTime,
1050 stat.lastActivityTime
1054 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1057 if(MysqlSetQuery(res.c_str()))
1059 errorStr = "Couldn't save user stat:\n";
1060 // errorStr += mysql_error(sock);
1066 //-----------------------------------------------------------------------------
1067 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
1069 std::string res, tempStr;
1078 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1079 if (!(sock=MysqlConnect())){
1080 errorStr = "Couldn't connect to Server";
1083 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1085 errorStr = "Couldn't get table " + tempStr + ":\n";
1086 errorStr += mysql_error(sock);
1091 my_ulonglong num_rows = mysql_num_rows(result);
1093 mysql_free_result(result);
1097 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1098 lt->tm_mon+1, lt->tm_year+1900);
1100 if(MysqlQuery(qbuf,sock))
1102 errorStr = "Couldn't create WriteDetailedStat table:\n";
1103 errorStr += mysql_error(sock);
1109 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1113 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1114 lt->tm_mon+1, lt->tm_year+1900,
1115 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1117 if(MysqlQuery(send.c_str(),sock))
1119 errorStr = "Couldn't write log string:\n";
1120 errorStr += mysql_error(sock);
1128 //-----------------------------------------------------------------------------
1129 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
1130 const std::string & admLogin,
1132 const std::string & paramName,
1133 const std::string & oldValue,
1134 const std::string & newValue,
1135 const std::string & message) const
1137 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1138 + paramName + "\' parameter changed from \'" + oldValue +
1139 "\' to \'" + newValue + "\'. " + message;
1141 return WriteLogString(userLogMsg, login);
1143 //-----------------------------------------------------------------------------
1144 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1146 std::string logStr = "Connect, " + inet_ntostring(ip);
1147 return WriteLogString(logStr, login);
1149 //-----------------------------------------------------------------------------
1150 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
1151 const DIR_TRAFF & up,
1152 const DIR_TRAFF & down,
1153 const DIR_TRAFF & sessionUp,
1154 const DIR_TRAFF & sessionDown,
1157 const std::string & /*reason*/) const
1159 std::string logStr = "Disconnect, ";
1160 std::ostringstream sssu;
1161 std::ostringstream sssd;
1162 std::ostringstream ssmu;
1163 std::ostringstream ssmd;
1164 std::ostringstream sscash;
1170 sssd << sessionDown;
1174 logStr += " session upload: \'";
1175 logStr += sssu.str();
1176 logStr += "\' session download: \'";
1177 logStr += sssd.str();
1178 logStr += "\' month upload: \'";
1179 logStr += ssmu.str();
1180 logStr += "\' month download: \'";
1181 logStr += ssmd.str();
1182 logStr += "\' cash: \'";
1183 logStr += sscash.str();
1186 return WriteLogString(logStr, login);
1188 //-----------------------------------------------------------------------------
1189 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1190 const std::string & login) const
1192 std::string param, res;
1194 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1195 login.c_str(), month+1, year+1900);
1197 for (int i = 0; i < DIR_NUM; i++)
1199 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1202 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1206 strprintf(¶m, " cash=%f", stat.cash);
1209 if(MysqlSetQuery(res.c_str()))
1211 errorStr = "Couldn't SaveMonthStat:\n";
1212 //errorStr += mysql_error(sock);
1218 //-----------------------------------------------------------------------------*/
1219 int MYSQL_STORE::AddAdmin(const std::string & login) const
1221 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1223 if(MysqlSetQuery(qbuf))
1225 errorStr = "Couldn't add admin:\n";
1226 //errorStr += mysql_error(sock);
1232 //-----------------------------------------------------------------------------*/
1233 int MYSQL_STORE::DelAdmin(const std::string & login) const
1235 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1237 if(MysqlSetQuery(qbuf))
1239 errorStr = "Couldn't delete admin:\n";
1240 //errorStr += mysql_error(sock);
1246 //-----------------------------------------------------------------------------*/
1247 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1249 char passwordE[2 * ADM_PASSWD_LEN + 2];
1250 char pass[ADM_PASSWD_LEN + 1];
1251 char adminPass[ADM_PASSWD_LEN + 1];
1253 memset(pass, 0, sizeof(pass));
1254 memset(adminPass, 0, sizeof(adminPass));
1257 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1259 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1260 adminPass[ADM_PASSWD_LEN - 1] = 0;
1262 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1264 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1267 pass[ADM_PASSWD_LEN - 1] = 0;
1268 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1270 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1271 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1272 "WHERE login='%s' LIMIT 1",
1284 if(MysqlSetQuery(qbuf))
1286 errorStr = "Couldn't save admin:\n";
1287 //errorStr += mysql_error(sock);
1293 //-----------------------------------------------------------------------------
1294 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1296 char pass[ADM_PASSWD_LEN + 1];
1297 char password[ADM_PASSWD_LEN + 1];
1298 char passwordE[2*ADM_PASSWD_LEN + 2];
1301 memset(password, 0, sizeof(password));
1307 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1309 if(MysqlGetQuery(qbuf,sock))
1311 errorStr = "Couldn't restore admin:\n";
1312 errorStr += mysql_error(sock);
1317 if (!(res=mysql_store_result(sock)))
1319 errorStr = "Couldn't restore admin:\n";
1320 errorStr += mysql_error(sock);
1325 if ( mysql_num_rows(res) == 0)
1327 mysql_free_result(res);
1328 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1333 row = mysql_fetch_row(res);
1339 mysql_free_result(res);
1340 errorStr = "Error in parameter password";
1345 memset(passwordE, 0, sizeof(passwordE));
1346 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1348 memset(pass, 0, sizeof(pass));
1350 if (passwordE[0] != 0)
1352 Decode21(pass, passwordE);
1353 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1355 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1357 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1365 ac->password = password;
1369 if (GetInt(row[2], &a) == 0)
1370 ac->priv.userConf = a;
1373 mysql_free_result(res);
1374 errorStr = "Error in parameter ChgConf";
1379 if (GetInt(row[3], &a) == 0)
1380 ac->priv.userPasswd = a;
1383 mysql_free_result(res);
1384 errorStr = "Error in parameter ChgPassword";
1389 if (GetInt(row[4], &a) == 0)
1390 ac->priv.userStat = a;
1393 mysql_free_result(res);
1394 errorStr = "Error in parameter ChgStat";
1399 if (GetInt(row[5], &a) == 0)
1400 ac->priv.userCash = a;
1403 mysql_free_result(res);
1404 errorStr = "Error in parameter ChgCash";
1409 if (GetInt(row[6], &a) == 0)
1410 ac->priv.userAddDel = a;
1413 mysql_free_result(res);
1414 errorStr = "Error in parameter UsrAddDel";
1419 if (GetInt(row[7], &a) == 0)
1420 ac->priv.tariffChg = a;
1423 mysql_free_result(res);
1424 errorStr = "Error in parameter ChgTariff";
1429 if (GetInt(row[8], &a) == 0)
1430 ac->priv.adminChg = a;
1433 mysql_free_result(res);
1434 errorStr = "Error in parameter ChgAdmin";
1439 mysql_free_result(res);
1443 //-----------------------------------------------------------------------------
1444 int MYSQL_STORE::AddTariff(const std::string & name) const
1446 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1448 if(MysqlSetQuery(qbuf))
1450 errorStr = "Couldn't add tariff:\n";
1451 // errorStr += mysql_error(sock);
1457 //-----------------------------------------------------------------------------
1458 int MYSQL_STORE::DelTariff(const std::string & name) const
1460 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1462 if(MysqlSetQuery(qbuf))
1464 errorStr = "Couldn't delete tariff: ";
1465 // errorStr += mysql_error(sock);
1471 //-----------------------------------------------------------------------------
1472 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1477 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1479 if(MysqlGetQuery(qbuf,sock))
1481 errorStr = "Couldn't restore Tariff:\n";
1482 errorStr += mysql_error(sock);
1487 if (!(res=mysql_store_result(sock)))
1489 errorStr = "Couldn't restore Tariff:\n";
1490 errorStr += mysql_error(sock);
1496 td->tariffConf.name = tariffName;
1498 row = mysql_fetch_row(res);
1501 for (int i = 0; i<DIR_NUM; i++)
1503 strprintf(¶m, "Time%d", i);
1505 if (str.length() == 0)
1507 mysql_free_result(res);
1508 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1513 ParseTariffTimeStr(str.c_str(),
1514 td->dirPrice[i].hDay,
1515 td->dirPrice[i].mDay,
1516 td->dirPrice[i].hNight,
1517 td->dirPrice[i].mNight);
1519 strprintf(¶m, "PriceDayA%d", i);
1520 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1522 mysql_free_result(res);
1523 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1527 td->dirPrice[i].priceDayA /= (1024*1024);
1529 strprintf(¶m, "PriceDayB%d", i);
1530 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1532 mysql_free_result(res);
1533 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1537 td->dirPrice[i].priceDayB /= (1024*1024);
1539 strprintf(¶m, "PriceNightA%d", i);
1540 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1542 mysql_free_result(res);
1543 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1547 td->dirPrice[i].priceNightA /= (1024*1024);
1549 strprintf(¶m, "PriceNightB%d", i);
1550 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1552 mysql_free_result(res);
1553 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1557 td->dirPrice[i].priceNightB /= (1024*1024);
1559 strprintf(¶m, "Threshold%d", i);
1560 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
1562 mysql_free_result(res);
1563 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1568 strprintf(¶m, "SinglePrice%d", i);
1569 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
1571 mysql_free_result(res);
1572 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1577 strprintf(¶m, "NoDiscount%d", i);
1578 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
1580 mysql_free_result(res);
1581 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1587 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1589 mysql_free_result(res);
1590 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1595 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1597 mysql_free_result(res);
1598 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1603 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1605 mysql_free_result(res);
1606 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1611 str = row[4+8*DIR_NUM];
1612 param = "TraffType";
1614 if (str.length() == 0)
1616 mysql_free_result(res);
1617 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1622 td->tariffConf.traffType = TARIFF::StringToTraffType(str);
1624 if (schemaVersion > 0)
1626 str = row[5+8*DIR_NUM];
1629 if (str.length() == 0)
1631 mysql_free_result(res);
1632 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1637 td->tariffConf.period = TARIFF::StringToPeriod(str);
1641 td->tariffConf.period = TARIFF::MONTH;
1644 mysql_free_result(res);
1648 //-----------------------------------------------------------------------------
1649 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1653 std::string res="UPDATE tariffs SET";
1655 for (int i = 0; i < DIR_NUM; i++)
1657 strprintf(¶m, " PriceDayA%d=%f,", i,
1658 td.dirPrice[i].priceDayA * pt_mega);
1661 strprintf(¶m, " PriceDayB%d=%f,", i,
1662 td.dirPrice[i].priceDayB * pt_mega);
1665 strprintf(¶m, " PriceNightA%d=%f,", i,
1666 td.dirPrice[i].priceNightA * pt_mega);
1669 strprintf(¶m, " PriceNightB%d=%f,", i,
1670 td.dirPrice[i].priceNightB * pt_mega);
1673 strprintf(¶m, " Threshold%d=%d,", i,
1674 td.dirPrice[i].threshold);
1678 strprintf(¶m, " Time%d", i);
1680 strprintf(&s, "%0d:%0d-%0d:%0d",
1681 td.dirPrice[i].hDay,
1682 td.dirPrice[i].mDay,
1683 td.dirPrice[i].hNight,
1684 td.dirPrice[i].mNight);
1686 res += (param + "='" + s + "',");
1688 strprintf(¶m, " NoDiscount%d=%d,", i,
1689 td.dirPrice[i].noDiscount);
1692 strprintf(¶m, " SinglePrice%d=%d,", i,
1693 td.dirPrice[i].singlePrice);
1697 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1700 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1703 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1706 res += " TraffType='" + TARIFF::TraffTypeToString(td.tariffConf.traffType) + "'";
1708 if (schemaVersion > 0)
1709 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1711 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1714 if(MysqlSetQuery(res.c_str()))
1716 errorStr = "Couldn't save tariff:\n";
1717 //errorStr += mysql_error(sock);
1723 //-----------------------------------------------------------------------------
1724 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1726 const std::string & login) const
1728 std::string res, stTime, endTime, tempStr;
1735 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1743 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1745 if (!(sock=MysqlConnect())){
1750 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1752 errorStr = "Couldn't get table " + tempStr + ":\n";
1753 errorStr += mysql_error(sock);
1758 my_ulonglong num_rows = mysql_num_rows(result);
1760 mysql_free_result(result);
1764 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1765 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1766 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1767 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1768 lt->tm_mon+1, lt->tm_year+1900);
1770 if(MysqlQuery(qbuf,sock))
1772 errorStr = "Couldn't create WriteDetailedStat table:\n";
1773 errorStr += mysql_error(sock);
1782 lt1 = localtime(&lastStat);
1791 lt2 = localtime(&t);
1797 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1798 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1800 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1801 "day=%d,startTime='%s',endTime='%s',",
1802 lt->tm_mon+1, lt->tm_year+1900,
1809 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1810 stIter = statTree.begin();
1812 while (stIter != statTree.end())
1814 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1815 inet_ntostring(stIter->first.ip).c_str(),
1817 stIter->second.down,
1822 if( MysqlQuery((res+tempStr).c_str(),sock) )
1824 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1825 errorStr += mysql_error(sock);
1830 result=mysql_store_result(sock);
1832 mysql_free_result(result);
1839 //-----------------------------------------------------------------------------
1840 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1844 gettimeofday(&tv, NULL);
1846 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1848 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1850 static_cast<long long>(msg->header.id)
1853 if(MysqlSetQuery(qbuf))
1855 errorStr = "Couldn't add message:\n";
1856 //errorStr += mysql_error(sock);
1860 return EditMessage(*msg, login);
1862 //-----------------------------------------------------------------------------
1863 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1867 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1868 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1869 "WHERE login='%s' AND id=%lld LIMIT 1",
1871 msg.header.lastSendTime,
1872 msg.header.creationTime,
1873 msg.header.showTime,
1875 msg.header.repeatPeriod,
1876 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1881 if(MysqlSetQuery(res.c_str()))
1883 errorStr = "Couldn't edit message:\n";
1884 //errorStr += mysql_error(sock);
1890 //-----------------------------------------------------------------------------
1891 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1897 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1898 login.c_str(), static_cast<unsigned long long>(id));
1900 if(MysqlGetQuery(qbuf,sock))
1902 errorStr = "Couldn't GetMessage:\n";
1903 errorStr += mysql_error(sock);
1908 if (!(res=mysql_store_result(sock)))
1910 errorStr = "Couldn't GetMessage:\n";
1911 errorStr += mysql_error(sock);
1916 row = mysql_fetch_row(res);
1918 if(row[2]&&str2x(row[2], msg->header.type))
1920 mysql_free_result(res);
1921 errorStr = "Invalid value in message header for user: " + login;
1926 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1928 mysql_free_result(res);
1929 errorStr = "Invalid value in message header for user: " + login;
1934 if(row[4] && str2x(row[4], msg->header.creationTime))
1936 mysql_free_result(res);
1937 errorStr = "Invalid value in message header for user: " + login;
1942 if(row[5] && str2x(row[5], msg->header.showTime))
1944 mysql_free_result(res);
1945 errorStr = "Invalid value in message header for user: " + login;
1950 if(row[6] && str2x(row[6], msg->header.repeat))
1952 mysql_free_result(res);
1953 errorStr = "Invalid value in message header for user: " + login;
1958 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1960 mysql_free_result(res);
1961 errorStr = "Invalid value in message header for user: " + login;
1966 msg->header.id = id;
1969 mysql_free_result(res);
1973 //-----------------------------------------------------------------------------
1974 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
1976 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1977 login.c_str(), static_cast<long long>(id));
1979 if(MysqlSetQuery(qbuf))
1981 errorStr = "Couldn't delete Message:\n";
1982 //errorStr += mysql_error(sock);
1988 //-----------------------------------------------------------------------------
1989 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
1994 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1996 if(MysqlGetQuery(qbuf,sock))
1998 errorStr = "Couldn't GetMessageHdrs:\n";
1999 errorStr += mysql_error(sock);
2004 if (!(res=mysql_store_result(sock)))
2006 errorStr = "Couldn't GetMessageHdrs:\n";
2007 errorStr += mysql_error(sock);
2013 my_ulonglong num_rows = mysql_num_rows(res);
2016 for (i = 0; i < num_rows; i++)
2018 row = mysql_fetch_row(res);
2019 if (str2x(row[1], id))
2024 if(str2x(row[2], hdr.type))
2028 if(str2x(row[3], hdr.lastSendTime))
2032 if(str2x(row[4], hdr.creationTime))
2036 if(str2x(row[5], hdr.showTime))
2040 if(str2x(row[6], hdr.repeat))
2044 if(str2x(row[7], hdr.repeatPeriod))
2048 hdrsList->push_back(hdr);
2051 mysql_free_result(res);
2055 //-----------------------------------------------------------------------------
2057 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2060 int ret=MysqlGetQuery(Query,sock);
2064 //-----------------------------------------------------------------------------
2065 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2066 if (!(sock=MysqlConnect())) {
2069 return MysqlQuery(Query,sock);
2071 //-----------------------------------------------------------------------------
2072 MYSQL * MYSQL_STORE::MysqlConnect() const {
2074 if ( !(sock=mysql_init(NULL)) ){
2075 errorStr= "mysql init susck\n";
2078 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2079 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2082 errorStr = "Couldn't connect to mysql engine! With error:\n";
2083 errorStr += mysql_error(sock);
2087 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2088 errorStr = "Database lost !\n";
2094 //-----------------------------------------------------------------------------