11 #include "stg/user_ips.h"
12 #include "stg/user_conf.h"
13 #include "stg/user_stat.h"
14 #include "stg/blowfish.h"
15 #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 //-----------------------------------------------------------------------------
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, "user", dbUser) < 0 &&
157 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
159 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
160 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
162 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
163 ParseParam(s.moduleParams, "dbname", dbName) < 0)
165 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
166 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
171 //-----------------------------------------------------------------------------
172 const string & MYSQL_STORE_SETTINGS::GetStrError() const
176 //-----------------------------------------------------------------------------
177 string MYSQL_STORE_SETTINGS::GetDBUser() const
181 //-----------------------------------------------------------------------------
182 string MYSQL_STORE_SETTINGS::GetDBPassword() const
186 //-----------------------------------------------------------------------------
187 string MYSQL_STORE_SETTINGS::GetDBHost() const
191 //-----------------------------------------------------------------------------
192 string MYSQL_STORE_SETTINGS::GetDBName() const
196 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
199 MYSQL_STORE::MYSQL_STORE()
201 version = "mysql_store v.0.67";
203 //-----------------------------------------------------------------------------
204 MYSQL_STORE::~MYSQL_STORE()
207 //-----------------------------------------------------------------------------
208 void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
212 //-----------------------------------------------------------------------------
213 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
217 if( (ret = mysql_query(sock,sQuery)) )
219 for(i=0; i<RepitTimes; i++)
221 if( (ret = mysql_query(sock,sQuery)) )
222 ;//need to send error result
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
233 int MYSQL_STORE::ParseSettings()
235 int ret = storeSettings.ParseSettings(settings);
240 errorStr = storeSettings.GetStrError();
243 if(storeSettings.GetDBPassword().length() == 0)
245 errorStr = "Database password must be not empty. Please read Manual.";
249 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
250 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
253 errorStr = "Couldn't connect to mysql engine! With error:\n";
254 errorStr += mysql_error(&mysql);
260 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
262 string res = "CREATE DATABASE " + storeSettings.GetDBName();
264 if(MysqlQuery(res.c_str(),sock))
266 errorStr = "Couldn't create database! With error:\n";
267 errorStr += mysql_error(sock);
273 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
275 errorStr = "Couldn't select database! With error:\n";
276 errorStr += mysql_error(sock);
280 ret = CheckAllTables(sock);
284 ret = CheckAllTables(sock);
291 //-----------------------------------------------------------------------------
292 const string & MYSQL_STORE::GetStrError() const
296 //-----------------------------------------------------------------------------
297 const string & MYSQL_STORE::GetVersion() const
301 //-----------------------------------------------------------------------------
302 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
306 if (!(result=mysql_list_tables(sock,str.c_str() )))
308 errorStr = "Couldn't get tables list With error:\n";
309 errorStr += mysql_error(sock);
314 unsigned int num_rows = mysql_num_rows(result);
317 mysql_free_result(result);
319 return (num_rows == 1);
321 //-----------------------------------------------------------------------------
322 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
324 //admins-----------------------------------------------------------------------
325 if(!IsTablePresent("admins",sock))
327 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
328 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
329 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
330 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
331 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
333 if(MysqlQuery(qbuf,sock))
335 errorStr = "Couldn't create admin table list With error:\n";
336 errorStr += mysql_error(sock);
341 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
342 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
343 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
345 if(MysqlQuery(qbuf,sock))
347 errorStr = "Couldn't create default admin. With error:\n";
348 errorStr += mysql_error(sock);
354 //tariffs-----------------------------------------------------------------------
356 if(!IsTablePresent("tariffs",sock))
358 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
360 for (int i = 0; i < DIR_NUM; i++)
362 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
365 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
368 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
371 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
374 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
377 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
380 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
383 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
387 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
388 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
390 if(MysqlQuery(res.c_str(),sock))
392 errorStr = "Couldn't create tariffs table list With error:\n";
393 errorStr += mysql_error(sock);
398 res = "INSERT INTO tariffs SET name='tariff',";
400 for (int i = 0; i < DIR_NUM; i++)
402 strprintf(¶m, " NoDiscount%d=1,", i);
405 strprintf(¶m, " Threshold%d=0,", i);
408 strprintf(¶m, " Time%d='0:0-0:0',", i);
413 strprintf(¶m, " SinglePrice%d=0,", i);
419 strprintf(¶m, " PriceDayA%d=0.0,", i);
424 strprintf(¶m, " PriceDayB%d=0.0,", i);
430 strprintf(¶m, " PriceNightA%d=0.0,", i);
435 strprintf(¶m, " PriceNightB%d=0.0,", i);
440 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
441 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
442 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
444 if(MysqlQuery(res.c_str(),sock))
446 errorStr = "Couldn't create default tariff. With error:\n";
447 errorStr += mysql_error(sock);
453 //users-----------------------------------------------------------------------
454 if(!IsTablePresent("users",sock))
456 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
457 "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 '',"\
458 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
459 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
460 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
462 for (int i = 0; i < USERDATA_NUM; i++)
464 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
468 param = " CreditExpire INT(11) DEFAULT 0,";
471 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
474 for (int i = 0; i < DIR_NUM; i++)
476 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
479 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
483 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
484 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
485 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
486 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
489 if(MysqlQuery(res.c_str(),sock))
491 errorStr = "Couldn't create users table list With error:\n";
492 errorStr += mysql_error(sock);
493 errorStr += "\n\n" + res;
498 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
499 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
500 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
501 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
504 for (int i = 0; i < DIR_NUM; i++)
506 strprintf(¶m, " D%d=0,", i);
509 strprintf(¶m, " U%d=0,", i);
513 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
514 "LastCashAddTime=0, PassiveTime=0";
516 if(MysqlQuery(res.c_str(),sock))
518 errorStr = "Couldn't create default user. With error:\n";
519 errorStr += mysql_error(sock);
525 //logs-----------------------------------------------------------------------
526 if(!IsTablePresent("logs"))
528 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
532 errorStr = "Couldn't create admin table list With error:\n";
533 errorStr += mysql_error(sock);
538 //messages---------------------------------------------------------------------
539 if(!IsTablePresent("messages",sock))
541 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
542 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
543 "stgRepeat INT, repeatPeriod INT, text TEXT)");
545 if(MysqlQuery(qbuf,sock))
547 errorStr = "Couldn't create messages table. With error:\n";
548 errorStr += mysql_error(sock);
554 //month_stat-------------------------------------------------------------------
555 if(!IsTablePresent("stat",sock))
557 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
559 for (int i = 0; i < DIR_NUM; i++)
561 strprintf(¶m, " U%d BIGINT,", i);
564 strprintf(¶m, " D%d BIGINT,", i);
568 res += " cash DOUBLE, INDEX (login))";
570 if(MysqlQuery(res.c_str(),sock))
572 errorStr = "Couldn't create stat table. With error:\n";
573 errorStr += mysql_error(sock);
581 //-----------------------------------------------------------------------------
582 //-----------------------------------------------------------------------------
584 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
585 const string & table, const string & name) const
594 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
596 if(MysqlGetQuery(qbuf,sock))
598 errorStr = "Couldn't GetAllParams Query for: ";
599 errorStr += name + " - " + table + "\n";
600 errorStr += mysql_error(sock);
605 if (!(res=mysql_store_result(sock)))
607 errorStr = "Couldn't GetAllParams Results for: ";
608 errorStr += name + " - " + table + "\n";
609 errorStr += mysql_error(sock);
613 num = mysql_num_rows(res);
617 row = mysql_fetch_row(res);
618 ParamList->push_back(row[0]);
621 mysql_free_result(res);
627 //-----------------------------------------------------------------------------
628 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
630 if(GetAllParams(usersList, "users", "login"))
635 //-----------------------------------------------------------------------------
636 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
638 if(GetAllParams(adminsList, "admins", "login"))
643 //-----------------------------------------------------------------------------
644 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
646 if(GetAllParams(tariffsList, "tariffs", "name"))
651 //-----------------------------------------------------------------------------
652 int MYSQL_STORE::AddUser(const string & login) const
654 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
656 if(MysqlSetQuery(qbuf))
658 errorStr = "Couldn't add user:\n";
659 //errorStr += mysql_error(sock);
665 //-----------------------------------------------------------------------------
666 int MYSQL_STORE::DelUser(const string & login) const
668 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
670 if(MysqlSetQuery(qbuf))
672 errorStr = "Couldn't delete user:\n";
673 //errorStr += mysql_error(sock);
679 //-----------------------------------------------------------------------------
680 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
687 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
688 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
689 RealName, StgGroup, Credit, TariffChange, ";
691 for (int i = 0; i < USERDATA_NUM; i++)
693 sprintf(qbuf, "Userdata%d, ", i);
697 query += "CreditExpire, IP FROM users WHERE login='";
698 query += login + "' LIMIT 1";
700 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
702 if(MysqlGetQuery(query.c_str(),sock))
704 errorStr = "Couldn't restore Tariff(on query):\n";
705 errorStr += mysql_error(sock);
710 if (!(res=mysql_store_result(sock)))
712 errorStr = "Couldn't restore Tariff(on getting result):\n";
713 errorStr += mysql_error(sock);
718 if (mysql_num_rows(res) != 1)
720 errorStr = "User not found";
725 row = mysql_fetch_row(res);
729 conf->password = row[1];
731 if (conf->password.empty())
733 mysql_free_result(res);
734 errorStr = "User \'" + login + "\' password is blank.";
739 if (GetInt(row[2],&conf->passive, 0) != 0)
741 mysql_free_result(res);
742 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
747 if (GetInt(row[3], &conf->disabled, 0) != 0)
749 mysql_free_result(res);
750 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
755 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
757 mysql_free_result(res);
758 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
763 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
765 mysql_free_result(res);
766 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
771 conf->tariffName = row[6];
773 if (conf->tariffName.empty())
775 mysql_free_result(res);
776 errorStr = "User \'" + login + "\' tariff is blank.";
781 conf->address = row[7];
782 conf->phone = row[8];
783 conf->email = row[9];
784 conf->note = row[10];
785 conf->realName = row[11];
786 conf->group = row[12];
788 if (GetDouble(row[13], &conf->credit, 0) != 0)
790 mysql_free_result(res);
791 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
796 conf->nextTariff = row[14];
798 for (int i = 0; i < USERDATA_NUM; i++)
800 conf->userdata[i] = row[15+i];
803 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
805 string ipStr = row[16+USERDATA_NUM];
811 catch (const string & s)
813 mysql_free_result(res);
814 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
820 mysql_free_result(res);
825 //-----------------------------------------------------------------------------
826 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
836 for (int i = 0; i < DIR_NUM; i++)
838 sprintf(qbuf, "D%d, U%d, ", i, i);
842 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
843 FROM users WHERE login = '";
844 query += login + "'";
846 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
848 if(MysqlGetQuery(query.c_str() ,sock))
850 errorStr = "Couldn't restore UserStat(on query):\n";
851 errorStr += mysql_error(sock);
856 if (!(res=mysql_store_result(sock)))
858 errorStr = "Couldn't restore UserStat(on getting result):\n";
859 errorStr += mysql_error(sock);
864 row = mysql_fetch_row(res);
866 unsigned int startPos=0;
870 for (int i = 0; i < DIR_NUM; i++)
873 sprintf(s, "D%d", i);
874 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
876 mysql_free_result(res);
877 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
881 stat->down[i] = traff;
883 sprintf(s, "U%d", i);
884 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
886 mysql_free_result(res);
887 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
894 startPos += (2*DIR_NUM);
896 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
898 mysql_free_result(res);
899 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
904 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
906 mysql_free_result(res);
907 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
912 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
914 mysql_free_result(res);
915 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
920 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
922 mysql_free_result(res);
923 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
928 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
930 mysql_free_result(res);
931 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
936 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
938 mysql_free_result(res);
939 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
944 mysql_free_result(res);
948 //-----------------------------------------------------------------------------
949 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
954 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
955 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
956 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
957 conf.password.c_str(),
960 conf.disabledDetailStat,
962 conf.tariffName.c_str(),
963 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
964 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
965 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
966 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
967 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
968 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
970 conf.nextTariff.c_str()
973 for (int i = 0; i < USERDATA_NUM; i++)
975 strprintf(¶m, " Userdata%d='%s',", i,
976 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
980 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
986 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
989 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
992 if(MysqlSetQuery(res.c_str()))
994 errorStr = "Couldn't save user conf:\n";
995 //errorStr += mysql_error(sock);
1001 //-----------------------------------------------------------------------------
1002 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1007 res = "UPDATE users SET";
1009 for (int i = 0; i < DIR_NUM; i++)
1011 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1014 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1018 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1019 " PassiveTime=%d, LastActivityTime=%d",
1023 stat.lastCashAddTime,
1025 stat.lastActivityTime
1029 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1032 if(MysqlSetQuery(res.c_str()))
1034 errorStr = "Couldn't save user stat:\n";
1035 // errorStr += mysql_error(sock);
1041 //-----------------------------------------------------------------------------
1042 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
1044 string res, tempStr;
1053 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1054 if (!(sock=MysqlConnect())){
1055 errorStr = "Couldn't connect to Server";
1058 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1060 errorStr = "Couldn't get table " + tempStr + ":\n";
1061 errorStr += mysql_error(sock);
1066 unsigned int num_rows = mysql_num_rows(result);
1068 mysql_free_result(result);
1072 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1073 lt->tm_mon+1, lt->tm_year+1900);
1075 if(MysqlQuery(qbuf,sock))
1077 errorStr = "Couldn't create WriteDetailedStat table:\n";
1078 errorStr += mysql_error(sock);
1084 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1088 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1089 lt->tm_mon+1, lt->tm_year+1900,
1090 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1092 if(MysqlQuery(send.c_str(),sock))
1094 errorStr = "Couldn't write log string:\n";
1095 errorStr += mysql_error(sock);
1103 //-----------------------------------------------------------------------------
1104 int MYSQL_STORE::WriteUserChgLog(const string & login,
1105 const string & admLogin,
1107 const string & paramName,
1108 const string & oldValue,
1109 const string & newValue,
1110 const string & message) const
1112 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1113 + paramName + "\' parameter changed from \'" + oldValue +
1114 "\' to \'" + newValue + "\'. " + message;
1116 return WriteLogString(userLogMsg, login);
1118 //-----------------------------------------------------------------------------
1119 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1121 string logStr = "Connect, " + inet_ntostring(ip);
1122 return WriteLogString(logStr, login);
1124 //-----------------------------------------------------------------------------
1125 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1126 const DIR_TRAFF & up,
1127 const DIR_TRAFF & down,
1128 const DIR_TRAFF & sessionUp,
1129 const DIR_TRAFF & sessionDown,
1132 const std::string & reason) const
1134 string logStr = "Disconnect, ";
1139 stringstream sscash;
1145 sssd << sessionDown;
1149 logStr += " session upload: \'";
1150 logStr += sssu.str();
1151 logStr += "\' session download: \'";
1152 logStr += sssd.str();
1153 logStr += "\' month upload: \'";
1154 logStr += ssmu.str();
1155 logStr += "\' month download: \'";
1156 logStr += ssmd.str();
1157 logStr += "\' cash: \'";
1158 logStr += sscash.str();
1161 return WriteLogString(logStr, login);
1163 //-----------------------------------------------------------------------------
1164 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1165 const string & login) const
1169 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1170 login.c_str(), month+1, year+1900);
1172 for (int i = 0; i < DIR_NUM; i++)
1174 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1177 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1181 strprintf(¶m, " cash=%f", stat.cash);
1184 if(MysqlSetQuery(res.c_str()))
1186 errorStr = "Couldn't SaveMonthStat:\n";
1187 //errorStr += mysql_error(sock);
1193 //-----------------------------------------------------------------------------*/
1194 int MYSQL_STORE::AddAdmin(const string & login) const
1196 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1198 if(MysqlSetQuery(qbuf))
1200 errorStr = "Couldn't add admin:\n";
1201 //errorStr += mysql_error(sock);
1207 //-----------------------------------------------------------------------------*/
1208 int MYSQL_STORE::DelAdmin(const string & login) const
1210 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1212 if(MysqlSetQuery(qbuf))
1214 errorStr = "Couldn't delete admin:\n";
1215 //errorStr += mysql_error(sock);
1221 //-----------------------------------------------------------------------------*/
1222 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1224 char passwordE[2 * ADM_PASSWD_LEN + 2];
1225 char pass[ADM_PASSWD_LEN + 1];
1226 char adminPass[ADM_PASSWD_LEN + 1];
1228 memset(pass, 0, sizeof(pass));
1229 memset(adminPass, 0, sizeof(adminPass));
1232 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1234 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1235 adminPass[ADM_PASSWD_LEN - 1] = 0;
1237 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1239 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1242 pass[ADM_PASSWD_LEN - 1] = 0;
1243 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1245 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1246 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1247 "WHERE login='%s' LIMIT 1",
1259 if(MysqlSetQuery(qbuf))
1261 errorStr = "Couldn't save admin:\n";
1262 //errorStr += mysql_error(sock);
1268 //-----------------------------------------------------------------------------
1269 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1271 char pass[ADM_PASSWD_LEN + 1];
1272 char password[ADM_PASSWD_LEN + 1];
1273 char passwordE[2*ADM_PASSWD_LEN + 2];
1276 memset(pass, 0, sizeof(pass));
1277 memset(password, 0, sizeof(password));
1278 memset(passwordE, 0, sizeof(passwordE));
1284 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1286 if(MysqlGetQuery(qbuf,sock))
1288 errorStr = "Couldn't restore admin:\n";
1289 errorStr += mysql_error(sock);
1294 if (!(res=mysql_store_result(sock)))
1296 errorStr = "Couldn't restore admin:\n";
1297 errorStr += mysql_error(sock);
1302 if ( mysql_num_rows(res) == 0)
1304 mysql_free_result(res);
1305 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1310 row = mysql_fetch_row(res);
1317 mysql_free_result(res);
1318 errorStr = "Error in parameter password";
1323 memset(passwordE, 0, sizeof(passwordE));
1324 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1326 memset(pass, 0, sizeof(pass));
1328 if (passwordE[0] != 0)
1330 Decode21(pass, passwordE);
1331 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1333 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1335 DecodeString(password + 8*i, pass + 8*i, &ctx);
1343 ac->password = password;
1345 if (GetInt(row[2], &a, 0) == 0)
1346 ac->priv.userConf = a;
1349 mysql_free_result(res);
1350 errorStr = "Error in parameter ChgConf";
1355 if (GetInt(row[3], &a, 0) == 0)
1356 ac->priv.userPasswd = a;
1359 mysql_free_result(res);
1360 errorStr = "Error in parameter ChgPassword";
1365 if (GetInt(row[4], &a, 0) == 0)
1366 ac->priv.userStat = a;
1369 mysql_free_result(res);
1370 errorStr = "Error in parameter ChgStat";
1375 if (GetInt(row[5], &a, 0) == 0)
1376 ac->priv.userCash = a;
1379 mysql_free_result(res);
1380 errorStr = "Error in parameter ChgCash";
1385 if (GetInt(row[6], &a, 0) == 0)
1386 ac->priv.userAddDel = a;
1389 mysql_free_result(res);
1390 errorStr = "Error in parameter UsrAddDel";
1395 if (GetInt(row[7], &a, 0) == 0)
1396 ac->priv.tariffChg = a;
1399 mysql_free_result(res);
1400 errorStr = "Error in parameter ChgTariff";
1405 if (GetInt(row[8], &a, 0) == 0)
1406 ac->priv.adminChg = a;
1409 mysql_free_result(res);
1410 errorStr = "Error in parameter ChgAdmin";
1415 mysql_free_result(res);
1419 //-----------------------------------------------------------------------------
1420 int MYSQL_STORE::AddTariff(const string & name) const
1422 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1424 if(MysqlSetQuery(qbuf))
1426 errorStr = "Couldn't add tariff:\n";
1427 // errorStr += mysql_error(sock);
1433 //-----------------------------------------------------------------------------
1434 int MYSQL_STORE::DelTariff(const string & name) const
1436 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1438 if(MysqlSetQuery(qbuf))
1440 errorStr = "Couldn't delete tariff: ";
1441 // errorStr += mysql_error(sock);
1447 //-----------------------------------------------------------------------------
1448 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1453 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1455 if(MysqlGetQuery(qbuf,sock))
1457 errorStr = "Couldn't restore Tariff:\n";
1458 errorStr += mysql_error(sock);
1463 if (!(res=mysql_store_result(sock)))
1465 errorStr = "Couldn't restore Tariff:\n";
1466 errorStr += mysql_error(sock);
1472 td->tariffConf.name = tariffName;
1474 row = mysql_fetch_row(res);
1477 for (int i = 0; i<DIR_NUM; i++)
1479 strprintf(¶m, "Time%d", i);
1481 if (str.length() == 0)
1483 mysql_free_result(res);
1484 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1489 ParseTariffTimeStr(str.c_str(),
1490 td->dirPrice[i].hDay,
1491 td->dirPrice[i].mDay,
1492 td->dirPrice[i].hNight,
1493 td->dirPrice[i].mNight);
1495 strprintf(¶m, "PriceDayA%d", i);
1496 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1498 mysql_free_result(res);
1499 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1503 td->dirPrice[i].priceDayA /= (1024*1024);
1505 strprintf(¶m, "PriceDayB%d", i);
1506 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1508 mysql_free_result(res);
1509 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1513 td->dirPrice[i].priceDayB /= (1024*1024);
1515 strprintf(¶m, "PriceNightA%d", i);
1516 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1518 mysql_free_result(res);
1519 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1523 td->dirPrice[i].priceNightA /= (1024*1024);
1525 strprintf(¶m, "PriceNightB%d", i);
1526 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1528 mysql_free_result(res);
1529 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1533 td->dirPrice[i].priceNightB /= (1024*1024);
1535 strprintf(¶m, "Threshold%d", i);
1536 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1538 mysql_free_result(res);
1539 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1544 strprintf(¶m, "SinglePrice%d", i);
1545 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1547 mysql_free_result(res);
1548 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1553 strprintf(¶m, "NoDiscount%d", i);
1554 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1556 mysql_free_result(res);
1557 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1563 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1565 mysql_free_result(res);
1566 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1571 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1573 mysql_free_result(res);
1574 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1579 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1581 mysql_free_result(res);
1582 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1587 str = row[4+8*DIR_NUM];
1588 param = "TraffType";
1590 if (str.length() == 0)
1592 mysql_free_result(res);
1593 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1598 if (!strcasecmp(str.c_str(), "up"))
1599 td->tariffConf.traffType = TRAFF_UP;
1601 if (!strcasecmp(str.c_str(), "down"))
1602 td->tariffConf.traffType = TRAFF_DOWN;
1604 if (!strcasecmp(str.c_str(), "up+down"))
1605 td->tariffConf.traffType = TRAFF_UP_DOWN;
1607 if (!strcasecmp(str.c_str(), "max"))
1608 td->tariffConf.traffType = TRAFF_MAX;
1611 mysql_free_result(res);
1612 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1617 mysql_free_result(res);
1621 //-----------------------------------------------------------------------------
1622 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1626 string res="UPDATE tariffs SET";
1628 for (int i = 0; i < DIR_NUM; i++)
1630 strprintf(¶m, " PriceDayA%d=%f,", i,
1631 td.dirPrice[i].priceDayA * pt_mega);
1634 strprintf(¶m, " PriceDayB%d=%f,", i,
1635 td.dirPrice[i].priceDayB * pt_mega);
1638 strprintf(¶m, " PriceNightA%d=%f,", i,
1639 td.dirPrice[i].priceNightA * pt_mega);
1642 strprintf(¶m, " PriceNightB%d=%f,", i,
1643 td.dirPrice[i].priceNightB * pt_mega);
1646 strprintf(¶m, " Threshold%d=%d,", i,
1647 td.dirPrice[i].threshold);
1651 strprintf(¶m, " Time%d", i);
1653 strprintf(&s, "%0d:%0d-%0d:%0d",
1654 td.dirPrice[i].hDay,
1655 td.dirPrice[i].mDay,
1656 td.dirPrice[i].hNight,
1657 td.dirPrice[i].mNight);
1659 res += (param + "='" + s + "',");
1661 strprintf(¶m, " NoDiscount%d=%d,", i,
1662 td.dirPrice[i].noDiscount);
1665 strprintf(¶m, " SinglePrice%d=%d,", i,
1666 td.dirPrice[i].singlePrice);
1670 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1673 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1676 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1679 switch (td.tariffConf.traffType)
1682 res += " TraffType='up'";
1685 res += " TraffType='down'";
1688 res += " TraffType='up+down'";
1691 res += " TraffType='max'";
1694 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1697 if(MysqlSetQuery(res.c_str()))
1699 errorStr = "Couldn't save admin:\n";
1700 //errorStr += mysql_error(sock);
1706 //-----------------------------------------------------------------------------
1707 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1709 const string & login) const
1711 string res, stTime, endTime, tempStr;
1718 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1726 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1728 if (!(sock=MysqlConnect())){
1733 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1735 errorStr = "Couldn't get table " + tempStr + ":\n";
1736 errorStr += mysql_error(sock);
1741 unsigned int num_rows = mysql_num_rows(result);
1743 mysql_free_result(result);
1747 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1748 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1749 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1750 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1751 lt->tm_mon+1, lt->tm_year+1900);
1753 if(MysqlQuery(qbuf,sock))
1755 errorStr = "Couldn't create WriteDetailedStat table:\n";
1756 errorStr += mysql_error(sock);
1765 lt1 = localtime(&lastStat);
1774 lt2 = localtime(&t);
1780 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1781 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1783 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1784 "day=%d,startTime='%s',endTime='%s',",
1785 lt->tm_mon+1, lt->tm_year+1900,
1792 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1793 stIter = statTree.begin();
1795 while (stIter != statTree.end())
1797 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1798 inet_ntostring(stIter->first.ip).c_str(),
1800 stIter->second.down,
1805 if( MysqlQuery((res+tempStr).c_str(),sock) )
1807 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1808 errorStr += mysql_error(sock);
1813 result=mysql_store_result(sock);
1815 mysql_free_result(result);
1822 //-----------------------------------------------------------------------------
1823 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1827 gettimeofday(&tv, NULL);
1829 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1831 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1833 (long long)msg->header.id
1836 if(MysqlSetQuery(qbuf))
1838 errorStr = "Couldn't add message:\n";
1839 //errorStr += mysql_error(sock);
1843 return EditMessage(*msg, login);
1845 //-----------------------------------------------------------------------------
1846 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1850 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1851 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1852 "WHERE login='%s' AND id=%lld LIMIT 1",
1854 msg.header.lastSendTime,
1855 msg.header.creationTime,
1856 msg.header.showTime,
1858 msg.header.repeatPeriod,
1859 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1861 (long long)msg.header.id
1864 if(MysqlSetQuery(res.c_str()))
1866 errorStr = "Couldn't edit message:\n";
1867 //errorStr += mysql_error(sock);
1873 //-----------------------------------------------------------------------------
1874 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1880 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1883 if(MysqlGetQuery(qbuf,sock))
1885 errorStr = "Couldn't GetMessage:\n";
1886 errorStr += mysql_error(sock);
1891 if (!(res=mysql_store_result(sock)))
1893 errorStr = "Couldn't GetMessage:\n";
1894 errorStr += mysql_error(sock);
1899 row = mysql_fetch_row(res);
1901 if(row[2]&&str2x(row[2], msg->header.type))
1903 mysql_free_result(res);
1904 errorStr = "Invalid value in message header for user: " + login;
1909 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1911 mysql_free_result(res);
1912 errorStr = "Invalid value in message header for user: " + login;
1917 if(row[4] && str2x(row[4], msg->header.creationTime))
1919 mysql_free_result(res);
1920 errorStr = "Invalid value in message header for user: " + login;
1925 if(row[5] && str2x(row[5], msg->header.showTime))
1927 mysql_free_result(res);
1928 errorStr = "Invalid value in message header for user: " + login;
1933 if(row[6] && str2x(row[6], msg->header.repeat))
1935 mysql_free_result(res);
1936 errorStr = "Invalid value in message header for user: " + login;
1941 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1943 mysql_free_result(res);
1944 errorStr = "Invalid value in message header for user: " + login;
1949 msg->header.id = id;
1952 mysql_free_result(res);
1956 //-----------------------------------------------------------------------------
1957 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1959 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1960 login.c_str(),(long long)id);
1962 if(MysqlSetQuery(qbuf))
1964 errorStr = "Couldn't delete Message:\n";
1965 //errorStr += mysql_error(sock);
1971 //-----------------------------------------------------------------------------
1972 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1977 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1979 if(MysqlGetQuery(qbuf,sock))
1981 errorStr = "Couldn't GetMessageHdrs:\n";
1982 errorStr += mysql_error(sock);
1987 if (!(res=mysql_store_result(sock)))
1989 errorStr = "Couldn't GetMessageHdrs:\n";
1990 errorStr += mysql_error(sock);
1995 unsigned int i, num_rows = mysql_num_rows(res);
1996 long long int unsigned id = 0;
1998 for (i=0; i<num_rows; i++)
2000 row = mysql_fetch_row(res);
2001 if (str2x(row[1], id))
2006 if(str2x(row[2], hdr.type))
2010 if(str2x(row[3], hdr.lastSendTime))
2014 if(str2x(row[4], hdr.creationTime))
2018 if(str2x(row[5], hdr.showTime))
2022 if(str2x(row[6], hdr.repeat))
2026 if(str2x(row[7], hdr.repeatPeriod))
2030 hdrsList->push_back(hdr);
2033 mysql_free_result(res);
2037 //-----------------------------------------------------------------------------
2039 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2042 int ret=MysqlGetQuery(Query,sock);
2046 //-----------------------------------------------------------------------------
2047 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2048 if (!(sock=MysqlConnect())) {
2051 return MysqlQuery(Query,sock);
2053 //-----------------------------------------------------------------------------
2054 MYSQL * MYSQL_STORE::MysqlConnect() const {
2056 if ( !(sock=mysql_init(NULL)) ){
2057 errorStr= "mysql init susck\n";
2060 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2061 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2064 errorStr = "Couldn't connect to mysql engine! With error:\n";
2065 errorStr += mysql_error(sock);
2069 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2070 errorStr = "Database lost !\n";
2076 //-----------------------------------------------------------------------------