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')";
373 if(MysqlQuery(res.c_str(),sock))
375 errorStr = "Couldn't create tariffs table list With error:\n";
376 errorStr += mysql_error(sock);
381 res = "INSERT INTO tariffs SET name='tariff',";
383 for (int i = 0; i < DIR_NUM; i++)
385 strprintf(¶m, " NoDiscount%d=1,", i);
388 strprintf(¶m, " Threshold%d=0,", i);
391 strprintf(¶m, " Time%d='0:0-0:0',", i);
396 strprintf(¶m, " SinglePrice%d=0,", i);
402 strprintf(¶m, " PriceDayA%d=0.0,", i);
407 strprintf(¶m, " PriceDayB%d=0.0,", i);
413 strprintf(¶m, " PriceNightA%d=0.0,", i);
418 strprintf(¶m, " PriceNightB%d=0.0,", i);
423 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
424 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
425 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'";
427 if(MysqlQuery(res.c_str(),sock))
429 errorStr = "Couldn't create default tariff. With error:\n";
430 errorStr += mysql_error(sock);
435 sprintf(qbuf,"UPDATE info SET version=1");
437 if(MysqlQuery(qbuf,sock))
439 errorStr = "Couldn't write default version. With error:\n";
440 errorStr += mysql_error(sock);
447 //users-----------------------------------------------------------------------
448 if(!IsTablePresent("users",sock))
450 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
451 "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 '',"\
452 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
453 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
454 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
456 for (int i = 0; i < USERDATA_NUM; i++)
458 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
462 param = " CreditExpire INT(11) DEFAULT 0,";
465 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
468 for (int i = 0; i < DIR_NUM; i++)
470 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
473 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
477 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
478 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
479 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
480 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
483 if(MysqlQuery(res.c_str(),sock))
485 errorStr = "Couldn't create users table list With error:\n";
486 errorStr += mysql_error(sock);
487 errorStr += "\n\n" + res;
492 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
493 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
494 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
495 "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
497 for (int i = 0; i < USERDATA_NUM; i++)
499 strprintf(¶m, " Userdata%d='',", i);
503 for (int i = 0; i < DIR_NUM; i++)
505 strprintf(¶m, " D%d=0,", i);
508 strprintf(¶m, " U%d=0,", i);
512 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
513 "LastCashAddTime=0, PassiveTime=0";
515 if(MysqlQuery(res.c_str(),sock))
517 errorStr = "Couldn't create default user. With error:\n";
518 errorStr += mysql_error(sock);
524 //logs-----------------------------------------------------------------------
525 if(!IsTablePresent("logs"))
527 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
531 errorStr = "Couldn't create admin table list With error:\n";
532 errorStr += mysql_error(sock);
537 //messages---------------------------------------------------------------------
538 if(!IsTablePresent("messages",sock))
540 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
541 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
542 "stgRepeat INT, repeatPeriod INT, text TEXT)");
544 if(MysqlQuery(qbuf,sock))
546 errorStr = "Couldn't create messages table. With error:\n";
547 errorStr += mysql_error(sock);
553 //month_stat-------------------------------------------------------------------
554 if(!IsTablePresent("stat",sock))
556 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
558 for (int i = 0; i < DIR_NUM; i++)
560 strprintf(¶m, " U%d BIGINT,", i);
563 strprintf(¶m, " D%d BIGINT,", i);
567 res += " cash DOUBLE, INDEX (login))";
569 if(MysqlQuery(res.c_str(),sock))
571 errorStr = "Couldn't create stat table. With error:\n";
572 errorStr += mysql_error(sock);
580 //-----------------------------------------------------------------------------
581 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
583 if (schemaVersion < 1)
585 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
587 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
588 errorStr += mysql_error(sock);
592 if (MysqlQuery("UPDATE info SET version = 1", sock))
594 errorStr = "Couldn't update DB schema version to 1. With error:\n";
595 errorStr += mysql_error(sock);
600 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
604 //-----------------------------------------------------------------------------
606 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList,
607 const std::string & table, const std::string & name) const
616 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
618 if(MysqlGetQuery(qbuf,sock))
620 errorStr = "Couldn't GetAllParams Query for: ";
621 errorStr += name + " - " + table + "\n";
622 errorStr += mysql_error(sock);
627 if (!(res=mysql_store_result(sock)))
629 errorStr = "Couldn't GetAllParams Results for: ";
630 errorStr += name + " - " + table + "\n";
631 errorStr += mysql_error(sock);
635 num = mysql_num_rows(res);
637 for(i = 0; i < num; i++)
639 row = mysql_fetch_row(res);
640 ParamList->push_back(row[0]);
643 mysql_free_result(res);
649 //-----------------------------------------------------------------------------
650 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
652 if(GetAllParams(usersList, "users", "login"))
657 //-----------------------------------------------------------------------------
658 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
660 if(GetAllParams(adminsList, "admins", "login"))
665 //-----------------------------------------------------------------------------
666 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
668 if(GetAllParams(tariffsList, "tariffs", "name"))
673 //-----------------------------------------------------------------------------
674 int MYSQL_STORE::AddUser(const std::string & login) const
676 std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
678 for (int i = 0; i < USERDATA_NUM; i++)
679 query += ",Userdata" + x2str(i) + "=''";
681 if(MysqlSetQuery(query.c_str()))
683 errorStr = "Couldn't add user:\n";
684 //errorStr += mysql_error(sock);
690 //-----------------------------------------------------------------------------
691 int MYSQL_STORE::DelUser(const std::string & login) const
693 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
695 if(MysqlSetQuery(qbuf))
697 errorStr = "Couldn't delete user:\n";
698 //errorStr += mysql_error(sock);
704 //-----------------------------------------------------------------------------
705 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
712 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
713 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
714 RealName, StgGroup, Credit, TariffChange, ";
716 for (int i = 0; i < USERDATA_NUM; i++)
718 sprintf(qbuf, "Userdata%d, ", i);
722 query += "CreditExpire, IP FROM users WHERE login='";
723 query += login + "' LIMIT 1";
725 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
727 if(MysqlGetQuery(query.c_str(),sock))
729 errorStr = "Couldn't restore Tariff(on query):\n";
730 errorStr += mysql_error(sock);
735 if (!(res=mysql_store_result(sock)))
737 errorStr = "Couldn't restore Tariff(on getting result):\n";
738 errorStr += mysql_error(sock);
743 if (mysql_num_rows(res) != 1)
745 errorStr = "User not found";
750 row = mysql_fetch_row(res);
752 conf->password = row[1];
754 if (conf->password.empty())
756 mysql_free_result(res);
757 errorStr = "User \'" + login + "\' password is blank.";
762 if (GetInt(row[2],&conf->passive) != 0)
764 mysql_free_result(res);
765 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
770 if (GetInt(row[3], &conf->disabled) != 0)
772 mysql_free_result(res);
773 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
778 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
780 mysql_free_result(res);
781 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
786 if (GetInt(row[5], &conf->alwaysOnline) != 0)
788 mysql_free_result(res);
789 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
794 conf->tariffName = row[6];
796 if (conf->tariffName.empty())
798 mysql_free_result(res);
799 errorStr = "User \'" + login + "\' tariff is blank.";
804 conf->address = row[7];
805 conf->phone = row[8];
806 conf->email = row[9];
807 conf->note = row[10];
808 conf->realName = row[11];
809 conf->group = row[12];
811 if (GetDouble(row[13], &conf->credit, 0) != 0)
813 mysql_free_result(res);
814 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
819 conf->nextTariff = row[14];
821 for (int i = 0; i < USERDATA_NUM; i++)
823 conf->userdata[i] = row[15+i];
826 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
828 std::string ipStr = row[16+USERDATA_NUM];
834 catch (const std::string & s)
836 mysql_free_result(res);
837 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
843 mysql_free_result(res);
848 //-----------------------------------------------------------------------------
849 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
859 for (int i = 0; i < DIR_NUM; i++)
861 sprintf(qbuf, "D%d, U%d, ", i, i);
865 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
866 FROM users WHERE login = '";
867 query += login + "'";
869 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
871 if(MysqlGetQuery(query.c_str() ,sock))
873 errorStr = "Couldn't restore UserStat(on query):\n";
874 errorStr += mysql_error(sock);
879 if (!(res=mysql_store_result(sock)))
881 errorStr = "Couldn't restore UserStat(on getting result):\n";
882 errorStr += mysql_error(sock);
887 row = mysql_fetch_row(res);
889 unsigned int startPos=0;
893 for (int i = 0; i < DIR_NUM; i++)
896 sprintf(s, "D%d", i);
897 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
899 mysql_free_result(res);
900 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
904 stat->monthDown[i] = traff;
906 sprintf(s, "U%d", i);
907 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
909 mysql_free_result(res);
910 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
914 stat->monthUp[i] = traff;
917 startPos += (2*DIR_NUM);
919 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
921 mysql_free_result(res);
922 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
927 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
929 mysql_free_result(res);
930 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
935 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
937 mysql_free_result(res);
938 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
943 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
945 mysql_free_result(res);
946 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
951 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
953 mysql_free_result(res);
954 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
959 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
961 mysql_free_result(res);
962 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
967 mysql_free_result(res);
971 //-----------------------------------------------------------------------------
972 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
977 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
978 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
979 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
980 conf.password.c_str(),
983 conf.disabledDetailStat,
985 conf.tariffName.c_str(),
986 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
987 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
988 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
989 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
990 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
991 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
993 conf.nextTariff.c_str()
996 for (int i = 0; i < USERDATA_NUM; i++)
998 strprintf(¶m, " Userdata%d='%s',", i,
999 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1003 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1006 std::ostringstream ipStr;
1009 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1012 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1015 if(MysqlSetQuery(res.c_str()))
1017 errorStr = "Couldn't save user conf:\n";
1018 //errorStr += mysql_error(sock);
1024 //-----------------------------------------------------------------------------
1025 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
1030 res = "UPDATE users SET";
1032 for (int i = 0; i < DIR_NUM; i++)
1034 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1037 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1041 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1042 " PassiveTime=%d, LastActivityTime=%d",
1046 stat.lastCashAddTime,
1048 stat.lastActivityTime
1052 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1055 if(MysqlSetQuery(res.c_str()))
1057 errorStr = "Couldn't save user stat:\n";
1058 // errorStr += mysql_error(sock);
1064 //-----------------------------------------------------------------------------
1065 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
1067 std::string res, tempStr;
1076 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1077 if (!(sock=MysqlConnect())){
1078 errorStr = "Couldn't connect to Server";
1081 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1083 errorStr = "Couldn't get table " + tempStr + ":\n";
1084 errorStr += mysql_error(sock);
1089 my_ulonglong num_rows = mysql_num_rows(result);
1091 mysql_free_result(result);
1095 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1096 lt->tm_mon+1, lt->tm_year+1900);
1098 if(MysqlQuery(qbuf,sock))
1100 errorStr = "Couldn't create WriteDetailedStat table:\n";
1101 errorStr += mysql_error(sock);
1107 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1111 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1112 lt->tm_mon+1, lt->tm_year+1900,
1113 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1115 if(MysqlQuery(send.c_str(),sock))
1117 errorStr = "Couldn't write log string:\n";
1118 errorStr += mysql_error(sock);
1126 //-----------------------------------------------------------------------------
1127 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
1128 const std::string & admLogin,
1130 const std::string & paramName,
1131 const std::string & oldValue,
1132 const std::string & newValue,
1133 const std::string & message) const
1135 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1136 + paramName + "\' parameter changed from \'" + oldValue +
1137 "\' to \'" + newValue + "\'. " + message;
1139 return WriteLogString(userLogMsg, login);
1141 //-----------------------------------------------------------------------------
1142 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1144 std::string logStr = "Connect, " + inet_ntostring(ip);
1145 return WriteLogString(logStr, login);
1147 //-----------------------------------------------------------------------------
1148 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
1149 const DIR_TRAFF & up,
1150 const DIR_TRAFF & down,
1151 const DIR_TRAFF & sessionUp,
1152 const DIR_TRAFF & sessionDown,
1155 const std::string & /*reason*/) const
1157 std::string logStr = "Disconnect, ";
1158 std::ostringstream sssu;
1159 std::ostringstream sssd;
1160 std::ostringstream ssmu;
1161 std::ostringstream ssmd;
1162 std::ostringstream sscash;
1168 sssd << sessionDown;
1172 logStr += " session upload: \'";
1173 logStr += sssu.str();
1174 logStr += "\' session download: \'";
1175 logStr += sssd.str();
1176 logStr += "\' month upload: \'";
1177 logStr += ssmu.str();
1178 logStr += "\' month download: \'";
1179 logStr += ssmd.str();
1180 logStr += "\' cash: \'";
1181 logStr += sscash.str();
1184 return WriteLogString(logStr, login);
1186 //-----------------------------------------------------------------------------
1187 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1188 const std::string & login) const
1190 std::string param, res;
1192 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1193 login.c_str(), month+1, year+1900);
1195 for (int i = 0; i < DIR_NUM; i++)
1197 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1200 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1204 strprintf(¶m, " cash=%f", stat.cash);
1207 if(MysqlSetQuery(res.c_str()))
1209 errorStr = "Couldn't SaveMonthStat:\n";
1210 //errorStr += mysql_error(sock);
1216 //-----------------------------------------------------------------------------*/
1217 int MYSQL_STORE::AddAdmin(const std::string & login) const
1219 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1221 if(MysqlSetQuery(qbuf))
1223 errorStr = "Couldn't add admin:\n";
1224 //errorStr += mysql_error(sock);
1230 //-----------------------------------------------------------------------------*/
1231 int MYSQL_STORE::DelAdmin(const std::string & login) const
1233 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1235 if(MysqlSetQuery(qbuf))
1237 errorStr = "Couldn't delete admin:\n";
1238 //errorStr += mysql_error(sock);
1244 //-----------------------------------------------------------------------------*/
1245 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1247 char passwordE[2 * ADM_PASSWD_LEN + 2];
1248 char pass[ADM_PASSWD_LEN + 1];
1249 char adminPass[ADM_PASSWD_LEN + 1];
1251 memset(pass, 0, sizeof(pass));
1252 memset(adminPass, 0, sizeof(adminPass));
1255 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1257 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1258 adminPass[ADM_PASSWD_LEN - 1] = 0;
1260 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1262 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1265 pass[ADM_PASSWD_LEN - 1] = 0;
1266 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1268 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1269 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1270 "WHERE login='%s' LIMIT 1",
1282 if(MysqlSetQuery(qbuf))
1284 errorStr = "Couldn't save admin:\n";
1285 //errorStr += mysql_error(sock);
1291 //-----------------------------------------------------------------------------
1292 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1294 char pass[ADM_PASSWD_LEN + 1];
1295 char password[ADM_PASSWD_LEN + 1];
1296 char passwordE[2*ADM_PASSWD_LEN + 2];
1299 memset(password, 0, sizeof(password));
1305 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1307 if(MysqlGetQuery(qbuf,sock))
1309 errorStr = "Couldn't restore admin:\n";
1310 errorStr += mysql_error(sock);
1315 if (!(res=mysql_store_result(sock)))
1317 errorStr = "Couldn't restore admin:\n";
1318 errorStr += mysql_error(sock);
1323 if ( mysql_num_rows(res) == 0)
1325 mysql_free_result(res);
1326 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1331 row = mysql_fetch_row(res);
1337 mysql_free_result(res);
1338 errorStr = "Error in parameter password";
1343 memset(passwordE, 0, sizeof(passwordE));
1344 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1346 memset(pass, 0, sizeof(pass));
1348 if (passwordE[0] != 0)
1350 Decode21(pass, passwordE);
1351 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1353 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1355 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1363 ac->password = password;
1367 if (GetInt(row[2], &a) == 0)
1368 ac->priv.userConf = a;
1371 mysql_free_result(res);
1372 errorStr = "Error in parameter ChgConf";
1377 if (GetInt(row[3], &a) == 0)
1378 ac->priv.userPasswd = a;
1381 mysql_free_result(res);
1382 errorStr = "Error in parameter ChgPassword";
1387 if (GetInt(row[4], &a) == 0)
1388 ac->priv.userStat = a;
1391 mysql_free_result(res);
1392 errorStr = "Error in parameter ChgStat";
1397 if (GetInt(row[5], &a) == 0)
1398 ac->priv.userCash = a;
1401 mysql_free_result(res);
1402 errorStr = "Error in parameter ChgCash";
1407 if (GetInt(row[6], &a) == 0)
1408 ac->priv.userAddDel = a;
1411 mysql_free_result(res);
1412 errorStr = "Error in parameter UsrAddDel";
1417 if (GetInt(row[7], &a) == 0)
1418 ac->priv.tariffChg = a;
1421 mysql_free_result(res);
1422 errorStr = "Error in parameter ChgTariff";
1427 if (GetInt(row[8], &a) == 0)
1428 ac->priv.adminChg = a;
1431 mysql_free_result(res);
1432 errorStr = "Error in parameter ChgAdmin";
1437 mysql_free_result(res);
1441 //-----------------------------------------------------------------------------
1442 int MYSQL_STORE::AddTariff(const std::string & name) const
1444 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1446 if(MysqlSetQuery(qbuf))
1448 errorStr = "Couldn't add tariff:\n";
1449 // errorStr += mysql_error(sock);
1455 //-----------------------------------------------------------------------------
1456 int MYSQL_STORE::DelTariff(const std::string & name) const
1458 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1460 if(MysqlSetQuery(qbuf))
1462 errorStr = "Couldn't delete tariff: ";
1463 // errorStr += mysql_error(sock);
1469 //-----------------------------------------------------------------------------
1470 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1475 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1477 if(MysqlGetQuery(qbuf,sock))
1479 errorStr = "Couldn't restore Tariff:\n";
1480 errorStr += mysql_error(sock);
1485 if (!(res=mysql_store_result(sock)))
1487 errorStr = "Couldn't restore Tariff:\n";
1488 errorStr += mysql_error(sock);
1494 td->tariffConf.name = tariffName;
1496 row = mysql_fetch_row(res);
1499 for (int i = 0; i<DIR_NUM; i++)
1501 strprintf(¶m, "Time%d", i);
1503 if (str.length() == 0)
1505 mysql_free_result(res);
1506 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1511 ParseTariffTimeStr(str.c_str(),
1512 td->dirPrice[i].hDay,
1513 td->dirPrice[i].mDay,
1514 td->dirPrice[i].hNight,
1515 td->dirPrice[i].mNight);
1517 strprintf(¶m, "PriceDayA%d", i);
1518 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1520 mysql_free_result(res);
1521 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1525 td->dirPrice[i].priceDayA /= (1024*1024);
1527 strprintf(¶m, "PriceDayB%d", i);
1528 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1530 mysql_free_result(res);
1531 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1535 td->dirPrice[i].priceDayB /= (1024*1024);
1537 strprintf(¶m, "PriceNightA%d", i);
1538 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1540 mysql_free_result(res);
1541 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1545 td->dirPrice[i].priceNightA /= (1024*1024);
1547 strprintf(¶m, "PriceNightB%d", i);
1548 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1550 mysql_free_result(res);
1551 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1555 td->dirPrice[i].priceNightB /= (1024*1024);
1557 strprintf(¶m, "Threshold%d", i);
1558 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
1560 mysql_free_result(res);
1561 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1566 strprintf(¶m, "SinglePrice%d", i);
1567 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
1569 mysql_free_result(res);
1570 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1575 strprintf(¶m, "NoDiscount%d", i);
1576 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
1578 mysql_free_result(res);
1579 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1585 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1587 mysql_free_result(res);
1588 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1593 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1595 mysql_free_result(res);
1596 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1601 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1603 mysql_free_result(res);
1604 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1609 str = row[4+8*DIR_NUM];
1610 param = "TraffType";
1612 if (str.length() == 0)
1614 mysql_free_result(res);
1615 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1620 td->tariffConf.traffType = TARIFF::StringToTraffType(str);
1622 if (schemaVersion > 0)
1624 str = row[5+8*DIR_NUM];
1627 if (str.length() == 0)
1629 mysql_free_result(res);
1630 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1635 td->tariffConf.period = TARIFF::StringToPeriod(str);
1639 td->tariffConf.period = TARIFF::MONTH;
1642 mysql_free_result(res);
1646 //-----------------------------------------------------------------------------
1647 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1651 std::string res="UPDATE tariffs SET";
1653 for (int i = 0; i < DIR_NUM; i++)
1655 strprintf(¶m, " PriceDayA%d=%f,", i,
1656 td.dirPrice[i].priceDayA * pt_mega);
1659 strprintf(¶m, " PriceDayB%d=%f,", i,
1660 td.dirPrice[i].priceDayB * pt_mega);
1663 strprintf(¶m, " PriceNightA%d=%f,", i,
1664 td.dirPrice[i].priceNightA * pt_mega);
1667 strprintf(¶m, " PriceNightB%d=%f,", i,
1668 td.dirPrice[i].priceNightB * pt_mega);
1671 strprintf(¶m, " Threshold%d=%d,", i,
1672 td.dirPrice[i].threshold);
1676 strprintf(¶m, " Time%d", i);
1678 strprintf(&s, "%0d:%0d-%0d:%0d",
1679 td.dirPrice[i].hDay,
1680 td.dirPrice[i].mDay,
1681 td.dirPrice[i].hNight,
1682 td.dirPrice[i].mNight);
1684 res += (param + "='" + s + "',");
1686 strprintf(¶m, " NoDiscount%d=%d,", i,
1687 td.dirPrice[i].noDiscount);
1690 strprintf(¶m, " SinglePrice%d=%d,", i,
1691 td.dirPrice[i].singlePrice);
1695 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1698 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1701 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1704 res += " TraffType='" + TARIFF::TraffTypeToString(td.tariffConf.traffType) + "'";
1706 if (schemaVersion > 0)
1707 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1709 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1712 if(MysqlSetQuery(res.c_str()))
1714 errorStr = "Couldn't save tariff:\n";
1715 //errorStr += mysql_error(sock);
1721 //-----------------------------------------------------------------------------
1722 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1724 const std::string & login) const
1726 std::string res, stTime, endTime, tempStr;
1733 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1741 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1743 if (!(sock=MysqlConnect())){
1748 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1750 errorStr = "Couldn't get table " + tempStr + ":\n";
1751 errorStr += mysql_error(sock);
1756 my_ulonglong num_rows = mysql_num_rows(result);
1758 mysql_free_result(result);
1762 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1763 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1764 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1765 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1766 lt->tm_mon+1, lt->tm_year+1900);
1768 if(MysqlQuery(qbuf,sock))
1770 errorStr = "Couldn't create WriteDetailedStat table:\n";
1771 errorStr += mysql_error(sock);
1780 lt1 = localtime(&lastStat);
1789 lt2 = localtime(&t);
1795 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1796 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1798 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1799 "day=%d,startTime='%s',endTime='%s',",
1800 lt->tm_mon+1, lt->tm_year+1900,
1807 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1808 stIter = statTree.begin();
1810 while (stIter != statTree.end())
1812 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1813 inet_ntostring(stIter->first.ip).c_str(),
1815 stIter->second.down,
1820 if( MysqlQuery((res+tempStr).c_str(),sock) )
1822 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1823 errorStr += mysql_error(sock);
1828 result=mysql_store_result(sock);
1830 mysql_free_result(result);
1837 //-----------------------------------------------------------------------------
1838 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1842 gettimeofday(&tv, NULL);
1844 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1846 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1848 static_cast<long long>(msg->header.id)
1851 if(MysqlSetQuery(qbuf))
1853 errorStr = "Couldn't add message:\n";
1854 //errorStr += mysql_error(sock);
1858 return EditMessage(*msg, login);
1860 //-----------------------------------------------------------------------------
1861 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1865 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1866 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1867 "WHERE login='%s' AND id=%lld LIMIT 1",
1869 msg.header.lastSendTime,
1870 msg.header.creationTime,
1871 msg.header.showTime,
1873 msg.header.repeatPeriod,
1874 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1879 if(MysqlSetQuery(res.c_str()))
1881 errorStr = "Couldn't edit message:\n";
1882 //errorStr += mysql_error(sock);
1888 //-----------------------------------------------------------------------------
1889 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1895 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1896 login.c_str(), static_cast<unsigned long long>(id));
1898 if(MysqlGetQuery(qbuf,sock))
1900 errorStr = "Couldn't GetMessage:\n";
1901 errorStr += mysql_error(sock);
1906 if (!(res=mysql_store_result(sock)))
1908 errorStr = "Couldn't GetMessage:\n";
1909 errorStr += mysql_error(sock);
1914 row = mysql_fetch_row(res);
1916 if(row[2]&&str2x(row[2], msg->header.type))
1918 mysql_free_result(res);
1919 errorStr = "Invalid value in message header for user: " + login;
1924 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1926 mysql_free_result(res);
1927 errorStr = "Invalid value in message header for user: " + login;
1932 if(row[4] && str2x(row[4], msg->header.creationTime))
1934 mysql_free_result(res);
1935 errorStr = "Invalid value in message header for user: " + login;
1940 if(row[5] && str2x(row[5], msg->header.showTime))
1942 mysql_free_result(res);
1943 errorStr = "Invalid value in message header for user: " + login;
1948 if(row[6] && str2x(row[6], msg->header.repeat))
1950 mysql_free_result(res);
1951 errorStr = "Invalid value in message header for user: " + login;
1956 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1958 mysql_free_result(res);
1959 errorStr = "Invalid value in message header for user: " + login;
1964 msg->header.id = id;
1967 mysql_free_result(res);
1971 //-----------------------------------------------------------------------------
1972 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
1974 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1975 login.c_str(), static_cast<long long>(id));
1977 if(MysqlSetQuery(qbuf))
1979 errorStr = "Couldn't delete Message:\n";
1980 //errorStr += mysql_error(sock);
1986 //-----------------------------------------------------------------------------
1987 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
1992 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1994 if(MysqlGetQuery(qbuf,sock))
1996 errorStr = "Couldn't GetMessageHdrs:\n";
1997 errorStr += mysql_error(sock);
2002 if (!(res=mysql_store_result(sock)))
2004 errorStr = "Couldn't GetMessageHdrs:\n";
2005 errorStr += mysql_error(sock);
2011 my_ulonglong num_rows = mysql_num_rows(res);
2014 for (i = 0; i < num_rows; i++)
2016 row = mysql_fetch_row(res);
2017 if (str2x(row[1], id))
2022 if(str2x(row[2], hdr.type))
2026 if(str2x(row[3], hdr.lastSendTime))
2030 if(str2x(row[4], hdr.creationTime))
2034 if(str2x(row[5], hdr.showTime))
2038 if(str2x(row[6], hdr.repeat))
2042 if(str2x(row[7], hdr.repeatPeriod))
2046 hdrsList->push_back(hdr);
2049 mysql_free_result(res);
2053 //-----------------------------------------------------------------------------
2055 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2058 int ret=MysqlGetQuery(Query,sock);
2062 //-----------------------------------------------------------------------------
2063 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2064 if (!(sock=MysqlConnect())) {
2067 return MysqlQuery(Query,sock);
2069 //-----------------------------------------------------------------------------
2070 MYSQL * MYSQL_STORE::MysqlConnect() const {
2072 if ( !(sock=mysql_init(NULL)) ){
2073 errorStr= "mysql init susck\n";
2076 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2077 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2080 errorStr = "Couldn't connect to mysql engine! With error:\n";
2081 errorStr += mysql_error(sock);
2085 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2086 errorStr = "Database lost !\n";
2092 //-----------------------------------------------------------------------------