<?php
  ///////////////////////////////////////////////////
  //Управление серверами DHCP,
  //обновление
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("lang/".$lang."/".$lang.".php")) {include "lang/".$lang."/".$lang.".php";}
     else {include "lang/ru/ru.php";}

  require_once "utils/top_update.php";
  require_once "utils/functions.php";

  $title=$TEXT['update-title'];
  $helppage = $TEXT['update-help'];

  if(empty($_POST['srv'])) {puterror2(base,$TEXT['error-install1']); exit();}
  if(strlen($_POST['srv'])>50) {puterror2(base,$TEXT['error-install2']); exit();}
  if(empty($_POST['usr'])) {puterror2(base,$TEXT['error-install3']); exit();}
  if(strlen($_POST['usr'])>50) {puterror2(base,$TEXT['error-install4']); exit();}
  if(strlen($_POST['pswd'])>100) {puterror2(base,$TEXT['error-install5']); exit();}
  if(empty($_POST['dbname'])) {puterror2(base,$TEXT['error-update1']); exit();}
  if(strlen($_POST['dbname'])>50) {puterror2(base,$TEXT['error-update2']); exit();}
  if(strlen($_POST['vers'])>10) {puterror2(base,$TEXT['error-update3']); exit();}

  if(isset($_POST['srv'])) {$srv=trim($_POST['srv']);}
    else {$srv="";}
     if(empty($srv)) {puterror2(base,$TEXT['error-install6']); exit();}

  if(isset($_POST['usr'])) {$usr=trim($_POST['usr']);}
    else {$usr="";}
     if (empty($usr)) {puterror2(base,$TEXT['error-install7']); exit();}

  if(isset($_POST['dbname'])) {$dbname=trim($_POST['usr']);}
    else {$dbname="";}
     if(empty($dbname)) {puterror2(base,$TEXT['error-update4']); exit();}

  $_POST['srv'] = rep($_POST['srv']);
  $_POST['usr'] = rep($_POST['usr']);
  $_POST['dbname'] = rep($_POST['dbname']);
  $_POST['vers'] = rep($_POST['vers']);

  ///////////////////////////////////////////////////
  // Подключение к базе данных
  ///////////////////////////////////////////////////

  $dblocation = $_POST['srv'];
  $dbport = "3306";
  $dbname = $_POST['dbname'];
  $dbuser = $_POST['usr'];
  $dbpasswd = $_POST['pswd'];

  $dbcnx = mysqli_connect($dblocation,$dbuser,$dbpasswd);
  if(!$dbcnx)
  {
    echo( "".$TEXT['error-conf1']."" );
    exit();
  }
  mysqli_query($dbcnx,"SET CHARACTER SET 'utf8'") or exit('SET CHARACTER SET error');
  mysqli_query($dbcnx,"SET character_set_client='utf8'") or exit('SET character_set_client error');
  mysqli_query($dbcnx,"SET character_set_results='utf8'") or exit('SET character_set_results error');
  mysqli_query($dbcnx,"SET collation_connection='utf8_general_ci'") or exit('SET collation_connection error');
  mysqli_query($dbcnx,"SET NAMES `utf8`") or exit('SET NAMES error');
  mysqli_query($dbcnx,"SET SQL_MODE=''") or exit('SET SQL_MODE error');
  if(!mysqli_select_db($dbcnx,$dbname))
  {
    echo( "".$TEXT['error-conf2']."" );
    exit();
  }

  $action = "1122";
    switch($_POST['vers'])
      {
      case 1: //1.11.2-1.12.2
      $action = "1122";
      break;
      case 2: //2.0.2-2.1.2
      $action = "212";
      break;
      case 3: //2.1.2-2.2.2
      $action = "222";
      break;
      case 4: //2.2.2-2.3.2
      $action = "232";
      break;
      case 5: //2.7.2-2.8.2
      $action = "282";
      break;
      }

if($action == "1122")
 {
  $query21 = "CREATE TABLE tbl_house(
                         `house_id` int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         `house` varchar(255),
                         UNIQUE KEY `uc_house` (`house`)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result21 = mysqli_query($dbcnx,$query21);
     if(!$result21) {puterror2(base,$TEXT['error-tbl10']); exit();}

  $query3 = "CREATE TABLE tbl_etazh(
                         `etazh_id` int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         `etazh` varchar(20),
                         UNIQUE KEY `uc_etazh` (`etazh`)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result3 = mysqli_query($dbcnx,$query3);
     if(!$result3) {puterror2(base,$TEXT['error-tbl11']); exit();}

   $query4 = "CREATE TABLE tbl_oborud(
                         `obor_id` int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         `oborud` varchar(255),
                         UNIQUE KEY `uc_oborud` (`oborud`)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result4 = mysqli_query($dbcnx,$query4);
     if(!$result4) {puterror2(base,$TEXT['error-tbl12']); exit();}

   $query5 = "CREATE TABLE tbl_podset(
                         `pods_id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         `srv_id` int(5),
                         `house_id` int(5),
                         `etazh_id` int(5),
                         `obor_id` int(5),
                         `podset` varchar(15),
                         `prefix` varchar(255),
                         `begin_subn` varchar(255),
                         `end_subn` varchar(255),
                         `opthstnm` enum('0','1')
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result5 = mysqli_query($dbcnx,$query5);
     if(!$result5) {puterror2(base,$TEXT['error-tbl13']); exit();}

   $query6 = "ALTER TABLE `tbl_logins` ADD COLUMN `fltr_size` INT(4)  NOT NULL DEFAULT 100 AFTER `dom_id`;";
    $result6 = mysqli_query($dbcnx,$query6);
     if(!$result6) {puterror2(base,$TEXT['error-tbl4']); exit();}

   $query7 = "ALTER TABLE `tbl_srv` ADD COLUMN `end_opt_ons` VARCHAR(255) NOT NULL AFTER `shablon`;";
    $result7 = mysqli_query($dbcnx,$query7);
     if(!$result7) {puterror2(base,$TEXT['error-tbl1']); exit();}

   $query8 = "CREATE TABLE tbl_srvparam(
                         `sp_id` int(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         `sams` enum('0','1'),
                         `usrpass` int(5)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;";
    $result8 = mysqli_query($dbcnx,$query8);
     if(!$result8) {puterror($TEXT['error-tbl14']);}

    $query_dump = mysqli_query($dbcnx,"INSERT INTO `tbl_srvparam` VALUES (1,'0',50);");

  $query10 = "FLUSH PRIVILEGES;";
    $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror2(base,$TEXT['error-install17']); exit();}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log1'];
  $who = "MySQL user: ".$_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php' />
                   </head></html>";
           } else {puterror2(base,$TEXT['error-log']); exit();}
 }

 if($action == "212")
 {
  $query22 = "ALTER TABLE `db_manydhcp`.`tbl_logins` ADD COLUMN `chk_upd` ENUM('yes','no') NOT NULL DEFAULT 'no' AFTER `fltr_size`;";
    $result22 = mysqli_query($dbcnx,$query22);
     if(!$result22) {puterror2(base,$TEXT['error-tbl4']); exit();}

  $query3 = "DROP TABLE IF EXISTS `db_manydhcp`.`tbl_start`;";
    $result3 = mysqli_query($dbcnx,$query3);
     if(!$result3) {puterror2(base,$TEXT['error-tbl6']); exit();}

   $query4 = "ALTER TABLE `db_manydhcp`.`tbl_podset`
                     ADD INDEX `srv_id_ind`(`srv_id`),
                     ADD INDEX `house_id_ind`(`house_id`),
                     ADD INDEX `etazh_id_ind`(`etazh_id`),
                     ADD INDEX `obor_id_ind`(`obor_id`);";
    $result4 = mysqli_query($dbcnx,$query4);
     if(!$result4) {puterror2(base,$TEXT['error-tbl13']); exit();}

  $query10 = "FLUSH PRIVILEGES;";
    $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror2(base,$TEXT['error-install17']); exit();}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log212'];
  $who = "MySQL user: ".$_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php' />
                   </head></html>";
           } else {puterror2(base,$TEXT['error-log']); exit();}
 }

 if($action == "222")
 {
  $query23 = "ALTER TABLE `db_manydhcp`.`tbl_ip` ADD COLUMN `pods_id` int NOT NULL DEFAULT 0 AFTER `last_acc`;";
    $result23 = mysqli_query($dbcnx,$query23);
     if(!$result23) {puterror2(base,$TEXT['error-tbl2']); exit();}

  $query10 = "FLUSH PRIVILEGES;";
    $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror2(base,$TEXT['error-install17']); exit();}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log222'];
  $who = "MySQL user: ".$_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php' />
                   </head></html>";
           } else {puterror2(base,$TEXT['error-log']); exit();}
 }

 if($action == "232")
 {
  $query24 = "ALTER TABLE `db_manydhcp`.`tbl_srv` ADD COLUMN `ping_ok` ENUM('yes','no') NOT NULL DEFAULT 'yes' AFTER `end_opt_ons`;";
    $result24 = mysqli_query($dbcnx,$query24);
     if(!$result24) {puterror2(base,$TEXT['error-tbl1']); exit();}

  $query10 = "FLUSH PRIVILEGES;";
    $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror2(base,$TEXT['error-install17']); exit();}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log232'];
  $who = "MySQL user: ".$_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php' />
                   </head></html>";
           } else {puterror2(base,$TEXT['error-log']); exit();}
 }
           
 if($action == "282")
 {
  $query24 = "ALTER TABLE `db_manydhcp`.`tbl_logins` ADD COLUMN `maintenance` ENUM('yes','no') DEFAULT 'no' AFTER `chk_upd`;";
    $result24 = mysqli_query($dbcnx,$query24);
     if(!$result24) {puterror2(base,$TEXT['error-tbl4']); exit();}

  $query25 = "ALTER TABLE `db_manydhcp`.`tbl_upr` ADD COLUMN `locpath` VARCHAR(255) NOT NULL DEFAULT '/var/www/html/manydhcp/temp' AFTER `vkl_upr`;";
    $result25 = mysqli_query($dbcnx,$query25);
     if(!$result25) {puterror2(base,$TEXT['error-tbl4']); exit();}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log282'];
  $who = "MySQL user: ".$_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php' />
                   </head></html>";
           } else {puterror2(base,$TEXT['error-log']); exit();}
 } else {puterror2($TEXT['error-update3']);}
?>