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"
24 const int pt_mega = 1024 * 1024;
25 const string badSyms = "'`";
26 const char repSym = '\"';
27 const int RepitTimes = 3;
29 int GetInt(const string & str, int * val, int defaultVal)
33 *val = strtol(str.c_str(), &res, 10);
37 *val = defaultVal; //Error!
44 int GetDouble(const string & str, double * val, double defaultVal)
48 *val = strtod(str.c_str(), &res);
52 *val = defaultVal; //Error!
59 int GetTime(const string & str, time_t * val, time_t defaultVal)
63 *val = strtol(str.c_str(), &res, 10);
67 *val = defaultVal; //Error!
74 //-----------------------------------------------------------------------------
75 string ReplaceStr(string source, const string symlist, const char chgsym)
77 string::size_type pos=0;
79 while( (pos = source.find_first_of(symlist,pos)) != string::npos)
80 source.replace(pos, 1,1, chgsym);
85 int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
89 *val = strtoull(str.c_str(), &res, 10);
93 *val = defaultVal; //Error!
100 PLUGIN_CREATOR<MYSQL_STORE> msc;
101 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
106 return msc.GetPlugin();
108 //-----------------------------------------------------------------------------
109 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
118 //-----------------------------------------------------------------------------
119 int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams,
120 const string & name, string & result)
124 vector<PARAM_VALUE>::const_iterator pvi;
125 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
126 if (pvi == moduleParams.end())
128 errorStr = "Parameter \'" + name + "\' not found.";
132 result = pvi->value[0];
136 //-----------------------------------------------------------------------------
137 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
139 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
140 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
142 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
143 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
145 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
146 ParseParam(s.moduleParams, "dbname", dbName) < 0)
148 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
149 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
154 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
157 MYSQL_STORE::MYSQL_STORE()
159 version("mysql_store v.0.67"),
163 WriteServLog(GetStgLogger())
166 //-----------------------------------------------------------------------------
167 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
171 if( (ret = mysql_query(sock,sQuery)) )
173 for(int i=0; i<RepitTimes; i++)
175 if( (ret = mysql_query(sock,sQuery)) )
176 ;//need to send error result
184 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
187 int MYSQL_STORE::ParseSettings()
189 int ret = storeSettings.ParseSettings(settings);
194 errorStr = storeSettings.GetStrError();
197 if(storeSettings.GetDBPassword().length() == 0)
199 errorStr = "Database password must be not empty. Please read Manual.";
203 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
204 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
207 errorStr = "Couldn't connect to mysql engine! With error:\n";
208 errorStr += mysql_error(&mysql);
214 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
216 string res = "CREATE DATABASE " + storeSettings.GetDBName();
218 if(MysqlQuery(res.c_str(),sock))
220 errorStr = "Couldn't create database! With error:\n";
221 errorStr += mysql_error(sock);
227 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
229 errorStr = "Couldn't select database! With error:\n";
230 errorStr += mysql_error(sock);
234 ret = CheckAllTables(sock);
239 ret = CheckAllTables(sock);
243 WriteServLog("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
251 //-----------------------------------------------------------------------------
252 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
256 if (!(result=mysql_list_tables(sock,str.c_str() )))
258 errorStr = "Couldn't get tables list With error:\n";
259 errorStr += mysql_error(sock);
264 unsigned int num_rows = mysql_num_rows(result);
267 mysql_free_result(result);
269 return (num_rows == 1);
271 //-----------------------------------------------------------------------------
272 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
274 //info-------------------------------------------------------------------------
275 if(!IsTablePresent("info",sock))
277 sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
279 if(MysqlQuery(qbuf,sock))
281 errorStr = "Couldn't create info table With error:\n";
282 errorStr += mysql_error(sock);
287 sprintf(qbuf,"INSERT INTO info SET version=0");
289 if(MysqlQuery(qbuf,sock))
291 errorStr = "Couldn't write default version. With error:\n";
292 errorStr += mysql_error(sock);
300 std::vector<std::string> info;
301 if (GetAllParams(&info, "info", "version"))
308 GetInt(info.front(), &schemaVersion, 0);
311 //admins-----------------------------------------------------------------------
312 if(!IsTablePresent("admins",sock))
314 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
315 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
316 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
317 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
318 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
320 if(MysqlQuery(qbuf,sock))
322 errorStr = "Couldn't create admin table list With error:\n";
323 errorStr += mysql_error(sock);
328 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
329 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
330 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
332 if(MysqlQuery(qbuf,sock))
334 errorStr = "Couldn't create default admin. With error:\n";
335 errorStr += mysql_error(sock);
341 //tariffs-----------------------------------------------------------------------
343 if(!IsTablePresent("tariffs",sock))
345 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
347 for (int i = 0; i < DIR_NUM; i++)
349 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
352 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
355 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
358 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
361 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
364 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
367 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
370 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
374 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
375 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
376 "period VARCHAR(32) NOT NULL DEFAULT 'month')";
378 if(MysqlQuery(res.c_str(),sock))
380 errorStr = "Couldn't create tariffs table list With error:\n";
381 errorStr += mysql_error(sock);
386 res = "INSERT INTO tariffs SET name='tariff',";
388 for (int i = 0; i < DIR_NUM; i++)
390 strprintf(¶m, " NoDiscount%d=1,", i);
393 strprintf(¶m, " Threshold%d=0,", i);
396 strprintf(¶m, " Time%d='0:0-0:0',", i);
401 strprintf(¶m, " SinglePrice%d=0,", i);
407 strprintf(¶m, " PriceDayA%d=0.0,", i);
412 strprintf(¶m, " PriceDayB%d=0.0,", i);
418 strprintf(¶m, " PriceNightA%d=0.0,", i);
423 strprintf(¶m, " PriceNightB%d=0.0,", i);
428 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
429 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
430 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'";
432 if(MysqlQuery(res.c_str(),sock))
434 errorStr = "Couldn't create default tariff. With error:\n";
435 errorStr += mysql_error(sock);
440 sprintf(qbuf,"UPDATE info SET version=1");
442 if(MysqlQuery(qbuf,sock))
444 errorStr = "Couldn't write default version. With error:\n";
445 errorStr += mysql_error(sock);
452 //users-----------------------------------------------------------------------
453 if(!IsTablePresent("users",sock))
455 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
456 "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 '',"\
457 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
458 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
459 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
461 for (int i = 0; i < USERDATA_NUM; i++)
463 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
467 param = " CreditExpire INT(11) DEFAULT 0,";
470 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
473 for (int i = 0; i < DIR_NUM; i++)
475 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
478 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
482 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
483 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
484 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
485 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
488 if(MysqlQuery(res.c_str(),sock))
490 errorStr = "Couldn't create users table list With error:\n";
491 errorStr += mysql_error(sock);
492 errorStr += "\n\n" + res;
497 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
498 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
499 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
500 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
503 for (int i = 0; i < DIR_NUM; i++)
505 strprintf(¶m, " D%d=0,", i);
508 strprintf(¶m, " U%d=0,", i);
512 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
513 "LastCashAddTime=0, PassiveTime=0";
515 if(MysqlQuery(res.c_str(),sock))
517 errorStr = "Couldn't create default user. With error:\n";
518 errorStr += mysql_error(sock);
524 //logs-----------------------------------------------------------------------
525 if(!IsTablePresent("logs"))
527 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
531 errorStr = "Couldn't create admin table list With error:\n";
532 errorStr += mysql_error(sock);
537 //messages---------------------------------------------------------------------
538 if(!IsTablePresent("messages",sock))
540 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
541 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
542 "stgRepeat INT, repeatPeriod INT, text TEXT)");
544 if(MysqlQuery(qbuf,sock))
546 errorStr = "Couldn't create messages table. With error:\n";
547 errorStr += mysql_error(sock);
553 //month_stat-------------------------------------------------------------------
554 if(!IsTablePresent("stat",sock))
556 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
558 for (int i = 0; i < DIR_NUM; i++)
560 strprintf(¶m, " U%d BIGINT,", i);
563 strprintf(¶m, " D%d BIGINT,", i);
567 res += " cash DOUBLE, INDEX (login))";
569 if(MysqlQuery(res.c_str(),sock))
571 errorStr = "Couldn't create stat table. With error:\n";
572 errorStr += mysql_error(sock);
580 //-----------------------------------------------------------------------------
581 int MYSQL_STORE::MakeUpdates(MYSQL * sock)
583 if (schemaVersion < 1)
585 if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
587 errorStr = "Couldn't update tariffs table to version 1. With error:\n";
588 errorStr += mysql_error(sock);
592 if (MysqlQuery("UPDATE info SET version = 1", sock))
594 errorStr = "Couldn't update DB schema version to 1. With error:\n";
595 errorStr += mysql_error(sock);
600 WriteServLog("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
604 //-----------------------------------------------------------------------------
606 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
607 const string & table, const string & name) const
616 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
618 if(MysqlGetQuery(qbuf,sock))
620 errorStr = "Couldn't GetAllParams Query for: ";
621 errorStr += name + " - " + table + "\n";
622 errorStr += mysql_error(sock);
627 if (!(res=mysql_store_result(sock)))
629 errorStr = "Couldn't GetAllParams Results for: ";
630 errorStr += name + " - " + table + "\n";
631 errorStr += mysql_error(sock);
635 num = mysql_num_rows(res);
639 row = mysql_fetch_row(res);
640 ParamList->push_back(row[0]);
643 mysql_free_result(res);
649 //-----------------------------------------------------------------------------
650 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
652 if(GetAllParams(usersList, "users", "login"))
657 //-----------------------------------------------------------------------------
658 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
660 if(GetAllParams(adminsList, "admins", "login"))
665 //-----------------------------------------------------------------------------
666 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
668 if(GetAllParams(tariffsList, "tariffs", "name"))
673 //-----------------------------------------------------------------------------
674 int MYSQL_STORE::AddUser(const string & login) const
676 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
678 if(MysqlSetQuery(qbuf))
680 errorStr = "Couldn't add user:\n";
681 //errorStr += mysql_error(sock);
687 //-----------------------------------------------------------------------------
688 int MYSQL_STORE::DelUser(const string & login) const
690 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
692 if(MysqlSetQuery(qbuf))
694 errorStr = "Couldn't delete user:\n";
695 //errorStr += mysql_error(sock);
701 //-----------------------------------------------------------------------------
702 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
709 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
710 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
711 RealName, StgGroup, Credit, TariffChange, ";
713 for (int i = 0; i < USERDATA_NUM; i++)
715 sprintf(qbuf, "Userdata%d, ", i);
719 query += "CreditExpire, IP FROM users WHERE login='";
720 query += login + "' LIMIT 1";
722 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
724 if(MysqlGetQuery(query.c_str(),sock))
726 errorStr = "Couldn't restore Tariff(on query):\n";
727 errorStr += mysql_error(sock);
732 if (!(res=mysql_store_result(sock)))
734 errorStr = "Couldn't restore Tariff(on getting result):\n";
735 errorStr += mysql_error(sock);
740 if (mysql_num_rows(res) != 1)
742 errorStr = "User not found";
747 row = mysql_fetch_row(res);
751 conf->password = row[1];
753 if (conf->password.empty())
755 mysql_free_result(res);
756 errorStr = "User \'" + login + "\' password is blank.";
761 if (GetInt(row[2],&conf->passive, 0) != 0)
763 mysql_free_result(res);
764 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
769 if (GetInt(row[3], &conf->disabled, 0) != 0)
771 mysql_free_result(res);
772 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
777 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
779 mysql_free_result(res);
780 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
785 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
787 mysql_free_result(res);
788 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
793 conf->tariffName = row[6];
795 if (conf->tariffName.empty())
797 mysql_free_result(res);
798 errorStr = "User \'" + login + "\' tariff is blank.";
803 conf->address = row[7];
804 conf->phone = row[8];
805 conf->email = row[9];
806 conf->note = row[10];
807 conf->realName = row[11];
808 conf->group = row[12];
810 if (GetDouble(row[13], &conf->credit, 0) != 0)
812 mysql_free_result(res);
813 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
818 conf->nextTariff = row[14];
820 for (int i = 0; i < USERDATA_NUM; i++)
822 conf->userdata[i] = row[15+i];
825 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
827 string ipStr = row[16+USERDATA_NUM];
833 catch (const string & s)
835 mysql_free_result(res);
836 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
842 mysql_free_result(res);
847 //-----------------------------------------------------------------------------
848 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
858 for (int i = 0; i < DIR_NUM; i++)
860 sprintf(qbuf, "D%d, U%d, ", i, i);
864 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
865 FROM users WHERE login = '";
866 query += login + "'";
868 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
870 if(MysqlGetQuery(query.c_str() ,sock))
872 errorStr = "Couldn't restore UserStat(on query):\n";
873 errorStr += mysql_error(sock);
878 if (!(res=mysql_store_result(sock)))
880 errorStr = "Couldn't restore UserStat(on getting result):\n";
881 errorStr += mysql_error(sock);
886 row = mysql_fetch_row(res);
888 unsigned int startPos=0;
892 for (int i = 0; i < DIR_NUM; i++)
895 sprintf(s, "D%d", i);
896 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
898 mysql_free_result(res);
899 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
903 stat->down[i] = traff;
905 sprintf(s, "U%d", i);
906 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
908 mysql_free_result(res);
909 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
916 startPos += (2*DIR_NUM);
918 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
920 mysql_free_result(res);
921 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
926 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
928 mysql_free_result(res);
929 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
934 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
936 mysql_free_result(res);
937 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
942 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
944 mysql_free_result(res);
945 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
950 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
952 mysql_free_result(res);
953 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
958 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
960 mysql_free_result(res);
961 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
966 mysql_free_result(res);
970 //-----------------------------------------------------------------------------
971 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
976 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
977 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
978 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
979 conf.password.c_str(),
982 conf.disabledDetailStat,
984 conf.tariffName.c_str(),
985 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
986 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
987 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
988 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
989 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
990 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
992 conf.nextTariff.c_str()
995 for (int i = 0; i < USERDATA_NUM; i++)
997 strprintf(¶m, " Userdata%d='%s',", i,
998 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
1002 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
1008 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
1011 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1014 if(MysqlSetQuery(res.c_str()))
1016 errorStr = "Couldn't save user conf:\n";
1017 //errorStr += mysql_error(sock);
1023 //-----------------------------------------------------------------------------
1024 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1029 res = "UPDATE users SET";
1031 for (int i = 0; i < DIR_NUM; i++)
1033 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1036 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1040 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1041 " PassiveTime=%d, LastActivityTime=%d",
1045 stat.lastCashAddTime,
1047 stat.lastActivityTime
1051 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1054 if(MysqlSetQuery(res.c_str()))
1056 errorStr = "Couldn't save user stat:\n";
1057 // errorStr += mysql_error(sock);
1063 //-----------------------------------------------------------------------------
1064 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
1066 string res, tempStr;
1075 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1076 if (!(sock=MysqlConnect())){
1077 errorStr = "Couldn't connect to Server";
1080 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1082 errorStr = "Couldn't get table " + tempStr + ":\n";
1083 errorStr += mysql_error(sock);
1088 unsigned int num_rows = mysql_num_rows(result);
1090 mysql_free_result(result);
1094 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1095 lt->tm_mon+1, lt->tm_year+1900);
1097 if(MysqlQuery(qbuf,sock))
1099 errorStr = "Couldn't create WriteDetailedStat table:\n";
1100 errorStr += mysql_error(sock);
1106 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1110 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1111 lt->tm_mon+1, lt->tm_year+1900,
1112 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1114 if(MysqlQuery(send.c_str(),sock))
1116 errorStr = "Couldn't write log string:\n";
1117 errorStr += mysql_error(sock);
1125 //-----------------------------------------------------------------------------
1126 int MYSQL_STORE::WriteUserChgLog(const string & login,
1127 const string & admLogin,
1129 const string & paramName,
1130 const string & oldValue,
1131 const string & newValue,
1132 const string & message) const
1134 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1135 + paramName + "\' parameter changed from \'" + oldValue +
1136 "\' to \'" + newValue + "\'. " + message;
1138 return WriteLogString(userLogMsg, login);
1140 //-----------------------------------------------------------------------------
1141 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1143 string logStr = "Connect, " + inet_ntostring(ip);
1144 return WriteLogString(logStr, login);
1146 //-----------------------------------------------------------------------------
1147 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1148 const DIR_TRAFF & up,
1149 const DIR_TRAFF & down,
1150 const DIR_TRAFF & sessionUp,
1151 const DIR_TRAFF & sessionDown,
1154 const std::string & reason) const
1156 string logStr = "Disconnect, ";
1161 stringstream sscash;
1167 sssd << sessionDown;
1171 logStr += " session upload: \'";
1172 logStr += sssu.str();
1173 logStr += "\' session download: \'";
1174 logStr += sssd.str();
1175 logStr += "\' month upload: \'";
1176 logStr += ssmu.str();
1177 logStr += "\' month download: \'";
1178 logStr += ssmd.str();
1179 logStr += "\' cash: \'";
1180 logStr += sscash.str();
1183 return WriteLogString(logStr, login);
1185 //-----------------------------------------------------------------------------
1186 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1187 const string & login) const
1191 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1192 login.c_str(), month+1, year+1900);
1194 for (int i = 0; i < DIR_NUM; i++)
1196 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1199 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1203 strprintf(¶m, " cash=%f", stat.cash);
1206 if(MysqlSetQuery(res.c_str()))
1208 errorStr = "Couldn't SaveMonthStat:\n";
1209 //errorStr += mysql_error(sock);
1215 //-----------------------------------------------------------------------------*/
1216 int MYSQL_STORE::AddAdmin(const string & login) const
1218 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1220 if(MysqlSetQuery(qbuf))
1222 errorStr = "Couldn't add admin:\n";
1223 //errorStr += mysql_error(sock);
1229 //-----------------------------------------------------------------------------*/
1230 int MYSQL_STORE::DelAdmin(const string & login) const
1232 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1234 if(MysqlSetQuery(qbuf))
1236 errorStr = "Couldn't delete admin:\n";
1237 //errorStr += mysql_error(sock);
1243 //-----------------------------------------------------------------------------*/
1244 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1246 char passwordE[2 * ADM_PASSWD_LEN + 2];
1247 char pass[ADM_PASSWD_LEN + 1];
1248 char adminPass[ADM_PASSWD_LEN + 1];
1250 memset(pass, 0, sizeof(pass));
1251 memset(adminPass, 0, sizeof(adminPass));
1254 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1256 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1257 adminPass[ADM_PASSWD_LEN - 1] = 0;
1259 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1261 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1264 pass[ADM_PASSWD_LEN - 1] = 0;
1265 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1267 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1268 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1269 "WHERE login='%s' LIMIT 1",
1281 if(MysqlSetQuery(qbuf))
1283 errorStr = "Couldn't save admin:\n";
1284 //errorStr += mysql_error(sock);
1290 //-----------------------------------------------------------------------------
1291 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1293 char pass[ADM_PASSWD_LEN + 1];
1294 char password[ADM_PASSWD_LEN + 1];
1295 char passwordE[2*ADM_PASSWD_LEN + 2];
1298 memset(pass, 0, sizeof(pass));
1299 memset(password, 0, sizeof(password));
1300 memset(passwordE, 0, sizeof(passwordE));
1306 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1308 if(MysqlGetQuery(qbuf,sock))
1310 errorStr = "Couldn't restore admin:\n";
1311 errorStr += mysql_error(sock);
1316 if (!(res=mysql_store_result(sock)))
1318 errorStr = "Couldn't restore admin:\n";
1319 errorStr += mysql_error(sock);
1324 if ( mysql_num_rows(res) == 0)
1326 mysql_free_result(res);
1327 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1332 row = mysql_fetch_row(res);
1339 mysql_free_result(res);
1340 errorStr = "Error in parameter password";
1345 memset(passwordE, 0, sizeof(passwordE));
1346 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1348 memset(pass, 0, sizeof(pass));
1350 if (passwordE[0] != 0)
1352 Decode21(pass, passwordE);
1353 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1355 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1357 DecodeString(password + 8*i, pass + 8*i, &ctx);
1365 ac->password = password;
1367 if (GetInt(row[2], &a, 0) == 0)
1368 ac->priv.userConf = a;
1371 mysql_free_result(res);
1372 errorStr = "Error in parameter ChgConf";
1377 if (GetInt(row[3], &a, 0) == 0)
1378 ac->priv.userPasswd = a;
1381 mysql_free_result(res);
1382 errorStr = "Error in parameter ChgPassword";
1387 if (GetInt(row[4], &a, 0) == 0)
1388 ac->priv.userStat = a;
1391 mysql_free_result(res);
1392 errorStr = "Error in parameter ChgStat";
1397 if (GetInt(row[5], &a, 0) == 0)
1398 ac->priv.userCash = a;
1401 mysql_free_result(res);
1402 errorStr = "Error in parameter ChgCash";
1407 if (GetInt(row[6], &a, 0) == 0)
1408 ac->priv.userAddDel = a;
1411 mysql_free_result(res);
1412 errorStr = "Error in parameter UsrAddDel";
1417 if (GetInt(row[7], &a, 0) == 0)
1418 ac->priv.tariffChg = a;
1421 mysql_free_result(res);
1422 errorStr = "Error in parameter ChgTariff";
1427 if (GetInt(row[8], &a, 0) == 0)
1428 ac->priv.adminChg = a;
1431 mysql_free_result(res);
1432 errorStr = "Error in parameter ChgAdmin";
1437 mysql_free_result(res);
1441 //-----------------------------------------------------------------------------
1442 int MYSQL_STORE::AddTariff(const string & name) const
1444 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1446 if(MysqlSetQuery(qbuf))
1448 errorStr = "Couldn't add tariff:\n";
1449 // errorStr += mysql_error(sock);
1455 //-----------------------------------------------------------------------------
1456 int MYSQL_STORE::DelTariff(const string & name) const
1458 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1460 if(MysqlSetQuery(qbuf))
1462 errorStr = "Couldn't delete tariff: ";
1463 // errorStr += mysql_error(sock);
1469 //-----------------------------------------------------------------------------
1470 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1475 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1477 if(MysqlGetQuery(qbuf,sock))
1479 errorStr = "Couldn't restore Tariff:\n";
1480 errorStr += mysql_error(sock);
1485 if (!(res=mysql_store_result(sock)))
1487 errorStr = "Couldn't restore Tariff:\n";
1488 errorStr += mysql_error(sock);
1494 td->tariffConf.name = tariffName;
1496 row = mysql_fetch_row(res);
1499 for (int i = 0; i<DIR_NUM; i++)
1501 strprintf(¶m, "Time%d", i);
1503 if (str.length() == 0)
1505 mysql_free_result(res);
1506 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1511 ParseTariffTimeStr(str.c_str(),
1512 td->dirPrice[i].hDay,
1513 td->dirPrice[i].mDay,
1514 td->dirPrice[i].hNight,
1515 td->dirPrice[i].mNight);
1517 strprintf(¶m, "PriceDayA%d", i);
1518 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1520 mysql_free_result(res);
1521 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1525 td->dirPrice[i].priceDayA /= (1024*1024);
1527 strprintf(¶m, "PriceDayB%d", i);
1528 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1530 mysql_free_result(res);
1531 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1535 td->dirPrice[i].priceDayB /= (1024*1024);
1537 strprintf(¶m, "PriceNightA%d", i);
1538 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1540 mysql_free_result(res);
1541 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1545 td->dirPrice[i].priceNightA /= (1024*1024);
1547 strprintf(¶m, "PriceNightB%d", i);
1548 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1550 mysql_free_result(res);
1551 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1555 td->dirPrice[i].priceNightB /= (1024*1024);
1557 strprintf(¶m, "Threshold%d", i);
1558 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1560 mysql_free_result(res);
1561 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1566 strprintf(¶m, "SinglePrice%d", i);
1567 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1569 mysql_free_result(res);
1570 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1575 strprintf(¶m, "NoDiscount%d", i);
1576 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1578 mysql_free_result(res);
1579 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1585 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1587 mysql_free_result(res);
1588 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1593 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1595 mysql_free_result(res);
1596 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1601 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1603 mysql_free_result(res);
1604 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1609 str = row[4+8*DIR_NUM];
1610 param = "TraffType";
1612 if (str.length() == 0)
1614 mysql_free_result(res);
1615 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1620 if (!strcasecmp(str.c_str(), "up"))
1621 td->tariffConf.traffType = TRAFF_UP;
1623 if (!strcasecmp(str.c_str(), "down"))
1624 td->tariffConf.traffType = TRAFF_DOWN;
1626 if (!strcasecmp(str.c_str(), "up+down"))
1627 td->tariffConf.traffType = TRAFF_UP_DOWN;
1629 if (!strcasecmp(str.c_str(), "max"))
1630 td->tariffConf.traffType = TRAFF_MAX;
1633 mysql_free_result(res);
1634 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1639 if (schemaVersion > 0)
1641 str = row[5+8*DIR_NUM];
1644 if (str.length() == 0)
1646 mysql_free_result(res);
1647 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1652 td->tariffConf.period = TARIFF::StringToPeriod(str);
1656 td->tariffConf.period = TARIFF::MONTH;
1659 mysql_free_result(res);
1663 //-----------------------------------------------------------------------------
1664 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1668 string res="UPDATE tariffs SET";
1670 for (int i = 0; i < DIR_NUM; i++)
1672 strprintf(¶m, " PriceDayA%d=%f,", i,
1673 td.dirPrice[i].priceDayA * pt_mega);
1676 strprintf(¶m, " PriceDayB%d=%f,", i,
1677 td.dirPrice[i].priceDayB * pt_mega);
1680 strprintf(¶m, " PriceNightA%d=%f,", i,
1681 td.dirPrice[i].priceNightA * pt_mega);
1684 strprintf(¶m, " PriceNightB%d=%f,", i,
1685 td.dirPrice[i].priceNightB * pt_mega);
1688 strprintf(¶m, " Threshold%d=%d,", i,
1689 td.dirPrice[i].threshold);
1693 strprintf(¶m, " Time%d", i);
1695 strprintf(&s, "%0d:%0d-%0d:%0d",
1696 td.dirPrice[i].hDay,
1697 td.dirPrice[i].mDay,
1698 td.dirPrice[i].hNight,
1699 td.dirPrice[i].mNight);
1701 res += (param + "='" + s + "',");
1703 strprintf(¶m, " NoDiscount%d=%d,", i,
1704 td.dirPrice[i].noDiscount);
1707 strprintf(¶m, " SinglePrice%d=%d,", i,
1708 td.dirPrice[i].singlePrice);
1712 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1715 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1718 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1721 switch (td.tariffConf.traffType)
1724 res += " TraffType='up'";
1727 res += " TraffType='down'";
1730 res += " TraffType='up+down'";
1733 res += " TraffType='max'";
1737 if (schemaVersion > 0)
1738 res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
1740 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1743 if(MysqlSetQuery(res.c_str()))
1745 errorStr = "Couldn't save tariff:\n";
1746 //errorStr += mysql_error(sock);
1752 //-----------------------------------------------------------------------------
1753 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1755 const string & login) const
1757 string res, stTime, endTime, tempStr;
1764 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1772 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1774 if (!(sock=MysqlConnect())){
1779 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1781 errorStr = "Couldn't get table " + tempStr + ":\n";
1782 errorStr += mysql_error(sock);
1787 unsigned int num_rows = mysql_num_rows(result);
1789 mysql_free_result(result);
1793 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1794 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1795 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1796 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1797 lt->tm_mon+1, lt->tm_year+1900);
1799 if(MysqlQuery(qbuf,sock))
1801 errorStr = "Couldn't create WriteDetailedStat table:\n";
1802 errorStr += mysql_error(sock);
1811 lt1 = localtime(&lastStat);
1820 lt2 = localtime(&t);
1826 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1827 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1829 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1830 "day=%d,startTime='%s',endTime='%s',",
1831 lt->tm_mon+1, lt->tm_year+1900,
1838 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1839 stIter = statTree.begin();
1841 while (stIter != statTree.end())
1843 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1844 inet_ntostring(stIter->first.ip).c_str(),
1846 stIter->second.down,
1851 if( MysqlQuery((res+tempStr).c_str(),sock) )
1853 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1854 errorStr += mysql_error(sock);
1859 result=mysql_store_result(sock);
1861 mysql_free_result(result);
1868 //-----------------------------------------------------------------------------
1869 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1873 gettimeofday(&tv, NULL);
1875 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1877 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1879 (long long)msg->header.id
1882 if(MysqlSetQuery(qbuf))
1884 errorStr = "Couldn't add message:\n";
1885 //errorStr += mysql_error(sock);
1889 return EditMessage(*msg, login);
1891 //-----------------------------------------------------------------------------
1892 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1896 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1897 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1898 "WHERE login='%s' AND id=%lld LIMIT 1",
1900 msg.header.lastSendTime,
1901 msg.header.creationTime,
1902 msg.header.showTime,
1904 msg.header.repeatPeriod,
1905 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1907 (long long)msg.header.id
1910 if(MysqlSetQuery(res.c_str()))
1912 errorStr = "Couldn't edit message:\n";
1913 //errorStr += mysql_error(sock);
1919 //-----------------------------------------------------------------------------
1920 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1926 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1927 login.c_str(), static_cast<unsigned long long>(id));
1929 if(MysqlGetQuery(qbuf,sock))
1931 errorStr = "Couldn't GetMessage:\n";
1932 errorStr += mysql_error(sock);
1937 if (!(res=mysql_store_result(sock)))
1939 errorStr = "Couldn't GetMessage:\n";
1940 errorStr += mysql_error(sock);
1945 row = mysql_fetch_row(res);
1947 if(row[2]&&str2x(row[2], msg->header.type))
1949 mysql_free_result(res);
1950 errorStr = "Invalid value in message header for user: " + login;
1955 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1957 mysql_free_result(res);
1958 errorStr = "Invalid value in message header for user: " + login;
1963 if(row[4] && str2x(row[4], msg->header.creationTime))
1965 mysql_free_result(res);
1966 errorStr = "Invalid value in message header for user: " + login;
1971 if(row[5] && str2x(row[5], msg->header.showTime))
1973 mysql_free_result(res);
1974 errorStr = "Invalid value in message header for user: " + login;
1979 if(row[6] && str2x(row[6], msg->header.repeat))
1981 mysql_free_result(res);
1982 errorStr = "Invalid value in message header for user: " + login;
1987 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1989 mysql_free_result(res);
1990 errorStr = "Invalid value in message header for user: " + login;
1995 msg->header.id = id;
1998 mysql_free_result(res);
2002 //-----------------------------------------------------------------------------
2003 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
2005 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
2006 login.c_str(),(long long)id);
2008 if(MysqlSetQuery(qbuf))
2010 errorStr = "Couldn't delete Message:\n";
2011 //errorStr += mysql_error(sock);
2017 //-----------------------------------------------------------------------------
2018 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2023 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
2025 if(MysqlGetQuery(qbuf,sock))
2027 errorStr = "Couldn't GetMessageHdrs:\n";
2028 errorStr += mysql_error(sock);
2033 if (!(res=mysql_store_result(sock)))
2035 errorStr = "Couldn't GetMessageHdrs:\n";
2036 errorStr += mysql_error(sock);
2041 unsigned int i, num_rows = mysql_num_rows(res);
2042 long long int unsigned id = 0;
2044 for (i=0; i<num_rows; i++)
2046 row = mysql_fetch_row(res);
2047 if (str2x(row[1], id))
2052 if(str2x(row[2], hdr.type))
2056 if(str2x(row[3], hdr.lastSendTime))
2060 if(str2x(row[4], hdr.creationTime))
2064 if(str2x(row[5], hdr.showTime))
2068 if(str2x(row[6], hdr.repeat))
2072 if(str2x(row[7], hdr.repeatPeriod))
2076 hdrsList->push_back(hdr);
2079 mysql_free_result(res);
2083 //-----------------------------------------------------------------------------
2085 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2088 int ret=MysqlGetQuery(Query,sock);
2092 //-----------------------------------------------------------------------------
2093 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2094 if (!(sock=MysqlConnect())) {
2097 return MysqlQuery(Query,sock);
2099 //-----------------------------------------------------------------------------
2100 MYSQL * MYSQL_STORE::MysqlConnect() const {
2102 if ( !(sock=mysql_init(NULL)) ){
2103 errorStr= "mysql init susck\n";
2106 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2107 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2110 errorStr = "Couldn't connect to mysql engine! With error:\n";
2111 errorStr += mysql_error(sock);
2115 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2116 errorStr = "Database lost !\n";
2122 //-----------------------------------------------------------------------------