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()
122 //-----------------------------------------------------------------------------
123 int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<PARAM_VALUE> & moduleParams,
124 const std::string & name, std::string & result)
128 std::vector<PARAM_VALUE>::const_iterator pvi;
129 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
130 if (pvi == moduleParams.end())
132 errorStr = "Parameter \'" + name + "\' not found.";
136 result = pvi->value[0];
140 //-----------------------------------------------------------------------------
141 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
143 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
144 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
146 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
147 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
149 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
150 ParseParam(s.moduleParams, "dbname", dbName) < 0)
152 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
153 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
158 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
161 MYSQL_STORE::MYSQL_STORE()
163 version("mysql_store v.0.67"),
166 logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
169 //-----------------------------------------------------------------------------
170 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
174 if( (ret = mysql_query(sock,sQuery)) )
176 for(int i=0; i<RepitTimes; i++)
178 if( (ret = mysql_query(sock,sQuery)) )
179 ;//need to send error result
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
190 int MYSQL_STORE::ParseSettings()
192 int ret = storeSettings.ParseSettings(settings);
197 errorStr = storeSettings.GetStrError();
200 if(storeSettings.GetDBPassword().length() == 0)
202 errorStr = "Database password must be not empty. Please read Manual.";
206 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
207 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
210 errorStr = "Couldn't connect to mysql engine! With error:\n";
211 errorStr += mysql_error(&mysql);
217 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
219 std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
221 if(MysqlQuery(res.c_str(),sock))
223 errorStr = "Couldn't create database! With error:\n";
224 errorStr += mysql_error(sock);
230 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
232 errorStr = "Couldn't select database! With error:\n";
233 errorStr += mysql_error(sock);
237 ret = CheckAllTables(sock);
242 ret = CheckAllTables(sock);
246 logger("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
254 //-----------------------------------------------------------------------------
255 bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
259 if (!(result=mysql_list_tables(sock,str.c_str() )))
261 errorStr = "Couldn't get tables list With error:\n";
262 errorStr += mysql_error(sock);
267 my_ulonglong num_rows = mysql_num_rows(result);
270 mysql_free_result(result);
272 return num_rows == 1;
274 //-----------------------------------------------------------------------------
275 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
277 //info-------------------------------------------------------------------------
278 if(!IsTablePresent("info",sock))
280 sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
282 if(MysqlQuery(qbuf,sock))
284 errorStr = "Couldn't create info table With error:\n";
285 errorStr += mysql_error(sock);
290 sprintf(qbuf,"INSERT INTO info SET version=0");
292 if(MysqlQuery(qbuf,sock))
294 errorStr = "Couldn't write default version. With error:\n";
295 errorStr += mysql_error(sock);
303 std::vector<std::string> info;
304 if (GetAllParams(&info, "info", "version"))
311 GetInt(info.front(), &schemaVersion, 0);
314 //admins-----------------------------------------------------------------------
315 if(!IsTablePresent("admins",sock))
317 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
318 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
319 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
320 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
321 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
323 if(MysqlQuery(qbuf,sock))
325 errorStr = "Couldn't create admin table list With error:\n";
326 errorStr += mysql_error(sock);
331 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
332 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
333 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
335 if(MysqlQuery(qbuf,sock))
337 errorStr = "Couldn't create default admin. With error:\n";
338 errorStr += mysql_error(sock);
344 //tariffs-----------------------------------------------------------------------
345 std::string param, res;
346 if(!IsTablePresent("tariffs",sock))
348 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
350 for (int i = 0; i < DIR_NUM; i++)
352 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
355 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
358 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
361 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
364 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
367 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
370 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
373 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
377 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
378 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
379 "period VARCHAR(32) NOT NULL DEFAULT 'month')";
381 if(MysqlQuery(res.c_str(),sock))
383 errorStr = "Couldn't create tariffs table list With error:\n";
384 errorStr += mysql_error(sock);
389 res = "INSERT INTO tariffs SET name='tariff',";
391 for (int i = 0; i < DIR_NUM; i++)
393 strprintf(¶m, " NoDiscount%d=1,", i);
396 strprintf(¶m, " Threshold%d=0,", i);
399 strprintf(¶m, " Time%d='0:0-0:0',", i);
404 strprintf(¶m, " SinglePrice%d=0,", i);
410 strprintf(¶m, " PriceDayA%d=0.0,", i);
415 strprintf(¶m, " PriceDayB%d=0.0,", i);
421 strprintf(¶m, " PriceNightA%d=0.0,", i);
426 strprintf(¶m, " PriceNightB%d=0.0,", i);
431 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
432 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
433 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'";
435 if(MysqlQuery(res.c_str(),sock))
437 errorStr = "Couldn't create default tariff. With error:\n";
438 errorStr += mysql_error(sock);
443 sprintf(qbuf,"UPDATE info SET version=1");
445 if(MysqlQuery(qbuf,sock))
447 errorStr = "Couldn't write default version. With error:\n";
448 errorStr += mysql_error(sock);
455 //users-----------------------------------------------------------------------
456 if(!IsTablePresent("users",sock))
458 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
459 "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 '',"\
460 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
461 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
462 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
464 for (int i = 0; i < USERDATA_NUM; i++)
466 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
470 param = " CreditExpire INT(11) DEFAULT 0,";
473 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
476 for (int i = 0; i < DIR_NUM; i++)
478 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
481 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
485 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
486 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
487 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
488 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
491 if(MysqlQuery(res.c_str(),sock))
493 errorStr = "Couldn't create users table list With error:\n";
494 errorStr += mysql_error(sock);
495 errorStr += "\n\n" + res;
500 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
501 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
502 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
503 "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
505 for (int i = 0; i < USERDATA_NUM; i++)
507 strprintf(¶m, " Userdata%d='',", i);
511 for (int i = 0; i < DIR_NUM; i++)
513 strprintf(¶m, " D%d=0,", i);
516 strprintf(¶m, " U%d=0,", i);
520 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
521 "LastCashAddTime=0, PassiveTime=0";
523 if(MysqlQuery(res.c_str(),sock))
525 errorStr = "Couldn't create default user. With error:\n";
526 errorStr += mysql_error(sock);
532 //logs-----------------------------------------------------------------------
533 if(!IsTablePresent("logs"))
535 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
539 errorStr = "Couldn't create admin table list With error:\n";
540 errorStr += mysql_error(sock);
545 //messages---------------------------------------------------------------------
546 if(!IsTablePresent("messages",sock))
548 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
549 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
550 "stgRepeat INT, repeatPeriod INT, text TEXT)");
552 if(MysqlQuery(qbuf,sock))
554 errorStr = "Couldn't create messages table. With error:\n";
555 errorStr += mysql_error(sock);
561 //month_stat-------------------------------------------------------------------
562 if(!IsTablePresent("stat",sock))
564 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
566 for (int i = 0; i < DIR_NUM; i++)
568 strprintf(¶m, " U%d BIGINT,", i);
571 strprintf(¶m, " D%d BIGINT,", i);
575 res += " cash DOUBLE, INDEX (login))";
577 if(MysqlQuery(res.c_str(),sock))
579 errorStr = "Couldn't create stat table. With error:\n";
580 errorStr += mysql_error(sock);
588 //-----------------------------------------------------------------------------
589 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
591 if (schemaVersion < 1)
593 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
595 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
596 errorStr += mysql_error(sock);
600 if (MysqlQuery("UPDATE info SET version = 1", sock))
602 errorStr = "Couldn't update DB schema version to 1. With error:\n";
603 errorStr += mysql_error(sock);
608 logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
612 //-----------------------------------------------------------------------------
614 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList,
615 const std::string & table, const std::string & name) const
624 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
626 if(MysqlGetQuery(qbuf,sock))
628 errorStr = "Couldn't GetAllParams Query for: ";
629 errorStr += name + " - " + table + "\n";
630 errorStr += mysql_error(sock);
635 if (!(res=mysql_store_result(sock)))
637 errorStr = "Couldn't GetAllParams Results for: ";
638 errorStr += name + " - " + table + "\n";
639 errorStr += mysql_error(sock);
643 num = mysql_num_rows(res);
645 for(i = 0; i < num; i++)
647 row = mysql_fetch_row(res);
648 ParamList->push_back(row[0]);
651 mysql_free_result(res);
657 //-----------------------------------------------------------------------------
658 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
660 if(GetAllParams(usersList, "users", "login"))
665 //-----------------------------------------------------------------------------
666 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
668 if(GetAllParams(adminsList, "admins", "login"))
673 //-----------------------------------------------------------------------------
674 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
676 if(GetAllParams(tariffsList, "tariffs", "name"))
681 //-----------------------------------------------------------------------------
682 int MYSQL_STORE::AddUser(const std::string & login) const
684 std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
686 for (int i = 0; i < USERDATA_NUM; i++)
687 query += ",Userdata" + x2str(i) + "=''";
689 if(MysqlSetQuery(query.c_str()))
691 errorStr = "Couldn't add user:\n";
692 //errorStr += mysql_error(sock);
698 //-----------------------------------------------------------------------------
699 int MYSQL_STORE::DelUser(const std::string & login) const
701 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
703 if(MysqlSetQuery(qbuf))
705 errorStr = "Couldn't delete user:\n";
706 //errorStr += mysql_error(sock);
712 //-----------------------------------------------------------------------------
713 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
720 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
721 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
722 RealName, StgGroup, Credit, TariffChange, ";
724 for (int i = 0; i < USERDATA_NUM; i++)
726 sprintf(qbuf, "Userdata%d, ", i);
730 query += "CreditExpire, IP FROM users WHERE login='";
731 query += login + "' LIMIT 1";
733 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
735 if(MysqlGetQuery(query.c_str(),sock))
737 errorStr = "Couldn't restore Tariff(on query):\n";
738 errorStr += mysql_error(sock);
743 if (!(res=mysql_store_result(sock)))
745 errorStr = "Couldn't restore Tariff(on getting result):\n";
746 errorStr += mysql_error(sock);
751 if (mysql_num_rows(res) != 1)
753 errorStr = "User not found";
758 row = mysql_fetch_row(res);
762 conf->password = row[1];
764 if (conf->password.empty())
766 mysql_free_result(res);
767 errorStr = "User \'" + login + "\' password is blank.";
772 if (GetInt(row[2],&conf->passive) != 0)
774 mysql_free_result(res);
775 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
780 if (GetInt(row[3], &conf->disabled) != 0)
782 mysql_free_result(res);
783 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
788 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
790 mysql_free_result(res);
791 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
796 if (GetInt(row[5], &conf->alwaysOnline) != 0)
798 mysql_free_result(res);
799 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
804 conf->tariffName = row[6];
806 if (conf->tariffName.empty())
808 mysql_free_result(res);
809 errorStr = "User \'" + login + "\' tariff is blank.";
814 conf->address = row[7];
815 conf->phone = row[8];
816 conf->email = row[9];
817 conf->note = row[10];
818 conf->realName = row[11];
819 conf->group = row[12];
821 if (GetDouble(row[13], &conf->credit, 0) != 0)
823 mysql_free_result(res);
824 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
829 conf->nextTariff = row[14];
831 for (int i = 0; i < USERDATA_NUM; i++)
833 conf->userdata[i] = row[15+i];
836 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
838 std::string ipStr = row[16+USERDATA_NUM];
844 catch (const std::string & s)
846 mysql_free_result(res);
847 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
853 mysql_free_result(res);
858 //-----------------------------------------------------------------------------
859 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
869 for (int i = 0; i < DIR_NUM; i++)
871 sprintf(qbuf, "D%d, U%d, ", i, i);
875 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
876 FROM users WHERE login = '";
877 query += login + "'";
879 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
881 if(MysqlGetQuery(query.c_str() ,sock))
883 errorStr = "Couldn't restore UserStat(on query):\n";
884 errorStr += mysql_error(sock);
889 if (!(res=mysql_store_result(sock)))
891 errorStr = "Couldn't restore UserStat(on getting result):\n";
892 errorStr += mysql_error(sock);
897 row = mysql_fetch_row(res);
899 unsigned int startPos=0;
903 for (int i = 0; i < DIR_NUM; i++)
906 sprintf(s, "D%d", i);
907 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
909 mysql_free_result(res);
910 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
914 stat->monthDown[i] = traff;
916 sprintf(s, "U%d", i);
917 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
919 mysql_free_result(res);
920 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
924 stat->monthUp[i] = traff;
927 startPos += (2*DIR_NUM);
929 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
931 mysql_free_result(res);
932 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
937 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
939 mysql_free_result(res);
940 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
945 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
947 mysql_free_result(res);
948 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
953 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
955 mysql_free_result(res);
956 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
961 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
963 mysql_free_result(res);
964 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
969 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
971 mysql_free_result(res);
972 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
977 mysql_free_result(res);
981 //-----------------------------------------------------------------------------
982 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
987 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
988 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
989 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
990 conf.password.c_str(),
993 conf.disabledDetailStat,
995 conf.tariffName.c_str(),
996 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
997 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
998 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
999 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
1000 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
1001 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
1003 conf.nextTariff.c_str()
1006 for (int i = 0; i < USERDATA_NUM; i++)
1008 strprintf(¶m, " Userdata%d='%s',", i,
1009 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1013 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1016 std::ostringstream ipStr;
1019 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1022 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1025 if(MysqlSetQuery(res.c_str()))
1027 errorStr = "Couldn't save user conf:\n";
1028 //errorStr += mysql_error(sock);
1034 //-----------------------------------------------------------------------------
1035 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
1040 res = "UPDATE users SET";
1042 for (int i = 0; i < DIR_NUM; i++)
1044 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1047 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1051 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1052 " PassiveTime=%d, LastActivityTime=%d",
1056 stat.lastCashAddTime,
1058 stat.lastActivityTime
1062 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1065 if(MysqlSetQuery(res.c_str()))
1067 errorStr = "Couldn't save user stat:\n";
1068 // errorStr += mysql_error(sock);
1074 //-----------------------------------------------------------------------------
1075 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
1077 std::string res, tempStr;
1086 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1087 if (!(sock=MysqlConnect())){
1088 errorStr = "Couldn't connect to Server";
1091 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1093 errorStr = "Couldn't get table " + tempStr + ":\n";
1094 errorStr += mysql_error(sock);
1099 my_ulonglong num_rows = mysql_num_rows(result);
1101 mysql_free_result(result);
1105 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1106 lt->tm_mon+1, lt->tm_year+1900);
1108 if(MysqlQuery(qbuf,sock))
1110 errorStr = "Couldn't create WriteDetailedStat table:\n";
1111 errorStr += mysql_error(sock);
1117 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1121 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1122 lt->tm_mon+1, lt->tm_year+1900,
1123 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1125 if(MysqlQuery(send.c_str(),sock))
1127 errorStr = "Couldn't write log string:\n";
1128 errorStr += mysql_error(sock);
1136 //-----------------------------------------------------------------------------
1137 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
1138 const std::string & admLogin,
1140 const std::string & paramName,
1141 const std::string & oldValue,
1142 const std::string & newValue,
1143 const std::string & message) const
1145 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1146 + paramName + "\' parameter changed from \'" + oldValue +
1147 "\' to \'" + newValue + "\'. " + message;
1149 return WriteLogString(userLogMsg, login);
1151 //-----------------------------------------------------------------------------
1152 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
1154 std::string logStr = "Connect, " + inet_ntostring(ip);
1155 return WriteLogString(logStr, login);
1157 //-----------------------------------------------------------------------------
1158 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
1159 const DIR_TRAFF & up,
1160 const DIR_TRAFF & down,
1161 const DIR_TRAFF & sessionUp,
1162 const DIR_TRAFF & sessionDown,
1165 const std::string & /*reason*/) const
1167 std::string logStr = "Disconnect, ";
1168 std::ostringstream sssu;
1169 std::ostringstream sssd;
1170 std::ostringstream ssmu;
1171 std::ostringstream ssmd;
1172 std::ostringstream sscash;
1178 sssd << sessionDown;
1182 logStr += " session upload: \'";
1183 logStr += sssu.str();
1184 logStr += "\' session download: \'";
1185 logStr += sssd.str();
1186 logStr += "\' month upload: \'";
1187 logStr += ssmu.str();
1188 logStr += "\' month download: \'";
1189 logStr += ssmd.str();
1190 logStr += "\' cash: \'";
1191 logStr += sscash.str();
1194 return WriteLogString(logStr, login);
1196 //-----------------------------------------------------------------------------
1197 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1198 const std::string & login) const
1200 std::string param, res;
1202 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1203 login.c_str(), month+1, year+1900);
1205 for (int i = 0; i < DIR_NUM; i++)
1207 strprintf(¶m, " U%d=%lld,", i, stat.monthUp[i]);
1210 strprintf(¶m, " D%d=%lld,", i, stat.monthDown[i]);
1214 strprintf(¶m, " cash=%f", stat.cash);
1217 if(MysqlSetQuery(res.c_str()))
1219 errorStr = "Couldn't SaveMonthStat:\n";
1220 //errorStr += mysql_error(sock);
1226 //-----------------------------------------------------------------------------*/
1227 int MYSQL_STORE::AddAdmin(const std::string & login) const
1229 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1231 if(MysqlSetQuery(qbuf))
1233 errorStr = "Couldn't add admin:\n";
1234 //errorStr += mysql_error(sock);
1240 //-----------------------------------------------------------------------------*/
1241 int MYSQL_STORE::DelAdmin(const std::string & login) const
1243 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1245 if(MysqlSetQuery(qbuf))
1247 errorStr = "Couldn't delete admin:\n";
1248 //errorStr += mysql_error(sock);
1254 //-----------------------------------------------------------------------------*/
1255 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1257 char passwordE[2 * ADM_PASSWD_LEN + 2];
1258 char pass[ADM_PASSWD_LEN + 1];
1259 char adminPass[ADM_PASSWD_LEN + 1];
1261 memset(pass, 0, sizeof(pass));
1262 memset(adminPass, 0, sizeof(adminPass));
1265 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1267 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1268 adminPass[ADM_PASSWD_LEN - 1] = 0;
1270 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1272 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1275 pass[ADM_PASSWD_LEN - 1] = 0;
1276 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1278 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1279 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1280 "WHERE login='%s' LIMIT 1",
1292 if(MysqlSetQuery(qbuf))
1294 errorStr = "Couldn't save admin:\n";
1295 //errorStr += mysql_error(sock);
1301 //-----------------------------------------------------------------------------
1302 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1304 char pass[ADM_PASSWD_LEN + 1];
1305 char password[ADM_PASSWD_LEN + 1];
1306 char passwordE[2*ADM_PASSWD_LEN + 2];
1309 memset(pass, 0, sizeof(pass));
1310 memset(password, 0, sizeof(password));
1311 memset(passwordE, 0, sizeof(passwordE));
1317 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1319 if(MysqlGetQuery(qbuf,sock))
1321 errorStr = "Couldn't restore admin:\n";
1322 errorStr += mysql_error(sock);
1327 if (!(res=mysql_store_result(sock)))
1329 errorStr = "Couldn't restore admin:\n";
1330 errorStr += mysql_error(sock);
1335 if ( mysql_num_rows(res) == 0)
1337 mysql_free_result(res);
1338 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1343 row = mysql_fetch_row(res);
1349 mysql_free_result(res);
1350 errorStr = "Error in parameter password";
1355 memset(passwordE, 0, sizeof(passwordE));
1356 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1358 memset(pass, 0, sizeof(pass));
1360 if (passwordE[0] != 0)
1362 Decode21(pass, passwordE);
1363 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1365 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1367 DecodeString(password + 8*i, pass + 8*i, &ctx);
1375 ac->password = password;
1379 if (GetInt(row[2], &a) == 0)
1380 ac->priv.userConf = a;
1383 mysql_free_result(res);
1384 errorStr = "Error in parameter ChgConf";
1389 if (GetInt(row[3], &a) == 0)
1390 ac->priv.userPasswd = a;
1393 mysql_free_result(res);
1394 errorStr = "Error in parameter ChgPassword";
1399 if (GetInt(row[4], &a) == 0)
1400 ac->priv.userStat = a;
1403 mysql_free_result(res);
1404 errorStr = "Error in parameter ChgStat";
1409 if (GetInt(row[5], &a) == 0)
1410 ac->priv.userCash = a;
1413 mysql_free_result(res);
1414 errorStr = "Error in parameter ChgCash";
1419 if (GetInt(row[6], &a) == 0)
1420 ac->priv.userAddDel = a;
1423 mysql_free_result(res);
1424 errorStr = "Error in parameter UsrAddDel";
1429 if (GetInt(row[7], &a) == 0)
1430 ac->priv.tariffChg = a;
1433 mysql_free_result(res);
1434 errorStr = "Error in parameter ChgTariff";
1439 if (GetInt(row[8], &a) == 0)
1440 ac->priv.adminChg = a;
1443 mysql_free_result(res);
1444 errorStr = "Error in parameter ChgAdmin";
1449 mysql_free_result(res);
1453 //-----------------------------------------------------------------------------
1454 int MYSQL_STORE::AddTariff(const std::string & name) const
1456 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1458 if(MysqlSetQuery(qbuf))
1460 errorStr = "Couldn't add tariff:\n";
1461 // errorStr += mysql_error(sock);
1467 //-----------------------------------------------------------------------------
1468 int MYSQL_STORE::DelTariff(const std::string & name) const
1470 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1472 if(MysqlSetQuery(qbuf))
1474 errorStr = "Couldn't delete tariff: ";
1475 // errorStr += mysql_error(sock);
1481 //-----------------------------------------------------------------------------
1482 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1487 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1489 if(MysqlGetQuery(qbuf,sock))
1491 errorStr = "Couldn't restore Tariff:\n";
1492 errorStr += mysql_error(sock);
1497 if (!(res=mysql_store_result(sock)))
1499 errorStr = "Couldn't restore Tariff:\n";
1500 errorStr += mysql_error(sock);
1506 td->tariffConf.name = tariffName;
1508 row = mysql_fetch_row(res);
1511 for (int i = 0; i<DIR_NUM; i++)
1513 strprintf(¶m, "Time%d", i);
1515 if (str.length() == 0)
1517 mysql_free_result(res);
1518 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1523 ParseTariffTimeStr(str.c_str(),
1524 td->dirPrice[i].hDay,
1525 td->dirPrice[i].mDay,
1526 td->dirPrice[i].hNight,
1527 td->dirPrice[i].mNight);
1529 strprintf(¶m, "PriceDayA%d", i);
1530 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1532 mysql_free_result(res);
1533 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1537 td->dirPrice[i].priceDayA /= (1024*1024);
1539 strprintf(¶m, "PriceDayB%d", i);
1540 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1542 mysql_free_result(res);
1543 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1547 td->dirPrice[i].priceDayB /= (1024*1024);
1549 strprintf(¶m, "PriceNightA%d", i);
1550 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1552 mysql_free_result(res);
1553 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1557 td->dirPrice[i].priceNightA /= (1024*1024);
1559 strprintf(¶m, "PriceNightB%d", i);
1560 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1562 mysql_free_result(res);
1563 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1567 td->dirPrice[i].priceNightB /= (1024*1024);
1569 strprintf(¶m, "Threshold%d", i);
1570 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
1572 mysql_free_result(res);
1573 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1578 strprintf(¶m, "SinglePrice%d", i);
1579 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
1581 mysql_free_result(res);
1582 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1587 strprintf(¶m, "NoDiscount%d", i);
1588 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
1590 mysql_free_result(res);
1591 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1597 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1599 mysql_free_result(res);
1600 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1605 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1607 mysql_free_result(res);
1608 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1613 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1615 mysql_free_result(res);
1616 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1621 str = row[4+8*DIR_NUM];
1622 param = "TraffType";
1624 if (str.length() == 0)
1626 mysql_free_result(res);
1627 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1632 if (!strcasecmp(str.c_str(), "up"))
1633 td->tariffConf.traffType = TRAFF_UP;
1635 if (!strcasecmp(str.c_str(), "down"))
1636 td->tariffConf.traffType = TRAFF_DOWN;
1638 if (!strcasecmp(str.c_str(), "up+down"))
1639 td->tariffConf.traffType = TRAFF_UP_DOWN;
1641 if (!strcasecmp(str.c_str(), "max"))
1642 td->tariffConf.traffType = TRAFF_MAX;
1645 mysql_free_result(res);
1646 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1651 if (schemaVersion > 0)
1653 str = row[5+8*DIR_NUM];
1656 if (str.length() == 0)
1658 mysql_free_result(res);
1659 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1664 td->tariffConf.period = TARIFF::StringToPeriod(str);
1668 td->tariffConf.period = TARIFF::MONTH;
1671 mysql_free_result(res);
1675 //-----------------------------------------------------------------------------
1676 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1680 std::string res="UPDATE tariffs SET";
1682 for (int i = 0; i < DIR_NUM; i++)
1684 strprintf(¶m, " PriceDayA%d=%f,", i,
1685 td.dirPrice[i].priceDayA * pt_mega);
1688 strprintf(¶m, " PriceDayB%d=%f,", i,
1689 td.dirPrice[i].priceDayB * pt_mega);
1692 strprintf(¶m, " PriceNightA%d=%f,", i,
1693 td.dirPrice[i].priceNightA * pt_mega);
1696 strprintf(¶m, " PriceNightB%d=%f,", i,
1697 td.dirPrice[i].priceNightB * pt_mega);
1700 strprintf(¶m, " Threshold%d=%d,", i,
1701 td.dirPrice[i].threshold);
1705 strprintf(¶m, " Time%d", i);
1707 strprintf(&s, "%0d:%0d-%0d:%0d",
1708 td.dirPrice[i].hDay,
1709 td.dirPrice[i].mDay,
1710 td.dirPrice[i].hNight,
1711 td.dirPrice[i].mNight);
1713 res += (param + "='" + s + "',");
1715 strprintf(¶m, " NoDiscount%d=%d,", i,
1716 td.dirPrice[i].noDiscount);
1719 strprintf(¶m, " SinglePrice%d=%d,", i,
1720 td.dirPrice[i].singlePrice);
1724 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1727 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1730 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1733 switch (td.tariffConf.traffType)
1736 res += " TraffType='up'";
1739 res += " TraffType='down'";
1742 res += " TraffType='up+down'";
1745 res += " TraffType='max'";
1749 if (schemaVersion > 0)
1750 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1752 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1755 if(MysqlSetQuery(res.c_str()))
1757 errorStr = "Couldn't save tariff:\n";
1758 //errorStr += mysql_error(sock);
1764 //-----------------------------------------------------------------------------
1765 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1767 const std::string & login) const
1769 std::string res, stTime, endTime, tempStr;
1776 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1784 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1786 if (!(sock=MysqlConnect())){
1791 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1793 errorStr = "Couldn't get table " + tempStr + ":\n";
1794 errorStr += mysql_error(sock);
1799 my_ulonglong num_rows = mysql_num_rows(result);
1801 mysql_free_result(result);
1805 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1806 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1807 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1808 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1809 lt->tm_mon+1, lt->tm_year+1900);
1811 if(MysqlQuery(qbuf,sock))
1813 errorStr = "Couldn't create WriteDetailedStat table:\n";
1814 errorStr += mysql_error(sock);
1823 lt1 = localtime(&lastStat);
1832 lt2 = localtime(&t);
1838 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1839 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1841 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1842 "day=%d,startTime='%s',endTime='%s',",
1843 lt->tm_mon+1, lt->tm_year+1900,
1850 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1851 stIter = statTree.begin();
1853 while (stIter != statTree.end())
1855 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1856 inet_ntostring(stIter->first.ip).c_str(),
1858 stIter->second.down,
1863 if( MysqlQuery((res+tempStr).c_str(),sock) )
1865 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1866 errorStr += mysql_error(sock);
1871 result=mysql_store_result(sock);
1873 mysql_free_result(result);
1880 //-----------------------------------------------------------------------------
1881 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1885 gettimeofday(&tv, NULL);
1887 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
1889 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1891 static_cast<long long>(msg->header.id)
1894 if(MysqlSetQuery(qbuf))
1896 errorStr = "Couldn't add message:\n";
1897 //errorStr += mysql_error(sock);
1901 return EditMessage(*msg, login);
1903 //-----------------------------------------------------------------------------
1904 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1908 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1909 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1910 "WHERE login='%s' AND id=%lld LIMIT 1",
1912 msg.header.lastSendTime,
1913 msg.header.creationTime,
1914 msg.header.showTime,
1916 msg.header.repeatPeriod,
1917 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1922 if(MysqlSetQuery(res.c_str()))
1924 errorStr = "Couldn't edit message:\n";
1925 //errorStr += mysql_error(sock);
1931 //-----------------------------------------------------------------------------
1932 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1938 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1939 login.c_str(), static_cast<unsigned long long>(id));
1941 if(MysqlGetQuery(qbuf,sock))
1943 errorStr = "Couldn't GetMessage:\n";
1944 errorStr += mysql_error(sock);
1949 if (!(res=mysql_store_result(sock)))
1951 errorStr = "Couldn't GetMessage:\n";
1952 errorStr += mysql_error(sock);
1957 row = mysql_fetch_row(res);
1959 if(row[2]&&str2x(row[2], msg->header.type))
1961 mysql_free_result(res);
1962 errorStr = "Invalid value in message header for user: " + login;
1967 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1969 mysql_free_result(res);
1970 errorStr = "Invalid value in message header for user: " + login;
1975 if(row[4] && str2x(row[4], msg->header.creationTime))
1977 mysql_free_result(res);
1978 errorStr = "Invalid value in message header for user: " + login;
1983 if(row[5] && str2x(row[5], msg->header.showTime))
1985 mysql_free_result(res);
1986 errorStr = "Invalid value in message header for user: " + login;
1991 if(row[6] && str2x(row[6], msg->header.repeat))
1993 mysql_free_result(res);
1994 errorStr = "Invalid value in message header for user: " + login;
1999 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
2001 mysql_free_result(res);
2002 errorStr = "Invalid value in message header for user: " + login;
2007 msg->header.id = id;
2010 mysql_free_result(res);
2014 //-----------------------------------------------------------------------------
2015 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
2017 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
2018 login.c_str(), static_cast<long long>(id));
2020 if(MysqlSetQuery(qbuf))
2022 errorStr = "Couldn't delete Message:\n";
2023 //errorStr += mysql_error(sock);
2029 //-----------------------------------------------------------------------------
2030 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
2035 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
2037 if(MysqlGetQuery(qbuf,sock))
2039 errorStr = "Couldn't GetMessageHdrs:\n";
2040 errorStr += mysql_error(sock);
2045 if (!(res=mysql_store_result(sock)))
2047 errorStr = "Couldn't GetMessageHdrs:\n";
2048 errorStr += mysql_error(sock);
2054 my_ulonglong num_rows = mysql_num_rows(res);
2057 for (i = 0; i < num_rows; i++)
2059 row = mysql_fetch_row(res);
2060 if (str2x(row[1], id))
2065 if(str2x(row[2], hdr.type))
2069 if(str2x(row[3], hdr.lastSendTime))
2073 if(str2x(row[4], hdr.creationTime))
2077 if(str2x(row[5], hdr.showTime))
2081 if(str2x(row[6], hdr.repeat))
2085 if(str2x(row[7], hdr.repeatPeriod))
2089 hdrsList->push_back(hdr);
2092 mysql_free_result(res);
2096 //-----------------------------------------------------------------------------
2098 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2101 int ret=MysqlGetQuery(Query,sock);
2105 //-----------------------------------------------------------------------------
2106 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2107 if (!(sock=MysqlConnect())) {
2110 return MysqlQuery(Query,sock);
2112 //-----------------------------------------------------------------------------
2113 MYSQL * MYSQL_STORE::MysqlConnect() const {
2115 if ( !(sock=mysql_init(NULL)) ){
2116 errorStr= "mysql init susck\n";
2119 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2120 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2123 errorStr = "Couldn't connect to mysql engine! With error:\n";
2124 errorStr += mysql_error(sock);
2128 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2129 errorStr = "Database lost !\n";
2135 //-----------------------------------------------------------------------------