10 #include "stg/common.h"
11 #include "stg/user_ips.h"
12 #include "stg/user_conf.h"
13 #include "stg/user_stat.h"
14 #include "stg/blowfish.h"
15 #include "stg/plugin_creator.h"
16 #include "stg/logger.h"
17 #include "mysql_store.h"
19 #define adm_enc_passwd "cjeifY8m3"
25 const int pt_mega = 1024 * 1024;
26 const std::string badSyms = "'`";
27 const char repSym = '\"';
28 const int RepitTimes = 3;
31 int GetInt(const std::string & str, T * val, T defaultVal = T())
35 *val = static_cast<T>(strtoll(str.c_str(), &res, 10));
39 *val = defaultVal; //Error!
46 int GetDouble(const std::string & str, double * val, double defaultVal)
50 *val = strtod(str.c_str(), &res);
54 *val = defaultVal; //Error!
61 int GetTime(const std::string & str, time_t * val, time_t defaultVal)
65 *val = strtol(str.c_str(), &res, 10);
69 *val = defaultVal; //Error!
76 //-----------------------------------------------------------------------------
77 std::string ReplaceStr(std::string source, const std::string & symlist, const char chgsym)
79 std::string::size_type pos=0;
81 while( (pos = source.find_first_of(symlist,pos)) != std::string::npos)
82 source.replace(pos, 1,1, chgsym);
87 int GetULongLongInt(const std::string & str, uint64_t * val, uint64_t defaultVal)
91 *val = strtoull(str.c_str(), &res, 10);
95 *val = defaultVal; //Error!
102 PLUGIN_CREATOR<MYSQL_STORE> msc;
105 extern "C" STORE * GetStore();
106 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
111 return msc.GetPlugin();
113 //-----------------------------------------------------------------------------
114 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
119 //-----------------------------------------------------------------------------
120 int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<PARAM_VALUE> & moduleParams,
121 const std::string & name, std::string & result)
125 std::vector<PARAM_VALUE>::const_iterator pvi;
126 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
127 if (pvi == moduleParams.end() || pvi->value.empty())
129 errorStr = "Parameter \'" + name + "\' not found.";
133 result = pvi->value[0];
137 //-----------------------------------------------------------------------------
138 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
140 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
141 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
143 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
144 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
146 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
147 ParseParam(s.moduleParams, "dbname", dbName) < 0)
149 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
150 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
153 std::string dbPortAsString;
154 if (ParseParam(s.moduleParams, "port", dbPortAsString) == 0 ||
155 ParseParam(s.moduleParams, "dbport", dbPortAsString) == 0)
157 if (GetInt<unsigned int>(dbPortAsString, &dbPort, 0) != 0)
159 errorStr = "Can't parse db port from string: \"" + dbPortAsString + "\"\n";
166 //-----------------------------------------------------------------------------
167 //-----------------------------------------------------------------------------
168 //-----------------------------------------------------------------------------
169 MYSQL_STORE::MYSQL_STORE()
170 : version("mysql_store v.0.67"),
172 logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
175 //-----------------------------------------------------------------------------
176 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
180 if( (ret = mysql_query(sock,sQuery)) )
182 for(int i=0; i<RepitTimes; i++)
184 if( (ret = mysql_query(sock,sQuery)) )
185 ;//need to send error result
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
196 int MYSQL_STORE::ParseSettings()
198 int ret = storeSettings.ParseSettings(settings);
202 errorStr = storeSettings.GetStrError();
205 if(storeSettings.GetDBPassword().length() == 0)
207 errorStr = "Database password must be not empty. Please read Manual.";
211 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
212 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
213 0,storeSettings.GetDBPort(),NULL,0)))
215 errorStr = "Couldn't connect to mysql engine! With error:\n";
216 errorStr += mysql_error(&mysql);
222 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
224 std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
226 if(MysqlQuery(res.c_str(),sock))
228 errorStr = "Couldn't create database! With error:\n";
229 errorStr += mysql_error(sock);
235 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
237 errorStr = "Couldn't select database! With error:\n";
238 errorStr += mysql_error(sock);
243 ret = CheckAllTables(sock);
248 ret = CheckAllTables(sock);
252 logger("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
260 //-----------------------------------------------------------------------------
261 bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
265 if (!(result=mysql_list_tables(sock,str.c_str() )))
267 errorStr = "Couldn't get tables list With error:\n";
268 errorStr += mysql_error(sock);
273 my_ulonglong num_rows = mysql_num_rows(result);
276 mysql_free_result(result);
278 return num_rows == 1;
280 //-----------------------------------------------------------------------------
281 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
283 //info-------------------------------------------------------------------------
284 if(!IsTablePresent("info",sock))
286 sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
288 if(MysqlQuery(qbuf,sock))
290 errorStr = "Couldn't create info table With error:\n";
291 errorStr += mysql_error(sock);
296 sprintf(qbuf,"INSERT INTO info SET version=0");
298 if(MysqlQuery(qbuf,sock))
300 errorStr = "Couldn't write default version. With error:\n";
301 errorStr += mysql_error(sock);
309 std::vector<std::string> info;
310 if (GetAllParams(&info, "info", "version"))
317 GetInt(info.front(), &schemaVersion, 0);
320 //admins-----------------------------------------------------------------------
321 if(!IsTablePresent("admins",sock))
323 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
324 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
325 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
326 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
327 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
329 if(MysqlQuery(qbuf,sock))
331 errorStr = "Couldn't create admin table list With error:\n";
332 errorStr += mysql_error(sock);
337 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
338 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
339 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
341 if(MysqlQuery(qbuf,sock))
343 errorStr = "Couldn't create default admin. With error:\n";
344 errorStr += mysql_error(sock);
350 //tariffs-----------------------------------------------------------------------
351 std::string param, res;
352 if(!IsTablePresent("tariffs",sock))
354 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
356 for (int i = 0; i < DIR_NUM; i++)
358 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
361 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
364 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
367 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
370 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
373 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
376 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
379 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
383 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
384 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
385 "period VARCHAR(32) NOT NULL DEFAULT 'month',"
386 "change_policy VARCHAR(32) NOT NULL DEFAULT 'allow',"
387 "change_policy_timeout TIMESTAMP NOT NULL DEFAULT 0)";
389 if(MysqlQuery(res.c_str(),sock))
391 errorStr = "Couldn't create tariffs table list With error:\n";
392 errorStr += mysql_error(sock);
397 res = "INSERT INTO tariffs SET name='tariff',";
399 for (int i = 0; i < DIR_NUM; i++)
401 strprintf(¶m, " NoDiscount%d=1,", i);
404 strprintf(¶m, " Threshold%d=0,", i);
407 strprintf(¶m, " Time%d='0:0-0:0',", i);
412 strprintf(¶m, " SinglePrice%d=0,", i);
418 strprintf(¶m, " PriceDayA%d=0.0,", i);
423 strprintf(¶m, " PriceDayB%d=0.0,", i);
429 strprintf(¶m, " PriceNightA%d=0.0,", i);
434 strprintf(¶m, " PriceNightB%d=0.0,", i);
439 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
440 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
441 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month',"\
442 "change_policy='allow', change_policy_timeout=0";
444 if(MysqlQuery(res.c_str(),sock))
446 errorStr = "Couldn't create default tariff. With error:\n";
447 errorStr += mysql_error(sock);
452 sprintf(qbuf,"UPDATE info SET version=1");
454 if(MysqlQuery(qbuf,sock))
456 errorStr = "Couldn't write default version. With error:\n";
457 errorStr += mysql_error(sock);
464 //users-----------------------------------------------------------------------
465 if(!IsTablePresent("users",sock))
467 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
468 "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 '',"\
469 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
470 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
471 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
473 for (int i = 0; i < USERDATA_NUM; i++)
475 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
479 param = " CreditExpire INT(11) DEFAULT 0,";
482 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
485 for (int i = 0; i < DIR_NUM; i++)
487 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
490 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
494 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
495 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
496 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
497 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
500 if(MysqlQuery(res.c_str(),sock))
502 errorStr = "Couldn't create users table list With error:\n";
503 errorStr += mysql_error(sock);
504 errorStr += "\n\n" + res;
509 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
510 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
511 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
512 "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
514 for (int i = 0; i < USERDATA_NUM; i++)
516 strprintf(¶m, " Userdata%d='',", i);
520 for (int i = 0; i < DIR_NUM; i++)
522 strprintf(¶m, " D%d=0,", i);
525 strprintf(¶m, " U%d=0,", i);
529 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
530 "LastCashAddTime=0, PassiveTime=0";
532 if(MysqlQuery(res.c_str(),sock))
534 errorStr = "Couldn't create default user. With error:\n";
535 errorStr += mysql_error(sock);
541 //logs-----------------------------------------------------------------------
542 if(!IsTablePresent("logs"))
544 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
548 errorStr = "Couldn't create admin table list With error:\n";
549 errorStr += mysql_error(sock);
554 //messages---------------------------------------------------------------------
555 if(!IsTablePresent("messages",sock))
557 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
558 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
559 "stgRepeat INT, repeatPeriod INT, text TEXT)");
561 if(MysqlQuery(qbuf,sock))
563 errorStr = "Couldn't create messages table. With error:\n";
564 errorStr += mysql_error(sock);
570 //month_stat-------------------------------------------------------------------
571 if(!IsTablePresent("stat",sock))
573 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
575 for (int i = 0; i < DIR_NUM; i++)
577 strprintf(¶m, " U%d BIGINT,", i);
580 strprintf(¶m, " D%d BIGINT,", i);
584 res += " cash DOUBLE, INDEX (login))";
586 if(MysqlQuery(res.c_str(),sock))
588 errorStr = "Couldn't create stat table. With error:\n";
589 errorStr += mysql_error(sock);
597 //-----------------------------------------------------------------------------
598 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
600 if (schemaVersion < 1)
602 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
604 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
605 errorStr += mysql_error(sock);
609 if (MysqlQuery("UPDATE info SET version = 1", sock))
611 errorStr = "Couldn't update DB schema version to 1. With error:\n";
612 errorStr += mysql_error(sock);
617 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
620 if (schemaVersion < 2)
622 if (MysqlQuery("ALTER TABLE tariffs ADD change_policy VARCHAR(32) NOT NULL DEFAULT 'allow'", sock) ||
623 MysqlQuery("ALTER TABLE tariffs ADD change_policy_timeout TIMESTAMP NOT NULL DEFAULT 0", sock))
625 errorStr = "Couldn't update tariffs table to version 2. With error:\n";
626 errorStr += mysql_error(sock);
630 if (MysqlQuery("UPDATE info SET version = 2", sock))
632 errorStr = "Couldn't update DB schema version to 2. With error:\n";
633 errorStr += mysql_error(sock);
638 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
642 //-----------------------------------------------------------------------------
644 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList,
645 const std::string & table, const std::string & name) const
654 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
656 if(MysqlGetQuery(qbuf,sock))
658 errorStr = "Couldn't GetAllParams Query for: ";
659 errorStr += name + " - " + table + "\n";
660 errorStr += mysql_error(sock);
665 if (!(res=mysql_store_result(sock)))
667 errorStr = "Couldn't GetAllParams Results for: ";
668 errorStr += name + " - " + table + "\n";
669 errorStr += mysql_error(sock);
673 num = mysql_num_rows(res);
675 for(i = 0; i < num; i++)
677 row = mysql_fetch_row(res);
678 ParamList->push_back(row[0]);
681 mysql_free_result(res);
687 //-----------------------------------------------------------------------------
688 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
690 if(GetAllParams(usersList, "users", "login"))
695 //-----------------------------------------------------------------------------
696 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
698 if(GetAllParams(adminsList, "admins", "login"))
703 //-----------------------------------------------------------------------------
704 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
706 if(GetAllParams(tariffsList, "tariffs", "name"))
711 //-----------------------------------------------------------------------------
712 int MYSQL_STORE::AddUser(const std::string & login) const
714 std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
716 for (int i = 0; i < USERDATA_NUM; i++)
717 query += ",Userdata" + x2str(i) + "=''";
719 if(MysqlSetQuery(query.c_str()))
721 errorStr = "Couldn't add user:\n";
722 //errorStr += mysql_error(sock);
728 //-----------------------------------------------------------------------------
729 int MYSQL_STORE::DelUser(const std::string & login) const
731 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
733 if(MysqlSetQuery(qbuf))
735 errorStr = "Couldn't delete user:\n";
736 //errorStr += mysql_error(sock);
742 //-----------------------------------------------------------------------------
743 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
750 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
751 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
752 RealName, StgGroup, Credit, TariffChange, ";
754 for (int i = 0; i < USERDATA_NUM; i++)
756 sprintf(qbuf, "Userdata%d, ", i);
760 query += "CreditExpire, IP FROM users WHERE login='";
761 query += login + "' LIMIT 1";
763 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
765 if(MysqlGetQuery(query.c_str(),sock))
767 errorStr = "Couldn't restore Tariff(on query):\n";
768 errorStr += mysql_error(sock);
773 if (!(res=mysql_store_result(sock)))
775 errorStr = "Couldn't restore Tariff(on getting result):\n";
776 errorStr += mysql_error(sock);
781 if (mysql_num_rows(res) != 1)
783 errorStr = "User not found";
788 row = mysql_fetch_row(res);
790 conf->password = row[1];
792 if (conf->password.empty())
794 mysql_free_result(res);
795 errorStr = "User \'" + login + "\' password is blank.";
800 if (GetInt(row[2],&conf->passive) != 0)
802 mysql_free_result(res);
803 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
808 if (GetInt(row[3], &conf->disabled) != 0)
810 mysql_free_result(res);
811 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
816 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
818 mysql_free_result(res);
819 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
824 if (GetInt(row[5], &conf->alwaysOnline) != 0)
826 mysql_free_result(res);
827 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
832 conf->tariffName = row[6];
834 if (conf->tariffName.empty())
836 mysql_free_result(res);
837 errorStr = "User \'" + login + "\' tariff is blank.";
842 conf->address = row[7];
843 conf->phone = row[8];
844 conf->email = row[9];
845 conf->note = row[10];
846 conf->realName = row[11];
847 conf->group = row[12];
849 if (GetDouble(row[13], &conf->credit, 0) != 0)
851 mysql_free_result(res);
852 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
857 conf->nextTariff = row[14];
859 for (int i = 0; i < USERDATA_NUM; i++)
861 conf->userdata[i] = row[15+i];
864 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
866 std::string ipStr = row[16+USERDATA_NUM];
872 catch (const std::string & s)
874 mysql_free_result(res);
875 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
881 mysql_free_result(res);
886 //-----------------------------------------------------------------------------
887 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
897 for (int i = 0; i < DIR_NUM; i++)
899 sprintf(qbuf, "D%d, U%d, ", i, i);
903 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
904 FROM users WHERE login = '";
905 query += login + "'";
907 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
909 if(MysqlGetQuery(query.c_str() ,sock))
911 errorStr = "Couldn't restore UserStat(on query):\n";
912 errorStr += mysql_error(sock);
917 if (!(res=mysql_store_result(sock)))
919 errorStr = "Couldn't restore UserStat(on getting result):\n";
920 errorStr += mysql_error(sock);
925 row = mysql_fetch_row(res);
927 unsigned int startPos=0;
931 for (int i = 0; i < DIR_NUM; i++)
934 sprintf(s, "D%d", i);
935 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
937 mysql_free_result(res);
938 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
942 stat->monthDown[i] = traff;
944 sprintf(s, "U%d", i);
945 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
947 mysql_free_result(res);
948 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
952 stat->monthUp[i] = traff;
955 startPos += (2*DIR_NUM);
957 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
959 mysql_free_result(res);
960 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
965 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
967 mysql_free_result(res);
968 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
973 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
975 mysql_free_result(res);
976 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
981 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
983 mysql_free_result(res);
984 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
989 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
991 mysql_free_result(res);
992 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
997 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
999 mysql_free_result(res);
1000 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
1005 mysql_free_result(res);
1009 //-----------------------------------------------------------------------------
1010 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
1015 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
1016 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
1017 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
1018 conf.password.c_str(),
1021 conf.disabledDetailStat,
1023 conf.tariffName.c_str(),
1024 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
1025 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
1026 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
1027 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
1028 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
1029 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
1031 conf.nextTariff.c_str()
1034 for (int i = 0; i < USERDATA_NUM; i++)
1036 strprintf(¶m, " Userdata%d='%s',", i,
1037 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1041 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1044 std::ostringstream ipStr;
1047 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1050 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1053 if(MysqlSetQuery(res.c_str()))
1055 errorStr = "Couldn't save user conf:\n";
1056 //errorStr += mysql_error(sock);
1062 //-----------------------------------------------------------------------------
1063 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
1068 res = "UPDATE users SET";
1070 for (int i = 0; i < DIR_NUM; i++)
1072 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1075 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1079 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1080 " PassiveTime=%d, LastActivityTime=%d",
1084 stat.lastCashAddTime,
1086 stat.lastActivityTime
1090 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1093 if(MysqlSetQuery(res.c_str()))
1095 errorStr = "Couldn't save user stat:\n";
1096 // errorStr += mysql_error(sock);
1102 //-----------------------------------------------------------------------------
1103 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
1105 std::string res, tempStr;
1114 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1115 if (!(sock=MysqlConnect())){
1116 errorStr = "Couldn't connect to Server";
1119 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1121 errorStr = "Couldn't get table " + tempStr + ":\n";
1122 errorStr += mysql_error(sock);
1127 my_ulonglong num_rows = mysql_num_rows(result);
1129 mysql_free_result(result);
1133 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1134 lt->tm_mon+1, lt->tm_year+1900);
1136 if(MysqlQuery(qbuf,sock))
1138 errorStr = "Couldn't create WriteDetailedStat table:\n";
1139 errorStr += mysql_error(sock);
1145 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1149 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1150 lt->tm_mon+1, lt->tm_year+1900,
1151 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1153 if(MysqlQuery(send.c_str(),sock))
1155 errorStr = "Couldn't write log string:\n";
1156 errorStr += mysql_error(sock);
1164 //-----------------------------------------------------------------------------
1165 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
1166 const std::string & admLogin,
1168 const std::string & paramName,
1169 const std::string & oldValue,
1170 const std::string & newValue,
1171 const std::string & message) const
1173 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1174 + paramName + "\' parameter changed from \'" + oldValue +
1175 "\' to \'" + newValue + "\'. " + message;
1177 return WriteLogString(userLogMsg, login);
1179 //-----------------------------------------------------------------------------
1180 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1182 std::string logStr = "Connect, " + inet_ntostring(ip);
1183 return WriteLogString(logStr, login);
1185 //-----------------------------------------------------------------------------
1186 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
1187 const DIR_TRAFF & up,
1188 const DIR_TRAFF & down,
1189 const DIR_TRAFF & sessionUp,
1190 const DIR_TRAFF & sessionDown,
1193 const std::string & /*reason*/) const
1195 std::string logStr = "Disconnect, ";
1196 std::ostringstream sssu;
1197 std::ostringstream sssd;
1198 std::ostringstream ssmu;
1199 std::ostringstream ssmd;
1200 std::ostringstream sscash;
1206 sssd << sessionDown;
1210 logStr += " session upload: \'";
1211 logStr += sssu.str();
1212 logStr += "\' session download: \'";
1213 logStr += sssd.str();
1214 logStr += "\' month upload: \'";
1215 logStr += ssmu.str();
1216 logStr += "\' month download: \'";
1217 logStr += ssmd.str();
1218 logStr += "\' cash: \'";
1219 logStr += sscash.str();
1222 return WriteLogString(logStr, login);
1224 //-----------------------------------------------------------------------------
1225 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1226 const std::string & login) const
1228 std::string param, res;
1230 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1231 login.c_str(), month+1, year+1900);
1233 for (int i = 0; i < DIR_NUM; i++)
1235 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1238 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1242 strprintf(¶m, " cash=%f", stat.cash);
1245 if(MysqlSetQuery(res.c_str()))
1247 errorStr = "Couldn't SaveMonthStat:\n";
1248 //errorStr += mysql_error(sock);
1254 //-----------------------------------------------------------------------------*/
1255 int MYSQL_STORE::AddAdmin(const std::string & login) const
1257 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1259 if(MysqlSetQuery(qbuf))
1261 errorStr = "Couldn't add admin:\n";
1262 //errorStr += mysql_error(sock);
1268 //-----------------------------------------------------------------------------*/
1269 int MYSQL_STORE::DelAdmin(const std::string & login) const
1271 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1273 if(MysqlSetQuery(qbuf))
1275 errorStr = "Couldn't delete admin:\n";
1276 //errorStr += mysql_error(sock);
1282 //-----------------------------------------------------------------------------*/
1283 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1285 char passwordE[2 * ADM_PASSWD_LEN + 2];
1286 char pass[ADM_PASSWD_LEN + 1];
1287 char adminPass[ADM_PASSWD_LEN + 1];
1289 memset(pass, 0, sizeof(pass));
1290 memset(adminPass, 0, sizeof(adminPass));
1293 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1295 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1296 adminPass[ADM_PASSWD_LEN - 1] = 0;
1298 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1300 EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
1303 pass[ADM_PASSWD_LEN - 1] = 0;
1304 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1306 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1307 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1308 "WHERE login='%s' LIMIT 1",
1320 if(MysqlSetQuery(qbuf))
1322 errorStr = "Couldn't save admin:\n";
1323 //errorStr += mysql_error(sock);
1329 //-----------------------------------------------------------------------------
1330 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1332 char pass[ADM_PASSWD_LEN + 1];
1333 char password[ADM_PASSWD_LEN + 1];
1334 char passwordE[2*ADM_PASSWD_LEN + 2];
1337 memset(password, 0, sizeof(password));
1343 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1345 if(MysqlGetQuery(qbuf,sock))
1347 errorStr = "Couldn't restore admin:\n";
1348 errorStr += mysql_error(sock);
1353 if (!(res=mysql_store_result(sock)))
1355 errorStr = "Couldn't restore admin:\n";
1356 errorStr += mysql_error(sock);
1361 if ( mysql_num_rows(res) == 0)
1363 mysql_free_result(res);
1364 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1369 row = mysql_fetch_row(res);
1375 mysql_free_result(res);
1376 errorStr = "Error in parameter password";
1381 memset(passwordE, 0, sizeof(passwordE));
1382 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1384 memset(pass, 0, sizeof(pass));
1386 if (passwordE[0] != 0)
1388 Decode21(pass, passwordE);
1389 InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1391 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1393 DecryptBlock(password + 8*i, pass + 8*i, &ctx);
1401 ac->password = password;
1405 if (GetInt(row[2], &a) == 0)
1406 ac->priv.userConf = a;
1409 mysql_free_result(res);
1410 errorStr = "Error in parameter ChgConf";
1415 if (GetInt(row[3], &a) == 0)
1416 ac->priv.userPasswd = a;
1419 mysql_free_result(res);
1420 errorStr = "Error in parameter ChgPassword";
1425 if (GetInt(row[4], &a) == 0)
1426 ac->priv.userStat = a;
1429 mysql_free_result(res);
1430 errorStr = "Error in parameter ChgStat";
1435 if (GetInt(row[5], &a) == 0)
1436 ac->priv.userCash = a;
1439 mysql_free_result(res);
1440 errorStr = "Error in parameter ChgCash";
1445 if (GetInt(row[6], &a) == 0)
1446 ac->priv.userAddDel = a;
1449 mysql_free_result(res);
1450 errorStr = "Error in parameter UsrAddDel";
1455 if (GetInt(row[7], &a) == 0)
1456 ac->priv.tariffChg = a;
1459 mysql_free_result(res);
1460 errorStr = "Error in parameter ChgTariff";
1465 if (GetInt(row[8], &a) == 0)
1466 ac->priv.adminChg = a;
1469 mysql_free_result(res);
1470 errorStr = "Error in parameter ChgAdmin";
1475 mysql_free_result(res);
1479 //-----------------------------------------------------------------------------
1480 int MYSQL_STORE::AddTariff(const std::string & name) const
1482 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1484 if(MysqlSetQuery(qbuf))
1486 errorStr = "Couldn't add tariff:\n";
1487 // errorStr += mysql_error(sock);
1493 //-----------------------------------------------------------------------------
1494 int MYSQL_STORE::DelTariff(const std::string & name) const
1496 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1498 if(MysqlSetQuery(qbuf))
1500 errorStr = "Couldn't delete tariff: ";
1501 // errorStr += mysql_error(sock);
1507 //-----------------------------------------------------------------------------
1508 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1513 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1515 if(MysqlGetQuery(qbuf,sock))
1517 errorStr = "Couldn't restore Tariff:\n";
1518 errorStr += mysql_error(sock);
1523 if (!(res=mysql_store_result(sock)))
1525 errorStr = "Couldn't restore Tariff:\n";
1526 errorStr += mysql_error(sock);
1532 td->tariffConf.name = tariffName;
1534 row = mysql_fetch_row(res);
1537 for (int i = 0; i<DIR_NUM; i++)
1539 strprintf(¶m, "Time%d", i);
1541 if (str.length() == 0)
1543 mysql_free_result(res);
1544 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1549 ParseTariffTimeStr(str.c_str(),
1550 td->dirPrice[i].hDay,
1551 td->dirPrice[i].mDay,
1552 td->dirPrice[i].hNight,
1553 td->dirPrice[i].mNight);
1555 strprintf(¶m, "PriceDayA%d", i);
1556 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1558 mysql_free_result(res);
1559 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1563 td->dirPrice[i].priceDayA /= (1024*1024);
1565 strprintf(¶m, "PriceDayB%d", i);
1566 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1568 mysql_free_result(res);
1569 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1573 td->dirPrice[i].priceDayB /= (1024*1024);
1575 strprintf(¶m, "PriceNightA%d", i);
1576 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1578 mysql_free_result(res);
1579 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1583 td->dirPrice[i].priceNightA /= (1024*1024);
1585 strprintf(¶m, "PriceNightB%d", i);
1586 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1588 mysql_free_result(res);
1589 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1593 td->dirPrice[i].priceNightB /= (1024*1024);
1595 strprintf(¶m, "Threshold%d", i);
1596 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
1598 mysql_free_result(res);
1599 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1604 strprintf(¶m, "SinglePrice%d", i);
1605 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
1607 mysql_free_result(res);
1608 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1613 strprintf(¶m, "NoDiscount%d", i);
1614 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
1616 mysql_free_result(res);
1617 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1623 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1625 mysql_free_result(res);
1626 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1631 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1633 mysql_free_result(res);
1634 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1639 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1641 mysql_free_result(res);
1642 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1647 str = row[4+8*DIR_NUM];
1648 param = "TraffType";
1650 if (str.length() == 0)
1652 mysql_free_result(res);
1653 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1658 td->tariffConf.traffType = TARIFF::StringToTraffType(str);
1660 if (schemaVersion > 0)
1662 str = row[5+8*DIR_NUM];
1665 if (str.length() == 0)
1667 mysql_free_result(res);
1668 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1673 td->tariffConf.period = TARIFF::StringToPeriod(str);
1677 td->tariffConf.period = TARIFF::MONTH;
1680 if (schemaVersion > 1)
1682 str = row[6+8*DIR_NUM];
1683 param = "ChangePolicy";
1685 if (str.length() == 0)
1687 mysql_free_result(res);
1688 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1693 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
1695 str = row[7+8*DIR_NUM];
1696 param = "ChangePolicyTimeout";
1698 if (str.length() == 0)
1700 mysql_free_result(res);
1701 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1706 td->tariffConf.changePolicyTimeout = readTime(str);
1710 td->tariffConf.changePolicy = TARIFF::ALLOW;
1711 td->tariffConf.changePolicyTimeout = 0;
1714 mysql_free_result(res);
1718 //-----------------------------------------------------------------------------
1719 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1723 std::string res="UPDATE tariffs SET";
1725 for (int i = 0; i < DIR_NUM; i++)
1727 strprintf(¶m, " PriceDayA%d=%f,", i,
1728 td.dirPrice[i].priceDayA * pt_mega);
1731 strprintf(¶m, " PriceDayB%d=%f,", i,
1732 td.dirPrice[i].priceDayB * pt_mega);
1735 strprintf(¶m, " PriceNightA%d=%f,", i,
1736 td.dirPrice[i].priceNightA * pt_mega);
1739 strprintf(¶m, " PriceNightB%d=%f,", i,
1740 td.dirPrice[i].priceNightB * pt_mega);
1743 strprintf(¶m, " Threshold%d=%d,", i,
1744 td.dirPrice[i].threshold);
1748 strprintf(¶m, " Time%d", i);
1750 strprintf(&s, "%0d:%0d-%0d:%0d",
1751 td.dirPrice[i].hDay,
1752 td.dirPrice[i].mDay,
1753 td.dirPrice[i].hNight,
1754 td.dirPrice[i].mNight);
1756 res += (param + "='" + s + "',");
1758 strprintf(¶m, " NoDiscount%d=%d,", i,
1759 td.dirPrice[i].noDiscount);
1762 strprintf(¶m, " SinglePrice%d=%d,", i,
1763 td.dirPrice[i].singlePrice);
1767 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1770 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1773 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1776 res += " TraffType='" + TARIFF::TraffTypeToString(td.tariffConf.traffType) + "'";
1778 if (schemaVersion > 0)
1779 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1781 if (schemaVersion > 1)
1782 res += ", change_policy='" + TARIFF::ChangePolicyToString(td.tariffConf.changePolicy) + "'"\
1783 ", change_policy_timeout='" + formatTime(td.tariffConf.changePolicy) + "'";
1785 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1788 if(MysqlSetQuery(res.c_str()))
1790 errorStr = "Couldn't save tariff:\n";
1791 //errorStr += mysql_error(sock);
1797 //-----------------------------------------------------------------------------
1798 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1800 const std::string & login) const
1802 std::string res, stTime, endTime, tempStr;
1809 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1817 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1819 if (!(sock=MysqlConnect())){
1824 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1826 errorStr = "Couldn't get table " + tempStr + ":\n";
1827 errorStr += mysql_error(sock);
1832 my_ulonglong num_rows = mysql_num_rows(result);
1834 mysql_free_result(result);
1838 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1839 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1840 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1841 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1842 lt->tm_mon+1, lt->tm_year+1900);
1844 if(MysqlQuery(qbuf,sock))
1846 errorStr = "Couldn't create WriteDetailedStat table:\n";
1847 errorStr += mysql_error(sock);
1856 lt1 = localtime(&lastStat);
1865 lt2 = localtime(&t);
1871 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1872 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1874 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1875 "day=%d,startTime='%s',endTime='%s',",
1876 lt->tm_mon+1, lt->tm_year+1900,
1883 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1884 stIter = statTree.begin();
1886 while (stIter != statTree.end())
1888 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1889 inet_ntostring(stIter->first.ip).c_str(),
1891 stIter->second.down,
1896 if( MysqlQuery((res+tempStr).c_str(),sock) )
1898 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1899 errorStr += mysql_error(sock);
1904 result=mysql_store_result(sock);
1906 mysql_free_result(result);
1913 //-----------------------------------------------------------------------------
1914 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1918 gettimeofday(&tv, NULL);
1920 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1922 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1924 static_cast<long long>(msg->header.id)
1927 if(MysqlSetQuery(qbuf))
1929 errorStr = "Couldn't add message:\n";
1930 //errorStr += mysql_error(sock);
1934 return EditMessage(*msg, login);
1936 //-----------------------------------------------------------------------------
1937 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1941 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1942 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1943 "WHERE login='%s' AND id=%lld LIMIT 1",
1945 msg.header.lastSendTime,
1946 msg.header.creationTime,
1947 msg.header.showTime,
1949 msg.header.repeatPeriod,
1950 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1955 if(MysqlSetQuery(res.c_str()))
1957 errorStr = "Couldn't edit message:\n";
1958 //errorStr += mysql_error(sock);
1964 //-----------------------------------------------------------------------------
1965 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1971 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1972 login.c_str(), static_cast<unsigned long long>(id));
1974 if(MysqlGetQuery(qbuf,sock))
1976 errorStr = "Couldn't GetMessage:\n";
1977 errorStr += mysql_error(sock);
1982 if (!(res=mysql_store_result(sock)))
1984 errorStr = "Couldn't GetMessage:\n";
1985 errorStr += mysql_error(sock);
1990 row = mysql_fetch_row(res);
1992 if(row[2]&&str2x(row[2], msg->header.type))
1994 mysql_free_result(res);
1995 errorStr = "Invalid value in message header for user: " + login;
2000 if(row[3] && str2x(row[3], msg->header.lastSendTime))
2002 mysql_free_result(res);
2003 errorStr = "Invalid value in message header for user: " + login;
2008 if(row[4] && str2x(row[4], msg->header.creationTime))
2010 mysql_free_result(res);
2011 errorStr = "Invalid value in message header for user: " + login;
2016 if(row[5] && str2x(row[5], msg->header.showTime))
2018 mysql_free_result(res);
2019 errorStr = "Invalid value in message header for user: " + login;
2024 if(row[6] && str2x(row[6], msg->header.repeat))
2026 mysql_free_result(res);
2027 errorStr = "Invalid value in message header for user: " + login;
2032 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
2034 mysql_free_result(res);
2035 errorStr = "Invalid value in message header for user: " + login;
2040 msg->header.id = id;
2043 mysql_free_result(res);
2047 //-----------------------------------------------------------------------------
2048 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
2050 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
2051 login.c_str(), static_cast<long long>(id));
2053 if(MysqlSetQuery(qbuf))
2055 errorStr = "Couldn't delete Message:\n";
2056 //errorStr += mysql_error(sock);
2062 //-----------------------------------------------------------------------------
2063 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
2068 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
2070 if(MysqlGetQuery(qbuf,sock))
2072 errorStr = "Couldn't GetMessageHdrs:\n";
2073 errorStr += mysql_error(sock);
2078 if (!(res=mysql_store_result(sock)))
2080 errorStr = "Couldn't GetMessageHdrs:\n";
2081 errorStr += mysql_error(sock);
2087 my_ulonglong num_rows = mysql_num_rows(res);
2090 for (i = 0; i < num_rows; i++)
2092 row = mysql_fetch_row(res);
2093 if (str2x(row[1], id))
2098 if(str2x(row[2], hdr.type))
2102 if(str2x(row[3], hdr.lastSendTime))
2106 if(str2x(row[4], hdr.creationTime))
2110 if(str2x(row[5], hdr.showTime))
2114 if(str2x(row[6], hdr.repeat))
2118 if(str2x(row[7], hdr.repeatPeriod))
2122 hdrsList->push_back(hdr);
2125 mysql_free_result(res);
2129 //-----------------------------------------------------------------------------
2131 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2134 int ret=MysqlGetQuery(Query,sock);
2138 //-----------------------------------------------------------------------------
2139 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2140 if (!(sock=MysqlConnect())) {
2143 return MysqlQuery(Query,sock);
2145 //-----------------------------------------------------------------------------
2146 MYSQL * MYSQL_STORE::MysqlConnect() const {
2148 if ( !(sock=mysql_init(NULL)) ){
2149 errorStr= "mysql init susck\n";
2152 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2153 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2154 0,storeSettings.GetDBPort(),NULL,0)))
2156 errorStr = "Couldn't connect to mysql engine! With error:\n";
2157 errorStr += mysql_error(sock);
2161 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2162 errorStr = "Database lost !\n";
2168 //-----------------------------------------------------------------------------