10 #include "stg/user_ips.h"
11 #include "stg/user_conf.h"
12 #include "stg/user_stat.h"
13 #include "stg/blowfish.h"
14 #include "stg/plugin_creator.h"
15 #include "stg/logger.h"
16 #include "mysql_store.h"
18 #define adm_enc_passwd "cjeifY8m3"
24 const int pt_mega = 1024 * 1024;
25 const std::string badSyms = "'`";
26 const char repSym = '\"';
27 const int RepitTimes = 3;
30 int GetInt(const std::string & str, T * val, T defaultVal = T())
34 *val = static_cast<T>(strtoll(str.c_str(), &res, 10));
38 *val = defaultVal; //Error!
45 int GetDouble(const std::string & str, double * val, double defaultVal)
49 *val = strtod(str.c_str(), &res);
53 *val = defaultVal; //Error!
60 int GetTime(const std::string & str, time_t * val, time_t defaultVal)
64 *val = strtol(str.c_str(), &res, 10);
68 *val = defaultVal; //Error!
75 //-----------------------------------------------------------------------------
76 std::string ReplaceStr(std::string source, const std::string & symlist, const char chgsym)
78 std::string::size_type pos=0;
80 while( (pos = source.find_first_of(symlist,pos)) != std::string::npos)
81 source.replace(pos, 1,1, chgsym);
86 int GetULongLongInt(const std::string & str, uint64_t * val, uint64_t defaultVal)
90 *val = strtoull(str.c_str(), &res, 10);
94 *val = defaultVal; //Error!
101 PLUGIN_CREATOR<MYSQL_STORE> msc;
104 extern "C" STORE * GetStore();
105 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
110 return msc.GetPlugin();
112 //-----------------------------------------------------------------------------
113 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
117 //-----------------------------------------------------------------------------
118 int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<PARAM_VALUE> & moduleParams,
119 const std::string & name, std::string & result)
123 std::vector<PARAM_VALUE>::const_iterator pvi;
124 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
125 if (pvi == moduleParams.end() || pvi->value.empty())
127 errorStr = "Parameter \'" + name + "\' not found.";
131 result = pvi->value[0];
135 //-----------------------------------------------------------------------------
136 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
138 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
139 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
141 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
142 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
144 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
145 ParseParam(s.moduleParams, "dbname", dbName) < 0)
147 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
148 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
153 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 MYSQL_STORE::MYSQL_STORE()
157 : version("mysql_store v.0.67"),
159 logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
162 //-----------------------------------------------------------------------------
163 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
167 if( (ret = mysql_query(sock,sQuery)) )
169 for(int i=0; i<RepitTimes; i++)
171 if( (ret = mysql_query(sock,sQuery)) )
172 ;//need to send error result
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
183 int MYSQL_STORE::ParseSettings()
185 int ret = storeSettings.ParseSettings(settings);
189 errorStr = storeSettings.GetStrError();
192 if(storeSettings.GetDBPassword().length() == 0)
194 errorStr = "Database password must be not empty. Please read Manual.";
198 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
199 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
202 errorStr = "Couldn't connect to mysql engine! With error:\n";
203 errorStr += mysql_error(&mysql);
209 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
211 std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
213 if(MysqlQuery(res.c_str(),sock))
215 errorStr = "Couldn't create database! With error:\n";
216 errorStr += mysql_error(sock);
222 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
224 errorStr = "Couldn't select database! With error:\n";
225 errorStr += mysql_error(sock);
229 ret = CheckAllTables(sock);
234 ret = CheckAllTables(sock);
238 logger("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
246 //-----------------------------------------------------------------------------
247 bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
251 if (!(result=mysql_list_tables(sock,str.c_str() )))
253 errorStr = "Couldn't get tables list With error:\n";
254 errorStr += mysql_error(sock);
259 my_ulonglong num_rows = mysql_num_rows(result);
262 mysql_free_result(result);
264 return num_rows == 1;
266 //-----------------------------------------------------------------------------
267 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
269 //info-------------------------------------------------------------------------
270 if(!IsTablePresent("info",sock))
272 sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
274 if(MysqlQuery(qbuf,sock))
276 errorStr = "Couldn't create info table With error:\n";
277 errorStr += mysql_error(sock);
282 sprintf(qbuf,"INSERT INTO info SET version=0");
284 if(MysqlQuery(qbuf,sock))
286 errorStr = "Couldn't write default version. With error:\n";
287 errorStr += mysql_error(sock);
295 std::vector<std::string> info;
296 if (GetAllParams(&info, "info", "version"))
303 GetInt(info.front(), &schemaVersion, 0);
306 //admins-----------------------------------------------------------------------
307 if(!IsTablePresent("admins",sock))
309 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
310 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
311 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
312 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
313 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
315 if(MysqlQuery(qbuf,sock))
317 errorStr = "Couldn't create admin table list With error:\n";
318 errorStr += mysql_error(sock);
323 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
324 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
325 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
327 if(MysqlQuery(qbuf,sock))
329 errorStr = "Couldn't create default admin. With error:\n";
330 errorStr += mysql_error(sock);
336 //tariffs-----------------------------------------------------------------------
337 std::string param, res;
338 if(!IsTablePresent("tariffs",sock))
340 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
342 for (int i = 0; i < DIR_NUM; i++)
344 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
347 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
350 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
353 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
356 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
359 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
362 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
365 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
369 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
370 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
371 "period VARCHAR(32) NOT NULL DEFAULT 'month',"
372 "change_policy VARCHAR(32) NOT NULL DEFAULT 'allow',"
373 "change_policy_timeout TIMEZONE NOT NULL DEFAULT '1970-01-01 00:00:00')";
375 if(MysqlQuery(res.c_str(),sock))
377 errorStr = "Couldn't create tariffs table list With error:\n";
378 errorStr += mysql_error(sock);
383 res = "INSERT INTO tariffs SET name='tariff',";
385 for (int i = 0; i < DIR_NUM; i++)
387 strprintf(¶m, " NoDiscount%d=1,", i);
390 strprintf(¶m, " Threshold%d=0,", i);
393 strprintf(¶m, " Time%d='0:0-0:0',", i);
398 strprintf(¶m, " SinglePrice%d=0,", i);
404 strprintf(¶m, " PriceDayA%d=0.0,", i);
409 strprintf(¶m, " PriceDayB%d=0.0,", i);
415 strprintf(¶m, " PriceNightA%d=0.0,", i);
420 strprintf(¶m, " PriceNightB%d=0.0,", i);
425 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
426 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
427 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month',"\
428 "change_policy='allow'";
430 if(MysqlQuery(res.c_str(),sock))
432 errorStr = "Couldn't create default tariff. With error:\n";
433 errorStr += mysql_error(sock);
438 sprintf(qbuf,"UPDATE info SET version=1");
440 if(MysqlQuery(qbuf,sock))
442 errorStr = "Couldn't write default version. With error:\n";
443 errorStr += mysql_error(sock);
450 //users-----------------------------------------------------------------------
451 if(!IsTablePresent("users",sock))
453 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
454 "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 '',"\
455 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
456 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
457 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
459 for (int i = 0; i < USERDATA_NUM; i++)
461 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
465 param = " CreditExpire INT(11) DEFAULT 0,";
468 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
471 for (int i = 0; i < DIR_NUM; i++)
473 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
476 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
480 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
481 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
482 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
483 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
486 if(MysqlQuery(res.c_str(),sock))
488 errorStr = "Couldn't create users table list With error:\n";
489 errorStr += mysql_error(sock);
490 errorStr += "\n\n" + res;
495 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
496 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
497 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
498 "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
500 for (int i = 0; i < USERDATA_NUM; i++)
502 strprintf(¶m, " Userdata%d='',", i);
506 for (int i = 0; i < DIR_NUM; i++)
508 strprintf(¶m, " D%d=0,", i);
511 strprintf(¶m, " U%d=0,", i);
515 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
516 "LastCashAddTime=0, PassiveTime=0";
518 if(MysqlQuery(res.c_str(),sock))
520 errorStr = "Couldn't create default user. With error:\n";
521 errorStr += mysql_error(sock);
527 //logs-----------------------------------------------------------------------
528 if(!IsTablePresent("logs"))
530 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
534 errorStr = "Couldn't create admin table list With error:\n";
535 errorStr += mysql_error(sock);
540 //messages---------------------------------------------------------------------
541 if(!IsTablePresent("messages",sock))
543 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
544 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
545 "stgRepeat INT, repeatPeriod INT, text TEXT)");
547 if(MysqlQuery(qbuf,sock))
549 errorStr = "Couldn't create messages table. With error:\n";
550 errorStr += mysql_error(sock);
556 //month_stat-------------------------------------------------------------------
557 if(!IsTablePresent("stat",sock))
559 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
561 for (int i = 0; i < DIR_NUM; i++)
563 strprintf(¶m, " U%d BIGINT,", i);
566 strprintf(¶m, " D%d BIGINT,", i);
570 res += " cash DOUBLE, INDEX (login))";
572 if(MysqlQuery(res.c_str(),sock))
574 errorStr = "Couldn't create stat table. With error:\n";
575 errorStr += mysql_error(sock);
583 //-----------------------------------------------------------------------------
584 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
586 if (schemaVersion < 1)
588 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
590 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
591 errorStr += mysql_error(sock);
595 if (MysqlQuery("UPDATE info SET version = 1", sock))
597 errorStr = "Couldn't update DB schema version to 1. With error:\n";
598 errorStr += mysql_error(sock);
603 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
606 if (schemaVersion < 2)
608 if (MysqlQuery("ALTER TABLE tariffs ADD change_policy VARCHAR(32) NOT NULL DEFAULT 'allow'", sock))
610 errorStr = "Couldn't update tariffs table to version 2. With error:\n";
611 errorStr += mysql_error(sock);
615 if (MysqlQuery("UPDATE info SET version = 2", sock))
617 errorStr = "Couldn't update DB schema version to 2. With error:\n";
618 errorStr += mysql_error(sock);
623 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
627 //-----------------------------------------------------------------------------
629 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList,
630 const std::string & table, const std::string & name) const
639 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
641 if(MysqlGetQuery(qbuf,sock))
643 errorStr = "Couldn't GetAllParams Query for: ";
644 errorStr += name + " - " + table + "\n";
645 errorStr += mysql_error(sock);
650 if (!(res=mysql_store_result(sock)))
652 errorStr = "Couldn't GetAllParams Results for: ";
653 errorStr += name + " - " + table + "\n";
654 errorStr += mysql_error(sock);
658 num = mysql_num_rows(res);
660 for(i = 0; i < num; i++)
662 row = mysql_fetch_row(res);
663 ParamList->push_back(row[0]);
666 mysql_free_result(res);
672 //-----------------------------------------------------------------------------
673 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
675 if(GetAllParams(usersList, "users", "login"))
680 //-----------------------------------------------------------------------------
681 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
683 if(GetAllParams(adminsList, "admins", "login"))
688 //-----------------------------------------------------------------------------
689 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
691 if(GetAllParams(tariffsList, "tariffs", "name"))
696 //-----------------------------------------------------------------------------
697 int MYSQL_STORE::AddUser(const std::string & login) const
699 std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
701 for (int i = 0; i < USERDATA_NUM; i++)
702 query += ",Userdata" + x2str(i) + "=''";
704 if(MysqlSetQuery(query.c_str()))
706 errorStr = "Couldn't add user:\n";
707 //errorStr += mysql_error(sock);
713 //-----------------------------------------------------------------------------
714 int MYSQL_STORE::DelUser(const std::string & login) const
716 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
718 if(MysqlSetQuery(qbuf))
720 errorStr = "Couldn't delete user:\n";
721 //errorStr += mysql_error(sock);
727 //-----------------------------------------------------------------------------
728 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
735 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
736 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
737 RealName, StgGroup, Credit, TariffChange, ";
739 for (int i = 0; i < USERDATA_NUM; i++)
741 sprintf(qbuf, "Userdata%d, ", i);
745 query += "CreditExpire, IP FROM users WHERE login='";
746 query += login + "' LIMIT 1";
748 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
750 if(MysqlGetQuery(query.c_str(),sock))
752 errorStr = "Couldn't restore Tariff(on query):\n";
753 errorStr += mysql_error(sock);
758 if (!(res=mysql_store_result(sock)))
760 errorStr = "Couldn't restore Tariff(on getting result):\n";
761 errorStr += mysql_error(sock);
766 if (mysql_num_rows(res) != 1)
768 errorStr = "User not found";
773 row = mysql_fetch_row(res);
775 conf->password = row[1];
777 if (conf->password.empty())
779 mysql_free_result(res);
780 errorStr = "User \'" + login + "\' password is blank.";
785 if (GetInt(row[2],&conf->passive) != 0)
787 mysql_free_result(res);
788 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
793 if (GetInt(row[3], &conf->disabled) != 0)
795 mysql_free_result(res);
796 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
801 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
803 mysql_free_result(res);
804 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
809 if (GetInt(row[5], &conf->alwaysOnline) != 0)
811 mysql_free_result(res);
812 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
817 conf->tariffName = row[6];
819 if (conf->tariffName.empty())
821 mysql_free_result(res);
822 errorStr = "User \'" + login + "\' tariff is blank.";
827 conf->address = row[7];
828 conf->phone = row[8];
829 conf->email = row[9];
830 conf->note = row[10];
831 conf->realName = row[11];
832 conf->group = row[12];
834 if (GetDouble(row[13], &conf->credit, 0) != 0)
836 mysql_free_result(res);
837 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
842 conf->nextTariff = row[14];
844 for (int i = 0; i < USERDATA_NUM; i++)
846 conf->userdata[i] = row[15+i];
849 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
851 std::string ipStr = row[16+USERDATA_NUM];
857 catch (const std::string & s)
859 mysql_free_result(res);
860 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
866 mysql_free_result(res);
871 //-----------------------------------------------------------------------------
872 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
882 for (int i = 0; i < DIR_NUM; i++)
884 sprintf(qbuf, "D%d, U%d, ", i, i);
888 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
889 FROM users WHERE login = '";
890 query += login + "'";
892 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
894 if(MysqlGetQuery(query.c_str() ,sock))
896 errorStr = "Couldn't restore UserStat(on query):\n";
897 errorStr += mysql_error(sock);
902 if (!(res=mysql_store_result(sock)))
904 errorStr = "Couldn't restore UserStat(on getting result):\n";
905 errorStr += mysql_error(sock);
910 row = mysql_fetch_row(res);
912 unsigned int startPos=0;
916 for (int i = 0; i < DIR_NUM; i++)
919 sprintf(s, "D%d", i);
920 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
922 mysql_free_result(res);
923 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
927 stat->monthDown[i] = traff;
929 sprintf(s, "U%d", i);
930 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
932 mysql_free_result(res);
933 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
937 stat->monthUp[i] = traff;
940 startPos += (2*DIR_NUM);
942 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
944 mysql_free_result(res);
945 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
950 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
952 mysql_free_result(res);
953 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
958 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
960 mysql_free_result(res);
961 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
966 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
968 mysql_free_result(res);
969 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
974 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
976 mysql_free_result(res);
977 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
982 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
984 mysql_free_result(res);
985 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
990 mysql_free_result(res);
994 //-----------------------------------------------------------------------------
995 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
1000 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
1001 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
1002 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
1003 conf.password.c_str(),
1006 conf.disabledDetailStat,
1008 conf.tariffName.c_str(),
1009 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
1010 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
1011 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
1012 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
1013 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
1014 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
1016 conf.nextTariff.c_str()
1019 for (int i = 0; i < USERDATA_NUM; i++)
1021 strprintf(¶m, " Userdata%d='%s',", i,
1022 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1026 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1029 std::ostringstream ipStr;
1032 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1035 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1038 if(MysqlSetQuery(res.c_str()))
1040 errorStr = "Couldn't save user conf:\n";
1041 //errorStr += mysql_error(sock);
1047 //-----------------------------------------------------------------------------
1048 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
1053 res = "UPDATE users SET";
1055 for (int i = 0; i < DIR_NUM; i++)
1057 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1060 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1064 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1065 " PassiveTime=%d, LastActivityTime=%d",
1069 stat.lastCashAddTime,
1071 stat.lastActivityTime
1075 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1078 if(MysqlSetQuery(res.c_str()))
1080 errorStr = "Couldn't save user stat:\n";
1081 // errorStr += mysql_error(sock);
1087 //-----------------------------------------------------------------------------
1088 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
1090 std::string res, tempStr;
1099 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1100 if (!(sock=MysqlConnect())){
1101 errorStr = "Couldn't connect to Server";
1104 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1106 errorStr = "Couldn't get table " + tempStr + ":\n";
1107 errorStr += mysql_error(sock);
1112 my_ulonglong num_rows = mysql_num_rows(result);
1114 mysql_free_result(result);
1118 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1119 lt->tm_mon+1, lt->tm_year+1900);
1121 if(MysqlQuery(qbuf,sock))
1123 errorStr = "Couldn't create WriteDetailedStat table:\n";
1124 errorStr += mysql_error(sock);
1130 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1134 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1135 lt->tm_mon+1, lt->tm_year+1900,
1136 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1138 if(MysqlQuery(send.c_str(),sock))
1140 errorStr = "Couldn't write log string:\n";
1141 errorStr += mysql_error(sock);
1149 //-----------------------------------------------------------------------------
1150 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
1151 const std::string & admLogin,
1153 const std::string & paramName,
1154 const std::string & oldValue,
1155 const std::string & newValue,
1156 const std::string & message) const
1158 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1159 + paramName + "\' parameter changed from \'" + oldValue +
1160 "\' to \'" + newValue + "\'. " + message;
1162 return WriteLogString(userLogMsg, login);
1164 //-----------------------------------------------------------------------------
1165 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1167 std::string logStr = "Connect, " + inet_ntostring(ip);
1168 return WriteLogString(logStr, login);
1170 //-----------------------------------------------------------------------------
1171 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
1172 const DIR_TRAFF & up,
1173 const DIR_TRAFF & down,
1174 const DIR_TRAFF & sessionUp,
1175 const DIR_TRAFF & sessionDown,
1178 const std::string & /*reason*/) const
1180 std::string logStr = "Disconnect, ";
1181 std::ostringstream sssu;
1182 std::ostringstream sssd;
1183 std::ostringstream ssmu;
1184 std::ostringstream ssmd;
1185 std::ostringstream sscash;
1191 sssd << sessionDown;
1195 logStr += " session upload: \'";
1196 logStr += sssu.str();
1197 logStr += "\' session download: \'";
1198 logStr += sssd.str();
1199 logStr += "\' month upload: \'";
1200 logStr += ssmu.str();
1201 logStr += "\' month download: \'";
1202 logStr += ssmd.str();
1203 logStr += "\' cash: \'";
1204 logStr += sscash.str();
1207 return WriteLogString(logStr, login);
1209 //-----------------------------------------------------------------------------
1210 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1211 const std::string & login) const
1213 std::string param, res;
1215 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1216 login.c_str(), month+1, year+1900);
1218 for (int i = 0; i < DIR_NUM; i++)
1220 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1223 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1227 strprintf(¶m, " cash=%f", stat.cash);
1230 if(MysqlSetQuery(res.c_str()))
1232 errorStr = "Couldn't SaveMonthStat:\n";
1233 //errorStr += mysql_error(sock);
1239 //-----------------------------------------------------------------------------*/
1240 int MYSQL_STORE::AddAdmin(const std::string & login) const
1242 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1244 if(MysqlSetQuery(qbuf))
1246 errorStr = "Couldn't add admin:\n";
1247 //errorStr += mysql_error(sock);
1253 //-----------------------------------------------------------------------------*/
1254 int MYSQL_STORE::DelAdmin(const std::string & login) const
1256 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1258 if(MysqlSetQuery(qbuf))
1260 errorStr = "Couldn't delete admin:\n";
1261 //errorStr += mysql_error(sock);
1267 //-----------------------------------------------------------------------------*/
1268 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1270 char passwordE[2 * ADM_PASSWD_LEN + 2];
1271 char pass[ADM_PASSWD_LEN + 1];
1272 char adminPass[ADM_PASSWD_LEN + 1];
1274 memset(pass, 0, sizeof(pass));
1275 memset(adminPass, 0, sizeof(adminPass));
1278 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1280 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1281 adminPass[ADM_PASSWD_LEN - 1] = 0;
1283 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1285 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1288 pass[ADM_PASSWD_LEN - 1] = 0;
1289 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1291 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1292 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1293 "WHERE login='%s' LIMIT 1",
1305 if(MysqlSetQuery(qbuf))
1307 errorStr = "Couldn't save admin:\n";
1308 //errorStr += mysql_error(sock);
1314 //-----------------------------------------------------------------------------
1315 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1317 char pass[ADM_PASSWD_LEN + 1];
1318 char password[ADM_PASSWD_LEN + 1];
1319 char passwordE[2*ADM_PASSWD_LEN + 2];
1322 memset(password, 0, sizeof(password));
1328 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1330 if(MysqlGetQuery(qbuf,sock))
1332 errorStr = "Couldn't restore admin:\n";
1333 errorStr += mysql_error(sock);
1338 if (!(res=mysql_store_result(sock)))
1340 errorStr = "Couldn't restore admin:\n";
1341 errorStr += mysql_error(sock);
1346 if ( mysql_num_rows(res) == 0)
1348 mysql_free_result(res);
1349 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1354 row = mysql_fetch_row(res);
1360 mysql_free_result(res);
1361 errorStr = "Error in parameter password";
1366 memset(passwordE, 0, sizeof(passwordE));
1367 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1369 memset(pass, 0, sizeof(pass));
1371 if (passwordE[0] != 0)
1373 Decode21(pass, passwordE);
1374 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1376 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1378 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1386 ac->password = password;
1390 if (GetInt(row[2], &a) == 0)
1391 ac->priv.userConf = a;
1394 mysql_free_result(res);
1395 errorStr = "Error in parameter ChgConf";
1400 if (GetInt(row[3], &a) == 0)
1401 ac->priv.userPasswd = a;
1404 mysql_free_result(res);
1405 errorStr = "Error in parameter ChgPassword";
1410 if (GetInt(row[4], &a) == 0)
1411 ac->priv.userStat = a;
1414 mysql_free_result(res);
1415 errorStr = "Error in parameter ChgStat";
1420 if (GetInt(row[5], &a) == 0)
1421 ac->priv.userCash = a;
1424 mysql_free_result(res);
1425 errorStr = "Error in parameter ChgCash";
1430 if (GetInt(row[6], &a) == 0)
1431 ac->priv.userAddDel = a;
1434 mysql_free_result(res);
1435 errorStr = "Error in parameter UsrAddDel";
1440 if (GetInt(row[7], &a) == 0)
1441 ac->priv.tariffChg = a;
1444 mysql_free_result(res);
1445 errorStr = "Error in parameter ChgTariff";
1450 if (GetInt(row[8], &a) == 0)
1451 ac->priv.adminChg = a;
1454 mysql_free_result(res);
1455 errorStr = "Error in parameter ChgAdmin";
1460 mysql_free_result(res);
1464 //-----------------------------------------------------------------------------
1465 int MYSQL_STORE::AddTariff(const std::string & name) const
1467 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1469 if(MysqlSetQuery(qbuf))
1471 errorStr = "Couldn't add tariff:\n";
1472 // errorStr += mysql_error(sock);
1478 //-----------------------------------------------------------------------------
1479 int MYSQL_STORE::DelTariff(const std::string & name) const
1481 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1483 if(MysqlSetQuery(qbuf))
1485 errorStr = "Couldn't delete tariff: ";
1486 // errorStr += mysql_error(sock);
1492 //-----------------------------------------------------------------------------
1493 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1498 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1500 if(MysqlGetQuery(qbuf,sock))
1502 errorStr = "Couldn't restore Tariff:\n";
1503 errorStr += mysql_error(sock);
1508 if (!(res=mysql_store_result(sock)))
1510 errorStr = "Couldn't restore Tariff:\n";
1511 errorStr += mysql_error(sock);
1517 td->tariffConf.name = tariffName;
1519 row = mysql_fetch_row(res);
1522 for (int i = 0; i<DIR_NUM; i++)
1524 strprintf(¶m, "Time%d", i);
1526 if (str.length() == 0)
1528 mysql_free_result(res);
1529 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1534 ParseTariffTimeStr(str.c_str(),
1535 td->dirPrice[i].hDay,
1536 td->dirPrice[i].mDay,
1537 td->dirPrice[i].hNight,
1538 td->dirPrice[i].mNight);
1540 strprintf(¶m, "PriceDayA%d", i);
1541 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1543 mysql_free_result(res);
1544 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1548 td->dirPrice[i].priceDayA /= (1024*1024);
1550 strprintf(¶m, "PriceDayB%d", i);
1551 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1553 mysql_free_result(res);
1554 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1558 td->dirPrice[i].priceDayB /= (1024*1024);
1560 strprintf(¶m, "PriceNightA%d", i);
1561 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1563 mysql_free_result(res);
1564 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1568 td->dirPrice[i].priceNightA /= (1024*1024);
1570 strprintf(¶m, "PriceNightB%d", i);
1571 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1573 mysql_free_result(res);
1574 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1578 td->dirPrice[i].priceNightB /= (1024*1024);
1580 strprintf(¶m, "Threshold%d", i);
1581 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
1583 mysql_free_result(res);
1584 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1589 strprintf(¶m, "SinglePrice%d", i);
1590 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
1592 mysql_free_result(res);
1593 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1598 strprintf(¶m, "NoDiscount%d", i);
1599 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
1601 mysql_free_result(res);
1602 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1608 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1610 mysql_free_result(res);
1611 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1616 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1618 mysql_free_result(res);
1619 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1624 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1626 mysql_free_result(res);
1627 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1632 str = row[4+8*DIR_NUM];
1633 param = "TraffType";
1635 if (str.length() == 0)
1637 mysql_free_result(res);
1638 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1643 td->tariffConf.traffType = TARIFF::StringToTraffType(str);
1645 if (schemaVersion > 0)
1647 str = row[5+8*DIR_NUM];
1650 if (str.length() == 0)
1652 mysql_free_result(res);
1653 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1658 td->tariffConf.period = TARIFF::StringToPeriod(str);
1662 td->tariffConf.period = TARIFF::MONTH;
1665 if (schemaVersion > 1)
1667 str = row[6+8*DIR_NUM];
1668 param = "ChangePolicy";
1670 if (str.length() == 0)
1672 mysql_free_result(res);
1673 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1678 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
1682 td->tariffConf.changePolicy = TARIFF::ALLOW;
1685 mysql_free_result(res);
1689 //-----------------------------------------------------------------------------
1690 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1694 std::string res="UPDATE tariffs SET";
1696 for (int i = 0; i < DIR_NUM; i++)
1698 strprintf(¶m, " PriceDayA%d=%f,", i,
1699 td.dirPrice[i].priceDayA * pt_mega);
1702 strprintf(¶m, " PriceDayB%d=%f,", i,
1703 td.dirPrice[i].priceDayB * pt_mega);
1706 strprintf(¶m, " PriceNightA%d=%f,", i,
1707 td.dirPrice[i].priceNightA * pt_mega);
1710 strprintf(¶m, " PriceNightB%d=%f,", i,
1711 td.dirPrice[i].priceNightB * pt_mega);
1714 strprintf(¶m, " Threshold%d=%d,", i,
1715 td.dirPrice[i].threshold);
1719 strprintf(¶m, " Time%d", i);
1721 strprintf(&s, "%0d:%0d-%0d:%0d",
1722 td.dirPrice[i].hDay,
1723 td.dirPrice[i].mDay,
1724 td.dirPrice[i].hNight,
1725 td.dirPrice[i].mNight);
1727 res += (param + "='" + s + "',");
1729 strprintf(¶m, " NoDiscount%d=%d,", i,
1730 td.dirPrice[i].noDiscount);
1733 strprintf(¶m, " SinglePrice%d=%d,", i,
1734 td.dirPrice[i].singlePrice);
1738 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1741 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1744 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1747 res += " TraffType='" + TARIFF::TraffTypeToString(td.tariffConf.traffType) + "'";
1749 if (schemaVersion > 0)
1750 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1752 if (schemaVersion > 1)
1753 res += ", change_policy='" + TARIFF::ChangePolicyToString(td.tariffConf.changePolicy) + "'";
1755 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1758 if(MysqlSetQuery(res.c_str()))
1760 errorStr = "Couldn't save tariff:\n";
1761 //errorStr += mysql_error(sock);
1767 //-----------------------------------------------------------------------------
1768 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1770 const std::string & login) const
1772 std::string res, stTime, endTime, tempStr;
1779 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1787 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1789 if (!(sock=MysqlConnect())){
1794 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1796 errorStr = "Couldn't get table " + tempStr + ":\n";
1797 errorStr += mysql_error(sock);
1802 my_ulonglong num_rows = mysql_num_rows(result);
1804 mysql_free_result(result);
1808 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1809 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1810 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1811 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1812 lt->tm_mon+1, lt->tm_year+1900);
1814 if(MysqlQuery(qbuf,sock))
1816 errorStr = "Couldn't create WriteDetailedStat table:\n";
1817 errorStr += mysql_error(sock);
1826 lt1 = localtime(&lastStat);
1835 lt2 = localtime(&t);
1841 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1842 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1844 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1845 "day=%d,startTime='%s',endTime='%s',",
1846 lt->tm_mon+1, lt->tm_year+1900,
1853 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1854 stIter = statTree.begin();
1856 while (stIter != statTree.end())
1858 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1859 inet_ntostring(stIter->first.ip).c_str(),
1861 stIter->second.down,
1866 if( MysqlQuery((res+tempStr).c_str(),sock) )
1868 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1869 errorStr += mysql_error(sock);
1874 result=mysql_store_result(sock);
1876 mysql_free_result(result);
1883 //-----------------------------------------------------------------------------
1884 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1888 gettimeofday(&tv, NULL);
1890 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1892 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1894 static_cast<long long>(msg->header.id)
1897 if(MysqlSetQuery(qbuf))
1899 errorStr = "Couldn't add message:\n";
1900 //errorStr += mysql_error(sock);
1904 return EditMessage(*msg, login);
1906 //-----------------------------------------------------------------------------
1907 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1911 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1912 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1913 "WHERE login='%s' AND id=%lld LIMIT 1",
1915 msg.header.lastSendTime,
1916 msg.header.creationTime,
1917 msg.header.showTime,
1919 msg.header.repeatPeriod,
1920 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1925 if(MysqlSetQuery(res.c_str()))
1927 errorStr = "Couldn't edit message:\n";
1928 //errorStr += mysql_error(sock);
1934 //-----------------------------------------------------------------------------
1935 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1941 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1942 login.c_str(), static_cast<unsigned long long>(id));
1944 if(MysqlGetQuery(qbuf,sock))
1946 errorStr = "Couldn't GetMessage:\n";
1947 errorStr += mysql_error(sock);
1952 if (!(res=mysql_store_result(sock)))
1954 errorStr = "Couldn't GetMessage:\n";
1955 errorStr += mysql_error(sock);
1960 row = mysql_fetch_row(res);
1962 if(row[2]&&str2x(row[2], msg->header.type))
1964 mysql_free_result(res);
1965 errorStr = "Invalid value in message header for user: " + login;
1970 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1972 mysql_free_result(res);
1973 errorStr = "Invalid value in message header for user: " + login;
1978 if(row[4] && str2x(row[4], msg->header.creationTime))
1980 mysql_free_result(res);
1981 errorStr = "Invalid value in message header for user: " + login;
1986 if(row[5] && str2x(row[5], msg->header.showTime))
1988 mysql_free_result(res);
1989 errorStr = "Invalid value in message header for user: " + login;
1994 if(row[6] && str2x(row[6], msg->header.repeat))
1996 mysql_free_result(res);
1997 errorStr = "Invalid value in message header for user: " + login;
2002 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
2004 mysql_free_result(res);
2005 errorStr = "Invalid value in message header for user: " + login;
2010 msg->header.id = id;
2013 mysql_free_result(res);
2017 //-----------------------------------------------------------------------------
2018 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
2020 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
2021 login.c_str(), static_cast<long long>(id));
2023 if(MysqlSetQuery(qbuf))
2025 errorStr = "Couldn't delete Message:\n";
2026 //errorStr += mysql_error(sock);
2032 //-----------------------------------------------------------------------------
2033 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
2038 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
2040 if(MysqlGetQuery(qbuf,sock))
2042 errorStr = "Couldn't GetMessageHdrs:\n";
2043 errorStr += mysql_error(sock);
2048 if (!(res=mysql_store_result(sock)))
2050 errorStr = "Couldn't GetMessageHdrs:\n";
2051 errorStr += mysql_error(sock);
2057 my_ulonglong num_rows = mysql_num_rows(res);
2060 for (i = 0; i < num_rows; i++)
2062 row = mysql_fetch_row(res);
2063 if (str2x(row[1], id))
2068 if(str2x(row[2], hdr.type))
2072 if(str2x(row[3], hdr.lastSendTime))
2076 if(str2x(row[4], hdr.creationTime))
2080 if(str2x(row[5], hdr.showTime))
2084 if(str2x(row[6], hdr.repeat))
2088 if(str2x(row[7], hdr.repeatPeriod))
2092 hdrsList->push_back(hdr);
2095 mysql_free_result(res);
2099 //-----------------------------------------------------------------------------
2101 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2104 int ret=MysqlGetQuery(Query,sock);
2108 //-----------------------------------------------------------------------------
2109 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2110 if (!(sock=MysqlConnect())) {
2113 return MysqlQuery(Query,sock);
2115 //-----------------------------------------------------------------------------
2116 MYSQL * MYSQL_STORE::MysqlConnect() const {
2118 if ( !(sock=mysql_init(NULL)) ){
2119 errorStr= "mysql init susck\n";
2122 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2123 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2126 errorStr = "Couldn't connect to mysql engine! With error:\n";
2127 errorStr += mysql_error(sock);
2131 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2132 errorStr = "Database lost !\n";
2138 //-----------------------------------------------------------------------------