<?php

require_once('MDB2.php');

class DbMod{
	var $ArrMsSp = '';
  var $VarDbDsnName = '';
  var $VarDbDefaultCharSet = '';
  var $VarDbCollate = '';
  var $VarDbEngine = '';

  var $VarTableConfig = '';
  var $VarTableCountry = '';
  var $VarTableCartridge = '';
  var $VarTableCartridgeType = '';
  var $VarTableLang = '';
  var $VarTableMenu = '';
	var $VarTableMenuName = '';
  var $VarTableMessage = '';
  var $VarTableNews = '';
  var $VarTableProgrammPref = '';
  var $VarTableRole = '';
	var $VarTableTemplate = '';
  var $VarTableWebServices = '';
  
  var $VarTableUsers = '';
  var $VarTableUsersPref = '';
  var $VarTableUsersRole = '';
  var $VarTableUsersServices = '';
	  
  var $VarTableDemands = '';
  var $VarTableDemandsClassif = '';
  var $VarTableDemandsComments = '';
  var $VarTableIp = '';
  var $VarTableOrganiz = '';
  var $VarTablePass = '';
  var $VarTablePo = '';
  var $VarTablePoLicence = '';
  var $VarTableProgrammPrefValue = '';
  var $VarTablePurchase = '';
  var $VarTablePurchaseStatus = '';
  var $VarTableRepair = '';
  var $VarTableRepairStatus = '';
  var $VarTableSprStatus = '';
  var $VarTableSvt = '';
  var $VarTableSvtType = '';
  var $VarTableSvtPo = '';
  var $VarTableSvtStatus = '';
  var $VarTableSvtStatusVal = '';
  var $VarTableSvtGiveout = '';
  var $VarTableSvtGiveoutStatus = '';
  var $VarTableSvtMove = '';
  var $VarTableStructureOrganization = '';
  var $VarTableSystem = '';
  var $VarTableUsersSettingsValue = '';
  var $VarTableUsersPrefValue = '';


	function DbMod($ArrMsSp, $VarDbDsnName = 'dbuser'){
		$this->ArrMsSp = $ArrMsSp;
    $this->VarDbDsnName = $VarDbDsnName;
    $this->VarDbDefaultCharSet = $this->ArrMsSp->Sp_DbConf[$this->VarDbDsnName]['DbDefaultCharSet'];
    $this->VarDbCollate = $this->ArrMsSp->Sp_DbConf[$this->VarDbDsnName]['DbCollate'];
    $this->VarDbEngine = $this->ArrMsSp->Sp_DbConf[$this->VarDbDsnName]['DbEngine'];
    $this->SP_DbDsnInfo['dbconfig'] = MDB2::parseDSN($this->ArrMsSp->Sp_DbConf['dbconfig']['DbDsn']);
    $this->SP_DbDsnInfo['dbuser'] = MDB2::parseDSN($this->ArrMsSp->Sp_DbConf['dbuser']['DbDsn']);

    $this->VarTableLang = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_Lang'];
    $this->VarTableMenu = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_Menu'];
    $this->VarTableMenuName = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_MenuName'];
    $this->VarTableNews = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_News'];
    $this->VarTableProgrammPref = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_ProgrammPref'];
    $this->VarTableRole = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_Role'];
    $this->VarTableTemplate = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_Template'];    
    $this->VarTableUsersPref = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_UsersPref'];
    $this->VarTableWebServices = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_WebServices'];
    $this->VarTableSystem = $this->SP_DbDsnInfo['dbconfig']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbconfig']['DbTbl_System'];
    

    $this->VarTableCartridge = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Cartridge'];
    $this->VarTableCartridgeType = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_CartridgeType'];
    $this->VarTableConfig = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Config'];    
    $this->VarTableCountry = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Country'];
    $this->VarTableMessage = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Message'];
    $this->VarTableDemands = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Demands'];
    $this->VarTableDemandsClassif = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_DemandsClassif'];
    $this->VarTableDemandsComments = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_DemandsComments'];
    $this->VarTableIp = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Ip'];
    $this->VarTableOrganiz = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Organiz'];
    $this->VarTablePass = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Pass'];
    $this->VarTablePo = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Po'];
    $this->VarTablePoLicence = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_PoLicence'];
    $this->VarTableProgrammPrefValue = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_ProgrammPrefValue'];
    $this->VarTablePurchase = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Purchase'];
    $this->VarTablePurchaseStatus = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_PurchaseStatus'];
    $this->VarTableRepair = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Repair'];
    $this->VarTableRepairStatus = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_RepairStatus'];
    $this->VarTableSprStatus = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SprStatus'];
    $this->VarTableSvt = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Svt'];
    $this->VarTableSvtType = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SvtType'];
    $this->VarTableSvtStatus = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SvtStatus'];
    $this->VarTableSvtStatusVal = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SvtStatusVal'];
    $this->VarTableSvtGiveout = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SvtGiveout'];
    $this->VarTableSvtGiveoutStatus = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SvtGiveoutStatus'];
    $this->VarTableSvtPo = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SvtPo'];
    $this->VarTableSvtMove = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_SvtMove'];
    $this->VarTableStructureOrganization = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_StructureOrganization'];
    $this->VarTableUsers = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_Users'];
    $this->VarTableUsersServices = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_UsersServices'];
    $this->VarTableUsersPrefValue = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_UsersPrefValue'];
    $this->VarTableUsersRole = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_UsersRole'];
    $this->VarTableUsersSettingsValue = $this->SP_DbDsnInfo['dbuser']['database'] .'.'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['dbuser']['DbTbl_UsersSettingsValue'];

    
		return true;
	}
  
  function DbConnect($VarDbConf = '', $VarDbDefaultCharSet = 'utf8'){
    $mdb2 = MDB2::connect($VarDbConf);
    if (PEAR::isError($mdb2)) {
      die($mdb2->getMessage());
    }
    DbMod::DbSetCharset($mdb2, $VarDbDefaultCharSet);
    //SiteLibs::GetVarDump($mdb2);
    $mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
    $mdb2->loadModule('Extended');
    return $mdb2;
  }
  
  function DbDisconnect($mdb2){
    return $mdb2->disconnect();
  }
  
  function DbSetCharset($mdb2, $codepage = ''){
    if(true === is_object($mdb2)){
      $mdb2->query("SET character_set_client = '". $codepage ."'");
      $mdb2->query("SET character_set_connection = '". $codepage ."'");
      $mdb2->query("SET character_set_results = '". $codepage ."'");
      $mdb2->query("SET NAMES '". $codepage ."'");
      return true;
    }
    else{
      return false;
    }
  }
  
  function DbGet($ArrSql = array(), $VarType = 'GetAll'){
    if(true === is_array($ArrSql) && 1 <= count($ArrSql)){
      $VarSql = SiteLibs::BuildSqlFromArray($ArrSql);
//      echo $VarSql."<br />";
      switch ($VarType) {
        case 'GetAssoc':
          $ArrResult = $this->ArrMsSp->SP_DbConnect->extended->getAssoc($VarSql);
        break;
        case 'GetOne':
          $ArrResult = $this->ArrMsSp->SP_DbConnect->extended->getOne($VarSql);
        break;
        case 'GetCol':
          $ArrResult = $this->ArrMsSp->SP_DbConnect->extended->getCol($VarSql);
        break;
        case 'GetRow':
          $ArrResult = $this->ArrMsSp->SP_DbConnect->extended->getRow($VarSql);
        break;
        case 'GetAll':
        default:
          $ArrResult = $this->ArrMsSp->SP_DbConnect->extended->getAll($VarSql);
        break;
      }
      //SiteLibs::GetVarDump($ArrSqlArray);
      if (PEAR::isError($ArrResult)) {
        die($ArrResult->getMessage());
      }
      return $ArrResult;
    }
    else{
      return false;
    }
  }
  
	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    $mdb2 =& self::DbConnect($this->ArrMsSp->Sp_DbConf[$this->VarDbDsnName]['DbDsn']);
//    $mdb2 =& MDB2::connect($this->ArrMsSp->Sp_DbConf[$this->VarDbDsnName]['DbDsn']);
//    if (PEAR::isError($mdb2)) {
//      die($mdb2->getMessage());
//    }
    //SiteLibs::DbSetCharset($mdb2, 'cp1251');
    //SiteLibs::GetVarDump($mdb2);
    //$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
    //$mdb2->loadModule('Extended');
    ///
    $ArrSqlArray['SqlAllTables']['select'] = 'a.*';
    $ArrSqlArray['SqlAllTables']['from'] = $VarWhere .' AS a';
    ///
    $ArrSqlArray['SqlShowTables']['query'] = 'SHOW TABLES';
    ///
    $ArrSqlArray['SqlColumnsTables']['query'] = 'SHOW COLUMNS FROM '. $VarWhere;
    $ArrSqlArray['SqlQuery']['query'] = $VarWhere;
    ///
    $sql = SiteLibs::BuildSqlFromArray($ArrSqlArray[$VarSqlName]);
//    if ('SqlShowTables' == $VarSqlName){
//      echo "sql: ". $sql. "<br />";
//    }
    switch ($VarSqlGet) {
      case 'GetAssoc':
        $ArrResult =& $mdb2->extended->getAssoc($sql);
      break;
      case 'GetOne':
        $ArrResult =& $mdb2->extended->getOne($sql);
      break;
      case 'GetCol':
        $ArrResult =& $mdb2->extended->getCol($sql);
      break;
      case 'GetRow':
        $ArrResult =& $mdb2->extended->getRow($sql);
      break;
      case 'Query':
        $mdb2->setOption('escape', true);
        $ArrResult =& $mdb2->query($sql);
        //SiteLibs::GetVarDump($ArrResult);

      break;
      case 'GetAll':
      default:
        $ArrResult =& $mdb2->extended->getAll($sql);
      break;
    }
    //SiteLibs::GetVarDump($ArrResult);
    if (PEAR::isError($ArrResult)) {
      die($ArrResult->getMessage());
    }
    //$mdb2->disconnect();
    self::DbDisconnect($mdb2);
  	return $ArrResult;
	}

  function Backup($VarWhere = ''){
    $this->VarDbDsnName = $VarWhere;
    $ArrDsnInfo = MDB2::parseDSN($this->ArrMsSp->Sp_DbConf["$VarWhere"]['DbDsn']);
    $ArrTables = self::GetFromDb('SqlShowTables', 'GetAll', $ArrDsnInfo['database']);
    $VarSql = '';
    if(true === is_array($ArrTables) && 0 < count($ArrTables)){
      //$i = 0;
      foreach ($ArrTables as $key => $value) {
        $VarKeyName = key($value);
        $ArrColumns = self::GetFromDb('SqlColumnsTables', 'GetAll', $value[$VarKeyName]);
        //SiteLibs::GetVarDump($ArrColumns);
        $ArrData = self::GetFromDb('SqlAllTables', 'GetAll', $value[$VarKeyName]);
        $VarSql .= $this->CreateExport($ArrDsnInfo['database'].'.'.$value[$VarKeyName], $ArrColumns, $ArrData);
        //$i++;
      }
    }
    return $VarSql;
  }

  function CreateExport($VarTableName = '', $ArrColumns = array(), $ArrData = array()){
    if('' != $VarTableName && true === is_array($ArrColumns) && 0 < count($ArrColumns)){
      $VarReturn = "DROP TABLE IF EXISTS ". $VarTableName .";\r\n---*-*-\r\n";
      $VarReturn .= 'CREATE TABLE IF NOT EXISTS '. $VarTableName .' (';
      foreach ($ArrColumns as $key => $value) {
        $VarK[] = '`'. $value['field'] .'`';
        $VarStr = "\r\n";
        $VarStr .= '`'. $value['field'] .'`';
        $VarStr .= ' '. $value['type'];
        if ('varchar' == substr($value['type'], 0, 7) || 'text' == substr($value['type'], 0, 4)){
          $VarStr .= ' COLLATE '. $this->VarDbCollate;
        }
        if('NO' == $value['null']){
          $VarStr .= ' NOT NULL';
        }
        if('YES' == $value['null']){
          $VarStr .= ' DEFAULT NULL';
        }
        if('PRI' == $value['key']){
          $VarPrimaryKey = ' PRIMARY KEY (`'. $value['field'] .'`)';
        }
        if('' != $value['extra']){
          $VarStr .= ' AUTO_INCREMENT';
        }
        $ArrTableColumns[] = $VarStr;
      }
      $VarReturn .= implode(',', $ArrTableColumns);
      if (true === isset($VarPrimaryKey) && '' != $VarPrimaryKey){
        $VarReturn .= ",\r\n". $VarPrimaryKey;
      }
      $VarReturn .= ') ENGINE='. $this->VarDbEngine .' DEFAULT CHARSET='. $this->VarDbDefaultCharSet .' COLLATE='. $this->VarDbCollate .';';
      $VarReturn .= "\r\n---*-*-\r\n";
      $i=0;
      if (true === is_array($ArrData) && 0 < count($ArrData)){
        foreach ($ArrData as $k => $v) {
          $VarV = array();
          $VarKey = floor($i/10);
          foreach ($ArrColumns as $key => $value){
            $value['field'] = strtolower($value['field']);
            if ('int' == substr($value['type'], 0, 3) || 'tinyint' == substr($value['type'], 0, 7)){
              if (true === is_numeric($v[$value['field']])){
                $VarV[] = $v[$value['field']];
              }
              else{
                $VarV[] = 'NULL';
              }
            }
            elseif ('datetime' == substr($value['type'], 0, 8)) {
              if('YES' == $value['null'] && '' == $v[$value['field']]){
                $VarV[] = 'NULL';
              }
              else{
                $VarV[] = "'". $v[$value['field']] ."'";
              }
            }
            else{
              //$VarV[] = "'". mysql_escape_string($v[$value['field']]) ."'";
              $VarV[] = "'". $v[$value['field']] ."'";
            }
          }
          $ArrV[$VarKey][] = "(". implode(",", $VarV) .")";
          $i++;
        }
        foreach ($ArrV as $value) {
          $VarReturn .= "INSERT INTO ". $VarTableName ." (". implode(',', $VarK) .") VALUES \r\n". implode(",\r\n", $value) .";\r\n---*-*-\r\n";
        } 
      }
      return $VarReturn;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
    return $VarReturn;
  }

  function Restore($values = array()){
    $VarFileFrom = $values['filerestore']['tmp_name'];
    $VarFileStripExt = SiteLibs::GetFileStripExt($values['filerestore']['name']);
    $VarFileNameZip = $VarFileStripExt .'.zip';
    $VarFileNameSql = $VarFileStripExt .'.sql';
    $VarFileNameZipPath = $_SERVER['DOCUMENT_ROOT'] .'/../tmp/'. $VarFileNameZip;
    $VarFileNameSqlPath = $_SERVER['DOCUMENT_ROOT'] .'/../tmp/'. $VarFileNameSql;
    //$VarWhere = $_SERVER['DOCUMENT_ROOT'].'/../tmp/';
    if (true === is_file($VarFileFrom)){
      move_uploaded_file($VarFileFrom, $VarFileNameZipPath);      
      $zip = new ZipArchive;
      if (true === $zip->open($VarFileNameZipPath)) {
        $VarContent =  $zip->getFromName($VarFileNameSql);
        $zip->close();
        $ArrContent = explode('---*-*-', $VarContent);
        $ArrResult = $this->RestoreToDb($ArrContent);
        $VarReturn = true;
      } 
      else {
        $VarReturn['error'] = $this->ArrMsSp->Sp_LangMessage['error']['ErrCannotOpenFile'] .' <'. $VarFileNameZip .">\n";
      }
    }
    else{
      $VarReturn['error'] = $this->ArrMsSp->Sp_LangMessage['error']['ErrCannotOpenFile'] .' <'. $VarFileNameZip .">\n";
    }
    return $VarReturn;
  }

  function RestoreToDb($ArrSql = ''){
    if (true === is_array($ArrSql) && 0 < count($ArrSql)){
      $mdb2 =& self::DbConnect($this->ArrMsSp->Sp_DbConf[$this->VarDbDsnName]['DbDsn']);
      $mdb2->setOption('escape', true);
      foreach ($ArrSql as $key => $value) {
        $value = trim($value);
        if ('' != $value){
          $ArrResult =& $mdb2->query($value);
          if (PEAR::isError($ArrResult)) {
            return array('error' => $ArrResult->getMessage());
          }
        }
      }
      $mdb2->disconnect();
      return true;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
  
  function ReIndex($ArrReIndex = array()){
    if(true === is_array($ArrReIndex) && 0 < count($ArrReIndex)){
      $mdb2 =& self::DbConnect($this->ArrMsSp->Sp_DbConf[$this->VarDbDsnName]['DbDsn']);
      foreach ($ArrReIndex as $key => $value) {
        $VarFlagOut = false;
        $i = 1;
        while (true != $VarFlagOut) {
          if(1 >= $i){
            $VarIndex = $value['index'];
          }
          else{
            $VarIndex = $value['index'] .'_'.$i;
          }
          $VarSql = 'ALTER TABLE '. $value['table'] .' DROP INDEX '. $VarIndex;
          //echo $VarSql ."<br />";
          $ArrResult = $mdb2->query($VarSql);
          if (PEAR::isError($ArrResult)) {
//            SiteLibs::GetVarDump($ArrResult);
            $VarFlagOut = true;
          }
          $i++;
        }
        $VarSql = 'ALTER TABLE '. $value['table'] .' ADD INDEX (`'. $value['index'] .'`)';
//        echo $VarSql ."<br />";
        $ArrResult = $mdb2->query($VarSql);
        if (PEAR::isError($ArrResult)) {
//          SiteLibs::GetVarDump($ArrResult);
        }
      }
      $mdb2->disconnect();
      return true;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }
}
?>