<?php
  ///////////////////////////////////////////////////
  //Домашняя библиотека,
  //обновление
  //2019 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  require_once "utils/chlang_osn.php";

  require_once "config.php";
  require_once "utils/functions.php";
  require_once "utils/top.php";

  if($result2['administrator'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}

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

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

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

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

  if(isset($_POST['dbname'])) {$dbname=trim($_POST['usr']);}
    else {$dbname="";}
     if(empty($dbname)) {puterror2(base,$TEXT['error-update4']); require_once "utils/bottom.php"; 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)
  {
    puterror2(base,$TEXT['error-conf1']); require_once "utils/bottom.php"; 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))
  {
    puterror2(base,$TEXT['error-conf2']); require_once "utils/bottom.php"; exit();
  }

  $action = "1121";
    switch($_POST['vers'])
      {
      case 1: //1.11.1-1.12.1
      $action = "1121";
      break;
      case 2: //2.0.2-2.1.2
      $action = "1181";
      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: //
      //$action = "";
      //break;
      }

if($action == "1121")
 {
   $query10 = "ALTER TABLE `db_library`.`tbl_book` 
                        ADD COLUMN `bl2_id` INT(5) NOT NULL DEFAULT '0' AFTER `bl_id`,
                        ADD COLUMN `paper_copy` ENUM('yes', 'no') NULL DEFAULT 'yes' AFTER `bl2_id`,
                        ADD COLUMN `remote_copy` ENUM('yes', 'no') NULL DEFAULT 'no' AFTER `paper_copy`,
                        ADD COLUMN `a_book` VARCHAR(255) NULL DEFAULT NULL AFTER `remote_copy`,
                        ADD COLUMN `cd_book` VARCHAR(255) NULL DEFAULT NULL AFTER `a_book`,
                        ADD INDEX `paper_ind` (`paper_copy` ASC),
                        ADD INDEX `remote_ind` (`remote_copy` ASC);";
    $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror2(base,$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}

   $query11 = "ALTER TABLE `db_library`.`tbl_statistik` 
                        CHANGE COLUMN `film_id` `film_id` BIGINT(20) NOT NULL DEFAULT '0' ;";
    $result11 = mysqli_query($dbcnx,$query11);
     if(!$result11) {puterror2(base,$TEXT['error-tbl4']); require_once "utils/bottom.php"; exit();}

    $query_pwd0 = mysqli_query($dbcnx,"ALTER USER 'library_user'@'%' IDENTIFIED WITH mysql_native_password BY 'library_user';");
    $query_pwd1 = mysqli_query($dbcnx,"ALTER USER 'library_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'library_user';");
    $query_pwd2 = mysqli_query($dbcnx,"ALTER USER 'library_admin'@'%' IDENTIFIED WITH mysql_native_password BY 'Pgt7d34smb9';");
    $query_pwd3 = mysqli_query($dbcnx,"ALTER USER 'library_admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Pgt7d34smb9';");
    $query_pwd4 = mysqli_query($dbcnx,"FLUSH PRIVILEGES;");

    $query_body = "SELECT book_id, bl_id FROM `db_library`.`tbl_book` WHERE book_id != 0";
    $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body)  {puterror2(base,$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
       $result_rows = mysqli_num_rows($result_body);
       if($result_rows > 0)
         {
           while($result_book_bl = mysqli_fetch_array($result_body))
               {
                $query_book_cicle = mysqli_query($dbcnx,"UPDATE `db_library`.`tbl_book` SET bl2_id='".$result_book_bl['bl_id']."' WHERE `book_id`='".$result_book_bl['book_id']."'");
               }
         }
  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['update-log'];
  $what = $TEXT['update-log1'];
  $who = "MySQL user: ".$_POST['usr'];
  //*************************************
    $query2 = "INSERT INTO `db_library`.`tbl_logs` (`log_id`, `data`, `ip`, `who`, `doing`, `what`) VALUES (NULL, NOW(), '$ip', '$who', '$doing', '$what')";
     $result2 = mysqli_query($dbcnx,$query2); 
    if(!$result2) {puterror2(base,$TEXT['error-log']); require_once "utils/bottom.php"; exit();}
       else {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=update1121frm.php?cycl=on' />
                   </head></html>";
            }
 }

 if($action == "1181")
  {
     $query10 = "ALTER TABLE `db_library`.`tbl_book` 
                ADD COLUMN `prev_books` BIGINT NOT NULL DEFAULT '0' AFTER `cd_book`;";
    $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror2(base,$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}

     $query11 = "ALTER TABLE `db_library`.`tbl_logins` 
                ADD COLUMN `maintenance` ENUM('yes','no') DEFAULT 'no' AFTER `chk_upd`;";
    $result11 = mysqli_query($dbcnx,$query11);
     if(!$result11) {puterror2(base,$TEXT['error-tbl7']); require_once "utils/bottom.php"; exit();}

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