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 "mysql_store.h"
 
  17 #define adm_enc_passwd "cjeifY8m3"
 
  23 const int pt_mega = 1024 * 1024;
 
  24 const std::string badSyms = "'`";
 
  25 const char repSym = '\"';
 
  26 const int RepitTimes = 3;
 
  29 int GetInt(const std::string & str, T * val, T defaultVal = T())
 
  33     *val = static_cast<T>(strtoll(str.c_str(), &res, 10));
 
  37         *val = defaultVal; //Error!
 
  44 int GetDouble(const std::string & str, double * val, double defaultVal)
 
  48     *val = strtod(str.c_str(), &res);
 
  52         *val = defaultVal; //Error!
 
  59 int GetTime(const std::string & str, time_t * val, time_t defaultVal)
 
  63     *val = strtol(str.c_str(), &res, 10);
 
  67         *val = defaultVal; //Error!
 
  74 //-----------------------------------------------------------------------------
 
  75 std::string ReplaceStr(std::string source, const std::string symlist, const char chgsym)
 
  77     std::string::size_type pos=0;
 
  79     while( (pos = source.find_first_of(symlist,pos)) != std::string::npos)
 
  80         source.replace(pos, 1,1, chgsym);
 
  85 int GetULongLongInt(const std::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;
 
 103 extern "C" STORE * GetStore();
 
 104 //-----------------------------------------------------------------------------
 
 105 //-----------------------------------------------------------------------------
 
 106 //-----------------------------------------------------------------------------
 
 109 return msc.GetPlugin();
 
 111 //-----------------------------------------------------------------------------
 
 112 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
 
 121 //-----------------------------------------------------------------------------
 
 122 int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<PARAM_VALUE> & moduleParams, 
 
 123                         const std::string & name, std::string & result)
 
 127 std::vector<PARAM_VALUE>::const_iterator pvi;
 
 128 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
 
 129 if (pvi == moduleParams.end())
 
 131     errorStr = "Parameter \'" + name + "\' not found.";
 
 135 result = pvi->value[0];
 
 139 //-----------------------------------------------------------------------------
 
 140 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
 
 142 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
 
 143     ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
 
 145 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
 
 146     ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
 
 148 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
 
 149     ParseParam(s.moduleParams, "dbname", dbName) < 0)
 
 151 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
 
 152     ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
 
 157 //-----------------------------------------------------------------------------
 
 158 //-----------------------------------------------------------------------------
 
 159 //-----------------------------------------------------------------------------
 
 160 MYSQL_STORE::MYSQL_STORE()
 
 162       version("mysql_store v.0.67"),
 
 165       logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
 
 168 //-----------------------------------------------------------------------------
 
 169 int    MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
 
 173     if( (ret = mysql_query(sock,sQuery)) )
 
 175         for(int i=0; i<RepitTimes; i++)
 
 177             if( (ret = mysql_query(sock,sQuery)) )
 
 178                 ;//need to send error result
 
 186 //-----------------------------------------------------------------------------
 
 188 //-----------------------------------------------------------------------------
 
 189 int MYSQL_STORE::ParseSettings()
 
 191 int ret = storeSettings.ParseSettings(settings);
 
 196     errorStr = storeSettings.GetStrError();
 
 199     if(storeSettings.GetDBPassword().length() == 0)
 
 201         errorStr = "Database password must be not empty. Please read Manual.";
 
 205     if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
 
 206             storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
 
 209             errorStr = "Couldn't connect to mysql engine! With error:\n";
 
 210             errorStr += mysql_error(&mysql);
 
 216          if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
 
 218              std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
 
 220             if(MysqlQuery(res.c_str(),sock))
 
 222                 errorStr = "Couldn't create database! With error:\n";
 
 223                 errorStr += mysql_error(sock);
 
 229                  if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
 
 231                     errorStr = "Couldn't select database! With error:\n";
 
 232                     errorStr += mysql_error(sock);
 
 236                  ret = CheckAllTables(sock);
 
 240              ret = CheckAllTables(sock);
 
 247 //-----------------------------------------------------------------------------
 
 248 bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
 
 252 if (!(result=mysql_list_tables(sock,str.c_str() )))
 
 254     errorStr = "Couldn't get tables list With error:\n";
 
 255     errorStr += mysql_error(sock);
 
 260 my_ulonglong num_rows =  mysql_num_rows(result);
 
 263     mysql_free_result(result);
 
 265 return num_rows == 1;
 
 267 //-----------------------------------------------------------------------------
 
 268 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
 
 270 //admins-----------------------------------------------------------------------
 
 271 if(!IsTablePresent("admins",sock))
 
 273     sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
 
 274         "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
 
 275         "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
 
 276         "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
 
 277         "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
 
 279     if(MysqlQuery(qbuf,sock))
 
 281         errorStr = "Couldn't create admin table list With error:\n";
 
 282         errorStr += mysql_error(sock);
 
 287     sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
 
 288         "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
 
 289         "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
 
 291     if(MysqlQuery(qbuf,sock))
 
 293         errorStr = "Couldn't create default admin. With error:\n";
 
 294         errorStr += mysql_error(sock);
 
 300 //tariffs-----------------------------------------------------------------------
 
 301 std::string param, res;
 
 302 if(!IsTablePresent("tariffs",sock))
 
 304     res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
 
 306     for (int i = 0; i < DIR_NUM; i++)
 
 308         strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i); 
 
 311         strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
 
 314         strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
 
 317         strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
 
 320         strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
 
 323         strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
 
 326         strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
 
 329         strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
 
 333     res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
 
 334         "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
 
 336     if(MysqlQuery(res.c_str(),sock))
 
 338         errorStr = "Couldn't create tariffs table list With error:\n";
 
 339         errorStr += mysql_error(sock);
 
 344     res = "INSERT INTO tariffs SET name='tariff',";
 
 346     for (int i = 0; i < DIR_NUM; i++)
 
 348         strprintf(¶m, " NoDiscount%d=1,", i);
 
 351         strprintf(¶m, " Threshold%d=0,", i);
 
 354         strprintf(¶m, " Time%d='0:0-0:0',", i);
 
 359             strprintf(¶m, " SinglePrice%d=0,", i);
 
 365             strprintf(¶m, " PriceDayA%d=0.0,", i); 
 
 370             strprintf(¶m, " PriceDayB%d=0.0,", i);        
 
 376             strprintf(¶m, " PriceNightA%d=0.0,", i); 
 
 381             strprintf(¶m, " PriceNightB%d=0.0,", i);        
 
 386     res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
 
 387         "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
 
 388         "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
 
 390     if(MysqlQuery(res.c_str(),sock))
 
 392         errorStr = "Couldn't create default tariff. With error:\n";
 
 393         errorStr += mysql_error(sock);
 
 399 //users-----------------------------------------------------------------------
 
 400 if(!IsTablePresent("users",sock))
 
 402     res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
 
 403         "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 '',"\
 
 404         "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
 
 405         "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
 
 406         "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
 
 408     for (int i = 0; i < USERDATA_NUM; i++)
 
 410         strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
 
 414     param = " CreditExpire INT(11) DEFAULT 0,";
 
 417     strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
 
 420     for (int i = 0; i < DIR_NUM; i++)
 
 422         strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
 
 425         strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
 
 429     strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
 
 430         "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
 
 431         "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
 
 432         " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
 
 435     if(MysqlQuery(res.c_str(),sock))
 
 437         errorStr = "Couldn't create users table list With error:\n";
 
 438         errorStr += mysql_error(sock);
 
 439         errorStr += "\n\n" + res;
 
 444     res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
 
 445         "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
 
 446         "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
 
 447         "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
 
 450     for (int i = 0; i < DIR_NUM; i++)
 
 452         strprintf(¶m, " D%d=0,", i);
 
 455         strprintf(¶m, " U%d=0,", i);
 
 459     res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
 
 460         "LastCashAddTime=0, PassiveTime=0";
 
 462     if(MysqlQuery(res.c_str(),sock))
 
 464         errorStr = "Couldn't create default user. With error:\n";
 
 465         errorStr += mysql_error(sock);
 
 471 //logs-----------------------------------------------------------------------
 
 472 if(!IsTablePresent("logs"))
 
 474     sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
 
 478         errorStr = "Couldn't create admin table list With error:\n";
 
 479         errorStr += mysql_error(sock);
 
 484 //messages---------------------------------------------------------------------
 
 485 if(!IsTablePresent("messages",sock))
 
 487     sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
 
 488             "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
 
 489             "stgRepeat INT, repeatPeriod INT, text TEXT)");
 
 491     if(MysqlQuery(qbuf,sock))
 
 493         errorStr = "Couldn't create messages table. With error:\n";
 
 494         errorStr += mysql_error(sock);
 
 500 //month_stat-------------------------------------------------------------------
 
 501 if(!IsTablePresent("stat",sock))
 
 503     res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
 
 505     for (int i = 0; i < DIR_NUM; i++)
 
 507         strprintf(¶m, " U%d BIGINT,", i); 
 
 510         strprintf(¶m, " D%d BIGINT,", i); 
 
 514     res += " cash DOUBLE, INDEX (login))";
 
 516     if(MysqlQuery(res.c_str(),sock))
 
 518         errorStr = "Couldn't create stat table. With error:\n";
 
 519         errorStr += mysql_error(sock);
 
 527 //-----------------------------------------------------------------------------
 
 528 //-----------------------------------------------------------------------------
 
 530 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList, 
 
 531                             const std::string & table, const std::string & name) const
 
 540 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
 
 542 if(MysqlGetQuery(qbuf,sock))
 
 544     errorStr = "Couldn't GetAllParams Query for: ";
 
 545     errorStr += name + " - " + table + "\n";
 
 546     errorStr += mysql_error(sock);
 
 551 if (!(res=mysql_store_result(sock)))
 
 553     errorStr = "Couldn't GetAllParams Results for: ";
 
 554     errorStr += name + " - " + table + "\n";
 
 555     errorStr += mysql_error(sock);
 
 559 num = mysql_num_rows(res);
 
 561 for(i = 0; i < num; i++)
 
 563     row = mysql_fetch_row(res);    
 
 564     ParamList->push_back(row[0]);
 
 567 mysql_free_result(res);
 
 573 //-----------------------------------------------------------------------------
 
 574 int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
 
 576 if(GetAllParams(usersList, "users", "login"))
 
 581 //-----------------------------------------------------------------------------
 
 582 int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
 
 584 if(GetAllParams(adminsList, "admins", "login"))
 
 589 //-----------------------------------------------------------------------------
 
 590 int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
 
 592 if(GetAllParams(tariffsList, "tariffs", "name"))
 
 597 //-----------------------------------------------------------------------------
 
 598 int MYSQL_STORE::AddUser(const std::string & login) const
 
 600 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
 
 602 if(MysqlSetQuery(qbuf))
 
 604     errorStr = "Couldn't add user:\n";
 
 605     //errorStr += mysql_error(sock);
 
 611 //-----------------------------------------------------------------------------
 
 612 int MYSQL_STORE::DelUser(const std::string & login) const
 
 614 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
 
 616 if(MysqlSetQuery(qbuf))
 
 618     errorStr = "Couldn't delete user:\n";
 
 619     //errorStr += mysql_error(sock);
 
 625 //-----------------------------------------------------------------------------
 
 626 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
 
 633 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
 
 634          AlwaysOnline, Tariff, Address, Phone, Email, Note, \
 
 635          RealName, StgGroup, Credit, TariffChange, ";
 
 637 for (int i = 0; i < USERDATA_NUM; i++)
 
 639     sprintf(qbuf, "Userdata%d, ", i);
 
 643 query += "CreditExpire, IP FROM users WHERE login='";
 
 644 query += login + "' LIMIT 1";
 
 646 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
 
 648 if(MysqlGetQuery(query.c_str(),sock))
 
 650     errorStr = "Couldn't restore Tariff(on query):\n";
 
 651     errorStr += mysql_error(sock);
 
 656 if (!(res=mysql_store_result(sock)))
 
 658     errorStr = "Couldn't restore Tariff(on getting result):\n";
 
 659     errorStr += mysql_error(sock);
 
 664 if (mysql_num_rows(res) != 1)
 
 666     errorStr = "User not found";
 
 671 row = mysql_fetch_row(res);
 
 675 conf->password = row[1];
 
 677 if (conf->password.empty())
 
 679     mysql_free_result(res);
 
 680     errorStr = "User \'" + login + "\' password is blank.";
 
 685 if (GetInt(row[2],&conf->passive) != 0)
 
 687     mysql_free_result(res);
 
 688     errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
 
 693 if (GetInt(row[3], &conf->disabled) != 0)
 
 695     mysql_free_result(res);
 
 696     errorStr = "User \'" + login + "\' data not read. Parameter Down.";
 
 701 if (GetInt(row[4], &conf->disabledDetailStat) != 0)
 
 703     mysql_free_result(res);
 
 704     errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
 
 709 if (GetInt(row[5], &conf->alwaysOnline) != 0)
 
 711     mysql_free_result(res);
 
 712     errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
 
 717 conf->tariffName = row[6];
 
 719 if (conf->tariffName.empty()) 
 
 721     mysql_free_result(res);
 
 722     errorStr = "User \'" + login + "\' tariff is blank.";
 
 727 conf->address = row[7];
 
 728 conf->phone = row[8];
 
 729 conf->email = row[9];
 
 730 conf->note = row[10];
 
 731 conf->realName = row[11];
 
 732 conf->group = row[12];
 
 734 if (GetDouble(row[13], &conf->credit, 0) != 0)
 
 736     mysql_free_result(res);
 
 737     errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
 
 742 conf->nextTariff = row[14];
 
 744 for (int i = 0; i < USERDATA_NUM; i++)
 
 746     conf->userdata[i] = row[15+i];
 
 749 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
 
 751 std::string ipStr = row[16+USERDATA_NUM];
 
 757 catch (const std::string & s)
 
 759     mysql_free_result(res);
 
 760     errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
 
 766 mysql_free_result(res);
 
 771 //-----------------------------------------------------------------------------
 
 772 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
 
 782 for (int i = 0; i < DIR_NUM; i++)
 
 784     sprintf(qbuf, "D%d, U%d, ", i, i);
 
 788 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
 
 789           FROM users WHERE login = '";
 
 790 query += login + "'";
 
 792 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
 
 794 if(MysqlGetQuery(query.c_str() ,sock))
 
 796     errorStr = "Couldn't restore UserStat(on query):\n";
 
 797     errorStr += mysql_error(sock);
 
 802 if (!(res=mysql_store_result(sock)))
 
 804     errorStr = "Couldn't restore UserStat(on getting result):\n";
 
 805     errorStr += mysql_error(sock);
 
 810 row = mysql_fetch_row(res);
 
 812 unsigned int startPos=0;
 
 816 for (int i = 0; i < DIR_NUM; i++)
 
 819     sprintf(s, "D%d", i);
 
 820     if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
 
 822         mysql_free_result(res);
 
 823         errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
 
 827     stat->down[i] = traff;
 
 829     sprintf(s, "U%d", i);
 
 830     if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
 
 832         mysql_free_result(res);
 
 833         errorStr =   "User \'" + login + "\' stat not read. Parameter " + std::string(s);
 
 840 startPos += (2*DIR_NUM);
 
 842 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
 
 844     mysql_free_result(res);
 
 845     errorStr =   "User \'" + login + "\' stat not read. Parameter Cash";
 
 850 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
 
 852     mysql_free_result(res);
 
 853     errorStr =   "User \'" + login + "\' stat not read. Parameter FreeMb";
 
 858 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
 
 860     mysql_free_result(res);
 
 861     errorStr =   "User \'" + login + "\' stat not read. Parameter LastCashAdd";
 
 866 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
 
 868     mysql_free_result(res);
 
 869     errorStr =   "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
 
 874 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
 
 876     mysql_free_result(res);
 
 877     errorStr =   "User \'" + login + "\' stat not read. Parameter PassiveTime";
 
 882 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
 
 884     mysql_free_result(res);
 
 885     errorStr =   "User \'" + login + "\' stat not read. Parameter LastActivityTime";
 
 890 mysql_free_result(res);
 
 894 //-----------------------------------------------------------------------------
 
 895 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
 
 900 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
 
 901     "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
 
 902     "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ", 
 
 903     conf.password.c_str(),
 
 906     conf.disabledDetailStat,
 
 908     conf.tariffName.c_str(),
 
 909     (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
 
 910     (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
 
 911     (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
 
 912     (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
 
 913     (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
 
 914     (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
 
 916     conf.nextTariff.c_str()
 
 919 for (int i = 0; i < USERDATA_NUM; i++)
 
 921     strprintf(¶m, " Userdata%d='%s',", i, 
 
 922         (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
 
 926 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
 
 929 std::ostringstream ipStr;
 
 932 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
 
 935 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
 
 938 if(MysqlSetQuery(res.c_str()))
 
 940     errorStr = "Couldn't save user conf:\n";
 
 941     //errorStr += mysql_error(sock);
 
 947 //-----------------------------------------------------------------------------
 
 948 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
 
 953 res = "UPDATE users SET";
 
 955 for (int i = 0; i < DIR_NUM; i++)
 
 957     strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
 
 960     strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
 
 964 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
 
 965     " PassiveTime=%d, LastActivityTime=%d", 
 
 969     stat.lastCashAddTime,
 
 971     stat.lastActivityTime
 
 975 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
 
 978 if(MysqlSetQuery(res.c_str()))
 
 980     errorStr = "Couldn't save user stat:\n";
 
 981 //    errorStr += mysql_error(sock);
 
 987 //-----------------------------------------------------------------------------
 
 988 int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
 
 990 std::string res, tempStr;
 
 999 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
 
1000 if (!(sock=MysqlConnect())){
 
1001     errorStr = "Couldn't connect to Server";
 
1004 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
 
1006     errorStr = "Couldn't get table " + tempStr + ":\n";
 
1007     errorStr += mysql_error(sock);
 
1012 my_ulonglong num_rows =  mysql_num_rows(result);
 
1014 mysql_free_result(result);
 
1018     sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
 
1019     lt->tm_mon+1, lt->tm_year+1900);
 
1021     if(MysqlQuery(qbuf,sock))
 
1023         errorStr = "Couldn't create WriteDetailedStat table:\n";
 
1024         errorStr += mysql_error(sock);
 
1030 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
 
1034 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
 
1035         lt->tm_mon+1, lt->tm_year+1900,
 
1036     login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
 
1038 if(MysqlQuery(send.c_str(),sock))
 
1040     errorStr = "Couldn't write log string:\n";
 
1041     errorStr += mysql_error(sock);
 
1049 //-----------------------------------------------------------------------------
 
1050 int MYSQL_STORE::WriteUserChgLog(const std::string & login,
 
1051                                  const std::string & admLogin,
 
1053                                  const std::string & paramName,
 
1054                                  const std::string & oldValue,
 
1055                                  const std::string & newValue,
 
1056                                  const std::string & message) const
 
1058 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
 
1059     + paramName + "\' parameter changed from \'" + oldValue +
 
1060     "\' to \'" + newValue + "\'. " + message;
 
1062 return WriteLogString(userLogMsg, login);
 
1064 //-----------------------------------------------------------------------------
 
1065 int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
 
1067 std::string logStr = "Connect, " + inet_ntostring(ip);
 
1068 return WriteLogString(logStr, login);
 
1070 //-----------------------------------------------------------------------------
 
1071 int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
 
1072                                      const DIR_TRAFF & up,
 
1073                                      const DIR_TRAFF & down,
 
1074                                      const DIR_TRAFF & sessionUp,
 
1075                                      const DIR_TRAFF & sessionDown,
 
1078                                      const std::string & /*reason*/) const
 
1080 std::string logStr = "Disconnect, ";
 
1081 std::ostringstream sssu;
 
1082 std::ostringstream sssd;
 
1083 std::ostringstream ssmu;
 
1084 std::ostringstream ssmd;
 
1085 std::ostringstream sscash;
 
1091 sssd << sessionDown;
 
1095 logStr += " session upload: \'";
 
1096 logStr += sssu.str();
 
1097 logStr += "\' session download: \'";
 
1098 logStr += sssd.str();
 
1099 logStr += "\' month upload: \'";
 
1100 logStr += ssmu.str();
 
1101 logStr += "\' month download: \'";
 
1102 logStr += ssmd.str();
 
1103 logStr += "\' cash: \'";
 
1104 logStr += sscash.str();
 
1107 return WriteLogString(logStr, login);
 
1109 //-----------------------------------------------------------------------------
 
1110 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, 
 
1111                                 const std::string & login) const
 
1113 std::string param, res;
 
1115 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,", 
 
1116     login.c_str(), month+1, year+1900);
 
1118 for (int i = 0; i < DIR_NUM; i++)
 
1120     strprintf(¶m, " U%d=%lld,", i, stat.up[i]); 
 
1123     strprintf(¶m, " D%d=%lld,", i, stat.down[i]);        
 
1127 strprintf(¶m, " cash=%f", stat.cash);        
 
1130 if(MysqlSetQuery(res.c_str()))
 
1132     errorStr = "Couldn't SaveMonthStat:\n";
 
1133     //errorStr += mysql_error(sock);
 
1139 //-----------------------------------------------------------------------------*/
 
1140 int MYSQL_STORE::AddAdmin(const std::string & login) const
 
1142 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
 
1144 if(MysqlSetQuery(qbuf))
 
1146     errorStr = "Couldn't add admin:\n";
 
1147     //errorStr += mysql_error(sock);
 
1153 //-----------------------------------------------------------------------------*/
 
1154 int MYSQL_STORE::DelAdmin(const std::string & login) const
 
1156 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
 
1158 if(MysqlSetQuery(qbuf))
 
1160     errorStr = "Couldn't delete admin:\n";
 
1161     //errorStr += mysql_error(sock);
 
1167 //-----------------------------------------------------------------------------*/
 
1168 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
 
1170 char passwordE[2 * ADM_PASSWD_LEN + 2];
 
1171 char pass[ADM_PASSWD_LEN + 1];
 
1172 char adminPass[ADM_PASSWD_LEN + 1];
 
1174 memset(pass, 0, sizeof(pass));
 
1175 memset(adminPass, 0, sizeof(adminPass));
 
1178 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
1180 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
 
1181 adminPass[ADM_PASSWD_LEN - 1] = 0;
 
1183 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
 
1185     EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
 
1188 pass[ADM_PASSWD_LEN - 1] = 0;
 
1189 Encode12(passwordE, pass, ADM_PASSWD_LEN);
 
1191 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
 
1192     "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
 
1193     "WHERE login='%s' LIMIT 1", 
 
1205 if(MysqlSetQuery(qbuf))
 
1207     errorStr = "Couldn't save admin:\n";
 
1208     //errorStr += mysql_error(sock);
 
1214 //-----------------------------------------------------------------------------
 
1215 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
 
1217 char pass[ADM_PASSWD_LEN + 1];
 
1218 char password[ADM_PASSWD_LEN + 1];
 
1219 char passwordE[2*ADM_PASSWD_LEN + 2];
 
1222 memset(pass, 0, sizeof(pass));
 
1223 memset(password, 0, sizeof(password));
 
1224 memset(passwordE, 0, sizeof(passwordE));
 
1230 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
 
1232 if(MysqlGetQuery(qbuf,sock))
 
1234     errorStr = "Couldn't restore admin:\n";
 
1235     errorStr += mysql_error(sock);
 
1240 if (!(res=mysql_store_result(sock)))
 
1242     errorStr = "Couldn't restore admin:\n";
 
1243     errorStr += mysql_error(sock);
 
1248 if ( mysql_num_rows(res) == 0)
 
1250     mysql_free_result(res);
 
1251     errorStr = "Couldn't restore admin as couldn't found him in table.\n";
 
1256 row = mysql_fetch_row(res);
 
1262     mysql_free_result(res);
 
1263     errorStr = "Error in parameter password";
 
1268 memset(passwordE, 0, sizeof(passwordE));
 
1269 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
 
1271 memset(pass, 0, sizeof(pass));
 
1273 if (passwordE[0] != 0)
 
1275     Decode21(pass, passwordE);
 
1276     EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
1278     for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
 
1280         DecodeString(password + 8*i, pass + 8*i, &ctx);
 
1288 ac->password = password;
 
1292 if (GetInt(row[2], &a) == 0) 
 
1293     ac->priv.userConf = a;
 
1296     mysql_free_result(res);
 
1297     errorStr = "Error in parameter ChgConf";
 
1302 if (GetInt(row[3], &a) == 0) 
 
1303     ac->priv.userPasswd = a;
 
1306     mysql_free_result(res);
 
1307     errorStr = "Error in parameter ChgPassword";
 
1312 if (GetInt(row[4], &a) == 0) 
 
1313     ac->priv.userStat = a;
 
1316     mysql_free_result(res);
 
1317     errorStr = "Error in parameter ChgStat";
 
1322 if (GetInt(row[5], &a) == 0) 
 
1323     ac->priv.userCash = a;
 
1326     mysql_free_result(res);
 
1327     errorStr = "Error in parameter ChgCash";
 
1332 if (GetInt(row[6], &a) == 0) 
 
1333     ac->priv.userAddDel = a;
 
1336     mysql_free_result(res);
 
1337     errorStr = "Error in parameter UsrAddDel";
 
1342 if (GetInt(row[7], &a) == 0) 
 
1343     ac->priv.tariffChg = a;
 
1346     mysql_free_result(res);
 
1347     errorStr = "Error in parameter ChgTariff";
 
1352 if (GetInt(row[8], &a) == 0) 
 
1353     ac->priv.adminChg = a;
 
1356     mysql_free_result(res);
 
1357     errorStr = "Error in parameter ChgAdmin";
 
1362 mysql_free_result(res);
 
1366 //-----------------------------------------------------------------------------
 
1367 int MYSQL_STORE::AddTariff(const std::string & name) const
 
1369 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
 
1371 if(MysqlSetQuery(qbuf))
 
1373     errorStr = "Couldn't add tariff:\n";
 
1374 //    errorStr += mysql_error(sock);
 
1380 //-----------------------------------------------------------------------------
 
1381 int MYSQL_STORE::DelTariff(const std::string & name) const
 
1383 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
 
1385 if(MysqlSetQuery(qbuf))
 
1387     errorStr = "Couldn't delete tariff: ";
 
1388 //    errorStr += mysql_error(sock);
 
1394 //-----------------------------------------------------------------------------
 
1395 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
 
1400 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
 
1402 if(MysqlGetQuery(qbuf,sock))
 
1404     errorStr = "Couldn't restore Tariff:\n";
 
1405     errorStr += mysql_error(sock);
 
1410 if (!(res=mysql_store_result(sock)))
 
1412     errorStr = "Couldn't restore Tariff:\n";
 
1413     errorStr += mysql_error(sock);
 
1419 td->tariffConf.name = tariffName;
 
1421 row = mysql_fetch_row(res);
 
1424 for (int i = 0; i<DIR_NUM; i++)
 
1426     strprintf(¶m, "Time%d", i);
 
1428     if (str.length() == 0)
 
1430         mysql_free_result(res);
 
1431         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1436     ParseTariffTimeStr(str.c_str(), 
 
1437                        td->dirPrice[i].hDay, 
 
1438                        td->dirPrice[i].mDay, 
 
1439                        td->dirPrice[i].hNight, 
 
1440                        td->dirPrice[i].mNight);
 
1442     strprintf(¶m, "PriceDayA%d", i);
 
1443     if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
 
1445         mysql_free_result(res);
 
1446         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1450     td->dirPrice[i].priceDayA /= (1024*1024);
 
1452     strprintf(¶m, "PriceDayB%d", i);
 
1453     if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
 
1455         mysql_free_result(res);
 
1456         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1460     td->dirPrice[i].priceDayB /= (1024*1024);
 
1462     strprintf(¶m, "PriceNightA%d", i);
 
1463     if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
 
1465         mysql_free_result(res);
 
1466         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1470     td->dirPrice[i].priceNightA /= (1024*1024);
 
1472     strprintf(¶m, "PriceNightB%d", i);
 
1473     if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
 
1475         mysql_free_result(res);
 
1476         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1480     td->dirPrice[i].priceNightB /= (1024*1024);
 
1482     strprintf(¶m, "Threshold%d", i);
 
1483     if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
 
1485         mysql_free_result(res);
 
1486         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1491     strprintf(¶m, "SinglePrice%d", i);
 
1492     if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
 
1494         mysql_free_result(res);
 
1495         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1500     strprintf(¶m, "NoDiscount%d", i);
 
1501     if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
 
1503         mysql_free_result(res);
 
1504         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1510 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
 
1512     mysql_free_result(res);
 
1513     errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
 
1518 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
 
1520     mysql_free_result(res);
 
1521     errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
 
1526 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
 
1528     mysql_free_result(res);
 
1529     errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
 
1534     str = row[4+8*DIR_NUM];
 
1535     param = "TraffType";
 
1537     if (str.length() == 0)
 
1539         mysql_free_result(res);
 
1540         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1545 if (!strcasecmp(str.c_str(), "up"))
 
1546     td->tariffConf.traffType = TRAFF_UP;
 
1548     if (!strcasecmp(str.c_str(), "down"))
 
1549         td->tariffConf.traffType = TRAFF_DOWN;
 
1551         if (!strcasecmp(str.c_str(), "up+down"))
 
1552             td->tariffConf.traffType = TRAFF_UP_DOWN;
 
1554             if (!strcasecmp(str.c_str(), "max"))
 
1555                 td->tariffConf.traffType = TRAFF_MAX;
 
1558                 mysql_free_result(res);
 
1559                 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
 
1564 mysql_free_result(res);
 
1568 //-----------------------------------------------------------------------------
 
1569 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
 
1573 std::string res="UPDATE tariffs SET";
 
1575 for (int i = 0; i < DIR_NUM; i++)
 
1577     strprintf(¶m, " PriceDayA%d=%f,", i, 
 
1578         td.dirPrice[i].priceDayA * pt_mega);
 
1581     strprintf(¶m, " PriceDayB%d=%f,", i, 
 
1582         td.dirPrice[i].priceDayB * pt_mega);        
 
1585     strprintf(¶m, " PriceNightA%d=%f,", i,
 
1586         td.dirPrice[i].priceNightA * pt_mega);
 
1589     strprintf(¶m, " PriceNightB%d=%f,", i, 
 
1590         td.dirPrice[i].priceNightB * pt_mega);
 
1593     strprintf(¶m, " Threshold%d=%d,", i, 
 
1594         td.dirPrice[i].threshold);
 
1598     strprintf(¶m, " Time%d", i);
 
1600     strprintf(&s, "%0d:%0d-%0d:%0d", 
 
1601             td.dirPrice[i].hDay,
 
1602             td.dirPrice[i].mDay,
 
1603             td.dirPrice[i].hNight,
 
1604             td.dirPrice[i].mNight);
 
1606     res += (param + "='" + s + "',");
 
1608     strprintf(¶m, " NoDiscount%d=%d,", i, 
 
1609         td.dirPrice[i].noDiscount);
 
1612     strprintf(¶m, " SinglePrice%d=%d,", i, 
 
1613         td.dirPrice[i].singlePrice);
 
1617 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
 
1620 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
 
1623 strprintf(¶m, " Free=%f,", td.tariffConf.free);
 
1626 switch (td.tariffConf.traffType)
 
1629         res += " TraffType='up'";
 
1632         res += " TraffType='down'";
 
1635         res += " TraffType='up+down'";
 
1638         res += " TraffType='max'";
 
1641 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
 
1644 if(MysqlSetQuery(res.c_str()))
 
1646     errorStr = "Couldn't save admin:\n";
 
1647     //errorStr += mysql_error(sock);
 
1653 //-----------------------------------------------------------------------------
 
1654 int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree, 
 
1656                                    const std::string & login) const
 
1658 std::string res, stTime, endTime, tempStr;
 
1665 if (lt->tm_hour == 0 && lt->tm_min <= 5)
 
1673 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
 
1675 if (!(sock=MysqlConnect())){
 
1680 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
 
1682     errorStr = "Couldn't get table " + tempStr + ":\n";
 
1683     errorStr += mysql_error(sock);
 
1688 my_ulonglong num_rows =  mysql_num_rows(result);
 
1690 mysql_free_result(result);
 
1694     sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
 
1695         "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
 
1696         "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
 
1697         "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
 
1698     lt->tm_mon+1, lt->tm_year+1900);
 
1700     if(MysqlQuery(qbuf,sock))
 
1702         errorStr = "Couldn't create WriteDetailedStat table:\n";
 
1703         errorStr += mysql_error(sock);
 
1712 lt1 = localtime(&lastStat);
 
1721 lt2 = localtime(&t);
 
1727 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
 
1728 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
 
1730 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
 
1731     "day=%d,startTime='%s',endTime='%s',", 
 
1732     lt->tm_mon+1, lt->tm_year+1900,
 
1739 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
 
1740 stIter = statTree.begin();
 
1742 while (stIter != statTree.end())
 
1744         strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f", 
 
1745                 inet_ntostring(stIter->first.ip).c_str(),
 
1747                 stIter->second.down, 
 
1752         if( MysqlQuery((res+tempStr).c_str(),sock) )
 
1754             errorStr = "Couldn't insert data in WriteDetailedStat:\n";
 
1755             errorStr += mysql_error(sock);
 
1760         result=mysql_store_result(sock);
 
1762             mysql_free_result(result);
 
1769 //-----------------------------------------------------------------------------
 
1770 int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
 
1774 gettimeofday(&tv, NULL);
 
1776 msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
 
1778 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld", 
 
1780     static_cast<long long>(msg->header.id)
 
1783 if(MysqlSetQuery(qbuf))
 
1785     errorStr = "Couldn't add message:\n";
 
1786     //errorStr += mysql_error(sock);
 
1790 return EditMessage(*msg, login);
 
1792 //-----------------------------------------------------------------------------
 
1793 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
 
1797 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
 
1798     "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
 
1799     "WHERE login='%s' AND id=%lld LIMIT 1", 
 
1801     msg.header.lastSendTime,
 
1802     msg.header.creationTime,
 
1803     msg.header.showTime,
 
1805     msg.header.repeatPeriod,
 
1806     (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
 
1811 if(MysqlSetQuery(res.c_str()))
 
1813     errorStr = "Couldn't edit message:\n";
 
1814     //errorStr += mysql_error(sock);
 
1820 //-----------------------------------------------------------------------------
 
1821 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
 
1827 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
 
1828         login.c_str(), static_cast<unsigned long long>(id));
 
1830 if(MysqlGetQuery(qbuf,sock))
 
1832     errorStr = "Couldn't GetMessage:\n";
 
1833     errorStr += mysql_error(sock);
 
1838 if (!(res=mysql_store_result(sock)))
 
1840     errorStr = "Couldn't GetMessage:\n";
 
1841     errorStr += mysql_error(sock);
 
1846 row = mysql_fetch_row(res);
 
1848 if(row[2]&&str2x(row[2], msg->header.type))
 
1850     mysql_free_result(res);
 
1851     errorStr = "Invalid value in message header for user: " + login;
 
1856 if(row[3] && str2x(row[3], msg->header.lastSendTime))
 
1858     mysql_free_result(res);
 
1859     errorStr = "Invalid value in message header for user: " + login;
 
1864 if(row[4] && str2x(row[4], msg->header.creationTime))
 
1866     mysql_free_result(res);
 
1867     errorStr = "Invalid value in message header for user: " + login;
 
1872 if(row[5] && str2x(row[5], msg->header.showTime))
 
1874     mysql_free_result(res);
 
1875     errorStr = "Invalid value in message header for user: " + login;
 
1880 if(row[6] && str2x(row[6], msg->header.repeat))
 
1882     mysql_free_result(res);
 
1883     errorStr = "Invalid value in message header for user: " + login;
 
1888 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
 
1890     mysql_free_result(res);
 
1891     errorStr = "Invalid value in message header for user: " + login;
 
1896 msg->header.id = id;
 
1899 mysql_free_result(res);
 
1903 //-----------------------------------------------------------------------------
 
1904 int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
 
1906 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1", 
 
1907         login.c_str(), static_cast<long long>(id));
 
1909 if(MysqlSetQuery(qbuf))
 
1911     errorStr = "Couldn't delete Message:\n";
 
1912     //errorStr += mysql_error(sock);
 
1918 //-----------------------------------------------------------------------------
 
1919 int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
 
1924 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
 
1926 if(MysqlGetQuery(qbuf,sock))
 
1928     errorStr = "Couldn't GetMessageHdrs:\n";
 
1929     errorStr += mysql_error(sock);
 
1934 if (!(res=mysql_store_result(sock)))
 
1936     errorStr = "Couldn't GetMessageHdrs:\n";
 
1937     errorStr += mysql_error(sock);
 
1943 my_ulonglong num_rows = mysql_num_rows(res);
 
1946 for (i = 0; i < num_rows; i++)
 
1948     row = mysql_fetch_row(res);
 
1949     if (str2x(row[1], id))
 
1954         if(str2x(row[2], hdr.type))
 
1958         if(str2x(row[3], hdr.lastSendTime))
 
1962         if(str2x(row[4], hdr.creationTime))
 
1966         if(str2x(row[5], hdr.showTime))
 
1970         if(str2x(row[6], hdr.repeat))
 
1974         if(str2x(row[7], hdr.repeatPeriod))
 
1978     hdrsList->push_back(hdr);
 
1981 mysql_free_result(res);
 
1985 //-----------------------------------------------------------------------------
 
1987 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
 
1990     int ret=MysqlGetQuery(Query,sock);
 
1994 //-----------------------------------------------------------------------------
 
1995 int  MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
 
1996     if (!(sock=MysqlConnect())) {
 
1999     return   MysqlQuery(Query,sock);
 
2001 //-----------------------------------------------------------------------------
 
2002 MYSQL *  MYSQL_STORE::MysqlConnect() const {
 
2004     if ( !(sock=mysql_init(NULL)) ){
 
2005         errorStr= "mysql init susck\n";
 
2008     if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
 
2009             storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
 
2012             errorStr = "Couldn't connect to mysql engine! With error:\n";
 
2013             errorStr += mysql_error(sock);
 
2017          if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
 
2018              errorStr = "Database lost !\n";
 
2024 //-----------------------------------------------------------------------------