12 #include "user_conf.h"
 
  13 #include "user_stat.h"
 
  14 #include "mysql_store.h"
 
  17 #define adm_enc_passwd "cjeifY8m3"
 
  22 const int pt_mega = 1024 * 1024;
 
  23 const string badSyms = "'`";
 
  24 const char repSym = '\"';
 
  25 const int RepitTimes = 3;
 
  27 int GetInt(const string & str, int * val, int defaultVal)
 
  31     *val = strtol(str.c_str(), &res, 10);
 
  35         *val = defaultVal; //Error!
 
  42 int GetDouble(const string & str, double * val, double defaultVal)
 
  46     *val = strtod(str.c_str(), &res);
 
  50         *val = defaultVal; //Error!
 
  57 int GetTime(const string & str, time_t * val, time_t defaultVal)
 
  61     *val = strtol(str.c_str(), &res, 10);
 
  65         *val = defaultVal; //Error!
 
  72 //-----------------------------------------------------------------------------
 
  73 string ReplaceStr(string source, const string symlist, const char chgsym)
 
  75     string::size_type pos=0;
 
  77     while( (pos = source.find_first_of(symlist,pos)) != string::npos)
 
  78         source.replace(pos, 1,1, chgsym);
 
  83 int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
 
  87     *val = strtoull(str.c_str(), &res, 10);
 
  91         *val = defaultVal; //Error!
 
  98 class MYSQL_STORE_CREATOR
 
 104     MYSQL_STORE_CREATOR()
 
 105         : ms(new MYSQL_STORE())
 
 108     ~MYSQL_STORE_CREATOR()
 
 113     MYSQL_STORE * GetStore()
 
 118 //-----------------------------------------------------------------------------
 
 119 //-----------------------------------------------------------------------------
 
 120 //-----------------------------------------------------------------------------
 
 121 BASE_STORE * GetStore()
 
 123 return msc.GetStore();
 
 125 //-----------------------------------------------------------------------------
 
 126 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
 
 130 //-----------------------------------------------------------------------------
 
 131 MYSQL_STORE_SETTINGS::~MYSQL_STORE_SETTINGS()
 
 135 //-----------------------------------------------------------------------------
 
 136 int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams, 
 
 137                         const string & name, string & result)
 
 141 vector<PARAM_VALUE>::const_iterator pvi;
 
 142 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
 
 143 if (pvi == moduleParams.end())
 
 145     errorStr = "Parameter \'" + name + "\' not found.";
 
 149 result = pvi->value[0];
 
 153 //-----------------------------------------------------------------------------
 
 154 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
 
 156 if (ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
 
 158 if (ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
 
 160 if (ParseParam(s.moduleParams, "dbname", dbName) < 0)
 
 162 if (ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
 
 167 //-----------------------------------------------------------------------------
 
 168 const string & MYSQL_STORE_SETTINGS::GetStrError() const
 
 172 //-----------------------------------------------------------------------------
 
 173 string MYSQL_STORE_SETTINGS::GetDBUser() const
 
 177 //-----------------------------------------------------------------------------
 
 178 string MYSQL_STORE_SETTINGS::GetDBPassword() const
 
 182 //-----------------------------------------------------------------------------
 
 183 string MYSQL_STORE_SETTINGS::GetDBHost() const
 
 187 //-----------------------------------------------------------------------------
 
 188 string MYSQL_STORE_SETTINGS::GetDBName() const
 
 192 //-----------------------------------------------------------------------------
 
 193 //-----------------------------------------------------------------------------
 
 194 //-----------------------------------------------------------------------------
 
 195 MYSQL_STORE::MYSQL_STORE()
 
 197 version = "mysql_store v.0.67";
 
 199 //-----------------------------------------------------------------------------
 
 200 MYSQL_STORE::~MYSQL_STORE()
 
 203 //-----------------------------------------------------------------------------
 
 204 void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
 
 208 //-----------------------------------------------------------------------------
 
 209 int    MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
 
 213     if( (ret = mysql_query(sock,sQuery)) )
 
 215         for(i=0; i<RepitTimes; i++)
 
 217             if( (ret = mysql_query(sock,sQuery)) )
 
 218                 ;//need to send error result
 
 226 //-----------------------------------------------------------------------------
 
 228 //-----------------------------------------------------------------------------
 
 229 int MYSQL_STORE::ParseSettings()
 
 231 int ret = storeSettings.ParseSettings(settings);
 
 236     errorStr = storeSettings.GetStrError();
 
 239     if(storeSettings.GetDBPassword().length() == 0)
 
 241         errorStr = "Database password must be not empty. Please read Manual.";
 
 245     if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
 
 246             storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
 
 249             errorStr = "Couldn't connect to mysql engine! With error:\n";
 
 250             errorStr += mysql_error(&mysql);
 
 256          if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
 
 258              string res = "CREATE DATABASE " + storeSettings.GetDBName();
 
 260             if(MysqlQuery(res.c_str(),sock))
 
 262                 errorStr = "Couldn't create database! With error:\n";
 
 263                 errorStr += mysql_error(sock);
 
 269                  if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
 
 271                     errorStr = "Couldn't select database! With error:\n";
 
 272                     errorStr += mysql_error(sock);
 
 276                  ret = CheckAllTables(sock);
 
 280              ret = CheckAllTables(sock);
 
 287 //-----------------------------------------------------------------------------
 
 288 const string & MYSQL_STORE::GetStrError() const
 
 292 //-----------------------------------------------------------------------------
 
 293 const string & MYSQL_STORE::GetVersion() const
 
 297 //-----------------------------------------------------------------------------
 
 298 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
 
 302 if (!(result=mysql_list_tables(sock,str.c_str() )))
 
 304     errorStr = "Couldn't get tables list With error:\n";
 
 305     errorStr += mysql_error(sock);
 
 310 unsigned int num_rows =  mysql_num_rows(result);
 
 313     mysql_free_result(result);
 
 315 return (num_rows == 1);
 
 317 //-----------------------------------------------------------------------------
 
 318 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
 
 320 //admins-----------------------------------------------------------------------
 
 321 if(!IsTablePresent("admins",sock))
 
 323     sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
 
 324         "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
 
 325         "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
 
 326         "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
 
 327         "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
 
 329     if(MysqlQuery(qbuf,sock))
 
 331         errorStr = "Couldn't create admin table list With error:\n";
 
 332         errorStr += mysql_error(sock);
 
 337     sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
 
 338         "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
 
 339         "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
 
 341     if(MysqlQuery(qbuf,sock))
 
 343         errorStr = "Couldn't create default admin. With error:\n";
 
 344         errorStr += mysql_error(sock);
 
 350 //tariffs-----------------------------------------------------------------------
 
 352 if(!IsTablePresent("tariffs",sock))
 
 354     res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
 
 356     for (int i = 0; i < DIR_NUM; i++)
 
 358         strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i); 
 
 361         strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
 
 364         strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
 
 367         strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
 
 370         strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
 
 373         strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
 
 376         strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
 
 379         strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
 
 383     res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
 
 384         "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
 
 386     if(MysqlQuery(res.c_str(),sock))
 
 388         errorStr = "Couldn't create tariffs table list With error:\n";
 
 389         errorStr += mysql_error(sock);
 
 394     res = "INSERT INTO tariffs SET name='tariff',";
 
 396     for (int i = 0; i < DIR_NUM; i++)
 
 398         strprintf(¶m, " NoDiscount%d=1,", i);
 
 401         strprintf(¶m, " Threshold%d=0,", i);
 
 404         strprintf(¶m, " Time%d='0:0-0:0',", i);
 
 409             strprintf(¶m, " SinglePrice%d=0,", i);
 
 415             strprintf(¶m, " PriceDayA%d=0.0,", i); 
 
 420             strprintf(¶m, " PriceDayB%d=0.0,", i);        
 
 426             strprintf(¶m, " PriceNightA%d=0.0,", i); 
 
 431             strprintf(¶m, " PriceNightB%d=0.0,", i);        
 
 436     res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
 
 437         "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
 
 438         "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
 
 440     if(MysqlQuery(res.c_str(),sock))
 
 442         errorStr = "Couldn't create default tariff. With error:\n";
 
 443         errorStr += mysql_error(sock);
 
 449 //users-----------------------------------------------------------------------
 
 450 if(!IsTablePresent("users",sock))
 
 452     res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
 
 453         "Passive INT(3) DEFAULT 0,Down INT(3) DEFAULT 0,DisabledDetailStat INT(3) DEFAULT 0,AlwaysOnline INT(3) DEFAULT 0,Tariff VARCHAR(40) NOT NULL DEFAULT '',"\
 
 454         "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
 
 455         "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
 
 456         "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
 
 458     for (int i = 0; i < USERDATA_NUM; i++)
 
 460         strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
 
 464     param = " CreditExpire INT(11) DEFAULT 0,";
 
 467     strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
 
 470     for (int i = 0; i < DIR_NUM; i++)
 
 472         strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
 
 475         strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
 
 479     strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
 
 480         "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
 
 481         "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
 
 482         " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
 
 485     if(MysqlQuery(res.c_str(),sock))
 
 487         errorStr = "Couldn't create users table list With error:\n";
 
 488         errorStr += mysql_error(sock);
 
 489         errorStr += "\n\n" + res;
 
 494     res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
 
 495         "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
 
 496         "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
 
 497         "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
 
 500     for (int i = 0; i < DIR_NUM; i++)
 
 502         strprintf(¶m, " D%d=0,", i);
 
 505         strprintf(¶m, " U%d=0,", i);
 
 509     res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
 
 510         "LastCashAddTime=0, PassiveTime=0";
 
 512     if(MysqlQuery(res.c_str(),sock))
 
 514         errorStr = "Couldn't create default user. With error:\n";
 
 515         errorStr += mysql_error(sock);
 
 521 //logs-----------------------------------------------------------------------
 
 522 if(!IsTablePresent("logs"))
 
 524     sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
 
 528         errorStr = "Couldn't create admin table list With error:\n";
 
 529         errorStr += mysql_error(sock);
 
 534 //messages---------------------------------------------------------------------
 
 535 if(!IsTablePresent("messages",sock))
 
 537     sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
 
 538             "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
 
 539             "stgRepeat INT, repeatPeriod INT, text TEXT)");
 
 541     if(MysqlQuery(qbuf,sock))
 
 543         errorStr = "Couldn't create messages table. With error:\n";
 
 544         errorStr += mysql_error(sock);
 
 550 //month_stat-------------------------------------------------------------------
 
 551 if(!IsTablePresent("stat",sock))
 
 553     res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
 
 555     for (int i = 0; i < DIR_NUM; i++)
 
 557         strprintf(¶m, " U%d BIGINT,", i); 
 
 560         strprintf(¶m, " D%d BIGINT,", i); 
 
 564     res += " cash DOUBLE, INDEX (login))";
 
 566     if(MysqlQuery(res.c_str(),sock))
 
 568         errorStr = "Couldn't create stat table. With error:\n";
 
 569         errorStr += mysql_error(sock);
 
 577 //-----------------------------------------------------------------------------
 
 578 //-----------------------------------------------------------------------------
 
 580 int MYSQL_STORE::GetAllParams(vector<string> * ParamList, 
 
 581                             const string & table, const string & name) const
 
 590 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
 
 592 if(MysqlGetQuery(qbuf,sock))
 
 594     errorStr = "Couldn't GetAllParams Query for: ";
 
 595     errorStr += name + " - " + table + "\n";
 
 596     errorStr += mysql_error(sock);
 
 601 if (!(res=mysql_store_result(sock)))
 
 603     errorStr = "Couldn't GetAllParams Results for: ";
 
 604     errorStr += name + " - " + table + "\n";
 
 605     errorStr += mysql_error(sock);
 
 609 num = mysql_num_rows(res);
 
 613     row = mysql_fetch_row(res);    
 
 614     ParamList->push_back(row[0]);
 
 617 mysql_free_result(res);
 
 623 //-----------------------------------------------------------------------------
 
 624 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
 
 626 if(GetAllParams(usersList, "users", "login"))
 
 631 //-----------------------------------------------------------------------------
 
 632 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
 
 634 if(GetAllParams(adminsList, "admins", "login"))
 
 639 //-----------------------------------------------------------------------------
 
 640 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
 
 642 if(GetAllParams(tariffsList, "tariffs", "name"))
 
 647 //-----------------------------------------------------------------------------
 
 648 int MYSQL_STORE::AddUser(const string & login) const
 
 650 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
 
 652 if(MysqlSetQuery(qbuf))
 
 654     errorStr = "Couldn't add user:\n";
 
 655     //errorStr += mysql_error(sock);
 
 661 //-----------------------------------------------------------------------------
 
 662 int MYSQL_STORE::DelUser(const string & login) const
 
 664 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
 
 666 if(MysqlSetQuery(qbuf))
 
 668     errorStr = "Couldn't delete user:\n";
 
 669     //errorStr += mysql_error(sock);
 
 675 //-----------------------------------------------------------------------------
 
 676 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
 
 683 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
 
 684          AlwaysOnline, Tariff, Address, Phone, Email, Note, \
 
 685          RealName, StgGroup, Credit, TariffChange, ";
 
 687 for (int i = 0; i < USERDATA_NUM; i++)
 
 689     sprintf(qbuf, "Userdata%d, ", i);
 
 693 query += "CreditExpire, IP FROM users WHERE login='";
 
 694 query += login + "' LIMIT 1";
 
 696 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
 
 698 if(MysqlGetQuery(query.c_str(),sock))
 
 700     errorStr = "Couldn't restore Tariff(on query):\n";
 
 701     errorStr += mysql_error(sock);
 
 706 if (!(res=mysql_store_result(sock)))
 
 708     errorStr = "Couldn't restore Tariff(on getting result):\n";
 
 709     errorStr += mysql_error(sock);
 
 714 row = mysql_fetch_row(res);
 
 718 conf->password = row[1];
 
 720 if (conf->password.empty())
 
 722     mysql_free_result(res);
 
 723     errorStr = "User \'" + login + "\' password is blank.";
 
 728 if (GetInt(row[2],&conf->passive, 0) != 0)
 
 730     mysql_free_result(res);
 
 731     errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
 
 736 if (GetInt(row[3], &conf->disabled, 0) != 0)
 
 738     mysql_free_result(res);
 
 739     errorStr = "User \'" + login + "\' data not read. Parameter Down.";
 
 744 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
 
 746     mysql_free_result(res);
 
 747     errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
 
 752 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
 
 754     mysql_free_result(res);
 
 755     errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
 
 760 conf->tariffName = row[6];
 
 762 if (conf->tariffName.empty()) 
 
 764     mysql_free_result(res);
 
 765     errorStr = "User \'" + login + "\' tariff is blank.";
 
 770 conf->address = row[7];
 
 771 conf->phone = row[8];
 
 772 conf->email = row[9];
 
 773 conf->note = row[10];
 
 774 conf->realName = row[11];
 
 775 conf->group = row[12];
 
 777 if (GetDouble(row[13], &conf->credit, 0) != 0)
 
 779     mysql_free_result(res);
 
 780     errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
 
 785 conf->nextTariff = row[14];
 
 787 for (int i = 0; i < USERDATA_NUM; i++)
 
 789     conf->userdata[i] = row[15+i];
 
 792 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
 
 794 string ipStr = row[16+USERDATA_NUM];
 
 802     mysql_free_result(res);
 
 803     errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
 
 809 mysql_free_result(res);
 
 814 //-----------------------------------------------------------------------------
 
 815 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
 
 825 for (int i = 0; i < DIR_NUM; i++)
 
 827     sprintf(qbuf, "D%d, U%d, ", i, i);
 
 831 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
 
 832           FROM users WHERE login = '";
 
 833 query += login + "'";
 
 835 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
 
 837 if(MysqlGetQuery(query.c_str() ,sock))
 
 839     errorStr = "Couldn't restore UserStat(on query):\n";
 
 840     errorStr += mysql_error(sock);
 
 845 if (!(res=mysql_store_result(sock)))
 
 847     errorStr = "Couldn't restore UserStat(on getting result):\n";
 
 848     errorStr += mysql_error(sock);
 
 853 row = mysql_fetch_row(res);
 
 855 unsigned int startPos=0;
 
 859 for (int i = 0; i < DIR_NUM; i++)
 
 862     sprintf(s, "D%d", i);
 
 863     if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
 
 865         mysql_free_result(res);
 
 866         errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
 
 870     stat->down[i] = traff;
 
 872     sprintf(s, "U%d", i);
 
 873     if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
 
 875         mysql_free_result(res);
 
 876         errorStr =   "User \'" + login + "\' stat not read. Parameter " + string(s);
 
 883 startPos += (2*DIR_NUM);
 
 885 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
 
 887     mysql_free_result(res);
 
 888     errorStr =   "User \'" + login + "\' stat not read. Parameter Cash";
 
 893 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
 
 895     mysql_free_result(res);
 
 896     errorStr =   "User \'" + login + "\' stat not read. Parameter FreeMb";
 
 901 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
 
 903     mysql_free_result(res);
 
 904     errorStr =   "User \'" + login + "\' stat not read. Parameter LastCashAdd";
 
 909 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
 
 911     mysql_free_result(res);
 
 912     errorStr =   "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
 
 917 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
 
 919     mysql_free_result(res);
 
 920     errorStr =   "User \'" + login + "\' stat not read. Parameter PassiveTime";
 
 925 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
 
 927     mysql_free_result(res);
 
 928     errorStr =   "User \'" + login + "\' stat not read. Parameter LastActivityTime";
 
 933 mysql_free_result(res);
 
 937 //-----------------------------------------------------------------------------
 
 938 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
 
 943 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
 
 944     "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
 
 945     "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ", 
 
 946     conf.password.c_str(),
 
 949     conf.disabledDetailStat,
 
 951     conf.tariffName.c_str(),
 
 952     (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
 
 953     (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
 
 954     (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
 
 955     (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
 
 956     (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
 
 957     (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
 
 959     conf.nextTariff.c_str()
 
 962 for (int i = 0; i < USERDATA_NUM; i++)
 
 964     strprintf(¶m, " Userdata%d='%s',", i, 
 
 965         (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
 
 969 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
 
 975 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
 
 978 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
 
 981 if(MysqlSetQuery(res.c_str()))
 
 983     errorStr = "Couldn't save user conf:\n";
 
 984     //errorStr += mysql_error(sock);
 
 990 //-----------------------------------------------------------------------------
 
 991 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
 
 996 res = "UPDATE users SET";
 
 998 for (int i = 0; i < DIR_NUM; i++)
 
1000     strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
 
1003     strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
 
1007 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
 
1008     " PassiveTime=%d, LastActivityTime=%d", 
 
1012     stat.lastCashAddTime,
 
1014     stat.lastActivityTime
 
1018 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
 
1021 if(MysqlSetQuery(res.c_str()))
 
1023     errorStr = "Couldn't save user stat:\n";
 
1024 //    errorStr += mysql_error(sock);
 
1030 //-----------------------------------------------------------------------------
 
1031 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
 
1033 string res, tempStr;
 
1042 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
 
1043 if (!(sock=MysqlConnect())){
 
1044     errorStr = "Couldn't connect to Server";
 
1047 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
 
1049     errorStr = "Couldn't get table " + tempStr + ":\n";
 
1050     errorStr += mysql_error(sock);
 
1055 unsigned int num_rows =  mysql_num_rows(result);
 
1057 mysql_free_result(result);
 
1061     sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
 
1062     lt->tm_mon+1, lt->tm_year+1900);
 
1064     if(MysqlQuery(qbuf,sock))
 
1066         errorStr = "Couldn't create WriteDetailedStat table:\n";
 
1067         errorStr += mysql_error(sock);
 
1073 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
 
1077 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
 
1078         lt->tm_mon+1, lt->tm_year+1900,
 
1079     login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
 
1081 if(MysqlQuery(send.c_str(),sock))
 
1083     errorStr = "Couldn't write log string:\n";
 
1084     errorStr += mysql_error(sock);
 
1092 //-----------------------------------------------------------------------------
 
1093 int MYSQL_STORE::WriteUserChgLog(const string & login,
 
1094                                  const string & admLogin,
 
1096                                  const string & paramName,
 
1097                                  const string & oldValue,
 
1098                                  const string & newValue,
 
1099                                  const string & message) const
 
1101 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
 
1102     + paramName + "\' parameter changed from \'" + oldValue +
 
1103     "\' to \'" + newValue + "\'. " + message;
 
1105 return WriteLogString(userLogMsg, login);
 
1107 //-----------------------------------------------------------------------------
 
1108 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
 
1110 string logStr = "Connect, " + inet_ntostring(ip);
 
1111 return WriteLogString(logStr, login);
 
1113 //-----------------------------------------------------------------------------
 
1114 int MYSQL_STORE::WriteUserDisconnect(const string & login,
 
1115                                      const DIR_TRAFF & up,
 
1116                                      const DIR_TRAFF & down,
 
1117                                      const DIR_TRAFF & sessionUp,
 
1118                                      const DIR_TRAFF & sessionDown,
 
1121                                      const std::string & reason) const
 
1123 string logStr = "Disconnect, ";
 
1128 stringstream sscash;
 
1134 sssd << sessionDown;
 
1138 logStr += " session upload: \'";
 
1139 logStr += sssu.str();
 
1140 logStr += "\' session download: \'";
 
1141 logStr += sssd.str();
 
1142 logStr += "\' month upload: \'";
 
1143 logStr += ssmu.str();
 
1144 logStr += "\' month download: \'";
 
1145 logStr += ssmd.str();
 
1146 logStr += "\' cash: \'";
 
1147 logStr += sscash.str();
 
1150 return WriteLogString(logStr, login);
 
1152 //-----------------------------------------------------------------------------
 
1153 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, 
 
1154                                 const string & login) const
 
1158 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,", 
 
1159     login.c_str(), month+1, year+1900);
 
1161 for (int i = 0; i < DIR_NUM; i++)
 
1163     strprintf(¶m, " U%d=%lld,", i, stat.up[i]); 
 
1166     strprintf(¶m, " D%d=%lld,", i, stat.down[i]);        
 
1170 strprintf(¶m, " cash=%f", stat.cash);        
 
1173 if(MysqlSetQuery(res.c_str()))
 
1175     errorStr = "Couldn't SaveMonthStat:\n";
 
1176     //errorStr += mysql_error(sock);
 
1182 //-----------------------------------------------------------------------------*/
 
1183 int MYSQL_STORE::AddAdmin(const string & login) const
 
1185 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
 
1187 if(MysqlSetQuery(qbuf))
 
1189     errorStr = "Couldn't add admin:\n";
 
1190     //errorStr += mysql_error(sock);
 
1196 //-----------------------------------------------------------------------------*/
 
1197 int MYSQL_STORE::DelAdmin(const string & login) const
 
1199 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
 
1201 if(MysqlSetQuery(qbuf))
 
1203     errorStr = "Couldn't delete admin:\n";
 
1204     //errorStr += mysql_error(sock);
 
1210 //-----------------------------------------------------------------------------*/
 
1211 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
 
1213 char passwordE[2 * ADM_PASSWD_LEN + 2];
 
1214 char pass[ADM_PASSWD_LEN + 1];
 
1215 char adminPass[ADM_PASSWD_LEN + 1];
 
1217 memset(pass, 0, sizeof(pass));
 
1218 memset(adminPass, 0, sizeof(adminPass));
 
1221 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
1223 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
 
1224 adminPass[ADM_PASSWD_LEN - 1] = 0;
 
1226 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
 
1228     EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
 
1231 pass[ADM_PASSWD_LEN - 1] = 0;
 
1232 Encode12(passwordE, pass, ADM_PASSWD_LEN);
 
1234 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
 
1235     "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
 
1236     "WHERE login='%s' LIMIT 1", 
 
1248 if(MysqlSetQuery(qbuf))
 
1250     errorStr = "Couldn't save admin:\n";
 
1251     //errorStr += mysql_error(sock);
 
1257 //-----------------------------------------------------------------------------
 
1258 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
 
1260 char pass[ADM_PASSWD_LEN + 1];
 
1261 char password[ADM_PASSWD_LEN + 1];
 
1262 char passwordE[2*ADM_PASSWD_LEN + 2];
 
1269 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
 
1271 if(MysqlGetQuery(qbuf,sock))
 
1273     errorStr = "Couldn't restore admin:\n";
 
1274     errorStr += mysql_error(sock);
 
1279 if (!(res=mysql_store_result(sock)))
 
1281     errorStr = "Couldn't restore admin:\n";
 
1282     errorStr += mysql_error(sock);
 
1287 if ( mysql_num_rows(res) == 0)
 
1289     mysql_free_result(res);
 
1290     errorStr = "Couldn't restore admin as couldn't found him in table.\n";
 
1295 row = mysql_fetch_row(res);
 
1302     mysql_free_result(res);
 
1303     errorStr = "Error in parameter password";
 
1308 memset(passwordE, 0, sizeof(passwordE));
 
1309 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
 
1311 memset(pass, 0, sizeof(pass));
 
1313 if (passwordE[0] != 0)
 
1315     Decode21(pass, passwordE);
 
1316     EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
1318     for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
 
1320         DecodeString(password + 8*i, pass + 8*i, &ctx);
 
1328 ac->password = password;
 
1330 if (GetInt(row[2], &a, 0) == 0) 
 
1331     ac->priv.userConf = a;
 
1334     mysql_free_result(res);
 
1335     errorStr = "Error in parameter ChgConf";
 
1340 if (GetInt(row[3], &a, 0) == 0) 
 
1341     ac->priv.userPasswd = a;
 
1344     mysql_free_result(res);
 
1345     errorStr = "Error in parameter ChgPassword";
 
1350 if (GetInt(row[4], &a, 0) == 0) 
 
1351     ac->priv.userStat = a;
 
1354     mysql_free_result(res);
 
1355     errorStr = "Error in parameter ChgStat";
 
1360 if (GetInt(row[5], &a, 0) == 0) 
 
1361     ac->priv.userCash = a;
 
1364     mysql_free_result(res);
 
1365     errorStr = "Error in parameter ChgCash";
 
1370 if (GetInt(row[6], &a, 0) == 0) 
 
1371     ac->priv.userAddDel = a;
 
1374     mysql_free_result(res);
 
1375     errorStr = "Error in parameter UsrAddDel";
 
1380 if (GetInt(row[7], &a, 0) == 0) 
 
1381     ac->priv.tariffChg = a;
 
1384     mysql_free_result(res);
 
1385     errorStr = "Error in parameter ChgTariff";
 
1390 if (GetInt(row[8], &a, 0) == 0) 
 
1391     ac->priv.adminChg = a;
 
1394     mysql_free_result(res);
 
1395     errorStr = "Error in parameter ChgAdmin";
 
1400 mysql_free_result(res);
 
1404 //-----------------------------------------------------------------------------
 
1405 int MYSQL_STORE::AddTariff(const string & name) const
 
1407 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
 
1409 if(MysqlSetQuery(qbuf))
 
1411     errorStr = "Couldn't add tariff:\n";
 
1412 //    errorStr += mysql_error(sock);
 
1418 //-----------------------------------------------------------------------------
 
1419 int MYSQL_STORE::DelTariff(const string & name) const
 
1421 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
 
1423 if(MysqlSetQuery(qbuf))
 
1425     errorStr = "Couldn't delete tariff: ";
 
1426 //    errorStr += mysql_error(sock);
 
1432 //-----------------------------------------------------------------------------
 
1433 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
 
1438 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
 
1440 if(MysqlGetQuery(qbuf,sock))
 
1442     errorStr = "Couldn't restore Tariff:\n";
 
1443     errorStr += mysql_error(sock);
 
1448 if (!(res=mysql_store_result(sock)))
 
1450     errorStr = "Couldn't restore Tariff:\n";
 
1451     errorStr += mysql_error(sock);
 
1457 td->tariffConf.name = tariffName;
 
1459 row = mysql_fetch_row(res);
 
1462 for (int i = 0; i<DIR_NUM; i++)
 
1464     strprintf(¶m, "Time%d", i);
 
1466     if (str.length() == 0)
 
1468         mysql_free_result(res);
 
1469         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1474     ParseTariffTimeStr(str.c_str(), 
 
1475                        td->dirPrice[i].hDay, 
 
1476                        td->dirPrice[i].mDay, 
 
1477                        td->dirPrice[i].hNight, 
 
1478                        td->dirPrice[i].mNight);
 
1480     strprintf(¶m, "PriceDayA%d", i);
 
1481     if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
 
1483         mysql_free_result(res);
 
1484         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1488     td->dirPrice[i].priceDayA /= (1024*1024);
 
1490     strprintf(¶m, "PriceDayB%d", i);
 
1491     if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
 
1493         mysql_free_result(res);
 
1494         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1498     td->dirPrice[i].priceDayB /= (1024*1024);
 
1500     strprintf(¶m, "PriceNightA%d", i);
 
1501     if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
 
1503         mysql_free_result(res);
 
1504         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1508     td->dirPrice[i].priceNightA /= (1024*1024);
 
1510     strprintf(¶m, "PriceNightB%d", i);
 
1511     if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
 
1513         mysql_free_result(res);
 
1514         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1518     td->dirPrice[i].priceNightB /= (1024*1024);
 
1520     strprintf(¶m, "Threshold%d", i);
 
1521     if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
 
1523         mysql_free_result(res);
 
1524         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1529     strprintf(¶m, "SinglePrice%d", i);
 
1530     if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
 
1532         mysql_free_result(res);
 
1533         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1538     strprintf(¶m, "NoDiscount%d", i);
 
1539     if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
 
1541         mysql_free_result(res);
 
1542         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1548 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
 
1550     mysql_free_result(res);
 
1551     errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
 
1556 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
 
1558     mysql_free_result(res);
 
1559     errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
 
1564 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
 
1566     mysql_free_result(res);
 
1567     errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
 
1572     str = row[4+8*DIR_NUM];
 
1573     param = "TraffType";
 
1575     if (str.length() == 0)
 
1577         mysql_free_result(res);
 
1578         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
 
1583 if (!strcasecmp(str.c_str(), "up"))
 
1584     td->tariffConf.traffType = TRAFF_UP;
 
1586     if (!strcasecmp(str.c_str(), "down"))
 
1587         td->tariffConf.traffType = TRAFF_DOWN;
 
1589         if (!strcasecmp(str.c_str(), "up+down"))
 
1590             td->tariffConf.traffType = TRAFF_UP_DOWN;
 
1592             if (!strcasecmp(str.c_str(), "max"))
 
1593                 td->tariffConf.traffType = TRAFF_MAX;
 
1596                 mysql_free_result(res);
 
1597                 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
 
1602 mysql_free_result(res);
 
1606 //-----------------------------------------------------------------------------
 
1607 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
 
1611 string res="UPDATE tariffs SET";
 
1613 for (int i = 0; i < DIR_NUM; i++)
 
1615     strprintf(¶m, " PriceDayA%d=%f,", i, 
 
1616         td.dirPrice[i].priceDayA * pt_mega);
 
1619     strprintf(¶m, " PriceDayB%d=%f,", i, 
 
1620         td.dirPrice[i].priceDayB * pt_mega);        
 
1623     strprintf(¶m, " PriceNightA%d=%f,", i,
 
1624         td.dirPrice[i].priceNightA * pt_mega);
 
1627     strprintf(¶m, " PriceNightB%d=%f,", i, 
 
1628         td.dirPrice[i].priceNightB * pt_mega);
 
1631     strprintf(¶m, " Threshold%d=%d,", i, 
 
1632         td.dirPrice[i].threshold);
 
1636     strprintf(¶m, " Time%d", i);
 
1638     strprintf(&s, "%0d:%0d-%0d:%0d", 
 
1639             td.dirPrice[i].hDay,
 
1640             td.dirPrice[i].mDay,
 
1641             td.dirPrice[i].hNight,
 
1642             td.dirPrice[i].mNight);
 
1644     res += (param + "='" + s + "',");
 
1646     strprintf(¶m, " NoDiscount%d=%d,", i, 
 
1647         td.dirPrice[i].noDiscount);
 
1650     strprintf(¶m, " SinglePrice%d=%d,", i, 
 
1651         td.dirPrice[i].singlePrice);
 
1655 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
 
1658 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
 
1661 strprintf(¶m, " Free=%f,", td.tariffConf.free);
 
1664 switch (td.tariffConf.traffType)
 
1667         res += " TraffType='up'";
 
1670         res += " TraffType='down'";
 
1673         res += " TraffType='up+down'";
 
1676         res += " TraffType='max'";
 
1679 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
 
1682 if(MysqlSetQuery(res.c_str()))
 
1684     errorStr = "Couldn't save admin:\n";
 
1685     //errorStr += mysql_error(sock);
 
1691 //-----------------------------------------------------------------------------
 
1692 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> * statTree, 
 
1694                                    const string & login) const
 
1696 string res, stTime, endTime, tempStr;
 
1703 if (lt->tm_hour == 0 && lt->tm_min <= 5)
 
1711 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
 
1713 if (!(sock=MysqlConnect())){
 
1718 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
 
1720     errorStr = "Couldn't get table " + tempStr + ":\n";
 
1721     errorStr += mysql_error(sock);
 
1726 unsigned int num_rows =  mysql_num_rows(result);
 
1728 mysql_free_result(result);
 
1732     sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
 
1733         "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
 
1734         "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
 
1735         "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
 
1736     lt->tm_mon+1, lt->tm_year+1900);
 
1738     if(MysqlQuery(qbuf,sock))
 
1740         errorStr = "Couldn't create WriteDetailedStat table:\n";
 
1741         errorStr += mysql_error(sock);
 
1750 lt1 = localtime(&lastStat);
 
1759 lt2 = localtime(&t);
 
1765 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
 
1766 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
 
1768 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
 
1769     "day=%d,startTime='%s',endTime='%s',", 
 
1770     lt->tm_mon+1, lt->tm_year+1900,
 
1778 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
 
1779 stIter = statTree->begin();
 
1781 while (stIter != statTree->end())
 
1783         strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f", 
 
1784                 inet_ntostring(stIter->first.ip).c_str(),
 
1786                 stIter->second.down, 
 
1791         if( (retRes = MysqlQuery((res+tempStr).c_str(),sock)) )
 
1793             errorStr = "Couldn't insert data in WriteDetailedStat:\n";
 
1794             errorStr += mysql_error(sock);
 
1799         result=mysql_store_result(sock);
 
1801             mysql_free_result(result);
 
1808 //-----------------------------------------------------------------------------
 
1809 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
 
1813 gettimeofday(&tv, NULL);
 
1815 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
 
1817 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld", 
 
1819     (long long)msg->header.id
 
1822 if(MysqlSetQuery(qbuf))
 
1824     errorStr = "Couldn't add message:\n";
 
1825     //errorStr += mysql_error(sock);
 
1829 return EditMessage(*msg, login);
 
1831 //-----------------------------------------------------------------------------
 
1832 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
 
1836 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
 
1837     "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
 
1838     "WHERE login='%s' AND id=%lld LIMIT 1", 
 
1840     msg.header.lastSendTime,
 
1841     msg.header.creationTime,
 
1842     msg.header.showTime,
 
1844     msg.header.repeatPeriod,
 
1845     (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
 
1847     (long long)msg.header.id
 
1850 if(MysqlSetQuery(res.c_str()))
 
1852     errorStr = "Couldn't edit message:\n";
 
1853     //errorStr += mysql_error(sock);
 
1859 //-----------------------------------------------------------------------------
 
1860 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
 
1866 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
 
1869 if(MysqlGetQuery(qbuf,sock))
 
1871     errorStr = "Couldn't GetMessage:\n";
 
1872     errorStr += mysql_error(sock);
 
1877 if (!(res=mysql_store_result(sock)))
 
1879     errorStr = "Couldn't GetMessage:\n";
 
1880     errorStr += mysql_error(sock);
 
1885 row = mysql_fetch_row(res);
 
1887 if(row[2]&&str2x(row[2], msg->header.type))
 
1889     mysql_free_result(res);
 
1890     errorStr = "Invalid value in message header for user: " + login;
 
1895 if(row[3] && str2x(row[3], msg->header.lastSendTime))
 
1897     mysql_free_result(res);
 
1898     errorStr = "Invalid value in message header for user: " + login;
 
1903 if(row[4] && str2x(row[4], msg->header.creationTime))
 
1905     mysql_free_result(res);
 
1906     errorStr = "Invalid value in message header for user: " + login;
 
1911 if(row[5] && str2x(row[5], msg->header.showTime))
 
1913     mysql_free_result(res);
 
1914     errorStr = "Invalid value in message header for user: " + login;
 
1919 if(row[6] && str2x(row[6], msg->header.repeat))
 
1921     mysql_free_result(res);
 
1922     errorStr = "Invalid value in message header for user: " + login;
 
1927 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
 
1929     mysql_free_result(res);
 
1930     errorStr = "Invalid value in message header for user: " + login;
 
1935 msg->header.id = id;
 
1938 mysql_free_result(res);
 
1942 //-----------------------------------------------------------------------------
 
1943 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
 
1945 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1", 
 
1946     login.c_str(),(long long)id);
 
1948 if(MysqlSetQuery(qbuf))
 
1950     errorStr = "Couldn't delete Message:\n";
 
1951     //errorStr += mysql_error(sock);
 
1957 //-----------------------------------------------------------------------------
 
1958 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
 
1963 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
 
1965 if(MysqlGetQuery(qbuf,sock))
 
1967     errorStr = "Couldn't GetMessageHdrs:\n";
 
1968     errorStr += mysql_error(sock);
 
1973 if (!(res=mysql_store_result(sock)))
 
1975     errorStr = "Couldn't GetMessageHdrs:\n";
 
1976     errorStr += mysql_error(sock);
 
1981 unsigned int i, num_rows =  mysql_num_rows(res);
 
1982 long long int unsigned id = 0;
 
1984 for (i=0; i<num_rows; i++)
 
1986     row = mysql_fetch_row(res);
 
1987     if (str2x(row[1], id))
 
1992         if(str2x(row[2], hdr.type))
 
1996         if(str2x(row[3], hdr.lastSendTime))
 
2000         if(str2x(row[4], hdr.creationTime))
 
2004         if(str2x(row[5], hdr.showTime))
 
2008         if(str2x(row[6], hdr.repeat))
 
2012         if(str2x(row[7], hdr.repeatPeriod))
 
2016     hdrsList->push_back(hdr);
 
2019 mysql_free_result(res);
 
2023 //-----------------------------------------------------------------------------
 
2025 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
 
2028     int ret=MysqlGetQuery(Query,sock);
 
2032 //-----------------------------------------------------------------------------
 
2033 int  MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
 
2034     if (!(sock=MysqlConnect())) {
 
2037     return   MysqlQuery(Query,sock);
 
2039 //-----------------------------------------------------------------------------
 
2040 MYSQL *  MYSQL_STORE::MysqlConnect() const {
 
2042     if ( !(sock=mysql_init(NULL)) ){
 
2043         errorStr= "mysql init susck\n";
 
2046     if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
 
2047             storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
 
2050             errorStr = "Couldn't connect to mysql engine! With error:\n";
 
2051             errorStr += mysql_error(sock);
 
2055          if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
 
2056              errorStr = "Database lost !\n";
 
2062 //-----------------------------------------------------------------------------