1 #include "mysql_store.h"
3 #include "stg/common.h"
4 #include "stg/user_ips.h"
5 #include "stg/user_conf.h"
6 #include "stg/user_stat.h"
7 #include "stg/admin_conf.h"
8 #include "stg/tariff_conf.h"
9 #include "stg/blowfish.h"
10 #include "stg/logger.h"
18 #include <mysql/errmsg.h>
20 #define adm_enc_passwd "cjeifY8m3"
26 const int pt_mega = 1024 * 1024;
27 const std::string badSyms = "'`";
28 const char repSym = '\"';
29 const int RepitTimes = 3;
32 int GetInt(const std::string & str, T * val, T defaultVal = T())
36 *val = static_cast<T>(strtoll(str.c_str(), &res, 10));
40 *val = defaultVal; //Error!
47 int GetDouble(const std::string & str, double * val, double defaultVal)
51 *val = strtod(str.c_str(), &res);
55 *val = defaultVal; //Error!
62 int GetTime(const std::string & str, time_t * val, time_t defaultVal)
66 *val = strtol(str.c_str(), &res, 10);
70 *val = defaultVal; //Error!
77 //-----------------------------------------------------------------------------
78 std::string ReplaceStr(std::string source, const std::string & symlist, const char chgsym)
80 std::string::size_type pos=0;
82 while( (pos = source.find_first_of(symlist,pos)) != std::string::npos)
83 source.replace(pos, 1,1, chgsym);
88 int GetULongLongInt(const std::string & str, uint64_t * val, uint64_t defaultVal)
92 *val = strtoull(str.c_str(), &res, 10);
96 *val = defaultVal; //Error!
105 extern "C" STG::Store* GetStore()
107 static MYSQL_STORE plugin;
110 //-----------------------------------------------------------------------------
111 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
115 //-----------------------------------------------------------------------------
116 int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<STG::ParamValue> & moduleParams,
117 const std::string & name, std::string & result)
121 std::vector<STG::ParamValue>::const_iterator pvi;
122 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
123 if (pvi == moduleParams.end() || pvi->value.empty())
125 errorStr = "Parameter \'" + name + "\' not found.";
129 result = pvi->value[0];
133 //-----------------------------------------------------------------------------
134 int MYSQL_STORE_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
136 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
137 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
139 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
140 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
142 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
143 ParseParam(s.moduleParams, "dbname", dbName) < 0)
145 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
146 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
151 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
154 MYSQL_STORE::MYSQL_STORE()
155 : version("mysql_store v.0.67"),
157 logger(STG::PluginLogger::get("store_mysql"))
160 //-----------------------------------------------------------------------------
161 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
165 if( (ret = mysql_query(sock,sQuery)) )
167 for(int i=0; i<RepitTimes; i++)
169 if( (ret = mysql_query(sock,sQuery)) )
170 ;//need to send error result
178 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
181 int MYSQL_STORE::ParseSettings()
183 int ret = storeSettings.ParseSettings(settings);
187 errorStr = storeSettings.GetStrError();
190 if(storeSettings.GetDBPassword().length() == 0)
192 errorStr = "Database password must be not empty. Please read Manual.";
196 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
197 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
200 errorStr = "Couldn't connect to mysql engine! With error:\n";
201 errorStr += mysql_error(&mysql);
207 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
209 std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
211 if(MysqlQuery(res.c_str(),sock))
213 errorStr = "Couldn't create database! With error:\n";
214 errorStr += mysql_error(sock);
220 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
222 errorStr = "Couldn't select database! With error:\n";
223 errorStr += mysql_error(sock);
228 ret = CheckAllTables(sock);
233 ret = CheckAllTables(sock);
237 logger("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
245 //-----------------------------------------------------------------------------
246 bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
250 if (!(result=mysql_list_tables(sock,str.c_str() )))
252 errorStr = "Couldn't get tables list With error:\n";
253 errorStr += mysql_error(sock);
258 my_ulonglong num_rows = mysql_num_rows(result);
261 mysql_free_result(result);
263 return num_rows == 1;
265 //-----------------------------------------------------------------------------
266 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
268 //info-------------------------------------------------------------------------
269 if(!IsTablePresent("info",sock))
271 sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
273 if(MysqlQuery(qbuf,sock))
275 errorStr = "Couldn't create info table With error:\n";
276 errorStr += mysql_error(sock);
281 sprintf(qbuf,"INSERT INTO info SET version=0");
283 if(MysqlQuery(qbuf,sock))
285 errorStr = "Couldn't write default version. With error:\n";
286 errorStr += mysql_error(sock);
294 std::vector<std::string> info;
295 if (GetAllParams(&info, "info", "version"))
302 GetInt(info.front(), &schemaVersion, 0);
305 //admins-----------------------------------------------------------------------
306 if(!IsTablePresent("admins",sock))
308 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
309 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
310 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
311 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
312 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
314 if(MysqlQuery(qbuf,sock))
316 errorStr = "Couldn't create admin table list With error:\n";
317 errorStr += mysql_error(sock);
322 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
323 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
324 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
326 if(MysqlQuery(qbuf,sock))
328 errorStr = "Couldn't create default admin. With error:\n";
329 errorStr += mysql_error(sock);
335 //tariffs-----------------------------------------------------------------------
336 std::string param, res;
337 if(!IsTablePresent("tariffs",sock))
339 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
341 for (int i = 0; i < DIR_NUM; i++)
343 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
346 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
349 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
352 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
355 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
358 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
361 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
364 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
368 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
369 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
370 "period VARCHAR(32) NOT NULL DEFAULT 'month',"
371 "change_policy VARCHAR(32) NOT NULL DEFAULT 'allow',"
372 "change_policy_timeout TIMESTAMP NOT NULL DEFAULT 0)";
374 if(MysqlQuery(res.c_str(),sock))
376 errorStr = "Couldn't create tariffs table list With error:\n";
377 errorStr += mysql_error(sock);
382 res = "INSERT INTO tariffs SET name='tariff',";
384 for (int i = 0; i < DIR_NUM; i++)
386 strprintf(¶m, " NoDiscount%d=1,", i);
389 strprintf(¶m, " Threshold%d=0,", i);
392 strprintf(¶m, " Time%d='0:0-0:0',", i);
397 strprintf(¶m, " SinglePrice%d=0,", i);
403 strprintf(¶m, " PriceDayA%d=0.0,", i);
408 strprintf(¶m, " PriceDayB%d=0.0,", i);
414 strprintf(¶m, " PriceNightA%d=0.0,", i);
419 strprintf(¶m, " PriceNightB%d=0.0,", i);
424 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
425 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
426 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month',"\
427 "change_policy='allow', change_policy_timeout=0";
429 if(MysqlQuery(res.c_str(),sock))
431 errorStr = "Couldn't create default tariff. With error:\n";
432 errorStr += mysql_error(sock);
437 sprintf(qbuf,"UPDATE info SET version=1");
439 if(MysqlQuery(qbuf,sock))
441 errorStr = "Couldn't write default version. With error:\n";
442 errorStr += mysql_error(sock);
449 //users-----------------------------------------------------------------------
450 if(!IsTablePresent("users",sock))
452 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
453 "Passive INT(3) DEFAULT 0,Down INT(3) DEFAULT 0,DisabledDetailStat INT(3) DEFAULT 0,AlwaysOnline INT(3) DEFAULT 0,Tariff VARCHAR(40) NOT NULL DEFAULT '',"\
454 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
455 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
456 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
458 for (int i = 0; i < USERDATA_NUM; i++)
460 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
464 param = " CreditExpire INT(11) DEFAULT 0,";
467 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
470 for (int i = 0; i < DIR_NUM; i++)
472 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
475 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
479 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
480 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
481 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
482 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
485 if(MysqlQuery(res.c_str(),sock))
487 errorStr = "Couldn't create users table list With error:\n";
488 errorStr += mysql_error(sock);
489 errorStr += "\n\n" + res;
494 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
495 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
496 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
497 "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
499 for (int i = 0; i < USERDATA_NUM; i++)
501 strprintf(¶m, " Userdata%d='',", i);
505 for (int i = 0; i < DIR_NUM; i++)
507 strprintf(¶m, " D%d=0,", i);
510 strprintf(¶m, " U%d=0,", i);
514 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
515 "LastCashAddTime=0, PassiveTime=0";
517 if(MysqlQuery(res.c_str(),sock))
519 errorStr = "Couldn't create default user. With error:\n";
520 errorStr += mysql_error(sock);
526 //logs-----------------------------------------------------------------------
527 if(!IsTablePresent("logs"))
529 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
533 errorStr = "Couldn't create admin table list With error:\n";
534 errorStr += mysql_error(sock);
539 //messages---------------------------------------------------------------------
540 if(!IsTablePresent("messages",sock))
542 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
543 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
544 "stgRepeat INT, repeatPeriod INT, text TEXT)");
546 if(MysqlQuery(qbuf,sock))
548 errorStr = "Couldn't create messages table. With error:\n";
549 errorStr += mysql_error(sock);
555 //month_stat-------------------------------------------------------------------
556 if(!IsTablePresent("stat",sock))
558 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
560 for (int i = 0; i < DIR_NUM; i++)
562 strprintf(¶m, " U%d BIGINT,", i);
565 strprintf(¶m, " D%d BIGINT,", i);
569 res += " cash DOUBLE, INDEX (login))";
571 if(MysqlQuery(res.c_str(),sock))
573 errorStr = "Couldn't create stat table. With error:\n";
574 errorStr += mysql_error(sock);
582 //-----------------------------------------------------------------------------
583 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
585 if (schemaVersion < 1)
587 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
589 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
590 errorStr += mysql_error(sock);
594 if (MysqlQuery("UPDATE info SET version = 1", sock))
596 errorStr = "Couldn't update DB schema version to 1. With error:\n";
597 errorStr += mysql_error(sock);
602 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
605 if (schemaVersion < 2)
607 if (MysqlQuery("ALTER TABLE tariffs ADD change_policy VARCHAR(32) NOT NULL DEFAULT 'allow'", sock) ||
608 MysqlQuery("ALTER TABLE tariffs ADD change_policy_timeout TIMESTAMP NOT NULL DEFAULT 0", 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" + std::to_string(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(STG::UserConf * 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];
855 i = STG::UserIPs::parse(ipStr);
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(STG::UserStat * 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 STG::UserConf & 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 STG::UserStat & 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 STG::DirTraff & up,
1173 const STG::DirTraff & down,
1174 const STG::DirTraff & sessionUp,
1175 const STG::DirTraff & 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 STG::UserStat & 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 STG::AdminConf & 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(STG::AdminConf * 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(STG::TariffData * 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 = STG::Tariff::parseTraffType(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 = STG::Tariff::parsePeriod(str);
1662 td->tariffConf.period = STG::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 = STG::Tariff::parseChangePolicy(str);
1680 str = row[7+8*DIR_NUM];
1681 param = "ChangePolicyTimeout";
1683 if (str.length() == 0)
1685 mysql_free_result(res);
1686 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1691 td->tariffConf.changePolicyTimeout = readTime(str);
1695 td->tariffConf.changePolicy = STG::Tariff::ALLOW;
1696 td->tariffConf.changePolicyTimeout = 0;
1699 mysql_free_result(res);
1703 //-----------------------------------------------------------------------------
1704 int MYSQL_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const
1708 std::string res="UPDATE tariffs SET";
1710 for (int i = 0; i < DIR_NUM; i++)
1712 strprintf(¶m, " PriceDayA%d=%f,", i,
1713 td.dirPrice[i].priceDayA * pt_mega);
1716 strprintf(¶m, " PriceDayB%d=%f,", i,
1717 td.dirPrice[i].priceDayB * pt_mega);
1720 strprintf(¶m, " PriceNightA%d=%f,", i,
1721 td.dirPrice[i].priceNightA * pt_mega);
1724 strprintf(¶m, " PriceNightB%d=%f,", i,
1725 td.dirPrice[i].priceNightB * pt_mega);
1728 strprintf(¶m, " Threshold%d=%d,", i,
1729 td.dirPrice[i].threshold);
1733 strprintf(¶m, " Time%d", i);
1735 strprintf(&s, "%0d:%0d-%0d:%0d",
1736 td.dirPrice[i].hDay,
1737 td.dirPrice[i].mDay,
1738 td.dirPrice[i].hNight,
1739 td.dirPrice[i].mNight);
1741 res += (param + "='" + s + "',");
1743 strprintf(¶m, " NoDiscount%d=%d,", i,
1744 td.dirPrice[i].noDiscount);
1747 strprintf(¶m, " SinglePrice%d=%d,", i,
1748 td.dirPrice[i].singlePrice);
1752 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1755 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1758 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1761 res += " TraffType='" + STG::Tariff::toString(td.tariffConf.traffType) + "'";
1763 if (schemaVersion > 0)
1764 res += ", Period='" + STG::Tariff::toString(td.tariffConf.period) + "'";
1766 if (schemaVersion > 1)
1767 res += ", change_policy='" + STG::Tariff::toString(td.tariffConf.changePolicy) + "'"\
1768 ", change_policy_timeout='" + formatTime(td.tariffConf.changePolicy) + "'";
1770 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1773 if(MysqlSetQuery(res.c_str()))
1775 errorStr = "Couldn't save tariff:\n";
1776 //errorStr += mysql_error(sock);
1782 //-----------------------------------------------------------------------------
1783 int MYSQL_STORE::WriteDetailedStat(const STG::TraffStat & statTree,
1785 const std::string & login) const
1787 std::string res, stTime, endTime, tempStr;
1794 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1802 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1804 if (!(sock=MysqlConnect())){
1809 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1811 errorStr = "Couldn't get table " + tempStr + ":\n";
1812 errorStr += mysql_error(sock);
1817 my_ulonglong num_rows = mysql_num_rows(result);
1819 mysql_free_result(result);
1823 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1824 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1825 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1826 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1827 lt->tm_mon+1, lt->tm_year+1900);
1829 if(MysqlQuery(qbuf,sock))
1831 errorStr = "Couldn't create WriteDetailedStat table:\n";
1832 errorStr += mysql_error(sock);
1841 lt1 = localtime(&lastStat);
1850 lt2 = localtime(&t);
1856 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1857 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1859 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1860 "day=%d,startTime='%s',endTime='%s',",
1861 lt->tm_mon+1, lt->tm_year+1900,
1868 STG::TraffStat::const_iterator stIter;
1869 stIter = statTree.begin();
1871 while (stIter != statTree.end())
1873 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1874 inet_ntostring(stIter->first.ip).c_str(),
1876 stIter->second.down,
1881 if( MysqlQuery((res+tempStr).c_str(),sock) )
1883 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1884 errorStr += mysql_error(sock);
1889 result=mysql_store_result(sock);
1891 mysql_free_result(result);
1898 //-----------------------------------------------------------------------------
1899 int MYSQL_STORE::AddMessage(STG::Message * msg, const std::string & login) const
1903 gettimeofday(&tv, NULL);
1905 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1907 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1909 static_cast<long long>(msg->header.id)
1912 if(MysqlSetQuery(qbuf))
1914 errorStr = "Couldn't add message:\n";
1915 //errorStr += mysql_error(sock);
1919 return EditMessage(*msg, login);
1921 //-----------------------------------------------------------------------------
1922 int MYSQL_STORE::EditMessage(const STG::Message & msg, const std::string & login) const
1926 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1927 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1928 "WHERE login='%s' AND id=%lld LIMIT 1",
1930 msg.header.lastSendTime,
1931 msg.header.creationTime,
1932 msg.header.showTime,
1934 msg.header.repeatPeriod,
1935 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1940 if(MysqlSetQuery(res.c_str()))
1942 errorStr = "Couldn't edit message:\n";
1943 //errorStr += mysql_error(sock);
1949 //-----------------------------------------------------------------------------
1950 int MYSQL_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const
1956 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1957 login.c_str(), static_cast<unsigned long long>(id));
1959 if(MysqlGetQuery(qbuf,sock))
1961 errorStr = "Couldn't GetMessage:\n";
1962 errorStr += mysql_error(sock);
1967 if (!(res=mysql_store_result(sock)))
1969 errorStr = "Couldn't GetMessage:\n";
1970 errorStr += mysql_error(sock);
1975 row = mysql_fetch_row(res);
1977 if(row[2]&&str2x(row[2], msg->header.type))
1979 mysql_free_result(res);
1980 errorStr = "Invalid value in message header for user: " + login;
1985 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1987 mysql_free_result(res);
1988 errorStr = "Invalid value in message header for user: " + login;
1993 if(row[4] && str2x(row[4], msg->header.creationTime))
1995 mysql_free_result(res);
1996 errorStr = "Invalid value in message header for user: " + login;
2001 if(row[5] && str2x(row[5], msg->header.showTime))
2003 mysql_free_result(res);
2004 errorStr = "Invalid value in message header for user: " + login;
2009 if(row[6] && str2x(row[6], msg->header.repeat))
2011 mysql_free_result(res);
2012 errorStr = "Invalid value in message header for user: " + login;
2017 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
2019 mysql_free_result(res);
2020 errorStr = "Invalid value in message header for user: " + login;
2025 msg->header.id = id;
2028 mysql_free_result(res);
2032 //-----------------------------------------------------------------------------
2033 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
2035 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
2036 login.c_str(), static_cast<long long>(id));
2038 if(MysqlSetQuery(qbuf))
2040 errorStr = "Couldn't delete Message:\n";
2041 //errorStr += mysql_error(sock);
2047 //-----------------------------------------------------------------------------
2048 int MYSQL_STORE::GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const
2053 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
2055 if(MysqlGetQuery(qbuf,sock))
2057 errorStr = "Couldn't GetMessageHdrs:\n";
2058 errorStr += mysql_error(sock);
2063 if (!(res=mysql_store_result(sock)))
2065 errorStr = "Couldn't GetMessageHdrs:\n";
2066 errorStr += mysql_error(sock);
2072 my_ulonglong num_rows = mysql_num_rows(res);
2075 for (i = 0; i < num_rows; i++)
2077 row = mysql_fetch_row(res);
2078 if (str2x(row[1], id))
2081 STG::Message::Header hdr;
2083 if(str2x(row[2], hdr.type))
2087 if(str2x(row[3], hdr.lastSendTime))
2091 if(str2x(row[4], hdr.creationTime))
2095 if(str2x(row[5], hdr.showTime))
2099 if(str2x(row[6], hdr.repeat))
2103 if(str2x(row[7], hdr.repeatPeriod))
2107 hdrsList->push_back(hdr);
2110 mysql_free_result(res);
2114 //-----------------------------------------------------------------------------
2116 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2119 int ret=MysqlGetQuery(Query,sock);
2123 //-----------------------------------------------------------------------------
2124 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2125 if (!(sock=MysqlConnect())) {
2128 return MysqlQuery(Query,sock);
2130 //-----------------------------------------------------------------------------
2131 MYSQL * MYSQL_STORE::MysqlConnect() const {
2133 if ( !(sock=mysql_init(NULL)) ){
2134 errorStr= "mysql init susck\n";
2137 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2138 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2141 errorStr = "Couldn't connect to mysql engine! With error:\n";
2142 errorStr += mysql_error(sock);
2146 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2147 errorStr = "Database lost !\n";
2153 //-----------------------------------------------------------------------------