12 #include "user_conf.h"
13 #include "user_stat.h"
14 #include "mysql_store.h"
17 #define adm_enc_passwd "cjeifY8m3"
22 const int pt_mega = 1024 * 1024;
23 const string badSyms = "'`";
24 const char repSym = '\"';
25 const int RepitTimes = 3;
27 int GetInt(const string & str, int * val, int defaultVal)
31 *val = strtol(str.c_str(), &res, 10);
35 *val = defaultVal; //Error!
42 int GetDouble(const string & str, double * val, double defaultVal)
46 *val = strtod(str.c_str(), &res);
50 *val = defaultVal; //Error!
57 int GetTime(const string & str, time_t * val, time_t defaultVal)
61 *val = strtol(str.c_str(), &res, 10);
65 *val = defaultVal; //Error!
72 //-----------------------------------------------------------------------------
73 string ReplaceStr(string source, const string symlist, const char chgsym)
75 string::size_type pos=0;
77 while( (pos = source.find_first_of(symlist,pos)) != string::npos)
78 source.replace(pos, 1,1, chgsym);
83 int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
87 *val = strtoull(str.c_str(), &res, 10);
91 *val = defaultVal; //Error!
98 class MYSQL_STORE_CREATOR
104 MYSQL_STORE_CREATOR()
105 : ms(new MYSQL_STORE())
108 ~MYSQL_STORE_CREATOR()
113 MYSQL_STORE * GetStore()
118 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
121 BASE_STORE * GetStore()
123 return msc.GetStore();
125 //-----------------------------------------------------------------------------
126 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
130 //-----------------------------------------------------------------------------
131 MYSQL_STORE_SETTINGS::~MYSQL_STORE_SETTINGS()
135 //-----------------------------------------------------------------------------
136 int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams,
137 const string & name, string & result)
141 vector<PARAM_VALUE>::const_iterator pvi;
142 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
143 if (pvi == moduleParams.end())
145 errorStr = "Parameter \'" + name + "\' not found.";
149 result = pvi->value[0];
153 //-----------------------------------------------------------------------------
154 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
156 if (ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
158 if (ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
160 if (ParseParam(s.moduleParams, "dbname", dbName) < 0)
162 if (ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
167 //-----------------------------------------------------------------------------
168 const string & MYSQL_STORE_SETTINGS::GetStrError() const
172 //-----------------------------------------------------------------------------
173 string MYSQL_STORE_SETTINGS::GetDBUser() const
177 //-----------------------------------------------------------------------------
178 string MYSQL_STORE_SETTINGS::GetDBPassword() const
182 //-----------------------------------------------------------------------------
183 string MYSQL_STORE_SETTINGS::GetDBHost() const
187 //-----------------------------------------------------------------------------
188 string MYSQL_STORE_SETTINGS::GetDBName() const
192 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
195 MYSQL_STORE::MYSQL_STORE()
197 version = "mysql_store v.0.67";
199 //-----------------------------------------------------------------------------
200 MYSQL_STORE::~MYSQL_STORE()
203 //-----------------------------------------------------------------------------
204 void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
208 //-----------------------------------------------------------------------------
209 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
213 if( (ret = mysql_query(sock,sQuery)) )
215 for(i=0; i<RepitTimes; i++)
217 if( (ret = mysql_query(sock,sQuery)) )
218 ;//need to send error result
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
229 int MYSQL_STORE::ParseSettings()
231 int ret = storeSettings.ParseSettings(settings);
236 errorStr = storeSettings.GetStrError();
239 if(storeSettings.GetDBPassword().length() == 0)
241 errorStr = "Database password must be not empty. Please read Manual.";
245 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
246 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
249 errorStr = "Couldn't connect to mysql engine! With error:\n";
250 errorStr += mysql_error(&mysql);
256 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
258 string res = "CREATE DATABASE " + storeSettings.GetDBName();
260 if(MysqlQuery(res.c_str(),sock))
262 errorStr = "Couldn't create database! With error:\n";
263 errorStr += mysql_error(sock);
269 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
271 errorStr = "Couldn't select database! With error:\n";
272 errorStr += mysql_error(sock);
276 ret = CheckAllTables(sock);
280 ret = CheckAllTables(sock);
287 //-----------------------------------------------------------------------------
288 const string & MYSQL_STORE::GetStrError() const
292 //-----------------------------------------------------------------------------
293 const string & MYSQL_STORE::GetVersion() const
297 //-----------------------------------------------------------------------------
298 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
302 if (!(result=mysql_list_tables(sock,str.c_str() )))
304 errorStr = "Couldn't get tables list With error:\n";
305 errorStr += mysql_error(sock);
310 unsigned int num_rows = mysql_num_rows(result);
313 mysql_free_result(result);
315 return (num_rows == 1);
317 //-----------------------------------------------------------------------------
318 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
320 //admins-----------------------------------------------------------------------
321 if(!IsTablePresent("admins",sock))
323 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
324 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
325 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
326 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
327 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
329 if(MysqlQuery(qbuf,sock))
331 errorStr = "Couldn't create admin table list With error:\n";
332 errorStr += mysql_error(sock);
337 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
338 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
339 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
341 if(MysqlQuery(qbuf,sock))
343 errorStr = "Couldn't create default admin. With error:\n";
344 errorStr += mysql_error(sock);
350 //tariffs-----------------------------------------------------------------------
352 if(!IsTablePresent("tariffs",sock))
354 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
356 for (int i = 0; i < DIR_NUM; i++)
358 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
361 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
364 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
367 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
370 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
373 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
376 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
379 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
383 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
384 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
386 if(MysqlQuery(res.c_str(),sock))
388 errorStr = "Couldn't create tariffs table list With error:\n";
389 errorStr += mysql_error(sock);
394 res = "INSERT INTO tariffs SET name='tariff',";
396 for (int i = 0; i < DIR_NUM; i++)
398 strprintf(¶m, " NoDiscount%d=1,", i);
401 strprintf(¶m, " Threshold%d=0,", i);
404 strprintf(¶m, " Time%d='0:0-0:0',", i);
409 strprintf(¶m, " SinglePrice%d=0,", i);
415 strprintf(¶m, " PriceDayA%d=0.0,", i);
420 strprintf(¶m, " PriceDayB%d=0.0,", i);
426 strprintf(¶m, " PriceNightA%d=0.0,", i);
431 strprintf(¶m, " PriceNightB%d=0.0,", i);
436 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
437 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
438 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
440 if(MysqlQuery(res.c_str(),sock))
442 errorStr = "Couldn't create default tariff. With error:\n";
443 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='',Userdata0='',"\
500 for (int i = 0; i < DIR_NUM; i++)
502 strprintf(¶m, " D%d=0,", i);
505 strprintf(¶m, " U%d=0,", i);
509 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
510 "LastCashAddTime=0, PassiveTime=0";
512 if(MysqlQuery(res.c_str(),sock))
514 errorStr = "Couldn't create default user. With error:\n";
515 errorStr += mysql_error(sock);
521 //logs-----------------------------------------------------------------------
522 if(!IsTablePresent("logs"))
524 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
528 errorStr = "Couldn't create admin table list With error:\n";
529 errorStr += mysql_error(sock);
534 //messages---------------------------------------------------------------------
535 if(!IsTablePresent("messages",sock))
537 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
538 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
539 "stgRepeat INT, repeatPeriod INT, text TEXT)");
541 if(MysqlQuery(qbuf,sock))
543 errorStr = "Couldn't create messages table. With error:\n";
544 errorStr += mysql_error(sock);
550 //month_stat-------------------------------------------------------------------
551 if(!IsTablePresent("stat",sock))
553 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
555 for (int i = 0; i < DIR_NUM; i++)
557 strprintf(¶m, " U%d BIGINT,", i);
560 strprintf(¶m, " D%d BIGINT,", i);
564 res += " cash DOUBLE, INDEX (login))";
566 if(MysqlQuery(res.c_str(),sock))
568 errorStr = "Couldn't create stat table. With error:\n";
569 errorStr += mysql_error(sock);
577 //-----------------------------------------------------------------------------
578 //-----------------------------------------------------------------------------
580 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
581 const string & table, const string & name) const
590 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
592 if(MysqlGetQuery(qbuf,sock))
594 errorStr = "Couldn't GetAllParams Query for: ";
595 errorStr += name + " - " + table + "\n";
596 errorStr += mysql_error(sock);
601 if (!(res=mysql_store_result(sock)))
603 errorStr = "Couldn't GetAllParams Results for: ";
604 errorStr += name + " - " + table + "\n";
605 errorStr += mysql_error(sock);
609 num = mysql_num_rows(res);
613 row = mysql_fetch_row(res);
614 ParamList->push_back(row[0]);
617 mysql_free_result(res);
623 //-----------------------------------------------------------------------------
624 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
626 if(GetAllParams(usersList, "users", "login"))
631 //-----------------------------------------------------------------------------
632 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
634 if(GetAllParams(adminsList, "admins", "login"))
639 //-----------------------------------------------------------------------------
640 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
642 if(GetAllParams(tariffsList, "tariffs", "name"))
647 //-----------------------------------------------------------------------------
648 int MYSQL_STORE::AddUser(const string & login) const
650 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
652 if(MysqlSetQuery(qbuf))
654 errorStr = "Couldn't add user:\n";
655 //errorStr += mysql_error(sock);
661 //-----------------------------------------------------------------------------
662 int MYSQL_STORE::DelUser(const string & login) const
664 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
666 if(MysqlSetQuery(qbuf))
668 errorStr = "Couldn't delete user:\n";
669 //errorStr += mysql_error(sock);
675 //-----------------------------------------------------------------------------
676 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
683 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
684 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
685 RealName, StgGroup, Credit, TariffChange, ";
687 for (int i = 0; i < USERDATA_NUM; i++)
689 sprintf(qbuf, "Userdata%d, ", i);
693 query += "CreditExpire, IP FROM users WHERE login='";
694 query += login + "' LIMIT 1";
696 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
698 if(MysqlGetQuery(query.c_str(),sock))
700 errorStr = "Couldn't restore Tariff(on query):\n";
701 errorStr += mysql_error(sock);
706 if (!(res=mysql_store_result(sock)))
708 errorStr = "Couldn't restore Tariff(on getting result):\n";
709 errorStr += mysql_error(sock);
714 row = mysql_fetch_row(res);
718 conf->password = row[1];
720 if (conf->password.empty())
722 mysql_free_result(res);
723 errorStr = "User \'" + login + "\' password is blank.";
728 if (GetInt(row[2],&conf->passive, 0) != 0)
730 mysql_free_result(res);
731 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
736 if (GetInt(row[3], &conf->disabled, 0) != 0)
738 mysql_free_result(res);
739 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
744 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
746 mysql_free_result(res);
747 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
752 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
754 mysql_free_result(res);
755 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
760 conf->tariffName = row[6];
762 if (conf->tariffName.empty())
764 mysql_free_result(res);
765 errorStr = "User \'" + login + "\' tariff is blank.";
770 conf->address = row[7];
771 conf->phone = row[8];
772 conf->email = row[9];
773 conf->note = row[10];
774 conf->realName = row[11];
775 conf->group = row[12];
777 if (GetDouble(row[13], &conf->credit, 0) != 0)
779 mysql_free_result(res);
780 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
785 conf->nextTariff = row[14];
787 for (int i = 0; i < USERDATA_NUM; i++)
789 conf->userdata[i] = row[15+i];
792 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
794 string ipStr = row[16+USERDATA_NUM];
802 mysql_free_result(res);
803 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
809 mysql_free_result(res);
814 //-----------------------------------------------------------------------------
815 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
825 for (int i = 0; i < DIR_NUM; i++)
827 sprintf(qbuf, "D%d, U%d, ", i, i);
831 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
832 FROM users WHERE login = '";
833 query += login + "'";
835 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
837 if(MysqlGetQuery(query.c_str() ,sock))
839 errorStr = "Couldn't restore UserStat(on query):\n";
840 errorStr += mysql_error(sock);
845 if (!(res=mysql_store_result(sock)))
847 errorStr = "Couldn't restore UserStat(on getting result):\n";
848 errorStr += mysql_error(sock);
853 row = mysql_fetch_row(res);
855 unsigned int startPos=0;
858 uint64_t traffU[DIR_NUM];
859 uint64_t traffD[DIR_NUM];
861 for (int i = 0; i < DIR_NUM; i++)
863 sprintf(s, "D%d", i);
864 if (GetULongLongInt(row[startPos+i*2],&traffD[i], 0) != 0)
866 mysql_free_result(res);
867 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
873 sprintf(s, "U%d", i);
874 if (GetULongLongInt(row[startPos+i*2+1], &traffU[i], 0) != 0)
876 mysql_free_result(res);
877 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
884 startPos += (2*DIR_NUM);
886 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
888 mysql_free_result(res);
889 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
894 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
896 mysql_free_result(res);
897 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
902 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
904 mysql_free_result(res);
905 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
910 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
912 mysql_free_result(res);
913 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
918 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
920 mysql_free_result(res);
921 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
926 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
928 mysql_free_result(res);
929 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
934 mysql_free_result(res);
938 //-----------------------------------------------------------------------------
939 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
944 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
945 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
946 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
947 conf.password.c_str(),
950 conf.disabledDetailStat,
952 conf.tariffName.c_str(),
953 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
954 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
955 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
956 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
957 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
958 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
960 conf.nextTariff.c_str()
963 for (int i = 0; i < USERDATA_NUM; i++)
965 strprintf(¶m, " Userdata%d='%s',", i,
966 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
970 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
976 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
979 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
982 if(MysqlSetQuery(res.c_str()))
984 errorStr = "Couldn't save user conf:\n";
985 //errorStr += mysql_error(sock);
991 //-----------------------------------------------------------------------------
992 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
997 res = "UPDATE users SET";
999 for (int i = 0; i < DIR_NUM; i++)
1001 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1004 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1008 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1009 " PassiveTime=%d, LastActivityTime=%d",
1013 stat.lastCashAddTime,
1015 stat.lastActivityTime
1019 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1022 if(MysqlSetQuery(res.c_str()))
1024 errorStr = "Couldn't save user stat:\n";
1025 // errorStr += mysql_error(sock);
1031 //-----------------------------------------------------------------------------
1032 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
1034 string res, tempStr;
1043 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1044 if (!(sock=MysqlConnect())){
1045 errorStr = "Couldn't connect to Server";
1048 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1050 errorStr = "Couldn't get table " + tempStr + ":\n";
1051 errorStr += mysql_error(sock);
1056 unsigned int num_rows = mysql_num_rows(result);
1058 mysql_free_result(result);
1062 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1063 lt->tm_mon+1, lt->tm_year+1900);
1065 if(MysqlQuery(qbuf,sock))
1067 errorStr = "Couldn't create WriteDetailedStat table:\n";
1068 errorStr += mysql_error(sock);
1074 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1078 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1079 lt->tm_mon+1, lt->tm_year+1900,
1080 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1082 if(MysqlQuery(send.c_str(),sock))
1084 errorStr = "Couldn't write log string:\n";
1085 errorStr += mysql_error(sock);
1093 //-----------------------------------------------------------------------------
1094 int MYSQL_STORE::WriteUserChgLog(const string & login,
1095 const string & admLogin,
1097 const string & paramName,
1098 const string & oldValue,
1099 const string & newValue,
1100 const string & message) const
1102 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1103 + paramName + "\' parameter changed from \'" + oldValue +
1104 "\' to \'" + newValue + "\'. " + message;
1106 return WriteLogString(userLogMsg, login);
1108 //-----------------------------------------------------------------------------
1109 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1111 string logStr = "Connect, " + inet_ntostring(ip);
1112 return WriteLogString(logStr, login);
1114 //-----------------------------------------------------------------------------
1115 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1116 const DIR_TRAFF & up,
1117 const DIR_TRAFF & down,
1118 const DIR_TRAFF & sessionUp,
1119 const DIR_TRAFF & sessionDown,
1122 const std::string & reason) const
1124 string logStr = "Disconnect, ";
1129 stringstream sscash;
1135 sssd << sessionDown;
1139 logStr += " session upload: \'";
1140 logStr += sssu.str();
1141 logStr += "\' session download: \'";
1142 logStr += sssd.str();
1143 logStr += "\' month upload: \'";
1144 logStr += ssmu.str();
1145 logStr += "\' month download: \'";
1146 logStr += ssmd.str();
1147 logStr += "\' cash: \'";
1148 logStr += sscash.str();
1151 return WriteLogString(logStr, login);
1153 //-----------------------------------------------------------------------------
1154 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1155 const string & login) const
1159 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1160 login.c_str(), month+1, year+1900);
1162 for (int i = 0; i < DIR_NUM; i++)
1164 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1167 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1171 strprintf(¶m, " cash=%f", stat.cash);
1174 if(MysqlSetQuery(res.c_str()))
1176 errorStr = "Couldn't SaveMonthStat:\n";
1177 //errorStr += mysql_error(sock);
1183 //-----------------------------------------------------------------------------*/
1184 int MYSQL_STORE::AddAdmin(const string & login) const
1186 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1188 if(MysqlSetQuery(qbuf))
1190 errorStr = "Couldn't add admin:\n";
1191 //errorStr += mysql_error(sock);
1197 //-----------------------------------------------------------------------------*/
1198 int MYSQL_STORE::DelAdmin(const string & login) const
1200 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1202 if(MysqlSetQuery(qbuf))
1204 errorStr = "Couldn't delete admin:\n";
1205 //errorStr += mysql_error(sock);
1211 //-----------------------------------------------------------------------------*/
1212 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1214 char passwordE[2 * ADM_PASSWD_LEN + 2];
1215 char pass[ADM_PASSWD_LEN + 1];
1216 char adminPass[ADM_PASSWD_LEN + 1];
1218 memset(pass, 0, sizeof(pass));
1219 memset(adminPass, 0, sizeof(adminPass));
1222 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1224 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1225 adminPass[ADM_PASSWD_LEN - 1] = 0;
1227 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1229 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1232 pass[ADM_PASSWD_LEN - 1] = 0;
1233 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1235 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1236 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1237 "WHERE login='%s' LIMIT 1",
1249 if(MysqlSetQuery(qbuf))
1251 errorStr = "Couldn't save admin:\n";
1252 //errorStr += mysql_error(sock);
1258 //-----------------------------------------------------------------------------
1259 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1261 char pass[ADM_PASSWD_LEN + 1];
1262 char password[ADM_PASSWD_LEN + 1];
1263 char passwordE[2*ADM_PASSWD_LEN + 2];
1270 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1272 if(MysqlGetQuery(qbuf,sock))
1274 errorStr = "Couldn't restore admin:\n";
1275 errorStr += mysql_error(sock);
1280 if (!(res=mysql_store_result(sock)))
1282 errorStr = "Couldn't restore admin:\n";
1283 errorStr += mysql_error(sock);
1288 if ( mysql_num_rows(res) == 0)
1290 mysql_free_result(res);
1291 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1296 row = mysql_fetch_row(res);
1303 mysql_free_result(res);
1304 errorStr = "Error in parameter password";
1309 memset(passwordE, 0, sizeof(passwordE));
1310 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1312 memset(pass, 0, sizeof(pass));
1314 if (passwordE[0] != 0)
1316 Decode21(pass, passwordE);
1317 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1319 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1321 DecodeString(password + 8*i, pass + 8*i, &ctx);
1329 ac->password = password;
1331 if (GetInt(row[2], &a, 0) == 0)
1332 ac->priv.userConf = a;
1335 mysql_free_result(res);
1336 errorStr = "Error in parameter ChgConf";
1341 if (GetInt(row[3], &a, 0) == 0)
1342 ac->priv.userPasswd = a;
1345 mysql_free_result(res);
1346 errorStr = "Error in parameter ChgPassword";
1351 if (GetInt(row[4], &a, 0) == 0)
1352 ac->priv.userStat = a;
1355 mysql_free_result(res);
1356 errorStr = "Error in parameter ChgStat";
1361 if (GetInt(row[5], &a, 0) == 0)
1362 ac->priv.userCash = a;
1365 mysql_free_result(res);
1366 errorStr = "Error in parameter ChgCash";
1371 if (GetInt(row[6], &a, 0) == 0)
1372 ac->priv.userAddDel = a;
1375 mysql_free_result(res);
1376 errorStr = "Error in parameter UsrAddDel";
1381 if (GetInt(row[7], &a, 0) == 0)
1382 ac->priv.tariffChg = a;
1385 mysql_free_result(res);
1386 errorStr = "Error in parameter ChgTariff";
1391 if (GetInt(row[8], &a, 0) == 0)
1392 ac->priv.adminChg = a;
1395 mysql_free_result(res);
1396 errorStr = "Error in parameter ChgAdmin";
1401 mysql_free_result(res);
1405 //-----------------------------------------------------------------------------
1406 int MYSQL_STORE::AddTariff(const string & name) const
1408 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1410 if(MysqlSetQuery(qbuf))
1412 errorStr = "Couldn't add tariff:\n";
1413 // errorStr += mysql_error(sock);
1419 //-----------------------------------------------------------------------------
1420 int MYSQL_STORE::DelTariff(const string & name) const
1422 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1424 if(MysqlSetQuery(qbuf))
1426 errorStr = "Couldn't delete tariff: ";
1427 // errorStr += mysql_error(sock);
1433 //-----------------------------------------------------------------------------
1434 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1439 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1441 if(MysqlGetQuery(qbuf,sock))
1443 errorStr = "Couldn't restore Tariff:\n";
1444 errorStr += mysql_error(sock);
1449 if (!(res=mysql_store_result(sock)))
1451 errorStr = "Couldn't restore Tariff:\n";
1452 errorStr += mysql_error(sock);
1458 td->tariffConf.name = tariffName;
1460 row = mysql_fetch_row(res);
1463 for (int i = 0; i<DIR_NUM; i++)
1465 strprintf(¶m, "Time%d", i);
1467 if (str.length() == 0)
1469 mysql_free_result(res);
1470 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1475 ParseTariffTimeStr(str.c_str(),
1476 td->dirPrice[i].hDay,
1477 td->dirPrice[i].mDay,
1478 td->dirPrice[i].hNight,
1479 td->dirPrice[i].mNight);
1481 strprintf(¶m, "PriceDayA%d", i);
1482 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1484 mysql_free_result(res);
1485 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1489 td->dirPrice[i].priceDayA /= (1024*1024);
1491 strprintf(¶m, "PriceDayB%d", i);
1492 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1494 mysql_free_result(res);
1495 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1499 td->dirPrice[i].priceDayB /= (1024*1024);
1501 strprintf(¶m, "PriceNightA%d", i);
1502 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1504 mysql_free_result(res);
1505 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1509 td->dirPrice[i].priceNightA /= (1024*1024);
1511 strprintf(¶m, "PriceNightB%d", i);
1512 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1514 mysql_free_result(res);
1515 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1519 td->dirPrice[i].priceNightB /= (1024*1024);
1521 strprintf(¶m, "Threshold%d", i);
1522 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1524 mysql_free_result(res);
1525 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1530 strprintf(¶m, "SinglePrice%d", i);
1531 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1533 mysql_free_result(res);
1534 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1539 strprintf(¶m, "NoDiscount%d", i);
1540 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1542 mysql_free_result(res);
1543 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1549 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1551 mysql_free_result(res);
1552 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1557 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1559 mysql_free_result(res);
1560 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1565 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1567 mysql_free_result(res);
1568 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1573 str = row[4+8*DIR_NUM];
1574 param = "TraffType";
1576 if (str.length() == 0)
1578 mysql_free_result(res);
1579 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1584 if (!strcasecmp(str.c_str(), "up"))
1585 td->tariffConf.traffType = TRAFF_UP;
1587 if (!strcasecmp(str.c_str(), "down"))
1588 td->tariffConf.traffType = TRAFF_DOWN;
1590 if (!strcasecmp(str.c_str(), "up+down"))
1591 td->tariffConf.traffType = TRAFF_UP_DOWN;
1593 if (!strcasecmp(str.c_str(), "max"))
1594 td->tariffConf.traffType = TRAFF_MAX;
1597 mysql_free_result(res);
1598 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1603 mysql_free_result(res);
1607 //-----------------------------------------------------------------------------
1608 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1612 string res="UPDATE tariffs SET";
1614 for (int i = 0; i < DIR_NUM; i++)
1616 strprintf(¶m, " PriceDayA%d=%f,", i,
1617 td.dirPrice[i].priceDayA * pt_mega);
1620 strprintf(¶m, " PriceDayB%d=%f,", i,
1621 td.dirPrice[i].priceDayB * pt_mega);
1624 strprintf(¶m, " PriceNightA%d=%f,", i,
1625 td.dirPrice[i].priceNightA * pt_mega);
1628 strprintf(¶m, " PriceNightB%d=%f,", i,
1629 td.dirPrice[i].priceNightB * pt_mega);
1632 strprintf(¶m, " Threshold%d=%d,", i,
1633 td.dirPrice[i].threshold);
1637 strprintf(¶m, " Time%d", i);
1639 strprintf(&s, "%0d:%0d-%0d:%0d",
1640 td.dirPrice[i].hDay,
1641 td.dirPrice[i].mDay,
1642 td.dirPrice[i].hNight,
1643 td.dirPrice[i].mNight);
1645 res += (param + "='" + s + "',");
1647 strprintf(¶m, " NoDiscount%d=%d,", i,
1648 td.dirPrice[i].noDiscount);
1651 strprintf(¶m, " SinglePrice%d=%d,", i,
1652 td.dirPrice[i].singlePrice);
1656 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1659 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1662 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1665 switch (td.tariffConf.traffType)
1668 res += " TraffType='up'";
1671 res += " TraffType='down'";
1674 res += " TraffType='up+down'";
1677 res += " TraffType='max'";
1680 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1683 if(MysqlSetQuery(res.c_str()))
1685 errorStr = "Couldn't save admin:\n";
1686 //errorStr += mysql_error(sock);
1692 //-----------------------------------------------------------------------------
1693 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> * statTree,
1695 const string & login) const
1697 string res, stTime, endTime, tempStr;
1704 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1712 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1714 if (!(sock=MysqlConnect())){
1719 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1721 errorStr = "Couldn't get table " + tempStr + ":\n";
1722 errorStr += mysql_error(sock);
1727 unsigned int num_rows = mysql_num_rows(result);
1729 mysql_free_result(result);
1733 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1734 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1735 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1736 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1737 lt->tm_mon+1, lt->tm_year+1900);
1739 if(MysqlQuery(qbuf,sock))
1741 errorStr = "Couldn't create WriteDetailedStat table:\n";
1742 errorStr += mysql_error(sock);
1751 lt1 = localtime(&lastStat);
1760 lt2 = localtime(&t);
1766 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1767 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1769 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1770 "day=%d,startTime='%s',endTime='%s',",
1771 lt->tm_mon+1, lt->tm_year+1900,
1779 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1780 stIter = statTree->begin();
1782 while (stIter != statTree->end())
1784 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1785 inet_ntostring(stIter->first.ip).c_str(),
1787 stIter->second.down,
1792 if( (retRes = MysqlQuery((res+tempStr).c_str(),sock)) )
1794 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1795 errorStr += mysql_error(sock);
1800 result=mysql_store_result(sock);
1802 mysql_free_result(result);
1809 //-----------------------------------------------------------------------------
1810 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1814 gettimeofday(&tv, NULL);
1816 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1818 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1820 (long long)msg->header.id
1823 if(MysqlSetQuery(qbuf))
1825 errorStr = "Couldn't add message:\n";
1826 //errorStr += mysql_error(sock);
1830 return EditMessage(*msg, login);
1832 //-----------------------------------------------------------------------------
1833 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1837 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1838 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1839 "WHERE login='%s' AND id=%lld LIMIT 1",
1841 msg.header.lastSendTime,
1842 msg.header.creationTime,
1843 msg.header.showTime,
1845 msg.header.repeatPeriod,
1846 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1848 (long long)msg.header.id
1851 if(MysqlSetQuery(res.c_str()))
1853 errorStr = "Couldn't edit message:\n";
1854 //errorStr += mysql_error(sock);
1860 //-----------------------------------------------------------------------------
1861 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1867 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1870 if(MysqlGetQuery(qbuf,sock))
1872 errorStr = "Couldn't GetMessage:\n";
1873 errorStr += mysql_error(sock);
1878 if (!(res=mysql_store_result(sock)))
1880 errorStr = "Couldn't GetMessage:\n";
1881 errorStr += mysql_error(sock);
1886 row = mysql_fetch_row(res);
1888 if(row[2]&&str2x(row[2], msg->header.type))
1890 mysql_free_result(res);
1891 errorStr = "Invalid value in message header for user: " + login;
1896 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1898 mysql_free_result(res);
1899 errorStr = "Invalid value in message header for user: " + login;
1904 if(row[4] && str2x(row[4], msg->header.creationTime))
1906 mysql_free_result(res);
1907 errorStr = "Invalid value in message header for user: " + login;
1912 if(row[5] && str2x(row[5], msg->header.showTime))
1914 mysql_free_result(res);
1915 errorStr = "Invalid value in message header for user: " + login;
1920 if(row[6] && str2x(row[6], msg->header.repeat))
1922 mysql_free_result(res);
1923 errorStr = "Invalid value in message header for user: " + login;
1928 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1930 mysql_free_result(res);
1931 errorStr = "Invalid value in message header for user: " + login;
1936 msg->header.id = id;
1939 mysql_free_result(res);
1943 //-----------------------------------------------------------------------------
1944 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1946 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1947 login.c_str(),(long long)id);
1949 if(MysqlSetQuery(qbuf))
1951 errorStr = "Couldn't delete Message:\n";
1952 //errorStr += mysql_error(sock);
1958 //-----------------------------------------------------------------------------
1959 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1964 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1966 if(MysqlGetQuery(qbuf,sock))
1968 errorStr = "Couldn't GetMessageHdrs:\n";
1969 errorStr += mysql_error(sock);
1974 if (!(res=mysql_store_result(sock)))
1976 errorStr = "Couldn't GetMessageHdrs:\n";
1977 errorStr += mysql_error(sock);
1982 unsigned int i, num_rows = mysql_num_rows(res);
1983 long long int unsigned id = 0;
1985 for (i=0; i<num_rows; i++)
1987 row = mysql_fetch_row(res);
1988 if (str2x(row[1], id))
1993 if(str2x(row[2], hdr.type))
1997 if(str2x(row[3], hdr.lastSendTime))
2001 if(str2x(row[4], hdr.creationTime))
2005 if(str2x(row[5], hdr.showTime))
2009 if(str2x(row[6], hdr.repeat))
2013 if(str2x(row[7], hdr.repeatPeriod))
2017 hdrsList->push_back(hdr);
2020 mysql_free_result(res);
2024 //-----------------------------------------------------------------------------
2026 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2029 int ret=MysqlGetQuery(Query,sock);
2033 //-----------------------------------------------------------------------------
2034 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2035 if (!(sock=MysqlConnect())) {
2038 return MysqlQuery(Query,sock);
2040 //-----------------------------------------------------------------------------
2041 MYSQL * MYSQL_STORE::MysqlConnect() const {
2043 if ( !(sock=mysql_init(NULL)) ){
2044 errorStr= "mysql init susck\n";
2047 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2048 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2051 errorStr = "Couldn't connect to mysql engine! With error:\n";
2052 errorStr += mysql_error(sock);
2056 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2057 errorStr = "Database lost !\n";
2063 //-----------------------------------------------------------------------------