]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/mysql/mysql_store.cpp
Ticket 37. TIMEZONE fixed to TIMESTAMP for the change_policy_timeout in
[stg.git] / projects / stargazer / plugins / store / mysql / mysql_store.cpp
index e52c055d8925d7676f2e9e700fb01e2e8ffdb0b4..8e12faadbd1c19d19e796e41bf822957a7b7f264 100644 (file)
@@ -2,7 +2,6 @@
 #include <cerrno>
 #include <cstdio>
 #include <cstdlib>
-#include <string>
 #include <algorithm>
 
 #include <mysql.h>
 #include "stg/user_stat.h"
 #include "stg/blowfish.h"
 #include "stg/plugin_creator.h"
+#include "stg/logger.h"
 #include "mysql_store.h"
 
 #define adm_enc_passwd "cjeifY8m3"
-char qbuf[4096];
 
-using namespace std;
+namespace
+{
+char qbuf[4096];
 
 const int pt_mega = 1024 * 1024;
-const string badSyms = "'`";
+const std::string badSyms = "'`";
 const char repSym = '\"';
 const int RepitTimes = 3;
 
-int GetInt(const string & str, int * val, int defaultVal)
+template <typename T>
+int GetInt(const std::string & str, T * val, T defaultVal = T())
 {
     char *res;
     
-    *val = strtol(str.c_str(), &res, 10);
+    *val = static_cast<T>(strtoll(str.c_str(), &res, 10));
     
     if (*res != 0) 
     {
@@ -40,7 +42,7 @@ int GetInt(const string & str, int * val, int defaultVal)
     return 0;
 }
 
-int GetDouble(const string & str, double * val, double defaultVal)
+int GetDouble(const std::string & str, double * val, double defaultVal)
 {
     char *res;
     
@@ -55,7 +57,7 @@ int GetDouble(const string & str, double * val, double defaultVal)
     return 0;
 }
 
-int GetTime(const string & str, time_t * val, time_t defaultVal)
+int GetTime(const std::string & str, time_t * val, time_t defaultVal)
 {
     char *res;
     
@@ -71,17 +73,17 @@ int GetTime(const string & str, time_t * val, time_t defaultVal)
 }
 
 //-----------------------------------------------------------------------------
-string ReplaceStr(string source, const string symlist, const char chgsym)
+std::string ReplaceStr(std::string source, const std::string & symlist, const char chgsym)
 {
-    string::size_type pos=0;
+    std::string::size_type pos=0;
 
-    while( (pos = source.find_first_of(symlist,pos)) != string::npos)
+    while( (pos = source.find_first_of(symlist,pos)) != std::string::npos)
         source.replace(pos, 1,1, chgsym);
 
     return source;
 }
 
-int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
+int GetULongLongInt(const std::string & str, uint64_t * val, uint64_t defaultVal)
 {
     char *res;
     
@@ -97,6 +99,9 @@ int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
 } 
 
 PLUGIN_CREATOR<MYSQL_STORE> msc;
+}
+
+extern "C" STORE * GetStore();
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -106,28 +111,23 @@ return msc.GetPlugin();
 }
 //-----------------------------------------------------------------------------
 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
-    : settings(NULL),
-      errorStr(),
-      dbUser(),
-      dbPass(),
-      dbName(),
-      dbHost()
+    : settings(NULL)
 {
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams, 
-                        const string & name, string & result)
+int MYSQL_STORE_SETTINGS::ParseParam(const std::vector<PARAM_VALUE> & moduleParams,
+                                     const std::string & name, std::string & result)
 {
 PARAM_VALUE pv;
 pv.param = name;
-vector<PARAM_VALUE>::const_iterator pvi;
+std::vector<PARAM_VALUE>::const_iterator pvi;
 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
-if (pvi == moduleParams.end())
+if (pvi == moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'" + name + "\' not found.";
     return -1;
     }
-    
+
 result = pvi->value[0];
 
 return 0;
@@ -154,20 +154,19 @@ return 0;
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 MYSQL_STORE::MYSQL_STORE()
-    : errorStr(),
-      version("mysql_store v.0.67"),
-      storeSettings(),
-      settings()
+    : version("mysql_store v.0.67"),
+      schemaVersion(0),
+      logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
 {
-};
+}
 //-----------------------------------------------------------------------------
 int    MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
 {
-    int ret,i;
+    int ret;
 
     if( (ret = mysql_query(sock,sQuery)) )
     {
-        for(i=0; i<RepitTimes; i++)
+        for(int i=0; i<RepitTimes; i++)
         {
             if( (ret = mysql_query(sock,sQuery)) )
                 ;//need to send error result
@@ -185,7 +184,6 @@ int MYSQL_STORE::ParseSettings()
 {
 int ret = storeSettings.ParseSettings(settings);
 MYSQL mysql;
-MYSQL * sock;
 mysql_init(&mysql);
 if (ret)
     errorStr = storeSettings.GetStrError();
@@ -196,7 +194,7 @@ else
         errorStr = "Database password must be not empty. Please read Manual.";
         return -1;
     }
-    
+    MYSQL * sock;
     if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
             storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
             0,0,NULL,0)))
@@ -210,7 +208,7 @@ else
     {
          if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
          {
-             string res = "CREATE DATABASE " + storeSettings.GetDBName();
+             std::string res = "CREATE DATABASE " + storeSettings.GetDBName();
             
             if(MysqlQuery(res.c_str(),sock))
             {
@@ -230,17 +228,23 @@ else
                  }
                  ret = CheckAllTables(sock);
             }
-         }
-         else
-             ret = CheckAllTables(sock);
-         mysql_close(sock);
+        }
+        else
+        {
+            ret = CheckAllTables(sock);
+        }
+        if (!ret)
+        {
+            logger("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
+            MakeUpdates(sock);
+        }
+        mysql_close(sock);
     }
-    
 }
 return ret;
 }
 //-----------------------------------------------------------------------------
-bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
+bool MYSQL_STORE::IsTablePresent(const std::string & str,MYSQL * sock)
 {
 MYSQL_RES* result;
 
@@ -252,16 +256,53 @@ if (!(result=mysql_list_tables(sock,str.c_str() )))
     return -1;
 }
 
-unsigned int num_rows =  mysql_num_rows(result);
+my_ulonglong num_rows =  mysql_num_rows(result);
 
 if(result)
     mysql_free_result(result);
 
-return (num_rows == 1);
+return num_rows == 1;
 }
 //-----------------------------------------------------------------------------
 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
 {
+//info-------------------------------------------------------------------------
+if(!IsTablePresent("info",sock))
+{
+    sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
+
+    if(MysqlQuery(qbuf,sock))
+        {
+        errorStr = "Couldn't create info table With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+
+    sprintf(qbuf,"INSERT INTO info SET version=0");
+
+    if(MysqlQuery(qbuf,sock))
+        {
+        errorStr = "Couldn't write default version. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    schemaVersion = 0;
+}
+else
+{
+    std::vector<std::string> info;
+    if (GetAllParams(&info, "info", "version"))
+        schemaVersion = 0;
+    else
+    {
+        if (info.empty())
+            schemaVersion = 0;
+        else
+            GetInt(info.front(), &schemaVersion, 0);
+    }
+}
 //admins-----------------------------------------------------------------------
 if(!IsTablePresent("admins",sock))
 {
@@ -293,7 +334,7 @@ if(!IsTablePresent("admins",sock))
 }
 
 //tariffs-----------------------------------------------------------------------
-string param, res;
+std::string param, res;
 if(!IsTablePresent("tariffs",sock))
 {
     res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
@@ -325,8 +366,11 @@ if(!IsTablePresent("tariffs",sock))
         res += param;
         }
     
-    res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
-        "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
+    res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
+        "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
+        "period VARCHAR(32) NOT NULL DEFAULT 'month',"
+        "change_policy VARCHAR(32) NOT NULL DEFAULT 'allow',"
+        "change_policy_timeout TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00')";
     
     if(MysqlQuery(res.c_str(),sock))
     {
@@ -380,7 +424,8 @@ if(!IsTablePresent("tariffs",sock))
     
     res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
         "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
-        "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
+        "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month',"\
+        "change_policy='allow', change_policy_timeout='1970-01-01 00:00:00'";
     
     if(MysqlQuery(res.c_str(),sock))
     {
@@ -389,6 +434,17 @@ if(!IsTablePresent("tariffs",sock))
         mysql_close(sock);
         return -1;
     }
+
+    sprintf(qbuf,"UPDATE info SET version=1");
+
+    if(MysqlQuery(qbuf,sock))
+    {
+        errorStr = "Couldn't write default version. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+    }
+    schemaVersion = 2;
 }
 
 //users-----------------------------------------------------------------------
@@ -439,8 +495,13 @@ if(!IsTablePresent("users",sock))
     res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
         "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
         "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
-        "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
-        "Userdata1='',";
+        "Phone='', RealName='',Tariff='tariff',TariffChange='',NAS='',";
+    
+    for (int i = 0; i < USERDATA_NUM; i++)
+        {
+        strprintf(&param, " Userdata%d='',", i);
+        res += param;
+        }
     
     for (int i = 0; i < DIR_NUM; i++)
         {
@@ -520,15 +581,59 @@ if(!IsTablePresent("stat",sock))
 return 0;
 }
 //-----------------------------------------------------------------------------
+int MYSQL_STORE::MakeUpdates(MYSQL * sock)
+{
+if (schemaVersion  < 1)
+    {
+    if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
+        {
+        errorStr = "Couldn't update tariffs table to version 1. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    if (MysqlQuery("UPDATE info SET version = 1", sock))
+        {
+        errorStr = "Couldn't update DB schema version to 1. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    schemaVersion = 1;
+    logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
+    }
+
+if (schemaVersion  < 2)
+    {
+    if (MysqlQuery("ALTER TABLE tariffs ADD change_policy VARCHAR(32) NOT NULL DEFAULT 'allow'", sock) ||
+        MysqlQuery("ALTER TABLE tariffs ADD change_policy_timeout TIMEZONE NOT NULL DEFAULT '1970-01-01 00:00:00'", sock))
+        {
+        errorStr = "Couldn't update tariffs table to version 2. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    if (MysqlQuery("UPDATE info SET version = 2", sock))
+        {
+        errorStr = "Couldn't update DB schema version to 2. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    schemaVersion = 2;
+    logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
+    }
+return 0;
+}
 //-----------------------------------------------------------------------------
 
-int MYSQL_STORE::GetAllParams(vector<string> * ParamList, 
-                            const string & table, const string & name) const
+int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList, 
+                            const std::string & table, const std::string & name) const
 {
 MYSQL_RES *res;
 MYSQL_ROW row;
 MYSQL * sock=NULL;
-unsigned int num,i;
+my_ulonglong num, i;
     
 ParamList->clear();
     
@@ -553,7 +658,7 @@ if (!(res=mysql_store_result(sock)))
 
 num = mysql_num_rows(res);
 
-for(i=0;i<num;i++)
+for(i = 0; i < num; i++)
 {
     row = mysql_fetch_row(res);    
     ParamList->push_back(row[0]);
@@ -566,7 +671,7 @@ return 0;
 }
 
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
+int MYSQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
 {
 if(GetAllParams(usersList, "users", "login"))
     return -1;
@@ -574,7 +679,7 @@ if(GetAllParams(usersList, "users", "login"))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
+int MYSQL_STORE::GetAdminsList(std::vector<std::string> * adminsList) const
 {
 if(GetAllParams(adminsList, "admins", "login"))
     return -1;
@@ -582,7 +687,7 @@ if(GetAllParams(adminsList, "admins", "login"))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
+int MYSQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
 {
 if(GetAllParams(tariffsList, "tariffs", "name"))
     return -1;
@@ -590,11 +695,14 @@ if(GetAllParams(tariffsList, "tariffs", "name"))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::AddUser(const string & login) const
+int MYSQL_STORE::AddUser(const std::string & login) const
 {
-sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
-    
-if(MysqlSetQuery(qbuf))
+std::string query = "INSERT INTO users SET login='" + login + "',Note='',NAS=''";
+
+for (int i = 0; i < USERDATA_NUM; i++)
+    query += ",Userdata" + x2str(i) + "=''";
+
+if(MysqlSetQuery(query.c_str()))
 {
     errorStr = "Couldn't add user:\n";
     //errorStr += mysql_error(sock);
@@ -604,7 +712,7 @@ if(MysqlSetQuery(qbuf))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::DelUser(const string & login) const
+int MYSQL_STORE::DelUser(const std::string & login) const
 {
 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
     
@@ -618,12 +726,12 @@ if(MysqlSetQuery(qbuf))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
+int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
 {
 MYSQL_RES *res;
 MYSQL_ROW row;
 MYSQL * sock;
-string query;
+std::string query;
 
 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
          AlwaysOnline, Tariff, Address, Phone, Email, Note, \
@@ -665,8 +773,6 @@ if (mysql_num_rows(res) != 1)
 
 row = mysql_fetch_row(res);
 
-string param;
-
 conf->password = row[1];
 
 if (conf->password.empty())
@@ -677,7 +783,7 @@ if (conf->password.empty())
     return -1;
     }
 
-if (GetInt(row[2],&conf->passive, 0) != 0)
+if (GetInt(row[2],&conf->passive) != 0)
     {
     mysql_free_result(res);
     errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
@@ -685,7 +791,7 @@ if (GetInt(row[2],&conf->passive, 0) != 0)
     return -1;
     }
 
-if (GetInt(row[3], &conf->disabled, 0) != 0)
+if (GetInt(row[3], &conf->disabled) != 0)
     {
     mysql_free_result(res);
     errorStr = "User \'" + login + "\' data not read. Parameter Down.";
@@ -693,7 +799,7 @@ if (GetInt(row[3], &conf->disabled, 0) != 0)
     return -1;
     }
 
-if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
+if (GetInt(row[4], &conf->disabledDetailStat) != 0)
     {
     mysql_free_result(res);
     errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
@@ -701,7 +807,7 @@ if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
     return -1;
     }
 
-if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
+if (GetInt(row[5], &conf->alwaysOnline) != 0)
     {
     mysql_free_result(res);
     errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
@@ -743,13 +849,13 @@ for (int i = 0; i < USERDATA_NUM; i++)
 
 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
     
-string ipStr = row[16+USERDATA_NUM];
+std::string ipStr = row[16+USERDATA_NUM];
 USER_IPS i;
 try
     {
     i = StrToIPS(ipStr);
     }
-catch (const string & s)
+catch (const std::string & s)
     {
     mysql_free_result(res);
     errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
@@ -764,13 +870,13 @@ mysql_close(sock);
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
+int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
 {
 MYSQL_RES *res;
 MYSQL_ROW row;
 MYSQL * sock;
 
-string query;
+std::string query;
 
 query = "SELECT ";
 
@@ -815,21 +921,21 @@ for (int i = 0; i < DIR_NUM; i++)
     if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
         {
         mysql_free_result(res);
-        errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
+        errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
         mysql_close(sock);
         return -1;
         }
-    stat->down[i] = traff;
+    stat->monthDown[i] = traff;
 
     sprintf(s, "U%d", i);
     if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
         {
         mysql_free_result(res);
-        errorStr =   "User \'" + login + "\' stat not read. Parameter " + string(s);
+        errorStr =   "User \'" + login + "\' stat not read. Parameter " + std::string(s);
         mysql_close(sock);
         return -1;
         }
-    stat->up[i] = traff;
+    stat->monthUp[i] = traff;
     }//for
 
 startPos += (2*DIR_NUM);
@@ -887,10 +993,10 @@ mysql_close(sock);
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
+int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
 {
-string param;
-string res;
+std::string param;
+std::string res;
 
 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
     "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
@@ -921,7 +1027,7 @@ for (int i = 0; i < USERDATA_NUM; i++)
 strprintf(&param, " CreditExpire=%d,", conf.creditExpire);
 res += param;
 
-stringstream ipStr;
+std::ostringstream ipStr;
 ipStr << conf.ips;
 
 strprintf(&param, " IP='%s'", ipStr.str().c_str());
@@ -940,19 +1046,19 @@ if(MysqlSetQuery(res.c_str()))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
+int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
 {
-string param;
-string res;
+std::string param;
+std::string res;
 
 res = "UPDATE users SET";
 
 for (int i = 0; i < DIR_NUM; i++)
     {
-    strprintf(&param, " D%d=%lld,", i, stat.down[i]);
+    strprintf(&param, " D%d=%lld,", i, stat.monthDown[i]);
     res += param;
 
-    strprintf(&param, " U%d=%lld,", i, stat.up[i]);
+    strprintf(&param, " U%d=%lld,", i, stat.monthUp[i]);
     res += param;
     }
 
@@ -980,9 +1086,9 @@ if(MysqlSetQuery(res.c_str()))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
+int MYSQL_STORE::WriteLogString(const std::string & str, const std::string & login) const
 {
-string res, tempStr;
+std::string res, tempStr;
 time_t t;
 tm * lt;
 
@@ -1004,7 +1110,7 @@ if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
     return -1;
 }
 
-unsigned int num_rows =  mysql_num_rows(result);
+my_ulonglong num_rows =  mysql_num_rows(result);
 
 mysql_free_result(result);
 
@@ -1024,7 +1130,7 @@ if (num_rows < 1)
 
 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
 
-string send;
+std::string send;
 
 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
         lt->tm_mon+1, lt->tm_year+1900,
@@ -1042,42 +1148,42 @@ return 0;
 
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::WriteUserChgLog(const string & login,
-                                 const string & admLogin,
+int MYSQL_STORE::WriteUserChgLog(const std::string & login,
+                                 const std::string & admLogin,
                                  uint32_t       admIP,
-                                 const string & paramName,
-                                 const string & oldValue,
-                                 const string & newValue,
-                                 const string & message) const
+                                 const std::string & paramName,
+                                 const std::string & oldValue,
+                                 const std::string & newValue,
+                                 const std::string & message) const
 {
-string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
+std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
     + paramName + "\' parameter changed from \'" + oldValue +
     "\' to \'" + newValue + "\'. " + message;
 
 return WriteLogString(userLogMsg, login);
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
+int MYSQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
 {
-string logStr = "Connect, " + inet_ntostring(ip);
+std::string logStr = "Connect, " + inet_ntostring(ip);
 return WriteLogString(logStr, login);
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::WriteUserDisconnect(const string & login,
+int MYSQL_STORE::WriteUserDisconnect(const std::string & login,
                                      const DIR_TRAFF & up,
                                      const DIR_TRAFF & down,
                                      const DIR_TRAFF & sessionUp,
                                      const DIR_TRAFF & sessionDown,
                                      double cash,
-                                     double freeMb,
-                                     const std::string & reason) const
+                                     double /*freeMb*/,
+                                     const std::string & /*reason*/) const
 {
-string logStr = "Disconnect, ";
-stringstream sssu;
-stringstream sssd;
-stringstream ssmu;
-stringstream ssmd;
-stringstream sscash;
+std::string logStr = "Disconnect, ";
+std::ostringstream sssu;
+std::ostringstream sssd;
+std::ostringstream ssmu;
+std::ostringstream ssmd;
+std::ostringstream sscash;
 
 ssmu << up;
 ssmd << down;
@@ -1103,19 +1209,19 @@ return WriteLogString(logStr, login);
 }
 //-----------------------------------------------------------------------------
 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, 
-                                const string & login) const
+                                const std::string & login) const
 {
-string param, res;
+std::string param, res;
 
 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,", 
     login.c_str(), month+1, year+1900);
     
 for (int i = 0; i < DIR_NUM; i++)
     {
-    strprintf(&param, " U%d=%lld,", i, stat.up[i]); 
+    strprintf(&param, " U%d=%lld,", i, stat.monthUp[i]); 
     res += param;
 
-    strprintf(&param, " D%d=%lld,", i, stat.down[i]);        
+    strprintf(&param, " D%d=%lld,", i, stat.monthDown[i]);        
     res += param;
     }
     
@@ -1132,7 +1238,7 @@ if(MysqlSetQuery(res.c_str()))
 return 0;
 }
 //-----------------------------------------------------------------------------*/
-int MYSQL_STORE::AddAdmin(const string & login) const
+int MYSQL_STORE::AddAdmin(const std::string & login) const
 {
 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
     
@@ -1146,7 +1252,7 @@ if(MysqlSetQuery(qbuf))
 return 0;
 }
 //-----------------------------------------------------------------------------*/
-int MYSQL_STORE::DelAdmin(const string & login) const
+int MYSQL_STORE::DelAdmin(const std::string & login) const
 {
 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
     
@@ -1170,14 +1276,14 @@ memset(pass, 0, sizeof(pass));
 memset(adminPass, 0, sizeof(adminPass));
 
 BLOWFISH_CTX ctx;
-EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
+InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
 adminPass[ADM_PASSWD_LEN - 1] = 0;
 
 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
     {
-    EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
+    EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx);
     }
 
 pass[ADM_PASSWD_LEN - 1] = 0;
@@ -1207,18 +1313,16 @@ if(MysqlSetQuery(qbuf))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
+int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
 {
 char pass[ADM_PASSWD_LEN + 1];
 char password[ADM_PASSWD_LEN + 1];
 char passwordE[2*ADM_PASSWD_LEN + 2];
 BLOWFISH_CTX ctx;
 
-memset(pass, 0, sizeof(pass));
 memset(password, 0, sizeof(password));
-memset(passwordE, 0, sizeof(passwordE));
 
-string p;
+std::string p;
 MYSQL_RES *res;
 MYSQL_ROW row;
 MYSQL * sock;
@@ -1251,7 +1355,6 @@ if ( mysql_num_rows(res) == 0)
 row = mysql_fetch_row(res);
 
 p = row[1];
-int a;
 
 if(p.length() == 0)
 {
@@ -1269,11 +1372,11 @@ memset(pass, 0, sizeof(pass));
 if (passwordE[0] != 0)
     {
     Decode21(pass, passwordE);
-    EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
+    InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
 
     for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
         {
-        DecodeString(password + 8*i, pass + 8*i, &ctx);
+        DecryptBlock(password + 8*i, pass + 8*i, &ctx);
         }
     }
 else
@@ -1283,7 +1386,9 @@ else
 
 ac->password = password;
 
-if (GetInt(row[2], &a, 0) == 0) 
+uint16_t a;
+
+if (GetInt(row[2], &a) == 0) 
     ac->priv.userConf = a;
 else
     {
@@ -1293,7 +1398,7 @@ else
     return -1;
     }
 
-if (GetInt(row[3], &a, 0) == 0) 
+if (GetInt(row[3], &a) == 0) 
     ac->priv.userPasswd = a;
 else
     {
@@ -1303,7 +1408,7 @@ else
     return -1;
     }
 
-if (GetInt(row[4], &a, 0) == 0) 
+if (GetInt(row[4], &a) == 0) 
     ac->priv.userStat = a;
 else
     {
@@ -1313,7 +1418,7 @@ else
     return -1;
     }
 
-if (GetInt(row[5], &a, 0) == 0) 
+if (GetInt(row[5], &a) == 0) 
     ac->priv.userCash = a;
 else
     {
@@ -1323,7 +1428,7 @@ else
     return -1;
     }
 
-if (GetInt(row[6], &a, 0) == 0) 
+if (GetInt(row[6], &a) == 0) 
     ac->priv.userAddDel = a;
 else
     {
@@ -1333,7 +1438,7 @@ else
     return -1;
     }
 
-if (GetInt(row[7], &a, 0) == 0) 
+if (GetInt(row[7], &a) == 0) 
     ac->priv.tariffChg = a;
 else
     {
@@ -1343,7 +1448,7 @@ else
     return -1;
     }
 
-if (GetInt(row[8], &a, 0) == 0) 
+if (GetInt(row[8], &a) == 0) 
     ac->priv.adminChg = a;
 else
     {
@@ -1358,7 +1463,7 @@ mysql_close(sock);
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::AddTariff(const string & name) const
+int MYSQL_STORE::AddTariff(const std::string & name) const
 {
 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
     
@@ -1372,7 +1477,7 @@ if(MysqlSetQuery(qbuf))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::DelTariff(const string & name) const
+int MYSQL_STORE::DelTariff(const std::string & name) const
 {
 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
     
@@ -1386,7 +1491,7 @@ if(MysqlSetQuery(qbuf))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
+int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
 {
 MYSQL_RES *res;
 MYSQL_ROW row;
@@ -1409,12 +1514,12 @@ if (!(res=mysql_store_result(sock)))
     return -1;
 }
 
-string str;
+std::string str;
 td->tariffConf.name = tariffName;
 
 row = mysql_fetch_row(res);
 
-string param;
+std::string param;
 for (int i = 0; i<DIR_NUM; i++)
     {
     strprintf(&param, "Time%d", i);
@@ -1474,7 +1579,7 @@ for (int i = 0; i<DIR_NUM; i++)
     td->dirPrice[i].priceNightB /= (1024*1024);
 
     strprintf(&param, "Threshold%d", i);
-    if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
+    if (GetInt(row[5+i*8], &td->dirPrice[i].threshold) < 0)
         {
         mysql_free_result(res);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
@@ -1483,7 +1588,7 @@ for (int i = 0; i<DIR_NUM; i++)
         }
 
     strprintf(&param, "SinglePrice%d", i);
-    if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
+    if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice) < 0)
         {
         mysql_free_result(res);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
@@ -1492,7 +1597,7 @@ for (int i = 0; i<DIR_NUM; i++)
         }
 
     strprintf(&param, "NoDiscount%d", i);
-    if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
+    if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount) < 0)
         {
         mysql_free_result(res);
         errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
@@ -1536,35 +1641,58 @@ if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
         return -1;
         }
 
-if (!strcasecmp(str.c_str(), "up"))
-    td->tariffConf.traffType = TRAFF_UP;
+td->tariffConf.traffType = TARIFF::StringToTraffType(str);
+
+if (schemaVersion > 0)
+{
+    str = row[5+8*DIR_NUM];
+    param = "Period";
+
+    if (str.length() == 0)
+        {
+        mysql_free_result(res);
+        errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
+        mysql_close(sock);
+        return -1;
+        }
+
+    td->tariffConf.period = TARIFF::StringToPeriod(str);
+    }
 else
-    if (!strcasecmp(str.c_str(), "down"))
-        td->tariffConf.traffType = TRAFF_DOWN;
-    else
-        if (!strcasecmp(str.c_str(), "up+down"))
-            td->tariffConf.traffType = TRAFF_UP_DOWN;
-        else
-            if (!strcasecmp(str.c_str(), "max"))
-                td->tariffConf.traffType = TRAFF_MAX;
-            else
-                {
-                mysql_free_result(res);
-                errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
-                mysql_close(sock);
-                return -1;
-                }
+    {
+    td->tariffConf.period = TARIFF::MONTH;
+    }
+
+if (schemaVersion > 1)
+{
+    str = row[6+8*DIR_NUM];
+    param = "ChangePolicy";
+
+    if (str.length() == 0)
+        {
+        mysql_free_result(res);
+        errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
+        mysql_close(sock);
+        return -1;
+        }
+
+    td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
+    }
+else
+    {
+    td->tariffConf.changePolicy = TARIFF::ALLOW;
+    }
 
 mysql_free_result(res);
 mysql_close(sock);
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
+int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
 {
-string param;
+std::string param;
 
-string res="UPDATE tariffs SET";
+std::string res="UPDATE tariffs SET";
 
 for (int i = 0; i < DIR_NUM; i++)
     {
@@ -1588,7 +1716,7 @@ for (int i = 0; i < DIR_NUM; i++)
         td.dirPrice[i].threshold);
     res += param;
 
-    string s;
+    std::string s;
     strprintf(&param, " Time%d", i);
 
     strprintf(&s, "%0d:%0d-%0d:%0d", 
@@ -1617,27 +1745,20 @@ res += param;
 strprintf(&param, " Free=%f,", td.tariffConf.free);
 res += param;
 
-switch (td.tariffConf.traffType)
-    {
-    case TRAFF_UP:
-        res += " TraffType='up'";
-        break;
-    case TRAFF_DOWN:
-        res += " TraffType='down'";
-        break;
-    case TRAFF_UP_DOWN:
-        res += " TraffType='up+down'";
-        break;
-    case TRAFF_MAX:
-        res += " TraffType='max'";
-        break;
-    }
+res += " TraffType='" + TARIFF::TraffTypeToString(td.tariffConf.traffType) + "'";
+
+if (schemaVersion > 0)
+    res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
+
+if (schemaVersion > 1)
+    res += ", change_policy='" + TARIFF::ChangePolicyToString(td.tariffConf.changePolicy) + "'";
+
 strprintf(&param, " WHERE name='%s' LIMIT 1", tariffName.c_str());
 res += param;
 
 if(MysqlSetQuery(res.c_str()))
 {
-    errorStr = "Couldn't save admin:\n";
+    errorStr = "Couldn't save tariff:\n";
     //errorStr += mysql_error(sock);
     return -1;
 }
@@ -1645,11 +1766,11 @@ if(MysqlSetQuery(res.c_str()))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree, 
+int MYSQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree, 
                                    time_t lastStat, 
-                                   const string & login) const
+                                   const std::string & login) const
 {
-string res, stTime, endTime, tempStr;
+std::string res, stTime, endTime, tempStr;
 time_t t;
 tm * lt;
 
@@ -1679,7 +1800,7 @@ if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
     return -1;
 }
 
-unsigned int num_rows =  mysql_num_rows(result);
+my_ulonglong num_rows =  mysql_num_rows(result);
 
 mysql_free_result(result);
 
@@ -1730,7 +1851,7 @@ strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
     endTime.c_str()
     );
 
-map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
+std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
 stIter = statTree.begin();
 
 while (stIter != statTree.end())
@@ -1761,17 +1882,17 @@ mysql_close(sock);
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
+int MYSQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
 {
 struct timeval tv;
 
 gettimeofday(&tv, NULL);
 
-msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
+msg->header.id = static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
 
 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld", 
     login.c_str(),
-    (long long)msg->header.id
+    static_cast<long long>(msg->header.id)
     );
     
 if(MysqlSetQuery(qbuf))
@@ -1784,9 +1905,9 @@ if(MysqlSetQuery(qbuf))
 return EditMessage(*msg, login);
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
+int MYSQL_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
 {
-string res;
+std::string res;
 
 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
     "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
@@ -1799,7 +1920,7 @@ strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "
     msg.header.repeatPeriod,
     (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
     login.c_str(),
-    (long long)msg.header.id
+    msg.header.id
     );
 
 if(MysqlSetQuery(res.c_str()))
@@ -1812,7 +1933,7 @@ if(MysqlSetQuery(res.c_str()))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
+int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
 {
 MYSQL_RES *res;
 MYSQL_ROW row;
@@ -1895,10 +2016,10 @@ mysql_close(sock);
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
+int MYSQL_STORE::DelMessage(uint64_t id, const std::string & login) const
 {
 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1", 
-    login.c_str(),(long long)id);
+        login.c_str(), static_cast<long long>(id));
     
 if(MysqlSetQuery(qbuf))
 {
@@ -1910,7 +2031,7 @@ if(MysqlSetQuery(qbuf))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
+int MYSQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
 {
 MYSQL_RES *res;
 MYSQL_ROW row;
@@ -1933,10 +2054,11 @@ if (!(res=mysql_store_result(sock)))
     return -1;
 }
 
-unsigned int i, num_rows =  mysql_num_rows(res);
-long long int unsigned id = 0;
+unsigned int i;
+my_ulonglong num_rows = mysql_num_rows(res);
+uint64_t id = 0;
 
-for (i=0; i<num_rows; i++)
+for (i = 0; i < num_rows; i++)
 {
     row = mysql_fetch_row(res);
     if (str2x(row[1], id))